blob: 684762aa151e71a20bfca20443b32e19f726e5f7 [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#pragma once
18
19#include <sys/cdefs.h>
20#include <sys/types.h>
21#include <map>
22#include <string>
23#include <vector>
24
25#include <android-base/unique_fd.h>
26#include <cgroup_map.h>
27
28class ProfileAttribute {
29 public:
30 ProfileAttribute(const CgroupController* controller, const std::string& file_name)
31 : controller_(controller), file_name_(file_name) {}
32
33 const CgroupController* controller() const { return controller_; }
34 const std::string& file_name() const { return file_name_; }
35
36 bool GetPathForTask(int tid, std::string* path) const;
37
38 private:
39 const CgroupController* controller_;
40 std::string file_name_;
41};
42
43// Abstract profile element
44class ProfileAction {
45 public:
46 virtual ~ProfileAction() {}
47
48 // Default implementations will fail
49 virtual bool ExecuteForProcess(uid_t, pid_t) const { return -1; };
50 virtual bool ExecuteForTask(int) const { return -1; };
51};
52
53// Profile actions
54class SetClampsAction : public ProfileAction {
55 public:
56 SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
57
58 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
59 virtual bool ExecuteForTask(int tid) const;
60
61 protected:
62 int boost_;
63 int clamp_;
64};
65
66class SetTimerSlackAction : public ProfileAction {
67 public:
68 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
69
70 virtual bool ExecuteForTask(int tid) const;
71
72 private:
73 unsigned long slack_;
74
75 static bool IsTimerSlackSupported(int tid);
76};
77
78// Set attribute profile element
79class SetAttributeAction : public ProfileAction {
80 public:
81 SetAttributeAction(const ProfileAttribute* attribute, const std::string& value)
82 : attribute_(attribute), value_(value) {}
83
84 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
85 virtual bool ExecuteForTask(int tid) const;
86
87 private:
88 const ProfileAttribute* attribute_;
89 std::string value_;
90};
91
92// Set cgroup profile element
93class SetCgroupAction : public ProfileAction {
94 public:
95 SetCgroupAction(const CgroupController* c, const std::string& p);
96
97 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
98 virtual bool ExecuteForTask(int tid) const;
99
100 const CgroupController* controller() const { return controller_; }
101 std::string path() const { return path_; }
102
103 private:
104 const CgroupController* controller_;
105 std::string path_;
106 android::base::unique_fd fd_;
107
108 static bool IsAppDependentPath(const std::string& path);
109 static bool AddTidToCgroup(int tid, int fd);
110};
111
112class TaskProfile {
113 public:
114 TaskProfile() {}
115
116 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
117
118 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
119 bool ExecuteForTask(int tid) const;
120
121 private:
122 std::vector<std::unique_ptr<ProfileAction>> elements_;
123};
124
125class TaskProfiles {
126 public:
127 // Should be used by all users
128 static TaskProfiles& GetInstance();
129
130 const TaskProfile* GetProfile(const std::string& name) const;
131 const ProfileAttribute* GetAttribute(const std::string& name) const;
132
133 private:
134 std::map<std::string, std::unique_ptr<TaskProfile>> profiles_;
135 std::map<std::string, std::unique_ptr<ProfileAttribute>> attributes_;
136
137 TaskProfiles();
138
139 bool Load(const CgroupMap& cg_map);
140};