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 | |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame^] | 68 | bool ExecuteForProcess(uid_t uid, pid_t pid) const override; |
| 69 | bool ExecuteForTask(int tid) const override; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 70 | |
| 71 | protected: |
| 72 | int boost_; |
| 73 | int clamp_; |
| 74 | }; |
| 75 | |
| 76 | class SetTimerSlackAction : public ProfileAction { |
| 77 | public: |
| 78 | SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {} |
| 79 | |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame^] | 80 | bool ExecuteForTask(int tid) const override; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 81 | |
| 82 | private: |
| 83 | unsigned long slack_; |
| 84 | |
| 85 | static bool IsTimerSlackSupported(int tid); |
| 86 | }; |
| 87 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 88 | // Set attribute profile element |
| 89 | class SetAttributeAction : public ProfileAction { |
| 90 | public: |
| 91 | SetAttributeAction(const ProfileAttribute* attribute, const std::string& value) |
| 92 | : attribute_(attribute), value_(value) {} |
| 93 | |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame^] | 94 | bool ExecuteForProcess(uid_t uid, pid_t pid) const override; |
| 95 | bool ExecuteForTask(int tid) const override; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 96 | |
| 97 | private: |
| 98 | const ProfileAttribute* attribute_; |
| 99 | std::string value_; |
| 100 | }; |
| 101 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 102 | // Set cgroup profile element |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 103 | class SetCgroupAction : public ProfileAction { |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 104 | public: |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 105 | SetCgroupAction(const CgroupController& c, const std::string& p); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 106 | |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame^] | 107 | bool ExecuteForProcess(uid_t uid, pid_t pid) const override; |
| 108 | bool ExecuteForTask(int tid) const override; |
| 109 | void EnableResourceCaching(ResourceCacheType cache_type) override; |
| 110 | void DropResourceCaching(ResourceCacheType cache_type) override; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 111 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 112 | const CgroupController* controller() const { return &controller_; } |
| 113 | |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 114 | private: |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 115 | CgroupController controller_; |
| 116 | std::string path_; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 117 | android::base::unique_fd fd_[ProfileAction::RCT_COUNT]; |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 118 | mutable std::mutex fd_mutex_; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 119 | |
| 120 | static bool AddTidToCgroup(int tid, int fd, const char* controller_name); |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 121 | CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | // Write to file action |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 125 | class WriteFileAction : public ProfileAction { |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 126 | public: |
| 127 | WriteFileAction(const std::string& path, const std::string& value, bool logfailures); |
| 128 | |
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 | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 133 | |
| 134 | private: |
| 135 | std::string path_, value_; |
Rick Yiu | d76053a | 2021-01-25 12:44:45 +0800 | [diff] [blame] | 136 | bool logfailures_; |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 137 | android::base::unique_fd fd_; |
| 138 | mutable std::mutex fd_mutex_; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 139 | |
| 140 | static bool WriteValueToFile(const std::string& value, const std::string& path, |
| 141 | bool logfailures); |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 142 | CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 143 | }; |
| 144 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 145 | class TaskProfile { |
| 146 | public: |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 147 | TaskProfile() : res_cached_(false) {} |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 148 | |
| 149 | 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] | 150 | void MoveTo(TaskProfile* profile); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 151 | |
| 152 | bool ExecuteForProcess(uid_t uid, pid_t pid) const; |
| 153 | bool ExecuteForTask(int tid) const; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 154 | void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type); |
| 155 | void DropResourceCaching(ProfileAction::ResourceCacheType cache_type); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 156 | |
| 157 | private: |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 158 | bool res_cached_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 159 | std::vector<std::unique_ptr<ProfileAction>> elements_; |
| 160 | }; |
| 161 | |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 162 | // Set aggregate profile element |
| 163 | class ApplyProfileAction : public ProfileAction { |
| 164 | public: |
| 165 | ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles) |
| 166 | : profiles_(profiles) {} |
| 167 | |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame^] | 168 | bool ExecuteForProcess(uid_t uid, pid_t pid) const override; |
| 169 | bool ExecuteForTask(int tid) const override; |
| 170 | void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) override; |
| 171 | void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) override; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 172 | |
| 173 | private: |
| 174 | std::vector<std::shared_ptr<TaskProfile>> profiles_; |
| 175 | }; |
| 176 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 177 | class TaskProfiles { |
| 178 | public: |
| 179 | // Should be used by all users |
| 180 | static TaskProfiles& GetInstance(); |
| 181 | |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 182 | TaskProfile* GetProfile(const std::string& name) const; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 183 | const ProfileAttribute* GetAttribute(const std::string& name) const; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 184 | 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 Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 187 | 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] | 188 | |
| 189 | private: |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 190 | std::map<std::string, std::shared_ptr<TaskProfile>> profiles_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 191 | std::map<std::string, std::unique_ptr<ProfileAttribute>> attributes_; |
| 192 | |
| 193 | TaskProfiles(); |
| 194 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 195 | bool Load(const CgroupMap& cg_map, const std::string& file_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 196 | }; |