blob: 9ee3781a622932e474ebeb08d84d1e34805ae9c3 [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>
24#include <vector>
25
26#include <android-base/unique_fd.h>
27#include <cgroup_map.h>
28
Bart Van Assche4c99e962022-02-03 19:50:16 +000029class IProfileAttribute {
30 public:
31 virtual ~IProfileAttribute() = 0;
32 virtual void Reset(const CgroupController& controller, const std::string& file_name) = 0;
33 virtual const CgroupController* controller() const = 0;
34 virtual const std::string& file_name() const = 0;
35 virtual bool GetPathForTask(int tid, std::string* path) const = 0;
36};
37
38class ProfileAttribute : public IProfileAttribute {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080039 public:
Yifan Hong53e0deb2019-03-22 17:01:08 -070040 ProfileAttribute(const CgroupController& controller, const std::string& file_name)
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080041 : controller_(controller), file_name_(file_name) {}
Bart Van Assche4c99e962022-02-03 19:50:16 +000042 ~ProfileAttribute() = default;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080043
Bart Van Assche4c99e962022-02-03 19:50:16 +000044 const CgroupController* controller() const override { return &controller_; }
45 const std::string& file_name() const override { return file_name_; }
46 void Reset(const CgroupController& controller, const std::string& file_name) override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080047
Bart Van Assche4c99e962022-02-03 19:50:16 +000048 bool GetPathForTask(int tid, std::string* path) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080049
50 private:
Yifan Hong53e0deb2019-03-22 17:01:08 -070051 CgroupController controller_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080052 std::string file_name_;
53};
54
55// Abstract profile element
56class ProfileAction {
57 public:
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080058 enum ResourceCacheType { RCT_TASK = 0, RCT_PROCESS, RCT_COUNT };
59
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080060 virtual ~ProfileAction() {}
61
Bart Van Asschef096bd22022-01-24 19:59:13 +000062 virtual const char* Name() const = 0;
63
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080064 // Default implementations will fail
Greg Kaiser5c5ed9c2019-02-04 06:33:26 -080065 virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; };
66 virtual bool ExecuteForTask(int) const { return false; };
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080067
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080068 virtual void EnableResourceCaching(ResourceCacheType) {}
69 virtual void DropResourceCaching(ResourceCacheType) {}
70
71 protected:
72 enum CacheUseResult { SUCCESS, FAIL, UNUSED };
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080073};
74
75// Profile actions
76class SetClampsAction : public ProfileAction {
77 public:
78 SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
79
Bart Van Asschef096bd22022-01-24 19:59:13 +000080 const char* Name() const override { return "SetClamps"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +000081 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
82 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080083
84 protected:
85 int boost_;
86 int clamp_;
87};
88
89class SetTimerSlackAction : public ProfileAction {
90 public:
91 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
92
Bart Van Asschef096bd22022-01-24 19:59:13 +000093 const char* Name() const override { return "SetTimerSlack"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +000094 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080095
96 private:
97 unsigned long slack_;
98
99 static bool IsTimerSlackSupported(int tid);
100};
101
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800102// Set attribute profile element
103class SetAttributeAction : public ProfileAction {
104 public:
Bart Van Assche4c99e962022-02-03 19:50:16 +0000105 SetAttributeAction(const IProfileAttribute* attribute, const std::string& value)
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800106 : attribute_(attribute), value_(value) {}
107
Bart Van Asschef096bd22022-01-24 19:59:13 +0000108 const char* Name() const override { return "SetAttribute"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000109 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
110 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800111
112 private:
Bart Van Assche4c99e962022-02-03 19:50:16 +0000113 const IProfileAttribute* attribute_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800114 std::string value_;
115};
116
Rick Yiud4c53512021-11-21 15:57:36 +0800117// Set cgroup profile element
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800118class SetCgroupAction : public ProfileAction {
Rick Yiubc1ad962020-10-26 20:32:52 +0800119 public:
Rick Yiud4c53512021-11-21 15:57:36 +0800120 SetCgroupAction(const CgroupController& c, const std::string& p);
Rick Yiubc1ad962020-10-26 20:32:52 +0800121
Bart Van Asschef096bd22022-01-24 19:59:13 +0000122 const char* Name() const override { return "SetCgroup"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000123 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
124 bool ExecuteForTask(int tid) const override;
125 void EnableResourceCaching(ResourceCacheType cache_type) override;
126 void DropResourceCaching(ResourceCacheType cache_type) override;
Rick Yiubc1ad962020-10-26 20:32:52 +0800127
Rick Yiud4c53512021-11-21 15:57:36 +0800128 const CgroupController* controller() const { return &controller_; }
129
Rick Yiubc1ad962020-10-26 20:32:52 +0800130 private:
Rick Yiud4c53512021-11-21 15:57:36 +0800131 CgroupController controller_;
132 std::string path_;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800133 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800134 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800135
136 static bool AddTidToCgroup(int tid, int fd, const char* controller_name);
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800137 CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const;
Rick Yiud4c53512021-11-21 15:57:36 +0800138};
139
140// Write to file action
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800141class WriteFileAction : public ProfileAction {
Rick Yiud4c53512021-11-21 15:57:36 +0800142 public:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800143 WriteFileAction(const std::string& task_path, const std::string& proc_path,
144 const std::string& value, bool logfailures);
Rick Yiud4c53512021-11-21 15:57:36 +0800145
Bart Van Asschef096bd22022-01-24 19:59:13 +0000146 const char* Name() const override { return "WriteFile"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000147 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
148 bool ExecuteForTask(int tid) const override;
149 void EnableResourceCaching(ResourceCacheType cache_type) override;
150 void DropResourceCaching(ResourceCacheType cache_type) override;
Rick Yiud4c53512021-11-21 15:57:36 +0800151
152 private:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800153 std::string task_path_, proc_path_, value_;
Rick Yiud76053a2021-01-25 12:44:45 +0800154 bool logfailures_;
Rick Yiu9221b1e2022-02-10 16:44:43 +0800155 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800156 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800157
Rick Yiu9221b1e2022-02-10 16:44:43 +0800158 bool WriteValueToFile(const std::string& value, ResourceCacheType cache_type, int uid, int pid,
159 bool logfailures) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800160 CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const;
Rick Yiubc1ad962020-10-26 20:32:52 +0800161};
162
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800163class TaskProfile {
164 public:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000165 TaskProfile(const std::string& name) : name_(name), res_cached_(false) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800166
Bart Van Asschef096bd22022-01-24 19:59:13 +0000167 const std::string& Name() const { return name_; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800168 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800169 void MoveTo(TaskProfile* profile);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800170
171 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
172 bool ExecuteForTask(int tid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800173 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
174 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800175
176 private:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000177 const std::string name_;
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800178 bool res_cached_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800179 std::vector<std::unique_ptr<ProfileAction>> elements_;
180};
181
Rick Yiu0b211fa2019-09-16 19:07:17 +0800182// Set aggregate profile element
183class ApplyProfileAction : public ProfileAction {
184 public:
185 ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles)
186 : profiles_(profiles) {}
187
Bart Van Asschef096bd22022-01-24 19:59:13 +0000188 const char* Name() const override { return "ApplyProfileAction"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000189 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
190 bool ExecuteForTask(int tid) const override;
191 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
192 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
Rick Yiu0b211fa2019-09-16 19:07:17 +0800193
194 private:
195 std::vector<std::shared_ptr<TaskProfile>> profiles_;
196};
197
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800198class TaskProfiles {
199 public:
200 // Should be used by all users
201 static TaskProfiles& GetInstance();
202
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800203 TaskProfile* GetProfile(const std::string& name) const;
Bart Van Assche4c99e962022-02-03 19:50:16 +0000204 const IProfileAttribute* GetAttribute(const std::string& name) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800205 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const;
206 bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles,
207 bool use_fd_cache);
Rick Yiu0b211fa2019-09-16 19:07:17 +0800208 bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800209
210 private:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800211 std::map<std::string, std::shared_ptr<TaskProfile>> profiles_;
Bart Van Assche4c99e962022-02-03 19:50:16 +0000212 std::map<std::string, std::unique_ptr<IProfileAttribute>> attributes_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800213
214 TaskProfiles();
215
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800216 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800217};