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