blob: 25e3a02efb0942f4bfc541d0423893fd7a3fed53 [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;
Suren Baghdasaryan34837982023-07-25 15:45:45 -070039 virtual bool GetPathForProcess(uid_t uid, pid_t pid, std::string* path) const = 0;
Bart Van Assche4c99e962022-02-03 19:50:16 +000040 virtual bool GetPathForTask(int 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.
49 ProfileAttribute(const CgroupController& controller, const std::string& file_name,
50 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
Bart Van Assche4c99e962022-02-03 19:50:16 +000054 const CgroupController* controller() const override { return &controller_; }
Suren Baghdasaryan35078462023-07-25 14:50:18 -070055 const std::string& file_name() const override;
56 void Reset(const CgroupController& controller, const std::string& file_name,
57 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;
Bart Van Assche4c99e962022-02-03 19:50:16 +000060 bool GetPathForTask(int 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:
Yifan Hong53e0deb2019-03-22 17:01:08 -070064 CgroupController 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) {}
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +000085 virtual bool IsValidForProcess(uid_t uid, pid_t pid) const { return false; }
86 virtual bool IsValidForTask(int tid) 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;
99 bool ExecuteForTask(int 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"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000111 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan8cacb612023-04-12 01:24:23 +0000112 bool IsValidForProcess(uid_t uid, pid_t pid) const override { return true; }
113 bool IsValidForTask(int tid) const override { return true; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800114
115 private:
116 unsigned long slack_;
117
118 static bool IsTimerSlackSupported(int tid);
119};
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;
129 bool ExecuteForTask(int 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;
132 bool IsValidForTask(int 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:
Rick Yiud4c53512021-11-21 15:57:36 +0800145 SetCgroupAction(const CgroupController& 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;
149 bool ExecuteForTask(int tid) const override;
150 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;
153 bool IsValidForTask(int tid) const override;
Rick Yiubc1ad962020-10-26 20:32:52 +0800154
Rick Yiud4c53512021-11-21 15:57:36 +0800155 const CgroupController* controller() const { return &controller_; }
156
Rick Yiubc1ad962020-10-26 20:32:52 +0800157 private:
Rick Yiud4c53512021-11-21 15:57:36 +0800158 CgroupController controller_;
159 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
Bart Van Asschedf985342023-11-13 15:19:43 -0800163 bool AddTidToCgroup(int 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;
175 bool ExecuteForTask(int tid) const override;
176 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;
179 bool IsValidForTask(int 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,
188 int 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;
201 bool ExecuteForTask(int 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;
206 bool IsValidForTask(int 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;
222 bool ExecuteForTask(int tid) const override;
223 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;
226 bool IsValidForTask(int 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>
243 bool SetTaskProfiles(int 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};