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 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "libprocessgroup" |
| 19 | |
T.J. Mercier | 3984611 | 2024-10-09 22:40:26 +0000 | [diff] [blame^] | 20 | #include <task_profiles.h> |
| 21 | |
| 22 | #include <map> |
| 23 | #include <string> |
| 24 | |
T.J. Mercier | 54bfde0 | 2024-06-04 23:25:29 +0000 | [diff] [blame] | 25 | #include <dirent.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 26 | #include <fcntl.h> |
T.J. Mercier | 3984611 | 2024-10-09 22:40:26 +0000 | [diff] [blame^] | 27 | #include <sched.h> |
| 28 | #include <sys/resource.h> |
T.J. Mercier | 54bfde0 | 2024-06-04 23:25:29 +0000 | [diff] [blame] | 29 | #include <unistd.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 30 | |
| 31 | #include <android-base/file.h> |
| 32 | #include <android-base/logging.h> |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 33 | #include <android-base/properties.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 34 | #include <android-base/stringprintf.h> |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 35 | #include <android-base/strings.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 36 | #include <android-base/threads.h> |
| 37 | |
T.J. Mercier | 3984611 | 2024-10-09 22:40:26 +0000 | [diff] [blame^] | 38 | #include <build_flags.h> |
| 39 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 40 | #include <cutils/android_filesystem_config.h> |
| 41 | |
| 42 | #include <json/reader.h> |
| 43 | #include <json/value.h> |
| 44 | |
| 45 | using android::base::GetThreadId; |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 46 | using android::base::GetUintProperty; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 47 | using android::base::StringPrintf; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 48 | using android::base::StringReplace; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 49 | using android::base::unique_fd; |
| 50 | using android::base::WriteStringToFile; |
| 51 | |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 52 | static constexpr const char* TASK_PROFILE_DB_FILE = "/etc/task_profiles.json"; |
| 53 | static constexpr const char* TASK_PROFILE_DB_VENDOR_FILE = "/vendor/etc/task_profiles.json"; |
| 54 | |
| 55 | static constexpr const char* TEMPLATE_TASK_PROFILE_API_FILE = |
| 56 | "/etc/task_profiles/task_profiles_%u.json"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 57 | |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 58 | class FdCacheHelper { |
| 59 | public: |
| 60 | enum FdState { |
| 61 | FDS_INACCESSIBLE = -1, |
| 62 | FDS_APP_DEPENDENT = -2, |
| 63 | FDS_NOT_CACHED = -3, |
| 64 | }; |
| 65 | |
| 66 | static void Cache(const std::string& path, android::base::unique_fd& fd); |
| 67 | static void Drop(android::base::unique_fd& fd); |
| 68 | static void Init(const std::string& path, android::base::unique_fd& fd); |
| 69 | static bool IsCached(const android::base::unique_fd& fd) { return fd > FDS_INACCESSIBLE; } |
| 70 | |
| 71 | private: |
| 72 | static bool IsAppDependentPath(const std::string& path); |
| 73 | }; |
| 74 | |
| 75 | void FdCacheHelper::Init(const std::string& path, android::base::unique_fd& fd) { |
| 76 | // file descriptors for app-dependent paths can't be cached |
| 77 | if (IsAppDependentPath(path)) { |
| 78 | // file descriptor is not cached |
| 79 | fd.reset(FDS_APP_DEPENDENT); |
| 80 | return; |
| 81 | } |
| 82 | // file descriptor can be cached later on request |
| 83 | fd.reset(FDS_NOT_CACHED); |
| 84 | } |
| 85 | |
| 86 | void FdCacheHelper::Cache(const std::string& path, android::base::unique_fd& fd) { |
| 87 | if (fd != FDS_NOT_CACHED) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | if (access(path.c_str(), W_OK) != 0) { |
| 92 | // file is not accessible |
| 93 | fd.reset(FDS_INACCESSIBLE); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_WRONLY | O_CLOEXEC))); |
| 98 | if (tmp_fd < 0) { |
| 99 | PLOG(ERROR) << "Failed to cache fd '" << path << "'"; |
| 100 | fd.reset(FDS_INACCESSIBLE); |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | fd = std::move(tmp_fd); |
| 105 | } |
| 106 | |
| 107 | void FdCacheHelper::Drop(android::base::unique_fd& fd) { |
| 108 | if (fd == FDS_NOT_CACHED) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | fd.reset(FDS_NOT_CACHED); |
| 113 | } |
| 114 | |
| 115 | bool FdCacheHelper::IsAppDependentPath(const std::string& path) { |
| 116 | return path.find("<uid>", 0) != std::string::npos || path.find("<pid>", 0) != std::string::npos; |
| 117 | } |
| 118 | |
Bart Van Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 119 | IProfileAttribute::~IProfileAttribute() = default; |
| 120 | |
Suren Baghdasaryan | 3507846 | 2023-07-25 14:50:18 -0700 | [diff] [blame] | 121 | const std::string& ProfileAttribute::file_name() const { |
| 122 | if (controller()->version() == 2 && !file_v2_name_.empty()) return file_v2_name_; |
| 123 | return file_name_; |
| 124 | } |
| 125 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 126 | void ProfileAttribute::Reset(const CgroupControllerWrapper& controller, |
| 127 | const std::string& file_name, const std::string& file_v2_name) { |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 128 | controller_ = controller; |
| 129 | file_name_ = file_name; |
Suren Baghdasaryan | 3507846 | 2023-07-25 14:50:18 -0700 | [diff] [blame] | 130 | file_v2_name_ = file_v2_name; |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 131 | } |
| 132 | |
T.J. Mercier | 1cfa2c4 | 2024-04-08 21:14:32 +0000 | [diff] [blame] | 133 | static bool isSystemApp(uid_t uid) { |
| 134 | return uid < AID_APP_START; |
| 135 | } |
| 136 | |
T.J. Mercier | d1e048f | 2024-03-28 00:33:44 +0000 | [diff] [blame] | 137 | std::string ConvertUidToPath(const char* root_cgroup_path, uid_t uid) { |
T.J. Mercier | 1cfa2c4 | 2024-04-08 21:14:32 +0000 | [diff] [blame] | 138 | if (android::libprocessgroup_flags::cgroup_v2_sys_app_isolation()) { |
| 139 | if (isSystemApp(uid)) |
| 140 | return StringPrintf("%s/system/uid_%u", root_cgroup_path, uid); |
| 141 | else |
| 142 | return StringPrintf("%s/apps/uid_%u", root_cgroup_path, uid); |
| 143 | } |
T.J. Mercier | d1e048f | 2024-03-28 00:33:44 +0000 | [diff] [blame] | 144 | return StringPrintf("%s/uid_%u", root_cgroup_path, uid); |
| 145 | } |
| 146 | |
| 147 | std::string ConvertUidPidToPath(const char* root_cgroup_path, uid_t uid, pid_t pid) { |
| 148 | const std::string uid_path = ConvertUidToPath(root_cgroup_path, uid); |
| 149 | return StringPrintf("%s/pid_%d", uid_path.c_str(), pid); |
| 150 | } |
| 151 | |
Suren Baghdasaryan | 3483798 | 2023-07-25 15:45:45 -0700 | [diff] [blame] | 152 | bool ProfileAttribute::GetPathForProcess(uid_t uid, pid_t pid, std::string* path) const { |
| 153 | if (controller()->version() == 2) { |
T.J. Mercier | d1e048f | 2024-03-28 00:33:44 +0000 | [diff] [blame] | 154 | const std::string cgroup_path = ConvertUidPidToPath(controller()->path(), uid, pid); |
| 155 | *path = cgroup_path + "/" + file_name(); |
Suren Baghdasaryan | 3483798 | 2023-07-25 15:45:45 -0700 | [diff] [blame] | 156 | return true; |
| 157 | } |
| 158 | return GetPathForTask(pid, path); |
| 159 | } |
| 160 | |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 161 | bool ProfileAttribute::GetPathForTask(pid_t tid, std::string* path) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 162 | std::string subgroup; |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 163 | if (!controller()->GetTaskGroup(tid, &subgroup)) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | |
| 167 | if (path == nullptr) { |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | if (subgroup.empty()) { |
Suren Baghdasaryan | 3507846 | 2023-07-25 14:50:18 -0700 | [diff] [blame] | 172 | *path = StringPrintf("%s/%s", controller()->path(), file_name().c_str()); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 173 | } else { |
Suren Baghdasaryan | 3507846 | 2023-07-25 14:50:18 -0700 | [diff] [blame] | 174 | *path = StringPrintf("%s/%s/%s", controller()->path(), subgroup.c_str(), |
| 175 | file_name().c_str()); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 176 | } |
| 177 | return true; |
| 178 | } |
| 179 | |
T.J. Mercier | d1e048f | 2024-03-28 00:33:44 +0000 | [diff] [blame] | 180 | // NOTE: This function is for cgroup v2 only |
T.J. Mercier | 5ed5e1b | 2022-08-22 21:25:09 +0000 | [diff] [blame] | 181 | bool ProfileAttribute::GetPathForUID(uid_t uid, std::string* path) const { |
| 182 | if (path == nullptr) { |
| 183 | return true; |
| 184 | } |
| 185 | |
T.J. Mercier | d1e048f | 2024-03-28 00:33:44 +0000 | [diff] [blame] | 186 | const std::string cgroup_path = ConvertUidToPath(controller()->path(), uid); |
| 187 | *path = cgroup_path + "/" + file_name(); |
T.J. Mercier | 5ed5e1b | 2022-08-22 21:25:09 +0000 | [diff] [blame] | 188 | return true; |
| 189 | } |
| 190 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 191 | bool SetClampsAction::ExecuteForProcess(uid_t, pid_t) const { |
| 192 | // TODO: add support when kernel supports util_clamp |
| 193 | LOG(WARNING) << "SetClampsAction::ExecuteForProcess is not supported"; |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | bool SetClampsAction::ExecuteForTask(int) const { |
| 198 | // TODO: add support when kernel supports util_clamp |
| 199 | LOG(WARNING) << "SetClampsAction::ExecuteForTask is not supported"; |
| 200 | return false; |
| 201 | } |
| 202 | |
Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 203 | // To avoid issues in sdk_mac build |
| 204 | #if defined(__ANDROID__) |
| 205 | |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 206 | bool SetTimerSlackAction::ExecuteForTask(pid_t tid) const { |
T.J. Mercier | 0750081 | 2024-10-09 17:41:32 +0000 | [diff] [blame] | 207 | const auto file = StringPrintf("/proc/%d/timerslack_ns", tid); |
| 208 | if (!WriteStringToFile(std::to_string(slack_), file)) { |
| 209 | if (errno == ENOENT) { |
| 210 | // This happens when process is already dead |
| 211 | return true; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 212 | } |
T.J. Mercier | 0750081 | 2024-10-09 17:41:32 +0000 | [diff] [blame] | 213 | PLOG(ERROR) << "set_timerslack_ns write failed"; |
| 214 | return false; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | return true; |
| 218 | } |
| 219 | |
Bart Van Assche | 20d59bd | 2022-01-24 19:45:59 +0000 | [diff] [blame] | 220 | #else |
| 221 | |
| 222 | bool SetTimerSlackAction::ExecuteForTask(int) const { |
| 223 | return true; |
| 224 | }; |
| 225 | |
Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 226 | #endif |
| 227 | |
Suren Baghdasaryan | 3483798 | 2023-07-25 15:45:45 -0700 | [diff] [blame] | 228 | bool SetAttributeAction::WriteValueToFile(const std::string& path) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 229 | if (!WriteStringToFile(value_, path)) { |
Bart Van Assche | 9b5a232 | 2022-03-22 16:15:00 -0700 | [diff] [blame] | 230 | if (access(path.c_str(), F_OK) < 0) { |
Bart Van Assche | 59af680 | 2022-01-24 21:08:57 +0000 | [diff] [blame] | 231 | if (optional_) { |
| 232 | return true; |
| 233 | } else { |
| 234 | LOG(ERROR) << "No such cgroup attribute: " << path; |
| 235 | return false; |
| 236 | } |
| 237 | } |
Bart Van Assche | 54136f8 | 2022-03-31 11:26:42 -0700 | [diff] [blame] | 238 | // The PLOG() statement below uses the error code stored in `errno` by |
| 239 | // WriteStringToFile() because access() only overwrites `errno` if it fails |
| 240 | // and because this code is only reached if the access() function returns 0. |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 241 | PLOG(ERROR) << "Failed to write '" << value_ << "' to " << path; |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | return true; |
| 246 | } |
| 247 | |
Suren Baghdasaryan | 3483798 | 2023-07-25 15:45:45 -0700 | [diff] [blame] | 248 | bool SetAttributeAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
| 249 | std::string path; |
| 250 | |
| 251 | if (!attribute_->GetPathForProcess(uid, pid, &path)) { |
| 252 | LOG(ERROR) << "Failed to find cgroup for uid " << uid << " pid " << pid; |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | return WriteValueToFile(path); |
| 257 | } |
| 258 | |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 259 | bool SetAttributeAction::ExecuteForTask(pid_t tid) const { |
Suren Baghdasaryan | 3483798 | 2023-07-25 15:45:45 -0700 | [diff] [blame] | 260 | std::string path; |
| 261 | |
| 262 | if (!attribute_->GetPathForTask(tid, &path)) { |
| 263 | LOG(ERROR) << "Failed to find cgroup for tid " << tid; |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | return WriteValueToFile(path); |
| 268 | } |
| 269 | |
T.J. Mercier | 5ed5e1b | 2022-08-22 21:25:09 +0000 | [diff] [blame] | 270 | bool SetAttributeAction::ExecuteForUID(uid_t uid) const { |
| 271 | std::string path; |
| 272 | |
| 273 | if (!attribute_->GetPathForUID(uid, &path)) { |
| 274 | LOG(ERROR) << "Failed to find cgroup for uid " << uid; |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | if (!WriteStringToFile(value_, path)) { |
| 279 | if (access(path.c_str(), F_OK) < 0) { |
| 280 | if (optional_) { |
| 281 | return true; |
| 282 | } else { |
| 283 | LOG(ERROR) << "No such cgroup attribute: " << path; |
| 284 | return false; |
| 285 | } |
| 286 | } |
| 287 | PLOG(ERROR) << "Failed to write '" << value_ << "' to " << path; |
| 288 | return false; |
| 289 | } |
| 290 | return true; |
| 291 | } |
| 292 | |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 293 | bool SetAttributeAction::IsValidForProcess(uid_t, pid_t pid) const { |
| 294 | return IsValidForTask(pid); |
| 295 | } |
| 296 | |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 297 | bool SetAttributeAction::IsValidForTask(pid_t tid) const { |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 298 | std::string path; |
| 299 | |
| 300 | if (!attribute_->GetPathForTask(tid, &path)) { |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | if (!access(path.c_str(), W_OK)) { |
| 305 | // operation will succeed |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | if (!access(path.c_str(), F_OK)) { |
| 310 | // file exists but not writable |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | // file does not exist, ignore if optional |
| 315 | return optional_; |
| 316 | } |
| 317 | |
T.J. Mercier | fcb8666 | 2024-08-01 20:52:30 +0000 | [diff] [blame] | 318 | SetCgroupAction::SetCgroupAction(const CgroupControllerWrapper& c, const std::string& p) |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 319 | : controller_(c), path_(p) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 320 | FdCacheHelper::Init(controller_.GetTasksFilePath(path_), fd_[ProfileAction::RCT_TASK]); |
| 321 | // uid and pid don't matter because IsAppDependentPath ensures the path doesn't use them |
| 322 | FdCacheHelper::Init(controller_.GetProcsFilePath(path_, 0, 0), fd_[ProfileAction::RCT_PROCESS]); |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 323 | } |
| 324 | |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 325 | bool SetCgroupAction::AddTidToCgroup(pid_t tid, int fd, ResourceCacheType cache_type) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 326 | if (tid <= 0) { |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | std::string value = std::to_string(tid); |
| 331 | |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 332 | if (TEMP_FAILURE_RETRY(write(fd, value.c_str(), value.length())) == value.length()) { |
| 333 | return true; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 336 | // If the thread is in the process of exiting, don't flag an error |
| 337 | if (errno == ESRCH) { |
| 338 | return true; |
| 339 | } |
| 340 | |
Bart Van Assche | df98534 | 2023-11-13 15:19:43 -0800 | [diff] [blame] | 341 | const char* controller_name = controller()->name(); |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 342 | // ENOSPC is returned when cpuset cgroup that we are joining has no online cpus |
| 343 | if (errno == ENOSPC && !strcmp(controller_name, "cpuset")) { |
| 344 | // This is an abnormal case happening only in testing, so report it only once |
| 345 | static bool empty_cpuset_reported = false; |
| 346 | |
| 347 | if (empty_cpuset_reported) { |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | LOG(ERROR) << "Failed to add task '" << value |
| 352 | << "' into cpuset because all cpus in that cpuset are offline"; |
| 353 | empty_cpuset_reported = true; |
| 354 | } else { |
Bart Van Assche | df98534 | 2023-11-13 15:19:43 -0800 | [diff] [blame] | 355 | PLOG(ERROR) << "AddTidToCgroup failed to write '" << value << "'; path=" << path_ << "; " |
| 356 | << (cache_type == RCT_TASK ? "task" : "process"); |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | return false; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 360 | } |
| 361 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 362 | ProfileAction::CacheUseResult SetCgroupAction::UseCachedFd(ResourceCacheType cache_type, |
| 363 | int id) const { |
| 364 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 365 | if (FdCacheHelper::IsCached(fd_[cache_type])) { |
| 366 | // fd is cached, reuse it |
Bart Van Assche | df98534 | 2023-11-13 15:19:43 -0800 | [diff] [blame] | 367 | if (!AddTidToCgroup(id, fd_[cache_type], cache_type)) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 368 | LOG(ERROR) << "Failed to add task into cgroup"; |
| 369 | return ProfileAction::FAIL; |
| 370 | } |
| 371 | return ProfileAction::SUCCESS; |
| 372 | } |
| 373 | |
| 374 | if (fd_[cache_type] == FdCacheHelper::FDS_INACCESSIBLE) { |
| 375 | // no permissions to access the file, ignore |
| 376 | return ProfileAction::SUCCESS; |
| 377 | } |
| 378 | |
| 379 | if (cache_type == ResourceCacheType::RCT_TASK && |
| 380 | fd_[cache_type] == FdCacheHelper::FDS_APP_DEPENDENT) { |
| 381 | // application-dependent path can't be used with tid |
Bart Van Assche | 7a95261 | 2022-10-12 13:27:28 -0700 | [diff] [blame] | 382 | LOG(ERROR) << Name() << ": application profile can't be applied to a thread"; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 383 | return ProfileAction::FAIL; |
| 384 | } |
| 385 | |
| 386 | return ProfileAction::UNUSED; |
| 387 | } |
| 388 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 389 | bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 390 | CacheUseResult result = UseCachedFd(ProfileAction::RCT_PROCESS, pid); |
| 391 | if (result != ProfileAction::UNUSED) { |
| 392 | return result == ProfileAction::SUCCESS; |
| 393 | } |
| 394 | |
| 395 | // fd was not cached or cached fd can't be used |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 396 | std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 397 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC))); |
| 398 | if (tmp_fd < 0) { |
Bart Van Assche | 7a95261 | 2022-10-12 13:27:28 -0700 | [diff] [blame] | 399 | PLOG(WARNING) << Name() << "::" << __func__ << ": failed to open " << procs_path; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 400 | return false; |
| 401 | } |
Bart Van Assche | df98534 | 2023-11-13 15:19:43 -0800 | [diff] [blame] | 402 | if (!AddTidToCgroup(pid, tmp_fd, RCT_PROCESS)) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 403 | LOG(ERROR) << "Failed to add task into cgroup"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 404 | return false; |
| 405 | } |
| 406 | |
| 407 | return true; |
| 408 | } |
| 409 | |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 410 | bool SetCgroupAction::ExecuteForTask(pid_t tid) const { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 411 | CacheUseResult result = UseCachedFd(ProfileAction::RCT_TASK, tid); |
| 412 | if (result != ProfileAction::UNUSED) { |
| 413 | return result == ProfileAction::SUCCESS; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 416 | // fd was not cached or cached fd can't be used |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 417 | std::string tasks_path = controller()->GetTasksFilePath(path_); |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 418 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC))); |
| 419 | if (tmp_fd < 0) { |
Bart Van Assche | 7a95261 | 2022-10-12 13:27:28 -0700 | [diff] [blame] | 420 | PLOG(WARNING) << Name() << "::" << __func__ << ": failed to open " << tasks_path; |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 421 | return false; |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 422 | } |
Bart Van Assche | df98534 | 2023-11-13 15:19:43 -0800 | [diff] [blame] | 423 | if (!AddTidToCgroup(tid, tmp_fd, RCT_TASK)) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 424 | LOG(ERROR) << "Failed to add task into cgroup"; |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 425 | return false; |
| 426 | } |
| 427 | |
| 428 | return true; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 429 | } |
| 430 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 431 | void SetCgroupAction::EnableResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 432 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 433 | // Return early to prevent unnecessary calls to controller_.Get{Tasks|Procs}FilePath() which |
| 434 | // include regex evaluations |
| 435 | if (fd_[cache_type] != FdCacheHelper::FDS_NOT_CACHED) { |
| 436 | return; |
| 437 | } |
| 438 | switch (cache_type) { |
| 439 | case (ProfileAction::RCT_TASK): |
| 440 | FdCacheHelper::Cache(controller_.GetTasksFilePath(path_), fd_[cache_type]); |
| 441 | break; |
| 442 | case (ProfileAction::RCT_PROCESS): |
| 443 | // uid and pid don't matter because IsAppDependentPath ensures the path doesn't use them |
| 444 | FdCacheHelper::Cache(controller_.GetProcsFilePath(path_, 0, 0), fd_[cache_type]); |
| 445 | break; |
| 446 | default: |
| 447 | LOG(ERROR) << "Invalid cache type is specified!"; |
| 448 | break; |
| 449 | } |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 450 | } |
| 451 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 452 | void SetCgroupAction::DropResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 453 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 454 | FdCacheHelper::Drop(fd_[cache_type]); |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 457 | bool SetCgroupAction::IsValidForProcess(uid_t uid, pid_t pid) const { |
| 458 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 459 | if (FdCacheHelper::IsCached(fd_[ProfileAction::RCT_PROCESS])) { |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | if (fd_[ProfileAction::RCT_PROCESS] == FdCacheHelper::FDS_INACCESSIBLE) { |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid); |
| 468 | return access(procs_path.c_str(), W_OK) == 0; |
| 469 | } |
| 470 | |
| 471 | bool SetCgroupAction::IsValidForTask(int) const { |
| 472 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 473 | if (FdCacheHelper::IsCached(fd_[ProfileAction::RCT_TASK])) { |
| 474 | return true; |
| 475 | } |
| 476 | |
| 477 | if (fd_[ProfileAction::RCT_TASK] == FdCacheHelper::FDS_INACCESSIBLE) { |
| 478 | return false; |
| 479 | } |
| 480 | |
| 481 | if (fd_[ProfileAction::RCT_TASK] == FdCacheHelper::FDS_APP_DEPENDENT) { |
| 482 | // application-dependent path can't be used with tid |
| 483 | return false; |
| 484 | } |
| 485 | |
| 486 | std::string tasks_path = controller()->GetTasksFilePath(path_); |
| 487 | return access(tasks_path.c_str(), W_OK) == 0; |
| 488 | } |
| 489 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 490 | WriteFileAction::WriteFileAction(const std::string& task_path, const std::string& proc_path, |
| 491 | const std::string& value, bool logfailures) |
| 492 | : task_path_(task_path), proc_path_(proc_path), value_(value), logfailures_(logfailures) { |
| 493 | FdCacheHelper::Init(task_path_, fd_[ProfileAction::RCT_TASK]); |
| 494 | if (!proc_path_.empty()) FdCacheHelper::Init(proc_path_, fd_[ProfileAction::RCT_PROCESS]); |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 495 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 496 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 497 | bool WriteFileAction::WriteValueToFile(const std::string& value_, ResourceCacheType cache_type, |
T.J. Mercier | d6fb225 | 2024-01-24 23:42:39 +0000 | [diff] [blame] | 498 | uid_t uid, pid_t pid, bool logfailures) const { |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 499 | std::string value(value_); |
| 500 | |
| 501 | value = StringReplace(value, "<uid>", std::to_string(uid), true); |
| 502 | value = StringReplace(value, "<pid>", std::to_string(pid), true); |
| 503 | |
| 504 | CacheUseResult result = UseCachedFd(cache_type, value); |
| 505 | |
| 506 | if (result != ProfileAction::UNUSED) { |
| 507 | return result == ProfileAction::SUCCESS; |
| 508 | } |
| 509 | |
| 510 | std::string path; |
| 511 | if (cache_type == ProfileAction::RCT_TASK || proc_path_.empty()) { |
| 512 | path = task_path_; |
| 513 | } else { |
| 514 | path = proc_path_; |
| 515 | } |
| 516 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 517 | // Use WriteStringToFd instead of WriteStringToFile because the latter will open file with |
| 518 | // O_TRUNC which causes kernfs_mutex contention |
| 519 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_WRONLY | O_CLOEXEC))); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 520 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 521 | if (tmp_fd < 0) { |
Bart Van Assche | 7a95261 | 2022-10-12 13:27:28 -0700 | [diff] [blame] | 522 | if (logfailures) PLOG(WARNING) << Name() << "::" << __func__ << ": failed to open " << path; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 523 | return false; |
| 524 | } |
| 525 | |
| 526 | if (!WriteStringToFd(value, tmp_fd)) { |
| 527 | if (logfailures) PLOG(ERROR) << "Failed to write '" << value << "' to " << path; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 528 | return false; |
| 529 | } |
| 530 | |
| 531 | return true; |
| 532 | } |
| 533 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 534 | ProfileAction::CacheUseResult WriteFileAction::UseCachedFd(ResourceCacheType cache_type, |
| 535 | const std::string& value) const { |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 536 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 537 | if (FdCacheHelper::IsCached(fd_[cache_type])) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 538 | // fd is cached, reuse it |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 539 | bool ret = WriteStringToFd(value, fd_[cache_type]); |
| 540 | |
| 541 | if (!ret && logfailures_) { |
| 542 | if (cache_type == ProfileAction::RCT_TASK || proc_path_.empty()) { |
| 543 | PLOG(ERROR) << "Failed to write '" << value << "' to " << task_path_; |
| 544 | } else { |
| 545 | PLOG(ERROR) << "Failed to write '" << value << "' to " << proc_path_; |
| 546 | } |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 547 | } |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 548 | return ret ? ProfileAction::SUCCESS : ProfileAction::FAIL; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 549 | } |
| 550 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 551 | if (fd_[cache_type] == FdCacheHelper::FDS_INACCESSIBLE) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 552 | // no permissions to access the file, ignore |
| 553 | return ProfileAction::SUCCESS; |
| 554 | } |
| 555 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 556 | if (cache_type == ResourceCacheType::RCT_TASK && |
| 557 | fd_[cache_type] == FdCacheHelper::FDS_APP_DEPENDENT) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 558 | // application-dependent path can't be used with tid |
Bart Van Assche | 7a95261 | 2022-10-12 13:27:28 -0700 | [diff] [blame] | 559 | LOG(ERROR) << Name() << ": application profile can't be applied to a thread"; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 560 | return ProfileAction::FAIL; |
| 561 | } |
| 562 | return ProfileAction::UNUSED; |
| 563 | } |
| 564 | |
| 565 | bool WriteFileAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 566 | if (!proc_path_.empty()) { |
| 567 | return WriteValueToFile(value_, ProfileAction::RCT_PROCESS, uid, pid, logfailures_); |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 570 | DIR* d; |
| 571 | struct dirent* de; |
| 572 | char proc_path[255]; |
T.J. Mercier | d6fb225 | 2024-01-24 23:42:39 +0000 | [diff] [blame] | 573 | pid_t t_pid; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 574 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 575 | sprintf(proc_path, "/proc/%d/task", pid); |
| 576 | if (!(d = opendir(proc_path))) { |
| 577 | return false; |
| 578 | } |
| 579 | |
| 580 | while ((de = readdir(d))) { |
| 581 | if (de->d_name[0] == '.') { |
| 582 | continue; |
| 583 | } |
| 584 | |
| 585 | t_pid = atoi(de->d_name); |
| 586 | |
| 587 | if (!t_pid) { |
| 588 | continue; |
| 589 | } |
| 590 | |
| 591 | WriteValueToFile(value_, ProfileAction::RCT_TASK, uid, t_pid, logfailures_); |
| 592 | } |
| 593 | |
| 594 | closedir(d); |
| 595 | |
| 596 | return true; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 597 | } |
| 598 | |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 599 | bool WriteFileAction::ExecuteForTask(pid_t tid) const { |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 600 | return WriteValueToFile(value_, ProfileAction::RCT_TASK, getuid(), tid, logfailures_); |
| 601 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 602 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 603 | void WriteFileAction::EnableResourceCaching(ResourceCacheType cache_type) { |
| 604 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 605 | if (fd_[cache_type] != FdCacheHelper::FDS_NOT_CACHED) { |
| 606 | return; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 607 | } |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 608 | switch (cache_type) { |
| 609 | case (ProfileAction::RCT_TASK): |
| 610 | FdCacheHelper::Cache(task_path_, fd_[cache_type]); |
| 611 | break; |
| 612 | case (ProfileAction::RCT_PROCESS): |
| 613 | if (!proc_path_.empty()) FdCacheHelper::Cache(proc_path_, fd_[cache_type]); |
| 614 | break; |
| 615 | default: |
| 616 | LOG(ERROR) << "Invalid cache type is specified!"; |
| 617 | break; |
| 618 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 619 | } |
| 620 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 621 | void WriteFileAction::DropResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 622 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 623 | FdCacheHelper::Drop(fd_[cache_type]); |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 626 | bool WriteFileAction::IsValidForProcess(uid_t, pid_t) const { |
| 627 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 628 | if (FdCacheHelper::IsCached(fd_[ProfileAction::RCT_PROCESS])) { |
| 629 | return true; |
| 630 | } |
| 631 | |
| 632 | if (fd_[ProfileAction::RCT_PROCESS] == FdCacheHelper::FDS_INACCESSIBLE) { |
| 633 | return false; |
| 634 | } |
| 635 | |
| 636 | return access(proc_path_.empty() ? task_path_.c_str() : proc_path_.c_str(), W_OK) == 0; |
| 637 | } |
| 638 | |
| 639 | bool WriteFileAction::IsValidForTask(int) const { |
| 640 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 641 | if (FdCacheHelper::IsCached(fd_[ProfileAction::RCT_TASK])) { |
| 642 | return true; |
| 643 | } |
| 644 | |
| 645 | if (fd_[ProfileAction::RCT_TASK] == FdCacheHelper::FDS_INACCESSIBLE) { |
| 646 | return false; |
| 647 | } |
| 648 | |
| 649 | if (fd_[ProfileAction::RCT_TASK] == FdCacheHelper::FDS_APP_DEPENDENT) { |
| 650 | // application-dependent path can't be used with tid |
| 651 | return false; |
| 652 | } |
| 653 | |
| 654 | return access(task_path_.c_str(), W_OK) == 0; |
| 655 | } |
| 656 | |
T.J. Mercier | 3984611 | 2024-10-09 22:40:26 +0000 | [diff] [blame^] | 657 | bool SetSchedulerPolicyAction::isNormalPolicy(int policy) { |
| 658 | return policy == SCHED_OTHER || policy == SCHED_BATCH || policy == SCHED_IDLE; |
| 659 | } |
| 660 | |
| 661 | bool SetSchedulerPolicyAction::toPriority(int policy, int virtual_priority, int& priority_out) { |
| 662 | constexpr int VIRTUAL_PRIORITY_MIN = 1; |
| 663 | constexpr int VIRTUAL_PRIORITY_MAX = 99; |
| 664 | |
| 665 | if (virtual_priority < VIRTUAL_PRIORITY_MIN || virtual_priority > VIRTUAL_PRIORITY_MAX) { |
| 666 | LOG(WARNING) << "SetSchedulerPolicy: invalid priority (" << virtual_priority |
| 667 | << ") for policy (" << policy << ")"; |
| 668 | return false; |
| 669 | } |
| 670 | |
| 671 | const int min = sched_get_priority_min(policy); |
| 672 | if (min == -1) { |
| 673 | PLOG(ERROR) << "SetSchedulerPolicy: Cannot get min sched priority for policy " << policy; |
| 674 | return false; |
| 675 | } |
| 676 | |
| 677 | const int max = sched_get_priority_max(policy); |
| 678 | if (max == -1) { |
| 679 | PLOG(ERROR) << "SetSchedulerPolicy: Cannot get max sched priority for policy " << policy; |
| 680 | return false; |
| 681 | } |
| 682 | |
| 683 | priority_out = min + (virtual_priority - VIRTUAL_PRIORITY_MIN) * (max - min) / |
| 684 | (VIRTUAL_PRIORITY_MAX - VIRTUAL_PRIORITY_MIN); |
| 685 | |
| 686 | return true; |
| 687 | } |
| 688 | |
| 689 | bool SetSchedulerPolicyAction::ExecuteForTask(pid_t tid) const { |
| 690 | struct sched_param param = {}; |
| 691 | param.sched_priority = isNormalPolicy(policy_) ? 0 : *priority_or_nice_; |
| 692 | if (sched_setscheduler(tid, policy_, ¶m) == -1) { |
| 693 | PLOG(WARNING) << "SetSchedulerPolicy: Failed to apply scheduler policy (" << policy_ |
| 694 | << ") with priority (" << *priority_or_nice_ << ") to tid " << tid; |
| 695 | return false; |
| 696 | } |
| 697 | |
| 698 | if (isNormalPolicy(policy_) && priority_or_nice_ && |
| 699 | setpriority(PRIO_PROCESS, tid, *priority_or_nice_) == -1) { |
| 700 | PLOG(WARNING) << "SetSchedulerPolicy: Failed to apply nice (" << *priority_or_nice_ |
| 701 | << ") to tid " << tid; |
| 702 | return false; |
| 703 | } |
| 704 | |
| 705 | return true; |
| 706 | } |
| 707 | |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 708 | bool ApplyProfileAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
| 709 | for (const auto& profile : profiles_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 710 | profile->ExecuteForProcess(uid, pid); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 711 | } |
| 712 | return true; |
| 713 | } |
| 714 | |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 715 | bool ApplyProfileAction::ExecuteForTask(pid_t tid) const { |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 716 | for (const auto& profile : profiles_) { |
Wei Wang | 8722e4d | 2021-05-14 12:34:54 -0700 | [diff] [blame] | 717 | profile->ExecuteForTask(tid); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 718 | } |
| 719 | return true; |
| 720 | } |
| 721 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 722 | void ApplyProfileAction::EnableResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 723 | for (const auto& profile : profiles_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 724 | profile->EnableResourceCaching(cache_type); |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 728 | void ApplyProfileAction::DropResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 729 | for (const auto& profile : profiles_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 730 | profile->DropResourceCaching(cache_type); |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 734 | bool ApplyProfileAction::IsValidForProcess(uid_t uid, pid_t pid) const { |
| 735 | for (const auto& profile : profiles_) { |
| 736 | if (!profile->IsValidForProcess(uid, pid)) { |
| 737 | return false; |
| 738 | } |
| 739 | } |
| 740 | return true; |
| 741 | } |
| 742 | |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 743 | bool ApplyProfileAction::IsValidForTask(pid_t tid) const { |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 744 | for (const auto& profile : profiles_) { |
| 745 | if (!profile->IsValidForTask(tid)) { |
| 746 | return false; |
| 747 | } |
| 748 | } |
| 749 | return true; |
| 750 | } |
| 751 | |
Suren Baghdasaryan | 8438595 | 2020-01-24 16:36:10 -0800 | [diff] [blame] | 752 | void TaskProfile::MoveTo(TaskProfile* profile) { |
| 753 | profile->elements_ = std::move(elements_); |
| 754 | profile->res_cached_ = res_cached_; |
| 755 | } |
| 756 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 757 | bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const { |
| 758 | for (const auto& element : elements_) { |
| 759 | if (!element->ExecuteForProcess(uid, pid)) { |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 760 | LOG(VERBOSE) << "Applying profile action " << element->Name() << " failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 761 | return false; |
| 762 | } |
| 763 | } |
| 764 | return true; |
| 765 | } |
| 766 | |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 767 | bool TaskProfile::ExecuteForTask(pid_t tid) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 768 | if (tid == 0) { |
| 769 | tid = GetThreadId(); |
| 770 | } |
| 771 | for (const auto& element : elements_) { |
| 772 | if (!element->ExecuteForTask(tid)) { |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 773 | LOG(VERBOSE) << "Applying profile action " << element->Name() << " failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 774 | return false; |
| 775 | } |
| 776 | } |
| 777 | return true; |
| 778 | } |
| 779 | |
T.J. Mercier | 5ed5e1b | 2022-08-22 21:25:09 +0000 | [diff] [blame] | 780 | bool TaskProfile::ExecuteForUID(uid_t uid) const { |
| 781 | for (const auto& element : elements_) { |
| 782 | if (!element->ExecuteForUID(uid)) { |
| 783 | LOG(VERBOSE) << "Applying profile action " << element->Name() << " failed"; |
| 784 | return false; |
| 785 | } |
| 786 | } |
| 787 | return true; |
| 788 | } |
| 789 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 790 | void TaskProfile::EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) { |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 791 | if (res_cached_) { |
| 792 | return; |
| 793 | } |
| 794 | |
| 795 | for (auto& element : elements_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 796 | element->EnableResourceCaching(cache_type); |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | res_cached_ = true; |
| 800 | } |
| 801 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 802 | void TaskProfile::DropResourceCaching(ProfileAction::ResourceCacheType cache_type) { |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 803 | if (!res_cached_) { |
| 804 | return; |
| 805 | } |
| 806 | |
| 807 | for (auto& element : elements_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 808 | element->DropResourceCaching(cache_type); |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | res_cached_ = false; |
| 812 | } |
| 813 | |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 814 | bool TaskProfile::IsValidForProcess(uid_t uid, pid_t pid) const { |
| 815 | for (const auto& element : elements_) { |
| 816 | if (!element->IsValidForProcess(uid, pid)) return false; |
| 817 | } |
| 818 | return true; |
| 819 | } |
| 820 | |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 821 | bool TaskProfile::IsValidForTask(pid_t tid) const { |
Suren Baghdasaryan | 8cacb61 | 2023-04-12 01:24:23 +0000 | [diff] [blame] | 822 | for (const auto& element : elements_) { |
| 823 | if (!element->IsValidForTask(tid)) return false; |
| 824 | } |
| 825 | return true; |
| 826 | } |
| 827 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 828 | void TaskProfiles::DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const { |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 829 | for (auto& iter : profiles_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 830 | iter.second->DropResourceCaching(cache_type); |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 834 | TaskProfiles& TaskProfiles::GetInstance() { |
Peter Collingbourne | dba6d44 | 2019-03-20 21:09:46 -0700 | [diff] [blame] | 835 | // Deliberately leak this object to avoid a race between destruction on |
| 836 | // process exit and concurrent access from another thread. |
| 837 | static auto* instance = new TaskProfiles; |
| 838 | return *instance; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | TaskProfiles::TaskProfiles() { |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 842 | // load system task profiles |
| 843 | if (!Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_FILE)) { |
| 844 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_FILE << " for [" << getpid() << "] failed"; |
| 845 | } |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 846 | |
| 847 | // load API-level specific system task profiles if available |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 848 | unsigned int api_level = GetUintProperty<unsigned int>("ro.product.first_api_level", 0); |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 849 | if (api_level > 0) { |
| 850 | std::string api_profiles_path = |
| 851 | android::base::StringPrintf(TEMPLATE_TASK_PROFILE_API_FILE, api_level); |
| 852 | if (!access(api_profiles_path.c_str(), F_OK) || errno != ENOENT) { |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 853 | if (!Load(CgroupMap::GetInstance(), api_profiles_path)) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 854 | LOG(ERROR) << "Loading " << api_profiles_path << " for [" << getpid() << "] failed"; |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 855 | } |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 859 | // load vendor task profiles if the file exists |
| 860 | if (!access(TASK_PROFILE_DB_VENDOR_FILE, F_OK) && |
| 861 | !Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_VENDOR_FILE)) { |
| 862 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_VENDOR_FILE << " for [" << getpid() |
| 863 | << "] failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 867 | bool TaskProfiles::Load(const CgroupMap& cg_map, const std::string& file_name) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 868 | std::string json_doc; |
| 869 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 870 | if (!android::base::ReadFileToString(file_name, &json_doc)) { |
| 871 | LOG(ERROR) << "Failed to read task profiles from " << file_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 872 | return false; |
| 873 | } |
| 874 | |
Haibo Huang | d9ac92a | 2021-02-24 17:34:50 -0800 | [diff] [blame] | 875 | Json::CharReaderBuilder builder; |
| 876 | std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 877 | Json::Value root; |
Haibo Huang | d9ac92a | 2021-02-24 17:34:50 -0800 | [diff] [blame] | 878 | std::string errorMessage; |
| 879 | if (!reader->parse(&*json_doc.begin(), &*json_doc.end(), &root, &errorMessage)) { |
| 880 | LOG(ERROR) << "Failed to parse task profiles: " << errorMessage; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 881 | return false; |
| 882 | } |
| 883 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 884 | const Json::Value& attr = root["Attributes"]; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 885 | for (Json::Value::ArrayIndex i = 0; i < attr.size(); ++i) { |
| 886 | std::string name = attr[i]["Name"].asString(); |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 887 | std::string controller_name = attr[i]["Controller"].asString(); |
| 888 | std::string file_attr = attr[i]["File"].asString(); |
Bart Van Assche | bc077ff | 2022-02-17 01:26:44 +0000 | [diff] [blame] | 889 | std::string file_v2_attr = attr[i]["FileV2"].asString(); |
| 890 | |
| 891 | if (!file_v2_attr.empty() && file_attr.empty()) { |
| 892 | LOG(ERROR) << "Attribute " << name << " has FileV2 but no File property"; |
| 893 | return false; |
| 894 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 895 | |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 896 | auto controller = cg_map.FindController(controller_name); |
| 897 | if (controller.HasValue()) { |
| 898 | auto iter = attributes_.find(name); |
| 899 | if (iter == attributes_.end()) { |
Bart Van Assche | bc077ff | 2022-02-17 01:26:44 +0000 | [diff] [blame] | 900 | attributes_[name] = |
| 901 | std::make_unique<ProfileAttribute>(controller, file_attr, file_v2_attr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 902 | } else { |
Suren Baghdasaryan | 3507846 | 2023-07-25 14:50:18 -0700 | [diff] [blame] | 903 | iter->second->Reset(controller, file_attr, file_v2_attr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 904 | } |
| 905 | } else { |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 906 | LOG(WARNING) << "Controller " << controller_name << " is not found"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 910 | const Json::Value& profiles_val = root["Profiles"]; |
| 911 | for (Json::Value::ArrayIndex i = 0; i < profiles_val.size(); ++i) { |
| 912 | const Json::Value& profile_val = profiles_val[i]; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 913 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 914 | std::string profile_name = profile_val["Name"].asString(); |
| 915 | const Json::Value& actions = profile_val["Actions"]; |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 916 | auto profile = std::make_shared<TaskProfile>(profile_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 917 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 918 | for (Json::Value::ArrayIndex act_idx = 0; act_idx < actions.size(); ++act_idx) { |
| 919 | const Json::Value& action_val = actions[act_idx]; |
| 920 | std::string action_name = action_val["Name"].asString(); |
| 921 | const Json::Value& params_val = action_val["Params"]; |
| 922 | if (action_name == "JoinCgroup") { |
| 923 | std::string controller_name = params_val["Controller"].asString(); |
| 924 | std::string path = params_val["Path"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 925 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 926 | auto controller = cg_map.FindController(controller_name); |
| 927 | if (controller.HasValue()) { |
Bart Van Assche | 2953a92 | 2023-11-14 07:33:00 -0800 | [diff] [blame] | 928 | if (controller.version() == 1) { |
| 929 | profile->Add(std::make_unique<SetCgroupAction>(controller, path)); |
| 930 | } else { |
| 931 | LOG(WARNING) << "A JoinCgroup action in the " << profile_name |
| 932 | << " profile is used for controller " << controller_name |
| 933 | << " in the cgroup v2 hierarchy and will be ignored"; |
| 934 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 935 | } else { |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 936 | LOG(WARNING) << "JoinCgroup: controller " << controller_name << " is not found"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 937 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 938 | } else if (action_name == "SetTimerSlack") { |
| 939 | std::string slack_value = params_val["Slack"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 940 | char* end; |
| 941 | unsigned long slack; |
| 942 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 943 | slack = strtoul(slack_value.c_str(), &end, 10); |
| 944 | if (end > slack_value.c_str()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 945 | profile->Add(std::make_unique<SetTimerSlackAction>(slack)); |
| 946 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 947 | LOG(WARNING) << "SetTimerSlack: invalid parameter: " << slack_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 948 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 949 | } else if (action_name == "SetAttribute") { |
| 950 | std::string attr_name = params_val["Name"].asString(); |
| 951 | std::string attr_value = params_val["Value"].asString(); |
Bart Van Assche | 59af680 | 2022-01-24 21:08:57 +0000 | [diff] [blame] | 952 | bool optional = strcmp(params_val["Optional"].asString().c_str(), "true") == 0; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 953 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 954 | auto iter = attributes_.find(attr_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 955 | if (iter != attributes_.end()) { |
Bart Van Assche | 59af680 | 2022-01-24 21:08:57 +0000 | [diff] [blame] | 956 | profile->Add(std::make_unique<SetAttributeAction>(iter->second.get(), |
| 957 | attr_value, optional)); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 958 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 959 | LOG(WARNING) << "SetAttribute: unknown attribute: " << attr_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 960 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 961 | } else if (action_name == "SetClamps") { |
| 962 | std::string boost_value = params_val["Boost"].asString(); |
| 963 | std::string clamp_value = params_val["Clamp"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 964 | char* end; |
| 965 | unsigned long boost; |
| 966 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 967 | boost = strtoul(boost_value.c_str(), &end, 10); |
| 968 | if (end > boost_value.c_str()) { |
| 969 | unsigned long clamp = strtoul(clamp_value.c_str(), &end, 10); |
| 970 | if (end > clamp_value.c_str()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 971 | profile->Add(std::make_unique<SetClampsAction>(boost, clamp)); |
| 972 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 973 | LOG(WARNING) << "SetClamps: invalid parameter " << clamp_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 974 | } |
| 975 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 976 | LOG(WARNING) << "SetClamps: invalid parameter: " << boost_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 977 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 978 | } else if (action_name == "WriteFile") { |
| 979 | std::string attr_filepath = params_val["FilePath"].asString(); |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 980 | std::string attr_procfilepath = params_val["ProcFilePath"].asString(); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 981 | std::string attr_value = params_val["Value"].asString(); |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 982 | // FilePath and Value are mandatory |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 983 | if (!attr_filepath.empty() && !attr_value.empty()) { |
Rick Yiu | 49fce95 | 2021-04-08 22:10:06 +0800 | [diff] [blame] | 984 | std::string attr_logfailures = params_val["LogFailures"].asString(); |
| 985 | bool logfailures = attr_logfailures.empty() || attr_logfailures == "true"; |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 986 | profile->Add(std::make_unique<WriteFileAction>(attr_filepath, attr_procfilepath, |
| 987 | attr_value, logfailures)); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 988 | } else if (attr_filepath.empty()) { |
| 989 | LOG(WARNING) << "WriteFile: invalid parameter: " |
| 990 | << "empty filepath"; |
| 991 | } else if (attr_value.empty()) { |
| 992 | LOG(WARNING) << "WriteFile: invalid parameter: " |
| 993 | << "empty value"; |
| 994 | } |
T.J. Mercier | 3984611 | 2024-10-09 22:40:26 +0000 | [diff] [blame^] | 995 | } else if (action_name == "SetSchedulerPolicy") { |
| 996 | const std::map<std::string, int> POLICY_MAP = { |
| 997 | {"SCHED_OTHER", SCHED_OTHER}, |
| 998 | {"SCHED_BATCH", SCHED_BATCH}, |
| 999 | {"SCHED_IDLE", SCHED_IDLE}, |
| 1000 | {"SCHED_FIFO", SCHED_FIFO}, |
| 1001 | {"SCHED_RR", SCHED_RR}, |
| 1002 | }; |
| 1003 | const std::string policy_str = params_val["Policy"].asString(); |
| 1004 | |
| 1005 | const auto it = POLICY_MAP.find(policy_str); |
| 1006 | if (it == POLICY_MAP.end()) { |
| 1007 | LOG(WARNING) << "SetSchedulerPolicy: invalid policy " << policy_str; |
| 1008 | continue; |
| 1009 | } |
| 1010 | |
| 1011 | const int policy = it->second; |
| 1012 | |
| 1013 | if (SetSchedulerPolicyAction::isNormalPolicy(policy)) { |
| 1014 | if (params_val.isMember("Priority")) { |
| 1015 | LOG(WARNING) << "SetSchedulerPolicy: Normal policies (" << policy_str |
| 1016 | << ") use Nice values, not Priority values"; |
| 1017 | } |
| 1018 | |
| 1019 | if (params_val.isMember("Nice")) { |
| 1020 | // If present, this optional value will be passed in an additional syscall |
| 1021 | // to setpriority(), since the sched_priority value must be 0 for calls to |
| 1022 | // sched_setscheduler() with "normal" policies. |
| 1023 | const int nice = params_val["Nice"].asInt(); |
| 1024 | |
| 1025 | const int LINUX_MIN_NICE = -20; |
| 1026 | const int LINUX_MAX_NICE = 19; |
| 1027 | if (nice < LINUX_MIN_NICE || nice > LINUX_MAX_NICE) { |
| 1028 | LOG(WARNING) << "SetSchedulerPolicy: Provided nice (" << nice |
| 1029 | << ") appears out of range."; |
| 1030 | } |
| 1031 | profile->Add(std::make_unique<SetSchedulerPolicyAction>(policy, nice)); |
| 1032 | } else { |
| 1033 | profile->Add(std::make_unique<SetSchedulerPolicyAction>(policy)); |
| 1034 | } |
| 1035 | } else { |
| 1036 | if (params_val.isMember("Nice")) { |
| 1037 | LOG(WARNING) << "SetSchedulerPolicy: Real-time policies (" << policy_str |
| 1038 | << ") use Priority values, not Nice values"; |
| 1039 | } |
| 1040 | |
| 1041 | // This is a "virtual priority" as described by `man 2 sched_get_priority_min` |
| 1042 | // that will be mapped onto the following range for the provided policy: |
| 1043 | // [sched_get_priority_min(), sched_get_priority_max()] |
| 1044 | const int virtual_priority = params_val["Priority"].asInt(); |
| 1045 | |
| 1046 | int priority; |
| 1047 | if (SetSchedulerPolicyAction::toPriority(policy, virtual_priority, priority)) { |
| 1048 | profile->Add(std::make_unique<SetSchedulerPolicyAction>(policy, priority)); |
| 1049 | } |
| 1050 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 1051 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 1052 | LOG(WARNING) << "Unknown profile action: " << action_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 1053 | } |
| 1054 | } |
Suren Baghdasaryan | 8438595 | 2020-01-24 16:36:10 -0800 | [diff] [blame] | 1055 | auto iter = profiles_.find(profile_name); |
| 1056 | if (iter == profiles_.end()) { |
| 1057 | profiles_[profile_name] = profile; |
| 1058 | } else { |
| 1059 | // Move the content rather that replace the profile because old profile might be |
| 1060 | // referenced from an aggregate profile if vendor overrides task profiles |
| 1061 | profile->MoveTo(iter->second.get()); |
| 1062 | profile.reset(); |
| 1063 | } |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | const Json::Value& aggregateprofiles_val = root["AggregateProfiles"]; |
| 1067 | for (Json::Value::ArrayIndex i = 0; i < aggregateprofiles_val.size(); ++i) { |
| 1068 | const Json::Value& aggregateprofile_val = aggregateprofiles_val[i]; |
| 1069 | |
| 1070 | std::string aggregateprofile_name = aggregateprofile_val["Name"].asString(); |
| 1071 | const Json::Value& aggregateprofiles = aggregateprofile_val["Profiles"]; |
| 1072 | std::vector<std::shared_ptr<TaskProfile>> profiles; |
| 1073 | bool ret = true; |
| 1074 | |
| 1075 | for (Json::Value::ArrayIndex pf_idx = 0; pf_idx < aggregateprofiles.size(); ++pf_idx) { |
| 1076 | std::string profile_name = aggregateprofiles[pf_idx].asString(); |
| 1077 | |
| 1078 | if (profile_name == aggregateprofile_name) { |
| 1079 | LOG(WARNING) << "AggregateProfiles: recursive profile name: " << profile_name; |
| 1080 | ret = false; |
| 1081 | break; |
| 1082 | } else if (profiles_.find(profile_name) == profiles_.end()) { |
| 1083 | LOG(WARNING) << "AggregateProfiles: undefined profile name: " << profile_name; |
| 1084 | ret = false; |
| 1085 | break; |
| 1086 | } else { |
| 1087 | profiles.push_back(profiles_[profile_name]); |
| 1088 | } |
| 1089 | } |
| 1090 | if (ret) { |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 1091 | auto profile = std::make_shared<TaskProfile>(aggregateprofile_name); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1092 | profile->Add(std::make_unique<ApplyProfileAction>(profiles)); |
| 1093 | profiles_[aggregateprofile_name] = profile; |
| 1094 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | return true; |
| 1098 | } |
| 1099 | |
Bart Van Assche | d0b8ce2 | 2022-08-02 13:06:26 -0700 | [diff] [blame] | 1100 | TaskProfile* TaskProfiles::GetProfile(std::string_view name) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 1101 | auto iter = profiles_.find(name); |
| 1102 | |
| 1103 | if (iter != profiles_.end()) { |
| 1104 | return iter->second.get(); |
| 1105 | } |
| 1106 | return nullptr; |
| 1107 | } |
| 1108 | |
Bart Van Assche | d0b8ce2 | 2022-08-02 13:06:26 -0700 | [diff] [blame] | 1109 | const IProfileAttribute* TaskProfiles::GetAttribute(std::string_view name) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 1110 | auto iter = attributes_.find(name); |
| 1111 | |
| 1112 | if (iter != attributes_.end()) { |
| 1113 | return iter->second.get(); |
| 1114 | } |
| 1115 | return nullptr; |
| 1116 | } |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1117 | |
Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 1118 | template <typename T> |
T.J. Mercier | 5ed5e1b | 2022-08-22 21:25:09 +0000 | [diff] [blame] | 1119 | bool TaskProfiles::SetUserProfiles(uid_t uid, std::span<const T> profiles, bool use_fd_cache) { |
| 1120 | for (const auto& name : profiles) { |
| 1121 | TaskProfile* profile = GetProfile(name); |
| 1122 | if (profile != nullptr) { |
| 1123 | if (use_fd_cache) { |
| 1124 | profile->EnableResourceCaching(ProfileAction::RCT_PROCESS); |
| 1125 | } |
| 1126 | if (!profile->ExecuteForUID(uid)) { |
| 1127 | PLOG(WARNING) << "Failed to apply " << name << " process profile"; |
| 1128 | } |
| 1129 | } else { |
| 1130 | PLOG(WARNING) << "Failed to find " << name << "process profile"; |
| 1131 | } |
| 1132 | } |
| 1133 | return true; |
| 1134 | } |
| 1135 | |
| 1136 | template <typename T> |
Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 1137 | bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, std::span<const T> profiles, |
| 1138 | bool use_fd_cache) { |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame] | 1139 | bool success = true; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1140 | for (const auto& name : profiles) { |
| 1141 | TaskProfile* profile = GetProfile(name); |
| 1142 | if (profile != nullptr) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 1143 | if (use_fd_cache) { |
| 1144 | profile->EnableResourceCaching(ProfileAction::RCT_PROCESS); |
| 1145 | } |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1146 | if (!profile->ExecuteForProcess(uid, pid)) { |
Krzysztof Kosiński | 0310ec4 | 2023-03-01 04:17:57 +0000 | [diff] [blame] | 1147 | LOG(WARNING) << "Failed to apply " << name << " process profile"; |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame] | 1148 | success = false; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1149 | } |
| 1150 | } else { |
Krzysztof Kosiński | 0310ec4 | 2023-03-01 04:17:57 +0000 | [diff] [blame] | 1151 | LOG(WARNING) << "Failed to find " << name << " process profile"; |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame] | 1152 | success = false; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1153 | } |
| 1154 | } |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame] | 1155 | return success; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1156 | } |
| 1157 | |
Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 1158 | template <typename T> |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 1159 | bool TaskProfiles::SetTaskProfiles(pid_t tid, std::span<const T> profiles, bool use_fd_cache) { |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame] | 1160 | bool success = true; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1161 | for (const auto& name : profiles) { |
| 1162 | TaskProfile* profile = GetProfile(name); |
| 1163 | if (profile != nullptr) { |
| 1164 | if (use_fd_cache) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 1165 | profile->EnableResourceCaching(ProfileAction::RCT_TASK); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1166 | } |
| 1167 | if (!profile->ExecuteForTask(tid)) { |
Krzysztof Kosiński | 0310ec4 | 2023-03-01 04:17:57 +0000 | [diff] [blame] | 1168 | LOG(WARNING) << "Failed to apply " << name << " task profile"; |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame] | 1169 | success = false; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1170 | } |
| 1171 | } else { |
Krzysztof Kosiński | 0310ec4 | 2023-03-01 04:17:57 +0000 | [diff] [blame] | 1172 | LOG(WARNING) << "Failed to find " << name << " task profile"; |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame] | 1173 | success = false; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1174 | } |
| 1175 | } |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame] | 1176 | return success; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 1177 | } |
Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 1178 | |
| 1179 | template bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, |
| 1180 | std::span<const std::string> profiles, |
| 1181 | bool use_fd_cache); |
| 1182 | template bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, |
| 1183 | std::span<const std::string_view> profiles, |
| 1184 | bool use_fd_cache); |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 1185 | template bool TaskProfiles::SetTaskProfiles(pid_t tid, std::span<const std::string> profiles, |
Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 1186 | bool use_fd_cache); |
T.J. Mercier | 1c00799 | 2024-01-25 16:29:54 +0000 | [diff] [blame] | 1187 | template bool TaskProfiles::SetTaskProfiles(pid_t tid, std::span<const std::string_view> profiles, |
Bart Van Assche | f32c4ec | 2022-08-02 13:18:12 -0700 | [diff] [blame] | 1188 | bool use_fd_cache); |
T.J. Mercier | 5ed5e1b | 2022-08-22 21:25:09 +0000 | [diff] [blame] | 1189 | template bool TaskProfiles::SetUserProfiles(uid_t uid, std::span<const std::string> profiles, |
| 1190 | bool use_fd_cache); |