| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2019 The Android Open Source Project | 
|  | 3 | * | 
|  | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | * you may not use this file except in compliance with the License. | 
|  | 6 | * You may obtain a copy of the License at | 
|  | 7 | * | 
|  | 8 | *      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | * | 
|  | 10 | * Unless required by applicable law or agreed to in writing, software | 
|  | 11 | * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | * See the License for the specific language governing permissions and | 
|  | 14 | * limitations under the License. | 
|  | 15 | */ | 
|  | 16 |  | 
|  | 17 | //#define LOG_NDEBUG 0 | 
|  | 18 | #define LOG_TAG "libprocessgroup" | 
|  | 19 |  | 
|  | 20 | #include <fcntl.h> | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 21 | #include <task_profiles.h> | 
|  | 22 | #include <string> | 
|  | 23 |  | 
|  | 24 | #include <android-base/file.h> | 
|  | 25 | #include <android-base/logging.h> | 
|  | 26 | #include <android-base/stringprintf.h> | 
|  | 27 | #include <android-base/threads.h> | 
|  | 28 |  | 
|  | 29 | #include <cutils/android_filesystem_config.h> | 
|  | 30 |  | 
|  | 31 | #include <json/reader.h> | 
|  | 32 | #include <json/value.h> | 
|  | 33 |  | 
| Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 34 | // To avoid issues in sdk_mac build | 
|  | 35 | #if defined(__ANDROID__) | 
|  | 36 | #include <sys/prctl.h> | 
|  | 37 | #endif | 
|  | 38 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 39 | using android::base::GetThreadId; | 
|  | 40 | using android::base::StringPrintf; | 
|  | 41 | using android::base::unique_fd; | 
|  | 42 | using android::base::WriteStringToFile; | 
|  | 43 |  | 
|  | 44 | #define TASK_PROFILE_DB_FILE "/etc/task_profiles.json" | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 45 | #define TASK_PROFILE_DB_VENDOR_FILE "/vendor/etc/task_profiles.json" | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 46 |  | 
|  | 47 | bool ProfileAttribute::GetPathForTask(int tid, std::string* path) const { | 
|  | 48 | std::string subgroup; | 
|  | 49 | if (!controller_->GetTaskGroup(tid, &subgroup)) { | 
|  | 50 | return false; | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | if (path == nullptr) { | 
|  | 54 | return true; | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | if (subgroup.empty()) { | 
|  | 58 | *path = StringPrintf("%s/%s", controller_->path(), file_name_.c_str()); | 
|  | 59 | } else { | 
|  | 60 | *path = StringPrintf("%s/%s/%s", controller_->path(), subgroup.c_str(), file_name_.c_str()); | 
|  | 61 | } | 
|  | 62 | return true; | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | bool SetClampsAction::ExecuteForProcess(uid_t, pid_t) const { | 
|  | 66 | // TODO: add support when kernel supports util_clamp | 
|  | 67 | LOG(WARNING) << "SetClampsAction::ExecuteForProcess is not supported"; | 
|  | 68 | return false; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | bool SetClampsAction::ExecuteForTask(int) const { | 
|  | 72 | // TODO: add support when kernel supports util_clamp | 
|  | 73 | LOG(WARNING) << "SetClampsAction::ExecuteForTask is not supported"; | 
|  | 74 | return false; | 
|  | 75 | } | 
|  | 76 |  | 
| Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 77 | // To avoid issues in sdk_mac build | 
|  | 78 | #if defined(__ANDROID__) | 
|  | 79 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 80 | bool SetTimerSlackAction::IsTimerSlackSupported(int tid) { | 
|  | 81 | auto file = StringPrintf("/proc/%d/timerslack_ns", tid); | 
|  | 82 |  | 
|  | 83 | return (access(file.c_str(), W_OK) == 0); | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | bool SetTimerSlackAction::ExecuteForTask(int tid) const { | 
|  | 87 | static bool sys_supports_timerslack = IsTimerSlackSupported(tid); | 
|  | 88 |  | 
|  | 89 | // v4.6+ kernels support the /proc/<tid>/timerslack_ns interface. | 
|  | 90 | // TODO: once we've backported this, log if the open(2) fails. | 
|  | 91 | if (sys_supports_timerslack) { | 
|  | 92 | auto file = StringPrintf("/proc/%d/timerslack_ns", tid); | 
|  | 93 | if (!WriteStringToFile(std::to_string(slack_), file)) { | 
| Suren Baghdasaryan | 2bc5228 | 2019-02-12 17:30:26 -0800 | [diff] [blame] | 94 | if (errno == ENOENT) { | 
|  | 95 | // This happens when process is already dead | 
|  | 96 | return true; | 
|  | 97 | } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 98 | PLOG(ERROR) << "set_timerslack_ns write failed"; | 
|  | 99 | } | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | // TODO: Remove when /proc/<tid>/timerslack_ns interface is backported. | 
|  | 103 | if (tid == 0 || tid == GetThreadId()) { | 
|  | 104 | if (prctl(PR_SET_TIMERSLACK, slack_) == -1) { | 
|  | 105 | PLOG(ERROR) << "set_timerslack_ns prctl failed"; | 
|  | 106 | } | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | return true; | 
|  | 110 | } | 
|  | 111 |  | 
| Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 112 | #endif | 
|  | 113 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 114 | bool SetAttributeAction::ExecuteForProcess(uid_t, pid_t pid) const { | 
|  | 115 | return ExecuteForTask(pid); | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | bool SetAttributeAction::ExecuteForTask(int tid) const { | 
|  | 119 | std::string path; | 
|  | 120 |  | 
|  | 121 | if (!attribute_->GetPathForTask(tid, &path)) { | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 122 | LOG(ERROR) << "Failed to find cgroup for tid " << tid; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 123 | return false; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | if (!WriteStringToFile(value_, path)) { | 
|  | 127 | PLOG(ERROR) << "Failed to write '" << value_ << "' to " << path; | 
|  | 128 | return false; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | return true; | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | bool SetCgroupAction::IsAppDependentPath(const std::string& path) { | 
|  | 135 | return path.find("<uid>", 0) != std::string::npos || path.find("<pid>", 0) != std::string::npos; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | SetCgroupAction::SetCgroupAction(const CgroupController* c, const std::string& p) | 
|  | 139 | : controller_(c), path_(p) { | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 140 | #ifdef CACHE_FILE_DESCRIPTORS | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 141 | // cache file descriptor only if path is app independent | 
|  | 142 | if (IsAppDependentPath(path_)) { | 
|  | 143 | // file descriptor is not cached | 
|  | 144 | fd_.reset(-2); | 
|  | 145 | return; | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | std::string tasks_path = c->GetTasksFilePath(p.c_str()); | 
|  | 149 |  | 
|  | 150 | if (access(tasks_path.c_str(), W_OK) != 0) { | 
|  | 151 | // file is not accessible | 
|  | 152 | fd_.reset(-1); | 
|  | 153 | return; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | unique_fd fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC))); | 
|  | 157 | if (fd < 0) { | 
|  | 158 | PLOG(ERROR) << "Failed to cache fd '" << tasks_path << "'"; | 
|  | 159 | fd_.reset(-1); | 
|  | 160 | return; | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | fd_ = std::move(fd); | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 164 | #endif | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 165 | } | 
|  | 166 |  | 
|  | 167 | bool SetCgroupAction::AddTidToCgroup(int tid, int fd) { | 
|  | 168 | if (tid <= 0) { | 
|  | 169 | return true; | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | std::string value = std::to_string(tid); | 
|  | 173 |  | 
|  | 174 | if (TEMP_FAILURE_RETRY(write(fd, value.c_str(), value.length())) < 0) { | 
|  | 175 | // If the thread is in the process of exiting, don't flag an error | 
|  | 176 | if (errno != ESRCH) { | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 177 | PLOG(ERROR) << "AddTidToCgroup failed to write '" << value << "'; fd=" << fd; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 178 | return false; | 
|  | 179 | } | 
|  | 180 | } | 
|  | 181 |  | 
|  | 182 | return true; | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const { | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 186 | #ifdef CACHE_FILE_DESCRIPTORS | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 187 | if (fd_ >= 0) { | 
|  | 188 | // fd is cached, reuse it | 
|  | 189 | if (!AddTidToCgroup(pid, fd_)) { | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 190 | LOG(ERROR) << "Failed to add task into cgroup"; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 191 | return false; | 
|  | 192 | } | 
|  | 193 | return true; | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | if (fd_ == -1) { | 
|  | 197 | // no permissions to access the file, ignore | 
|  | 198 | return true; | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | // this is app-dependent path, file descriptor is not cached | 
| Wei Wang | 1770566 | 2019-02-26 14:43:51 -0800 | [diff] [blame] | 202 | std::string procs_path = controller_->GetProcsFilePath(path_, uid, pid); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 203 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC))); | 
|  | 204 | if (tmp_fd < 0) { | 
| Elliott Hughes | 08b4d32 | 2019-03-14 20:06:36 -0700 | [diff] [blame] | 205 | PLOG(WARNING) << "Failed to open " << procs_path; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 206 | return false; | 
|  | 207 | } | 
|  | 208 | if (!AddTidToCgroup(pid, tmp_fd)) { | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 209 | LOG(ERROR) << "Failed to add task into cgroup"; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 210 | return false; | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | return true; | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 214 | #else | 
| Wei Wang | 1770566 | 2019-02-26 14:43:51 -0800 | [diff] [blame] | 215 | std::string procs_path = controller_->GetProcsFilePath(path_, uid, pid); | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 216 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC))); | 
|  | 217 | if (tmp_fd < 0) { | 
|  | 218 | // no permissions to access the file, ignore | 
|  | 219 | return true; | 
|  | 220 | } | 
|  | 221 | if (!AddTidToCgroup(pid, tmp_fd)) { | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 222 | LOG(ERROR) << "Failed to add task into cgroup"; | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 223 | return false; | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | return true; | 
|  | 227 | #endif | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 228 | } | 
|  | 229 |  | 
|  | 230 | bool SetCgroupAction::ExecuteForTask(int tid) const { | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 231 | #ifdef CACHE_FILE_DESCRIPTORS | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 232 | if (fd_ >= 0) { | 
|  | 233 | // fd is cached, reuse it | 
|  | 234 | if (!AddTidToCgroup(tid, fd_)) { | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 235 | LOG(ERROR) << "Failed to add task into cgroup"; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 236 | return false; | 
|  | 237 | } | 
|  | 238 | return true; | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | if (fd_ == -1) { | 
|  | 242 | // no permissions to access the file, ignore | 
|  | 243 | return true; | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 | // application-dependent path can't be used with tid | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 247 | LOG(ERROR) << "Application profile can't be applied to a thread"; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 248 | return false; | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 249 | #else | 
| Wei Wang | 1770566 | 2019-02-26 14:43:51 -0800 | [diff] [blame] | 250 | std::string tasks_path = controller_->GetTasksFilePath(path_); | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 251 | unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC))); | 
|  | 252 | if (tmp_fd < 0) { | 
|  | 253 | // no permissions to access the file, ignore | 
|  | 254 | return true; | 
|  | 255 | } | 
|  | 256 | if (!AddTidToCgroup(tid, tmp_fd)) { | 
| Wei Wang | d71d301 | 2019-03-07 11:59:12 -0800 | [diff] [blame] | 257 | LOG(ERROR) << "Failed to add task into cgroup"; | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 258 | return false; | 
|  | 259 | } | 
|  | 260 |  | 
|  | 261 | return true; | 
|  | 262 | #endif | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 263 | } | 
|  | 264 |  | 
|  | 265 | bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const { | 
|  | 266 | for (const auto& element : elements_) { | 
|  | 267 | if (!element->ExecuteForProcess(uid, pid)) { | 
|  | 268 | return false; | 
|  | 269 | } | 
|  | 270 | } | 
|  | 271 | return true; | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | bool TaskProfile::ExecuteForTask(int tid) const { | 
|  | 275 | if (tid == 0) { | 
|  | 276 | tid = GetThreadId(); | 
|  | 277 | } | 
|  | 278 | for (const auto& element : elements_) { | 
|  | 279 | if (!element->ExecuteForTask(tid)) { | 
|  | 280 | return false; | 
|  | 281 | } | 
|  | 282 | } | 
|  | 283 | return true; | 
|  | 284 | } | 
|  | 285 |  | 
|  | 286 | TaskProfiles& TaskProfiles::GetInstance() { | 
|  | 287 | static TaskProfiles instance; | 
|  | 288 | return instance; | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | TaskProfiles::TaskProfiles() { | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 292 | // load system task profiles | 
|  | 293 | if (!Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_FILE)) { | 
|  | 294 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_FILE << " for [" << getpid() << "] failed"; | 
|  | 295 | } | 
|  | 296 |  | 
|  | 297 | // load vendor task profiles if the file exists | 
|  | 298 | if (!access(TASK_PROFILE_DB_VENDOR_FILE, F_OK) && | 
|  | 299 | !Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_VENDOR_FILE)) { | 
|  | 300 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_VENDOR_FILE << " for [" << getpid() | 
|  | 301 | << "] failed"; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 302 | } | 
|  | 303 | } | 
|  | 304 |  | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 305 | bool TaskProfiles::Load(const CgroupMap& cg_map, const std::string& file_name) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 306 | std::string json_doc; | 
|  | 307 |  | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 308 | if (!android::base::ReadFileToString(file_name, &json_doc)) { | 
|  | 309 | LOG(ERROR) << "Failed to read task profiles from " << file_name; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 310 | return false; | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | Json::Reader reader; | 
|  | 314 | Json::Value root; | 
|  | 315 | if (!reader.parse(json_doc, root)) { | 
|  | 316 | LOG(ERROR) << "Failed to parse task profiles: " << reader.getFormattedErrorMessages(); | 
|  | 317 | return false; | 
|  | 318 | } | 
|  | 319 |  | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 320 | const Json::Value& attr = root["Attributes"]; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 321 | for (Json::Value::ArrayIndex i = 0; i < attr.size(); ++i) { | 
|  | 322 | std::string name = attr[i]["Name"].asString(); | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 323 | std::string controller_name = attr[i]["Controller"].asString(); | 
|  | 324 | std::string file_attr = attr[i]["File"].asString(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 325 |  | 
|  | 326 | if (attributes_.find(name) == attributes_.end()) { | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 327 | const CgroupController* controller = cg_map.FindController(controller_name); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 328 | if (controller) { | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 329 | attributes_[name] = std::make_unique<ProfileAttribute>(controller, file_attr); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 330 | } else { | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 331 | LOG(WARNING) << "Controller " << controller_name << " is not found"; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 332 | } | 
|  | 333 | } else { | 
|  | 334 | LOG(WARNING) << "Attribute " << name << " is already defined"; | 
|  | 335 | } | 
|  | 336 | } | 
|  | 337 |  | 
|  | 338 | std::map<std::string, std::string> params; | 
|  | 339 |  | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 340 | const Json::Value& profiles_val = root["Profiles"]; | 
|  | 341 | for (Json::Value::ArrayIndex i = 0; i < profiles_val.size(); ++i) { | 
|  | 342 | const Json::Value& profile_val = profiles_val[i]; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 343 |  | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 344 | std::string profile_name = profile_val["Name"].asString(); | 
|  | 345 | const Json::Value& actions = profile_val["Actions"]; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 346 | auto profile = std::make_unique<TaskProfile>(); | 
|  | 347 |  | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 348 | for (Json::Value::ArrayIndex act_idx = 0; act_idx < actions.size(); ++act_idx) { | 
|  | 349 | const Json::Value& action_val = actions[act_idx]; | 
|  | 350 | std::string action_name = action_val["Name"].asString(); | 
|  | 351 | const Json::Value& params_val = action_val["Params"]; | 
|  | 352 | if (action_name == "JoinCgroup") { | 
|  | 353 | std::string controller_name = params_val["Controller"].asString(); | 
|  | 354 | std::string path = params_val["Path"].asString(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 355 |  | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 356 | const CgroupController* controller = cg_map.FindController(controller_name); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 357 | if (controller) { | 
|  | 358 | profile->Add(std::make_unique<SetCgroupAction>(controller, path)); | 
|  | 359 | } else { | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 360 | LOG(WARNING) << "JoinCgroup: controller " << controller_name << " is not found"; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 361 | } | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 362 | } else if (action_name == "SetTimerSlack") { | 
|  | 363 | std::string slack_value = params_val["Slack"].asString(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 364 | char* end; | 
|  | 365 | unsigned long slack; | 
|  | 366 |  | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 367 | slack = strtoul(slack_value.c_str(), &end, 10); | 
|  | 368 | if (end > slack_value.c_str()) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 369 | profile->Add(std::make_unique<SetTimerSlackAction>(slack)); | 
|  | 370 | } else { | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 371 | LOG(WARNING) << "SetTimerSlack: invalid parameter: " << slack_value; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 372 | } | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 373 | } else if (action_name == "SetAttribute") { | 
|  | 374 | std::string attr_name = params_val["Name"].asString(); | 
|  | 375 | std::string attr_value = params_val["Value"].asString(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 376 |  | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 377 | auto iter = attributes_.find(attr_name); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 378 | if (iter != attributes_.end()) { | 
|  | 379 | profile->Add( | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 380 | std::make_unique<SetAttributeAction>(iter->second.get(), attr_value)); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 381 | } else { | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 382 | LOG(WARNING) << "SetAttribute: unknown attribute: " << attr_name; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 383 | } | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 384 | } else if (action_name == "SetClamps") { | 
|  | 385 | std::string boost_value = params_val["Boost"].asString(); | 
|  | 386 | std::string clamp_value = params_val["Clamp"].asString(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 387 | char* end; | 
|  | 388 | unsigned long boost; | 
|  | 389 |  | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 390 | boost = strtoul(boost_value.c_str(), &end, 10); | 
|  | 391 | if (end > boost_value.c_str()) { | 
|  | 392 | unsigned long clamp = strtoul(clamp_value.c_str(), &end, 10); | 
|  | 393 | if (end > clamp_value.c_str()) { | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 394 | profile->Add(std::make_unique<SetClampsAction>(boost, clamp)); | 
|  | 395 | } else { | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 396 | LOG(WARNING) << "SetClamps: invalid parameter " << clamp_value; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 397 | } | 
|  | 398 | } else { | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 399 | LOG(WARNING) << "SetClamps: invalid parameter: " << boost_value; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 400 | } | 
|  | 401 | } else { | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 402 | LOG(WARNING) << "Unknown profile action: " << action_name; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 403 | } | 
|  | 404 | } | 
| Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 405 | profiles_[profile_name] = std::move(profile); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 406 | } | 
|  | 407 |  | 
|  | 408 | return true; | 
|  | 409 | } | 
|  | 410 |  | 
|  | 411 | const TaskProfile* TaskProfiles::GetProfile(const std::string& name) const { | 
|  | 412 | auto iter = profiles_.find(name); | 
|  | 413 |  | 
|  | 414 | if (iter != profiles_.end()) { | 
|  | 415 | return iter->second.get(); | 
|  | 416 | } | 
|  | 417 | return nullptr; | 
|  | 418 | } | 
|  | 419 |  | 
|  | 420 | const ProfileAttribute* TaskProfiles::GetAttribute(const std::string& name) const { | 
|  | 421 | auto iter = attributes_.find(name); | 
|  | 422 |  | 
|  | 423 | if (iter != attributes_.end()) { | 
|  | 424 | return iter->second.get(); | 
|  | 425 | } | 
|  | 426 | return nullptr; | 
|  | 427 | } |