blob: 9ee81c1ef06cc8414ee7e67f0d6c1321a1d28ead [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_;
Suren Baghdasaryanbee9f572019-02-05 16:44:22 -0800120#ifdef CACHE_FILE_DESCRIPTORS
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800121 android::base::unique_fd fd_;
Suren Baghdasaryanbee9f572019-02-05 16:44:22 -0800122#endif
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800123
124 static bool IsAppDependentPath(const std::string& path);
125 static bool AddTidToCgroup(int tid, int fd);
126};
127
128class TaskProfile {
129 public:
130 TaskProfile() {}
131
132 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
133
134 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
135 bool ExecuteForTask(int tid) const;
136
137 private:
138 std::vector<std::unique_ptr<ProfileAction>> elements_;
139};
140
141class TaskProfiles {
142 public:
143 // Should be used by all users
144 static TaskProfiles& GetInstance();
145
146 const TaskProfile* GetProfile(const std::string& name) const;
147 const ProfileAttribute* GetAttribute(const std::string& name) const;
148
149 private:
150 std::map<std::string, std::unique_ptr<TaskProfile>> profiles_;
151 std::map<std::string, std::unique_ptr<ProfileAttribute>> attributes_;
152
153 TaskProfiles();
154
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800155 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800156};