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>
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/properties.h>
27 #include <android-base/stringprintf.h>
28 #include <android-base/strings.h>
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
36 // To avoid issues in sdk_mac build
37 #if defined(__ANDROID__)
38 #include <sys/prctl.h>
39 #endif
40
41 using android::base::GetThreadId;
42 using android::base::GetUintProperty;
43 using android::base::StringPrintf;
44 using android::base::StringReplace;
45 using android::base::unique_fd;
46 using android::base::WriteStringToFile;
47
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";
53
Reset(const CgroupController & controller,const std::string & file_name)54 void ProfileAttribute::Reset(const CgroupController& controller, const std::string& file_name) {
55 controller_ = controller;
56 file_name_ = file_name;
57 }
58
GetPathForTask(int tid,std::string * path) const59 bool ProfileAttribute::GetPathForTask(int tid, std::string* path) const {
60 std::string subgroup;
61 if (!controller()->GetTaskGroup(tid, &subgroup)) {
62 return false;
63 }
64
65 if (path == nullptr) {
66 return true;
67 }
68
69 if (subgroup.empty()) {
70 *path = StringPrintf("%s/%s", controller()->path(), file_name_.c_str());
71 } else {
72 *path = StringPrintf("%s/%s/%s", controller()->path(), subgroup.c_str(),
73 file_name_.c_str());
74 }
75 return true;
76 }
77
ExecuteForProcess(uid_t,pid_t) const78 bool SetClampsAction::ExecuteForProcess(uid_t, pid_t) const {
79 // TODO: add support when kernel supports util_clamp
80 LOG(WARNING) << "SetClampsAction::ExecuteForProcess is not supported";
81 return false;
82 }
83
ExecuteForTask(int) const84 bool SetClampsAction::ExecuteForTask(int) const {
85 // TODO: add support when kernel supports util_clamp
86 LOG(WARNING) << "SetClampsAction::ExecuteForTask is not supported";
87 return false;
88 }
89
90 // To avoid issues in sdk_mac build
91 #if defined(__ANDROID__)
92
IsTimerSlackSupported(int tid)93 bool SetTimerSlackAction::IsTimerSlackSupported(int tid) {
94 auto file = StringPrintf("/proc/%d/timerslack_ns", tid);
95
96 return (access(file.c_str(), W_OK) == 0);
97 }
98
ExecuteForTask(int tid) const99 bool SetTimerSlackAction::ExecuteForTask(int tid) const {
100 static bool sys_supports_timerslack = IsTimerSlackSupported(tid);
101
102 // v4.6+ kernels support the /proc/<tid>/timerslack_ns interface.
103 // TODO: once we've backported this, log if the open(2) fails.
104 if (sys_supports_timerslack) {
105 auto file = StringPrintf("/proc/%d/timerslack_ns", tid);
106 if (!WriteStringToFile(std::to_string(slack_), file)) {
107 if (errno == ENOENT) {
108 // This happens when process is already dead
109 return true;
110 }
111 PLOG(ERROR) << "set_timerslack_ns write failed";
112 }
113 }
114
115 // TODO: Remove when /proc/<tid>/timerslack_ns interface is backported.
116 if (tid == 0 || tid == GetThreadId()) {
117 if (prctl(PR_SET_TIMERSLACK, slack_) == -1) {
118 PLOG(ERROR) << "set_timerslack_ns prctl failed";
119 }
120 }
121
122 return true;
123 }
124
125 #endif
126
ExecuteForProcess(uid_t,pid_t pid) const127 bool SetAttributeAction::ExecuteForProcess(uid_t, pid_t pid) const {
128 return ExecuteForTask(pid);
129 }
130
ExecuteForTask(int tid) const131 bool SetAttributeAction::ExecuteForTask(int tid) const {
132 std::string path;
133
134 if (!attribute_->GetPathForTask(tid, &path)) {
135 LOG(ERROR) << "Failed to find cgroup for tid " << tid;
136 return false;
137 }
138
139 if (!WriteStringToFile(value_, path)) {
140 PLOG(ERROR) << "Failed to write '" << value_ << "' to " << path;
141 return false;
142 }
143
144 return true;
145 }
146
EnableResourceCaching()147 void CachedFdProfileAction::EnableResourceCaching() {
148 std::lock_guard<std::mutex> lock(fd_mutex_);
149 if (fd_ != FDS_NOT_CACHED) {
150 return;
151 }
152
153 std::string tasks_path = GetPath();
154
155 if (access(tasks_path.c_str(), W_OK) != 0) {
156 // file is not accessible
157 fd_.reset(FDS_INACCESSIBLE);
158 return;
159 }
160
161 unique_fd fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC)));
162 if (fd < 0) {
163 PLOG(ERROR) << "Failed to cache fd '" << tasks_path << "'";
164 fd_.reset(FDS_INACCESSIBLE);
165 return;
166 }
167
168 fd_ = std::move(fd);
169 }
170
DropResourceCaching()171 void CachedFdProfileAction::DropResourceCaching() {
172 std::lock_guard<std::mutex> lock(fd_mutex_);
173 if (fd_ == FDS_NOT_CACHED) {
174 return;
175 }
176
177 fd_.reset(FDS_NOT_CACHED);
178 }
179
IsAppDependentPath(const std::string & path)180 bool CachedFdProfileAction::IsAppDependentPath(const std::string& path) {
181 return path.find("<uid>", 0) != std::string::npos || path.find("<pid>", 0) != std::string::npos;
182 }
183
InitFd(const std::string & path)184 void CachedFdProfileAction::InitFd(const std::string& path) {
185 // file descriptors for app-dependent paths can't be cached
186 if (IsAppDependentPath(path)) {
187 // file descriptor is not cached
188 fd_.reset(FDS_APP_DEPENDENT);
189 return;
190 }
191 // file descriptor can be cached later on request
192 fd_.reset(FDS_NOT_CACHED);
193 }
194
SetCgroupAction(const CgroupController & c,const std::string & p)195 SetCgroupAction::SetCgroupAction(const CgroupController& c, const std::string& p)
196 : controller_(c), path_(p) {
197 InitFd(controller_.GetTasksFilePath(path_));
198 }
199
AddTidToCgroup(int tid,int fd,const char * controller_name)200 bool SetCgroupAction::AddTidToCgroup(int tid, int fd, const char* controller_name) {
201 if (tid <= 0) {
202 return true;
203 }
204
205 std::string value = std::to_string(tid);
206
207 if (TEMP_FAILURE_RETRY(write(fd, value.c_str(), value.length())) == value.length()) {
208 return true;
209 }
210
211 // If the thread is in the process of exiting, don't flag an error
212 if (errno == ESRCH) {
213 return true;
214 }
215
216 // ENOSPC is returned when cpuset cgroup that we are joining has no online cpus
217 if (errno == ENOSPC && !strcmp(controller_name, "cpuset")) {
218 // This is an abnormal case happening only in testing, so report it only once
219 static bool empty_cpuset_reported = false;
220
221 if (empty_cpuset_reported) {
222 return true;
223 }
224
225 LOG(ERROR) << "Failed to add task '" << value
226 << "' into cpuset because all cpus in that cpuset are offline";
227 empty_cpuset_reported = true;
228 } else {
229 PLOG(ERROR) << "AddTidToCgroup failed to write '" << value << "'; fd=" << fd;
230 }
231
232 return false;
233 }
234
ExecuteForProcess(uid_t uid,pid_t pid) const235 bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
236 std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid);
237 unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC)));
238 if (tmp_fd < 0) {
239 PLOG(WARNING) << "Failed to open " << procs_path;
240 return false;
241 }
242 if (!AddTidToCgroup(pid, tmp_fd, controller()->name())) {
243 LOG(ERROR) << "Failed to add task into cgroup";
244 return false;
245 }
246
247 return true;
248 }
249
ExecuteForTask(int tid) const250 bool SetCgroupAction::ExecuteForTask(int tid) const {
251 std::lock_guard<std::mutex> lock(fd_mutex_);
252 if (IsFdValid()) {
253 // fd is cached, reuse it
254 if (!AddTidToCgroup(tid, fd_, controller()->name())) {
255 LOG(ERROR) << "Failed to add task into cgroup";
256 return false;
257 }
258 return true;
259 }
260
261 if (fd_ == FDS_INACCESSIBLE) {
262 // no permissions to access the file, ignore
263 return true;
264 }
265
266 if (fd_ == FDS_APP_DEPENDENT) {
267 // application-dependent path can't be used with tid
268 PLOG(ERROR) << "Application profile can't be applied to a thread";
269 return false;
270 }
271
272 // fd was not cached because cached fd can't be used
273 std::string tasks_path = controller()->GetTasksFilePath(path_);
274 unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC)));
275 if (tmp_fd < 0) {
276 PLOG(WARNING) << "Failed to open " << tasks_path;
277 return false;
278 }
279 if (!AddTidToCgroup(tid, tmp_fd, controller()->name())) {
280 LOG(ERROR) << "Failed to add task into cgroup";
281 return false;
282 }
283
284 return true;
285 }
286
WriteFileAction(const std::string & path,const std::string & value,bool logfailures)287 WriteFileAction::WriteFileAction(const std::string& path, const std::string& value,
288 bool logfailures)
289 : path_(path), value_(value), logfailures_(logfailures) {
290 InitFd(path_);
291 }
292
WriteValueToFile(const std::string & value,const std::string & path,bool logfailures)293 bool WriteFileAction::WriteValueToFile(const std::string& value, const std::string& path,
294 bool logfailures) {
295 // Use WriteStringToFd instead of WriteStringToFile because the latter will open file with
296 // O_TRUNC which causes kernfs_mutex contention
297 unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(path.c_str(), O_WRONLY | O_CLOEXEC)));
298
299 if (tmp_fd < 0) {
300 if (logfailures) PLOG(WARNING) << "Failed to open " << path;
301 return false;
302 }
303
304 if (!WriteStringToFd(value, tmp_fd)) {
305 if (logfailures) PLOG(ERROR) << "Failed to write '" << value << "' to " << path;
306 return false;
307 }
308
309 return true;
310 }
311
ExecuteForProcess(uid_t uid,pid_t pid) const312 bool WriteFileAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
313 std::lock_guard<std::mutex> lock(fd_mutex_);
314 std::string value(value_);
315 std::string path(path_);
316
317 value = StringReplace(value, "<uid>", std::to_string(uid), true);
318 value = StringReplace(value, "<pid>", std::to_string(pid), true);
319 path = StringReplace(path, "<uid>", std::to_string(uid), true);
320 path = StringReplace(path, "<pid>", std::to_string(pid), true);
321
322 return WriteValueToFile(value, path, logfailures_);
323 }
324
ExecuteForTask(int tid) const325 bool WriteFileAction::ExecuteForTask(int tid) const {
326 std::lock_guard<std::mutex> lock(fd_mutex_);
327 std::string value(value_);
328 int uid = getuid();
329
330 value = StringReplace(value, "<uid>", std::to_string(uid), true);
331 value = StringReplace(value, "<pid>", std::to_string(tid), true);
332
333 if (IsFdValid()) {
334 // fd is cached, reuse it
335 if (!WriteStringToFd(value, fd_)) {
336 if (logfailures_) PLOG(ERROR) << "Failed to write '" << value << "' to " << path_;
337 return false;
338 }
339 return true;
340 }
341
342 if (fd_ == FDS_INACCESSIBLE) {
343 // no permissions to access the file, ignore
344 return true;
345 }
346
347 if (fd_ == FDS_APP_DEPENDENT) {
348 // application-dependent path can't be used with tid
349 PLOG(ERROR) << "Application profile can't be applied to a thread";
350 return false;
351 }
352
353 return WriteValueToFile(value, path_, logfailures_);
354 }
355
ExecuteForProcess(uid_t uid,pid_t pid) const356 bool ApplyProfileAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
357 for (const auto& profile : profiles_) {
358 if (!profile->ExecuteForProcess(uid, pid)) {
359 PLOG(WARNING) << "ExecuteForProcess failed for aggregate profile";
360 }
361 }
362 return true;
363 }
364
ExecuteForTask(int tid) const365 bool ApplyProfileAction::ExecuteForTask(int tid) const {
366 for (const auto& profile : profiles_) {
367 profile->ExecuteForTask(tid);
368 }
369 return true;
370 }
371
EnableResourceCaching()372 void ApplyProfileAction::EnableResourceCaching() {
373 for (const auto& profile : profiles_) {
374 profile->EnableResourceCaching();
375 }
376 }
377
DropResourceCaching()378 void ApplyProfileAction::DropResourceCaching() {
379 for (const auto& profile : profiles_) {
380 profile->DropResourceCaching();
381 }
382 }
383
MoveTo(TaskProfile * profile)384 void TaskProfile::MoveTo(TaskProfile* profile) {
385 profile->elements_ = std::move(elements_);
386 profile->res_cached_ = res_cached_;
387 }
388
ExecuteForProcess(uid_t uid,pid_t pid) const389 bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const {
390 for (const auto& element : elements_) {
391 if (!element->ExecuteForProcess(uid, pid)) {
392 return false;
393 }
394 }
395 return true;
396 }
397
ExecuteForTask(int tid) const398 bool TaskProfile::ExecuteForTask(int tid) const {
399 if (tid == 0) {
400 tid = GetThreadId();
401 }
402 for (const auto& element : elements_) {
403 if (!element->ExecuteForTask(tid)) {
404 return false;
405 }
406 }
407 return true;
408 }
409
EnableResourceCaching()410 void TaskProfile::EnableResourceCaching() {
411 if (res_cached_) {
412 return;
413 }
414
415 for (auto& element : elements_) {
416 element->EnableResourceCaching();
417 }
418
419 res_cached_ = true;
420 }
421
DropResourceCaching()422 void TaskProfile::DropResourceCaching() {
423 if (!res_cached_) {
424 return;
425 }
426
427 for (auto& element : elements_) {
428 element->DropResourceCaching();
429 }
430
431 res_cached_ = false;
432 }
433
DropResourceCaching() const434 void TaskProfiles::DropResourceCaching() const {
435 for (auto& iter : profiles_) {
436 iter.second->DropResourceCaching();
437 }
438 }
439
GetInstance()440 TaskProfiles& TaskProfiles::GetInstance() {
441 // Deliberately leak this object to avoid a race between destruction on
442 // process exit and concurrent access from another thread.
443 static auto* instance = new TaskProfiles;
444 return *instance;
445 }
446
TaskProfiles()447 TaskProfiles::TaskProfiles() {
448 // load system task profiles
449 if (!Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_FILE)) {
450 LOG(ERROR) << "Loading " << TASK_PROFILE_DB_FILE << " for [" << getpid() << "] failed";
451 }
452
453 // load API-level specific system task profiles if available
454 unsigned int api_level = GetUintProperty<unsigned int>("ro.product.first_api_level", 0);
455 if (api_level > 0) {
456 std::string api_profiles_path =
457 android::base::StringPrintf(TEMPLATE_TASK_PROFILE_API_FILE, api_level);
458 if (!access(api_profiles_path.c_str(), F_OK) || errno != ENOENT) {
459 if (!Load(CgroupMap::GetInstance(), api_profiles_path)) {
460 LOG(ERROR) << "Loading " << api_profiles_path << " for [" << getpid()
461 << "] failed";
462 }
463 }
464 }
465
466 // load vendor task profiles if the file exists
467 if (!access(TASK_PROFILE_DB_VENDOR_FILE, F_OK) &&
468 !Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_VENDOR_FILE)) {
469 LOG(ERROR) << "Loading " << TASK_PROFILE_DB_VENDOR_FILE << " for [" << getpid()
470 << "] failed";
471 }
472 }
473
Load(const CgroupMap & cg_map,const std::string & file_name)474 bool TaskProfiles::Load(const CgroupMap& cg_map, const std::string& file_name) {
475 std::string json_doc;
476
477 if (!android::base::ReadFileToString(file_name, &json_doc)) {
478 LOG(ERROR) << "Failed to read task profiles from " << file_name;
479 return false;
480 }
481
482 Json::CharReaderBuilder builder;
483 std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
484 Json::Value root;
485 std::string errorMessage;
486 if (!reader->parse(&*json_doc.begin(), &*json_doc.end(), &root, &errorMessage)) {
487 LOG(ERROR) << "Failed to parse task profiles: " << errorMessage;
488 return false;
489 }
490
491 const Json::Value& attr = root["Attributes"];
492 for (Json::Value::ArrayIndex i = 0; i < attr.size(); ++i) {
493 std::string name = attr[i]["Name"].asString();
494 std::string controller_name = attr[i]["Controller"].asString();
495 std::string file_attr = attr[i]["File"].asString();
496
497 auto controller = cg_map.FindController(controller_name);
498 if (controller.HasValue()) {
499 auto iter = attributes_.find(name);
500 if (iter == attributes_.end()) {
501 attributes_[name] = std::make_unique<ProfileAttribute>(controller, file_attr);
502 } else {
503 iter->second->Reset(controller, file_attr);
504 }
505 } else {
506 LOG(WARNING) << "Controller " << controller_name << " is not found";
507 }
508 }
509
510 const Json::Value& profiles_val = root["Profiles"];
511 for (Json::Value::ArrayIndex i = 0; i < profiles_val.size(); ++i) {
512 const Json::Value& profile_val = profiles_val[i];
513
514 std::string profile_name = profile_val["Name"].asString();
515 const Json::Value& actions = profile_val["Actions"];
516 auto profile = std::make_shared<TaskProfile>();
517
518 for (Json::Value::ArrayIndex act_idx = 0; act_idx < actions.size(); ++act_idx) {
519 const Json::Value& action_val = actions[act_idx];
520 std::string action_name = action_val["Name"].asString();
521 const Json::Value& params_val = action_val["Params"];
522 if (action_name == "JoinCgroup") {
523 std::string controller_name = params_val["Controller"].asString();
524 std::string path = params_val["Path"].asString();
525
526 auto controller = cg_map.FindController(controller_name);
527 if (controller.HasValue()) {
528 profile->Add(std::make_unique<SetCgroupAction>(controller, path));
529 } else {
530 LOG(WARNING) << "JoinCgroup: controller " << controller_name << " is not found";
531 }
532 } else if (action_name == "SetTimerSlack") {
533 std::string slack_value = params_val["Slack"].asString();
534 char* end;
535 unsigned long slack;
536
537 slack = strtoul(slack_value.c_str(), &end, 10);
538 if (end > slack_value.c_str()) {
539 profile->Add(std::make_unique<SetTimerSlackAction>(slack));
540 } else {
541 LOG(WARNING) << "SetTimerSlack: invalid parameter: " << slack_value;
542 }
543 } else if (action_name == "SetAttribute") {
544 std::string attr_name = params_val["Name"].asString();
545 std::string attr_value = params_val["Value"].asString();
546
547 auto iter = attributes_.find(attr_name);
548 if (iter != attributes_.end()) {
549 profile->Add(
550 std::make_unique<SetAttributeAction>(iter->second.get(), attr_value));
551 } else {
552 LOG(WARNING) << "SetAttribute: unknown attribute: " << attr_name;
553 }
554 } else if (action_name == "SetClamps") {
555 std::string boost_value = params_val["Boost"].asString();
556 std::string clamp_value = params_val["Clamp"].asString();
557 char* end;
558 unsigned long boost;
559
560 boost = strtoul(boost_value.c_str(), &end, 10);
561 if (end > boost_value.c_str()) {
562 unsigned long clamp = strtoul(clamp_value.c_str(), &end, 10);
563 if (end > clamp_value.c_str()) {
564 profile->Add(std::make_unique<SetClampsAction>(boost, clamp));
565 } else {
566 LOG(WARNING) << "SetClamps: invalid parameter " << clamp_value;
567 }
568 } else {
569 LOG(WARNING) << "SetClamps: invalid parameter: " << boost_value;
570 }
571 } else if (action_name == "WriteFile") {
572 std::string attr_filepath = params_val["FilePath"].asString();
573 std::string attr_value = params_val["Value"].asString();
574 if (!attr_filepath.empty() && !attr_value.empty()) {
575 std::string attr_logfailures = params_val["LogFailures"].asString();
576 bool logfailures = attr_logfailures.empty() || attr_logfailures == "true";
577 profile->Add(std::make_unique<WriteFileAction>(attr_filepath, attr_value,
578 logfailures));
579 } else if (attr_filepath.empty()) {
580 LOG(WARNING) << "WriteFile: invalid parameter: "
581 << "empty filepath";
582 } else if (attr_value.empty()) {
583 LOG(WARNING) << "WriteFile: invalid parameter: "
584 << "empty value";
585 }
586 } else {
587 LOG(WARNING) << "Unknown profile action: " << action_name;
588 }
589 }
590 auto iter = profiles_.find(profile_name);
591 if (iter == profiles_.end()) {
592 profiles_[profile_name] = profile;
593 } else {
594 // Move the content rather that replace the profile because old profile might be
595 // referenced from an aggregate profile if vendor overrides task profiles
596 profile->MoveTo(iter->second.get());
597 profile.reset();
598 }
599 }
600
601 const Json::Value& aggregateprofiles_val = root["AggregateProfiles"];
602 for (Json::Value::ArrayIndex i = 0; i < aggregateprofiles_val.size(); ++i) {
603 const Json::Value& aggregateprofile_val = aggregateprofiles_val[i];
604
605 std::string aggregateprofile_name = aggregateprofile_val["Name"].asString();
606 const Json::Value& aggregateprofiles = aggregateprofile_val["Profiles"];
607 std::vector<std::shared_ptr<TaskProfile>> profiles;
608 bool ret = true;
609
610 for (Json::Value::ArrayIndex pf_idx = 0; pf_idx < aggregateprofiles.size(); ++pf_idx) {
611 std::string profile_name = aggregateprofiles[pf_idx].asString();
612
613 if (profile_name == aggregateprofile_name) {
614 LOG(WARNING) << "AggregateProfiles: recursive profile name: " << profile_name;
615 ret = false;
616 break;
617 } else if (profiles_.find(profile_name) == profiles_.end()) {
618 LOG(WARNING) << "AggregateProfiles: undefined profile name: " << profile_name;
619 ret = false;
620 break;
621 } else {
622 profiles.push_back(profiles_[profile_name]);
623 }
624 }
625 if (ret) {
626 auto profile = std::make_shared<TaskProfile>();
627 profile->Add(std::make_unique<ApplyProfileAction>(profiles));
628 profiles_[aggregateprofile_name] = profile;
629 }
630 }
631
632 return true;
633 }
634
GetProfile(const std::string & name) const635 TaskProfile* TaskProfiles::GetProfile(const std::string& name) const {
636 auto iter = profiles_.find(name);
637
638 if (iter != profiles_.end()) {
639 return iter->second.get();
640 }
641 return nullptr;
642 }
643
GetAttribute(const std::string & name) const644 const ProfileAttribute* TaskProfiles::GetAttribute(const std::string& name) const {
645 auto iter = attributes_.find(name);
646
647 if (iter != attributes_.end()) {
648 return iter->second.get();
649 }
650 return nullptr;
651 }
652
SetProcessProfiles(uid_t uid,pid_t pid,const std::vector<std::string> & profiles)653 bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid,
654 const std::vector<std::string>& profiles) {
655 for (const auto& name : profiles) {
656 TaskProfile* profile = GetProfile(name);
657 if (profile != nullptr) {
658 if (!profile->ExecuteForProcess(uid, pid)) {
659 PLOG(WARNING) << "Failed to apply " << name << " process profile";
660 }
661 } else {
662 PLOG(WARNING) << "Failed to find " << name << "process profile";
663 }
664 }
665 return true;
666 }
667
SetTaskProfiles(int tid,const std::vector<std::string> & profiles,bool use_fd_cache)668 bool TaskProfiles::SetTaskProfiles(int tid, const std::vector<std::string>& profiles,
669 bool use_fd_cache) {
670 for (const auto& name : profiles) {
671 TaskProfile* profile = GetProfile(name);
672 if (profile != nullptr) {
673 if (use_fd_cache) {
674 profile->EnableResourceCaching();
675 }
676 if (!profile->ExecuteForTask(tid)) {
677 PLOG(WARNING) << "Failed to apply " << name << " task profile";
678 }
679 } else {
680 PLOG(WARNING) << "Failed to find " << name << "task profile";
681 }
682 }
683 return true;
684 }
685