blob: ecc67a31290046e6f6eb3fa315e122734392838c [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
62 // Default implementations will fail
Greg Kaiser5c5ed9c2019-02-04 06:33:26 -080063 virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; };
64 virtual bool ExecuteForTask(int) const { return false; };
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080065
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080066 virtual void EnableResourceCaching(ResourceCacheType) {}
67 virtual void DropResourceCaching(ResourceCacheType) {}
68
69 protected:
70 enum CacheUseResult { SUCCESS, FAIL, UNUSED };
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080071};
72
73// Profile actions
74class SetClampsAction : public ProfileAction {
75 public:
76 SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
77
Bart Van Assche6856cfc2022-01-24 20:52:51 +000078 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
79 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080080
81 protected:
82 int boost_;
83 int clamp_;
84};
85
86class SetTimerSlackAction : public ProfileAction {
87 public:
88 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
89
Bart Van Assche6856cfc2022-01-24 20:52:51 +000090 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080091
92 private:
93 unsigned long slack_;
94
95 static bool IsTimerSlackSupported(int tid);
96};
97
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080098// Set attribute profile element
99class SetAttributeAction : public ProfileAction {
100 public:
Bart Van Assche4c99e962022-02-03 19:50:16 +0000101 SetAttributeAction(const IProfileAttribute* attribute, const std::string& value)
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800102 : attribute_(attribute), value_(value) {}
103
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000104 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
105 bool ExecuteForTask(int tid) const override;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800106
107 private:
Bart Van Assche4c99e962022-02-03 19:50:16 +0000108 const IProfileAttribute* attribute_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800109 std::string value_;
110};
111
Rick Yiud4c53512021-11-21 15:57:36 +0800112// Set cgroup profile element
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800113class SetCgroupAction : public ProfileAction {
Rick Yiubc1ad962020-10-26 20:32:52 +0800114 public:
Rick Yiud4c53512021-11-21 15:57:36 +0800115 SetCgroupAction(const CgroupController& c, const std::string& p);
Rick Yiubc1ad962020-10-26 20:32:52 +0800116
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;
119 void EnableResourceCaching(ResourceCacheType cache_type) override;
120 void DropResourceCaching(ResourceCacheType cache_type) override;
Rick Yiubc1ad962020-10-26 20:32:52 +0800121
Rick Yiud4c53512021-11-21 15:57:36 +0800122 const CgroupController* controller() const { return &controller_; }
123
Rick Yiubc1ad962020-10-26 20:32:52 +0800124 private:
Rick Yiud4c53512021-11-21 15:57:36 +0800125 CgroupController controller_;
126 std::string path_;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800127 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800128 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800129
130 static bool AddTidToCgroup(int tid, int fd, const char* controller_name);
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800131 CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const;
Rick Yiud4c53512021-11-21 15:57:36 +0800132};
133
134// Write to file action
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800135class WriteFileAction : public ProfileAction {
Rick Yiud4c53512021-11-21 15:57:36 +0800136 public:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800137 WriteFileAction(const std::string& task_path, const std::string& proc_path,
138 const std::string& value, bool logfailures);
Rick Yiud4c53512021-11-21 15:57:36 +0800139
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000140 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
141 bool ExecuteForTask(int tid) const override;
142 void EnableResourceCaching(ResourceCacheType cache_type) override;
143 void DropResourceCaching(ResourceCacheType cache_type) override;
Rick Yiud4c53512021-11-21 15:57:36 +0800144
145 private:
Rick Yiu9221b1e2022-02-10 16:44:43 +0800146 std::string task_path_, proc_path_, value_;
Rick Yiud76053a2021-01-25 12:44:45 +0800147 bool logfailures_;
Rick Yiu9221b1e2022-02-10 16:44:43 +0800148 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800149 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800150
Rick Yiu9221b1e2022-02-10 16:44:43 +0800151 bool WriteValueToFile(const std::string& value, ResourceCacheType cache_type, int uid, int pid,
152 bool logfailures) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800153 CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const;
Rick Yiubc1ad962020-10-26 20:32:52 +0800154};
155
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800156class TaskProfile {
157 public:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800158 TaskProfile() : res_cached_(false) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800159
160 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800161 void MoveTo(TaskProfile* profile);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800162
163 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
164 bool ExecuteForTask(int tid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800165 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
166 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800167
168 private:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800169 bool res_cached_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800170 std::vector<std::unique_ptr<ProfileAction>> elements_;
171};
172
Rick Yiu0b211fa2019-09-16 19:07:17 +0800173// Set aggregate profile element
174class ApplyProfileAction : public ProfileAction {
175 public:
176 ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles)
177 : profiles_(profiles) {}
178
Bart Van Assche6856cfc2022-01-24 20:52:51 +0000179 bool ExecuteForProcess(uid_t uid, pid_t pid) const override;
180 bool ExecuteForTask(int tid) const override;
181 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
182 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) override;
Rick Yiu0b211fa2019-09-16 19:07:17 +0800183
184 private:
185 std::vector<std::shared_ptr<TaskProfile>> profiles_;
186};
187
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800188class TaskProfiles {
189 public:
190 // Should be used by all users
191 static TaskProfiles& GetInstance();
192
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800193 TaskProfile* GetProfile(const std::string& name) const;
Bart Van Assche4c99e962022-02-03 19:50:16 +0000194 const IProfileAttribute* GetAttribute(const std::string& name) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800195 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const;
196 bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles,
197 bool use_fd_cache);
Rick Yiu0b211fa2019-09-16 19:07:17 +0800198 bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800199
200 private:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800201 std::map<std::string, std::shared_ptr<TaskProfile>> profiles_;
Bart Van Assche4c99e962022-02-03 19:50:16 +0000202 std::map<std::string, std::unique_ptr<IProfileAttribute>> attributes_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800203
204 TaskProfiles();
205
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800206 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800207};