blob: df08f65c760480b72b7cc1902639ea5425869607 [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:
Bart Van Asschebc077ff2022-02-17 01:26:44 +000040 // Cgroup attributes may have different names in the v1 and v2 hierarchies. If `file_v2_name` is
41 // not empty, `file_name` is the name for the v1 hierarchy and `file_v2_name` is the name for
42 // the v2 hierarchy. If `file_v2_name` is empty, `file_name` is used for both hierarchies.
43 ProfileAttribute(const CgroupController& controller, const std::string& file_name,
44 const std::string& file_v2_name)
45 : controller_(controller), file_name_(file_name), file_v2_name_(file_v2_name) {}
Bart Van Assche4c99e962022-02-03 19:50:16 +000046 ~ProfileAttribute() = default;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080047
Bart Van Assche4c99e962022-02-03 19:50:16 +000048 const CgroupController* controller() const override { return &controller_; }
49 const std::string& file_name() const override { return file_name_; }
50 void Reset(const CgroupController& controller, const std::string& file_name) override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080051
Bart Van Assche4c99e962022-02-03 19:50:16 +000052 bool GetPathForTask(int tid, std::string* path) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080053
54 private:
Yifan Hong53e0deb2019-03-22 17:01:08 -070055 CgroupController controller_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080056 std::string file_name_;
Bart Van Asschebc077ff2022-02-17 01:26:44 +000057 std::string file_v2_name_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080058};
59
60// Abstract profile element
61class ProfileAction {
62 public:
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080063 enum ResourceCacheType { RCT_TASK = 0, RCT_PROCESS, RCT_COUNT };
64
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080065 virtual ~ProfileAction() {}
66
Bart Van Asschef096bd22022-01-24 19:59:13 +000067 virtual const char* Name() const = 0;
68
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080069 // Default implementations will fail
Greg Kaiser5c5ed9c2019-02-04 06:33:26 -080070 virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; };
71 virtual bool ExecuteForTask(int) const { return false; };
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080072
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080073 virtual void EnableResourceCaching(ResourceCacheType) {}
74 virtual void DropResourceCaching(ResourceCacheType) {}
75
76 protected:
77 enum CacheUseResult { SUCCESS, FAIL, UNUSED };
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080078};
79
80// Profile actions
81class SetClampsAction : public ProfileAction {
82 public:
83 SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
84
Bart Van Asschef096bd22022-01-24 19:59:13 +000085 const char* Name() const override { return "SetClamps"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +000086 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
87 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080088
89 protected:
90 int boost_;
91 int clamp_;
92};
93
94class SetTimerSlackAction : public ProfileAction {
95 public:
96 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
97
Bart Van Asschef096bd22022-01-24 19:59:13 +000098 const char* Name() const override { return "SetTimerSlack"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +000099 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800100
101 private:
102 unsigned long slack_;
103
104 static bool IsTimerSlackSupported(int tid);
105};
106
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800107// Set attribute profile element
108class SetAttributeAction : public ProfileAction {
109 public:
Bart Van Assche59af6802022-01-24 21:08:57 +0000110 SetAttributeAction(const IProfileAttribute* attribute, const std::string& value, bool optional)
111 : attribute_(attribute), value_(value), optional_(optional) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800112
Bart Van Asschef096bd22022-01-24 19:59:13 +0000113 const char* Name() const override { return "SetAttribute"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000114 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
115 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800116
117 private:
Bart Van Assche4c99e962022-02-03 19:50:16 +0000118 const IProfileAttribute* attribute_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800119 std::string value_;
Bart Van Assche59af6802022-01-24 21:08:57 +0000120 bool optional_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800121};
122
Rick Yiud4c53512021-11-21 15:57:36 +0800123// Set cgroup profile element
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800124class SetCgroupAction : public ProfileAction {
Rick Yiubc1ad962020-10-26 20:32:52 +0800125 public:
Rick Yiud4c53512021-11-21 15:57:36 +0800126 SetCgroupAction(const CgroupController& c, const std::string& p);
Rick Yiubc1ad962020-10-26 20:32:52 +0800127
Bart Van Asschef096bd22022-01-24 19:59:13 +0000128 const char* Name() const override { return "SetCgroup"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000129 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
130 bool ExecuteForTask(int tid) const override;
131 void EnableResourceCaching(ResourceCacheType cache_type) override;
132 void DropResourceCaching(ResourceCacheType cache_type) override;
Rick Yiubc1ad962020-10-26 20:32:52 +0800133
Rick Yiud4c53512021-11-21 15:57:36 +0800134 const CgroupController* controller() const { return &controller_; }
135
Rick Yiubc1ad962020-10-26 20:32:52 +0800136 private:
Rick Yiud4c53512021-11-21 15:57:36 +0800137 CgroupController controller_;
138 std::string path_;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800139 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800140 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800141
142 static bool AddTidToCgroup(int tid, int fd, const char* controller_name);
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800143 CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const;
Rick Yiud4c53512021-11-21 15:57:36 +0800144};
145
146// Write to file action
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800147class WriteFileAction : public ProfileAction {
Rick Yiud4c53512021-11-21 15:57:36 +0800148 public:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800149 WriteFileAction(const std::string& task_path, const std::string& proc_path,
150 const std::string& value, bool logfailures);
Rick Yiud4c53512021-11-21 15:57:36 +0800151
Bart Van Asschef096bd22022-01-24 19:59:13 +0000152 const char* Name() const override { return "WriteFile"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000153 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
154 bool ExecuteForTask(int tid) const override;
155 void EnableResourceCaching(ResourceCacheType cache_type) override;
156 void DropResourceCaching(ResourceCacheType cache_type) override;
Rick Yiud4c53512021-11-21 15:57:36 +0800157
158 private:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800159 std::string task_path_, proc_path_, value_;
Rick Yiud76053a2021-01-25 12:44:45 +0800160 bool logfailures_;
Rick Yiu9221b1e2022-02-10 16:44:43 +0800161 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800162 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800163
Rick Yiu9221b1e2022-02-10 16:44:43 +0800164 bool WriteValueToFile(const std::string& value, ResourceCacheType cache_type, int uid, int pid,
165 bool logfailures) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800166 CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const;
Rick Yiubc1ad962020-10-26 20:32:52 +0800167};
168
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800169class TaskProfile {
170 public:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000171 TaskProfile(const std::string& name) : name_(name), res_cached_(false) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800172
Bart Van Asschef096bd22022-01-24 19:59:13 +0000173 const std::string& Name() const { return name_; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800174 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800175 void MoveTo(TaskProfile* profile);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800176
177 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
178 bool ExecuteForTask(int tid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800179 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
180 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800181
182 private:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000183 const std::string name_;
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800184 bool res_cached_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800185 std::vector<std::unique_ptr<ProfileAction>> elements_;
186};
187
Rick Yiu0b211fa2019-09-16 19:07:17 +0800188// Set aggregate profile element
189class ApplyProfileAction : public ProfileAction {
190 public:
191 ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles)
192 : profiles_(profiles) {}
193
Bart Van Asschef096bd22022-01-24 19:59:13 +0000194 const char* Name() const override { return "ApplyProfileAction"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000195 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
196 bool ExecuteForTask(int tid) const override;
197 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
198 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
Rick Yiu0b211fa2019-09-16 19:07:17 +0800199
200 private:
201 std::vector<std::shared_ptr<TaskProfile>> profiles_;
202};
203
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800204class TaskProfiles {
205 public:
206 // Should be used by all users
207 static TaskProfiles& GetInstance();
208
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800209 TaskProfile* GetProfile(const std::string& name) const;
Bart Van Assche4c99e962022-02-03 19:50:16 +0000210 const IProfileAttribute* GetAttribute(const std::string& name) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800211 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const;
212 bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles,
213 bool use_fd_cache);
Rick Yiu0b211fa2019-09-16 19:07:17 +0800214 bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800215
216 private:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800217 std::map<std::string, std::shared_ptr<TaskProfile>> profiles_;
Bart Van Assche4c99e962022-02-03 19:50:16 +0000218 std::map<std::string, std::unique_ptr<IProfileAttribute>> attributes_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800219
220 TaskProfiles();
221
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800222 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800223};