blob: abb3ca5c37d6aa7061da9bb8422991c732c450e0 [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; }
80 virtual bool ExecuteForTask(int) const { return false; }
81 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_;
117
T.J. Mercier1c007992024-01-25 16:29:54 +0000118 static bool IsTimerSlackSupported(pid_t tid);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800119};
120
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800121// Set attribute profile element
122class SetAttributeAction : public ProfileAction {
123 public:
Bart Van Assche59af6802022-01-24 21:08:57 +0000124 SetAttributeAction(const IProfileAttribute* attribute, const std::string& value, bool optional)
125 : attribute_(attribute), value_(value), optional_(optional) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800126
Bart Van Asschef096bd22022-01-24 19:59:13 +0000127 const char* Name() const override { return "SetAttribute"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000128 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000129 bool ExecuteForTask(pid_t tid) const override;
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +0000130 bool ExecuteForUID(uid_t uid) const override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000131 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000132 bool IsValidForTask(pid_t tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800133
134 private:
Bart Van Assche4c99e962022-02-03 19:50:16 +0000135 const IProfileAttribute* attribute_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800136 std::string value_;
Bart Van Assche59af6802022-01-24 21:08:57 +0000137 bool optional_;
Suren Baghdasaryan34837982023-07-25 15:45:45 -0700138
139 bool WriteValueToFile(const std::string& path) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800140};
141
Rick Yiud4c53512021-11-21 15:57:36 +0800142// Set cgroup profile element
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800143class SetCgroupAction : public ProfileAction {
Rick Yiubc1ad962020-10-26 20:32:52 +0800144 public:
T.J. Mercierfcb86662024-08-01 20:52:30 +0000145 SetCgroupAction(const CgroupControllerWrapper& c, const std::string& p);
Rick Yiubc1ad962020-10-26 20:32:52 +0800146
Bart Van Asschef096bd22022-01-24 19:59:13 +0000147 const char* Name() const override { return "SetCgroup"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000148 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000149 bool ExecuteForTask(pid_t tid) const override;
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000150 void EnableResourceCaching(ResourceCacheType cache_type) override;
151 void DropResourceCaching(ResourceCacheType cache_type) override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000152 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000153 bool IsValidForTask(pid_t tid) const override;
Rick Yiubc1ad962020-10-26 20:32:52 +0800154
T.J. Mercierfcb86662024-08-01 20:52:30 +0000155 const CgroupControllerWrapper* controller() const { return &controller_; }
Rick Yiud4c53512021-11-21 15:57:36 +0800156
Rick Yiubc1ad962020-10-26 20:32:52 +0800157 private:
T.J. Mercierfcb86662024-08-01 20:52:30 +0000158 CgroupControllerWrapper controller_;
Rick Yiud4c53512021-11-21 15:57:36 +0800159 std::string path_;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800160 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800161 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800162
T.J. Mercier1c007992024-01-25 16:29:54 +0000163 bool AddTidToCgroup(pid_t tid, int fd, ResourceCacheType cache_type) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800164 CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const;
Rick Yiud4c53512021-11-21 15:57:36 +0800165};
166
167// Write to file action
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800168class WriteFileAction : public ProfileAction {
Rick Yiud4c53512021-11-21 15:57:36 +0800169 public:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800170 WriteFileAction(const std::string& task_path, const std::string& proc_path,
171 const std::string& value, bool logfailures);
Rick Yiud4c53512021-11-21 15:57:36 +0800172
Bart Van Asschef096bd22022-01-24 19:59:13 +0000173 const char* Name() const override { return "WriteFile"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000174 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000175 bool ExecuteForTask(pid_t tid) const override;
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000176 void EnableResourceCaching(ResourceCacheType cache_type) override;
177 void DropResourceCaching(ResourceCacheType cache_type) override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000178 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000179 bool IsValidForTask(pid_t tid) const override;
Rick Yiud4c53512021-11-21 15:57:36 +0800180
181 private:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800182 std::string task_path_, proc_path_, value_;
Rick Yiud76053a2021-01-25 12:44:45 +0800183 bool logfailures_;
Rick Yiu9221b1e2022-02-10 16:44:43 +0800184 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800185 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800186
T.J. Merciera757eb82024-01-24 22:45:26 +0000187 bool WriteValueToFile(const std::string& value, ResourceCacheType cache_type, uid_t uid,
T.J. Mercierd6fb2252024-01-24 23:42:39 +0000188 pid_t pid, bool logfailures) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800189 CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const;
Rick Yiubc1ad962020-10-26 20:32:52 +0800190};
191
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800192class TaskProfile {
193 public:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000194 TaskProfile(const std::string& name) : name_(name), res_cached_(false) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800195
Bart Van Asschef096bd22022-01-24 19:59:13 +0000196 const std::string& Name() const { return name_; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800197 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800198 void MoveTo(TaskProfile* profile);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800199
200 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
T.J. Mercier1c007992024-01-25 16:29:54 +0000201 bool ExecuteForTask(pid_t tid) const;
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +0000202 bool ExecuteForUID(uid_t uid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800203 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
204 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000205 bool IsValidForProcess(uid_t uid, pid_t pid) const;
T.J. Mercier1c007992024-01-25 16:29:54 +0000206 bool IsValidForTask(pid_t tid) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800207
208 private:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000209 const std::string name_;
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800210 bool res_cached_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800211 std::vector<std::unique_ptr<ProfileAction>> elements_;
212};
213
Rick Yiu0b211fa2019-09-16 19:07:17 +0800214// Set aggregate profile element
215class ApplyProfileAction : public ProfileAction {
216 public:
217 ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles)
218 : profiles_(profiles) {}
219
Bart Van Asschef096bd22022-01-24 19:59:13 +0000220 const char* Name() const override { return "ApplyProfileAction"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000221 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000222 bool ExecuteForTask(pid_t tid) const override;
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000223 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
224 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000225 bool IsValidForProcess(uid_t uid, pid_t pid) const override;
T.J. Mercier1c007992024-01-25 16:29:54 +0000226 bool IsValidForTask(pid_t tid) const override;
Rick Yiu0b211fa2019-09-16 19:07:17 +0800227
228 private:
229 std::vector<std::shared_ptr<TaskProfile>> profiles_;
230};
231
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800232class TaskProfiles {
233 public:
234 // Should be used by all users
235 static TaskProfiles& GetInstance();
236
Bart Van Assched0b8ce22022-08-02 13:06:26 -0700237 TaskProfile* GetProfile(std::string_view name) const;
238 const IProfileAttribute* GetAttribute(std::string_view name) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800239 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const;
Bart Van Asschef32c4ec2022-08-02 13:18:12 -0700240 template <typename T>
241 bool SetProcessProfiles(uid_t uid, pid_t pid, std::span<const T> profiles, bool use_fd_cache);
242 template <typename T>
T.J. Mercier1c007992024-01-25 16:29:54 +0000243 bool SetTaskProfiles(pid_t tid, std::span<const T> profiles, bool use_fd_cache);
T.J. Mercier5ed5e1b2022-08-22 21:25:09 +0000244 template <typename T>
245 bool SetUserProfiles(uid_t uid, std::span<const T> profiles, bool use_fd_cache);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800246
247 private:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800248 TaskProfiles();
249
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800250 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Bart Van Assched0b8ce22022-08-02 13:06:26 -0700251
252 std::map<std::string, std::shared_ptr<TaskProfile>, std::less<>> profiles_;
253 std::map<std::string, std::unique_ptr<IProfileAttribute>, std::less<>> attributes_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800254};
T.J. Mercierd1e048f2024-03-28 00:33:44 +0000255
256std::string ConvertUidToPath(const char* root_cgroup_path, uid_t uid);
257std::string ConvertUidPidToPath(const char* root_cgroup_path, uid_t uid, pid_t pid);