| 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> | 
|  | 26 | #include <android-base/stringprintf.h> | 
|  | 27 | #include <android-base/threads.h> | 
|  | 28 |  | 
|  | 29 | #include <cutils/android_filesystem_config.h> | 
|  | 30 |  | 
|  | 31 | #include <json/reader.h> | 
|  | 32 | #include <json/value.h> | 
|  | 33 |  | 
| Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 34 | // To avoid issues in sdk_mac build | 
|  | 35 | #if defined(__ANDROID__) | 
|  | 36 | #include <sys/prctl.h> | 
|  | 37 | #endif | 
|  | 38 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 39 | using android::base::GetThreadId; | 
|  | 40 | using android::base::StringPrintf; | 
|  | 41 | using android::base::unique_fd; | 
|  | 42 | using android::base::WriteStringToFile; | 
|  | 43 |  | 
|  | 44 | #define TASK_PROFILE_DB_FILE "/etc/task_profiles.json" | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 45 | #define TASK_PROFILE_DB_VENDOR_FILE "/vendor/etc/task_profiles.json" | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 46 |  | 
|  | 47 | bool ProfileAttribute::GetPathForTask(int tid, std::string* path) const { | 
|  | 48 | std::string subgroup; | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 49 | if (!controller()->GetTaskGroup(tid, &subgroup)) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 50 | return false; | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | if (path == nullptr) { | 
|  | 54 | return true; | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | if (subgroup.empty()) { | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 58 | *path = StringPrintf("%s/%s", controller()->path(), file_name_.c_str()); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 59 | } else { | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 60 | *path = StringPrintf("%s/%s/%s", controller()->path(), subgroup.c_str(), | 
|  | 61 | file_name_.c_str()); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 62 | } | 
|  | 63 | return true; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | bool SetClampsAction::ExecuteForProcess(uid_t, pid_t) const { | 
|  | 67 | // TODO: add support when kernel supports util_clamp | 
|  | 68 | LOG(WARNING) << "SetClampsAction::ExecuteForProcess is not supported"; | 
|  | 69 | return false; | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | bool SetClampsAction::ExecuteForTask(int) const { | 
|  | 73 | // TODO: add support when kernel supports util_clamp | 
|  | 74 | LOG(WARNING) << "SetClampsAction::ExecuteForTask is not supported"; | 
|  | 75 | return false; | 
|  | 76 | } | 
|  | 77 |  | 
| Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 78 | // To avoid issues in sdk_mac build | 
|  | 79 | #if defined(__ANDROID__) | 
|  | 80 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 81 | bool SetTimerSlackAction::IsTimerSlackSupported(int tid) { | 
|  | 82 | auto file = StringPrintf("/proc/%d/timerslack_ns", tid); | 
|  | 83 |  | 
|  | 84 | return (access(file.c_str(), W_OK) == 0); | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | bool SetTimerSlackAction::ExecuteForTask(int tid) const { | 
|  | 88 | static bool sys_supports_timerslack = IsTimerSlackSupported(tid); | 
|  | 89 |  | 
|  | 90 | // v4.6+ kernels support the /proc/<tid>/timerslack_ns interface. | 
|  | 91 | // TODO: once we've backported this, log if the open(2) fails. | 
|  | 92 | if (sys_supports_timerslack) { | 
|  | 93 | auto file = StringPrintf("/proc/%d/timerslack_ns", tid); | 
|  | 94 | if (!WriteStringToFile(std::to_string(slack_), file)) { | 
| Suren Baghdasaryan | 2bc5228 | 2019-02-12 17:30:26 -0800 | [diff] [blame] | 95 | if (errno == ENOENT) { | 
|  | 96 | // This happens when process is already dead | 
|  | 97 | return true; | 
|  | 98 | } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 99 | PLOG(ERROR) << "set_timerslack_ns write failed"; | 
|  | 100 | } | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | // TODO: Remove when /proc/<tid>/timerslack_ns interface is backported. | 
|  | 104 | if (tid == 0 || tid == GetThreadId()) { | 
|  | 105 | if (prctl(PR_SET_TIMERSLACK, slack_) == -1) { | 
|  | 106 | PLOG(ERROR) << "set_timerslack_ns prctl failed"; | 
|  | 107 | } | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | return true; | 
|  | 111 | } | 
|  | 112 |  | 
| Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 113 | #endif | 
|  | 114 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 115 | bool SetAttributeAction::ExecuteForProcess(uid_t, pid_t pid) const { | 
|  | 116 | return ExecuteForTask(pid); | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | bool SetAttributeAction::ExecuteForTask(int tid) const { | 
|  | 120 | std::string path; | 
|  | 121 |  | 
|  | 122 | if (!attribute_->GetPathForTask(tid, &path)) { | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 123 | LOG(ERROR) << "Failed to find cgroup for tid " << tid; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 124 | return false; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | if (!WriteStringToFile(value_, path)) { | 
|  | 128 | PLOG(ERROR) << "Failed to write '" << value_ << "' to " << path; | 
|  | 129 | return false; | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | return true; | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | bool SetCgroupAction::IsAppDependentPath(const std::string& path) { | 
|  | 136 | return path.find("<uid>", 0) != std::string::npos || path.find("<pid>", 0) != std::string::npos; | 
|  | 137 | } | 
|  | 138 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 139 | SetCgroupAction::SetCgroupAction(const CgroupController& c, const std::string& p) | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 140 | : controller_(c), path_(p) { | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 141 | // file descriptors for app-dependent paths can't be cached | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 142 | if (IsAppDependentPath(path_)) { | 
|  | 143 | // file descriptor is not cached | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 144 | fd_.reset(FDS_APP_DEPENDENT); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 145 | return; | 
|  | 146 | } | 
|  | 147 |  | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 148 | // file descriptor can be cached later on request | 
|  | 149 | fd_.reset(FDS_NOT_CACHED); | 
|  | 150 | } | 
|  | 151 |  | 
|  | 152 | void SetCgroupAction::EnableResourceCaching() { | 
| mtk16036 | 53f79e6 | 2019-05-31 19:05:22 +0800 | [diff] [blame] | 153 | std::lock_guard<std::mutex> lock(fd_mutex_); | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 154 | if (fd_ != FDS_NOT_CACHED) { | 
|  | 155 | return; | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | std::string tasks_path = controller_.GetTasksFilePath(path_); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 159 |  | 
|  | 160 | if (access(tasks_path.c_str(), W_OK) != 0) { | 
|  | 161 | // file is not accessible | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 162 | fd_.reset(FDS_INACCESSIBLE); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 163 | return; | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | unique_fd fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC))); | 
|  | 167 | if (fd < 0) { | 
|  | 168 | PLOG(ERROR) << "Failed to cache fd '" << tasks_path << "'"; | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 169 | fd_.reset(FDS_INACCESSIBLE); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 170 | return; | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | fd_ = std::move(fd); | 
|  | 174 | } | 
|  | 175 |  | 
| Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 176 | void SetCgroupAction::DropResourceCaching() { | 
|  | 177 | std::lock_guard<std::mutex> lock(fd_mutex_); | 
|  | 178 | if (fd_ == FDS_NOT_CACHED) { | 
|  | 179 | return; | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | fd_.reset(FDS_NOT_CACHED); | 
|  | 183 | } | 
|  | 184 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 185 | bool SetCgroupAction::AddTidToCgroup(int tid, int fd) { | 
|  | 186 | if (tid <= 0) { | 
|  | 187 | return true; | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | std::string value = std::to_string(tid); | 
|  | 191 |  | 
|  | 192 | if (TEMP_FAILURE_RETRY(write(fd, value.c_str(), value.length())) < 0) { | 
|  | 193 | // If the thread is in the process of exiting, don't flag an error | 
|  | 194 | if (errno != ESRCH) { | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 195 | PLOG(ERROR) << "AddTidToCgroup failed to write '" << value << "'; fd=" << fd; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 196 | return false; | 
|  | 197 | } | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 | return true; | 
|  | 201 | } | 
|  | 202 |  | 
|  | 203 | bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const { | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 204 | std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 205 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC))); | 
|  | 206 | if (tmp_fd < 0) { | 
| Elliott Hughes | 08b4d32 | 2019-03-14 20:06:36 -0700 | [diff] [blame] | 207 | PLOG(WARNING) << "Failed to open " << procs_path; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 208 | return false; | 
|  | 209 | } | 
|  | 210 | if (!AddTidToCgroup(pid, tmp_fd)) { | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 211 | LOG(ERROR) << "Failed to add task into cgroup"; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 212 | return false; | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | return true; | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | bool SetCgroupAction::ExecuteForTask(int tid) const { | 
| mtk16036 | 53f79e6 | 2019-05-31 19:05:22 +0800 | [diff] [blame] | 219 | std::lock_guard<std::mutex> lock(fd_mutex_); | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 220 | if (IsFdValid()) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 221 | // fd is cached, reuse it | 
|  | 222 | if (!AddTidToCgroup(tid, fd_)) { | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 223 | LOG(ERROR) << "Failed to add task into cgroup"; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 224 | return false; | 
|  | 225 | } | 
|  | 226 | return true; | 
|  | 227 | } | 
|  | 228 |  | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 229 | if (fd_ == FDS_INACCESSIBLE) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 230 | // no permissions to access the file, ignore | 
|  | 231 | return true; | 
|  | 232 | } | 
|  | 233 |  | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 234 | if (fd_ == FDS_APP_DEPENDENT) { | 
|  | 235 | // application-dependent path can't be used with tid | 
|  | 236 | PLOG(ERROR) << "Application profile can't be applied to a thread"; | 
|  | 237 | return false; | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | // fd was not cached because cached fd can't be used | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 241 | std::string tasks_path = controller()->GetTasksFilePath(path_); | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 242 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC))); | 
|  | 243 | if (tmp_fd < 0) { | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 244 | PLOG(WARNING) << "Failed to open " << tasks_path << ": " << strerror(errno); | 
|  | 245 | return false; | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 246 | } | 
|  | 247 | if (!AddTidToCgroup(tid, tmp_fd)) { | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 248 | LOG(ERROR) << "Failed to add task into cgroup"; | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 249 | return false; | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | return true; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 253 | } | 
|  | 254 |  | 
| Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 255 | bool ApplyProfileAction::ExecuteForProcess(uid_t uid, pid_t pid) const { | 
|  | 256 | for (const auto& profile : profiles_) { | 
| Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 257 | if (!profile->ExecuteForProcess(uid, pid)) { | 
|  | 258 | PLOG(WARNING) << "ExecuteForProcess failed for aggregate profile"; | 
|  | 259 | } | 
|  | 260 | } | 
|  | 261 | return true; | 
|  | 262 | } | 
|  | 263 |  | 
|  | 264 | bool ApplyProfileAction::ExecuteForTask(int tid) const { | 
|  | 265 | for (const auto& profile : profiles_) { | 
| Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 266 | if (!profile->ExecuteForTask(tid)) { | 
|  | 267 | PLOG(WARNING) << "ExecuteForTask failed for aggregate profile"; | 
|  | 268 | } | 
|  | 269 | } | 
|  | 270 | return true; | 
|  | 271 | } | 
|  | 272 |  | 
| Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 273 | void ApplyProfileAction::EnableResourceCaching() { | 
|  | 274 | for (const auto& profile : profiles_) { | 
|  | 275 | profile->EnableResourceCaching(); | 
|  | 276 | } | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | void ApplyProfileAction::DropResourceCaching() { | 
|  | 280 | for (const auto& profile : profiles_) { | 
|  | 281 | profile->DropResourceCaching(); | 
|  | 282 | } | 
|  | 283 | } | 
|  | 284 |  | 
| Suren Baghdasaryan | 8438595 | 2020-01-24 16:36:10 -0800 | [diff] [blame] | 285 | void TaskProfile::MoveTo(TaskProfile* profile) { | 
|  | 286 | profile->elements_ = std::move(elements_); | 
|  | 287 | profile->res_cached_ = res_cached_; | 
|  | 288 | } | 
|  | 289 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 290 | bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const { | 
|  | 291 | for (const auto& element : elements_) { | 
|  | 292 | if (!element->ExecuteForProcess(uid, pid)) { | 
|  | 293 | return false; | 
|  | 294 | } | 
|  | 295 | } | 
|  | 296 | return true; | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | bool TaskProfile::ExecuteForTask(int tid) const { | 
|  | 300 | if (tid == 0) { | 
|  | 301 | tid = GetThreadId(); | 
|  | 302 | } | 
|  | 303 | for (const auto& element : elements_) { | 
|  | 304 | if (!element->ExecuteForTask(tid)) { | 
|  | 305 | return false; | 
|  | 306 | } | 
|  | 307 | } | 
|  | 308 | return true; | 
|  | 309 | } | 
|  | 310 |  | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 311 | void TaskProfile::EnableResourceCaching() { | 
|  | 312 | if (res_cached_) { | 
|  | 313 | return; | 
|  | 314 | } | 
|  | 315 |  | 
|  | 316 | for (auto& element : elements_) { | 
|  | 317 | element->EnableResourceCaching(); | 
|  | 318 | } | 
|  | 319 |  | 
|  | 320 | res_cached_ = true; | 
|  | 321 | } | 
|  | 322 |  | 
| Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 323 | void TaskProfile::DropResourceCaching() { | 
|  | 324 | if (!res_cached_) { | 
|  | 325 | return; | 
|  | 326 | } | 
|  | 327 |  | 
|  | 328 | for (auto& element : elements_) { | 
|  | 329 | element->DropResourceCaching(); | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | res_cached_ = false; | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | void TaskProfiles::DropResourceCaching() const { | 
|  | 336 | for (auto& iter : profiles_) { | 
|  | 337 | iter.second->DropResourceCaching(); | 
|  | 338 | } | 
|  | 339 | } | 
|  | 340 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 341 | TaskProfiles& TaskProfiles::GetInstance() { | 
| Peter Collingbourne | dba6d44 | 2019-03-20 21:09:46 -0700 | [diff] [blame] | 342 | // Deliberately leak this object to avoid a race between destruction on | 
|  | 343 | // process exit and concurrent access from another thread. | 
|  | 344 | static auto* instance = new TaskProfiles; | 
|  | 345 | return *instance; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 346 | } | 
|  | 347 |  | 
|  | 348 | TaskProfiles::TaskProfiles() { | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 349 | // load system task profiles | 
|  | 350 | if (!Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_FILE)) { | 
|  | 351 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_FILE << " for [" << getpid() << "] failed"; | 
|  | 352 | } | 
|  | 353 |  | 
|  | 354 | // load vendor task profiles if the file exists | 
|  | 355 | if (!access(TASK_PROFILE_DB_VENDOR_FILE, F_OK) && | 
|  | 356 | !Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_VENDOR_FILE)) { | 
|  | 357 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_VENDOR_FILE << " for [" << getpid() | 
|  | 358 | << "] failed"; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 359 | } | 
|  | 360 | } | 
|  | 361 |  | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 362 | bool TaskProfiles::Load(const CgroupMap& cg_map, const std::string& file_name) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 363 | std::string json_doc; | 
|  | 364 |  | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 365 | if (!android::base::ReadFileToString(file_name, &json_doc)) { | 
|  | 366 | LOG(ERROR) << "Failed to read task profiles from " << file_name; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 367 | return false; | 
|  | 368 | } | 
|  | 369 |  | 
|  | 370 | Json::Reader reader; | 
|  | 371 | Json::Value root; | 
|  | 372 | if (!reader.parse(json_doc, root)) { | 
|  | 373 | LOG(ERROR) << "Failed to parse task profiles: " << reader.getFormattedErrorMessages(); | 
|  | 374 | return false; | 
|  | 375 | } | 
|  | 376 |  | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 377 | const Json::Value& attr = root["Attributes"]; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 378 | for (Json::Value::ArrayIndex i = 0; i < attr.size(); ++i) { | 
|  | 379 | std::string name = attr[i]["Name"].asString(); | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 380 | std::string controller_name = attr[i]["Controller"].asString(); | 
|  | 381 | std::string file_attr = attr[i]["File"].asString(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 382 |  | 
|  | 383 | if (attributes_.find(name) == attributes_.end()) { | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 384 | auto controller = cg_map.FindController(controller_name); | 
|  | 385 | if (controller.HasValue()) { | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 386 | attributes_[name] = std::make_unique<ProfileAttribute>(controller, file_attr); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 387 | } else { | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 388 | LOG(WARNING) << "Controller " << controller_name << " is not found"; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 389 | } | 
|  | 390 | } else { | 
|  | 391 | LOG(WARNING) << "Attribute " << name << " is already defined"; | 
|  | 392 | } | 
|  | 393 | } | 
|  | 394 |  | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 395 | const Json::Value& profiles_val = root["Profiles"]; | 
|  | 396 | for (Json::Value::ArrayIndex i = 0; i < profiles_val.size(); ++i) { | 
|  | 397 | const Json::Value& profile_val = profiles_val[i]; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 398 |  | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 399 | std::string profile_name = profile_val["Name"].asString(); | 
|  | 400 | const Json::Value& actions = profile_val["Actions"]; | 
| Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 401 | auto profile = std::make_shared<TaskProfile>(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 402 |  | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 403 | for (Json::Value::ArrayIndex act_idx = 0; act_idx < actions.size(); ++act_idx) { | 
|  | 404 | const Json::Value& action_val = actions[act_idx]; | 
|  | 405 | std::string action_name = action_val["Name"].asString(); | 
|  | 406 | const Json::Value& params_val = action_val["Params"]; | 
|  | 407 | if (action_name == "JoinCgroup") { | 
|  | 408 | std::string controller_name = params_val["Controller"].asString(); | 
|  | 409 | std::string path = params_val["Path"].asString(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 410 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 411 | auto controller = cg_map.FindController(controller_name); | 
|  | 412 | if (controller.HasValue()) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 413 | profile->Add(std::make_unique<SetCgroupAction>(controller, path)); | 
|  | 414 | } else { | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 415 | LOG(WARNING) << "JoinCgroup: controller " << controller_name << " is not found"; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 416 | } | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 417 | } else if (action_name == "SetTimerSlack") { | 
|  | 418 | std::string slack_value = params_val["Slack"].asString(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 419 | char* end; | 
|  | 420 | unsigned long slack; | 
|  | 421 |  | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 422 | slack = strtoul(slack_value.c_str(), &end, 10); | 
|  | 423 | if (end > slack_value.c_str()) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 424 | profile->Add(std::make_unique<SetTimerSlackAction>(slack)); | 
|  | 425 | } else { | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 426 | LOG(WARNING) << "SetTimerSlack: invalid parameter: " << slack_value; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 427 | } | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 428 | } else if (action_name == "SetAttribute") { | 
|  | 429 | std::string attr_name = params_val["Name"].asString(); | 
|  | 430 | std::string attr_value = params_val["Value"].asString(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 431 |  | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 432 | auto iter = attributes_.find(attr_name); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 433 | if (iter != attributes_.end()) { | 
|  | 434 | profile->Add( | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 435 | std::make_unique<SetAttributeAction>(iter->second.get(), attr_value)); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 436 | } else { | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 437 | LOG(WARNING) << "SetAttribute: unknown attribute: " << attr_name; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 438 | } | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 439 | } else if (action_name == "SetClamps") { | 
|  | 440 | std::string boost_value = params_val["Boost"].asString(); | 
|  | 441 | std::string clamp_value = params_val["Clamp"].asString(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 442 | char* end; | 
|  | 443 | unsigned long boost; | 
|  | 444 |  | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 445 | boost = strtoul(boost_value.c_str(), &end, 10); | 
|  | 446 | if (end > boost_value.c_str()) { | 
|  | 447 | unsigned long clamp = strtoul(clamp_value.c_str(), &end, 10); | 
|  | 448 | if (end > clamp_value.c_str()) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 449 | profile->Add(std::make_unique<SetClampsAction>(boost, clamp)); | 
|  | 450 | } else { | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 451 | LOG(WARNING) << "SetClamps: invalid parameter " << clamp_value; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 452 | } | 
|  | 453 | } else { | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 454 | LOG(WARNING) << "SetClamps: invalid parameter: " << boost_value; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 455 | } | 
|  | 456 | } else { | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 457 | LOG(WARNING) << "Unknown profile action: " << action_name; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 458 | } | 
|  | 459 | } | 
| Suren Baghdasaryan | 8438595 | 2020-01-24 16:36:10 -0800 | [diff] [blame] | 460 | auto iter = profiles_.find(profile_name); | 
|  | 461 | if (iter == profiles_.end()) { | 
|  | 462 | profiles_[profile_name] = profile; | 
|  | 463 | } else { | 
|  | 464 | // Move the content rather that replace the profile because old profile might be | 
|  | 465 | // referenced from an aggregate profile if vendor overrides task profiles | 
|  | 466 | profile->MoveTo(iter->second.get()); | 
|  | 467 | profile.reset(); | 
|  | 468 | } | 
| Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 469 | } | 
|  | 470 |  | 
|  | 471 | const Json::Value& aggregateprofiles_val = root["AggregateProfiles"]; | 
|  | 472 | for (Json::Value::ArrayIndex i = 0; i < aggregateprofiles_val.size(); ++i) { | 
|  | 473 | const Json::Value& aggregateprofile_val = aggregateprofiles_val[i]; | 
|  | 474 |  | 
|  | 475 | std::string aggregateprofile_name = aggregateprofile_val["Name"].asString(); | 
|  | 476 | const Json::Value& aggregateprofiles = aggregateprofile_val["Profiles"]; | 
|  | 477 | std::vector<std::shared_ptr<TaskProfile>> profiles; | 
|  | 478 | bool ret = true; | 
|  | 479 |  | 
|  | 480 | for (Json::Value::ArrayIndex pf_idx = 0; pf_idx < aggregateprofiles.size(); ++pf_idx) { | 
|  | 481 | std::string profile_name = aggregateprofiles[pf_idx].asString(); | 
|  | 482 |  | 
|  | 483 | if (profile_name == aggregateprofile_name) { | 
|  | 484 | LOG(WARNING) << "AggregateProfiles: recursive profile name: " << profile_name; | 
|  | 485 | ret = false; | 
|  | 486 | break; | 
|  | 487 | } else if (profiles_.find(profile_name) == profiles_.end()) { | 
|  | 488 | LOG(WARNING) << "AggregateProfiles: undefined profile name: " << profile_name; | 
|  | 489 | ret = false; | 
|  | 490 | break; | 
|  | 491 | } else { | 
|  | 492 | profiles.push_back(profiles_[profile_name]); | 
|  | 493 | } | 
|  | 494 | } | 
|  | 495 | if (ret) { | 
|  | 496 | auto profile = std::make_shared<TaskProfile>(); | 
|  | 497 | profile->Add(std::make_unique<ApplyProfileAction>(profiles)); | 
|  | 498 | profiles_[aggregateprofile_name] = profile; | 
|  | 499 | } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 500 | } | 
|  | 501 |  | 
|  | 502 | return true; | 
|  | 503 | } | 
|  | 504 |  | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 505 | TaskProfile* TaskProfiles::GetProfile(const std::string& name) const { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 506 | auto iter = profiles_.find(name); | 
|  | 507 |  | 
|  | 508 | if (iter != profiles_.end()) { | 
|  | 509 | return iter->second.get(); | 
|  | 510 | } | 
|  | 511 | return nullptr; | 
|  | 512 | } | 
|  | 513 |  | 
|  | 514 | const ProfileAttribute* TaskProfiles::GetAttribute(const std::string& name) const { | 
|  | 515 | auto iter = attributes_.find(name); | 
|  | 516 |  | 
|  | 517 | if (iter != attributes_.end()) { | 
|  | 518 | return iter->second.get(); | 
|  | 519 | } | 
|  | 520 | return nullptr; | 
|  | 521 | } | 
| Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 522 |  | 
|  | 523 | bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, | 
| Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 524 | const std::vector<std::string>& profiles) { | 
| Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 525 | for (const auto& name : profiles) { | 
|  | 526 | TaskProfile* profile = GetProfile(name); | 
|  | 527 | if (profile != nullptr) { | 
| Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 528 | if (!profile->ExecuteForProcess(uid, pid)) { | 
|  | 529 | PLOG(WARNING) << "Failed to apply " << name << " process profile"; | 
|  | 530 | } | 
|  | 531 | } else { | 
|  | 532 | PLOG(WARNING) << "Failed to find " << name << "process profile"; | 
|  | 533 | } | 
|  | 534 | } | 
|  | 535 | return true; | 
|  | 536 | } | 
|  | 537 |  | 
|  | 538 | bool TaskProfiles::SetTaskProfiles(int tid, const std::vector<std::string>& profiles, | 
|  | 539 | bool use_fd_cache) { | 
|  | 540 | for (const auto& name : profiles) { | 
|  | 541 | TaskProfile* profile = GetProfile(name); | 
|  | 542 | if (profile != nullptr) { | 
|  | 543 | if (use_fd_cache) { | 
|  | 544 | profile->EnableResourceCaching(); | 
|  | 545 | } | 
|  | 546 | if (!profile->ExecuteForTask(tid)) { | 
|  | 547 | PLOG(WARNING) << "Failed to apply " << name << " task profile"; | 
|  | 548 | } | 
|  | 549 | } else { | 
|  | 550 | PLOG(WARNING) << "Failed to find " << name << "task profile"; | 
|  | 551 | } | 
|  | 552 | } | 
|  | 553 | return true; | 
|  | 554 | } |