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 | |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 115 | void ProfileAttribute::Reset(const CgroupController& controller, const std::string& file_name) { |
| 116 | controller_ = controller; |
| 117 | file_name_ = file_name; |
| 118 | } |
| 119 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 120 | bool ProfileAttribute::GetPathForTask(int tid, std::string* path) const { |
| 121 | std::string subgroup; |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 122 | if (!controller()->GetTaskGroup(tid, &subgroup)) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | |
| 126 | if (path == nullptr) { |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | if (subgroup.empty()) { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 131 | *path = StringPrintf("%s/%s", controller()->path(), file_name_.c_str()); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 132 | } else { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 133 | *path = StringPrintf("%s/%s/%s", controller()->path(), subgroup.c_str(), |
| 134 | file_name_.c_str()); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 135 | } |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | bool SetClampsAction::ExecuteForProcess(uid_t, pid_t) const { |
| 140 | // TODO: add support when kernel supports util_clamp |
| 141 | LOG(WARNING) << "SetClampsAction::ExecuteForProcess is not supported"; |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | bool SetClampsAction::ExecuteForTask(int) const { |
| 146 | // TODO: add support when kernel supports util_clamp |
| 147 | LOG(WARNING) << "SetClampsAction::ExecuteForTask is not supported"; |
| 148 | return false; |
| 149 | } |
| 150 | |
Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 151 | // To avoid issues in sdk_mac build |
| 152 | #if defined(__ANDROID__) |
| 153 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 154 | bool SetTimerSlackAction::IsTimerSlackSupported(int tid) { |
| 155 | auto file = StringPrintf("/proc/%d/timerslack_ns", tid); |
| 156 | |
| 157 | return (access(file.c_str(), W_OK) == 0); |
| 158 | } |
| 159 | |
| 160 | bool SetTimerSlackAction::ExecuteForTask(int tid) const { |
| 161 | static bool sys_supports_timerslack = IsTimerSlackSupported(tid); |
| 162 | |
| 163 | // v4.6+ kernels support the /proc/<tid>/timerslack_ns interface. |
| 164 | // TODO: once we've backported this, log if the open(2) fails. |
| 165 | if (sys_supports_timerslack) { |
| 166 | auto file = StringPrintf("/proc/%d/timerslack_ns", tid); |
| 167 | if (!WriteStringToFile(std::to_string(slack_), file)) { |
Suren Baghdasaryan | 2bc5228 | 2019-02-12 17:30:26 -0800 | [diff] [blame] | 168 | if (errno == ENOENT) { |
| 169 | // This happens when process is already dead |
| 170 | return true; |
| 171 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 172 | PLOG(ERROR) << "set_timerslack_ns write failed"; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // TODO: Remove when /proc/<tid>/timerslack_ns interface is backported. |
| 177 | if (tid == 0 || tid == GetThreadId()) { |
| 178 | if (prctl(PR_SET_TIMERSLACK, slack_) == -1) { |
| 179 | PLOG(ERROR) << "set_timerslack_ns prctl failed"; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return true; |
| 184 | } |
| 185 | |
Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 186 | #endif |
| 187 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 188 | bool SetAttributeAction::ExecuteForProcess(uid_t, pid_t pid) const { |
| 189 | return ExecuteForTask(pid); |
| 190 | } |
| 191 | |
| 192 | bool SetAttributeAction::ExecuteForTask(int tid) const { |
| 193 | std::string path; |
| 194 | |
| 195 | if (!attribute_->GetPathForTask(tid, &path)) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 196 | LOG(ERROR) << "Failed to find cgroup for tid " << tid; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | |
| 200 | if (!WriteStringToFile(value_, path)) { |
| 201 | PLOG(ERROR) << "Failed to write '" << value_ << "' to " << path; |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | return true; |
| 206 | } |
| 207 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 208 | SetCgroupAction::SetCgroupAction(const CgroupController& c, const std::string& p) |
| 209 | : controller_(c), path_(p) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame^] | 210 | FdCacheHelper::Init(controller_.GetTasksFilePath(path_), fd_); |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 211 | } |
| 212 | |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 213 | bool SetCgroupAction::AddTidToCgroup(int tid, int fd, const char* controller_name) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 214 | if (tid <= 0) { |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | std::string value = std::to_string(tid); |
| 219 | |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 220 | if (TEMP_FAILURE_RETRY(write(fd, value.c_str(), value.length())) == value.length()) { |
| 221 | return true; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 224 | // If the thread is in the process of exiting, don't flag an error |
| 225 | if (errno == ESRCH) { |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | // ENOSPC is returned when cpuset cgroup that we are joining has no online cpus |
| 230 | if (errno == ENOSPC && !strcmp(controller_name, "cpuset")) { |
| 231 | // This is an abnormal case happening only in testing, so report it only once |
| 232 | static bool empty_cpuset_reported = false; |
| 233 | |
| 234 | if (empty_cpuset_reported) { |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | LOG(ERROR) << "Failed to add task '" << value |
| 239 | << "' into cpuset because all cpus in that cpuset are offline"; |
| 240 | empty_cpuset_reported = true; |
| 241 | } else { |
| 242 | PLOG(ERROR) << "AddTidToCgroup failed to write '" << value << "'; fd=" << fd; |
| 243 | } |
| 244 | |
| 245 | return false; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 249 | std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 250 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC))); |
| 251 | if (tmp_fd < 0) { |
Elliott Hughes | 08b4d32 | 2019-03-14 20:06:36 -0700 | [diff] [blame] | 252 | PLOG(WARNING) << "Failed to open " << procs_path; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 253 | return false; |
| 254 | } |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 255 | if (!AddTidToCgroup(pid, tmp_fd, controller()->name())) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 256 | LOG(ERROR) << "Failed to add task into cgroup"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 257 | return false; |
| 258 | } |
| 259 | |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | bool SetCgroupAction::ExecuteForTask(int tid) const { |
mtk16036 | 53f79e6 | 2019-05-31 19:05:22 +0800 | [diff] [blame] | 264 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame^] | 265 | if (FdCacheHelper::IsCached(fd_)) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 266 | // fd is cached, reuse it |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 267 | if (!AddTidToCgroup(tid, fd_, controller()->name())) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 268 | LOG(ERROR) << "Failed to add task into cgroup"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 269 | return false; |
| 270 | } |
| 271 | return true; |
| 272 | } |
| 273 | |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame^] | 274 | if (fd_ == FdCacheHelper::FDS_INACCESSIBLE) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 275 | // no permissions to access the file, ignore |
| 276 | return true; |
| 277 | } |
| 278 | |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame^] | 279 | if (fd_ == FdCacheHelper::FDS_APP_DEPENDENT) { |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 280 | // application-dependent path can't be used with tid |
| 281 | PLOG(ERROR) << "Application profile can't be applied to a thread"; |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | // fd was not cached because cached fd can't be used |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 286 | std::string tasks_path = controller()->GetTasksFilePath(path_); |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 287 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC))); |
| 288 | if (tmp_fd < 0) { |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 289 | PLOG(WARNING) << "Failed to open " << tasks_path; |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 290 | return false; |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 291 | } |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 292 | if (!AddTidToCgroup(tid, tmp_fd, controller()->name())) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 293 | LOG(ERROR) << "Failed to add task into cgroup"; |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 294 | return false; |
| 295 | } |
| 296 | |
| 297 | return true; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame^] | 300 | void SetCgroupAction::EnableResourceCaching() { |
| 301 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 302 | FdCacheHelper::Cache(controller_.GetTasksFilePath(path_), fd_); |
| 303 | } |
| 304 | |
| 305 | void SetCgroupAction::DropResourceCaching() { |
| 306 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 307 | FdCacheHelper::Drop(fd_); |
| 308 | } |
| 309 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 310 | WriteFileAction::WriteFileAction(const std::string& path, const std::string& value, |
| 311 | bool logfailures) |
| 312 | : path_(path), value_(value), logfailures_(logfailures) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame^] | 313 | FdCacheHelper::Init(path_, fd_); |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 314 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 315 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 316 | bool WriteFileAction::WriteValueToFile(const std::string& value, const std::string& path, |
| 317 | bool logfailures) { |
| 318 | // Use WriteStringToFd instead of WriteStringToFile because the latter will open file with |
| 319 | // O_TRUNC which causes kernfs_mutex contention |
| 320 | 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] | 321 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 322 | if (tmp_fd < 0) { |
| 323 | if (logfailures) PLOG(WARNING) << "Failed to open " << path; |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | if (!WriteStringToFd(value, tmp_fd)) { |
| 328 | if (logfailures) PLOG(ERROR) << "Failed to write '" << value << "' to " << path; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 329 | return false; |
| 330 | } |
| 331 | |
| 332 | return true; |
| 333 | } |
| 334 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 335 | bool WriteFileAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
| 336 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 337 | std::string value(value_); |
| 338 | std::string path(path_); |
| 339 | |
| 340 | value = StringReplace(value, "<uid>", std::to_string(uid), true); |
| 341 | value = StringReplace(value, "<pid>", std::to_string(pid), true); |
| 342 | path = StringReplace(path, "<uid>", std::to_string(uid), true); |
| 343 | path = StringReplace(path, "<pid>", std::to_string(pid), true); |
| 344 | |
| 345 | return WriteValueToFile(value, path, logfailures_); |
| 346 | } |
| 347 | |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 348 | bool WriteFileAction::ExecuteForTask(int tid) const { |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 349 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 350 | std::string value(value_); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 351 | int uid = getuid(); |
| 352 | |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 353 | value = StringReplace(value, "<uid>", std::to_string(uid), true); |
| 354 | value = StringReplace(value, "<pid>", std::to_string(tid), true); |
| 355 | |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame^] | 356 | if (FdCacheHelper::IsCached(fd_)) { |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 357 | // fd is cached, reuse it |
| 358 | if (!WriteStringToFd(value, fd_)) { |
| 359 | if (logfailures_) PLOG(ERROR) << "Failed to write '" << value << "' to " << path_; |
| 360 | return false; |
| 361 | } |
| 362 | return true; |
| 363 | } |
| 364 | |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame^] | 365 | if (fd_ == FdCacheHelper::FDS_INACCESSIBLE) { |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 366 | // no permissions to access the file, ignore |
| 367 | return true; |
| 368 | } |
| 369 | |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame^] | 370 | if (fd_ == FdCacheHelper::FDS_APP_DEPENDENT) { |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 371 | // application-dependent path can't be used with tid |
| 372 | PLOG(ERROR) << "Application profile can't be applied to a thread"; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 373 | return false; |
| 374 | } |
| 375 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 376 | return WriteValueToFile(value, path_, logfailures_); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 377 | } |
| 378 | |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame^] | 379 | void WriteFileAction::EnableResourceCaching() { |
| 380 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 381 | FdCacheHelper::Cache(path_, fd_); |
| 382 | } |
| 383 | |
| 384 | void WriteFileAction::DropResourceCaching() { |
| 385 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 386 | FdCacheHelper::Drop(fd_); |
| 387 | } |
| 388 | |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 389 | bool ApplyProfileAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
| 390 | for (const auto& profile : profiles_) { |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 391 | if (!profile->ExecuteForProcess(uid, pid)) { |
| 392 | PLOG(WARNING) << "ExecuteForProcess failed for aggregate profile"; |
| 393 | } |
| 394 | } |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | bool ApplyProfileAction::ExecuteForTask(int tid) const { |
| 399 | for (const auto& profile : profiles_) { |
Wei Wang | 8722e4d | 2021-05-14 12:34:54 -0700 | [diff] [blame] | 400 | profile->ExecuteForTask(tid); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 401 | } |
| 402 | return true; |
| 403 | } |
| 404 | |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 405 | void ApplyProfileAction::EnableResourceCaching() { |
| 406 | for (const auto& profile : profiles_) { |
| 407 | profile->EnableResourceCaching(); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | void ApplyProfileAction::DropResourceCaching() { |
| 412 | for (const auto& profile : profiles_) { |
| 413 | profile->DropResourceCaching(); |
| 414 | } |
| 415 | } |
| 416 | |
Suren Baghdasaryan | 8438595 | 2020-01-24 16:36:10 -0800 | [diff] [blame] | 417 | void TaskProfile::MoveTo(TaskProfile* profile) { |
| 418 | profile->elements_ = std::move(elements_); |
| 419 | profile->res_cached_ = res_cached_; |
| 420 | } |
| 421 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 422 | bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const { |
| 423 | for (const auto& element : elements_) { |
| 424 | if (!element->ExecuteForProcess(uid, pid)) { |
| 425 | return false; |
| 426 | } |
| 427 | } |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | bool TaskProfile::ExecuteForTask(int tid) const { |
| 432 | if (tid == 0) { |
| 433 | tid = GetThreadId(); |
| 434 | } |
| 435 | for (const auto& element : elements_) { |
| 436 | if (!element->ExecuteForTask(tid)) { |
| 437 | return false; |
| 438 | } |
| 439 | } |
| 440 | return true; |
| 441 | } |
| 442 | |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 443 | void TaskProfile::EnableResourceCaching() { |
| 444 | if (res_cached_) { |
| 445 | return; |
| 446 | } |
| 447 | |
| 448 | for (auto& element : elements_) { |
| 449 | element->EnableResourceCaching(); |
| 450 | } |
| 451 | |
| 452 | res_cached_ = true; |
| 453 | } |
| 454 | |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 455 | void TaskProfile::DropResourceCaching() { |
| 456 | if (!res_cached_) { |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | for (auto& element : elements_) { |
| 461 | element->DropResourceCaching(); |
| 462 | } |
| 463 | |
| 464 | res_cached_ = false; |
| 465 | } |
| 466 | |
| 467 | void TaskProfiles::DropResourceCaching() const { |
| 468 | for (auto& iter : profiles_) { |
| 469 | iter.second->DropResourceCaching(); |
| 470 | } |
| 471 | } |
| 472 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 473 | TaskProfiles& TaskProfiles::GetInstance() { |
Peter Collingbourne | dba6d44 | 2019-03-20 21:09:46 -0700 | [diff] [blame] | 474 | // Deliberately leak this object to avoid a race between destruction on |
| 475 | // process exit and concurrent access from another thread. |
| 476 | static auto* instance = new TaskProfiles; |
| 477 | return *instance; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | TaskProfiles::TaskProfiles() { |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 481 | // load system task profiles |
| 482 | if (!Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_FILE)) { |
| 483 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_FILE << " for [" << getpid() << "] failed"; |
| 484 | } |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 485 | |
| 486 | // load API-level specific system task profiles if available |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 487 | 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] | 488 | if (api_level > 0) { |
| 489 | std::string api_profiles_path = |
| 490 | android::base::StringPrintf(TEMPLATE_TASK_PROFILE_API_FILE, api_level); |
| 491 | if (!access(api_profiles_path.c_str(), F_OK) || errno != ENOENT) { |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 492 | if (!Load(CgroupMap::GetInstance(), api_profiles_path)) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame^] | 493 | LOG(ERROR) << "Loading " << api_profiles_path << " for [" << getpid() << "] failed"; |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 494 | } |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 498 | // load vendor task profiles if the file exists |
| 499 | if (!access(TASK_PROFILE_DB_VENDOR_FILE, F_OK) && |
| 500 | !Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_VENDOR_FILE)) { |
| 501 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_VENDOR_FILE << " for [" << getpid() |
| 502 | << "] failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 506 | bool TaskProfiles::Load(const CgroupMap& cg_map, const std::string& file_name) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 507 | std::string json_doc; |
| 508 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 509 | if (!android::base::ReadFileToString(file_name, &json_doc)) { |
| 510 | LOG(ERROR) << "Failed to read task profiles from " << file_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 511 | return false; |
| 512 | } |
| 513 | |
Haibo Huang | d9ac92a | 2021-02-24 17:34:50 -0800 | [diff] [blame] | 514 | Json::CharReaderBuilder builder; |
| 515 | std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 516 | Json::Value root; |
Haibo Huang | d9ac92a | 2021-02-24 17:34:50 -0800 | [diff] [blame] | 517 | std::string errorMessage; |
| 518 | if (!reader->parse(&*json_doc.begin(), &*json_doc.end(), &root, &errorMessage)) { |
| 519 | LOG(ERROR) << "Failed to parse task profiles: " << errorMessage; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 520 | return false; |
| 521 | } |
| 522 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 523 | const Json::Value& attr = root["Attributes"]; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 524 | for (Json::Value::ArrayIndex i = 0; i < attr.size(); ++i) { |
| 525 | std::string name = attr[i]["Name"].asString(); |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 526 | std::string controller_name = attr[i]["Controller"].asString(); |
| 527 | std::string file_attr = attr[i]["File"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 528 | |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 529 | auto controller = cg_map.FindController(controller_name); |
| 530 | if (controller.HasValue()) { |
| 531 | auto iter = attributes_.find(name); |
| 532 | if (iter == attributes_.end()) { |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 533 | attributes_[name] = std::make_unique<ProfileAttribute>(controller, file_attr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 534 | } else { |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 535 | iter->second->Reset(controller, file_attr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 536 | } |
| 537 | } else { |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 538 | LOG(WARNING) << "Controller " << controller_name << " is not found"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 542 | const Json::Value& profiles_val = root["Profiles"]; |
| 543 | for (Json::Value::ArrayIndex i = 0; i < profiles_val.size(); ++i) { |
| 544 | const Json::Value& profile_val = profiles_val[i]; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 545 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 546 | std::string profile_name = profile_val["Name"].asString(); |
| 547 | const Json::Value& actions = profile_val["Actions"]; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 548 | auto profile = std::make_shared<TaskProfile>(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 549 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 550 | for (Json::Value::ArrayIndex act_idx = 0; act_idx < actions.size(); ++act_idx) { |
| 551 | const Json::Value& action_val = actions[act_idx]; |
| 552 | std::string action_name = action_val["Name"].asString(); |
| 553 | const Json::Value& params_val = action_val["Params"]; |
| 554 | if (action_name == "JoinCgroup") { |
| 555 | std::string controller_name = params_val["Controller"].asString(); |
| 556 | std::string path = params_val["Path"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 557 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 558 | auto controller = cg_map.FindController(controller_name); |
| 559 | if (controller.HasValue()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 560 | profile->Add(std::make_unique<SetCgroupAction>(controller, path)); |
| 561 | } else { |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 562 | LOG(WARNING) << "JoinCgroup: controller " << controller_name << " is not found"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 563 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 564 | } else if (action_name == "SetTimerSlack") { |
| 565 | std::string slack_value = params_val["Slack"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 566 | char* end; |
| 567 | unsigned long slack; |
| 568 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 569 | slack = strtoul(slack_value.c_str(), &end, 10); |
| 570 | if (end > slack_value.c_str()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 571 | profile->Add(std::make_unique<SetTimerSlackAction>(slack)); |
| 572 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 573 | LOG(WARNING) << "SetTimerSlack: invalid parameter: " << slack_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 574 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 575 | } else if (action_name == "SetAttribute") { |
| 576 | std::string attr_name = params_val["Name"].asString(); |
| 577 | std::string attr_value = params_val["Value"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 578 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 579 | auto iter = attributes_.find(attr_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 580 | if (iter != attributes_.end()) { |
| 581 | profile->Add( |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 582 | std::make_unique<SetAttributeAction>(iter->second.get(), attr_value)); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 583 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 584 | LOG(WARNING) << "SetAttribute: unknown attribute: " << attr_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 585 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 586 | } else if (action_name == "SetClamps") { |
| 587 | std::string boost_value = params_val["Boost"].asString(); |
| 588 | std::string clamp_value = params_val["Clamp"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 589 | char* end; |
| 590 | unsigned long boost; |
| 591 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 592 | boost = strtoul(boost_value.c_str(), &end, 10); |
| 593 | if (end > boost_value.c_str()) { |
| 594 | unsigned long clamp = strtoul(clamp_value.c_str(), &end, 10); |
| 595 | if (end > clamp_value.c_str()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 596 | profile->Add(std::make_unique<SetClampsAction>(boost, clamp)); |
| 597 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 598 | LOG(WARNING) << "SetClamps: invalid parameter " << clamp_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 599 | } |
| 600 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 601 | LOG(WARNING) << "SetClamps: invalid parameter: " << boost_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 602 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 603 | } else if (action_name == "WriteFile") { |
| 604 | std::string attr_filepath = params_val["FilePath"].asString(); |
| 605 | std::string attr_value = params_val["Value"].asString(); |
| 606 | if (!attr_filepath.empty() && !attr_value.empty()) { |
Rick Yiu | 49fce95 | 2021-04-08 22:10:06 +0800 | [diff] [blame] | 607 | std::string attr_logfailures = params_val["LogFailures"].asString(); |
| 608 | bool logfailures = attr_logfailures.empty() || attr_logfailures == "true"; |
Rick Yiu | d76053a | 2021-01-25 12:44:45 +0800 | [diff] [blame] | 609 | profile->Add(std::make_unique<WriteFileAction>(attr_filepath, attr_value, |
Rick Yiu | 49fce95 | 2021-04-08 22:10:06 +0800 | [diff] [blame] | 610 | logfailures)); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 611 | } else if (attr_filepath.empty()) { |
| 612 | LOG(WARNING) << "WriteFile: invalid parameter: " |
| 613 | << "empty filepath"; |
| 614 | } else if (attr_value.empty()) { |
| 615 | LOG(WARNING) << "WriteFile: invalid parameter: " |
| 616 | << "empty value"; |
| 617 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 618 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 619 | LOG(WARNING) << "Unknown profile action: " << action_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 620 | } |
| 621 | } |
Suren Baghdasaryan | 8438595 | 2020-01-24 16:36:10 -0800 | [diff] [blame] | 622 | auto iter = profiles_.find(profile_name); |
| 623 | if (iter == profiles_.end()) { |
| 624 | profiles_[profile_name] = profile; |
| 625 | } else { |
| 626 | // Move the content rather that replace the profile because old profile might be |
| 627 | // referenced from an aggregate profile if vendor overrides task profiles |
| 628 | profile->MoveTo(iter->second.get()); |
| 629 | profile.reset(); |
| 630 | } |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | const Json::Value& aggregateprofiles_val = root["AggregateProfiles"]; |
| 634 | for (Json::Value::ArrayIndex i = 0; i < aggregateprofiles_val.size(); ++i) { |
| 635 | const Json::Value& aggregateprofile_val = aggregateprofiles_val[i]; |
| 636 | |
| 637 | std::string aggregateprofile_name = aggregateprofile_val["Name"].asString(); |
| 638 | const Json::Value& aggregateprofiles = aggregateprofile_val["Profiles"]; |
| 639 | std::vector<std::shared_ptr<TaskProfile>> profiles; |
| 640 | bool ret = true; |
| 641 | |
| 642 | for (Json::Value::ArrayIndex pf_idx = 0; pf_idx < aggregateprofiles.size(); ++pf_idx) { |
| 643 | std::string profile_name = aggregateprofiles[pf_idx].asString(); |
| 644 | |
| 645 | if (profile_name == aggregateprofile_name) { |
| 646 | LOG(WARNING) << "AggregateProfiles: recursive profile name: " << profile_name; |
| 647 | ret = false; |
| 648 | break; |
| 649 | } else if (profiles_.find(profile_name) == profiles_.end()) { |
| 650 | LOG(WARNING) << "AggregateProfiles: undefined profile name: " << profile_name; |
| 651 | ret = false; |
| 652 | break; |
| 653 | } else { |
| 654 | profiles.push_back(profiles_[profile_name]); |
| 655 | } |
| 656 | } |
| 657 | if (ret) { |
| 658 | auto profile = std::make_shared<TaskProfile>(); |
| 659 | profile->Add(std::make_unique<ApplyProfileAction>(profiles)); |
| 660 | profiles_[aggregateprofile_name] = profile; |
| 661 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | return true; |
| 665 | } |
| 666 | |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 667 | TaskProfile* TaskProfiles::GetProfile(const std::string& name) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 668 | auto iter = profiles_.find(name); |
| 669 | |
| 670 | if (iter != profiles_.end()) { |
| 671 | return iter->second.get(); |
| 672 | } |
| 673 | return nullptr; |
| 674 | } |
| 675 | |
| 676 | const ProfileAttribute* TaskProfiles::GetAttribute(const std::string& name) const { |
| 677 | auto iter = attributes_.find(name); |
| 678 | |
| 679 | if (iter != attributes_.end()) { |
| 680 | return iter->second.get(); |
| 681 | } |
| 682 | return nullptr; |
| 683 | } |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 684 | |
| 685 | bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 686 | const std::vector<std::string>& profiles) { |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 687 | for (const auto& name : profiles) { |
| 688 | TaskProfile* profile = GetProfile(name); |
| 689 | if (profile != nullptr) { |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 690 | if (!profile->ExecuteForProcess(uid, pid)) { |
| 691 | PLOG(WARNING) << "Failed to apply " << name << " process profile"; |
| 692 | } |
| 693 | } else { |
| 694 | PLOG(WARNING) << "Failed to find " << name << "process profile"; |
| 695 | } |
| 696 | } |
| 697 | return true; |
| 698 | } |
| 699 | |
| 700 | bool TaskProfiles::SetTaskProfiles(int tid, const std::vector<std::string>& profiles, |
| 701 | bool use_fd_cache) { |
| 702 | for (const auto& name : profiles) { |
| 703 | TaskProfile* profile = GetProfile(name); |
| 704 | if (profile != nullptr) { |
| 705 | if (use_fd_cache) { |
| 706 | profile->EnableResourceCaching(); |
| 707 | } |
| 708 | if (!profile->ExecuteForTask(tid)) { |
| 709 | PLOG(WARNING) << "Failed to apply " << name << " task profile"; |
| 710 | } |
| 711 | } else { |
| 712 | PLOG(WARNING) << "Failed to find " << name << "task profile"; |
| 713 | } |
| 714 | } |
| 715 | return true; |
| 716 | } |