blob: b2b4f5488c57472bde6f726cbce5f52f11b95417 [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>
21#include <map>
mtk1603653f79e62019-05-31 19:05:22 +080022#include <mutex>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080023#include <string>
Bart Van Assched0b8ce22022-08-02 13:06:26 -070024#include <string_view>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080025#include <vector>
26
27#include <android-base/unique_fd.h>
28#include <cgroup_map.h>
29
Bart Van Assche4c99e962022-02-03 19:50:16 +000030class IProfileAttribute {
31 public:
32 virtual ~IProfileAttribute() = 0;
33 virtual void Reset(const CgroupController& controller, const std::string& file_name) = 0;
34 virtual const CgroupController* controller() const = 0;
35 virtual const std::string& file_name() const = 0;
36 virtual bool GetPathForTask(int tid, std::string* path) const = 0;
37};
38
39class ProfileAttribute : public IProfileAttribute {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080040 public:
Bart Van Asschebc077ff2022-02-17 01:26:44 +000041 // Cgroup attributes may have different names in the v1 and v2 hierarchies. If `file_v2_name` is
42 // not empty, `file_name` is the name for the v1 hierarchy and `file_v2_name` is the name for
43 // the v2 hierarchy. If `file_v2_name` is empty, `file_name` is used for both hierarchies.
44 ProfileAttribute(const CgroupController& controller, const std::string& file_name,
45 const std::string& file_v2_name)
46 : controller_(controller), file_name_(file_name), file_v2_name_(file_v2_name) {}
Bart Van Assche4c99e962022-02-03 19:50:16 +000047 ~ProfileAttribute() = default;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080048
Bart Van Assche4c99e962022-02-03 19:50:16 +000049 const CgroupController* controller() const override { return &controller_; }
50 const std::string& file_name() const override { return file_name_; }
51 void Reset(const CgroupController& controller, const std::string& file_name) override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080052
Bart Van Assche4c99e962022-02-03 19:50:16 +000053 bool GetPathForTask(int tid, std::string* path) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080054
55 private:
Yifan Hong53e0deb2019-03-22 17:01:08 -070056 CgroupController controller_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080057 std::string file_name_;
Bart Van Asschebc077ff2022-02-17 01:26:44 +000058 std::string file_v2_name_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080059};
60
61// Abstract profile element
62class ProfileAction {
63 public:
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080064 enum ResourceCacheType { RCT_TASK = 0, RCT_PROCESS, RCT_COUNT };
65
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080066 virtual ~ProfileAction() {}
67
Bart Van Asschef096bd22022-01-24 19:59:13 +000068 virtual const char* Name() const = 0;
69
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080070 // Default implementations will fail
Greg Kaiser5c5ed9c2019-02-04 06:33:26 -080071 virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; };
72 virtual bool ExecuteForTask(int) const { return false; };
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080073
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080074 virtual void EnableResourceCaching(ResourceCacheType) {}
75 virtual void DropResourceCaching(ResourceCacheType) {}
76
77 protected:
78 enum CacheUseResult { SUCCESS, FAIL, UNUSED };
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080079};
80
81// Profile actions
82class SetClampsAction : public ProfileAction {
83 public:
84 SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
85
Bart Van Asschef096bd22022-01-24 19:59:13 +000086 const char* Name() const override { return "SetClamps"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +000087 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
88 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080089
90 protected:
91 int boost_;
92 int clamp_;
93};
94
95class SetTimerSlackAction : public ProfileAction {
96 public:
97 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
98
Bart Van Asschef096bd22022-01-24 19:59:13 +000099 const char* Name() const override { return "SetTimerSlack"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000100 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800101
102 private:
103 unsigned long slack_;
104
105 static bool IsTimerSlackSupported(int tid);
106};
107
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800108// Set attribute profile element
109class SetAttributeAction : public ProfileAction {
110 public:
Bart Van Assche59af6802022-01-24 21:08:57 +0000111 SetAttributeAction(const IProfileAttribute* attribute, const std::string& value, bool optional)
112 : attribute_(attribute), value_(value), optional_(optional) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800113
Bart Van Asschef096bd22022-01-24 19:59:13 +0000114 const char* Name() const override { return "SetAttribute"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000115 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
116 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800117
118 private:
Bart Van Assche4c99e962022-02-03 19:50:16 +0000119 const IProfileAttribute* attribute_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800120 std::string value_;
Bart Van Assche59af6802022-01-24 21:08:57 +0000121 bool optional_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800122};
123
Rick Yiud4c53512021-11-21 15:57:36 +0800124// Set cgroup profile element
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800125class SetCgroupAction : public ProfileAction {
Rick Yiubc1ad962020-10-26 20:32:52 +0800126 public:
Rick Yiud4c53512021-11-21 15:57:36 +0800127 SetCgroupAction(const CgroupController& c, const std::string& p);
Rick Yiubc1ad962020-10-26 20:32:52 +0800128
Bart Van Asschef096bd22022-01-24 19:59:13 +0000129 const char* Name() const override { return "SetCgroup"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000130 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
131 bool ExecuteForTask(int tid) const override;
132 void EnableResourceCaching(ResourceCacheType cache_type) override;
133 void DropResourceCaching(ResourceCacheType cache_type) override;
Rick Yiubc1ad962020-10-26 20:32:52 +0800134
Rick Yiud4c53512021-11-21 15:57:36 +0800135 const CgroupController* controller() const { return &controller_; }
136
Rick Yiubc1ad962020-10-26 20:32:52 +0800137 private:
Rick Yiud4c53512021-11-21 15:57:36 +0800138 CgroupController controller_;
139 std::string path_;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800140 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800141 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800142
143 static bool AddTidToCgroup(int tid, int fd, const char* controller_name);
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800144 CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const;
Rick Yiud4c53512021-11-21 15:57:36 +0800145};
146
147// Write to file action
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800148class WriteFileAction : public ProfileAction {
Rick Yiud4c53512021-11-21 15:57:36 +0800149 public:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800150 WriteFileAction(const std::string& task_path, const std::string& proc_path,
151 const std::string& value, bool logfailures);
Rick Yiud4c53512021-11-21 15:57:36 +0800152
Bart Van Asschef096bd22022-01-24 19:59:13 +0000153 const char* Name() const override { return "WriteFile"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000154 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
155 bool ExecuteForTask(int tid) const override;
156 void EnableResourceCaching(ResourceCacheType cache_type) override;
157 void DropResourceCaching(ResourceCacheType cache_type) override;
Rick Yiud4c53512021-11-21 15:57:36 +0800158
159 private:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800160 std::string task_path_, proc_path_, value_;
Rick Yiud76053a2021-01-25 12:44:45 +0800161 bool logfailures_;
Rick Yiu9221b1e2022-02-10 16:44:43 +0800162 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800163 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800164
Rick Yiu9221b1e2022-02-10 16:44:43 +0800165 bool WriteValueToFile(const std::string& value, ResourceCacheType cache_type, int uid, int pid,
166 bool logfailures) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800167 CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const;
Rick Yiubc1ad962020-10-26 20:32:52 +0800168};
169
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800170class TaskProfile {
171 public:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000172 TaskProfile(const std::string& name) : name_(name), res_cached_(false) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800173
Bart Van Asschef096bd22022-01-24 19:59:13 +0000174 const std::string& Name() const { return name_; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800175 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800176 void MoveTo(TaskProfile* profile);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800177
178 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
179 bool ExecuteForTask(int tid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800180 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
181 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800182
183 private:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000184 const std::string name_;
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800185 bool res_cached_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800186 std::vector<std::unique_ptr<ProfileAction>> elements_;
187};
188
Rick Yiu0b211fa2019-09-16 19:07:17 +0800189// Set aggregate profile element
190class ApplyProfileAction : public ProfileAction {
191 public:
192 ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles)
193 : profiles_(profiles) {}
194
Bart Van Asschef096bd22022-01-24 19:59:13 +0000195 const char* Name() const override { return "ApplyProfileAction"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000196 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
197 bool ExecuteForTask(int tid) const override;
198 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
199 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
Rick Yiu0b211fa2019-09-16 19:07:17 +0800200
201 private:
202 std::vector<std::shared_ptr<TaskProfile>> profiles_;
203};
204
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800205class TaskProfiles {
206 public:
207 // Should be used by all users
208 static TaskProfiles& GetInstance();
209
Bart Van Assched0b8ce22022-08-02 13:06:26 -0700210 TaskProfile* GetProfile(std::string_view name) const;
211 const IProfileAttribute* GetAttribute(std::string_view name) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800212 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const;
213 bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles,
214 bool use_fd_cache);
Rick Yiu0b211fa2019-09-16 19:07:17 +0800215 bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800216
217 private:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800218 TaskProfiles();
219
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800220 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Bart Van Assched0b8ce22022-08-02 13:06:26 -0700221
222 std::map<std::string, std::shared_ptr<TaskProfile>, std::less<>> profiles_;
223 std::map<std::string, std::unique_ptr<IProfileAttribute>, std::less<>> attributes_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800224};