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