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