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)) { |
Bart Van Assche | 59af680 | 2022-01-24 21:08:57 +0000 | [diff] [blame^] | 209 | if (errno == ENOENT) { |
| 210 | if (optional_) { |
| 211 | return true; |
| 212 | } else { |
| 213 | LOG(ERROR) << "No such cgroup attribute: " << path; |
| 214 | return false; |
| 215 | } |
| 216 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 217 | PLOG(ERROR) << "Failed to write '" << value_ << "' to " << path; |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | return true; |
| 222 | } |
| 223 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 224 | SetCgroupAction::SetCgroupAction(const CgroupController& c, const std::string& p) |
| 225 | : controller_(c), path_(p) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 226 | FdCacheHelper::Init(controller_.GetTasksFilePath(path_), fd_[ProfileAction::RCT_TASK]); |
| 227 | // uid and pid don't matter because IsAppDependentPath ensures the path doesn't use them |
| 228 | FdCacheHelper::Init(controller_.GetProcsFilePath(path_, 0, 0), fd_[ProfileAction::RCT_PROCESS]); |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 229 | } |
| 230 | |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 231 | bool SetCgroupAction::AddTidToCgroup(int tid, int fd, const char* controller_name) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 232 | if (tid <= 0) { |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | std::string value = std::to_string(tid); |
| 237 | |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 238 | if (TEMP_FAILURE_RETRY(write(fd, value.c_str(), value.length())) == value.length()) { |
| 239 | return true; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 242 | // If the thread is in the process of exiting, don't flag an error |
| 243 | if (errno == ESRCH) { |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | // ENOSPC is returned when cpuset cgroup that we are joining has no online cpus |
| 248 | if (errno == ENOSPC && !strcmp(controller_name, "cpuset")) { |
| 249 | // This is an abnormal case happening only in testing, so report it only once |
| 250 | static bool empty_cpuset_reported = false; |
| 251 | |
| 252 | if (empty_cpuset_reported) { |
| 253 | return true; |
| 254 | } |
| 255 | |
| 256 | LOG(ERROR) << "Failed to add task '" << value |
| 257 | << "' into cpuset because all cpus in that cpuset are offline"; |
| 258 | empty_cpuset_reported = true; |
| 259 | } else { |
| 260 | PLOG(ERROR) << "AddTidToCgroup failed to write '" << value << "'; fd=" << fd; |
| 261 | } |
| 262 | |
| 263 | return false; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 266 | ProfileAction::CacheUseResult SetCgroupAction::UseCachedFd(ResourceCacheType cache_type, |
| 267 | int id) const { |
| 268 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 269 | if (FdCacheHelper::IsCached(fd_[cache_type])) { |
| 270 | // fd is cached, reuse it |
| 271 | if (!AddTidToCgroup(id, fd_[cache_type], controller()->name())) { |
| 272 | LOG(ERROR) << "Failed to add task into cgroup"; |
| 273 | return ProfileAction::FAIL; |
| 274 | } |
| 275 | return ProfileAction::SUCCESS; |
| 276 | } |
| 277 | |
| 278 | if (fd_[cache_type] == FdCacheHelper::FDS_INACCESSIBLE) { |
| 279 | // no permissions to access the file, ignore |
| 280 | return ProfileAction::SUCCESS; |
| 281 | } |
| 282 | |
| 283 | if (cache_type == ResourceCacheType::RCT_TASK && |
| 284 | fd_[cache_type] == FdCacheHelper::FDS_APP_DEPENDENT) { |
| 285 | // application-dependent path can't be used with tid |
| 286 | PLOG(ERROR) << "Application profile can't be applied to a thread"; |
| 287 | return ProfileAction::FAIL; |
| 288 | } |
| 289 | |
| 290 | return ProfileAction::UNUSED; |
| 291 | } |
| 292 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 293 | bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 294 | CacheUseResult result = UseCachedFd(ProfileAction::RCT_PROCESS, pid); |
| 295 | if (result != ProfileAction::UNUSED) { |
| 296 | return result == ProfileAction::SUCCESS; |
| 297 | } |
| 298 | |
| 299 | // fd was not cached or cached fd can't be used |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 300 | std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 301 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC))); |
| 302 | if (tmp_fd < 0) { |
Elliott Hughes | 08b4d32 | 2019-03-14 20:06:36 -0700 | [diff] [blame] | 303 | PLOG(WARNING) << "Failed to open " << procs_path; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 304 | return false; |
| 305 | } |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 306 | if (!AddTidToCgroup(pid, tmp_fd, controller()->name())) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 307 | LOG(ERROR) << "Failed to add task into cgroup"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 308 | return false; |
| 309 | } |
| 310 | |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | bool SetCgroupAction::ExecuteForTask(int tid) const { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 315 | CacheUseResult result = UseCachedFd(ProfileAction::RCT_TASK, tid); |
| 316 | if (result != ProfileAction::UNUSED) { |
| 317 | return result == ProfileAction::SUCCESS; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 320 | // fd was not cached or cached fd can't be used |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 321 | std::string tasks_path = controller()->GetTasksFilePath(path_); |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 322 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC))); |
| 323 | if (tmp_fd < 0) { |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 324 | PLOG(WARNING) << "Failed to open " << tasks_path; |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 325 | return false; |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 326 | } |
Suren Baghdasaryan | ec88556 | 2021-09-02 19:47:12 -0700 | [diff] [blame] | 327 | if (!AddTidToCgroup(tid, tmp_fd, controller()->name())) { |
Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 328 | LOG(ERROR) << "Failed to add task into cgroup"; |
Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 329 | return false; |
| 330 | } |
| 331 | |
| 332 | return true; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 335 | void SetCgroupAction::EnableResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 336 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 337 | // Return early to prevent unnecessary calls to controller_.Get{Tasks|Procs}FilePath() which |
| 338 | // include regex evaluations |
| 339 | if (fd_[cache_type] != FdCacheHelper::FDS_NOT_CACHED) { |
| 340 | return; |
| 341 | } |
| 342 | switch (cache_type) { |
| 343 | case (ProfileAction::RCT_TASK): |
| 344 | FdCacheHelper::Cache(controller_.GetTasksFilePath(path_), fd_[cache_type]); |
| 345 | break; |
| 346 | case (ProfileAction::RCT_PROCESS): |
| 347 | // uid and pid don't matter because IsAppDependentPath ensures the path doesn't use them |
| 348 | FdCacheHelper::Cache(controller_.GetProcsFilePath(path_, 0, 0), fd_[cache_type]); |
| 349 | break; |
| 350 | default: |
| 351 | LOG(ERROR) << "Invalid cache type is specified!"; |
| 352 | break; |
| 353 | } |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 354 | } |
| 355 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 356 | void SetCgroupAction::DropResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 357 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 358 | FdCacheHelper::Drop(fd_[cache_type]); |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 361 | WriteFileAction::WriteFileAction(const std::string& task_path, const std::string& proc_path, |
| 362 | const std::string& value, bool logfailures) |
| 363 | : task_path_(task_path), proc_path_(proc_path), value_(value), logfailures_(logfailures) { |
| 364 | FdCacheHelper::Init(task_path_, fd_[ProfileAction::RCT_TASK]); |
| 365 | if (!proc_path_.empty()) FdCacheHelper::Init(proc_path_, fd_[ProfileAction::RCT_PROCESS]); |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 366 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 367 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 368 | bool WriteFileAction::WriteValueToFile(const std::string& value_, ResourceCacheType cache_type, |
| 369 | int uid, int pid, bool logfailures) const { |
| 370 | std::string value(value_); |
| 371 | |
| 372 | value = StringReplace(value, "<uid>", std::to_string(uid), true); |
| 373 | value = StringReplace(value, "<pid>", std::to_string(pid), true); |
| 374 | |
| 375 | CacheUseResult result = UseCachedFd(cache_type, value); |
| 376 | |
| 377 | if (result != ProfileAction::UNUSED) { |
| 378 | return result == ProfileAction::SUCCESS; |
| 379 | } |
| 380 | |
| 381 | std::string path; |
| 382 | if (cache_type == ProfileAction::RCT_TASK || proc_path_.empty()) { |
| 383 | path = task_path_; |
| 384 | } else { |
| 385 | path = proc_path_; |
| 386 | } |
| 387 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 388 | // Use WriteStringToFd instead of WriteStringToFile because the latter will open file with |
| 389 | // O_TRUNC which causes kernfs_mutex contention |
| 390 | 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] | 391 | |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 392 | if (tmp_fd < 0) { |
| 393 | if (logfailures) PLOG(WARNING) << "Failed to open " << path; |
| 394 | return false; |
| 395 | } |
| 396 | |
| 397 | if (!WriteStringToFd(value, tmp_fd)) { |
| 398 | if (logfailures) PLOG(ERROR) << "Failed to write '" << value << "' to " << path; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 399 | return false; |
| 400 | } |
| 401 | |
| 402 | return true; |
| 403 | } |
| 404 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 405 | ProfileAction::CacheUseResult WriteFileAction::UseCachedFd(ResourceCacheType cache_type, |
| 406 | const std::string& value) const { |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 407 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 408 | if (FdCacheHelper::IsCached(fd_[cache_type])) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 409 | // fd is cached, reuse it |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 410 | bool ret = WriteStringToFd(value, fd_[cache_type]); |
| 411 | |
| 412 | if (!ret && logfailures_) { |
| 413 | if (cache_type == ProfileAction::RCT_TASK || proc_path_.empty()) { |
| 414 | PLOG(ERROR) << "Failed to write '" << value << "' to " << task_path_; |
| 415 | } else { |
| 416 | PLOG(ERROR) << "Failed to write '" << value << "' to " << proc_path_; |
| 417 | } |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 418 | } |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 419 | return ret ? ProfileAction::SUCCESS : ProfileAction::FAIL; |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 422 | if (fd_[cache_type] == FdCacheHelper::FDS_INACCESSIBLE) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 423 | // no permissions to access the file, ignore |
| 424 | return ProfileAction::SUCCESS; |
| 425 | } |
| 426 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 427 | if (cache_type == ResourceCacheType::RCT_TASK && |
| 428 | fd_[cache_type] == FdCacheHelper::FDS_APP_DEPENDENT) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 429 | // application-dependent path can't be used with tid |
| 430 | PLOG(ERROR) << "Application profile can't be applied to a thread"; |
| 431 | return ProfileAction::FAIL; |
| 432 | } |
| 433 | return ProfileAction::UNUSED; |
| 434 | } |
| 435 | |
| 436 | bool WriteFileAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 437 | if (!proc_path_.empty()) { |
| 438 | return WriteValueToFile(value_, ProfileAction::RCT_PROCESS, uid, pid, logfailures_); |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 439 | } |
| 440 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 441 | DIR* d; |
| 442 | struct dirent* de; |
| 443 | char proc_path[255]; |
| 444 | int t_pid; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 445 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 446 | sprintf(proc_path, "/proc/%d/task", pid); |
| 447 | if (!(d = opendir(proc_path))) { |
| 448 | return false; |
| 449 | } |
| 450 | |
| 451 | while ((de = readdir(d))) { |
| 452 | if (de->d_name[0] == '.') { |
| 453 | continue; |
| 454 | } |
| 455 | |
| 456 | t_pid = atoi(de->d_name); |
| 457 | |
| 458 | if (!t_pid) { |
| 459 | continue; |
| 460 | } |
| 461 | |
| 462 | WriteValueToFile(value_, ProfileAction::RCT_TASK, uid, t_pid, logfailures_); |
| 463 | } |
| 464 | |
| 465 | closedir(d); |
| 466 | |
| 467 | return true; |
Rick Yiu | d4c5351 | 2021-11-21 15:57:36 +0800 | [diff] [blame] | 468 | } |
| 469 | |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 470 | bool WriteFileAction::ExecuteForTask(int tid) const { |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 471 | return WriteValueToFile(value_, ProfileAction::RCT_TASK, getuid(), tid, logfailures_); |
| 472 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 473 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 474 | void WriteFileAction::EnableResourceCaching(ResourceCacheType cache_type) { |
| 475 | std::lock_guard<std::mutex> lock(fd_mutex_); |
| 476 | if (fd_[cache_type] != FdCacheHelper::FDS_NOT_CACHED) { |
| 477 | return; |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 478 | } |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 479 | switch (cache_type) { |
| 480 | case (ProfileAction::RCT_TASK): |
| 481 | FdCacheHelper::Cache(task_path_, fd_[cache_type]); |
| 482 | break; |
| 483 | case (ProfileAction::RCT_PROCESS): |
| 484 | if (!proc_path_.empty()) FdCacheHelper::Cache(proc_path_, fd_[cache_type]); |
| 485 | break; |
| 486 | default: |
| 487 | LOG(ERROR) << "Invalid cache type is specified!"; |
| 488 | break; |
| 489 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 490 | } |
| 491 | |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 492 | void WriteFileAction::DropResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 493 | std::lock_guard<std::mutex> lock(fd_mutex_); |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 494 | FdCacheHelper::Drop(fd_[cache_type]); |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 495 | } |
| 496 | |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 497 | bool ApplyProfileAction::ExecuteForProcess(uid_t uid, pid_t pid) const { |
| 498 | for (const auto& profile : profiles_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 499 | profile->ExecuteForProcess(uid, pid); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 500 | } |
| 501 | return true; |
| 502 | } |
| 503 | |
| 504 | bool ApplyProfileAction::ExecuteForTask(int tid) const { |
| 505 | for (const auto& profile : profiles_) { |
Wei Wang | 8722e4d | 2021-05-14 12:34:54 -0700 | [diff] [blame] | 506 | profile->ExecuteForTask(tid); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 507 | } |
| 508 | return true; |
| 509 | } |
| 510 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 511 | void ApplyProfileAction::EnableResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 512 | for (const auto& profile : profiles_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 513 | profile->EnableResourceCaching(cache_type); |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 517 | void ApplyProfileAction::DropResourceCaching(ResourceCacheType cache_type) { |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 518 | for (const auto& profile : profiles_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 519 | profile->DropResourceCaching(cache_type); |
Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Suren Baghdasaryan | 8438595 | 2020-01-24 16:36:10 -0800 | [diff] [blame] | 523 | void TaskProfile::MoveTo(TaskProfile* profile) { |
| 524 | profile->elements_ = std::move(elements_); |
| 525 | profile->res_cached_ = res_cached_; |
| 526 | } |
| 527 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 528 | bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const { |
| 529 | for (const auto& element : elements_) { |
| 530 | if (!element->ExecuteForProcess(uid, pid)) { |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 531 | LOG(VERBOSE) << "Applying profile action " << element->Name() << " failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 532 | return false; |
| 533 | } |
| 534 | } |
| 535 | return true; |
| 536 | } |
| 537 | |
| 538 | bool TaskProfile::ExecuteForTask(int tid) const { |
| 539 | if (tid == 0) { |
| 540 | tid = GetThreadId(); |
| 541 | } |
| 542 | for (const auto& element : elements_) { |
| 543 | if (!element->ExecuteForTask(tid)) { |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 544 | LOG(VERBOSE) << "Applying profile action " << element->Name() << " failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 545 | return false; |
| 546 | } |
| 547 | } |
| 548 | return true; |
| 549 | } |
| 550 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 551 | void TaskProfile::EnableResourceCaching(ProfileAction::ResourceCacheType cache_type) { |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 552 | if (res_cached_) { |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | for (auto& element : elements_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 557 | element->EnableResourceCaching(cache_type); |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | res_cached_ = true; |
| 561 | } |
| 562 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 563 | void TaskProfile::DropResourceCaching(ProfileAction::ResourceCacheType cache_type) { |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 564 | if (!res_cached_) { |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | for (auto& element : elements_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 569 | element->DropResourceCaching(cache_type); |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | res_cached_ = false; |
| 573 | } |
| 574 | |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 575 | void TaskProfiles::DropResourceCaching(ProfileAction::ResourceCacheType cache_type) const { |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 576 | for (auto& iter : profiles_) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 577 | iter.second->DropResourceCaching(cache_type); |
Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 581 | TaskProfiles& TaskProfiles::GetInstance() { |
Peter Collingbourne | dba6d44 | 2019-03-20 21:09:46 -0700 | [diff] [blame] | 582 | // Deliberately leak this object to avoid a race between destruction on |
| 583 | // process exit and concurrent access from another thread. |
| 584 | static auto* instance = new TaskProfiles; |
| 585 | return *instance; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | TaskProfiles::TaskProfiles() { |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 589 | // load system task profiles |
| 590 | if (!Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_FILE)) { |
| 591 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_FILE << " for [" << getpid() << "] failed"; |
| 592 | } |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 593 | |
| 594 | // load API-level specific system task profiles if available |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 595 | 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] | 596 | if (api_level > 0) { |
| 597 | std::string api_profiles_path = |
| 598 | android::base::StringPrintf(TEMPLATE_TASK_PROFILE_API_FILE, api_level); |
| 599 | if (!access(api_profiles_path.c_str(), F_OK) || errno != ENOENT) { |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 600 | if (!Load(CgroupMap::GetInstance(), api_profiles_path)) { |
Suren Baghdasaryan | c2ee2e5 | 2022-01-20 10:58:43 -0800 | [diff] [blame] | 601 | LOG(ERROR) << "Loading " << api_profiles_path << " for [" << getpid() << "] failed"; |
Suren Baghdasaryan | 756a604 | 2020-12-03 11:38:42 -0800 | [diff] [blame] | 602 | } |
Suren Baghdasaryan | 35221b5 | 2020-11-20 17:08:51 -0800 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 606 | // load vendor task profiles if the file exists |
| 607 | if (!access(TASK_PROFILE_DB_VENDOR_FILE, F_OK) && |
| 608 | !Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_VENDOR_FILE)) { |
| 609 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_VENDOR_FILE << " for [" << getpid() |
| 610 | << "] failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 614 | bool TaskProfiles::Load(const CgroupMap& cg_map, const std::string& file_name) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 615 | std::string json_doc; |
| 616 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 617 | if (!android::base::ReadFileToString(file_name, &json_doc)) { |
| 618 | LOG(ERROR) << "Failed to read task profiles from " << file_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 619 | return false; |
| 620 | } |
| 621 | |
Haibo Huang | d9ac92a | 2021-02-24 17:34:50 -0800 | [diff] [blame] | 622 | Json::CharReaderBuilder builder; |
| 623 | std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 624 | Json::Value root; |
Haibo Huang | d9ac92a | 2021-02-24 17:34:50 -0800 | [diff] [blame] | 625 | std::string errorMessage; |
| 626 | if (!reader->parse(&*json_doc.begin(), &*json_doc.end(), &root, &errorMessage)) { |
| 627 | LOG(ERROR) << "Failed to parse task profiles: " << errorMessage; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 628 | return false; |
| 629 | } |
| 630 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 631 | const Json::Value& attr = root["Attributes"]; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 632 | for (Json::Value::ArrayIndex i = 0; i < attr.size(); ++i) { |
| 633 | std::string name = attr[i]["Name"].asString(); |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 634 | std::string controller_name = attr[i]["Controller"].asString(); |
| 635 | std::string file_attr = attr[i]["File"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 636 | |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 637 | auto controller = cg_map.FindController(controller_name); |
| 638 | if (controller.HasValue()) { |
| 639 | auto iter = attributes_.find(name); |
| 640 | if (iter == attributes_.end()) { |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 641 | attributes_[name] = std::make_unique<ProfileAttribute>(controller, file_attr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 642 | } else { |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 643 | iter->second->Reset(controller, file_attr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 644 | } |
| 645 | } else { |
Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 646 | LOG(WARNING) << "Controller " << controller_name << " is not found"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 650 | const Json::Value& profiles_val = root["Profiles"]; |
| 651 | for (Json::Value::ArrayIndex i = 0; i < profiles_val.size(); ++i) { |
| 652 | const Json::Value& profile_val = profiles_val[i]; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 653 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 654 | std::string profile_name = profile_val["Name"].asString(); |
| 655 | const Json::Value& actions = profile_val["Actions"]; |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 656 | auto profile = std::make_shared<TaskProfile>(profile_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 657 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 658 | for (Json::Value::ArrayIndex act_idx = 0; act_idx < actions.size(); ++act_idx) { |
| 659 | const Json::Value& action_val = actions[act_idx]; |
| 660 | std::string action_name = action_val["Name"].asString(); |
| 661 | const Json::Value& params_val = action_val["Params"]; |
| 662 | if (action_name == "JoinCgroup") { |
| 663 | std::string controller_name = params_val["Controller"].asString(); |
| 664 | std::string path = params_val["Path"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 665 | |
Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 666 | auto controller = cg_map.FindController(controller_name); |
| 667 | if (controller.HasValue()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 668 | profile->Add(std::make_unique<SetCgroupAction>(controller, path)); |
| 669 | } else { |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 670 | LOG(WARNING) << "JoinCgroup: controller " << controller_name << " is not found"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 671 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 672 | } else if (action_name == "SetTimerSlack") { |
| 673 | std::string slack_value = params_val["Slack"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 674 | char* end; |
| 675 | unsigned long slack; |
| 676 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 677 | slack = strtoul(slack_value.c_str(), &end, 10); |
| 678 | if (end > slack_value.c_str()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 679 | profile->Add(std::make_unique<SetTimerSlackAction>(slack)); |
| 680 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 681 | LOG(WARNING) << "SetTimerSlack: invalid parameter: " << slack_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 682 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 683 | } else if (action_name == "SetAttribute") { |
| 684 | std::string attr_name = params_val["Name"].asString(); |
| 685 | std::string attr_value = params_val["Value"].asString(); |
Bart Van Assche | 59af680 | 2022-01-24 21:08:57 +0000 | [diff] [blame^] | 686 | bool optional = strcmp(params_val["Optional"].asString().c_str(), "true") == 0; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 687 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 688 | auto iter = attributes_.find(attr_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 689 | if (iter != attributes_.end()) { |
Bart Van Assche | 59af680 | 2022-01-24 21:08:57 +0000 | [diff] [blame^] | 690 | profile->Add(std::make_unique<SetAttributeAction>(iter->second.get(), |
| 691 | attr_value, optional)); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 692 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 693 | LOG(WARNING) << "SetAttribute: unknown attribute: " << attr_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 694 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 695 | } else if (action_name == "SetClamps") { |
| 696 | std::string boost_value = params_val["Boost"].asString(); |
| 697 | std::string clamp_value = params_val["Clamp"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 698 | char* end; |
| 699 | unsigned long boost; |
| 700 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 701 | boost = strtoul(boost_value.c_str(), &end, 10); |
| 702 | if (end > boost_value.c_str()) { |
| 703 | unsigned long clamp = strtoul(clamp_value.c_str(), &end, 10); |
| 704 | if (end > clamp_value.c_str()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 705 | profile->Add(std::make_unique<SetClampsAction>(boost, clamp)); |
| 706 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 707 | LOG(WARNING) << "SetClamps: invalid parameter " << clamp_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 708 | } |
| 709 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 710 | LOG(WARNING) << "SetClamps: invalid parameter: " << boost_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 711 | } |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 712 | } else if (action_name == "WriteFile") { |
| 713 | std::string attr_filepath = params_val["FilePath"].asString(); |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 714 | std::string attr_procfilepath = params_val["ProcFilePath"].asString(); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 715 | std::string attr_value = params_val["Value"].asString(); |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 716 | // FilePath and Value are mandatory |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 717 | if (!attr_filepath.empty() && !attr_value.empty()) { |
Rick Yiu | 49fce95 | 2021-04-08 22:10:06 +0800 | [diff] [blame] | 718 | std::string attr_logfailures = params_val["LogFailures"].asString(); |
| 719 | bool logfailures = attr_logfailures.empty() || attr_logfailures == "true"; |
Rick Yiu | 9221b1e | 2022-02-10 16:44:43 +0800 | [diff] [blame] | 720 | profile->Add(std::make_unique<WriteFileAction>(attr_filepath, attr_procfilepath, |
| 721 | attr_value, logfailures)); |
Rick Yiu | bc1ad96 | 2020-10-26 20:32:52 +0800 | [diff] [blame] | 722 | } else if (attr_filepath.empty()) { |
| 723 | LOG(WARNING) << "WriteFile: invalid parameter: " |
| 724 | << "empty filepath"; |
| 725 | } else if (attr_value.empty()) { |
| 726 | LOG(WARNING) << "WriteFile: invalid parameter: " |
| 727 | << "empty value"; |
| 728 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 729 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 730 | LOG(WARNING) << "Unknown profile action: " << action_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 731 | } |
| 732 | } |
Suren Baghdasaryan | 8438595 | 2020-01-24 16:36:10 -0800 | [diff] [blame] | 733 | auto iter = profiles_.find(profile_name); |
| 734 | if (iter == profiles_.end()) { |
| 735 | profiles_[profile_name] = profile; |
| 736 | } else { |
| 737 | // Move the content rather that replace the profile because old profile might be |
| 738 | // referenced from an aggregate profile if vendor overrides task profiles |
| 739 | profile->MoveTo(iter->second.get()); |
| 740 | profile.reset(); |
| 741 | } |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | const Json::Value& aggregateprofiles_val = root["AggregateProfiles"]; |
| 745 | for (Json::Value::ArrayIndex i = 0; i < aggregateprofiles_val.size(); ++i) { |
| 746 | const Json::Value& aggregateprofile_val = aggregateprofiles_val[i]; |
| 747 | |
| 748 | std::string aggregateprofile_name = aggregateprofile_val["Name"].asString(); |
| 749 | const Json::Value& aggregateprofiles = aggregateprofile_val["Profiles"]; |
| 750 | std::vector<std::shared_ptr<TaskProfile>> profiles; |
| 751 | bool ret = true; |
| 752 | |
| 753 | for (Json::Value::ArrayIndex pf_idx = 0; pf_idx < aggregateprofiles.size(); ++pf_idx) { |
| 754 | std::string profile_name = aggregateprofiles[pf_idx].asString(); |
| 755 | |
| 756 | if (profile_name == aggregateprofile_name) { |
| 757 | LOG(WARNING) << "AggregateProfiles: recursive profile name: " << profile_name; |
| 758 | ret = false; |
| 759 | break; |
| 760 | } else if (profiles_.find(profile_name) == profiles_.end()) { |
| 761 | LOG(WARNING) << "AggregateProfiles: undefined profile name: " << profile_name; |
| 762 | ret = false; |
| 763 | break; |
| 764 | } else { |
| 765 | profiles.push_back(profiles_[profile_name]); |
| 766 | } |
| 767 | } |
| 768 | if (ret) { |
Bart Van Assche | f096bd2 | 2022-01-24 19:59:13 +0000 | [diff] [blame] | 769 | auto profile = std::make_shared<TaskProfile>(aggregateprofile_name); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 770 | profile->Add(std::make_unique<ApplyProfileAction>(profiles)); |
| 771 | profiles_[aggregateprofile_name] = profile; |
| 772 | } |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | return true; |
| 776 | } |
| 777 | |
Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 778 | TaskProfile* TaskProfiles::GetProfile(const std::string& name) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 779 | auto iter = profiles_.find(name); |
| 780 | |
| 781 | if (iter != profiles_.end()) { |
| 782 | return iter->second.get(); |
| 783 | } |
| 784 | return nullptr; |
| 785 | } |
| 786 | |
Bart Van Assche | 4c99e96 | 2022-02-03 19:50:16 +0000 | [diff] [blame] | 787 | const IProfileAttribute* TaskProfiles::GetAttribute(const std::string& name) const { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 788 | auto iter = attributes_.find(name); |
| 789 | |
| 790 | if (iter != attributes_.end()) { |
| 791 | return iter->second.get(); |
| 792 | } |
| 793 | return nullptr; |
| 794 | } |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 795 | |
| 796 | bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid, |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 797 | const std::vector<std::string>& profiles, bool use_fd_cache) { |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 798 | for (const auto& name : profiles) { |
| 799 | TaskProfile* profile = GetProfile(name); |
| 800 | if (profile != nullptr) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 801 | if (use_fd_cache) { |
| 802 | profile->EnableResourceCaching(ProfileAction::RCT_PROCESS); |
| 803 | } |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 804 | if (!profile->ExecuteForProcess(uid, pid)) { |
| 805 | PLOG(WARNING) << "Failed to apply " << name << " process profile"; |
| 806 | } |
| 807 | } else { |
| 808 | PLOG(WARNING) << "Failed to find " << name << "process profile"; |
| 809 | } |
| 810 | } |
| 811 | return true; |
| 812 | } |
| 813 | |
| 814 | bool TaskProfiles::SetTaskProfiles(int tid, const std::vector<std::string>& profiles, |
| 815 | bool use_fd_cache) { |
| 816 | for (const auto& name : profiles) { |
| 817 | TaskProfile* profile = GetProfile(name); |
| 818 | if (profile != nullptr) { |
| 819 | if (use_fd_cache) { |
Suren Baghdasaryan | f3bdac7 | 2022-01-20 15:41:28 -0800 | [diff] [blame] | 820 | profile->EnableResourceCaching(ProfileAction::RCT_TASK); |
Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 821 | } |
| 822 | if (!profile->ExecuteForTask(tid)) { |
| 823 | PLOG(WARNING) << "Failed to apply " << name << " task profile"; |
| 824 | } |
| 825 | } else { |
| 826 | PLOG(WARNING) << "Failed to find " << name << "task profile"; |
| 827 | } |
| 828 | } |
| 829 | return true; |
| 830 | } |