blob: 4e767db52a76b794b5e2b9d0344b921f65fec14f [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>
Rick Yiubc1ad962020-10-26 20:32:52 +080027#include <android-base/strings.h>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080028#include <android-base/threads.h>
29
30#include <cutils/android_filesystem_config.h>
31
32#include <json/reader.h>
33#include <json/value.h>
34
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080035// To avoid issues in sdk_mac build
36#if defined(__ANDROID__)
37#include <sys/prctl.h>
38#endif
39
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080040using android::base::GetThreadId;
41using android::base::StringPrintf;
Rick Yiubc1ad962020-10-26 20:32:52 +080042using android::base::StringReplace;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080043using android::base::unique_fd;
44using android::base::WriteStringToFile;
45
46#define TASK_PROFILE_DB_FILE "/etc/task_profiles.json"
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -080047#define TASK_PROFILE_DB_VENDOR_FILE "/vendor/etc/task_profiles.json"
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080048
Suren Baghdasaryan81b9f0b2020-07-01 12:34:17 -070049void ProfileAttribute::Reset(const CgroupController& controller, const std::string& file_name) {
50 controller_ = controller;
51 file_name_ = file_name;
52}
53
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080054bool ProfileAttribute::GetPathForTask(int tid, std::string* path) const {
55 std::string subgroup;
Yifan Hong53e0deb2019-03-22 17:01:08 -070056 if (!controller()->GetTaskGroup(tid, &subgroup)) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080057 return false;
58 }
59
60 if (path == nullptr) {
61 return true;
62 }
63
64 if (subgroup.empty()) {
Yifan Hong53e0deb2019-03-22 17:01:08 -070065 *path = StringPrintf("%s/%s", controller()->path(), file_name_.c_str());
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080066 } else {
Yifan Hong53e0deb2019-03-22 17:01:08 -070067 *path = StringPrintf("%s/%s/%s", controller()->path(), subgroup.c_str(),
68 file_name_.c_str());
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080069 }
70 return true;
71}
72
73bool SetClampsAction::ExecuteForProcess(uid_t, pid_t) const {
74 // TODO: add support when kernel supports util_clamp
75 LOG(WARNING) << "SetClampsAction::ExecuteForProcess is not supported";
76 return false;
77}
78
79bool SetClampsAction::ExecuteForTask(int) const {
80 // TODO: add support when kernel supports util_clamp
81 LOG(WARNING) << "SetClampsAction::ExecuteForTask is not supported";
82 return false;
83}
84
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080085// To avoid issues in sdk_mac build
86#if defined(__ANDROID__)
87
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080088bool SetTimerSlackAction::IsTimerSlackSupported(int tid) {
89 auto file = StringPrintf("/proc/%d/timerslack_ns", tid);
90
91 return (access(file.c_str(), W_OK) == 0);
92}
93
94bool SetTimerSlackAction::ExecuteForTask(int tid) const {
95 static bool sys_supports_timerslack = IsTimerSlackSupported(tid);
96
97 // v4.6+ kernels support the /proc/<tid>/timerslack_ns interface.
98 // TODO: once we've backported this, log if the open(2) fails.
99 if (sys_supports_timerslack) {
100 auto file = StringPrintf("/proc/%d/timerslack_ns", tid);
101 if (!WriteStringToFile(std::to_string(slack_), file)) {
Suren Baghdasaryan2bc52282019-02-12 17:30:26 -0800102 if (errno == ENOENT) {
103 // This happens when process is already dead
104 return true;
105 }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800106 PLOG(ERROR) << "set_timerslack_ns write failed";
107 }
108 }
109
110 // TODO: Remove when /proc/<tid>/timerslack_ns interface is backported.
111 if (tid == 0 || tid == GetThreadId()) {
112 if (prctl(PR_SET_TIMERSLACK, slack_) == -1) {
113 PLOG(ERROR) << "set_timerslack_ns prctl failed";
114 }
115 }
116
117 return true;
118}
119
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -0800120#endif
121
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800122bool SetAttributeAction::ExecuteForProcess(uid_t, pid_t pid) const {
123 return ExecuteForTask(pid);
124}
125
126bool SetAttributeAction::ExecuteForTask(int tid) const {
127 std::string path;
128
129 if (!attribute_->GetPathForTask(tid, &path)) {
Wei Wangd71d3012019-03-07 11:59:12 -0800130 LOG(ERROR) << "Failed to find cgroup for tid " << tid;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800131 return false;
132 }
133
134 if (!WriteStringToFile(value_, path)) {
135 PLOG(ERROR) << "Failed to write '" << value_ << "' to " << path;
136 return false;
137 }
138
139 return true;
140}
141
142bool SetCgroupAction::IsAppDependentPath(const std::string& path) {
143 return path.find("<uid>", 0) != std::string::npos || path.find("<pid>", 0) != std::string::npos;
144}
145
Yifan Hong53e0deb2019-03-22 17:01:08 -0700146SetCgroupAction::SetCgroupAction(const CgroupController& c, const std::string& p)
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800147 : controller_(c), path_(p) {
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800148 // file descriptors for app-dependent paths can't be cached
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800149 if (IsAppDependentPath(path_)) {
150 // file descriptor is not cached
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800151 fd_.reset(FDS_APP_DEPENDENT);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800152 return;
153 }
154
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800155 // file descriptor can be cached later on request
156 fd_.reset(FDS_NOT_CACHED);
157}
158
159void SetCgroupAction::EnableResourceCaching() {
mtk1603653f79e62019-05-31 19:05:22 +0800160 std::lock_guard<std::mutex> lock(fd_mutex_);
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800161 if (fd_ != FDS_NOT_CACHED) {
162 return;
163 }
164
165 std::string tasks_path = controller_.GetTasksFilePath(path_);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800166
167 if (access(tasks_path.c_str(), W_OK) != 0) {
168 // file is not accessible
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800169 fd_.reset(FDS_INACCESSIBLE);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800170 return;
171 }
172
173 unique_fd fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC)));
174 if (fd < 0) {
175 PLOG(ERROR) << "Failed to cache fd '" << tasks_path << "'";
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800176 fd_.reset(FDS_INACCESSIBLE);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800177 return;
178 }
179
180 fd_ = std::move(fd);
181}
182
Riddle Hsua6abd822019-06-18 15:53:53 -0600183void SetCgroupAction::DropResourceCaching() {
184 std::lock_guard<std::mutex> lock(fd_mutex_);
185 if (fd_ == FDS_NOT_CACHED) {
186 return;
187 }
188
189 fd_.reset(FDS_NOT_CACHED);
190}
191
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800192bool SetCgroupAction::AddTidToCgroup(int tid, int fd) {
193 if (tid <= 0) {
194 return true;
195 }
196
197 std::string value = std::to_string(tid);
198
199 if (TEMP_FAILURE_RETRY(write(fd, value.c_str(), value.length())) < 0) {
200 // If the thread is in the process of exiting, don't flag an error
201 if (errno != ESRCH) {
Wei Wangd71d3012019-03-07 11:59:12 -0800202 PLOG(ERROR) << "AddTidToCgroup failed to write '" << value << "'; fd=" << fd;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800203 return false;
204 }
205 }
206
207 return true;
208}
209
210bool SetCgroupAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
Yifan Hong53e0deb2019-03-22 17:01:08 -0700211 std::string procs_path = controller()->GetProcsFilePath(path_, uid, pid);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800212 unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(procs_path.c_str(), O_WRONLY | O_CLOEXEC)));
213 if (tmp_fd < 0) {
Elliott Hughes08b4d322019-03-14 20:06:36 -0700214 PLOG(WARNING) << "Failed to open " << procs_path;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800215 return false;
216 }
217 if (!AddTidToCgroup(pid, tmp_fd)) {
Wei Wangd71d3012019-03-07 11:59:12 -0800218 LOG(ERROR) << "Failed to add task into cgroup";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800219 return false;
220 }
221
222 return true;
223}
224
225bool SetCgroupAction::ExecuteForTask(int tid) const {
mtk1603653f79e62019-05-31 19:05:22 +0800226 std::lock_guard<std::mutex> lock(fd_mutex_);
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800227 if (IsFdValid()) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800228 // fd is cached, reuse it
229 if (!AddTidToCgroup(tid, fd_)) {
Wei Wangd71d3012019-03-07 11:59:12 -0800230 LOG(ERROR) << "Failed to add task into cgroup";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800231 return false;
232 }
233 return true;
234 }
235
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800236 if (fd_ == FDS_INACCESSIBLE) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800237 // no permissions to access the file, ignore
238 return true;
239 }
240
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800241 if (fd_ == FDS_APP_DEPENDENT) {
242 // application-dependent path can't be used with tid
243 PLOG(ERROR) << "Application profile can't be applied to a thread";
244 return false;
245 }
246
247 // fd was not cached because cached fd can't be used
Yifan Hong53e0deb2019-03-22 17:01:08 -0700248 std::string tasks_path = controller()->GetTasksFilePath(path_);
Suren Baghdasaryanbee9f572019-02-05 16:44:22 -0800249 unique_fd tmp_fd(TEMP_FAILURE_RETRY(open(tasks_path.c_str(), O_WRONLY | O_CLOEXEC)));
250 if (tmp_fd < 0) {
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800251 PLOG(WARNING) << "Failed to open " << tasks_path << ": " << strerror(errno);
252 return false;
Suren Baghdasaryanbee9f572019-02-05 16:44:22 -0800253 }
254 if (!AddTidToCgroup(tid, tmp_fd)) {
Wei Wangd71d3012019-03-07 11:59:12 -0800255 LOG(ERROR) << "Failed to add task into cgroup";
Suren Baghdasaryanbee9f572019-02-05 16:44:22 -0800256 return false;
257 }
258
259 return true;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800260}
261
Rick Yiubc1ad962020-10-26 20:32:52 +0800262bool WriteFileAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
263 std::string filepath(filepath_), value(value_);
264
265 filepath = StringReplace(filepath, "<uid>", std::to_string(uid), true);
266 filepath = StringReplace(filepath, "<pid>", std::to_string(pid), true);
267 value = StringReplace(value, "<uid>", std::to_string(uid), true);
268 value = StringReplace(value, "<pid>", std::to_string(pid), true);
269
270 if (!WriteStringToFile(value, filepath)) {
271 PLOG(ERROR) << "Failed to write '" << value << "' to " << filepath;
272 return false;
273 }
274
275 return true;
276}
277
278bool WriteFileAction::ExecuteForTask(int tid) const {
279 std::string filepath(filepath_), value(value_);
280 int uid = getuid();
281
282 filepath = StringReplace(filepath, "<uid>", std::to_string(uid), true);
283 filepath = StringReplace(filepath, "<pid>", std::to_string(tid), true);
284 value = StringReplace(value, "<uid>", std::to_string(uid), true);
285 value = StringReplace(value, "<pid>", std::to_string(tid), true);
286
287 if (!WriteStringToFile(value, filepath)) {
288 PLOG(ERROR) << "Failed to write '" << value << "' to " << filepath;
289 return false;
290 }
291
292 return true;
293}
294
Rick Yiu0b211fa2019-09-16 19:07:17 +0800295bool ApplyProfileAction::ExecuteForProcess(uid_t uid, pid_t pid) const {
296 for (const auto& profile : profiles_) {
Rick Yiu0b211fa2019-09-16 19:07:17 +0800297 if (!profile->ExecuteForProcess(uid, pid)) {
298 PLOG(WARNING) << "ExecuteForProcess failed for aggregate profile";
299 }
300 }
301 return true;
302}
303
304bool ApplyProfileAction::ExecuteForTask(int tid) const {
305 for (const auto& profile : profiles_) {
Rick Yiu0b211fa2019-09-16 19:07:17 +0800306 if (!profile->ExecuteForTask(tid)) {
307 PLOG(WARNING) << "ExecuteForTask failed for aggregate profile";
308 }
309 }
310 return true;
311}
312
Suren Baghdasaryan911109c2020-02-13 17:28:00 -0800313void ApplyProfileAction::EnableResourceCaching() {
314 for (const auto& profile : profiles_) {
315 profile->EnableResourceCaching();
316 }
317}
318
319void ApplyProfileAction::DropResourceCaching() {
320 for (const auto& profile : profiles_) {
321 profile->DropResourceCaching();
322 }
323}
324
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800325void TaskProfile::MoveTo(TaskProfile* profile) {
326 profile->elements_ = std::move(elements_);
327 profile->res_cached_ = res_cached_;
328}
329
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800330bool TaskProfile::ExecuteForProcess(uid_t uid, pid_t pid) const {
331 for (const auto& element : elements_) {
332 if (!element->ExecuteForProcess(uid, pid)) {
333 return false;
334 }
335 }
336 return true;
337}
338
339bool TaskProfile::ExecuteForTask(int tid) const {
340 if (tid == 0) {
341 tid = GetThreadId();
342 }
343 for (const auto& element : elements_) {
344 if (!element->ExecuteForTask(tid)) {
345 return false;
346 }
347 }
348 return true;
349}
350
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800351void TaskProfile::EnableResourceCaching() {
352 if (res_cached_) {
353 return;
354 }
355
356 for (auto& element : elements_) {
357 element->EnableResourceCaching();
358 }
359
360 res_cached_ = true;
361}
362
Riddle Hsua6abd822019-06-18 15:53:53 -0600363void TaskProfile::DropResourceCaching() {
364 if (!res_cached_) {
365 return;
366 }
367
368 for (auto& element : elements_) {
369 element->DropResourceCaching();
370 }
371
372 res_cached_ = false;
373}
374
375void TaskProfiles::DropResourceCaching() const {
376 for (auto& iter : profiles_) {
377 iter.second->DropResourceCaching();
378 }
379}
380
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800381TaskProfiles& TaskProfiles::GetInstance() {
Peter Collingbournedba6d442019-03-20 21:09:46 -0700382 // Deliberately leak this object to avoid a race between destruction on
383 // process exit and concurrent access from another thread.
384 static auto* instance = new TaskProfiles;
385 return *instance;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800386}
387
388TaskProfiles::TaskProfiles() {
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800389 // load system task profiles
390 if (!Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_FILE)) {
391 LOG(ERROR) << "Loading " << TASK_PROFILE_DB_FILE << " for [" << getpid() << "] failed";
392 }
393
394 // load vendor task profiles if the file exists
395 if (!access(TASK_PROFILE_DB_VENDOR_FILE, F_OK) &&
396 !Load(CgroupMap::GetInstance(), TASK_PROFILE_DB_VENDOR_FILE)) {
397 LOG(ERROR) << "Loading " << TASK_PROFILE_DB_VENDOR_FILE << " for [" << getpid()
398 << "] failed";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800399 }
400}
401
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800402bool TaskProfiles::Load(const CgroupMap& cg_map, const std::string& file_name) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800403 std::string json_doc;
404
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800405 if (!android::base::ReadFileToString(file_name, &json_doc)) {
406 LOG(ERROR) << "Failed to read task profiles from " << file_name;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800407 return false;
408 }
409
410 Json::Reader reader;
411 Json::Value root;
412 if (!reader.parse(json_doc, root)) {
413 LOG(ERROR) << "Failed to parse task profiles: " << reader.getFormattedErrorMessages();
414 return false;
415 }
416
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800417 const Json::Value& attr = root["Attributes"];
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800418 for (Json::Value::ArrayIndex i = 0; i < attr.size(); ++i) {
419 std::string name = attr[i]["Name"].asString();
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800420 std::string controller_name = attr[i]["Controller"].asString();
421 std::string file_attr = attr[i]["File"].asString();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800422
Suren Baghdasaryan81b9f0b2020-07-01 12:34:17 -0700423 auto controller = cg_map.FindController(controller_name);
424 if (controller.HasValue()) {
425 auto iter = attributes_.find(name);
426 if (iter == attributes_.end()) {
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800427 attributes_[name] = std::make_unique<ProfileAttribute>(controller, file_attr);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800428 } else {
Suren Baghdasaryan81b9f0b2020-07-01 12:34:17 -0700429 iter->second->Reset(controller, file_attr);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800430 }
431 } else {
Suren Baghdasaryan81b9f0b2020-07-01 12:34:17 -0700432 LOG(WARNING) << "Controller " << controller_name << " is not found";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800433 }
434 }
435
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800436 const Json::Value& profiles_val = root["Profiles"];
437 for (Json::Value::ArrayIndex i = 0; i < profiles_val.size(); ++i) {
438 const Json::Value& profile_val = profiles_val[i];
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800439
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800440 std::string profile_name = profile_val["Name"].asString();
441 const Json::Value& actions = profile_val["Actions"];
Rick Yiu0b211fa2019-09-16 19:07:17 +0800442 auto profile = std::make_shared<TaskProfile>();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800443
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800444 for (Json::Value::ArrayIndex act_idx = 0; act_idx < actions.size(); ++act_idx) {
445 const Json::Value& action_val = actions[act_idx];
446 std::string action_name = action_val["Name"].asString();
447 const Json::Value& params_val = action_val["Params"];
448 if (action_name == "JoinCgroup") {
449 std::string controller_name = params_val["Controller"].asString();
450 std::string path = params_val["Path"].asString();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800451
Yifan Hong53e0deb2019-03-22 17:01:08 -0700452 auto controller = cg_map.FindController(controller_name);
453 if (controller.HasValue()) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800454 profile->Add(std::make_unique<SetCgroupAction>(controller, path));
455 } else {
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800456 LOG(WARNING) << "JoinCgroup: controller " << controller_name << " is not found";
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800457 }
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800458 } else if (action_name == "SetTimerSlack") {
459 std::string slack_value = params_val["Slack"].asString();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800460 char* end;
461 unsigned long slack;
462
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800463 slack = strtoul(slack_value.c_str(), &end, 10);
464 if (end > slack_value.c_str()) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800465 profile->Add(std::make_unique<SetTimerSlackAction>(slack));
466 } else {
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800467 LOG(WARNING) << "SetTimerSlack: invalid parameter: " << slack_value;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800468 }
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800469 } else if (action_name == "SetAttribute") {
470 std::string attr_name = params_val["Name"].asString();
471 std::string attr_value = params_val["Value"].asString();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800472
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800473 auto iter = attributes_.find(attr_name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800474 if (iter != attributes_.end()) {
475 profile->Add(
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800476 std::make_unique<SetAttributeAction>(iter->second.get(), attr_value));
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800477 } else {
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800478 LOG(WARNING) << "SetAttribute: unknown attribute: " << attr_name;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800479 }
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800480 } else if (action_name == "SetClamps") {
481 std::string boost_value = params_val["Boost"].asString();
482 std::string clamp_value = params_val["Clamp"].asString();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800483 char* end;
484 unsigned long boost;
485
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800486 boost = strtoul(boost_value.c_str(), &end, 10);
487 if (end > boost_value.c_str()) {
488 unsigned long clamp = strtoul(clamp_value.c_str(), &end, 10);
489 if (end > clamp_value.c_str()) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800490 profile->Add(std::make_unique<SetClampsAction>(boost, clamp));
491 } else {
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800492 LOG(WARNING) << "SetClamps: invalid parameter " << clamp_value;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800493 }
494 } else {
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800495 LOG(WARNING) << "SetClamps: invalid parameter: " << boost_value;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800496 }
Rick Yiubc1ad962020-10-26 20:32:52 +0800497 } else if (action_name == "WriteFile") {
498 std::string attr_filepath = params_val["FilePath"].asString();
499 std::string attr_value = params_val["Value"].asString();
500 if (!attr_filepath.empty() && !attr_value.empty()) {
501 profile->Add(std::make_unique<WriteFileAction>(attr_filepath, attr_value));
502 } else if (attr_filepath.empty()) {
503 LOG(WARNING) << "WriteFile: invalid parameter: "
504 << "empty filepath";
505 } else if (attr_value.empty()) {
506 LOG(WARNING) << "WriteFile: invalid parameter: "
507 << "empty value";
508 }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800509 } else {
Suren Baghdasaryane681df42019-02-20 16:17:22 -0800510 LOG(WARNING) << "Unknown profile action: " << action_name;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800511 }
512 }
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800513 auto iter = profiles_.find(profile_name);
514 if (iter == profiles_.end()) {
515 profiles_[profile_name] = profile;
516 } else {
517 // Move the content rather that replace the profile because old profile might be
518 // referenced from an aggregate profile if vendor overrides task profiles
519 profile->MoveTo(iter->second.get());
520 profile.reset();
521 }
Rick Yiu0b211fa2019-09-16 19:07:17 +0800522 }
523
524 const Json::Value& aggregateprofiles_val = root["AggregateProfiles"];
525 for (Json::Value::ArrayIndex i = 0; i < aggregateprofiles_val.size(); ++i) {
526 const Json::Value& aggregateprofile_val = aggregateprofiles_val[i];
527
528 std::string aggregateprofile_name = aggregateprofile_val["Name"].asString();
529 const Json::Value& aggregateprofiles = aggregateprofile_val["Profiles"];
530 std::vector<std::shared_ptr<TaskProfile>> profiles;
531 bool ret = true;
532
533 for (Json::Value::ArrayIndex pf_idx = 0; pf_idx < aggregateprofiles.size(); ++pf_idx) {
534 std::string profile_name = aggregateprofiles[pf_idx].asString();
535
536 if (profile_name == aggregateprofile_name) {
537 LOG(WARNING) << "AggregateProfiles: recursive profile name: " << profile_name;
538 ret = false;
539 break;
540 } else if (profiles_.find(profile_name) == profiles_.end()) {
541 LOG(WARNING) << "AggregateProfiles: undefined profile name: " << profile_name;
542 ret = false;
543 break;
544 } else {
545 profiles.push_back(profiles_[profile_name]);
546 }
547 }
548 if (ret) {
549 auto profile = std::make_shared<TaskProfile>();
550 profile->Add(std::make_unique<ApplyProfileAction>(profiles));
551 profiles_[aggregateprofile_name] = profile;
552 }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800553 }
554
555 return true;
556}
557
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800558TaskProfile* TaskProfiles::GetProfile(const std::string& name) const {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800559 auto iter = profiles_.find(name);
560
561 if (iter != profiles_.end()) {
562 return iter->second.get();
563 }
564 return nullptr;
565}
566
567const ProfileAttribute* TaskProfiles::GetAttribute(const std::string& name) const {
568 auto iter = attributes_.find(name);
569
570 if (iter != attributes_.end()) {
571 return iter->second.get();
572 }
573 return nullptr;
574}
Rick Yiu0b211fa2019-09-16 19:07:17 +0800575
576bool TaskProfiles::SetProcessProfiles(uid_t uid, pid_t pid,
Suren Baghdasaryan911109c2020-02-13 17:28:00 -0800577 const std::vector<std::string>& profiles) {
Rick Yiu0b211fa2019-09-16 19:07:17 +0800578 for (const auto& name : profiles) {
579 TaskProfile* profile = GetProfile(name);
580 if (profile != nullptr) {
Rick Yiu0b211fa2019-09-16 19:07:17 +0800581 if (!profile->ExecuteForProcess(uid, pid)) {
582 PLOG(WARNING) << "Failed to apply " << name << " process profile";
583 }
584 } else {
585 PLOG(WARNING) << "Failed to find " << name << "process profile";
586 }
587 }
588 return true;
589}
590
591bool TaskProfiles::SetTaskProfiles(int tid, const std::vector<std::string>& profiles,
592 bool use_fd_cache) {
593 for (const auto& name : profiles) {
594 TaskProfile* profile = GetProfile(name);
595 if (profile != nullptr) {
596 if (use_fd_cache) {
597 profile->EnableResourceCaching();
598 }
599 if (!profile->ExecuteForTask(tid)) {
600 PLOG(WARNING) << "Failed to apply " << name << " task profile";
601 }
602 } else {
603 PLOG(WARNING) << "Failed to find " << name << "task profile";
604 }
605 }
606 return true;
607}