blob: 85b3f9162f7c91e4c47eedb6174b7f4ad08a34b4 [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;
35 virtual void Reset(const CgroupController& controller, const std::string& file_name) = 0;
36 virtual const CgroupController* controller() const = 0;
37 virtual const std::string& file_name() const = 0;
38 virtual bool GetPathForTask(int tid, std::string* path) const = 0;
39};
40
41class ProfileAttribute : public IProfileAttribute {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080042 public:
Bart Van Asschebc077ff2022-02-17 01:26:44 +000043 // Cgroup attributes may have different names in the v1 and v2 hierarchies. If `file_v2_name` is
44 // not empty, `file_name` is the name for the v1 hierarchy and `file_v2_name` is the name for
45 // the v2 hierarchy. If `file_v2_name` is empty, `file_name` is used for both hierarchies.
46 ProfileAttribute(const CgroupController& controller, const std::string& file_name,
47 const std::string& file_v2_name)
48 : controller_(controller), file_name_(file_name), file_v2_name_(file_v2_name) {}
Bart Van Assche4c99e962022-02-03 19:50:16 +000049 ~ProfileAttribute() = default;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080050
Bart Van Assche4c99e962022-02-03 19:50:16 +000051 const CgroupController* controller() const override { return &controller_; }
52 const std::string& file_name() const override { return file_name_; }
53 void Reset(const CgroupController& controller, const std::string& file_name) override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080054
Bart Van Assche4c99e962022-02-03 19:50:16 +000055 bool GetPathForTask(int tid, std::string* path) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080056
57 private:
Yifan Hong53e0deb2019-03-22 17:01:08 -070058 CgroupController controller_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080059 std::string file_name_;
Bart Van Asschebc077ff2022-02-17 01:26:44 +000060 std::string file_v2_name_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080061};
62
63// Abstract profile element
64class ProfileAction {
65 public:
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080066 enum ResourceCacheType { RCT_TASK = 0, RCT_PROCESS, RCT_COUNT };
67
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080068 virtual ~ProfileAction() {}
69
Bart Van Asschef096bd22022-01-24 19:59:13 +000070 virtual const char* Name() const = 0;
71
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080072 // Default implementations will fail
Greg Kaiser5c5ed9c2019-02-04 06:33:26 -080073 virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; };
74 virtual bool ExecuteForTask(int) const { return false; };
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080075
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080076 virtual void EnableResourceCaching(ResourceCacheType) {}
77 virtual void DropResourceCaching(ResourceCacheType) {}
78
79 protected:
80 enum CacheUseResult { SUCCESS, FAIL, UNUSED };
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080081};
82
83// Profile actions
84class SetClampsAction : public ProfileAction {
85 public:
86 SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
87
Bart Van Asschef096bd22022-01-24 19:59:13 +000088 const char* Name() const override { return "SetClamps"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +000089 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
90 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080091
92 protected:
93 int boost_;
94 int clamp_;
95};
96
97class SetTimerSlackAction : public ProfileAction {
98 public:
99 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
100
Bart Van Asschef096bd22022-01-24 19:59:13 +0000101 const char* Name() const override { return "SetTimerSlack"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000102 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800103
104 private:
105 unsigned long slack_;
106
107 static bool IsTimerSlackSupported(int tid);
108};
109
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800110// Set attribute profile element
111class SetAttributeAction : public ProfileAction {
112 public:
Bart Van Assche59af6802022-01-24 21:08:57 +0000113 SetAttributeAction(const IProfileAttribute* attribute, const std::string& value, bool optional)
114 : attribute_(attribute), value_(value), optional_(optional) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800115
Bart Van Asschef096bd22022-01-24 19:59:13 +0000116 const char* Name() const override { return "SetAttribute"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000117 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
118 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800119
120 private:
Bart Van Assche4c99e962022-02-03 19:50:16 +0000121 const IProfileAttribute* attribute_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800122 std::string value_;
Bart Van Assche59af6802022-01-24 21:08:57 +0000123 bool optional_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800124};
125
Rick Yiud4c53512021-11-21 15:57:36 +0800126// Set cgroup profile element
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800127class SetCgroupAction : public ProfileAction {
Rick Yiubc1ad962020-10-26 20:32:52 +0800128 public:
Rick Yiud4c53512021-11-21 15:57:36 +0800129 SetCgroupAction(const CgroupController& c, const std::string& p);
Rick Yiubc1ad962020-10-26 20:32:52 +0800130
Bart Van Asschef096bd22022-01-24 19:59:13 +0000131 const char* Name() const override { return "SetCgroup"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000132 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
133 bool ExecuteForTask(int tid) const override;
134 void EnableResourceCaching(ResourceCacheType cache_type) override;
135 void DropResourceCaching(ResourceCacheType cache_type) override;
Rick Yiubc1ad962020-10-26 20:32:52 +0800136
Rick Yiud4c53512021-11-21 15:57:36 +0800137 const CgroupController* controller() const { return &controller_; }
138
Rick Yiubc1ad962020-10-26 20:32:52 +0800139 private:
Rick Yiud4c53512021-11-21 15:57:36 +0800140 CgroupController controller_;
141 std::string path_;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800142 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800143 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800144
145 static bool AddTidToCgroup(int tid, int fd, const char* controller_name);
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800146 CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const;
Rick Yiud4c53512021-11-21 15:57:36 +0800147};
148
149// Write to file action
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800150class WriteFileAction : public ProfileAction {
Rick Yiud4c53512021-11-21 15:57:36 +0800151 public:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800152 WriteFileAction(const std::string& task_path, const std::string& proc_path,
153 const std::string& value, bool logfailures);
Rick Yiud4c53512021-11-21 15:57:36 +0800154
Bart Van Asschef096bd22022-01-24 19:59:13 +0000155 const char* Name() const override { return "WriteFile"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000156 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
157 bool ExecuteForTask(int tid) const override;
158 void EnableResourceCaching(ResourceCacheType cache_type) override;
159 void DropResourceCaching(ResourceCacheType cache_type) override;
Rick Yiud4c53512021-11-21 15:57:36 +0800160
161 private:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800162 std::string task_path_, proc_path_, value_;
Rick Yiud76053a2021-01-25 12:44:45 +0800163 bool logfailures_;
Rick Yiu9221b1e2022-02-10 16:44:43 +0800164 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800165 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800166
Rick Yiu9221b1e2022-02-10 16:44:43 +0800167 bool WriteValueToFile(const std::string& value, ResourceCacheType cache_type, int uid, int pid,
168 bool logfailures) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800169 CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const;
Rick Yiubc1ad962020-10-26 20:32:52 +0800170};
171
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800172class TaskProfile {
173 public:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000174 TaskProfile(const std::string& name) : name_(name), res_cached_(false) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800175
Bart Van Asschef096bd22022-01-24 19:59:13 +0000176 const std::string& Name() const { return name_; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800177 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800178 void MoveTo(TaskProfile* profile);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800179
180 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
181 bool ExecuteForTask(int tid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800182 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
183 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800184
185 private:
Bart Van Asschef096bd22022-01-24 19:59:13 +0000186 const std::string name_;
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800187 bool res_cached_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800188 std::vector<std::unique_ptr<ProfileAction>> elements_;
189};
190
Rick Yiu0b211fa2019-09-16 19:07:17 +0800191// Set aggregate profile element
192class ApplyProfileAction : public ProfileAction {
193 public:
194 ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles)
195 : profiles_(profiles) {}
196
Bart Van Asschef096bd22022-01-24 19:59:13 +0000197 const char* Name() const override { return "ApplyProfileAction"; }
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000198 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
199 bool ExecuteForTask(int tid) const override;
200 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
201 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
Rick Yiu0b211fa2019-09-16 19:07:17 +0800202
203 private:
204 std::vector<std::shared_ptr<TaskProfile>> profiles_;
205};
206
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800207class TaskProfiles {
208 public:
209 // Should be used by all users
210 static TaskProfiles& GetInstance();
211
Bart Van Assched0b8ce22022-08-02 13:06:26 -0700212 TaskProfile* GetProfile(std::string_view name) const;
213 const IProfileAttribute* GetAttribute(std::string_view name) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800214 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const;
Bart Van Asschef32c4ec2022-08-02 13:18:12 -0700215 template <typename T>
216 bool SetProcessProfiles(uid_t uid, pid_t pid, std::span<const T> profiles, bool use_fd_cache);
217 template <typename T>
218 bool SetTaskProfiles(int tid, std::span<const T> profiles, bool use_fd_cache);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800219
220 private:
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800221 TaskProfiles();
222
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800223 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Bart Van Assched0b8ce22022-08-02 13:06:26 -0700224
225 std::map<std::string, std::shared_ptr<TaskProfile>, std::less<>> profiles_;
226 std::map<std::string, std::unique_ptr<IProfileAttribute>, std::less<>> attributes_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800227};