Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 1 | /* |
| 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> |
mtk16036 | 53f79e6 | 2019-05-31 19:05:22 +0800 | [diff] [blame] | 22 | #include <mutex> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <android-base/unique_fd.h> |
| 27 | #include <cgroup_map.h> |
| 28 | |
Bart Van Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 29 | class 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 | |
| 38 | class ProfileAttribute : public IProfileAttribute { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 39 | public: |
Bart Van Assche | bc077ff | 2022-02-17 01:26:44 +0000 | [diff] [blame] | 40 | // 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 Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 46 | ~ProfileAttribute() = default; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 47 | |
Bart Van Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 48 | 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 Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 51 | |
Bart Van Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 52 | bool GetPathForTask(int tid, std::string* path) const override; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 53 | |
| 54 | private: |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 55 | CgroupController controller_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 56 | std::string file_name_; |
Bart Van Assche | bc077ff | 2022-02-17 01:26:44 +0000 | [diff] [blame] | 57 | std::string file_v2_name_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | // Abstract profile element |
| 61 | class ProfileAction { |
| 62 | public: |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 63 | enum ResourceCacheType { RCT_TASK = 0, RCT_PROCESS, RCT_COUNT }; |
| 64 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 65 | virtual ~ProfileAction() {} |
| 66 | |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 67 | virtual const char* Name() const = 0; |
| 68 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 69 | // Default implementations will fail |
Greg Kaiser | 5c5ed9c | 2019-02-04 06:33:26 -0800 | [diff] [blame] | 70 | virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; }; |
| 71 | virtual bool ExecuteForTask(int) const { return false; }; |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 72 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 73 | virtual void EnableResourceCaching(ResourceCacheType) {} |
| 74 | virtual void DropResourceCaching(ResourceCacheType) {} |
| 75 | |
| 76 | protected: |
| 77 | enum CacheUseResult { SUCCESS, FAIL, UNUSED }; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | // Profile actions |
| 81 | class SetClampsAction : public ProfileAction { |
| 82 | public: |
| 83 | SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {} |
| 84 | |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 85 | const char* Name() const override { return "SetClamps"; } |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame] | 86 | bool ExecuteForProcess(uid_t uid, pid_t pid) const override; |
| 87 | bool ExecuteForTask(int tid) const override; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 88 | |
| 89 | protected: |
| 90 | int boost_; |
| 91 | int clamp_; |
| 92 | }; |
| 93 | |
| 94 | class SetTimerSlackAction : public ProfileAction { |
| 95 | public: |
| 96 | SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {} |
| 97 | |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 98 | const char* Name() const override { return "SetTimerSlack"; } |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame] | 99 | bool ExecuteForTask(int tid) const override; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 100 | |
| 101 | private: |
| 102 | unsigned long slack_; |
| 103 | |
| 104 | static bool IsTimerSlackSupported(int tid); |
| 105 | }; |
| 106 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 107 | // Set attribute profile element |
| 108 | class SetAttributeAction : public ProfileAction { |
| 109 | public: |
Bart Van Assche | 59af680 | 2022-01-24 21:08:57 +0000 | [diff] [blame] | 110 | SetAttributeAction(const IProfileAttribute* attribute, const std::string& value, bool optional) |
| 111 | : attribute_(attribute), value_(value), optional_(optional) {} |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 112 | |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 113 | const char* Name() const override { return "SetAttribute"; } |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame] | 114 | bool ExecuteForProcess(uid_t uid, pid_t pid) const override; |
| 115 | bool ExecuteForTask(int tid) const override; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 116 | |
| 117 | private: |
Bart Van Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 118 | const IProfileAttribute* attribute_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 119 | std::string value_; |
Bart Van Assche | 59af680 | 2022-01-24 21:08:57 +0000 | [diff] [blame] | 120 | bool optional_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 123 | // Set cgroup profile element |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 124 | class SetCgroupAction : public ProfileAction { |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 125 | public: |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 126 | SetCgroupAction(const CgroupController& c, const std::string& p); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 127 | |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 128 | const char* Name() const override { return "SetCgroup"; } |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame] | 129 | 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 Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 133 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 134 | const CgroupController* controller() const { return &controller_; } |
| 135 | |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 136 | private: |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 137 | CgroupController controller_; |
| 138 | std::string path_; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 139 | android::base::unique_fd fd_[ProfileAction::RCT_COUNT]; |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 140 | mutable std::mutex fd_mutex_; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 141 | |
| 142 | static bool AddTidToCgroup(int tid, int fd, const char* controller_name); |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 143 | CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | // Write to file action |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 147 | class WriteFileAction : public ProfileAction { |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 148 | public: |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 149 | WriteFileAction(const std::string& task_path, const std::string& proc_path, |
| 150 | const std::string& value, bool logfailures); |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 151 | |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 152 | const char* Name() const override { return "WriteFile"; } |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame] | 153 | 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 Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 157 | |
| 158 | private: |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 159 | std::string task_path_, proc_path_, value_; |
Rick Yiu | d76053a | 2021-01-25 12:44:45 +0800 | [diff] [blame] | 160 | bool logfailures_; |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 161 | android::base::unique_fd fd_[ProfileAction::RCT_COUNT]; |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 162 | mutable std::mutex fd_mutex_; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 163 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 164 | bool WriteValueToFile(const std::string& value, ResourceCacheType cache_type, int uid, int pid, |
| 165 | bool logfailures) const; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 166 | CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 167 | }; |
| 168 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 169 | class TaskProfile { |
| 170 | public: |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 171 | TaskProfile(const std::string& name) : name_(name), res_cached_(false) {} |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 172 | |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 173 | const std::string& Name() const { return name_; } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 174 | void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); } |
Suren Baghdasaryan | 8438595 | 2020-01-24 16:36:10 -0800 | [diff] [blame] | 175 | void MoveTo(TaskProfile* profile); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 176 | |
| 177 | bool ExecuteForProcess(uid_t uid, pid_t pid) const; |
| 178 | bool ExecuteForTask(int tid) const; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 179 | void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type); |
| 180 | void DropResourceCaching(ProfileAction::ResourceCacheType cache_type); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 181 | |
| 182 | private: |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 183 | const std::string name_; |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 184 | bool res_cached_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 185 | std::vector<std::unique_ptr<ProfileAction>> elements_; |
| 186 | }; |
| 187 | |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 188 | // Set aggregate profile element |
| 189 | class ApplyProfileAction : public ProfileAction { |
| 190 | public: |
| 191 | ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles) |
| 192 | : profiles_(profiles) {} |
| 193 | |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 194 | const char* Name() const override { return "ApplyProfileAction"; } |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame] | 195 | 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 Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 199 | |
| 200 | private: |
| 201 | std::vector<std::shared_ptr<TaskProfile>> profiles_; |
| 202 | }; |
| 203 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 204 | class TaskProfiles { |
| 205 | public: |
| 206 | // Should be used by all users |
| 207 | static TaskProfiles& GetInstance(); |
| 208 | |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 209 | TaskProfile* GetProfile(const std::string& name) const; |
Bart Van Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 210 | const IProfileAttribute* GetAttribute(const std::string& name) const; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 211 | 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 Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 214 | bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 215 | |
| 216 | private: |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 217 | std::map<std::string, std::shared_ptr<TaskProfile>> profiles_; |
Bart Van Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 218 | std::map<std::string, std::unique_ptr<IProfileAttribute>> attributes_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 219 | |
| 220 | TaskProfiles(); |
| 221 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 222 | bool Load(const CgroupMap& cg_map, const std::string& file_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 223 | }; |