blob: 2b2eda009146d7b514ff71603e0599495fff9447 [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:
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080048 enum ResourceCacheType { RCT_TASK = 0, RCT_PROCESS, RCT_COUNT };
49
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080050 virtual ~ProfileAction() {}
51
52 // Default implementations will fail
Greg Kaiser5c5ed9c2019-02-04 06:33:26 -080053 virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; };
54 virtual bool ExecuteForTask(int) const { return false; };
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080055
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -080056 virtual void EnableResourceCaching(ResourceCacheType) {}
57 virtual void DropResourceCaching(ResourceCacheType) {}
58
59 protected:
60 enum CacheUseResult { SUCCESS, FAIL, UNUSED };
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080061};
62
63// Profile actions
64class SetClampsAction : public ProfileAction {
65 public:
66 SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
67
68 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
69 virtual bool ExecuteForTask(int tid) const;
70
71 protected:
72 int boost_;
73 int clamp_;
74};
75
76class SetTimerSlackAction : public ProfileAction {
77 public:
78 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
79
80 virtual bool ExecuteForTask(int tid) const;
81
82 private:
83 unsigned long slack_;
84
85 static bool IsTimerSlackSupported(int tid);
86};
87
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080088// Set attribute profile element
89class SetAttributeAction : public ProfileAction {
90 public:
91 SetAttributeAction(const ProfileAttribute* attribute, const std::string& value)
92 : attribute_(attribute), value_(value) {}
93
94 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
95 virtual bool ExecuteForTask(int tid) const;
96
97 private:
98 const ProfileAttribute* attribute_;
99 std::string value_;
100};
101
Rick Yiud4c53512021-11-21 15:57:36 +0800102// Set cgroup profile element
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800103class SetCgroupAction : public ProfileAction {
Rick Yiubc1ad962020-10-26 20:32:52 +0800104 public:
Rick Yiud4c53512021-11-21 15:57:36 +0800105 SetCgroupAction(const CgroupController& c, const std::string& p);
Rick Yiubc1ad962020-10-26 20:32:52 +0800106
107 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
108 virtual bool ExecuteForTask(int tid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800109 virtual void EnableResourceCaching(ResourceCacheType cache_type);
110 virtual void DropResourceCaching(ResourceCacheType cache_type);
Rick Yiubc1ad962020-10-26 20:32:52 +0800111
Rick Yiud4c53512021-11-21 15:57:36 +0800112 const CgroupController* controller() const { return &controller_; }
113
Rick Yiubc1ad962020-10-26 20:32:52 +0800114 private:
Rick Yiud4c53512021-11-21 15:57:36 +0800115 CgroupController controller_;
116 std::string path_;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800117 android::base::unique_fd fd_[ProfileAction::RCT_COUNT];
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800118 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800119
120 static bool AddTidToCgroup(int tid, int fd, const char* controller_name);
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800121 CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const;
Rick Yiud4c53512021-11-21 15:57:36 +0800122};
123
124// Write to file action
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800125class WriteFileAction : public ProfileAction {
Rick Yiud4c53512021-11-21 15:57:36 +0800126 public:
127 WriteFileAction(const std::string& path, const std::string& value, bool logfailures);
128
129 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
130 virtual bool ExecuteForTask(int tid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800131 virtual void EnableResourceCaching(ResourceCacheType cache_type);
132 virtual void DropResourceCaching(ResourceCacheType cache_type);
Rick Yiud4c53512021-11-21 15:57:36 +0800133
134 private:
135 std::string path_, value_;
Rick Yiud76053a2021-01-25 12:44:45 +0800136 bool logfailures_;
Suren Baghdasaryanc2ee2e52022-01-20 10:58:43 -0800137 android::base::unique_fd fd_;
138 mutable std::mutex fd_mutex_;
Rick Yiud4c53512021-11-21 15:57:36 +0800139
140 static bool WriteValueToFile(const std::string& value, const std::string& path,
141 bool logfailures);
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800142 CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const;
Rick Yiubc1ad962020-10-26 20:32:52 +0800143};
144
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800145class TaskProfile {
146 public:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800147 TaskProfile() : res_cached_(false) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800148
149 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800150 void MoveTo(TaskProfile* profile);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800151
152 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
153 bool ExecuteForTask(int tid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800154 void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
155 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800156
157 private:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800158 bool res_cached_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800159 std::vector<std::unique_ptr<ProfileAction>> elements_;
160};
161
Rick Yiu0b211fa2019-09-16 19:07:17 +0800162// Set aggregate profile element
163class ApplyProfileAction : public ProfileAction {
164 public:
165 ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles)
166 : profiles_(profiles) {}
167
168 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
169 virtual bool ExecuteForTask(int tid) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800170 virtual void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type);
171 virtual void DropResourceCaching(ProfileAction::ResourceCacheType cache_type);
Rick Yiu0b211fa2019-09-16 19:07:17 +0800172
173 private:
174 std::vector<std::shared_ptr<TaskProfile>> profiles_;
175};
176
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800177class TaskProfiles {
178 public:
179 // Should be used by all users
180 static TaskProfiles& GetInstance();
181
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800182 TaskProfile* GetProfile(const std::string& name) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800183 const ProfileAttribute* GetAttribute(const std::string& name) const;
Suren Baghdasaryanf3bdac72022-01-20 15:41:28 -0800184 void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const;
185 bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles,
186 bool use_fd_cache);
Rick Yiu0b211fa2019-09-16 19:07:17 +0800187 bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800188
189 private:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800190 std::map<std::string, std::shared_ptr<TaskProfile>> profiles_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800191 std::map<std::string, std::unique_ptr<ProfileAttribute>> attributes_;
192
193 TaskProfiles();
194
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800195 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800196};