blob: 1aaa196ba5c0d0978279a1333e94006e672dfa3c [file] [log] [blame]
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -08001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <sys/cdefs.h>
20#include <sys/types.h>
21#include <map>
mtk1603653f79e62019-05-31 19:05:22 +080022#include <mutex>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080023#include <string>
24#include <vector>
25
26#include <android-base/unique_fd.h>
27#include <cgroup_map.h>
28
29class ProfileAttribute {
30 public:
Yifan Hong53e0deb2019-03-22 17:01:08 -070031 ProfileAttribute(const CgroupController& controller, const std::string& file_name)
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080032 : controller_(controller), file_name_(file_name) {}
33
Yifan Hong53e0deb2019-03-22 17:01:08 -070034 const CgroupController* controller() const { return &controller_; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080035 const std::string& file_name() const { return file_name_; }
Suren Baghdasaryan81b9f0b2020-07-01 12:34:17 -070036 void Reset(const CgroupController& controller, const std::string& file_name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080037
38 bool GetPathForTask(int tid, std::string* path) const;
39
40 private:
Yifan Hong53e0deb2019-03-22 17:01:08 -070041 CgroupController controller_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080042 std::string file_name_;
43};
44
45// Abstract profile element
46class ProfileAction {
47 public:
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080048 enum ResourceCacheType { RCT_TASK = 0, RCT_PROCESS, RCT_COUNT };
49
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080050 virtual ~ProfileAction() {}
51
52 // Default implementations will fail
Greg Kaiser5c5ed9c2019-02-04 06:33:26 -080053 virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; };
54 virtual bool ExecuteForTask(int) const { return false; };
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080055
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080056 virtual void EnableResourceCaching(ResourceCacheType) {}
57 virtual void DropResourceCaching(ResourceCacheType) {}
58
59 protected:
60 enum CacheUseResult { SUCCESS, FAIL, UNUSED };
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080061};
62
63// Profile actions
64class SetClampsAction : public ProfileAction {
65 public:
66 SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
67
68 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
69 virtual bool ExecuteForTask(int tid) const;
70
71 protected:
72 int boost_;
73 int clamp_;
74};
75
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080076// To avoid issues in sdk_mac build
77#if defined(__ANDROID__)
78
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080079class SetTimerSlackAction : public ProfileAction {
80 public:
81 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
82
83 virtual bool ExecuteForTask(int tid) const;
84
85 private:
86 unsigned long slack_;
87
88 static bool IsTimerSlackSupported(int tid);
89};
90
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080091#else
92
93class SetTimerSlackAction : public ProfileAction {
94 public:
95 SetTimerSlackAction(unsigned long) noexcept {}
96
97 virtual bool ExecuteForTask(int) const { return true; }
98};
99
100#endif
101
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800102// Set attribute profile element
103class SetAttributeAction : public ProfileAction {
104 public:
105 SetAttributeAction(const ProfileAttribute* attribute, const std::string& value)
106 : attribute_(attribute), value_(value) {}
107
108 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
109 virtual bool ExecuteForTask(int tid) const;
110
111 private:
112 const ProfileAttribute* attribute_;
113 std::string value_;
114};
115
Rick Yiud4c53512021-11-21 15:57:36 +0800116// Set cgroup profile element
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800117class SetCgroupAction : public ProfileAction {
Rick Yiubc1ad962020-10-26 20:32:52 +0800118 public:
Rick Yiud4c53512021-11-21 15:57:36 +0800119 SetCgroupAction(const CgroupController& c, const std::string& p);
Rick Yiubc1ad962020-10-26 20:32:52 +0800120
121 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
122 virtual bool ExecuteForTask(int tid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800123 virtual void EnableResourceCaching(ResourceCacheType cache_type);
124 virtual void DropResourceCaching(ResourceCacheType cache_type);
Rick Yiubc1ad962020-10-26 20:32:52 +0800125
Rick Yiud4c53512021-11-21 15:57:36 +0800126 const CgroupController* controller() const { return &controller_; }
127
Rick Yiubc1ad962020-10-26 20:32:52 +0800128 private:
Rick Yiud4c53512021-11-21 15:57:36 +0800129 CgroupController controller_;
130 std::string path_;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800131 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800132 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800133
134 static bool AddTidToCgroup(int tid, int fd, const char* controller_name);
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800135 CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const;
Rick Yiud4c53512021-11-21 15:57:36 +0800136};
137
138// Write to file action
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800139class WriteFileAction : public ProfileAction {
Rick Yiud4c53512021-11-21 15:57:36 +0800140 public:
141 WriteFileAction(const std::string& path, const std::string& value, bool logfailures);
142
143 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
144 virtual bool ExecuteForTask(int tid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800145 virtual void EnableResourceCaching(ResourceCacheType cache_type);
146 virtual void DropResourceCaching(ResourceCacheType cache_type);
Rick Yiud4c53512021-11-21 15:57:36 +0800147
148 private:
149 std::string path_, value_;
Rick Yiud76053a2021-01-25 12:44:45 +0800150 bool logfailures_;
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800151 android::base::unique_fd fd_;
152 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800153
154 static bool WriteValueToFile(const std::string& value, const std::string& path,
155 bool logfailures);
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800156 CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const;
Rick Yiubc1ad962020-10-26 20:32:52 +0800157};
158
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800159class TaskProfile {
160 public:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800161 TaskProfile() : res_cached_(false) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800162
163 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800164 void MoveTo(TaskProfile* profile);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800165
166 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
167 bool ExecuteForTask(int tid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800168 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
169 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800170
171 private:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800172 bool res_cached_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800173 std::vector<std::unique_ptr<ProfileAction>> elements_;
174};
175
Rick Yiu0b211fa2019-09-16 19:07:17 +0800176// Set aggregate profile element
177class ApplyProfileAction : public ProfileAction {
178 public:
179 ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles)
180 : profiles_(profiles) {}
181
182 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
183 virtual bool ExecuteForTask(int tid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800184 virtual void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
185 virtual void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);
Rick Yiu0b211fa2019-09-16 19:07:17 +0800186
187 private:
188 std::vector<std::shared_ptr<TaskProfile>> profiles_;
189};
190
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800191class TaskProfiles {
192 public:
193 // Should be used by all users
194 static TaskProfiles& GetInstance();
195
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800196 TaskProfile* GetProfile(const std::string& name) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800197 const ProfileAttribute* GetAttribute(const std::string& name) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800198 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const;
199 bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles,
200 bool use_fd_cache);
Rick Yiu0b211fa2019-09-16 19:07:17 +0800201 bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800202
203 private:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800204 std::map<std::string, std::shared_ptr<TaskProfile>> profiles_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800205 std::map<std::string, std::unique_ptr<ProfileAttribute>> attributes_;
206
207 TaskProfiles();
208
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800209 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800210};