blob: ea948b58ed1440b998a26409a2aecbae5693926d [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
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080019#include <sys/types.h>
T.J. Mercier54bfde02024-06-04 23:25:29 +000020
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080021#include <map>
T.J. Mercier54bfde02024-06-04 23:25:29 +000022#include <memory>
mtk1603653f79e62019-05-31 19:05:22 +080023#include <mutex>
T.J. Mercier39846112024-10-09 22:40:26 +000024#include <optional>
Bart Van Asschef32c4ec2022-08-02 13:18:12 -070025#include <span>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080026#include <string>
Bart Van Assched0b8ce22022-08-02 13:06:26 -070027#include <string_view>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080028#include <vector>
29
30#include <android-base/unique_fd.h>
31#include <cgroup_map.h>
32
Bart Van Assche4c99e962022-02-03 19:50:16 +000033class IProfileAttribute {
34 public:
35 virtual ~IProfileAttribute() = 0;
T.J. Mercierfcb86662024-08-01 20:52:30 +000036 virtual void Reset(const CgroupControllerWrapper& controller, const std::string& file_name,
Suren Baghdasaryan35078462023-07-25 14:50:18 -070037 const std::string& file_v2_name) = 0;
T.J. Mercierfcb86662024-08-01 20:52:30 +000038 virtual const CgroupControllerWrapper* controller() const = 0;
Bart Van Assche4c99e962022-02-03 19:50:16 +000039 virtual const std::string& file_name() const = 0;
Suren Baghdasaryan34837982023-07-25 15:45:45 -070040 virtual bool GetPathForProcess(uid_t uid, pid_t pid, std::string* path) const = 0;
T.J. Mercier1c007992024-01-25 16:29:54 +000041 virtual bool GetPathForTask(pid_t tid, std::string* path) const = 0;
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +000042 virtual bool GetPathForUID(uid_t uid, std::string* path) const = 0;
Bart Van Assche4c99e962022-02-03 19:50:16 +000043};
44
45class ProfileAttribute : public IProfileAttribute {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080046 public:
Bart Van Asschebc077ff2022-02-17 01:26:44 +000047 // Cgroup attributes may have different names in the v1 and v2 hierarchies. If `file_v2_name` is
48 // not empty, `file_name` is the name for the v1 hierarchy and `file_v2_name` is the name for
49 // the v2 hierarchy. If `file_v2_name` is empty, `file_name` is used for both hierarchies.
T.J. Mercierfcb86662024-08-01 20:52:30 +000050 ProfileAttribute(const CgroupControllerWrapper& controller, const std::string& file_name,
Bart Van Asschebc077ff2022-02-17 01:26:44 +000051 const std::string& file_v2_name)
52 : controller_(controller), file_name_(file_name), file_v2_name_(file_v2_name) {}
Bart Van Assche4c99e962022-02-03 19:50:16 +000053 ~ProfileAttribute() = default;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080054
T.J. Mercierfcb86662024-08-01 20:52:30 +000055 const CgroupControllerWrapper* controller() const override { return &controller_; }
Suren Baghdasaryan35078462023-07-25 14:50:18 -070056 const std::string& file_name() const override;
T.J. Mercierfcb86662024-08-01 20:52:30 +000057 void Reset(const CgroupControllerWrapper& controller, const std::string& file_name,
Suren Baghdasaryan35078462023-07-25 14:50:18 -070058 const std::string& file_v2_name) override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080059
Suren Baghdasaryan34837982023-07-25 15:45:45 -070060 bool GetPathForProcess(uid_t uid, pid_t pid, std::string* path) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +000061 bool GetPathForTask(pid_t tid, std::string* path) const override;
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +000062 bool GetPathForUID(uid_t uid, std::string* path) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080063
64 private:
T.J. Mercierfcb86662024-08-01 20:52:30 +000065 CgroupControllerWrapper controller_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080066 std::string file_name_;
Bart Van Asschebc077ff2022-02-17 01:26:44 +000067 std::string file_v2_name_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080068};
69
70// Abstract profile element
71class ProfileAction {
72 public:
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080073 enum ResourceCacheType { RCT_TASK = 0, RCT_PROCESS, RCT_COUNT };
74
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080075 virtual ~ProfileAction() {}
76
Bart Van Asschef096bd22022-01-24 19:59:13 +000077 virtual const char* Name() const = 0;
78
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080079 // Default implementations will fail
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +000080 virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; }
T.J. Mercierb53eb9d2024-10-09 22:46:33 +000081 virtual bool ExecuteForTask(pid_t) const { return false; }
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +000082 virtual bool ExecuteForUID(uid_t) const { return false; }
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080083
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080084 virtual void EnableResourceCaching(ResourceCacheType) {}
85 virtual void DropResourceCaching(ResourceCacheType) {}
T.J. Mercier44eb7052024-03-27 23:40:43 +000086 virtual bool IsValidForProcess(uid_t, pid_t) const { return false; }
87 virtual bool IsValidForTask(pid_t) const { return false; }
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080088
89 protected:
90 enum CacheUseResult { SUCCESS, FAIL, UNUSED };
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080091};
92
93// Profile actions
94class SetClampsAction : public ProfileAction {
95 public:
96 SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
97
Bart Van Asschef096bd22022-01-24 19:59:13 +000098 const char* Name() const override { return "SetClamps"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +000099 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000100 bool ExecuteForTask(pid_t tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800101
102 protected:
103 int boost_;
104 int clamp_;
105};
106
107class SetTimerSlackAction : public ProfileAction {
108 public:
109 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
110
Bart Van Asschef096bd22022-01-24 19:59:13 +0000111 const char* Name() const override { return "SetTimerSlack"; }
T.J. Mercier1c007992024-01-25 16:29:54 +0000112 bool ExecuteForTask(pid_t tid) const override;
T.J. Mercier44eb7052024-03-27 23:40:43 +0000113 bool IsValidForProcess(uid_t, pid_t) const override { return true; }
114 bool IsValidForTask(pid_t) const override { return true; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800115
116 private:
117 unsigned long slack_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800118};
119
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800120// Set attribute profile element
121class SetAttributeAction : public ProfileAction {
122 public:
Bart Van Assche59af6802022-01-24 21:08:57 +0000123 SetAttributeAction(const IProfileAttribute* attribute, const std::string& value, bool optional)
124 : attribute_(attribute), value_(value), optional_(optional) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800125
Bart Van Asschef096bd22022-01-24 19:59:13 +0000126 const char* Name() const override { return "SetAttribute"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000127 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000128 bool ExecuteForTask(pid_t tid) const override;
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +0000129 bool ExecuteForUID(uid_t uid) const override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000130 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000131 bool IsValidForTask(pid_t tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800132
133 private:
Bart Van Assche4c99e962022-02-03 19:50:16 +0000134 const IProfileAttribute* attribute_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800135 std::string value_;
Bart Van Assche59af6802022-01-24 21:08:57 +0000136 bool optional_;
Suren Baghdasaryan34837982023-07-25 15:45:45 -0700137
138 bool WriteValueToFile(const std::string& path) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800139};
140
Rick Yiud4c53512021-11-21 15:57:36 +0800141// Set cgroup profile element
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800142class SetCgroupAction : public ProfileAction {
Rick Yiubc1ad962020-10-26 20:32:52 +0800143 public:
T.J. Mercierfcb86662024-08-01 20:52:30 +0000144 SetCgroupAction(const CgroupControllerWrapper& c, const std::string& p);
Rick Yiubc1ad962020-10-26 20:32:52 +0800145
Bart Van Asschef096bd22022-01-24 19:59:13 +0000146 const char* Name() const override { return "SetCgroup"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000147 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000148 bool ExecuteForTask(pid_t tid) const override;
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000149 void EnableResourceCaching(ResourceCacheType cache_type) override;
150 void DropResourceCaching(ResourceCacheType cache_type) override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000151 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000152 bool IsValidForTask(pid_t tid) const override;
Rick Yiubc1ad962020-10-26 20:32:52 +0800153
T.J. Mercierfcb86662024-08-01 20:52:30 +0000154 const CgroupControllerWrapper* controller() const { return &controller_; }
Rick Yiud4c53512021-11-21 15:57:36 +0800155
Rick Yiubc1ad962020-10-26 20:32:52 +0800156 private:
T.J. Mercierfcb86662024-08-01 20:52:30 +0000157 CgroupControllerWrapper controller_;
Rick Yiud4c53512021-11-21 15:57:36 +0800158 std::string path_;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800159 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800160 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800161
T.J. Mercier1c007992024-01-25 16:29:54 +0000162 bool AddTidToCgroup(pid_t tid, int fd, ResourceCacheType cache_type) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800163 CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const;
Rick Yiud4c53512021-11-21 15:57:36 +0800164};
165
166// Write to file action
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800167class WriteFileAction : public ProfileAction {
Rick Yiud4c53512021-11-21 15:57:36 +0800168 public:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800169 WriteFileAction(const std::string& task_path, const std::string& proc_path,
170 const std::string& value, bool logfailures);
Rick Yiud4c53512021-11-21 15:57:36 +0800171
Bart Van Asschef096bd22022-01-24 19:59:13 +0000172 const char* Name() const override { return "WriteFile"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000173 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000174 bool ExecuteForTask(pid_t tid) const override;
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000175 void EnableResourceCaching(ResourceCacheType cache_type) override;
176 void DropResourceCaching(ResourceCacheType cache_type) override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000177 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000178 bool IsValidForTask(pid_t tid) const override;
Rick Yiud4c53512021-11-21 15:57:36 +0800179
180 private:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800181 std::string task_path_, proc_path_, value_;
Rick Yiud76053a2021-01-25 12:44:45 +0800182 bool logfailures_;
Rick Yiu9221b1e2022-02-10 16:44:43 +0800183 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800184 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800185
T.J. Merciera757eb82024-01-24 22:45:26 +0000186 bool WriteValueToFile(const std::string& value, ResourceCacheType cache_type, uid_t uid,
T.J. Mercierd6fb2252024-01-24 23:42:39 +0000187 pid_t pid, bool logfailures) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800188 CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const;
Rick Yiubc1ad962020-10-26 20:32:52 +0800189};
190
T.J. Mercier39846112024-10-09 22:40:26 +0000191// Set scheduler policy action
192class SetSchedulerPolicyAction : public ProfileAction {
193 public:
194 SetSchedulerPolicyAction(int policy)
195 : policy_(policy) {}
196 SetSchedulerPolicyAction(int policy, int priority_or_nice)
197 : policy_(policy), priority_or_nice_(priority_or_nice) {}
198
199 const char* Name() const override { return "SetSchedulerPolicy"; }
200 bool ExecuteForTask(pid_t tid) const override;
201
202 static bool isNormalPolicy(int policy);
203 static bool toPriority(int policy, int virtual_priority, int& priority_out);
204
205 private:
206 int policy_;
207 std::optional<int> priority_or_nice_;
208};
209
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800210class TaskProfile {
211 public:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000212 TaskProfile(const std::string& name) : name_(name), res_cached_(false) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800213
Bart Van Asschef096bd22022-01-24 19:59:13 +0000214 const std::string& Name() const { return name_; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800215 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800216 void MoveTo(TaskProfile* profile);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800217
218 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
T.J. Mercier1c007992024-01-25 16:29:54 +0000219 bool ExecuteForTask(pid_t tid) const;
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +0000220 bool ExecuteForUID(uid_t uid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800221 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
222 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000223 bool IsValidForProcess(uid_t uid, pid_t pid) const;
T.J. Mercier1c007992024-01-25 16:29:54 +0000224 bool IsValidForTask(pid_t tid) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800225
226 private:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000227 const std::string name_;
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800228 bool res_cached_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800229 std::vector<std::unique_ptr<ProfileAction>> elements_;
230};
231
Rick Yiu0b211fa2019-09-16 19:07:17 +0800232// Set aggregate profile element
233class ApplyProfileAction : public ProfileAction {
234 public:
235 ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles)
236 : profiles_(profiles) {}
237
Bart Van Asschef096bd22022-01-24 19:59:13 +0000238 const char* Name() const override { return "ApplyProfileAction"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000239 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000240 bool ExecuteForTask(pid_t tid) const override;
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000241 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
242 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000243 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000244 bool IsValidForTask(pid_t tid) const override;
Rick Yiu0b211fa2019-09-16 19:07:17 +0800245
246 private:
247 std::vector<std::shared_ptr<TaskProfile>> profiles_;
248};
249
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800250class TaskProfiles {
251 public:
252 // Should be used by all users
253 static TaskProfiles& GetInstance();
254
Bart Van Assched0b8ce22022-08-02 13:06:26 -0700255 TaskProfile* GetProfile(std::string_view name) const;
256 const IProfileAttribute* GetAttribute(std::string_view name) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800257 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const;
Bart Van Asschef32c4ec2022-08-02 13:18:12 -0700258 template <typename T>
259 bool SetProcessProfiles(uid_t uid, pid_t pid, std::span<const T> profiles, bool use_fd_cache);
260 template <typename T>
T.J. Mercier1c007992024-01-25 16:29:54 +0000261 bool SetTaskProfiles(pid_t tid, std::span<const T> profiles, bool use_fd_cache);
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +0000262 template <typename T>
263 bool SetUserProfiles(uid_t uid, std::span<const T> profiles, bool use_fd_cache);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800264
265 private:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800266 TaskProfiles();
267
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800268 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Bart Van Assched0b8ce22022-08-02 13:06:26 -0700269
270 std::map<std::string, std::shared_ptr<TaskProfile>, std::less<>> profiles_;
271 std::map<std::string, std::unique_ptr<IProfileAttribute>, std::less<>> attributes_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800272};
T.J. Mercierd1e048f2024-03-28 00:33:44 +0000273
274std::string ConvertUidToPath(const char* root_cgroup_path, uid_t uid);
275std::string ConvertUidPidToPath(const char* root_cgroup_path, uid_t uid, pid_t pid);