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