blob: ac8918e650fcde2f4a88706e3c9ffa3a14e6eb44 [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>
Bart Van Asschef32c4ec2022-08-02 13:18:12 -070021#include <functional>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080022#include <map>
mtk1603653f79e62019-05-31 19:05:22 +080023#include <mutex>
Bart Van Asschef32c4ec2022-08-02 13:18:12 -070024#include <span>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080025#include <string>
Bart Van Assched0b8ce22022-08-02 13:06:26 -070026#include <string_view>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080027#include <vector>
28
29#include <android-base/unique_fd.h>
30#include <cgroup_map.h>
31
Bart Van Assche4c99e962022-02-03 19:50:16 +000032class IProfileAttribute {
33 public:
34 virtual ~IProfileAttribute() = 0;
Suren Baghdasaryan35078462023-07-25 14:50:18 -070035 virtual void Reset(const CgroupController& controller, const std::string& file_name,
36 const std::string& file_v2_name) = 0;
Bart Van Assche4c99e962022-02-03 19:50:16 +000037 virtual const CgroupController* controller() const = 0;
38 virtual const std::string& file_name() const = 0;
39 virtual bool GetPathForTask(int tid, std::string* path) const = 0;
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +000040 virtual bool GetPathForUID(uid_t uid, std::string* path) const = 0;
Bart Van Assche4c99e962022-02-03 19:50:16 +000041};
42
43class ProfileAttribute : public IProfileAttribute {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080044 public:
Bart Van Asschebc077ff2022-02-17 01:26:44 +000045 // Cgroup attributes may have different names in the v1 and v2 hierarchies. If `file_v2_name` is
46 // not empty, `file_name` is the name for the v1 hierarchy and `file_v2_name` is the name for
47 // the v2 hierarchy. If `file_v2_name` is empty, `file_name` is used for both hierarchies.
48 ProfileAttribute(const CgroupController& controller, const std::string& file_name,
49 const std::string& file_v2_name)
50 : controller_(controller), file_name_(file_name), file_v2_name_(file_v2_name) {}
Bart Van Assche4c99e962022-02-03 19:50:16 +000051 ~ProfileAttribute() = default;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080052
Bart Van Assche4c99e962022-02-03 19:50:16 +000053 const CgroupController* controller() const override { return &controller_; }
Suren Baghdasaryan35078462023-07-25 14:50:18 -070054 const std::string& file_name() const override;
55 void Reset(const CgroupController& controller, const std::string& file_name,
56 const std::string& file_v2_name) override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080057
Bart Van Assche4c99e962022-02-03 19:50:16 +000058 bool GetPathForTask(int tid, std::string* path) const override;
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +000059 bool GetPathForUID(uid_t uid, std::string* path) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080060
61 private:
Yifan Hong53e0deb2019-03-22 17:01:08 -070062 CgroupController controller_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080063 std::string file_name_;
Bart Van Asschebc077ff2022-02-17 01:26:44 +000064 std::string file_v2_name_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080065};
66
67// Abstract profile element
68class ProfileAction {
69 public:
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080070 enum ResourceCacheType { RCT_TASK = 0, RCT_PROCESS, RCT_COUNT };
71
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080072 virtual ~ProfileAction() {}
73
Bart Van Asschef096bd22022-01-24 19:59:13 +000074 virtual const char* Name() const = 0;
75
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080076 // Default implementations will fail
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +000077 virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; }
78 virtual bool ExecuteForTask(int) const { return false; }
79 virtual bool ExecuteForUID(uid_t) const { return false; }
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080080
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080081 virtual void EnableResourceCaching(ResourceCacheType) {}
82 virtual void DropResourceCaching(ResourceCacheType) {}
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +000083 virtual bool IsValidForProcess(uid_t uid, pid_t pid) const { return false; }
84 virtual bool IsValidForTask(int tid) const { return false; }
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080085
86 protected:
87 enum CacheUseResult { SUCCESS, FAIL, UNUSED };
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080088};
89
90// Profile actions
91class SetClampsAction : public ProfileAction {
92 public:
93 SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
94
Bart Van Asschef096bd22022-01-24 19:59:13 +000095 const char* Name() const override { return "SetClamps"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +000096 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
97 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080098
99 protected:
100 int boost_;
101 int clamp_;
102};
103
104class SetTimerSlackAction : public ProfileAction {
105 public:
106 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
107
Bart Van Asschef096bd22022-01-24 19:59:13 +0000108 const char* Name() const override { return "SetTimerSlack"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000109 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000110 bool IsValidForProcess(uid_t uid, pid_t pid) const override { return true; }
111 bool IsValidForTask(int tid) const override { return true; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800112
113 private:
114 unsigned long slack_;
115
116 static bool IsTimerSlackSupported(int tid);
117};
118
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800119// Set attribute profile element
120class SetAttributeAction : public ProfileAction {
121 public:
Bart Van Assche59af6802022-01-24 21:08:57 +0000122 SetAttributeAction(const IProfileAttribute* attribute, const std::string& value, bool optional)
123 : attribute_(attribute), value_(value), optional_(optional) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800124
Bart Van Asschef096bd22022-01-24 19:59:13 +0000125 const char* Name() const override { return "SetAttribute"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000126 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
127 bool ExecuteForTask(int tid) const override;
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +0000128 bool ExecuteForUID(uid_t uid) const override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000129 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
130 bool IsValidForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800131
132 private:
Bart Van Assche4c99e962022-02-03 19:50:16 +0000133 const IProfileAttribute* attribute_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800134 std::string value_;
Bart Van Assche59af6802022-01-24 21:08:57 +0000135 bool optional_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800136};
137
Rick Yiud4c53512021-11-21 15:57:36 +0800138// Set cgroup profile element
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800139class SetCgroupAction : public ProfileAction {
Rick Yiubc1ad962020-10-26 20:32:52 +0800140 public:
Rick Yiud4c53512021-11-21 15:57:36 +0800141 SetCgroupAction(const CgroupController& c, const std::string& p);
Rick Yiubc1ad962020-10-26 20:32:52 +0800142
Bart Van Asschef096bd22022-01-24 19:59:13 +0000143 const char* Name() const override { return "SetCgroup"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000144 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
145 bool ExecuteForTask(int tid) const override;
146 void EnableResourceCaching(ResourceCacheType cache_type) override;
147 void DropResourceCaching(ResourceCacheType cache_type) override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000148 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
149 bool IsValidForTask(int tid) const override;
Rick Yiubc1ad962020-10-26 20:32:52 +0800150
Rick Yiud4c53512021-11-21 15:57:36 +0800151 const CgroupController* controller() const { return &controller_; }
152
Rick Yiubc1ad962020-10-26 20:32:52 +0800153 private:
Rick Yiud4c53512021-11-21 15:57:36 +0800154 CgroupController controller_;
155 std::string path_;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800156 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800157 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800158
159 static bool AddTidToCgroup(int tid, int fd, const char* controller_name);
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800160 CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const;
Rick Yiud4c53512021-11-21 15:57:36 +0800161};
162
163// Write to file action
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800164class WriteFileAction : public ProfileAction {
Rick Yiud4c53512021-11-21 15:57:36 +0800165 public:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800166 WriteFileAction(const std::string& task_path, const std::string& proc_path,
167 const std::string& value, bool logfailures);
Rick Yiud4c53512021-11-21 15:57:36 +0800168
Bart Van Asschef096bd22022-01-24 19:59:13 +0000169 const char* Name() const override { return "WriteFile"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000170 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
171 bool ExecuteForTask(int tid) const override;
172 void EnableResourceCaching(ResourceCacheType cache_type) override;
173 void DropResourceCaching(ResourceCacheType cache_type) override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000174 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
175 bool IsValidForTask(int tid) const override;
Rick Yiud4c53512021-11-21 15:57:36 +0800176
177 private:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800178 std::string task_path_, proc_path_, value_;
Rick Yiud76053a2021-01-25 12:44:45 +0800179 bool logfailures_;
Rick Yiu9221b1e2022-02-10 16:44:43 +0800180 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800181 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800182
Rick Yiu9221b1e2022-02-10 16:44:43 +0800183 bool WriteValueToFile(const std::string& value, ResourceCacheType cache_type, int uid, int pid,
184 bool logfailures) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800185 CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const;
Rick Yiubc1ad962020-10-26 20:32:52 +0800186};
187
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800188class TaskProfile {
189 public:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000190 TaskProfile(const std::string& name) : name_(name), res_cached_(false) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800191
Bart Van Asschef096bd22022-01-24 19:59:13 +0000192 const std::string& Name() const { return name_; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800193 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800194 void MoveTo(TaskProfile* profile);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800195
196 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
197 bool ExecuteForTask(int tid) const;
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +0000198 bool ExecuteForUID(uid_t uid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800199 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
200 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000201 bool IsValidForProcess(uid_t uid, pid_t pid) const;
202 bool IsValidForTask(int tid) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800203
204 private:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000205 const std::string name_;
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800206 bool res_cached_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800207 std::vector<std::unique_ptr<ProfileAction>> elements_;
208};
209
Rick Yiu0b211fa2019-09-16 19:07:17 +0800210// Set aggregate profile element
211class ApplyProfileAction : public ProfileAction {
212 public:
213 ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles)
214 : profiles_(profiles) {}
215
Bart Van Asschef096bd22022-01-24 19:59:13 +0000216 const char* Name() const override { return "ApplyProfileAction"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000217 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
218 bool ExecuteForTask(int tid) const override;
219 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
220 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000221 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
222 bool IsValidForTask(int tid) const override;
Rick Yiu0b211fa2019-09-16 19:07:17 +0800223
224 private:
225 std::vector<std::shared_ptr<TaskProfile>> profiles_;
226};
227
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800228class TaskProfiles {
229 public:
230 // Should be used by all users
231 static TaskProfiles& GetInstance();
232
Bart Van Assched0b8ce22022-08-02 13:06:26 -0700233 TaskProfile* GetProfile(std::string_view name) const;
234 const IProfileAttribute* GetAttribute(std::string_view name) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800235 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const;
Bart Van Asschef32c4ec2022-08-02 13:18:12 -0700236 template <typename T>
237 bool SetProcessProfiles(uid_t uid, pid_t pid, std::span<const T> profiles, bool use_fd_cache);
238 template <typename T>
239 bool SetTaskProfiles(int tid, std::span<const T> profiles, bool use_fd_cache);
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +0000240 template <typename T>
241 bool SetUserProfiles(uid_t uid, std::span<const T> profiles, bool use_fd_cache);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800242
243 private:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800244 TaskProfiles();
245
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800246 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Bart Van Assched0b8ce22022-08-02 13:06:26 -0700247
248 std::map<std::string, std::shared_ptr<TaskProfile>, std::less<>> profiles_;
249 std::map<std::string, std::unique_ptr<IProfileAttribute>, std::less<>> attributes_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800250};