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 | |
| 20 | #include <fcntl.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 21 | #include <task_profiles.h> |
| 22 | #include <string> |
| 23 | |
| 24 | #include <android-base/file.h> |
| 25 | #include <android-base/logging.h> |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 26 | #include <android-base/properties.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 28 | #include <android-base/strings.h> |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 29 | #include <android-base/threads.h> |
| 30 | |
| 31 | #include <cutils/android_filesystem_config.h> |
| 32 | |
| 33 | #include <json/reader.h> |
| 34 | #include <json/value.h> |
| 35 | |
Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 36 | // To avoid issues in sdk_mac build |
| 37 | #if defined(__ANDROID__) |
| 38 | #include <sys/prctl.h> |
| 39 | #endif |
| 40 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 41 | using android::base::GetThreadId; |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 42 | using android::base::GetUintProperty; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 43 | using android::base::StringPrintf; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 44 | using android::base::StringReplace; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 45 | using android::base::unique_fd; |
| 46 | using android::base::WriteStringToFile; |
| 47 | |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 48 | static constexpr const char* TASK_PROFILE_DB_FILE = "/etc/task_profiles.json"; |
| 49 | static constexpr const char* TASK_PROFILE_DB_VENDOR_FILE = "/vendor/etc/task_profiles.json"; |
| 50 | |
| 51 | static constexpr const char* TEMPLATE_TASK_PROFILE_API_FILE = |
| 52 | "/etc/task_profiles/task_profiles_%u.json"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 53 | |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 54 | class FdCacheHelper { |
| 55 | public: |
| 56 | enum FdState { |
| 57 | FDS_INACCESSIBLE = -1, |
| 58 | FDS_APP_DEPENDENT = -2, |
| 59 | FDS_NOT_CACHED = -3, |
| 60 | }; |
| 61 | |
| 62 | static void Cache(const std::string& path, android::base::unique_fd& fd); |
| 63 | static void Drop(android::base::unique_fd& fd); |
| 64 | static void Init(const std::string& path, android::base::unique_fd& fd); |
| 65 | static bool IsCached(const android::base::unique_fd& fd) { return fd > FDS_INACCESSIBLE; } |
| 66 | |
| 67 | private: |
| 68 | static bool IsAppDependentPath(const std::string& path); |
| 69 | }; |
| 70 | |
| 71 | void FdCacheHelper::Init(const std::string& path, android::base::unique_fd& fd) { |
| 72 | // file descriptors for app-dependent paths can't be cached |
| 73 | if (IsAppDependentPath(path)) { |
| 74 | // file descriptor is not cached |
| 75 | fd.reset(FDS_APP_DEPENDENT); |
| 76 | return; |
| 77 | } |
| 78 | // file descriptor can be cached later on request |
| 79 | fd.reset(FDS_NOT_CACHED); |
| 80 | } |
| 81 | |
| 82 | void FdCacheHelper::Cache(const std::string& path, android::base::unique_fd& fd) { |
| 83 | if (fd != FDS_NOT_CACHED) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | if (access(path.c_str(), W_OK) != 0) { |
| 88 | // file is not accessible |
| 89 | fd.reset(FDS_INACCESSIBLE); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_WRONLY | O_CLOEXEC))); |
| 94 | if (tmp_fd < 0) { |
| 95 | PLOG(ERROR) << "Failed to cache fd '" << path << "'"; |
| 96 | fd.reset(FDS_INACCESSIBLE); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | fd = std::move(tmp_fd); |
| 101 | } |
| 102 | |
| 103 | void FdCacheHelper::Drop(android::base::unique_fd& fd) { |
| 104 | if (fd == FDS_NOT_CACHED) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | fd.reset(FDS_NOT_CACHED); |
| 109 | } |
| 110 | |
| 111 | bool FdCacheHelper::IsAppDependentPath(const std::string& path) { |
| 112 | return path.find("<uid>", 0) != std::string::npos || path.find("<pid>", 0) != std::string::npos; |
| 113 | } |
| 114 | |
Bart Van Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 115 | IProfileAttribute::~IProfileAttribute() = default; |
| 116 | |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 117 | void ProfileAttribute::Reset(const CgroupController& controller, const std::string& file_name) { |
| 118 | controller_ = controller; |
| 119 | file_name_ = file_name; |
| 120 | } |
| 121 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 122 | bool ProfileAttribute::GetPathForTask(int tid, std::string* path) const { |
| 123 | std::string subgroup; |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 124 | if (!controller()->GetTaskGroup(tid, &subgroup)) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | |
| 128 | if (path == nullptr) { |
| 129 | return true; |
| 130 | } |
| 131 | |
Bart Van Assche | bc077ff | 2022-02-17 01:26:44 +0000 | [diff] [blame] | 132 | const std::string& file_name = |
| 133 | controller()->version() == 2 && !file_v2_name_.empty() ? file_v2_name_ : file_name_; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 134 | if (subgroup.empty()) { |
Bart Van Assche | bc077ff | 2022-02-17 01:26:44 +0000 | [diff] [blame] | 135 | *path = StringPrintf("%s/%s", controller()->path(), file_name.c_str()); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 136 | } else { |
Bart Van Assche | bc077ff | 2022-02-17 01:26:44 +0000 | [diff] [blame] | 137 | *path = StringPrintf("%s/%s/%s", controller()->path(), subgroup.c_str(), file_name.c_str()); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 138 | } |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | bool SetClampsAction::ExecuteForProcess(uid_t, pid_t) const { |
| 143 | // TODO: add support when kernel supports util_clamp |
| 144 | LOG(WARNING) << "SetClampsAction::ExecuteForProcess is not supported"; |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | bool SetClampsAction::ExecuteForTask(int) const { |
| 149 | // TODO: add support when kernel supports util_clamp |
| 150 | LOG(WARNING) << "SetClampsAction::ExecuteForTask is not supported"; |
| 151 | return false; |
| 152 | } |
| 153 | |
Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 154 | // To avoid issues in sdk_mac build |
| 155 | #if defined(__ANDROID__) |
| 156 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 157 | bool SetTimerSlackAction::IsTimerSlackSupported(int tid) { |
| 158 | auto file = StringPrintf("/proc/%d/timerslack_ns", tid); |
| 159 | |
| 160 | return (access(file.c_str(), W_OK) == 0); |
| 161 | } |
| 162 | |
| 163 | bool SetTimerSlackAction::ExecuteForTask(int tid) const { |
| 164 | static bool sys_supports_timerslack = IsTimerSlackSupported(tid); |
| 165 | |
| 166 | // v4.6+ kernels support the /proc/<tid>/timerslack_ns interface. |
| 167 | // TODO: once we've backported this, log if the open(2) fails. |
| 168 | if (sys_supports_timerslack) { |
| 169 | auto file = StringPrintf("/proc/%d/timerslack_ns", tid); |
| 170 | if (!WriteStringToFile(std::to_string(slack_), file)) { |
Suren Baghdasaryan | 2bc5228 | 2019-02-12 17:30:26 -0800 | [diff] [blame] | 171 | if (errno == ENOENT) { |
| 172 | // This happens when process is already dead |
| 173 | return true; |
| 174 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 175 | PLOG(ERROR) << "set_timerslack_ns write failed"; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // TODO: Remove when /proc/<tid>/timerslack_ns interface is backported. |
| 180 | if (tid == 0 || tid == GetThreadId()) { |
| 181 | if (prctl(PR_SET_TIMERSLACK, slack_) == -1) { |
| 182 | PLOG(ERROR) << "set_timerslack_ns prctl failed"; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
Bart Van Assche | 20d59bd | 2022-01-24 19:45:59 +0000 | [diff] [blame] | 189 | #else |
| 190 | |
| 191 | bool SetTimerSlackAction::ExecuteForTask(int) const { |
| 192 | return true; |
| 193 | }; |
| 194 | |
Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 195 | #endif |
| 196 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 197 | bool SetAttributeAction::ExecuteForProcess(uid_t, pid_t pid) const { |
| 198 | return ExecuteForTask(pid); |
| 199 | } |
| 200 | |
| 201 | bool SetAttributeAction::ExecuteForTask(int tid) const { |
| 202 | std::string path; |
| 203 | |
| 204 | if (!attribute_->GetPathForTask(tid, &path)) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 205 | LOG(ERROR) << "Failed to find cgroup for tid " << tid; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 206 | return false; |
| 207 | } |
| 208 | |
| 209 | if (!WriteStringToFile(value_, path)) { |
Bart Van Assche | 9b5a232 | 2022-03-22 16:15:00 -0700 | [diff] [blame] | 210 | if (access(path.c_str(), F_OK) < 0) { |
Bart Van Assche | 59af680 | 2022-01-24 21:08:57 +0000 | [diff] [blame] | 211 | if (optional_) { |
| 212 | return true; |
| 213 | } else { |
| 214 | LOG(ERROR) << "No such cgroup attribute: " << path; |
| 215 | return false; |
| 216 | } |
| 217 | } |
Bart Van Assche | 54136f8 | 2022-03-31 11:26:42 -0700 | [diff] [blame] | 218 | // The PLOG() statement below uses the error code stored in `errno` by |
| 219 | // WriteStringToFile() because access() only overwrites `errno` if it fails |
| 220 | // 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] | 221 | PLOG(ERROR) << "Failed to write '" << value_ << "' to " << path; |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | return true; |
| 226 | } |
| 227 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 228 | SetCgroupAction::SetCgroupAction(const CgroupController& c, const std::string& p) |
| 229 | : controller_(c), path_(p) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 230 | FdCacheHelper::Init(controller_.GetTasksFilePath(path_), fd_[ProfileAction::RCT_TASK]); |
| 231 | // uid and pid don't matter because IsAppDependentPath ensures the path doesn't use them |
| 232 | FdCacheHelper::Init(controller_.GetProcsFilePath(path_, 0, 0), fd_[ProfileAction::RCT_PROCESS]); |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 233 | } |
| 234 | |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 235 | bool SetCgroupAction::AddTidToCgroup(int tid, int fd, const char* controller_name) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 236 | if (tid <= 0) { |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | std::string value = std::to_string(tid); |
| 241 | |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 242 | if (TEMP_FAILURE_RETRY(write(fd, value.c_str(), value.length())) == value.length()) { |
| 243 | return true; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 246 | // If the thread is in the process of exiting, don't flag an error |
| 247 | if (errno == ESRCH) { |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | // ENOSPC is returned when cpuset cgroup that we are joining has no online cpus |
| 252 | if (errno == ENOSPC && !strcmp(controller_name, "cpuset")) { |
| 253 | // This is an abnormal case happening only in testing, so report it only once |
| 254 | static bool empty_cpuset_reported = false; |
| 255 | |
| 256 | if (empty_cpuset_reported) { |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | LOG(ERROR) << "Failed to add task '" << value |
| 261 | << "' into cpuset because all cpus in that cpuset are offline"; |
| 262 | empty_cpuset_reported = true; |
| 263 | } else { |
| 264 | PLOG(ERROR) << "AddTidToCgroup failed to write '" << value << "'; fd=" << fd; |
| 265 | } |
| 266 | |
| 267 | return false; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 268 | } |
| 269 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 270 | ProfileAction::CacheUseResult SetCgroupAction::UseCachedFd(ResourceCacheType cache_type, |
| 271 | int id) const { |
| 272 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 273 | if (FdCacheHelper::IsCached(fd_[cache_type])) { |
| 274 | // fd is cached, reuse it |
| 275 | if (!AddTidToCgroup(id, fd_[cache_type], controller()->name())) { |
| 276 | LOG(ERROR) << "Failed to add task into cgroup"; |
| 277 | return ProfileAction::FAIL; |
| 278 | } |
| 279 | return ProfileAction::SUCCESS; |
| 280 | } |
| 281 | |
| 282 | if (fd_[cache_type] == FdCacheHelper::FDS_INACCESSIBLE) { |
| 283 | // no permissions to access the file, ignore |
| 284 | return ProfileAction::SUCCESS; |
| 285 | } |
| 286 | |
| 287 | if (cache_type == ResourceCacheType::RCT_TASK && |
| 288 | fd_[cache_type] == FdCacheHelper::FDS_APP_DEPENDENT) { |
| 289 | // application-dependent path can't be used with tid |
| 290 | PLOG(ERROR) << "Application profile can't be applied to a thread"; |
| 291 | return ProfileAction::FAIL; |
| 292 | } |
| 293 | |
| 294 | return ProfileAction::UNUSED; |
| 295 | } |
| 296 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 297 | bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 298 | CacheUseResult result = UseCachedFd(ProfileAction::RCT_PROCESS, pid); |
| 299 | if (result != ProfileAction::UNUSED) { |
| 300 | return result == ProfileAction::SUCCESS; |
| 301 | } |
| 302 | |
| 303 | // fd was not cached or cached fd can't be used |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 304 | std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 305 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC))); |
| 306 | if (tmp_fd < 0) { |
Elliott Hughes | 08b4d32 | 2019-03-14 20:06:36 -0700 | [diff] [blame] | 307 | PLOG(WARNING) << "Failed to open " << procs_path; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 308 | return false; |
| 309 | } |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 310 | if (!AddTidToCgroup(pid, tmp_fd, controller()->name())) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 311 | LOG(ERROR) << "Failed to add task into cgroup"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 312 | return false; |
| 313 | } |
| 314 | |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | bool SetCgroupAction::ExecuteForTask(int tid) const { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 319 | CacheUseResult result = UseCachedFd(ProfileAction::RCT_TASK, tid); |
| 320 | if (result != ProfileAction::UNUSED) { |
| 321 | return result == ProfileAction::SUCCESS; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 324 | // fd was not cached or cached fd can't be used |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 325 | std::string tasks_path = controller()->GetTasksFilePath(path_); |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 326 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC))); |
| 327 | if (tmp_fd < 0) { |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 328 | PLOG(WARNING) << "Failed to open " << tasks_path; |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 329 | return false; |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 330 | } |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 331 | if (!AddTidToCgroup(tid, tmp_fd, controller()->name())) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 332 | LOG(ERROR) << "Failed to add task into cgroup"; |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 333 | return false; |
| 334 | } |
| 335 | |
| 336 | return true; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 339 | void SetCgroupAction::EnableResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 340 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 341 | // Return early to prevent unnecessary calls to controller_.Get{Tasks|Procs}FilePath() which |
| 342 | // include regex evaluations |
| 343 | if (fd_[cache_type] != FdCacheHelper::FDS_NOT_CACHED) { |
| 344 | return; |
| 345 | } |
| 346 | switch (cache_type) { |
| 347 | case (ProfileAction::RCT_TASK): |
| 348 | FdCacheHelper::Cache(controller_.GetTasksFilePath(path_), fd_[cache_type]); |
| 349 | break; |
| 350 | case (ProfileAction::RCT_PROCESS): |
| 351 | // uid and pid don't matter because IsAppDependentPath ensures the path doesn't use them |
| 352 | FdCacheHelper::Cache(controller_.GetProcsFilePath(path_, 0, 0), fd_[cache_type]); |
| 353 | break; |
| 354 | default: |
| 355 | LOG(ERROR) << "Invalid cache type is specified!"; |
| 356 | break; |
| 357 | } |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 360 | void SetCgroupAction::DropResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 361 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 362 | FdCacheHelper::Drop(fd_[cache_type]); |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 365 | WriteFileAction::WriteFileAction(const std::string& task_path, const std::string& proc_path, |
| 366 | const std::string& value, bool logfailures) |
| 367 | : task_path_(task_path), proc_path_(proc_path), value_(value), logfailures_(logfailures) { |
| 368 | FdCacheHelper::Init(task_path_, fd_[ProfileAction::RCT_TASK]); |
| 369 | if (!proc_path_.empty()) FdCacheHelper::Init(proc_path_, fd_[ProfileAction::RCT_PROCESS]); |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 370 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 371 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 372 | bool WriteFileAction::WriteValueToFile(const std::string& value_, ResourceCacheType cache_type, |
| 373 | int uid, int pid, bool logfailures) const { |
| 374 | std::string value(value_); |
| 375 | |
| 376 | value = StringReplace(value, "<uid>", std::to_string(uid), true); |
| 377 | value = StringReplace(value, "<pid>", std::to_string(pid), true); |
| 378 | |
| 379 | CacheUseResult result = UseCachedFd(cache_type, value); |
| 380 | |
| 381 | if (result != ProfileAction::UNUSED) { |
| 382 | return result == ProfileAction::SUCCESS; |
| 383 | } |
| 384 | |
| 385 | std::string path; |
| 386 | if (cache_type == ProfileAction::RCT_TASK || proc_path_.empty()) { |
| 387 | path = task_path_; |
| 388 | } else { |
| 389 | path = proc_path_; |
| 390 | } |
| 391 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 392 | // Use WriteStringToFd instead of WriteStringToFile because the latter will open file with |
| 393 | // O_TRUNC which causes kernfs_mutex contention |
| 394 | 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] | 395 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 396 | if (tmp_fd < 0) { |
| 397 | if (logfailures) PLOG(WARNING) << "Failed to open " << path; |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | if (!WriteStringToFd(value, tmp_fd)) { |
| 402 | if (logfailures) PLOG(ERROR) << "Failed to write '" << value << "' to " << path; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 403 | return false; |
| 404 | } |
| 405 | |
| 406 | return true; |
| 407 | } |
| 408 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 409 | ProfileAction::CacheUseResult WriteFileAction::UseCachedFd(ResourceCacheType cache_type, |
| 410 | const std::string& value) const { |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 411 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 412 | if (FdCacheHelper::IsCached(fd_[cache_type])) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 413 | // fd is cached, reuse it |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 414 | bool ret = WriteStringToFd(value, fd_[cache_type]); |
| 415 | |
| 416 | if (!ret && logfailures_) { |
| 417 | if (cache_type == ProfileAction::RCT_TASK || proc_path_.empty()) { |
| 418 | PLOG(ERROR) << "Failed to write '" << value << "' to " << task_path_; |
| 419 | } else { |
| 420 | PLOG(ERROR) << "Failed to write '" << value << "' to " << proc_path_; |
| 421 | } |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 422 | } |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 423 | return ret ? ProfileAction::SUCCESS : ProfileAction::FAIL; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 426 | if (fd_[cache_type] == FdCacheHelper::FDS_INACCESSIBLE) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 427 | // no permissions to access the file, ignore |
| 428 | return ProfileAction::SUCCESS; |
| 429 | } |
| 430 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 431 | if (cache_type == ResourceCacheType::RCT_TASK && |
| 432 | fd_[cache_type] == FdCacheHelper::FDS_APP_DEPENDENT) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 433 | // application-dependent path can't be used with tid |
| 434 | PLOG(ERROR) << "Application profile can't be applied to a thread"; |
| 435 | return ProfileAction::FAIL; |
| 436 | } |
| 437 | return ProfileAction::UNUSED; |
| 438 | } |
| 439 | |
| 440 | bool WriteFileAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 441 | if (!proc_path_.empty()) { |
| 442 | return WriteValueToFile(value_, ProfileAction::RCT_PROCESS, uid, pid, logfailures_); |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 445 | DIR* d; |
| 446 | struct dirent* de; |
| 447 | char proc_path[255]; |
| 448 | int t_pid; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 449 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 450 | sprintf(proc_path, "/proc/%d/task", pid); |
| 451 | if (!(d = opendir(proc_path))) { |
| 452 | return false; |
| 453 | } |
| 454 | |
| 455 | while ((de = readdir(d))) { |
| 456 | if (de->d_name[0] == '.') { |
| 457 | continue; |
| 458 | } |
| 459 | |
| 460 | t_pid = atoi(de->d_name); |
| 461 | |
| 462 | if (!t_pid) { |
| 463 | continue; |
| 464 | } |
| 465 | |
| 466 | WriteValueToFile(value_, ProfileAction::RCT_TASK, uid, t_pid, logfailures_); |
| 467 | } |
| 468 | |
| 469 | closedir(d); |
| 470 | |
| 471 | return true; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 472 | } |
| 473 | |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 474 | bool WriteFileAction::ExecuteForTask(int tid) const { |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 475 | return WriteValueToFile(value_, ProfileAction::RCT_TASK, getuid(), tid, logfailures_); |
| 476 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 477 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 478 | void WriteFileAction::EnableResourceCaching(ResourceCacheType cache_type) { |
| 479 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 480 | if (fd_[cache_type] != FdCacheHelper::FDS_NOT_CACHED) { |
| 481 | return; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 482 | } |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 483 | switch (cache_type) { |
| 484 | case (ProfileAction::RCT_TASK): |
| 485 | FdCacheHelper::Cache(task_path_, fd_[cache_type]); |
| 486 | break; |
| 487 | case (ProfileAction::RCT_PROCESS): |
| 488 | if (!proc_path_.empty()) FdCacheHelper::Cache(proc_path_, fd_[cache_type]); |
| 489 | break; |
| 490 | default: |
| 491 | LOG(ERROR) << "Invalid cache type is specified!"; |
| 492 | break; |
| 493 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 494 | } |
| 495 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 496 | void WriteFileAction::DropResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 497 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 498 | FdCacheHelper::Drop(fd_[cache_type]); |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 501 | bool ApplyProfileAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
| 502 | for (const auto& profile : profiles_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 503 | profile->ExecuteForProcess(uid, pid); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 504 | } |
| 505 | return true; |
| 506 | } |
| 507 | |
| 508 | bool ApplyProfileAction::ExecuteForTask(int tid) const { |
| 509 | for (const auto& profile : profiles_) { |
Wei Wang | 8722e4d | 2021-05-14 12:34:54 -0700 | [diff] [blame] | 510 | profile->ExecuteForTask(tid); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 511 | } |
| 512 | return true; |
| 513 | } |
| 514 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 515 | void ApplyProfileAction::EnableResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 516 | for (const auto& profile : profiles_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 517 | profile->EnableResourceCaching(cache_type); |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 521 | void ApplyProfileAction::DropResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 522 | for (const auto& profile : profiles_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 523 | profile->DropResourceCaching(cache_type); |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
Suren Baghdasaryan | 8438595 | 2020-01-24 16:36:10 -0800 | [diff] [blame] | 527 | void TaskProfile::MoveTo(TaskProfile* profile) { |
| 528 | profile->elements_ = std::move(elements_); |
| 529 | profile->res_cached_ = res_cached_; |
| 530 | } |
| 531 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 532 | bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const { |
| 533 | for (const auto& element : elements_) { |
| 534 | if (!element->ExecuteForProcess(uid, pid)) { |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 535 | LOG(VERBOSE) << "Applying profile action " << element->Name() << " failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 536 | return false; |
| 537 | } |
| 538 | } |
| 539 | return true; |
| 540 | } |
| 541 | |
| 542 | bool TaskProfile::ExecuteForTask(int tid) const { |
| 543 | if (tid == 0) { |
| 544 | tid = GetThreadId(); |
| 545 | } |
| 546 | for (const auto& element : elements_) { |
| 547 | if (!element->ExecuteForTask(tid)) { |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 548 | LOG(VERBOSE) << "Applying profile action " << element->Name() << " failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 549 | return false; |
| 550 | } |
| 551 | } |
| 552 | return true; |
| 553 | } |
| 554 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 555 | void TaskProfile::EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) { |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 556 | if (res_cached_) { |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | for (auto& element : elements_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 561 | element->EnableResourceCaching(cache_type); |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | res_cached_ = true; |
| 565 | } |
| 566 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 567 | void TaskProfile::DropResourceCaching(ProfileAction::ResourceCacheType cache_type) { |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 568 | if (!res_cached_) { |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | for (auto& element : elements_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 573 | element->DropResourceCaching(cache_type); |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | res_cached_ = false; |
| 577 | } |
| 578 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 579 | void TaskProfiles::DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const { |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 580 | for (auto& iter : profiles_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 581 | iter.second->DropResourceCaching(cache_type); |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 585 | TaskProfiles& TaskProfiles::GetInstance() { |
Peter Collingbourne | dba6d44 | 2019-03-20 21:09:46 -0700 | [diff] [blame] | 586 | // Deliberately leak this object to avoid a race between destruction on |
| 587 | // process exit and concurrent access from another thread. |
| 588 | static auto* instance = new TaskProfiles; |
| 589 | return *instance; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | TaskProfiles::TaskProfiles() { |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 593 | // load system task profiles |
| 594 | if (!Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_FILE)) { |
| 595 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_FILE << " for [" << getpid() << "] failed"; |
| 596 | } |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 597 | |
| 598 | // load API-level specific system task profiles if available |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 599 | 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] | 600 | if (api_level > 0) { |
| 601 | std::string api_profiles_path = |
| 602 | android::base::StringPrintf(TEMPLATE_TASK_PROFILE_API_FILE, api_level); |
| 603 | if (!access(api_profiles_path.c_str(), F_OK) || errno != ENOENT) { |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 604 | if (!Load(CgroupMap::GetInstance(), api_profiles_path)) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 605 | LOG(ERROR) << "Loading " << api_profiles_path << " for [" << getpid() << "] failed"; |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 606 | } |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 610 | // load vendor task profiles if the file exists |
| 611 | if (!access(TASK_PROFILE_DB_VENDOR_FILE, F_OK) && |
| 612 | !Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_VENDOR_FILE)) { |
| 613 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_VENDOR_FILE << " for [" << getpid() |
| 614 | << "] failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 618 | bool TaskProfiles::Load(const CgroupMap& cg_map, const std::string& file_name) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 619 | std::string json_doc; |
| 620 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 621 | if (!android::base::ReadFileToString(file_name, &json_doc)) { |
| 622 | LOG(ERROR) << "Failed to read task profiles from " << file_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 623 | return false; |
| 624 | } |
| 625 | |
Haibo Huang | d9ac92a | 2021-02-24 17:34:50 -0800 | [diff] [blame] | 626 | Json::CharReaderBuilder builder; |
| 627 | std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 628 | Json::Value root; |
Haibo Huang | d9ac92a | 2021-02-24 17:34:50 -0800 | [diff] [blame] | 629 | std::string errorMessage; |
| 630 | if (!reader->parse(&*json_doc.begin(), &*json_doc.end(), &root, &errorMessage)) { |
| 631 | LOG(ERROR) << "Failed to parse task profiles: " << errorMessage; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 632 | return false; |
| 633 | } |
| 634 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 635 | const Json::Value& attr = root["Attributes"]; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 636 | for (Json::Value::ArrayIndex i = 0; i < attr.size(); ++i) { |
| 637 | std::string name = attr[i]["Name"].asString(); |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 638 | std::string controller_name = attr[i]["Controller"].asString(); |
| 639 | std::string file_attr = attr[i]["File"].asString(); |
Bart Van Assche | bc077ff | 2022-02-17 01:26:44 +0000 | [diff] [blame] | 640 | std::string file_v2_attr = attr[i]["FileV2"].asString(); |
| 641 | |
| 642 | if (!file_v2_attr.empty() && file_attr.empty()) { |
| 643 | LOG(ERROR) << "Attribute " << name << " has FileV2 but no File property"; |
| 644 | return false; |
| 645 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 646 | |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 647 | auto controller = cg_map.FindController(controller_name); |
| 648 | if (controller.HasValue()) { |
| 649 | auto iter = attributes_.find(name); |
| 650 | if (iter == attributes_.end()) { |
Bart Van Assche | bc077ff | 2022-02-17 01:26:44 +0000 | [diff] [blame] | 651 | attributes_[name] = |
| 652 | std::make_unique<ProfileAttribute>(controller, file_attr, file_v2_attr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 653 | } else { |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 654 | iter->second->Reset(controller, file_attr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 655 | } |
| 656 | } else { |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 657 | LOG(WARNING) << "Controller " << controller_name << " is not found"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 661 | const Json::Value& profiles_val = root["Profiles"]; |
| 662 | for (Json::Value::ArrayIndex i = 0; i < profiles_val.size(); ++i) { |
| 663 | const Json::Value& profile_val = profiles_val[i]; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 664 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 665 | std::string profile_name = profile_val["Name"].asString(); |
| 666 | const Json::Value& actions = profile_val["Actions"]; |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 667 | auto profile = std::make_shared<TaskProfile>(profile_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 668 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 669 | for (Json::Value::ArrayIndex act_idx = 0; act_idx < actions.size(); ++act_idx) { |
| 670 | const Json::Value& action_val = actions[act_idx]; |
| 671 | std::string action_name = action_val["Name"].asString(); |
| 672 | const Json::Value& params_val = action_val["Params"]; |
| 673 | if (action_name == "JoinCgroup") { |
| 674 | std::string controller_name = params_val["Controller"].asString(); |
| 675 | std::string path = params_val["Path"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 676 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 677 | auto controller = cg_map.FindController(controller_name); |
| 678 | if (controller.HasValue()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 679 | profile->Add(std::make_unique<SetCgroupAction>(controller, path)); |
| 680 | } else { |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 681 | LOG(WARNING) << "JoinCgroup: controller " << controller_name << " is not found"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 682 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 683 | } else if (action_name == "SetTimerSlack") { |
| 684 | std::string slack_value = params_val["Slack"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 685 | char* end; |
| 686 | unsigned long slack; |
| 687 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 688 | slack = strtoul(slack_value.c_str(), &end, 10); |
| 689 | if (end > slack_value.c_str()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 690 | profile->Add(std::make_unique<SetTimerSlackAction>(slack)); |
| 691 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 692 | LOG(WARNING) << "SetTimerSlack: invalid parameter: " << slack_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 693 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 694 | } else if (action_name == "SetAttribute") { |
| 695 | std::string attr_name = params_val["Name"].asString(); |
| 696 | std::string attr_value = params_val["Value"].asString(); |
Bart Van Assche | 59af680 | 2022-01-24 21:08:57 +0000 | [diff] [blame] | 697 | bool optional = strcmp(params_val["Optional"].asString().c_str(), "true") == 0; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 698 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 699 | auto iter = attributes_.find(attr_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 700 | if (iter != attributes_.end()) { |
Bart Van Assche | 59af680 | 2022-01-24 21:08:57 +0000 | [diff] [blame] | 701 | profile->Add(std::make_unique<SetAttributeAction>(iter->second.get(), |
| 702 | attr_value, optional)); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 703 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 704 | LOG(WARNING) << "SetAttribute: unknown attribute: " << attr_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 705 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 706 | } else if (action_name == "SetClamps") { |
| 707 | std::string boost_value = params_val["Boost"].asString(); |
| 708 | std::string clamp_value = params_val["Clamp"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 709 | char* end; |
| 710 | unsigned long boost; |
| 711 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 712 | boost = strtoul(boost_value.c_str(), &end, 10); |
| 713 | if (end > boost_value.c_str()) { |
| 714 | unsigned long clamp = strtoul(clamp_value.c_str(), &end, 10); |
| 715 | if (end > clamp_value.c_str()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 716 | profile->Add(std::make_unique<SetClampsAction>(boost, clamp)); |
| 717 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 718 | LOG(WARNING) << "SetClamps: invalid parameter " << clamp_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 719 | } |
| 720 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 721 | LOG(WARNING) << "SetClamps: invalid parameter: " << boost_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 722 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 723 | } else if (action_name == "WriteFile") { |
| 724 | std::string attr_filepath = params_val["FilePath"].asString(); |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 725 | std::string attr_procfilepath = params_val["ProcFilePath"].asString(); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 726 | std::string attr_value = params_val["Value"].asString(); |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 727 | // FilePath and Value are mandatory |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 728 | if (!attr_filepath.empty() && !attr_value.empty()) { |
Rick Yiu | 49fce95 | 2021-04-08 22:10:06 +0800 | [diff] [blame] | 729 | std::string attr_logfailures = params_val["LogFailures"].asString(); |
| 730 | bool logfailures = attr_logfailures.empty() || attr_logfailures == "true"; |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 731 | profile->Add(std::make_unique<WriteFileAction>(attr_filepath, attr_procfilepath, |
| 732 | attr_value, logfailures)); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 733 | } else if (attr_filepath.empty()) { |
| 734 | LOG(WARNING) << "WriteFile: invalid parameter: " |
| 735 | << "empty filepath"; |
| 736 | } else if (attr_value.empty()) { |
| 737 | LOG(WARNING) << "WriteFile: invalid parameter: " |
| 738 | << "empty value"; |
| 739 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 740 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 741 | LOG(WARNING) << "Unknown profile action: " << action_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 742 | } |
| 743 | } |
Suren Baghdasaryan | 8438595 | 2020-01-24 16:36:10 -0800 | [diff] [blame] | 744 | auto iter = profiles_.find(profile_name); |
| 745 | if (iter == profiles_.end()) { |
| 746 | profiles_[profile_name] = profile; |
| 747 | } else { |
| 748 | // Move the content rather that replace the profile because old profile might be |
| 749 | // referenced from an aggregate profile if vendor overrides task profiles |
| 750 | profile->MoveTo(iter->second.get()); |
| 751 | profile.reset(); |
| 752 | } |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | const Json::Value& aggregateprofiles_val = root["AggregateProfiles"]; |
| 756 | for (Json::Value::ArrayIndex i = 0; i < aggregateprofiles_val.size(); ++i) { |
| 757 | const Json::Value& aggregateprofile_val = aggregateprofiles_val[i]; |
| 758 | |
| 759 | std::string aggregateprofile_name = aggregateprofile_val["Name"].asString(); |
| 760 | const Json::Value& aggregateprofiles = aggregateprofile_val["Profiles"]; |
| 761 | std::vector<std::shared_ptr<TaskProfile>> profiles; |
| 762 | bool ret = true; |
| 763 | |
| 764 | for (Json::Value::ArrayIndex pf_idx = 0; pf_idx < aggregateprofiles.size(); ++pf_idx) { |
| 765 | std::string profile_name = aggregateprofiles[pf_idx].asString(); |
| 766 | |
| 767 | if (profile_name == aggregateprofile_name) { |
| 768 | LOG(WARNING) << "AggregateProfiles: recursive profile name: " << profile_name; |
| 769 | ret = false; |
| 770 | break; |
| 771 | } else if (profiles_.find(profile_name) == profiles_.end()) { |
| 772 | LOG(WARNING) << "AggregateProfiles: undefined profile name: " << profile_name; |
| 773 | ret = false; |
| 774 | break; |
| 775 | } else { |
| 776 | profiles.push_back(profiles_[profile_name]); |
| 777 | } |
| 778 | } |
| 779 | if (ret) { |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 780 | auto profile = std::make_shared<TaskProfile>(aggregateprofile_name); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 781 | profile->Add(std::make_unique<ApplyProfileAction>(profiles)); |
| 782 | profiles_[aggregateprofile_name] = profile; |
| 783 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | return true; |
| 787 | } |
| 788 | |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 789 | TaskProfile* TaskProfiles::GetProfile(const std::string& name) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 790 | auto iter = profiles_.find(name); |
| 791 | |
| 792 | if (iter != profiles_.end()) { |
| 793 | return iter->second.get(); |
| 794 | } |
| 795 | return nullptr; |
| 796 | } |
| 797 | |
Bart Van Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 798 | const IProfileAttribute* TaskProfiles::GetAttribute(const std::string& name) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 799 | auto iter = attributes_.find(name); |
| 800 | |
| 801 | if (iter != attributes_.end()) { |
| 802 | return iter->second.get(); |
| 803 | } |
| 804 | return nullptr; |
| 805 | } |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 806 | |
| 807 | bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 808 | const std::vector<std::string>& profiles, bool use_fd_cache) { |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame^] | 809 | bool success = true; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 810 | for (const auto& name : profiles) { |
| 811 | TaskProfile* profile = GetProfile(name); |
| 812 | if (profile != nullptr) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 813 | if (use_fd_cache) { |
| 814 | profile->EnableResourceCaching(ProfileAction::RCT_PROCESS); |
| 815 | } |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 816 | if (!profile->ExecuteForProcess(uid, pid)) { |
| 817 | PLOG(WARNING) << "Failed to apply " << name << " process profile"; |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame^] | 818 | success = false; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 819 | } |
| 820 | } else { |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame^] | 821 | PLOG(WARNING) << "Failed to find " << name << " process profile"; |
| 822 | success = false; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 823 | } |
| 824 | } |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame^] | 825 | return success; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | bool TaskProfiles::SetTaskProfiles(int tid, const std::vector<std::string>& profiles, |
| 829 | bool use_fd_cache) { |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame^] | 830 | bool success = true; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 831 | for (const auto& name : profiles) { |
| 832 | TaskProfile* profile = GetProfile(name); |
| 833 | if (profile != nullptr) { |
| 834 | if (use_fd_cache) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 835 | profile->EnableResourceCaching(ProfileAction::RCT_TASK); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 836 | } |
| 837 | if (!profile->ExecuteForTask(tid)) { |
| 838 | PLOG(WARNING) << "Failed to apply " << name << " task profile"; |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame^] | 839 | success = false; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 840 | } |
| 841 | } else { |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame^] | 842 | PLOG(WARNING) << "Failed to find " << name << " task profile"; |
| 843 | success = false; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 844 | } |
| 845 | } |
Inseob Kim | 538fc1f | 2022-04-13 18:50:12 +0000 | [diff] [blame^] | 846 | return success; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 847 | } |