blob: 77bac2d94aba8bbabf9875b60c3f4fed34f56f1a [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() {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080054};
55
56// Profile actions
57class SetClampsAction : public ProfileAction {
58 public:
59 SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {}
60
61 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
62 virtual bool ExecuteForTask(int tid) const;
63
64 protected:
65 int boost_;
66 int clamp_;
67};
68
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080069// To avoid issues in sdk_mac build
70#if defined(__ANDROID__)
71
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080072class SetTimerSlackAction : public ProfileAction {
73 public:
74 SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {}
75
76 virtual bool ExecuteForTask(int tid) const;
77
78 private:
79 unsigned long slack_;
80
81 static bool IsTimerSlackSupported(int tid);
82};
83
Suren Baghdasaryaneca87cb2019-02-02 14:19:41 -080084#else
85
86class SetTimerSlackAction : public ProfileAction {
87 public:
88 SetTimerSlackAction(unsigned long) noexcept {}
89
90 virtual bool ExecuteForTask(int) const { return true; }
91};
92
93#endif
94
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080095// Set attribute profile element
96class SetAttributeAction : public ProfileAction {
97 public:
98 SetAttributeAction(const ProfileAttribute* attribute, const std::string& value)
99 : attribute_(attribute), value_(value) {}
100
101 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
102 virtual bool ExecuteForTask(int tid) const;
103
104 private:
105 const ProfileAttribute* attribute_;
106 std::string value_;
107};
108
109// Set cgroup profile element
110class SetCgroupAction : public ProfileAction {
111 public:
Yifan Hong53e0deb2019-03-22 17:01:08 -0700112 SetCgroupAction(const CgroupController& c, const std::string& p);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800113
114 virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const;
115 virtual bool ExecuteForTask(int tid) const;
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800116 virtual void EnableResourceCaching();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800117
Yifan Hong53e0deb2019-03-22 17:01:08 -0700118 const CgroupController* controller() const { return &controller_; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800119 std::string path() const { return path_; }
120
121 private:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800122 enum FdState {
123 FDS_INACCESSIBLE = -1,
124 FDS_APP_DEPENDENT = -2,
125 FDS_NOT_CACHED = -3,
126 };
127
Yifan Hong53e0deb2019-03-22 17:01:08 -0700128 CgroupController controller_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800129 std::string path_;
130 android::base::unique_fd fd_;
mtk1603653f79e62019-05-31 19:05:22 +0800131 mutable std::mutex fd_mutex_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800132
133 static bool IsAppDependentPath(const std::string& path);
134 static bool AddTidToCgroup(int tid, int fd);
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800135
136 bool IsFdValid() const { return fd_ > FDS_INACCESSIBLE; }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800137};
138
139class TaskProfile {
140 public:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800141 TaskProfile() : res_cached_(false) {}
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800142
143 void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); }
144
145 bool ExecuteForProcess(uid_t uid, pid_t pid) const;
146 bool ExecuteForTask(int tid) const;
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800147 void EnableResourceCaching();
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800148
149 private:
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800150 bool res_cached_;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800151 std::vector<std::unique_ptr<ProfileAction>> elements_;
152};
153
154class TaskProfiles {
155 public:
156 // Should be used by all users
157 static TaskProfiles& GetInstance();
158
Suren Baghdasaryan8a315d22019-02-14 14:40:41 -0800159 TaskProfile* GetProfile(const std::string& name) const;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800160 const ProfileAttribute* GetAttribute(const std::string& name) const;
161
162 private:
163 std::map<std::string, std::unique_ptr<TaskProfile>> profiles_;
164 std::map<std::string, std::unique_ptr<ProfileAttribute>> attributes_;
165
166 TaskProfiles();
167
Suren Baghdasaryan05da67c2019-02-19 15:01:28 -0800168 bool Load(const CgroupMap& cg_map, const std::string& file_name);
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800169};