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() { |
Peter Collingbourne | dba6d44 | 2019-03-20 21:09:46 -0700 | [diff] [blame^] | 287 | // Deliberately leak this object to avoid a race between destruction on |
| 288 | // process exit and concurrent access from another thread. |
| 289 | static auto* instance = new TaskProfiles; |
| 290 | return *instance; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | TaskProfiles::TaskProfiles() { |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 294 | // load system task profiles |
| 295 | if (!Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_FILE)) { |
| 296 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_FILE << " for [" << getpid() << "] failed"; |
| 297 | } |
| 298 | |
| 299 | // load vendor task profiles if the file exists |
| 300 | if (!access(TASK_PROFILE_DB_VENDOR_FILE, F_OK) && |
| 301 | !Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_VENDOR_FILE)) { |
| 302 | LOG(ERROR) << "Loading " << TASK_PROFILE_DB_VENDOR_FILE << " for [" << getpid() |
| 303 | << "] failed"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 307 | bool TaskProfiles::Load(const CgroupMap& cg_map, const std::string& file_name) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 308 | std::string json_doc; |
| 309 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 310 | if (!android::base::ReadFileToString(file_name, &json_doc)) { |
| 311 | LOG(ERROR) << "Failed to read task profiles from " << file_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 312 | return false; |
| 313 | } |
| 314 | |
| 315 | Json::Reader reader; |
| 316 | Json::Value root; |
| 317 | if (!reader.parse(json_doc, root)) { |
| 318 | LOG(ERROR) << "Failed to parse task profiles: " << reader.getFormattedErrorMessages(); |
| 319 | return false; |
| 320 | } |
| 321 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 322 | const Json::Value& attr = root["Attributes"]; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 323 | for (Json::Value::ArrayIndex i = 0; i < attr.size(); ++i) { |
| 324 | std::string name = attr[i]["Name"].asString(); |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 325 | std::string controller_name = attr[i]["Controller"].asString(); |
| 326 | std::string file_attr = attr[i]["File"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 327 | |
| 328 | if (attributes_.find(name) == attributes_.end()) { |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 329 | const CgroupController* controller = cg_map.FindController(controller_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 330 | if (controller) { |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 331 | attributes_[name] = std::make_unique<ProfileAttribute>(controller, file_attr); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 332 | } else { |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 333 | LOG(WARNING) << "Controller " << controller_name << " is not found"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 334 | } |
| 335 | } else { |
| 336 | LOG(WARNING) << "Attribute " << name << " is already defined"; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | std::map<std::string, std::string> params; |
| 341 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 342 | const Json::Value& profiles_val = root["Profiles"]; |
| 343 | for (Json::Value::ArrayIndex i = 0; i < profiles_val.size(); ++i) { |
| 344 | const Json::Value& profile_val = profiles_val[i]; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 345 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 346 | std::string profile_name = profile_val["Name"].asString(); |
| 347 | const Json::Value& actions = profile_val["Actions"]; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 348 | auto profile = std::make_unique<TaskProfile>(); |
| 349 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 350 | for (Json::Value::ArrayIndex act_idx = 0; act_idx < actions.size(); ++act_idx) { |
| 351 | const Json::Value& action_val = actions[act_idx]; |
| 352 | std::string action_name = action_val["Name"].asString(); |
| 353 | const Json::Value& params_val = action_val["Params"]; |
| 354 | if (action_name == "JoinCgroup") { |
| 355 | std::string controller_name = params_val["Controller"].asString(); |
| 356 | std::string path = params_val["Path"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 357 | |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 358 | const CgroupController* controller = cg_map.FindController(controller_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 359 | if (controller) { |
| 360 | profile->Add(std::make_unique<SetCgroupAction>(controller, path)); |
| 361 | } else { |
Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 362 | LOG(WARNING) << "JoinCgroup: controller " << controller_name << " is not found"; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 363 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 364 | } else if (action_name == "SetTimerSlack") { |
| 365 | std::string slack_value = params_val["Slack"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 366 | char* end; |
| 367 | unsigned long slack; |
| 368 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 369 | slack = strtoul(slack_value.c_str(), &end, 10); |
| 370 | if (end > slack_value.c_str()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 371 | profile->Add(std::make_unique<SetTimerSlackAction>(slack)); |
| 372 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 373 | LOG(WARNING) << "SetTimerSlack: invalid parameter: " << slack_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 374 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 375 | } else if (action_name == "SetAttribute") { |
| 376 | std::string attr_name = params_val["Name"].asString(); |
| 377 | std::string attr_value = params_val["Value"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 378 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 379 | auto iter = attributes_.find(attr_name); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 380 | if (iter != attributes_.end()) { |
| 381 | profile->Add( |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 382 | std::make_unique<SetAttributeAction>(iter->second.get(), attr_value)); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 383 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 384 | LOG(WARNING) << "SetAttribute: unknown attribute: " << attr_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 385 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 386 | } else if (action_name == "SetClamps") { |
| 387 | std::string boost_value = params_val["Boost"].asString(); |
| 388 | std::string clamp_value = params_val["Clamp"].asString(); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 389 | char* end; |
| 390 | unsigned long boost; |
| 391 | |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 392 | boost = strtoul(boost_value.c_str(), &end, 10); |
| 393 | if (end > boost_value.c_str()) { |
| 394 | unsigned long clamp = strtoul(clamp_value.c_str(), &end, 10); |
| 395 | if (end > clamp_value.c_str()) { |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 396 | profile->Add(std::make_unique<SetClampsAction>(boost, clamp)); |
| 397 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 398 | LOG(WARNING) << "SetClamps: invalid parameter " << clamp_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 399 | } |
| 400 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 401 | LOG(WARNING) << "SetClamps: invalid parameter: " << boost_value; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 402 | } |
| 403 | } else { |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 404 | LOG(WARNING) << "Unknown profile action: " << action_name; |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 405 | } |
| 406 | } |
Suren Baghdasaryan | e681df4 | 2019-02-20 16:17:22 -0800 | [diff] [blame] | 407 | profiles_[profile_name] = std::move(profile); |
Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | return true; |
| 411 | } |
| 412 | |
| 413 | const TaskProfile* TaskProfiles::GetProfile(const std::string& name) const { |
| 414 | auto iter = profiles_.find(name); |
| 415 | |
| 416 | if (iter != profiles_.end()) { |
| 417 | return iter->second.get(); |
| 418 | } |
| 419 | return nullptr; |
| 420 | } |
| 421 | |
| 422 | const ProfileAttribute* TaskProfiles::GetAttribute(const std::string& name) const { |
| 423 | auto iter = attributes_.find(name); |
| 424 | |
| 425 | if (iter != attributes_.end()) { |
| 426 | return iter->second.get(); |
| 427 | } |
| 428 | return nullptr; |
| 429 | } |