blob: 278892dd55f634033214a409abae62d34720bf7b [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
29class ProfileAttribute {
30 public:
Yifan Hong53e0deb2019-03-22 17:01:08 -070031 ProfileAttribute(const CgroupController& controller, const std::string& file_name)
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080032 : controller_(controller), file_name_(file_name) {}
33
Yifan Hong53e0deb2019-03-22 17:01:08 -070034 const CgroupController* controller() const { return &controller_; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080035 const std::string& file_name() const { return file_name_; }
Suren Baghdasaryan81b9f0b2020-07-01 12:34:17 -070036 void Reset(const CgroupController& controller, const std::string& file_name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080037
38 bool GetPathForTask(int tid, std::string* path) const;
39
40 private:
Yifan Hong53e0deb2019-03-22 17:01:08 -070041 CgroupController controller_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080042 std::string file_name_;
43};
44
45// Abstract profile element
46class ProfileAction {
47 public:
48 virtual ~ProfileAction() {}
49
50 // Default implementations will fail
Greg Kaiser5c5ed9c2019-02-04 06:33:26 -080051 virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; };
52 virtual bool ExecuteForTask(int) const { return false; };
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080053
54 virtual void EnableResourceCaching() {}
Riddle Hsua6abd822019-06-18 15:53:53 -060055 virtual void DropResourceCaching() {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080056};
57
58// Profile actions
59class SetClampsAction : public ProfileAction {
60 public:
61 SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
62
63 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
64 virtual bool ExecuteForTask(int tid) const;
65
66 protected:
67 int boost_;
68 int clamp_;
69};
70
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080071// To avoid issues in sdk_mac build
72#if defined(__ANDROID__)
73
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080074class SetTimerSlackAction : public ProfileAction {
75 public:
76 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
77
78 virtual bool ExecuteForTask(int tid) const;
79
80 private:
81 unsigned long slack_;
82
83 static bool IsTimerSlackSupported(int tid);
84};
85
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080086#else
87
88class SetTimerSlackAction : public ProfileAction {
89 public:
90 SetTimerSlackAction(unsigned long) noexcept {}
91
92 virtual bool ExecuteForTask(int) const { return true; }
93};
94
95#endif
96
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080097// Set attribute profile element
98class SetAttributeAction : public ProfileAction {
99 public:
100 SetAttributeAction(const ProfileAttribute* attribute, const std::string& value)
101 : attribute_(attribute), value_(value) {}
102
103 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
104 virtual bool ExecuteForTask(int tid) const;
105
106 private:
107 const ProfileAttribute* attribute_;
108 std::string value_;
109};
110
Rick Yiud4c53512021-11-21 15:57:36 +0800111// Abstract profile element for cached fd
112class CachedFdProfileAction : public ProfileAction {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800113 public:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800114 virtual void EnableResourceCaching();
Riddle Hsua6abd822019-06-18 15:53:53 -0600115 virtual void DropResourceCaching();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800116
Rick Yiud4c53512021-11-21 15:57:36 +0800117 protected:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800118 enum FdState {
119 FDS_INACCESSIBLE = -1,
120 FDS_APP_DEPENDENT = -2,
121 FDS_NOT_CACHED = -3,
122 };
123
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800124 android::base::unique_fd fd_;
mtk1603653f79e62019-05-31 19:05:22 +0800125 mutable std::mutex fd_mutex_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800126
127 static bool IsAppDependentPath(const std::string& path);
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800128
Rick Yiud4c53512021-11-21 15:57:36 +0800129 void InitFd(const std::string& path);
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800130 bool IsFdValid() const { return fd_ > FDS_INACCESSIBLE; }
Rick Yiud4c53512021-11-21 15:57:36 +0800131
132 virtual const std::string GetPath() const = 0;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800133};
134
Rick Yiud4c53512021-11-21 15:57:36 +0800135// Set cgroup profile element
136class SetCgroupAction : public CachedFdProfileAction {
Rick Yiubc1ad962020-10-26 20:32:52 +0800137 public:
Rick Yiud4c53512021-11-21 15:57:36 +0800138 SetCgroupAction(const CgroupController& c, const std::string& p);
Rick Yiubc1ad962020-10-26 20:32:52 +0800139
140 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
141 virtual bool ExecuteForTask(int tid) const;
142
Rick Yiud4c53512021-11-21 15:57:36 +0800143 const CgroupController* controller() const { return &controller_; }
144
145 protected:
146 const std::string GetPath() const override { return controller_.GetTasksFilePath(path_); }
147
Rick Yiubc1ad962020-10-26 20:32:52 +0800148 private:
Rick Yiud4c53512021-11-21 15:57:36 +0800149 CgroupController controller_;
150 std::string path_;
151
152 static bool AddTidToCgroup(int tid, int fd, const char* controller_name);
153};
154
155// Write to file action
156class WriteFileAction : public CachedFdProfileAction {
157 public:
158 WriteFileAction(const std::string& path, const std::string& value, bool logfailures);
159
160 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
161 virtual bool ExecuteForTask(int tid) const;
162
163 protected:
164 const std::string GetPath() const override { return path_; }
165
166 private:
167 std::string path_, value_;
Rick Yiud76053a2021-01-25 12:44:45 +0800168 bool logfailures_;
Rick Yiud4c53512021-11-21 15:57:36 +0800169
170 static bool WriteValueToFile(const std::string& value, const std::string& path,
171 bool logfailures);
Rick Yiubc1ad962020-10-26 20:32:52 +0800172};
173
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800174class TaskProfile {
175 public:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800176 TaskProfile() : res_cached_(false) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800177
178 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800179 void MoveTo(TaskProfile* profile);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800180
181 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
182 bool ExecuteForTask(int tid) const;
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800183 void EnableResourceCaching();
Riddle Hsua6abd822019-06-18 15:53:53 -0600184 void DropResourceCaching();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800185
186 private:
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
197 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
198 virtual bool ExecuteForTask(int tid) const;
Suren Baghdasaryan911109c2020-02-13 17:28:00 -0800199 virtual void EnableResourceCaching();
200 virtual void DropResourceCaching();
Rick Yiu0b211fa2019-09-16 19:07:17 +0800201
202 private:
203 std::vector<std::shared_ptr<TaskProfile>> profiles_;
204};
205
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800206class TaskProfiles {
207 public:
208 // Should be used by all users
209 static TaskProfiles& GetInstance();
210
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800211 TaskProfile* GetProfile(const std::string& name) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800212 const ProfileAttribute* GetAttribute(const std::string& name) const;
Riddle Hsua6abd822019-06-18 15:53:53 -0600213 void DropResourceCaching() const;
Suren Baghdasaryan911109c2020-02-13 17:28:00 -0800214 bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles);
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:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800218 std::map<std::string, std::shared_ptr<TaskProfile>> profiles_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800219 std::map<std::string, std::unique_ptr<ProfileAttribute>> attributes_;
220
221 TaskProfiles();
222
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800223 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800224};