blob: e52ce38feabe8942c39c17172d98381ec634d3b3 [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>
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;
T.J. Mercierfcb86662024-08-01 20:52:30 +000035 virtual void Reset(const CgroupControllerWrapper& controller, const std::string& file_name,
Suren Baghdasaryan35078462023-07-25 14:50:18 -070036 const std::string& file_v2_name) = 0;
T.J. Mercierfcb86662024-08-01 20:52:30 +000037 virtual const CgroupControllerWrapper* controller() const = 0;
Bart Van Assche4c99e962022-02-03 19:50:16 +000038 virtual const std::string& file_name() const = 0;
Suren Baghdasaryan34837982023-07-25 15:45:45 -070039 virtual bool GetPathForProcess(uid_t uid, pid_t pid, std::string* path) const = 0;
T.J. Mercier1c007992024-01-25 16:29:54 +000040 virtual bool GetPathForTask(pid_t tid, std::string* path) const = 0;
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +000041 virtual bool GetPathForUID(uid_t uid, std::string* path) const = 0;
Bart Van Assche4c99e962022-02-03 19:50:16 +000042};
43
44class ProfileAttribute : public IProfileAttribute {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080045 public:
Bart Van Asschebc077ff2022-02-17 01:26:44 +000046 // Cgroup attributes may have different names in the v1 and v2 hierarchies. If `file_v2_name` is
47 // not empty, `file_name` is the name for the v1 hierarchy and `file_v2_name` is the name for
48 // the v2 hierarchy. If `file_v2_name` is empty, `file_name` is used for both hierarchies.
T.J. Mercierfcb86662024-08-01 20:52:30 +000049 ProfileAttribute(const CgroupControllerWrapper& controller, const std::string& file_name,
Bart Van Asschebc077ff2022-02-17 01:26:44 +000050 const std::string& file_v2_name)
51 : controller_(controller), file_name_(file_name), file_v2_name_(file_v2_name) {}
Bart Van Assche4c99e962022-02-03 19:50:16 +000052 ~ProfileAttribute() = default;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080053
T.J. Mercierfcb86662024-08-01 20:52:30 +000054 const CgroupControllerWrapper* controller() const override { return &controller_; }
Suren Baghdasaryan35078462023-07-25 14:50:18 -070055 const std::string& file_name() const override;
T.J. Mercierfcb86662024-08-01 20:52:30 +000056 void Reset(const CgroupControllerWrapper& controller, const std::string& file_name,
Suren Baghdasaryan35078462023-07-25 14:50:18 -070057 const std::string& file_v2_name) override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080058
Suren Baghdasaryan34837982023-07-25 15:45:45 -070059 bool GetPathForProcess(uid_t uid, pid_t pid, std::string* path) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +000060 bool GetPathForTask(pid_t tid, std::string* path) const override;
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +000061 bool GetPathForUID(uid_t uid, std::string* path) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080062
63 private:
T.J. Mercierfcb86662024-08-01 20:52:30 +000064 CgroupControllerWrapper controller_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080065 std::string file_name_;
Bart Van Asschebc077ff2022-02-17 01:26:44 +000066 std::string file_v2_name_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080067};
68
69// Abstract profile element
70class ProfileAction {
71 public:
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080072 enum ResourceCacheType { RCT_TASK = 0, RCT_PROCESS, RCT_COUNT };
73
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080074 virtual ~ProfileAction() {}
75
Bart Van Asschef096bd22022-01-24 19:59:13 +000076 virtual const char* Name() const = 0;
77
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080078 // Default implementations will fail
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +000079 virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; }
T.J. Mercierb53eb9d2024-10-09 22:46:33 +000080 virtual bool ExecuteForTask(pid_t) const { return false; }
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +000081 virtual bool ExecuteForUID(uid_t) const { return false; }
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080082
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080083 virtual void EnableResourceCaching(ResourceCacheType) {}
84 virtual void DropResourceCaching(ResourceCacheType) {}
T.J. Mercier44eb7052024-03-27 23:40:43 +000085 virtual bool IsValidForProcess(uid_t, pid_t) const { return false; }
86 virtual bool IsValidForTask(pid_t) const { return false; }
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080087
88 protected:
89 enum CacheUseResult { SUCCESS, FAIL, UNUSED };
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080090};
91
92// Profile actions
93class SetClampsAction : public ProfileAction {
94 public:
95 SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
96
Bart Van Asschef096bd22022-01-24 19:59:13 +000097 const char* Name() const override { return "SetClamps"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +000098 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +000099 bool ExecuteForTask(pid_t tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800100
101 protected:
102 int boost_;
103 int clamp_;
104};
105
106class SetTimerSlackAction : public ProfileAction {
107 public:
108 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
109
Bart Van Asschef096bd22022-01-24 19:59:13 +0000110 const char* Name() const override { return "SetTimerSlack"; }
T.J. Mercier1c007992024-01-25 16:29:54 +0000111 bool ExecuteForTask(pid_t tid) const override;
T.J. Mercier44eb7052024-03-27 23:40:43 +0000112 bool IsValidForProcess(uid_t, pid_t) const override { return true; }
113 bool IsValidForTask(pid_t) const override { return true; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800114
115 private:
116 unsigned long slack_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800117};
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;
T.J. Mercier1c007992024-01-25 16:29:54 +0000127 bool ExecuteForTask(pid_t 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;
T.J. Mercier1c007992024-01-25 16:29:54 +0000130 bool IsValidForTask(pid_t 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 Baghdasaryan34837982023-07-25 15:45:45 -0700136
137 bool WriteValueToFile(const std::string& path) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800138};
139
Rick Yiud4c53512021-11-21 15:57:36 +0800140// Set cgroup profile element
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800141class SetCgroupAction : public ProfileAction {
Rick Yiubc1ad962020-10-26 20:32:52 +0800142 public:
T.J. Mercierfcb86662024-08-01 20:52:30 +0000143 SetCgroupAction(const CgroupControllerWrapper& c, const std::string& p);
Rick Yiubc1ad962020-10-26 20:32:52 +0800144
Bart Van Asschef096bd22022-01-24 19:59:13 +0000145 const char* Name() const override { return "SetCgroup"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000146 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000147 bool ExecuteForTask(pid_t tid) const override;
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000148 void EnableResourceCaching(ResourceCacheType cache_type) override;
149 void DropResourceCaching(ResourceCacheType cache_type) override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000150 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000151 bool IsValidForTask(pid_t tid) const override;
Rick Yiubc1ad962020-10-26 20:32:52 +0800152
T.J. Mercierfcb86662024-08-01 20:52:30 +0000153 const CgroupControllerWrapper* controller() const { return &controller_; }
Rick Yiud4c53512021-11-21 15:57:36 +0800154
Rick Yiubc1ad962020-10-26 20:32:52 +0800155 private:
T.J. Mercierfcb86662024-08-01 20:52:30 +0000156 CgroupControllerWrapper controller_;
Rick Yiud4c53512021-11-21 15:57:36 +0800157 std::string path_;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800158 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800159 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800160
T.J. Mercier1c007992024-01-25 16:29:54 +0000161 bool AddTidToCgroup(pid_t tid, int fd, ResourceCacheType cache_type) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800162 CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const;
Rick Yiud4c53512021-11-21 15:57:36 +0800163};
164
165// Write to file action
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800166class WriteFileAction : public ProfileAction {
Rick Yiud4c53512021-11-21 15:57:36 +0800167 public:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800168 WriteFileAction(const std::string& task_path, const std::string& proc_path,
169 const std::string& value, bool logfailures);
Rick Yiud4c53512021-11-21 15:57:36 +0800170
Bart Van Asschef096bd22022-01-24 19:59:13 +0000171 const char* Name() const override { return "WriteFile"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000172 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000173 bool ExecuteForTask(pid_t tid) const override;
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000174 void EnableResourceCaching(ResourceCacheType cache_type) override;
175 void DropResourceCaching(ResourceCacheType cache_type) override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000176 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000177 bool IsValidForTask(pid_t tid) const override;
Rick Yiud4c53512021-11-21 15:57:36 +0800178
179 private:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800180 std::string task_path_, proc_path_, value_;
Rick Yiud76053a2021-01-25 12:44:45 +0800181 bool logfailures_;
Rick Yiu9221b1e2022-02-10 16:44:43 +0800182 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800183 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800184
T.J. Merciera757eb82024-01-24 22:45:26 +0000185 bool WriteValueToFile(const std::string& value, ResourceCacheType cache_type, uid_t uid,
T.J. Mercierd6fb2252024-01-24 23:42:39 +0000186 pid_t pid, bool logfailures) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800187 CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const;
Rick Yiubc1ad962020-10-26 20:32:52 +0800188};
189
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800190class TaskProfile {
191 public:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000192 TaskProfile(const std::string& name) : name_(name), res_cached_(false) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800193
Bart Van Asschef096bd22022-01-24 19:59:13 +0000194 const std::string& Name() const { return name_; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800195 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800196 void MoveTo(TaskProfile* profile);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800197
198 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
T.J. Mercier1c007992024-01-25 16:29:54 +0000199 bool ExecuteForTask(pid_t tid) const;
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +0000200 bool ExecuteForUID(uid_t uid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800201 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
202 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000203 bool IsValidForProcess(uid_t uid, pid_t pid) const;
T.J. Mercier1c007992024-01-25 16:29:54 +0000204 bool IsValidForTask(pid_t tid) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800205
206 private:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000207 const std::string name_;
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800208 bool res_cached_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800209 std::vector<std::unique_ptr<ProfileAction>> elements_;
210};
211
Rick Yiu0b211fa2019-09-16 19:07:17 +0800212// Set aggregate profile element
213class ApplyProfileAction : public ProfileAction {
214 public:
215 ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles)
216 : profiles_(profiles) {}
217
Bart Van Asschef096bd22022-01-24 19:59:13 +0000218 const char* Name() const override { return "ApplyProfileAction"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000219 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000220 bool ExecuteForTask(pid_t tid) const override;
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000221 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
222 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000223 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000224 bool IsValidForTask(pid_t tid) const override;
Rick Yiu0b211fa2019-09-16 19:07:17 +0800225
226 private:
227 std::vector<std::shared_ptr<TaskProfile>> profiles_;
228};
229
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800230class TaskProfiles {
231 public:
232 // Should be used by all users
233 static TaskProfiles& GetInstance();
234
Bart Van Assched0b8ce22022-08-02 13:06:26 -0700235 TaskProfile* GetProfile(std::string_view name) const;
236 const IProfileAttribute* GetAttribute(std::string_view name) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800237 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const;
Bart Van Asschef32c4ec2022-08-02 13:18:12 -0700238 template <typename T>
239 bool SetProcessProfiles(uid_t uid, pid_t pid, std::span<const T> profiles, bool use_fd_cache);
240 template <typename T>
T.J. Mercier1c007992024-01-25 16:29:54 +0000241 bool SetTaskProfiles(pid_t tid, std::span<const T> profiles, bool use_fd_cache);
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +0000242 template <typename T>
243 bool SetUserProfiles(uid_t uid, std::span<const T> profiles, bool use_fd_cache);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800244
245 private:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800246 TaskProfiles();
247
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800248 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Bart Van Assched0b8ce22022-08-02 13:06:26 -0700249
250 std::map<std::string, std::shared_ptr<TaskProfile>, std::less<>> profiles_;
251 std::map<std::string, std::unique_ptr<IProfileAttribute>, std::less<>> attributes_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800252};
T.J. Mercierd1e048f2024-03-28 00:33:44 +0000253
254std::string ConvertUidToPath(const char* root_cgroup_path, uid_t uid);
255std::string ConvertUidPidToPath(const char* root_cgroup_path, uid_t uid, pid_t pid);