blob: 28bc00c1022d21b9ee55595385f5a50ea8f1774a [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>
mtk1603653f79e62019-05-31 19:05:22 +080022#include <mutex>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080023#include <string>
24#include <vector>
25
26#include <android-base/unique_fd.h>
27#include <cgroup_map.h>
28
29class ProfileAttribute {
30 public:
Yifan Hong53e0deb2019-03-22 17:01:08 -070031 ProfileAttribute(const CgroupController& controller, const std::string& file_name)
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080032 : controller_(controller), file_name_(file_name) {}
33
Yifan Hong53e0deb2019-03-22 17:01:08 -070034 const CgroupController* controller() const { return &controller_; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080035 const std::string& file_name() const { return file_name_; }
36
37 bool GetPathForTask(int tid, std::string* path) const;
38
39 private:
Yifan Hong53e0deb2019-03-22 17:01:08 -070040 CgroupController controller_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080041 std::string file_name_;
42};
43
44// Abstract profile element
45class ProfileAction {
46 public:
47 virtual ~ProfileAction() {}
48
49 // Default implementations will fail
Greg Kaiser5c5ed9c2019-02-04 06:33:26 -080050 virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; };
51 virtual bool ExecuteForTask(int) const { return false; };
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -080052
53 virtual void EnableResourceCaching() {}
Riddle Hsua6abd822019-06-18 15:53:53 -060054 virtual void DropResourceCaching() {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080055};
56
57// Profile actions
58class SetClampsAction : public ProfileAction {
59 public:
60 SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
61
62 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
63 virtual bool ExecuteForTask(int tid) const;
64
65 protected:
66 int boost_;
67 int clamp_;
68};
69
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080070// To avoid issues in sdk_mac build
71#if defined(__ANDROID__)
72
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080073class SetTimerSlackAction : public ProfileAction {
74 public:
75 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
76
77 virtual bool ExecuteForTask(int tid) const;
78
79 private:
80 unsigned long slack_;
81
82 static bool IsTimerSlackSupported(int tid);
83};
84
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080085#else
86
87class SetTimerSlackAction : public ProfileAction {
88 public:
89 SetTimerSlackAction(unsigned long) noexcept {}
90
91 virtual bool ExecuteForTask(int) const { return true; }
92};
93
94#endif
95
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080096// Set attribute profile element
97class SetAttributeAction : public ProfileAction {
98 public:
99 SetAttributeAction(const ProfileAttribute* attribute, const std::string& value)
100 : attribute_(attribute), value_(value) {}
101
102 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
103 virtual bool ExecuteForTask(int tid) const;
104
105 private:
106 const ProfileAttribute* attribute_;
107 std::string value_;
108};
109
110// Set cgroup profile element
111class SetCgroupAction : public ProfileAction {
112 public:
Yifan Hong53e0deb2019-03-22 17:01:08 -0700113 SetCgroupAction(const CgroupController& c, const std::string& p);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800114
115 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
116 virtual bool ExecuteForTask(int tid) const;
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800117 virtual void EnableResourceCaching();
Riddle Hsua6abd822019-06-18 15:53:53 -0600118 virtual void DropResourceCaching();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800119
Yifan Hong53e0deb2019-03-22 17:01:08 -0700120 const CgroupController* controller() const { return &controller_; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800121 std::string path() const { return path_; }
122
123 private:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800124 enum FdState {
125 FDS_INACCESSIBLE = -1,
126 FDS_APP_DEPENDENT = -2,
127 FDS_NOT_CACHED = -3,
128 };
129
Yifan Hong53e0deb2019-03-22 17:01:08 -0700130 CgroupController controller_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800131 std::string path_;
132 android::base::unique_fd fd_;
mtk1603653f79e62019-05-31 19:05:22 +0800133 mutable std::mutex fd_mutex_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800134
135 static bool IsAppDependentPath(const std::string& path);
136 static bool AddTidToCgroup(int tid, int fd);
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800137
138 bool IsFdValid() const { return fd_ > FDS_INACCESSIBLE; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800139};
140
141class TaskProfile {
142 public:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800143 TaskProfile() : res_cached_(false) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800144
145 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
Suren Baghdasaryan84385952020-01-24 16:36:10 -0800146 void MoveTo(TaskProfile* profile);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800147
148 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
149 bool ExecuteForTask(int tid) const;
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800150 void EnableResourceCaching();
Riddle Hsua6abd822019-06-18 15:53:53 -0600151 void DropResourceCaching();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800152
153 private:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800154 bool res_cached_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800155 std::vector<std::unique_ptr<ProfileAction>> elements_;
156};
157
Rick Yiu0b211fa2019-09-16 19:07:17 +0800158// Set aggregate profile element
159class ApplyProfileAction : public ProfileAction {
160 public:
161 ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles)
162 : profiles_(profiles) {}
163
164 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
165 virtual bool ExecuteForTask(int tid) const;
Suren Baghdasaryan911109c2020-02-13 17:28:00 -0800166 virtual void EnableResourceCaching();
167 virtual void DropResourceCaching();
Rick Yiu0b211fa2019-09-16 19:07:17 +0800168
169 private:
170 std::vector<std::shared_ptr<TaskProfile>> profiles_;
171};
172
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800173class TaskProfiles {
174 public:
175 // Should be used by all users
176 static TaskProfiles& GetInstance();
177
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800178 TaskProfile* GetProfile(const std::string& name) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800179 const ProfileAttribute* GetAttribute(const std::string& name) const;
Riddle Hsua6abd822019-06-18 15:53:53 -0600180 void DropResourceCaching() const;
Suren Baghdasaryan911109c2020-02-13 17:28:00 -0800181 bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles);
Rick Yiu0b211fa2019-09-16 19:07:17 +0800182 bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800183
184 private:
Rick Yiu0b211fa2019-09-16 19:07:17 +0800185 std::map<std::string, std::shared_ptr<TaskProfile>> profiles_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800186 std::map<std::string, std::unique_ptr<ProfileAttribute>> attributes_;
187
188 TaskProfiles();
189
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800190 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800191};