| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 1 | /* | 
|  | 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> | 
| mtk16036 | 53f79e6 | 2019-05-31 19:05:22 +0800 | [diff] [blame] | 22 | #include <mutex> | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 23 | #include <string> | 
|  | 24 | #include <vector> | 
|  | 25 |  | 
|  | 26 | #include <android-base/unique_fd.h> | 
|  | 27 | #include <cgroup_map.h> | 
|  | 28 |  | 
|  | 29 | class ProfileAttribute { | 
|  | 30 | public: | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 31 | ProfileAttribute(const CgroupController& controller, const std::string& file_name) | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 32 | : controller_(controller), file_name_(file_name) {} | 
|  | 33 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 34 | const CgroupController* controller() const { return &controller_; } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 35 | const std::string& file_name() const { return file_name_; } | 
| Suren Baghdasaryan | 81b9f0b | 2020-07-01 12:34:17 -0700 | [diff] [blame] | 36 | void Reset(const CgroupController& controller, const std::string& file_name); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 37 |  | 
|  | 38 | bool GetPathForTask(int tid, std::string* path) const; | 
|  | 39 |  | 
|  | 40 | private: | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 41 | CgroupController controller_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 42 | std::string file_name_; | 
|  | 43 | }; | 
|  | 44 |  | 
|  | 45 | // Abstract profile element | 
|  | 46 | class ProfileAction { | 
|  | 47 | public: | 
|  | 48 | virtual ~ProfileAction() {} | 
|  | 49 |  | 
|  | 50 | // Default implementations will fail | 
| Greg Kaiser | 5c5ed9c | 2019-02-04 06:33:26 -0800 | [diff] [blame] | 51 | virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; }; | 
|  | 52 | virtual bool ExecuteForTask(int) const { return false; }; | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 53 |  | 
|  | 54 | virtual void EnableResourceCaching() {} | 
| Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 55 | virtual void DropResourceCaching() {} | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 56 | }; | 
|  | 57 |  | 
|  | 58 | // Profile actions | 
|  | 59 | class SetClampsAction : public ProfileAction { | 
|  | 60 | public: | 
|  | 61 | SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {} | 
|  | 62 |  | 
|  | 63 | virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const; | 
|  | 64 | virtual bool ExecuteForTask(int tid) const; | 
|  | 65 |  | 
|  | 66 | protected: | 
|  | 67 | int boost_; | 
|  | 68 | int clamp_; | 
|  | 69 | }; | 
|  | 70 |  | 
| Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 71 | // To avoid issues in sdk_mac build | 
|  | 72 | #if defined(__ANDROID__) | 
|  | 73 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 74 | class SetTimerSlackAction : public ProfileAction { | 
|  | 75 | public: | 
|  | 76 | SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {} | 
|  | 77 |  | 
|  | 78 | virtual bool ExecuteForTask(int tid) const; | 
|  | 79 |  | 
|  | 80 | private: | 
|  | 81 | unsigned long slack_; | 
|  | 82 |  | 
|  | 83 | static bool IsTimerSlackSupported(int tid); | 
|  | 84 | }; | 
|  | 85 |  | 
| Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 86 | #else | 
|  | 87 |  | 
|  | 88 | class SetTimerSlackAction : public ProfileAction { | 
|  | 89 | public: | 
|  | 90 | SetTimerSlackAction(unsigned long) noexcept {} | 
|  | 91 |  | 
|  | 92 | virtual bool ExecuteForTask(int) const { return true; } | 
|  | 93 | }; | 
|  | 94 |  | 
|  | 95 | #endif | 
|  | 96 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 97 | // Set attribute profile element | 
|  | 98 | class SetAttributeAction : public ProfileAction { | 
|  | 99 | public: | 
|  | 100 | SetAttributeAction(const ProfileAttribute* attribute, const std::string& value) | 
|  | 101 | : attribute_(attribute), value_(value) {} | 
|  | 102 |  | 
|  | 103 | virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const; | 
|  | 104 | virtual bool ExecuteForTask(int tid) const; | 
|  | 105 |  | 
|  | 106 | private: | 
|  | 107 | const ProfileAttribute* attribute_; | 
|  | 108 | std::string value_; | 
|  | 109 | }; | 
|  | 110 |  | 
|  | 111 | // Set cgroup profile element | 
|  | 112 | class SetCgroupAction : public ProfileAction { | 
|  | 113 | public: | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 114 | SetCgroupAction(const CgroupController& c, const std::string& p); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 115 |  | 
|  | 116 | virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const; | 
|  | 117 | virtual bool ExecuteForTask(int tid) const; | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 118 | virtual void EnableResourceCaching(); | 
| Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 119 | virtual void DropResourceCaching(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 120 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 121 | const CgroupController* controller() const { return &controller_; } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 122 | std::string path() const { return path_; } | 
|  | 123 |  | 
|  | 124 | private: | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 125 | enum FdState { | 
|  | 126 | FDS_INACCESSIBLE = -1, | 
|  | 127 | FDS_APP_DEPENDENT = -2, | 
|  | 128 | FDS_NOT_CACHED = -3, | 
|  | 129 | }; | 
|  | 130 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 131 | CgroupController controller_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 132 | std::string path_; | 
|  | 133 | android::base::unique_fd fd_; | 
| mtk16036 | 53f79e6 | 2019-05-31 19:05:22 +0800 | [diff] [blame] | 134 | mutable std::mutex fd_mutex_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 135 |  | 
|  | 136 | static bool IsAppDependentPath(const std::string& path); | 
|  | 137 | static bool AddTidToCgroup(int tid, int fd); | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 138 |  | 
|  | 139 | bool IsFdValid() const { return fd_ > FDS_INACCESSIBLE; } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 140 | }; | 
|  | 141 |  | 
|  | 142 | class TaskProfile { | 
|  | 143 | public: | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 144 | TaskProfile() : res_cached_(false) {} | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 145 |  | 
|  | 146 | void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); } | 
| Suren Baghdasaryan | 8438595 | 2020-01-24 16:36:10 -0800 | [diff] [blame] | 147 | void MoveTo(TaskProfile* profile); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 148 |  | 
|  | 149 | bool ExecuteForProcess(uid_t uid, pid_t pid) const; | 
|  | 150 | bool ExecuteForTask(int tid) const; | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 151 | void EnableResourceCaching(); | 
| Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 152 | void DropResourceCaching(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 153 |  | 
|  | 154 | private: | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 155 | bool res_cached_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 156 | std::vector<std::unique_ptr<ProfileAction>> elements_; | 
|  | 157 | }; | 
|  | 158 |  | 
| Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 159 | // Set aggregate profile element | 
|  | 160 | class ApplyProfileAction : public ProfileAction { | 
|  | 161 | public: | 
|  | 162 | ApplyProfileAction(const std::vector<std::shared_ptr<TaskProfile>>& profiles) | 
|  | 163 | : profiles_(profiles) {} | 
|  | 164 |  | 
|  | 165 | virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const; | 
|  | 166 | virtual bool ExecuteForTask(int tid) const; | 
| Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 167 | virtual void EnableResourceCaching(); | 
|  | 168 | virtual void DropResourceCaching(); | 
| Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 169 |  | 
|  | 170 | private: | 
|  | 171 | std::vector<std::shared_ptr<TaskProfile>> profiles_; | 
|  | 172 | }; | 
|  | 173 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 174 | class TaskProfiles { | 
|  | 175 | public: | 
|  | 176 | // Should be used by all users | 
|  | 177 | static TaskProfiles& GetInstance(); | 
|  | 178 |  | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 179 | TaskProfile* GetProfile(const std::string& name) const; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 180 | const ProfileAttribute* GetAttribute(const std::string& name) const; | 
| Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 181 | void DropResourceCaching() const; | 
| Suren Baghdasaryan | 911109c | 2020-02-13 17:28:00 -0800 | [diff] [blame] | 182 | bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles); | 
| Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 183 | bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 184 |  | 
|  | 185 | private: | 
| Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 186 | std::map<std::string, std::shared_ptr<TaskProfile>> profiles_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 187 | std::map<std::string, std::unique_ptr<ProfileAttribute>> attributes_; | 
|  | 188 |  | 
|  | 189 | TaskProfiles(); | 
|  | 190 |  | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 191 | bool Load(const CgroupMap& cg_map, const std::string& file_name); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 192 | }; |