| 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" | 
 | 45 |  | 
 | 46 | bool ProfileAttribute::GetPathForTask(int tid, std::string* path) const { | 
 | 47 |     std::string subgroup; | 
 | 48 |     if (!controller_->GetTaskGroup(tid, &subgroup)) { | 
 | 49 |         return false; | 
 | 50 |     } | 
 | 51 |  | 
 | 52 |     if (path == nullptr) { | 
 | 53 |         return true; | 
 | 54 |     } | 
 | 55 |  | 
 | 56 |     if (subgroup.empty()) { | 
 | 57 |         *path = StringPrintf("%s/%s", controller_->path(), file_name_.c_str()); | 
 | 58 |     } else { | 
 | 59 |         *path = StringPrintf("%s/%s/%s", controller_->path(), subgroup.c_str(), file_name_.c_str()); | 
 | 60 |     } | 
 | 61 |     return true; | 
 | 62 | } | 
 | 63 |  | 
 | 64 | bool SetClampsAction::ExecuteForProcess(uid_t, pid_t) const { | 
 | 65 |     // TODO: add support when kernel supports util_clamp | 
 | 66 |     LOG(WARNING) << "SetClampsAction::ExecuteForProcess is not supported"; | 
 | 67 |     return false; | 
 | 68 | } | 
 | 69 |  | 
 | 70 | bool SetClampsAction::ExecuteForTask(int) const { | 
 | 71 |     // TODO: add support when kernel supports util_clamp | 
 | 72 |     LOG(WARNING) << "SetClampsAction::ExecuteForTask is not supported"; | 
 | 73 |     return false; | 
 | 74 | } | 
 | 75 |  | 
| Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 76 | // To avoid issues in sdk_mac build | 
 | 77 | #if defined(__ANDROID__) | 
 | 78 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 79 | bool SetTimerSlackAction::IsTimerSlackSupported(int tid) { | 
 | 80 |     auto file = StringPrintf("/proc/%d/timerslack_ns", tid); | 
 | 81 |  | 
 | 82 |     return (access(file.c_str(), W_OK) == 0); | 
 | 83 | } | 
 | 84 |  | 
 | 85 | bool SetTimerSlackAction::ExecuteForTask(int tid) const { | 
 | 86 |     static bool sys_supports_timerslack = IsTimerSlackSupported(tid); | 
 | 87 |  | 
 | 88 |     // v4.6+ kernels support the /proc/<tid>/timerslack_ns interface. | 
 | 89 |     // TODO: once we've backported this, log if the open(2) fails. | 
 | 90 |     if (sys_supports_timerslack) { | 
 | 91 |         auto file = StringPrintf("/proc/%d/timerslack_ns", tid); | 
 | 92 |         if (!WriteStringToFile(std::to_string(slack_), file)) { | 
 | 93 |             PLOG(ERROR) << "set_timerslack_ns write failed"; | 
 | 94 |         } | 
 | 95 |     } | 
 | 96 |  | 
 | 97 |     // TODO: Remove when /proc/<tid>/timerslack_ns interface is backported. | 
 | 98 |     if (tid == 0 || tid == GetThreadId()) { | 
 | 99 |         if (prctl(PR_SET_TIMERSLACK, slack_) == -1) { | 
 | 100 |             PLOG(ERROR) << "set_timerslack_ns prctl failed"; | 
 | 101 |         } | 
 | 102 |     } | 
 | 103 |  | 
 | 104 |     return true; | 
 | 105 | } | 
 | 106 |  | 
| Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 107 | #endif | 
 | 108 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 109 | bool SetAttributeAction::ExecuteForProcess(uid_t, pid_t pid) const { | 
 | 110 |     return ExecuteForTask(pid); | 
 | 111 | } | 
 | 112 |  | 
 | 113 | bool SetAttributeAction::ExecuteForTask(int tid) const { | 
 | 114 |     std::string path; | 
 | 115 |  | 
 | 116 |     if (!attribute_->GetPathForTask(tid, &path)) { | 
 | 117 |         PLOG(ERROR) << "Failed to find cgroup for tid " << tid; | 
 | 118 |         return false; | 
 | 119 |     } | 
 | 120 |  | 
 | 121 |     if (!WriteStringToFile(value_, path)) { | 
 | 122 |         PLOG(ERROR) << "Failed to write '" << value_ << "' to " << path; | 
 | 123 |         return false; | 
 | 124 |     } | 
 | 125 |  | 
 | 126 |     return true; | 
 | 127 | } | 
 | 128 |  | 
 | 129 | bool SetCgroupAction::IsAppDependentPath(const std::string& path) { | 
 | 130 |     return path.find("<uid>", 0) != std::string::npos || path.find("<pid>", 0) != std::string::npos; | 
 | 131 | } | 
 | 132 |  | 
 | 133 | SetCgroupAction::SetCgroupAction(const CgroupController* c, const std::string& p) | 
 | 134 |     : controller_(c), path_(p) { | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 135 | #ifdef CACHE_FILE_DESCRIPTORS | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 136 |     // cache file descriptor only if path is app independent | 
 | 137 |     if (IsAppDependentPath(path_)) { | 
 | 138 |         // file descriptor is not cached | 
 | 139 |         fd_.reset(-2); | 
 | 140 |         return; | 
 | 141 |     } | 
 | 142 |  | 
 | 143 |     std::string tasks_path = c->GetTasksFilePath(p.c_str()); | 
 | 144 |  | 
 | 145 |     if (access(tasks_path.c_str(), W_OK) != 0) { | 
 | 146 |         // file is not accessible | 
 | 147 |         fd_.reset(-1); | 
 | 148 |         return; | 
 | 149 |     } | 
 | 150 |  | 
 | 151 |     unique_fd fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC))); | 
 | 152 |     if (fd < 0) { | 
 | 153 |         PLOG(ERROR) << "Failed to cache fd '" << tasks_path << "'"; | 
 | 154 |         fd_.reset(-1); | 
 | 155 |         return; | 
 | 156 |     } | 
 | 157 |  | 
 | 158 |     fd_ = std::move(fd); | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 159 | #endif | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 160 | } | 
 | 161 |  | 
 | 162 | bool SetCgroupAction::AddTidToCgroup(int tid, int fd) { | 
 | 163 |     if (tid <= 0) { | 
 | 164 |         return true; | 
 | 165 |     } | 
 | 166 |  | 
 | 167 |     std::string value = std::to_string(tid); | 
 | 168 |  | 
 | 169 |     if (TEMP_FAILURE_RETRY(write(fd, value.c_str(), value.length())) < 0) { | 
 | 170 |         // If the thread is in the process of exiting, don't flag an error | 
 | 171 |         if (errno != ESRCH) { | 
 | 172 |             PLOG(ERROR) << "JoinGroup failed to write '" << value << "'; fd=" << fd; | 
 | 173 |             return false; | 
 | 174 |         } | 
 | 175 |     } | 
 | 176 |  | 
 | 177 |     return true; | 
 | 178 | } | 
 | 179 |  | 
 | 180 | bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const { | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 181 | #ifdef CACHE_FILE_DESCRIPTORS | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 182 |     if (fd_ >= 0) { | 
 | 183 |         // fd is cached, reuse it | 
 | 184 |         if (!AddTidToCgroup(pid, fd_)) { | 
 | 185 |             PLOG(ERROR) << "Failed to add task into cgroup"; | 
 | 186 |             return false; | 
 | 187 |         } | 
 | 188 |         return true; | 
 | 189 |     } | 
 | 190 |  | 
 | 191 |     if (fd_ == -1) { | 
 | 192 |         // no permissions to access the file, ignore | 
 | 193 |         return true; | 
 | 194 |     } | 
 | 195 |  | 
 | 196 |     // this is app-dependent path, file descriptor is not cached | 
 | 197 |     std::string procs_path = controller_->GetProcsFilePath(path_.c_str(), uid, pid); | 
 | 198 |     unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC))); | 
 | 199 |     if (tmp_fd < 0) { | 
 | 200 |         PLOG(WARNING) << "Failed to open " << procs_path << ": " << strerror(errno); | 
 | 201 |         return false; | 
 | 202 |     } | 
 | 203 |     if (!AddTidToCgroup(pid, tmp_fd)) { | 
 | 204 |         PLOG(ERROR) << "Failed to add task into cgroup"; | 
 | 205 |         return false; | 
 | 206 |     } | 
 | 207 |  | 
 | 208 |     return true; | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 209 | #else | 
 | 210 |     std::string procs_path = controller_->GetProcsFilePath(path_.c_str(), uid, pid); | 
 | 211 |     unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC))); | 
 | 212 |     if (tmp_fd < 0) { | 
 | 213 |         // no permissions to access the file, ignore | 
 | 214 |         return true; | 
 | 215 |     } | 
 | 216 |     if (!AddTidToCgroup(pid, tmp_fd)) { | 
 | 217 |         PLOG(ERROR) << "Failed to add task into cgroup"; | 
 | 218 |         return false; | 
 | 219 |     } | 
 | 220 |  | 
 | 221 |     return true; | 
 | 222 | #endif | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 223 | } | 
 | 224 |  | 
 | 225 | bool SetCgroupAction::ExecuteForTask(int tid) const { | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 226 | #ifdef CACHE_FILE_DESCRIPTORS | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 227 |     if (fd_ >= 0) { | 
 | 228 |         // fd is cached, reuse it | 
 | 229 |         if (!AddTidToCgroup(tid, fd_)) { | 
 | 230 |             PLOG(ERROR) << "Failed to add task into cgroup"; | 
 | 231 |             return false; | 
 | 232 |         } | 
 | 233 |         return true; | 
 | 234 |     } | 
 | 235 |  | 
 | 236 |     if (fd_ == -1) { | 
 | 237 |         // no permissions to access the file, ignore | 
 | 238 |         return true; | 
 | 239 |     } | 
 | 240 |  | 
 | 241 |     // application-dependent path can't be used with tid | 
 | 242 |     PLOG(ERROR) << "Application profile can't be applied to a thread"; | 
 | 243 |     return false; | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 244 | #else | 
 | 245 |     std::string tasks_path = controller_->GetTasksFilePath(path_.c_str()); | 
 | 246 |     unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC))); | 
 | 247 |     if (tmp_fd < 0) { | 
 | 248 |         // no permissions to access the file, ignore | 
 | 249 |         return true; | 
 | 250 |     } | 
 | 251 |     if (!AddTidToCgroup(tid, tmp_fd)) { | 
 | 252 |         PLOG(ERROR) << "Failed to add task into cgroup"; | 
 | 253 |         return false; | 
 | 254 |     } | 
 | 255 |  | 
 | 256 |     return true; | 
 | 257 | #endif | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 258 | } | 
 | 259 |  | 
 | 260 | bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const { | 
 | 261 |     for (const auto& element : elements_) { | 
 | 262 |         if (!element->ExecuteForProcess(uid, pid)) { | 
 | 263 |             return false; | 
 | 264 |         } | 
 | 265 |     } | 
 | 266 |     return true; | 
 | 267 | } | 
 | 268 |  | 
 | 269 | bool TaskProfile::ExecuteForTask(int tid) const { | 
 | 270 |     if (tid == 0) { | 
 | 271 |         tid = GetThreadId(); | 
 | 272 |     } | 
 | 273 |     for (const auto& element : elements_) { | 
 | 274 |         if (!element->ExecuteForTask(tid)) { | 
 | 275 |             return false; | 
 | 276 |         } | 
 | 277 |     } | 
 | 278 |     return true; | 
 | 279 | } | 
 | 280 |  | 
 | 281 | TaskProfiles& TaskProfiles::GetInstance() { | 
 | 282 |     static TaskProfiles instance; | 
 | 283 |     return instance; | 
 | 284 | } | 
 | 285 |  | 
 | 286 | TaskProfiles::TaskProfiles() { | 
 | 287 |     if (!Load(CgroupMap::GetInstance())) { | 
 | 288 |         LOG(ERROR) << "TaskProfiles::Load for [" << getpid() << "] failed"; | 
 | 289 |     } | 
 | 290 | } | 
 | 291 |  | 
 | 292 | bool TaskProfiles::Load(const CgroupMap& cg_map) { | 
 | 293 |     std::string json_doc; | 
 | 294 |  | 
 | 295 |     if (!android::base::ReadFileToString(TASK_PROFILE_DB_FILE, &json_doc)) { | 
 | 296 |         LOG(ERROR) << "Failed to read task profiles from " << TASK_PROFILE_DB_FILE; | 
 | 297 |         return false; | 
 | 298 |     } | 
 | 299 |  | 
 | 300 |     Json::Reader reader; | 
 | 301 |     Json::Value root; | 
 | 302 |     if (!reader.parse(json_doc, root)) { | 
 | 303 |         LOG(ERROR) << "Failed to parse task profiles: " << reader.getFormattedErrorMessages(); | 
 | 304 |         return false; | 
 | 305 |     } | 
 | 306 |  | 
 | 307 |     Json::Value attr = root["Attributes"]; | 
 | 308 |     for (Json::Value::ArrayIndex i = 0; i < attr.size(); ++i) { | 
 | 309 |         std::string name = attr[i]["Name"].asString(); | 
 | 310 |         std::string ctrlName = attr[i]["Controller"].asString(); | 
 | 311 |         std::string file_name = attr[i]["File"].asString(); | 
 | 312 |  | 
 | 313 |         if (attributes_.find(name) == attributes_.end()) { | 
 | 314 |             const CgroupController* controller = cg_map.FindController(ctrlName.c_str()); | 
 | 315 |             if (controller) { | 
 | 316 |                 attributes_[name] = std::make_unique<ProfileAttribute>(controller, file_name); | 
 | 317 |             } else { | 
 | 318 |                 LOG(WARNING) << "Controller " << ctrlName << " is not found"; | 
 | 319 |             } | 
 | 320 |         } else { | 
 | 321 |             LOG(WARNING) << "Attribute " << name << " is already defined"; | 
 | 322 |         } | 
 | 323 |     } | 
 | 324 |  | 
 | 325 |     std::map<std::string, std::string> params; | 
 | 326 |  | 
 | 327 |     Json::Value profilesVal = root["Profiles"]; | 
 | 328 |     for (Json::Value::ArrayIndex i = 0; i < profilesVal.size(); ++i) { | 
 | 329 |         Json::Value profileVal = profilesVal[i]; | 
 | 330 |  | 
 | 331 |         std::string profileName = profileVal["Name"].asString(); | 
 | 332 |         Json::Value actions = profileVal["Actions"]; | 
 | 333 |         auto profile = std::make_unique<TaskProfile>(); | 
 | 334 |  | 
 | 335 |         for (Json::Value::ArrayIndex actIdx = 0; actIdx < actions.size(); ++actIdx) { | 
 | 336 |             Json::Value actionVal = actions[actIdx]; | 
 | 337 |             std::string actionName = actionVal["Name"].asString(); | 
 | 338 |             Json::Value paramsVal = actionVal["Params"]; | 
 | 339 |             if (actionName == "JoinCgroup") { | 
 | 340 |                 std::string ctrlName = paramsVal["Controller"].asString(); | 
 | 341 |                 std::string path = paramsVal["Path"].asString(); | 
 | 342 |  | 
 | 343 |                 const CgroupController* controller = cg_map.FindController(ctrlName.c_str()); | 
 | 344 |                 if (controller) { | 
 | 345 |                     profile->Add(std::make_unique<SetCgroupAction>(controller, path)); | 
 | 346 |                 } else { | 
 | 347 |                     LOG(WARNING) << "JoinCgroup: controller " << ctrlName << " is not found"; | 
 | 348 |                 } | 
 | 349 |             } else if (actionName == "SetTimerSlack") { | 
 | 350 |                 std::string slackValue = paramsVal["Slack"].asString(); | 
 | 351 |                 char* end; | 
 | 352 |                 unsigned long slack; | 
 | 353 |  | 
 | 354 |                 slack = strtoul(slackValue.c_str(), &end, 10); | 
 | 355 |                 if (end > slackValue.c_str()) { | 
 | 356 |                     profile->Add(std::make_unique<SetTimerSlackAction>(slack)); | 
 | 357 |                 } else { | 
 | 358 |                     LOG(WARNING) << "SetTimerSlack: invalid parameter: " << slackValue; | 
 | 359 |                 } | 
 | 360 |             } else if (actionName == "SetAttribute") { | 
 | 361 |                 std::string attrName = paramsVal["Name"].asString(); | 
 | 362 |                 std::string attrValue = paramsVal["Value"].asString(); | 
 | 363 |  | 
 | 364 |                 auto iter = attributes_.find(attrName); | 
 | 365 |                 if (iter != attributes_.end()) { | 
 | 366 |                     profile->Add( | 
 | 367 |                             std::make_unique<SetAttributeAction>(iter->second.get(), attrValue)); | 
 | 368 |                 } else { | 
 | 369 |                     LOG(WARNING) << "SetAttribute: unknown attribute: " << attrName; | 
 | 370 |                 } | 
 | 371 |             } else if (actionName == "SetClamps") { | 
 | 372 |                 std::string boostValue = paramsVal["Boost"].asString(); | 
 | 373 |                 std::string clampValue = paramsVal["Clamp"].asString(); | 
 | 374 |                 char* end; | 
 | 375 |                 unsigned long boost; | 
 | 376 |  | 
 | 377 |                 boost = strtoul(boostValue.c_str(), &end, 10); | 
 | 378 |                 if (end > boostValue.c_str()) { | 
 | 379 |                     unsigned long clamp = strtoul(clampValue.c_str(), &end, 10); | 
 | 380 |                     if (end > clampValue.c_str()) { | 
 | 381 |                         profile->Add(std::make_unique<SetClampsAction>(boost, clamp)); | 
 | 382 |                     } else { | 
 | 383 |                         LOG(WARNING) << "SetClamps: invalid parameter " << clampValue; | 
 | 384 |                     } | 
 | 385 |                 } else { | 
 | 386 |                     LOG(WARNING) << "SetClamps: invalid parameter: " << boostValue; | 
 | 387 |                 } | 
 | 388 |             } else { | 
 | 389 |                 LOG(WARNING) << "Unknown profile action: " << actionName; | 
 | 390 |             } | 
 | 391 |         } | 
 | 392 |         profiles_[profileName] = std::move(profile); | 
 | 393 |     } | 
 | 394 |  | 
 | 395 |     return true; | 
 | 396 | } | 
 | 397 |  | 
 | 398 | const TaskProfile* TaskProfiles::GetProfile(const std::string& name) const { | 
 | 399 |     auto iter = profiles_.find(name); | 
 | 400 |  | 
 | 401 |     if (iter != profiles_.end()) { | 
 | 402 |         return iter->second.get(); | 
 | 403 |     } | 
 | 404 |     return nullptr; | 
 | 405 | } | 
 | 406 |  | 
 | 407 | const ProfileAttribute* TaskProfiles::GetAttribute(const std::string& name) const { | 
 | 408 |     auto iter = attributes_.find(name); | 
 | 409 |  | 
 | 410 |     if (iter != attributes_.end()) { | 
 | 411 |         return iter->second.get(); | 
 | 412 |     } | 
 | 413 |     return nullptr; | 
 | 414 | } |