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