blob: 445647dea6933f55fd255668f64d8168c2079183 [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:
Yifan Hong53e0deb2019-03-22 17:01:08 -070030 ProfileAttribute(const CgroupController& controller, const std::string& file_name)
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080031 : controller_(controller), file_name_(file_name) {}
32
Yifan Hong53e0deb2019-03-22 17:01:08 -070033 const CgroupController* controller() const { return &controller_; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080034 const std::string& file_name() const { return file_name_; }
35
36 bool GetPathForTask(int tid, std::string* path) const;
37
38 private:
Yifan Hong53e0deb2019-03-22 17:01:08 -070039 CgroupController controller_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080040 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 Baghdasaryan8a315d22019-02-14 14:40:41 -080051
52 virtual void EnableResourceCaching() {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080053};
54
55// Profile actions
56class SetClampsAction : public ProfileAction {
57 public:
58 SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
59
60 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
61 virtual bool ExecuteForTask(int tid) const;
62
63 protected:
64 int boost_;
65 int clamp_;
66};
67
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080068// To avoid issues in sdk_mac build
69#if defined(__ANDROID__)
70
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080071class SetTimerSlackAction : public ProfileAction {
72 public:
73 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
74
75 virtual bool ExecuteForTask(int tid) const;
76
77 private:
78 unsigned long slack_;
79
80 static bool IsTimerSlackSupported(int tid);
81};
82
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080083#else
84
85class SetTimerSlackAction : public ProfileAction {
86 public:
87 SetTimerSlackAction(unsigned long) noexcept {}
88
89 virtual bool ExecuteForTask(int) const { return true; }
90};
91
92#endif
93
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080094// Set attribute profile element
95class SetAttributeAction : public ProfileAction {
96 public:
97 SetAttributeAction(const ProfileAttribute* attribute, const std::string& value)
98 : attribute_(attribute), value_(value) {}
99
100 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
101 virtual bool ExecuteForTask(int tid) const;
102
103 private:
104 const ProfileAttribute* attribute_;
105 std::string value_;
106};
107
108// Set cgroup profile element
109class SetCgroupAction : public ProfileAction {
110 public:
Yifan Hong53e0deb2019-03-22 17:01:08 -0700111 SetCgroupAction(const CgroupController& c, const std::string& p);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800112
113 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
114 virtual bool ExecuteForTask(int tid) const;
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800115 virtual void EnableResourceCaching();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800116
Yifan Hong53e0deb2019-03-22 17:01:08 -0700117 const CgroupController* controller() const { return &controller_; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800118 std::string path() const { return path_; }
119
120 private:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800121 enum FdState {
122 FDS_INACCESSIBLE = -1,
123 FDS_APP_DEPENDENT = -2,
124 FDS_NOT_CACHED = -3,
125 };
126
Yifan Hong53e0deb2019-03-22 17:01:08 -0700127 CgroupController controller_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800128 std::string path_;
129 android::base::unique_fd fd_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800130
131 static bool IsAppDependentPath(const std::string& path);
132 static bool AddTidToCgroup(int tid, int fd);
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800133
134 bool IsFdValid() const { return fd_ > FDS_INACCESSIBLE; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800135};
136
137class TaskProfile {
138 public:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800139 TaskProfile() : res_cached_(false) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800140
141 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
142
143 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
144 bool ExecuteForTask(int tid) const;
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800145 void EnableResourceCaching();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800146
147 private:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800148 bool res_cached_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800149 std::vector<std::unique_ptr<ProfileAction>> elements_;
150};
151
152class TaskProfiles {
153 public:
154 // Should be used by all users
155 static TaskProfiles& GetInstance();
156
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800157 TaskProfile* GetProfile(const std::string& name) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800158 const ProfileAttribute* GetAttribute(const std::string& name) const;
159
160 private:
161 std::map<std::string, std::unique_ptr<TaskProfile>> profiles_;
162 std::map<std::string, std::unique_ptr<ProfileAttribute>> attributes_;
163
164 TaskProfiles();
165
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800166 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800167};