blob: 72f01af6790f14c690983ffa688e950d1de8a62c [file] [log] [blame]
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -08001/*
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 Baghdasaryan82b72a52018-12-21 11:41:50 -080021#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 Baghdasaryaneca87cb2019-02-02 14:19:41 -080034// To avoid issues in sdk_mac build
35#if defined(__ANDROID__)
36#include <sys/prctl.h>
37#endif
38
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080039using android::base::GetThreadId;
40using android::base::StringPrintf;
41using android::base::unique_fd;
42using android::base::WriteStringToFile;
43
44#define TASK_PROFILE_DB_FILE "/etc/task_profiles.json"
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -080045#define TASK_PROFILE_DB_VENDOR_FILE "/vendor/etc/task_profiles.json"
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080046
47bool ProfileAttribute::GetPathForTask(int tid, std::string* path) const {
48 std::string subgroup;
Yifan Hong53e0deb2019-03-22 17:01:08 -070049 if (!controller()->GetTaskGroup(tid, &subgroup)) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080050 return false;
51 }
52
53 if (path == nullptr) {
54 return true;
55 }
56
57 if (subgroup.empty()) {
Yifan Hong53e0deb2019-03-22 17:01:08 -070058 *path = StringPrintf("%s/%s", controller()->path(), file_name_.c_str());
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080059 } else {
Yifan Hong53e0deb2019-03-22 17:01:08 -070060 *path = StringPrintf("%s/%s/%s", controller()->path(), subgroup.c_str(),
61 file_name_.c_str());
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080062 }
63 return true;
64}
65
66bool SetClampsAction::ExecuteForProcess(uid_t, pid_t) const {
67 // TODO: add support when kernel supports util_clamp
68 LOG(WARNING) << "SetClampsAction::ExecuteForProcess is not supported";
69 return false;
70}
71
72bool SetClampsAction::ExecuteForTask(int) const {
73 // TODO: add support when kernel supports util_clamp
74 LOG(WARNING) << "SetClampsAction::ExecuteForTask is not supported";
75 return false;
76}
77
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080078// To avoid issues in sdk_mac build
79#if defined(__ANDROID__)
80
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080081bool SetTimerSlackAction::IsTimerSlackSupported(int tid) {
82 auto file = StringPrintf("/proc/%d/timerslack_ns", tid);
83
84 return (access(file.c_str(), W_OK) == 0);
85}
86
87bool SetTimerSlackAction::ExecuteForTask(int tid) const {
88 static bool sys_supports_timerslack = IsTimerSlackSupported(tid);
89
90 // v4.6+ kernels support the /proc/<tid>/timerslack_ns interface.
91 // TODO: once we've backported this, log if the open(2) fails.
92 if (sys_supports_timerslack) {
93 auto file = StringPrintf("/proc/%d/timerslack_ns", tid);
94 if (!WriteStringToFile(std::to_string(slack_), file)) {
Suren Baghdasaryan2bc52282019-02-12 17:30:26 -080095 if (errno == ENOENT) {
96 // This happens when process is already dead
97 return true;
98 }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080099 PLOG(ERROR) << "set_timerslack_ns write failed";
100 }
101 }
102
103 // TODO: Remove when /proc/<tid>/timerslack_ns interface is backported.
104 if (tid == 0 || tid == GetThreadId()) {
105 if (prctl(PR_SET_TIMERSLACK, slack_) == -1) {
106 PLOG(ERROR) << "set_timerslack_ns prctl failed";
107 }
108 }
109
110 return true;
111}
112
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -0800113#endif
114
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800115bool SetAttributeAction::ExecuteForProcess(uid_t, pid_t pid) const {
116 return ExecuteForTask(pid);
117}
118
119bool SetAttributeAction::ExecuteForTask(int tid) const {
120 std::string path;
121
122 if (!attribute_->GetPathForTask(tid, &path)) {
Wei Wangd71d3012019-03-07 11:59:12 -0800123 LOG(ERROR) << "Failed to find cgroup for tid " << tid;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800124 return false;
125 }
126
127 if (!WriteStringToFile(value_, path)) {
128 PLOG(ERROR) << "Failed to write '" << value_ << "' to " << path;
129 return false;
130 }
131
132 return true;
133}
134
135bool SetCgroupAction::IsAppDependentPath(const std::string& path) {
136 return path.find("<uid>", 0) != std::string::npos || path.find("<pid>", 0) != std::string::npos;
137}
138
Yifan Hong53e0deb2019-03-22 17:01:08 -0700139SetCgroupAction::SetCgroupAction(const CgroupController& c, const std::string& p)
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800140 : controller_(c), path_(p) {
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800141 // file descriptors for app-dependent paths can't be cached
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800142 if (IsAppDependentPath(path_)) {
143 // file descriptor is not cached
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800144 fd_.reset(FDS_APP_DEPENDENT);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800145 return;
146 }
147
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800148 // file descriptor can be cached later on request
149 fd_.reset(FDS_NOT_CACHED);
150}
151
152void SetCgroupAction::EnableResourceCaching() {
mtk1603653f79e62019-05-31 19:05:22 +0800153 std::lock_guard<std::mutex> lock(fd_mutex_);
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800154 if (fd_ != FDS_NOT_CACHED) {
155 return;
156 }
157
158 std::string tasks_path = controller_.GetTasksFilePath(path_);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800159
160 if (access(tasks_path.c_str(), W_OK) != 0) {
161 // file is not accessible
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800162 fd_.reset(FDS_INACCESSIBLE);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800163 return;
164 }
165
166 unique_fd fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC)));
167 if (fd < 0) {
168 PLOG(ERROR) << "Failed to cache fd '" << tasks_path << "'";
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800169 fd_.reset(FDS_INACCESSIBLE);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800170 return;
171 }
172
173 fd_ = std::move(fd);
174}
175
Riddle Hsua6abd822019-06-18 15:53:53 -0600176void SetCgroupAction::DropResourceCaching() {
177 std::lock_guard<std::mutex> lock(fd_mutex_);
178 if (fd_ == FDS_NOT_CACHED) {
179 return;
180 }
181
182 fd_.reset(FDS_NOT_CACHED);
183}
184
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800185bool SetCgroupAction::AddTidToCgroup(int tid, int fd) {
186 if (tid <= 0) {
187 return true;
188 }
189
190 std::string value = std::to_string(tid);
191
192 if (TEMP_FAILURE_RETRY(write(fd, value.c_str(), value.length())) < 0) {
193 // If the thread is in the process of exiting, don't flag an error
194 if (errno != ESRCH) {
Wei Wangd71d3012019-03-07 11:59:12 -0800195 PLOG(ERROR) << "AddTidToCgroup failed to write '" << value << "'; fd=" << fd;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800196 return false;
197 }
198 }
199
200 return true;
201}
202
203bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
mtk1603653f79e62019-05-31 19:05:22 +0800204 std::lock_guard<std::mutex> lock(fd_mutex_);
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800205 if (IsFdValid()) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800206 // fd is cached, reuse it
207 if (!AddTidToCgroup(pid, fd_)) {
Wei Wangd71d3012019-03-07 11:59:12 -0800208 LOG(ERROR) << "Failed to add task into cgroup";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800209 return false;
210 }
211 return true;
212 }
213
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800214 if (fd_ == FDS_INACCESSIBLE) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800215 // no permissions to access the file, ignore
216 return true;
217 }
218
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800219 // this is app-dependent path and fd is not cached or cached fd can't be used
Yifan Hong53e0deb2019-03-22 17:01:08 -0700220 std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800221 unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC)));
222 if (tmp_fd < 0) {
Elliott Hughes08b4d322019-03-14 20:06:36 -0700223 PLOG(WARNING) << "Failed to open " << procs_path;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800224 return false;
225 }
226 if (!AddTidToCgroup(pid, tmp_fd)) {
Wei Wangd71d3012019-03-07 11:59:12 -0800227 LOG(ERROR) << "Failed to add task into cgroup";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800228 return false;
229 }
230
231 return true;
232}
233
234bool SetCgroupAction::ExecuteForTask(int tid) const {
mtk1603653f79e62019-05-31 19:05:22 +0800235 std::lock_guard<std::mutex> lock(fd_mutex_);
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800236 if (IsFdValid()) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800237 // fd is cached, reuse it
238 if (!AddTidToCgroup(tid, fd_)) {
Wei Wangd71d3012019-03-07 11:59:12 -0800239 LOG(ERROR) << "Failed to add task into cgroup";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800240 return false;
241 }
242 return true;
243 }
244
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800245 if (fd_ == FDS_INACCESSIBLE) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800246 // no permissions to access the file, ignore
247 return true;
248 }
249
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800250 if (fd_ == FDS_APP_DEPENDENT) {
251 // application-dependent path can't be used with tid
252 PLOG(ERROR) << "Application profile can't be applied to a thread";
253 return false;
254 }
255
256 // fd was not cached because cached fd can't be used
Yifan Hong53e0deb2019-03-22 17:01:08 -0700257 std::string tasks_path = controller()->GetTasksFilePath(path_);
Suren Baghdasaryanbee9f572019-02-05 16:44:22 -0800258 unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC)));
259 if (tmp_fd < 0) {
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800260 PLOG(WARNING) << "Failed to open " << tasks_path << ": " << strerror(errno);
261 return false;
Suren Baghdasaryanbee9f572019-02-05 16:44:22 -0800262 }
263 if (!AddTidToCgroup(tid, tmp_fd)) {
Wei Wangd71d3012019-03-07 11:59:12 -0800264 LOG(ERROR) << "Failed to add task into cgroup";
Suren Baghdasaryanbee9f572019-02-05 16:44:22 -0800265 return false;
266 }
267
268 return true;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800269}
270
Rick Yiu0b211fa2019-09-16 19:07:17 +0800271bool ApplyProfileAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
272 for (const auto& profile : profiles_) {
273 profile->EnableResourceCaching();
274 if (!profile->ExecuteForProcess(uid, pid)) {
275 PLOG(WARNING) << "ExecuteForProcess failed for aggregate profile";
276 }
277 }
278 return true;
279}
280
281bool ApplyProfileAction::ExecuteForTask(int tid) const {
282 for (const auto& profile : profiles_) {
283 profile->EnableResourceCaching();
284 if (!profile->ExecuteForTask(tid)) {
285 PLOG(WARNING) << "ExecuteForTask failed for aggregate profile";
286 }
287 }
288 return true;
289}
290
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800291void TaskProfile::MoveTo(TaskProfile* profile) {
292 profile->elements_ = std::move(elements_);
293 profile->res_cached_ = res_cached_;
294}
295
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800296bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const {
297 for (const auto& element : elements_) {
298 if (!element->ExecuteForProcess(uid, pid)) {
299 return false;
300 }
301 }
302 return true;
303}
304
305bool TaskProfile::ExecuteForTask(int tid) const {
306 if (tid == 0) {
307 tid = GetThreadId();
308 }
309 for (const auto& element : elements_) {
310 if (!element->ExecuteForTask(tid)) {
311 return false;
312 }
313 }
314 return true;
315}
316
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800317void TaskProfile::EnableResourceCaching() {
318 if (res_cached_) {
319 return;
320 }
321
322 for (auto& element : elements_) {
323 element->EnableResourceCaching();
324 }
325
326 res_cached_ = true;
327}
328
Riddle Hsua6abd822019-06-18 15:53:53 -0600329void TaskProfile::DropResourceCaching() {
330 if (!res_cached_) {
331 return;
332 }
333
334 for (auto& element : elements_) {
335 element->DropResourceCaching();
336 }
337
338 res_cached_ = false;
339}
340
341void TaskProfiles::DropResourceCaching() const {
342 for (auto& iter : profiles_) {
343 iter.second->DropResourceCaching();
344 }
345}
346
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800347TaskProfiles& TaskProfiles::GetInstance() {
Peter Collingbournedba6d442019-03-20 21:09:46 -0700348 // Deliberately leak this object to avoid a race between destruction on
349 // process exit and concurrent access from another thread.
350 static auto* instance = new TaskProfiles;
351 return *instance;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800352}
353
354TaskProfiles::TaskProfiles() {
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800355 // load system task profiles
356 if (!Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_FILE)) {
357 LOG(ERROR) << "Loading " << TASK_PROFILE_DB_FILE << " for [" << getpid() << "] failed";
358 }
359
360 // load vendor task profiles if the file exists
361 if (!access(TASK_PROFILE_DB_VENDOR_FILE, F_OK) &&
362 !Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_VENDOR_FILE)) {
363 LOG(ERROR) << "Loading " << TASK_PROFILE_DB_VENDOR_FILE << " for [" << getpid()
364 << "] failed";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800365 }
366}
367
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800368bool TaskProfiles::Load(const CgroupMap& cg_map, const std::string& file_name) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800369 std::string json_doc;
370
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800371 if (!android::base::ReadFileToString(file_name, &json_doc)) {
372 LOG(ERROR) << "Failed to read task profiles from " << file_name;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800373 return false;
374 }
375
376 Json::Reader reader;
377 Json::Value root;
378 if (!reader.parse(json_doc, root)) {
379 LOG(ERROR) << "Failed to parse task profiles: " << reader.getFormattedErrorMessages();
380 return false;
381 }
382
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800383 const Json::Value& attr = root["Attributes"];
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800384 for (Json::Value::ArrayIndex i = 0; i < attr.size(); ++i) {
385 std::string name = attr[i]["Name"].asString();
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800386 std::string controller_name = attr[i]["Controller"].asString();
387 std::string file_attr = attr[i]["File"].asString();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800388
389 if (attributes_.find(name) == attributes_.end()) {
Yifan Hong53e0deb2019-03-22 17:01:08 -0700390 auto controller = cg_map.FindController(controller_name);
391 if (controller.HasValue()) {
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800392 attributes_[name] = std::make_unique<ProfileAttribute>(controller, file_attr);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800393 } else {
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800394 LOG(WARNING) << "Controller " << controller_name << " is not found";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800395 }
396 } else {
397 LOG(WARNING) << "Attribute " << name << " is already defined";
398 }
399 }
400
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800401 const Json::Value& profiles_val = root["Profiles"];
402 for (Json::Value::ArrayIndex i = 0; i < profiles_val.size(); ++i) {
403 const Json::Value& profile_val = profiles_val[i];
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800404
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800405 std::string profile_name = profile_val["Name"].asString();
406 const Json::Value& actions = profile_val["Actions"];
Rick Yiu0b211fa2019-09-16 19:07:17 +0800407 auto profile = std::make_shared<TaskProfile>();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800408
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800409 for (Json::Value::ArrayIndex act_idx = 0; act_idx < actions.size(); ++act_idx) {
410 const Json::Value& action_val = actions[act_idx];
411 std::string action_name = action_val["Name"].asString();
412 const Json::Value& params_val = action_val["Params"];
413 if (action_name == "JoinCgroup") {
414 std::string controller_name = params_val["Controller"].asString();
415 std::string path = params_val["Path"].asString();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800416
Yifan Hong53e0deb2019-03-22 17:01:08 -0700417 auto controller = cg_map.FindController(controller_name);
418 if (controller.HasValue()) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800419 profile->Add(std::make_unique<SetCgroupAction>(controller, path));
420 } else {
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800421 LOG(WARNING) << "JoinCgroup: controller " << controller_name << " is not found";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800422 }
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800423 } else if (action_name == "SetTimerSlack") {
424 std::string slack_value = params_val["Slack"].asString();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800425 char* end;
426 unsigned long slack;
427
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800428 slack = strtoul(slack_value.c_str(), &end, 10);
429 if (end > slack_value.c_str()) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800430 profile->Add(std::make_unique<SetTimerSlackAction>(slack));
431 } else {
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800432 LOG(WARNING) << "SetTimerSlack: invalid parameter: " << slack_value;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800433 }
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800434 } else if (action_name == "SetAttribute") {
435 std::string attr_name = params_val["Name"].asString();
436 std::string attr_value = params_val["Value"].asString();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800437
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800438 auto iter = attributes_.find(attr_name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800439 if (iter != attributes_.end()) {
440 profile->Add(
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800441 std::make_unique<SetAttributeAction>(iter->second.get(), attr_value));
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800442 } else {
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800443 LOG(WARNING) << "SetAttribute: unknown attribute: " << attr_name;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800444 }
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800445 } else if (action_name == "SetClamps") {
446 std::string boost_value = params_val["Boost"].asString();
447 std::string clamp_value = params_val["Clamp"].asString();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800448 char* end;
449 unsigned long boost;
450
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800451 boost = strtoul(boost_value.c_str(), &end, 10);
452 if (end > boost_value.c_str()) {
453 unsigned long clamp = strtoul(clamp_value.c_str(), &end, 10);
454 if (end > clamp_value.c_str()) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800455 profile->Add(std::make_unique<SetClampsAction>(boost, clamp));
456 } else {
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800457 LOG(WARNING) << "SetClamps: invalid parameter " << clamp_value;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800458 }
459 } else {
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800460 LOG(WARNING) << "SetClamps: invalid parameter: " << boost_value;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800461 }
462 } else {
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800463 LOG(WARNING) << "Unknown profile action: " << action_name;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800464 }
465 }
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800466 auto iter = profiles_.find(profile_name);
467 if (iter == profiles_.end()) {
468 profiles_[profile_name] = profile;
469 } else {
470 // Move the content rather that replace the profile because old profile might be
471 // referenced from an aggregate profile if vendor overrides task profiles
472 profile->MoveTo(iter->second.get());
473 profile.reset();
474 }
Rick Yiu0b211fa2019-09-16 19:07:17 +0800475 }
476
477 const Json::Value& aggregateprofiles_val = root["AggregateProfiles"];
478 for (Json::Value::ArrayIndex i = 0; i < aggregateprofiles_val.size(); ++i) {
479 const Json::Value& aggregateprofile_val = aggregateprofiles_val[i];
480
481 std::string aggregateprofile_name = aggregateprofile_val["Name"].asString();
482 const Json::Value& aggregateprofiles = aggregateprofile_val["Profiles"];
483 std::vector<std::shared_ptr<TaskProfile>> profiles;
484 bool ret = true;
485
486 for (Json::Value::ArrayIndex pf_idx = 0; pf_idx < aggregateprofiles.size(); ++pf_idx) {
487 std::string profile_name = aggregateprofiles[pf_idx].asString();
488
489 if (profile_name == aggregateprofile_name) {
490 LOG(WARNING) << "AggregateProfiles: recursive profile name: " << profile_name;
491 ret = false;
492 break;
493 } else if (profiles_.find(profile_name) == profiles_.end()) {
494 LOG(WARNING) << "AggregateProfiles: undefined profile name: " << profile_name;
495 ret = false;
496 break;
497 } else {
498 profiles.push_back(profiles_[profile_name]);
499 }
500 }
501 if (ret) {
502 auto profile = std::make_shared<TaskProfile>();
503 profile->Add(std::make_unique<ApplyProfileAction>(profiles));
504 profiles_[aggregateprofile_name] = profile;
505 }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800506 }
507
508 return true;
509}
510
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800511TaskProfile* TaskProfiles::GetProfile(const std::string& name) const {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800512 auto iter = profiles_.find(name);
513
514 if (iter != profiles_.end()) {
515 return iter->second.get();
516 }
517 return nullptr;
518}
519
520const ProfileAttribute* TaskProfiles::GetAttribute(const std::string& name) const {
521 auto iter = attributes_.find(name);
522
523 if (iter != attributes_.end()) {
524 return iter->second.get();
525 }
526 return nullptr;
527}
Rick Yiu0b211fa2019-09-16 19:07:17 +0800528
529bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid,
530 const std::vector<std::string>& profiles, bool use_fd_cache) {
531 for (const auto& name : profiles) {
532 TaskProfile* profile = GetProfile(name);
533 if (profile != nullptr) {
534 if (use_fd_cache) {
535 profile->EnableResourceCaching();
536 }
537 if (!profile->ExecuteForProcess(uid, pid)) {
538 PLOG(WARNING) << "Failed to apply " << name << " process profile";
539 }
540 } else {
541 PLOG(WARNING) << "Failed to find " << name << "process profile";
542 }
543 }
544 return true;
545}
546
547bool TaskProfiles::SetTaskProfiles(int tid, const std::vector<std::string>& profiles,
548 bool use_fd_cache) {
549 for (const auto& name : profiles) {
550 TaskProfile* profile = GetProfile(name);
551 if (profile != nullptr) {
552 if (use_fd_cache) {
553 profile->EnableResourceCaching();
554 }
555 if (!profile->ExecuteForTask(tid)) {
556 PLOG(WARNING) << "Failed to apply " << name << " task profile";
557 }
558 } else {
559 PLOG(WARNING) << "Failed to find " << name << "task profile";
560 }
561 }
562 return true;
563}