blob: 83e74b20b8655711f82eaa30a15b410ecbe58540 [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
Greg Kaiser5c5ed9c2019-02-04 06:33:26 -080049 virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; };
50 virtual bool ExecuteForTask(int) const { return false; };
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080051};
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
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080066// To avoid issues in sdk_mac build
67#if defined(__ANDROID__)
68
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080069class SetTimerSlackAction : public ProfileAction {
70 public:
71 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
72
73 virtual bool ExecuteForTask(int tid) const;
74
75 private:
76 unsigned long slack_;
77
78 static bool IsTimerSlackSupported(int tid);
79};
80
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080081#else
82
83class SetTimerSlackAction : public ProfileAction {
84 public:
85 SetTimerSlackAction(unsigned long) noexcept {}
86
87 virtual bool ExecuteForTask(int) const { return true; }
88};
89
90#endif
91
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080092// Set attribute profile element
93class SetAttributeAction : public ProfileAction {
94 public:
95 SetAttributeAction(const ProfileAttribute* attribute, const std::string& value)
96 : attribute_(attribute), value_(value) {}
97
98 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
99 virtual bool ExecuteForTask(int tid) const;
100
101 private:
102 const ProfileAttribute* attribute_;
103 std::string value_;
104};
105
106// Set cgroup profile element
107class SetCgroupAction : public ProfileAction {
108 public:
109 SetCgroupAction(const CgroupController* c, const std::string& p);
110
111 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
112 virtual bool ExecuteForTask(int tid) const;
113
114 const CgroupController* controller() const { return controller_; }
115 std::string path() const { return path_; }
116
117 private:
118 const CgroupController* controller_;
119 std::string path_;
120 android::base::unique_fd fd_;
121
122 static bool IsAppDependentPath(const std::string& path);
123 static bool AddTidToCgroup(int tid, int fd);
124};
125
126class TaskProfile {
127 public:
128 TaskProfile() {}
129
130 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
131
132 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
133 bool ExecuteForTask(int tid) const;
134
135 private:
136 std::vector<std::unique_ptr<ProfileAction>> elements_;
137};
138
139class TaskProfiles {
140 public:
141 // Should be used by all users
142 static TaskProfiles& GetInstance();
143
144 const TaskProfile* GetProfile(const std::string& name) const;
145 const ProfileAttribute* GetAttribute(const std::string& name) const;
146
147 private:
148 std::map<std::string, std::unique_ptr<TaskProfile>> profiles_;
149 std::map<std::string, std::unique_ptr<ProfileAttribute>> attributes_;
150
151 TaskProfiles();
152
153 bool Load(const CgroupMap& cg_map);
154};