blob: d0b50436c0c31217caaf51612b785a829e945304 [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
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080094class SetTimerSlackAction : public ProfileAction {
95 public:
96 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
97
Bart Van Asschef096bd22022-01-24 19:59:13 +000098 const char* Name() const override { return "SetTimerSlack"; }
T.J. Mercier1c007992024-01-25 16:29:54 +000099 bool ExecuteForTask(pid_t tid) const override;
T.J. Mercier44eb7052024-03-27 23:40:43 +0000100 bool IsValidForProcess(uid_t, pid_t) const override { return true; }
101 bool IsValidForTask(pid_t) const override { return true; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800102
103 private:
104 unsigned long slack_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800105};
106
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800107// Set attribute profile element
108class SetAttributeAction : public ProfileAction {
109 public:
Bart Van Assche59af6802022-01-24 21:08:57 +0000110 SetAttributeAction(const IProfileAttribute* attribute, const std::string& value, bool optional)
111 : attribute_(attribute), value_(value), optional_(optional) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800112
Bart Van Asschef096bd22022-01-24 19:59:13 +0000113 const char* Name() const override { return "SetAttribute"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000114 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000115 bool ExecuteForTask(pid_t tid) const override;
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +0000116 bool ExecuteForUID(uid_t uid) const override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000117 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000118 bool IsValidForTask(pid_t tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800119
120 private:
Bart Van Assche4c99e962022-02-03 19:50:16 +0000121 const IProfileAttribute* attribute_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800122 std::string value_;
Bart Van Assche59af6802022-01-24 21:08:57 +0000123 bool optional_;
Suren Baghdasaryan34837982023-07-25 15:45:45 -0700124
125 bool WriteValueToFile(const std::string& path) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800126};
127
Rick Yiud4c53512021-11-21 15:57:36 +0800128// Set cgroup profile element
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800129class SetCgroupAction : public ProfileAction {
Rick Yiubc1ad962020-10-26 20:32:52 +0800130 public:
T.J. Mercierfcb86662024-08-01 20:52:30 +0000131 SetCgroupAction(const CgroupControllerWrapper& c, const std::string& p);
Rick Yiubc1ad962020-10-26 20:32:52 +0800132
Bart Van Asschef096bd22022-01-24 19:59:13 +0000133 const char* Name() const override { return "SetCgroup"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000134 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000135 bool ExecuteForTask(pid_t tid) const override;
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000136 void EnableResourceCaching(ResourceCacheType cache_type) override;
137 void DropResourceCaching(ResourceCacheType cache_type) override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000138 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000139 bool IsValidForTask(pid_t tid) const override;
Rick Yiubc1ad962020-10-26 20:32:52 +0800140
T.J. Mercierfcb86662024-08-01 20:52:30 +0000141 const CgroupControllerWrapper* controller() const { return &controller_; }
Rick Yiud4c53512021-11-21 15:57:36 +0800142
Rick Yiubc1ad962020-10-26 20:32:52 +0800143 private:
T.J. Mercierfcb86662024-08-01 20:52:30 +0000144 CgroupControllerWrapper controller_;
Rick Yiud4c53512021-11-21 15:57:36 +0800145 std::string path_;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800146 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800147 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800148
T.J. Mercier1c007992024-01-25 16:29:54 +0000149 bool AddTidToCgroup(pid_t tid, int fd, ResourceCacheType cache_type) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800150 CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const;
Rick Yiud4c53512021-11-21 15:57:36 +0800151};
152
153// Write to file action
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800154class WriteFileAction : public ProfileAction {
Rick Yiud4c53512021-11-21 15:57:36 +0800155 public:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800156 WriteFileAction(const std::string& task_path, const std::string& proc_path,
157 const std::string& value, bool logfailures);
Rick Yiud4c53512021-11-21 15:57:36 +0800158
Bart Van Asschef096bd22022-01-24 19:59:13 +0000159 const char* Name() const override { return "WriteFile"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000160 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000161 bool ExecuteForTask(pid_t tid) const override;
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000162 void EnableResourceCaching(ResourceCacheType cache_type) override;
163 void DropResourceCaching(ResourceCacheType cache_type) override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000164 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000165 bool IsValidForTask(pid_t tid) const override;
Rick Yiud4c53512021-11-21 15:57:36 +0800166
167 private:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800168 std::string task_path_, proc_path_, value_;
Rick Yiud76053a2021-01-25 12:44:45 +0800169 bool logfailures_;
Rick Yiu9221b1e2022-02-10 16:44:43 +0800170 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800171 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800172
T.J. Merciera757eb82024-01-24 22:45:26 +0000173 bool WriteValueToFile(const std::string& value, ResourceCacheType cache_type, uid_t uid,
T.J. Mercierd6fb2252024-01-24 23:42:39 +0000174 pid_t pid, bool logfailures) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800175 CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const;
Rick Yiubc1ad962020-10-26 20:32:52 +0800176};
177
T.J. Mercier39846112024-10-09 22:40:26 +0000178// Set scheduler policy action
179class SetSchedulerPolicyAction : public ProfileAction {
180 public:
181 SetSchedulerPolicyAction(int policy)
182 : policy_(policy) {}
183 SetSchedulerPolicyAction(int policy, int priority_or_nice)
184 : policy_(policy), priority_or_nice_(priority_or_nice) {}
185
186 const char* Name() const override { return "SetSchedulerPolicy"; }
187 bool ExecuteForTask(pid_t tid) const override;
188
189 static bool isNormalPolicy(int policy);
190 static bool toPriority(int policy, int virtual_priority, int& priority_out);
191
192 private:
193 int policy_;
194 std::optional<int> priority_or_nice_;
195};
196
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800197class TaskProfile {
198 public:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000199 TaskProfile(const std::string& name) : name_(name), res_cached_(false) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800200
Bart Van Asschef096bd22022-01-24 19:59:13 +0000201 const std::string& Name() const { return name_; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800202 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800203 void MoveTo(TaskProfile* profile);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800204
205 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
T.J. Mercier1c007992024-01-25 16:29:54 +0000206 bool ExecuteForTask(pid_t tid) const;
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +0000207 bool ExecuteForUID(uid_t uid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800208 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
209 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000210 bool IsValidForProcess(uid_t uid, pid_t pid) const;
T.J. Mercier1c007992024-01-25 16:29:54 +0000211 bool IsValidForTask(pid_t tid) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800212
213 private:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000214 const std::string name_;
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800215 bool res_cached_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800216 std::vector<std::unique_ptr<ProfileAction>> elements_;
217};
218
Rick Yiu0b211fa2019-09-16 19:07:17 +0800219// Set aggregate profile element
220class ApplyProfileAction : public ProfileAction {
221 public:
222 ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles)
223 : profiles_(profiles) {}
224
Bart Van Asschef096bd22022-01-24 19:59:13 +0000225 const char* Name() const override { return "ApplyProfileAction"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000226 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000227 bool ExecuteForTask(pid_t tid) const override;
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000228 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
229 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000230 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000231 bool IsValidForTask(pid_t tid) const override;
Rick Yiu0b211fa2019-09-16 19:07:17 +0800232
233 private:
234 std::vector<std::shared_ptr<TaskProfile>> profiles_;
235};
236
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800237class TaskProfiles {
238 public:
239 // Should be used by all users
240 static TaskProfiles& GetInstance();
241
Bart Van Assched0b8ce22022-08-02 13:06:26 -0700242 TaskProfile* GetProfile(std::string_view name) const;
243 const IProfileAttribute* GetAttribute(std::string_view name) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800244 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const;
Bart Van Asschef32c4ec2022-08-02 13:18:12 -0700245 template <typename T>
246 bool SetProcessProfiles(uid_t uid, pid_t pid, std::span<const T> profiles, bool use_fd_cache);
247 template <typename T>
T.J. Mercier1c007992024-01-25 16:29:54 +0000248 bool SetTaskProfiles(pid_t tid, std::span<const T> profiles, bool use_fd_cache);
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +0000249 template <typename T>
250 bool SetUserProfiles(uid_t uid, std::span<const T> profiles, bool use_fd_cache);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800251
252 private:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800253 TaskProfiles();
254
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800255 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Bart Van Assched0b8ce22022-08-02 13:06:26 -0700256
257 std::map<std::string, std::shared_ptr<TaskProfile>, std::less<>> profiles_;
258 std::map<std::string, std::unique_ptr<IProfileAttribute>, std::less<>> attributes_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800259};
T.J. Mercierd1e048f2024-03-28 00:33:44 +0000260
261std::string ConvertUidToPath(const char* root_cgroup_path, uid_t uid);
262std::string ConvertUidPidToPath(const char* root_cgroup_path, uid_t uid, pid_t pid);