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