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 | |
| 147 | bool SetCgroupAction::IsAppDependentPath(const std::string& path) { |
| 148 | return path.find("<uid>", 0) != std::string::npos || path.find("<pid>", 0) != std::string::npos; |
| 149 | } |
| 150 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 151 | SetCgroupAction::SetCgroupAction(const CgroupController& c, const std::string& p) |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 152 | : controller_(c), path_(p) { |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 153 | // file descriptors for app-dependent paths can't be cached |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 154 | if (IsAppDependentPath(path_)) { |
| 155 | // file descriptor is not cached |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 156 | fd_.reset(FDS_APP_DEPENDENT); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 160 | // file descriptor can be cached later on request |
| 161 | fd_.reset(FDS_NOT_CACHED); |
| 162 | } |
| 163 | |
| 164 | void SetCgroupAction::EnableResourceCaching() { |
mtk16036 | 53f79e6 | 2019-05-31 19:05:22 +0800 | [diff] [blame] | 165 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 166 | if (fd_ != FDS_NOT_CACHED) { |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | std::string tasks_path = controller_.GetTasksFilePath(path_); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 171 | |
| 172 | if (access(tasks_path.c_str(), W_OK) != 0) { |
| 173 | // file is not accessible |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 174 | fd_.reset(FDS_INACCESSIBLE); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
| 178 | unique_fd fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC))); |
| 179 | if (fd < 0) { |
| 180 | PLOG(ERROR) << "Failed to cache fd '" << tasks_path << "'"; |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 181 | fd_.reset(FDS_INACCESSIBLE); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 182 | return; |
| 183 | } |
| 184 | |
| 185 | fd_ = std::move(fd); |
| 186 | } |
| 187 | |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 188 | void SetCgroupAction::DropResourceCaching() { |
| 189 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 190 | if (fd_ == FDS_NOT_CACHED) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | fd_.reset(FDS_NOT_CACHED); |
| 195 | } |
| 196 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 197 | bool SetCgroupAction::AddTidToCgroup(int tid, int fd) { |
| 198 | if (tid <= 0) { |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | std::string value = std::to_string(tid); |
| 203 | |
| 204 | if (TEMP_FAILURE_RETRY(write(fd, value.c_str(), value.length())) < 0) { |
| 205 | // If the thread is in the process of exiting, don't flag an error |
| 206 | if (errno != ESRCH) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 207 | PLOG(ERROR) << "AddTidToCgroup failed to write '" << value << "'; fd=" << fd; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 208 | return false; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 216 | std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 217 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC))); |
| 218 | if (tmp_fd < 0) { |
Elliott Hughes | 08b4d32 | 2019-03-14 20:06:36 -0700 | [diff] [blame] | 219 | PLOG(WARNING) << "Failed to open " << procs_path; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 220 | return false; |
| 221 | } |
| 222 | if (!AddTidToCgroup(pid, tmp_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 | |
| 227 | return true; |
| 228 | } |
| 229 | |
| 230 | bool SetCgroupAction::ExecuteForTask(int tid) const { |
mtk16036 | 53f79e6 | 2019-05-31 19:05:22 +0800 | [diff] [blame] | 231 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 232 | if (IsFdValid()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 233 | // fd is cached, reuse it |
| 234 | if (!AddTidToCgroup(tid, fd_)) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 235 | LOG(ERROR) << "Failed to add task into cgroup"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 236 | return false; |
| 237 | } |
| 238 | return true; |
| 239 | } |
| 240 | |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 241 | if (fd_ == FDS_INACCESSIBLE) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 242 | // no permissions to access the file, ignore |
| 243 | return true; |
| 244 | } |
| 245 | |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 246 | if (fd_ == FDS_APP_DEPENDENT) { |
| 247 | // application-dependent path can't be used with tid |
| 248 | PLOG(ERROR) << "Application profile can't be applied to a thread"; |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | // fd was not cached because cached fd can't be used |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 253 | std::string tasks_path = controller()->GetTasksFilePath(path_); |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 254 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC))); |
| 255 | if (tmp_fd < 0) { |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 256 | PLOG(WARNING) << "Failed to open " << tasks_path << ": " << strerror(errno); |
| 257 | return false; |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 258 | } |
| 259 | if (!AddTidToCgroup(tid, tmp_fd)) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 260 | LOG(ERROR) << "Failed to add task into cgroup"; |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
| 264 | return true; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 265 | } |
| 266 | |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 267 | bool WriteFileAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
| 268 | std::string filepath(filepath_), value(value_); |
| 269 | |
| 270 | filepath = StringReplace(filepath, "<uid>", std::to_string(uid), true); |
| 271 | filepath = StringReplace(filepath, "<pid>", std::to_string(pid), true); |
| 272 | value = StringReplace(value, "<uid>", std::to_string(uid), true); |
| 273 | value = StringReplace(value, "<pid>", std::to_string(pid), true); |
| 274 | |
| 275 | if (!WriteStringToFile(value, filepath)) { |
Rick Yiu | d76053a | 2021-01-25 12:44:45 +0800 | [diff] [blame] | 276 | if (logfailures_) PLOG(ERROR) << "Failed to write '" << value << "' to " << filepath; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | bool WriteFileAction::ExecuteForTask(int tid) const { |
| 284 | std::string filepath(filepath_), value(value_); |
| 285 | int uid = getuid(); |
| 286 | |
| 287 | filepath = StringReplace(filepath, "<uid>", std::to_string(uid), true); |
| 288 | filepath = StringReplace(filepath, "<pid>", std::to_string(tid), true); |
| 289 | value = StringReplace(value, "<uid>", std::to_string(uid), true); |
| 290 | value = StringReplace(value, "<pid>", std::to_string(tid), true); |
| 291 | |
| 292 | if (!WriteStringToFile(value, filepath)) { |
Rick Yiu | d76053a | 2021-01-25 12:44:45 +0800 | [diff] [blame] | 293 | if (logfailures_) PLOG(ERROR) << "Failed to write '" << value << "' to " << filepath; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 294 | return false; |
| 295 | } |
| 296 | |
| 297 | return true; |
| 298 | } |
| 299 | |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 300 | bool ApplyProfileAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
| 301 | for (const auto& profile : profiles_) { |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 302 | if (!profile->ExecuteForProcess(uid, pid)) { |
| 303 | PLOG(WARNING) << "ExecuteForProcess failed for aggregate profile"; |
| 304 | } |
| 305 | } |
| 306 | return true; |
| 307 | } |
| 308 | |
| 309 | bool ApplyProfileAction::ExecuteForTask(int tid) const { |
| 310 | for (const auto& profile : profiles_) { |
Wei Wang | 8722e4d | 2021-05-14 12:34:54 -0700 | [diff] [blame^] | 311 | profile->ExecuteForTask(tid); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 312 | } |
| 313 | return true; |
| 314 | } |
| 315 | |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 316 | void ApplyProfileAction::EnableResourceCaching() { |
| 317 | for (const auto& profile : profiles_) { |
| 318 | profile->EnableResourceCaching(); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | void ApplyProfileAction::DropResourceCaching() { |
| 323 | for (const auto& profile : profiles_) { |
| 324 | profile->DropResourceCaching(); |
| 325 | } |
| 326 | } |
| 327 | |
Suren Baghdasaryan | 8438595 | 2020-01-24 16:36:10 -0800 | [diff] [blame] | 328 | void TaskProfile::MoveTo(TaskProfile* profile) { |
| 329 | profile->elements_ = std::move(elements_); |
| 330 | profile->res_cached_ = res_cached_; |
| 331 | } |
| 332 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 333 | bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const { |
| 334 | for (const auto& element : elements_) { |
| 335 | if (!element->ExecuteForProcess(uid, pid)) { |
| 336 | return false; |
| 337 | } |
| 338 | } |
| 339 | return true; |
| 340 | } |
| 341 | |
| 342 | bool TaskProfile::ExecuteForTask(int tid) const { |
| 343 | if (tid == 0) { |
| 344 | tid = GetThreadId(); |
| 345 | } |
| 346 | for (const auto& element : elements_) { |
| 347 | if (!element->ExecuteForTask(tid)) { |
| 348 | return false; |
| 349 | } |
| 350 | } |
| 351 | return true; |
| 352 | } |
| 353 | |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 354 | void TaskProfile::EnableResourceCaching() { |
| 355 | if (res_cached_) { |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | for (auto& element : elements_) { |
| 360 | element->EnableResourceCaching(); |
| 361 | } |
| 362 | |
| 363 | res_cached_ = true; |
| 364 | } |
| 365 | |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 366 | void TaskProfile::DropResourceCaching() { |
| 367 | if (!res_cached_) { |
| 368 | return; |
| 369 | } |
| 370 | |
| 371 | for (auto& element : elements_) { |
| 372 | element->DropResourceCaching(); |
| 373 | } |
| 374 | |
| 375 | res_cached_ = false; |
| 376 | } |
| 377 | |
| 378 | void TaskProfiles::DropResourceCaching() const { |
| 379 | for (auto& iter : profiles_) { |
| 380 | iter.second->DropResourceCaching(); |
| 381 | } |
| 382 | } |
| 383 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 384 | TaskProfiles& TaskProfiles::GetInstance() { |
Peter Collingbourne | dba6d44 | 2019-03-20 21:09:46 -0700 | [diff] [blame] | 385 | // Deliberately leak this object to avoid a race between destruction on |
| 386 | // process exit and concurrent access from another thread. |
| 387 | static auto* instance = new TaskProfiles; |
| 388 | return *instance; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | TaskProfiles::TaskProfiles() { |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 392 | // load system task profiles |
| 393 | if (!Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_FILE)) { |
| 394 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_FILE << " for [" << getpid() << "] failed"; |
| 395 | } |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 396 | |
| 397 | // load API-level specific system task profiles if available |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 398 | 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] | 399 | if (api_level > 0) { |
| 400 | std::string api_profiles_path = |
| 401 | android::base::StringPrintf(TEMPLATE_TASK_PROFILE_API_FILE, api_level); |
| 402 | if (!access(api_profiles_path.c_str(), F_OK) || errno != ENOENT) { |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 403 | if (!Load(CgroupMap::GetInstance(), api_profiles_path)) { |
| 404 | LOG(ERROR) << "Loading " << api_profiles_path << " for [" << getpid() |
| 405 | << "] failed"; |
| 406 | } |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 410 | // load vendor task profiles if the file exists |
| 411 | if (!access(TASK_PROFILE_DB_VENDOR_FILE, F_OK) && |
| 412 | !Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_VENDOR_FILE)) { |
| 413 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_VENDOR_FILE << " for [" << getpid() |
| 414 | << "] failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 418 | bool TaskProfiles::Load(const CgroupMap& cg_map, const std::string& file_name) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 419 | std::string json_doc; |
| 420 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 421 | if (!android::base::ReadFileToString(file_name, &json_doc)) { |
| 422 | LOG(ERROR) << "Failed to read task profiles from " << file_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 423 | return false; |
| 424 | } |
| 425 | |
Haibo Huang | d9ac92a | 2021-02-24 17:34:50 -0800 | [diff] [blame] | 426 | Json::CharReaderBuilder builder; |
| 427 | std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 428 | Json::Value root; |
Haibo Huang | d9ac92a | 2021-02-24 17:34:50 -0800 | [diff] [blame] | 429 | std::string errorMessage; |
| 430 | if (!reader->parse(&*json_doc.begin(), &*json_doc.end(), &root, &errorMessage)) { |
| 431 | LOG(ERROR) << "Failed to parse task profiles: " << errorMessage; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 432 | return false; |
| 433 | } |
| 434 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 435 | const Json::Value& attr = root["Attributes"]; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 436 | for (Json::Value::ArrayIndex i = 0; i < attr.size(); ++i) { |
| 437 | std::string name = attr[i]["Name"].asString(); |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 438 | std::string controller_name = attr[i]["Controller"].asString(); |
| 439 | std::string file_attr = attr[i]["File"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 440 | |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 441 | auto controller = cg_map.FindController(controller_name); |
| 442 | if (controller.HasValue()) { |
| 443 | auto iter = attributes_.find(name); |
| 444 | if (iter == attributes_.end()) { |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 445 | attributes_[name] = std::make_unique<ProfileAttribute>(controller, file_attr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 446 | } else { |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 447 | iter->second->Reset(controller, file_attr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 448 | } |
| 449 | } else { |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 450 | LOG(WARNING) << "Controller " << controller_name << " is not found"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 454 | const Json::Value& profiles_val = root["Profiles"]; |
| 455 | for (Json::Value::ArrayIndex i = 0; i < profiles_val.size(); ++i) { |
| 456 | const Json::Value& profile_val = profiles_val[i]; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 457 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 458 | std::string profile_name = profile_val["Name"].asString(); |
| 459 | const Json::Value& actions = profile_val["Actions"]; |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 460 | auto profile = std::make_shared<TaskProfile>(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 461 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 462 | for (Json::Value::ArrayIndex act_idx = 0; act_idx < actions.size(); ++act_idx) { |
| 463 | const Json::Value& action_val = actions[act_idx]; |
| 464 | std::string action_name = action_val["Name"].asString(); |
| 465 | const Json::Value& params_val = action_val["Params"]; |
| 466 | if (action_name == "JoinCgroup") { |
| 467 | std::string controller_name = params_val["Controller"].asString(); |
| 468 | std::string path = params_val["Path"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 469 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 470 | auto controller = cg_map.FindController(controller_name); |
| 471 | if (controller.HasValue()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 472 | profile->Add(std::make_unique<SetCgroupAction>(controller, path)); |
| 473 | } else { |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 474 | LOG(WARNING) << "JoinCgroup: controller " << controller_name << " is not found"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 475 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 476 | } else if (action_name == "SetTimerSlack") { |
| 477 | std::string slack_value = params_val["Slack"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 478 | char* end; |
| 479 | unsigned long slack; |
| 480 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 481 | slack = strtoul(slack_value.c_str(), &end, 10); |
| 482 | if (end > slack_value.c_str()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 483 | profile->Add(std::make_unique<SetTimerSlackAction>(slack)); |
| 484 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 485 | LOG(WARNING) << "SetTimerSlack: invalid parameter: " << slack_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 486 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 487 | } else if (action_name == "SetAttribute") { |
| 488 | std::string attr_name = params_val["Name"].asString(); |
| 489 | std::string attr_value = params_val["Value"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 490 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 491 | auto iter = attributes_.find(attr_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 492 | if (iter != attributes_.end()) { |
| 493 | profile->Add( |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 494 | std::make_unique<SetAttributeAction>(iter->second.get(), attr_value)); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 495 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 496 | LOG(WARNING) << "SetAttribute: unknown attribute: " << attr_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 497 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 498 | } else if (action_name == "SetClamps") { |
| 499 | std::string boost_value = params_val["Boost"].asString(); |
| 500 | std::string clamp_value = params_val["Clamp"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 501 | char* end; |
| 502 | unsigned long boost; |
| 503 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 504 | boost = strtoul(boost_value.c_str(), &end, 10); |
| 505 | if (end > boost_value.c_str()) { |
| 506 | unsigned long clamp = strtoul(clamp_value.c_str(), &end, 10); |
| 507 | if (end > clamp_value.c_str()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 508 | profile->Add(std::make_unique<SetClampsAction>(boost, clamp)); |
| 509 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 510 | LOG(WARNING) << "SetClamps: invalid parameter " << clamp_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 511 | } |
| 512 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 513 | LOG(WARNING) << "SetClamps: invalid parameter: " << boost_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 514 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 515 | } else if (action_name == "WriteFile") { |
| 516 | std::string attr_filepath = params_val["FilePath"].asString(); |
| 517 | std::string attr_value = params_val["Value"].asString(); |
| 518 | if (!attr_filepath.empty() && !attr_value.empty()) { |
Rick Yiu | d76053a | 2021-01-25 12:44:45 +0800 | [diff] [blame] | 519 | const Json::Value& logfailures = params_val["LogFailures"]; |
| 520 | bool attr_logfailures = logfailures.isNull() || logfailures.asBool(); |
| 521 | profile->Add(std::make_unique<WriteFileAction>(attr_filepath, attr_value, |
| 522 | attr_logfailures)); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 523 | } else if (attr_filepath.empty()) { |
| 524 | LOG(WARNING) << "WriteFile: invalid parameter: " |
| 525 | << "empty filepath"; |
| 526 | } else if (attr_value.empty()) { |
| 527 | LOG(WARNING) << "WriteFile: invalid parameter: " |
| 528 | << "empty value"; |
| 529 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 530 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 531 | LOG(WARNING) << "Unknown profile action: " << action_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 532 | } |
| 533 | } |
Suren Baghdasaryan | 8438595 | 2020-01-24 16:36:10 -0800 | [diff] [blame] | 534 | auto iter = profiles_.find(profile_name); |
| 535 | if (iter == profiles_.end()) { |
| 536 | profiles_[profile_name] = profile; |
| 537 | } else { |
| 538 | // Move the content rather that replace the profile because old profile might be |
| 539 | // referenced from an aggregate profile if vendor overrides task profiles |
| 540 | profile->MoveTo(iter->second.get()); |
| 541 | profile.reset(); |
| 542 | } |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | const Json::Value& aggregateprofiles_val = root["AggregateProfiles"]; |
| 546 | for (Json::Value::ArrayIndex i = 0; i < aggregateprofiles_val.size(); ++i) { |
| 547 | const Json::Value& aggregateprofile_val = aggregateprofiles_val[i]; |
| 548 | |
| 549 | std::string aggregateprofile_name = aggregateprofile_val["Name"].asString(); |
| 550 | const Json::Value& aggregateprofiles = aggregateprofile_val["Profiles"]; |
| 551 | std::vector<std::shared_ptr<TaskProfile>> profiles; |
| 552 | bool ret = true; |
| 553 | |
| 554 | for (Json::Value::ArrayIndex pf_idx = 0; pf_idx < aggregateprofiles.size(); ++pf_idx) { |
| 555 | std::string profile_name = aggregateprofiles[pf_idx].asString(); |
| 556 | |
| 557 | if (profile_name == aggregateprofile_name) { |
| 558 | LOG(WARNING) << "AggregateProfiles: recursive profile name: " << profile_name; |
| 559 | ret = false; |
| 560 | break; |
| 561 | } else if (profiles_.find(profile_name) == profiles_.end()) { |
| 562 | LOG(WARNING) << "AggregateProfiles: undefined profile name: " << profile_name; |
| 563 | ret = false; |
| 564 | break; |
| 565 | } else { |
| 566 | profiles.push_back(profiles_[profile_name]); |
| 567 | } |
| 568 | } |
| 569 | if (ret) { |
| 570 | auto profile = std::make_shared<TaskProfile>(); |
| 571 | profile->Add(std::make_unique<ApplyProfileAction>(profiles)); |
| 572 | profiles_[aggregateprofile_name] = profile; |
| 573 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | return true; |
| 577 | } |
| 578 | |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 579 | TaskProfile* TaskProfiles::GetProfile(const std::string& name) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 580 | auto iter = profiles_.find(name); |
| 581 | |
| 582 | if (iter != profiles_.end()) { |
| 583 | return iter->second.get(); |
| 584 | } |
| 585 | return nullptr; |
| 586 | } |
| 587 | |
| 588 | const ProfileAttribute* TaskProfiles::GetAttribute(const std::string& name) const { |
| 589 | auto iter = attributes_.find(name); |
| 590 | |
| 591 | if (iter != attributes_.end()) { |
| 592 | return iter->second.get(); |
| 593 | } |
| 594 | return nullptr; |
| 595 | } |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 596 | |
| 597 | bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 598 | const std::vector<std::string>& profiles) { |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 599 | for (const auto& name : profiles) { |
| 600 | TaskProfile* profile = GetProfile(name); |
| 601 | if (profile != nullptr) { |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 602 | if (!profile->ExecuteForProcess(uid, pid)) { |
| 603 | PLOG(WARNING) << "Failed to apply " << name << " process profile"; |
| 604 | } |
| 605 | } else { |
| 606 | PLOG(WARNING) << "Failed to find " << name << "process profile"; |
| 607 | } |
| 608 | } |
| 609 | return true; |
| 610 | } |
| 611 | |
| 612 | bool TaskProfiles::SetTaskProfiles(int tid, const std::vector<std::string>& profiles, |
| 613 | bool use_fd_cache) { |
| 614 | for (const auto& name : profiles) { |
| 615 | TaskProfile* profile = GetProfile(name); |
| 616 | if (profile != nullptr) { |
| 617 | if (use_fd_cache) { |
| 618 | profile->EnableResourceCaching(); |
| 619 | } |
| 620 | if (!profile->ExecuteForTask(tid)) { |
| 621 | PLOG(WARNING) << "Failed to apply " << name << " task profile"; |
| 622 | } |
| 623 | } else { |
| 624 | PLOG(WARNING) << "Failed to find " << name << "task profile"; |
| 625 | } |
| 626 | } |
| 627 | return true; |
| 628 | } |