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 |
| 94 | class SetClampsAction : public ProfileAction { |
| 95 | public: |
| 96 | SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {} |
| 97 | |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 98 | const char* Name() const override { return "SetClamps"; } |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame] | 99 | bool ExecuteForProcess(uid_t uid, pid_t pid) const override; |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 100 | bool ExecuteForTask(pid_t tid) const override; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 101 | |
| 102 | protected: |
| 103 | int boost_; |
| 104 | int clamp_; |
| 105 | }; |
| 106 | |
| 107 | class SetTimerSlackAction : public ProfileAction { |
| 108 | public: |
| 109 | SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {} |
| 110 | |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 111 | const char* Name() const override { return "SetTimerSlack"; } |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 112 | bool ExecuteForTask(pid_t tid) const override; |
T.J. Mercier | 44eb705 | 2024-03-27 23:40:43 +0000 | [diff] [blame] | 113 | bool IsValidForProcess(uid_t, pid_t) const override { return true; } |
| 114 | bool IsValidForTask(pid_t) const override { return true; } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 115 | |
| 116 | private: |
| 117 | unsigned long slack_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 118 | }; |
| 119 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 120 | // Set attribute profile element |
| 121 | class SetAttributeAction : public ProfileAction { |
| 122 | public: |
Bart Van Assche | 59af680 | 2022-01-24 21:08:57 +0000 | [diff] [blame] | 123 | SetAttributeAction(const IProfileAttribute* attribute, const std::string& value, bool optional) |
| 124 | : attribute_(attribute), value_(value), optional_(optional) {} |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 125 | |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 126 | const char* Name() const override { return "SetAttribute"; } |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame] | 127 | bool ExecuteForProcess(uid_t uid, pid_t pid) const override; |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 128 | bool ExecuteForTask(pid_t tid) const override; |
T.J. Mercier | 5ed5e1b | 2022-08-22 21:25:09 +0000 | [diff] [blame] | 129 | bool ExecuteForUID(uid_t uid) const override; |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 130 | bool IsValidForProcess(uid_t uid, pid_t pid) const override; |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 131 | bool IsValidForTask(pid_t tid) const override; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 132 | |
| 133 | private: |
Bart Van Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 134 | const IProfileAttribute* attribute_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 135 | std::string value_; |
Bart Van Assche | 59af680 | 2022-01-24 21:08:57 +0000 | [diff] [blame] | 136 | bool optional_; |
Suren Baghdasaryan | 3483798 | 2023-07-25 15:45:45 -0700 | [diff] [blame] | 137 | |
| 138 | bool WriteValueToFile(const std::string& path) const; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 139 | }; |
| 140 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 141 | // Set cgroup profile element |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 142 | class SetCgroupAction : public ProfileAction { |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 143 | public: |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 144 | SetCgroupAction(const CgroupControllerWrapper& c, const std::string& p); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 145 | |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 146 | const char* Name() const override { return "SetCgroup"; } |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame] | 147 | bool ExecuteForProcess(uid_t uid, pid_t pid) const override; |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 148 | bool ExecuteForTask(pid_t tid) const override; |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame] | 149 | void EnableResourceCaching(ResourceCacheType cache_type) override; |
| 150 | void DropResourceCaching(ResourceCacheType cache_type) override; |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 151 | bool IsValidForProcess(uid_t uid, pid_t pid) const override; |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 152 | bool IsValidForTask(pid_t tid) const override; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 153 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 154 | const CgroupControllerWrapper* controller() const { return &controller_; } |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 155 | |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 156 | private: |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 157 | CgroupControllerWrapper controller_; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 158 | std::string path_; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 159 | android::base::unique_fd fd_[ProfileAction::RCT_COUNT]; |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 160 | mutable std::mutex fd_mutex_; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 161 | |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 162 | bool AddTidToCgroup(pid_t tid, int fd, ResourceCacheType cache_type) const; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 163 | CacheUseResult UseCachedFd(ResourceCacheType cache_type, int id) const; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | // Write to file action |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 167 | class WriteFileAction : public ProfileAction { |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 168 | public: |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 169 | WriteFileAction(const std::string& task_path, const std::string& proc_path, |
| 170 | const std::string& value, bool logfailures); |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 171 | |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 172 | const char* Name() const override { return "WriteFile"; } |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame] | 173 | bool ExecuteForProcess(uid_t uid, pid_t pid) const override; |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 174 | bool ExecuteForTask(pid_t tid) const override; |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame] | 175 | void EnableResourceCaching(ResourceCacheType cache_type) override; |
| 176 | void DropResourceCaching(ResourceCacheType cache_type) override; |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 177 | bool IsValidForProcess(uid_t uid, pid_t pid) const override; |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 178 | bool IsValidForTask(pid_t tid) const override; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 179 | |
| 180 | private: |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 181 | std::string task_path_, proc_path_, value_; |
Rick Yiu | d76053a | 2021-01-25 12:44:45 +0800 | [diff] [blame] | 182 | bool logfailures_; |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 183 | android::base::unique_fd fd_[ProfileAction::RCT_COUNT]; |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 184 | mutable std::mutex fd_mutex_; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 185 | |
T.J. Mercier | a757eb8 | 2024-01-24 22:45:26 +0000 | [diff] [blame] | 186 | 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] | 187 | pid_t pid, bool logfailures) const; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 188 | CacheUseResult UseCachedFd(ResourceCacheType cache_type, const std::string& value) const; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 189 | }; |
| 190 | |
T.J. Mercier | 3984611 | 2024-10-09 22:40:26 +0000 | [diff] [blame^] | 191 | // Set scheduler policy action |
| 192 | class SetSchedulerPolicyAction : public ProfileAction { |
| 193 | public: |
| 194 | SetSchedulerPolicyAction(int policy) |
| 195 | : policy_(policy) {} |
| 196 | SetSchedulerPolicyAction(int policy, int priority_or_nice) |
| 197 | : policy_(policy), priority_or_nice_(priority_or_nice) {} |
| 198 | |
| 199 | const char* Name() const override { return "SetSchedulerPolicy"; } |
| 200 | bool ExecuteForTask(pid_t tid) const override; |
| 201 | |
| 202 | static bool isNormalPolicy(int policy); |
| 203 | static bool toPriority(int policy, int virtual_priority, int& priority_out); |
| 204 | |
| 205 | private: |
| 206 | int policy_; |
| 207 | std::optional<int> priority_or_nice_; |
| 208 | }; |
| 209 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 210 | class TaskProfile { |
| 211 | public: |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 212 | TaskProfile(const std::string& name) : name_(name), res_cached_(false) {} |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 213 | |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 214 | const std::string& Name() const { return name_; } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 215 | 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] | 216 | void MoveTo(TaskProfile* profile); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 217 | |
| 218 | bool ExecuteForProcess(uid_t uid, pid_t pid) const; |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 219 | bool ExecuteForTask(pid_t tid) const; |
T.J. Mercier | 5ed5e1b | 2022-08-22 21:25:09 +0000 | [diff] [blame] | 220 | bool ExecuteForUID(uid_t uid) const; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 221 | void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type); |
| 222 | void DropResourceCaching(ProfileAction::ResourceCacheType cache_type); |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 223 | bool IsValidForProcess(uid_t uid, pid_t pid) const; |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 224 | bool IsValidForTask(pid_t tid) const; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 225 | |
| 226 | private: |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 227 | const std::string name_; |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 228 | bool res_cached_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 229 | std::vector<std::unique_ptr<ProfileAction>> elements_; |
| 230 | }; |
| 231 | |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 232 | // Set aggregate profile element |
| 233 | class ApplyProfileAction : public ProfileAction { |
| 234 | public: |
| 235 | ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles) |
| 236 | : profiles_(profiles) {} |
| 237 | |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 238 | const char* Name() const override { return "ApplyProfileAction"; } |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame] | 239 | bool ExecuteForProcess(uid_t uid, pid_t pid) const override; |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 240 | bool ExecuteForTask(pid_t tid) const override; |
Bart Van Assche | 6856cfc | 2022-01-24 20:52:51 +0000 | [diff] [blame] | 241 | void EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) override; |
| 242 | void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) override; |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 243 | bool IsValidForProcess(uid_t uid, pid_t pid) const override; |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 244 | bool IsValidForTask(pid_t tid) const override; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 245 | |
| 246 | private: |
| 247 | std::vector<std::shared_ptr<TaskProfile>> profiles_; |
| 248 | }; |
| 249 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 250 | class TaskProfiles { |
| 251 | public: |
| 252 | // Should be used by all users |
| 253 | static TaskProfiles& GetInstance(); |
| 254 | |
Bart Van Assche | d0b8ce2 | 2022-08-02 13:06:26 -0700 | [diff] [blame] | 255 | TaskProfile* GetProfile(std::string_view name) const; |
| 256 | const IProfileAttribute* GetAttribute(std::string_view name) const; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 257 | void DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const; |
Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 258 | template <typename T> |
| 259 | bool SetProcessProfiles(uid_t uid, pid_t pid, std::span<const T> profiles, bool use_fd_cache); |
| 260 | template <typename T> |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 261 | 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] | 262 | template <typename T> |
| 263 | 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] | 264 | |
| 265 | private: |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 266 | TaskProfiles(); |
| 267 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 268 | bool Load(const CgroupMap& cg_map, const std::string& file_name); |
Bart Van Assche | d0b8ce2 | 2022-08-02 13:06:26 -0700 | [diff] [blame] | 269 | |
| 270 | std::map<std::string, std::shared_ptr<TaskProfile>, std::less<>> profiles_; |
| 271 | std::map<std::string, std::unique_ptr<IProfileAttribute>, std::less<>> attributes_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 272 | }; |
T.J. Mercier | d1e048f | 2024-03-28 00:33:44 +0000 | [diff] [blame] | 273 | |
| 274 | std::string ConvertUidToPath(const char* root_cgroup_path, uid_t uid); |
| 275 | std::string ConvertUidPidToPath(const char* root_cgroup_path, uid_t uid, pid_t pid); |