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