| 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_; } | 
|  | 36 |  | 
|  | 37 | bool GetPathForTask(int tid, std::string* path) const; | 
|  | 38 |  | 
|  | 39 | private: | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 40 | CgroupController controller_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 41 | std::string file_name_; | 
|  | 42 | }; | 
|  | 43 |  | 
|  | 44 | // Abstract profile element | 
|  | 45 | class ProfileAction { | 
|  | 46 | public: | 
|  | 47 | virtual ~ProfileAction() {} | 
|  | 48 |  | 
|  | 49 | // Default implementations will fail | 
| Greg Kaiser | 5c5ed9c | 2019-02-04 06:33:26 -0800 | [diff] [blame] | 50 | virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; }; | 
|  | 51 | virtual bool ExecuteForTask(int) const { return false; }; | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 52 |  | 
|  | 53 | virtual void EnableResourceCaching() {} | 
| Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 54 | virtual void DropResourceCaching() {} | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 55 | }; | 
|  | 56 |  | 
|  | 57 | // Profile actions | 
|  | 58 | class 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 Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 70 | // To avoid issues in sdk_mac build | 
|  | 71 | #if defined(__ANDROID__) | 
|  | 72 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 73 | class 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 Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 85 | #else | 
|  | 86 |  | 
|  | 87 | class 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 Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 96 | // Set attribute profile element | 
|  | 97 | class 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 | 
|  | 111 | class SetCgroupAction : public ProfileAction { | 
|  | 112 | public: | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 113 | SetCgroupAction(const CgroupController& c, const std::string& p); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 114 |  | 
|  | 115 | virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const; | 
|  | 116 | virtual bool ExecuteForTask(int tid) const; | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 117 | virtual void EnableResourceCaching(); | 
| Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 118 | virtual void DropResourceCaching(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 119 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 120 | const CgroupController* controller() const { return &controller_; } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 121 | std::string path() const { return path_; } | 
|  | 122 |  | 
|  | 123 | private: | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 124 | enum FdState { | 
|  | 125 | FDS_INACCESSIBLE = -1, | 
|  | 126 | FDS_APP_DEPENDENT = -2, | 
|  | 127 | FDS_NOT_CACHED = -3, | 
|  | 128 | }; | 
|  | 129 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 130 | CgroupController controller_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 131 | std::string path_; | 
|  | 132 | android::base::unique_fd fd_; | 
| mtk16036 | 53f79e6 | 2019-05-31 19:05:22 +0800 | [diff] [blame] | 133 | mutable std::mutex fd_mutex_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 134 |  | 
|  | 135 | static bool IsAppDependentPath(const std::string& path); | 
|  | 136 | static bool AddTidToCgroup(int tid, int fd); | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 137 |  | 
|  | 138 | bool IsFdValid() const { return fd_ > FDS_INACCESSIBLE; } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 139 | }; | 
|  | 140 |  | 
|  | 141 | class TaskProfile { | 
|  | 142 | public: | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 143 | TaskProfile() : res_cached_(false) {} | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 144 |  | 
|  | 145 | 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] | 146 | void MoveTo(TaskProfile* profile); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 147 |  | 
|  | 148 | bool ExecuteForProcess(uid_t uid, pid_t pid) const; | 
|  | 149 | bool ExecuteForTask(int tid) const; | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 150 | void EnableResourceCaching(); | 
| Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 151 | void DropResourceCaching(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 152 |  | 
|  | 153 | private: | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 154 | bool res_cached_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 155 | std::vector<std::unique_ptr<ProfileAction>> elements_; | 
|  | 156 | }; | 
|  | 157 |  | 
| Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 158 | // Set aggregate profile element | 
|  | 159 | class 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; | 
|  | 166 |  | 
|  | 167 | private: | 
|  | 168 | std::vector<std::shared_ptr<TaskProfile>> profiles_; | 
|  | 169 | }; | 
|  | 170 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 171 | class TaskProfiles { | 
|  | 172 | public: | 
|  | 173 | // Should be used by all users | 
|  | 174 | static TaskProfiles& GetInstance(); | 
|  | 175 |  | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 176 | TaskProfile* GetProfile(const std::string& name) const; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 177 | const ProfileAttribute* GetAttribute(const std::string& name) const; | 
| Riddle Hsu | a6abd82 | 2019-06-18 15:53:53 -0600 | [diff] [blame] | 178 | void DropResourceCaching() const; | 
| Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 179 | bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles, | 
|  | 180 | bool use_fd_cache); | 
|  | 181 | 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] | 182 |  | 
|  | 183 | private: | 
| Rick Yiu | 0b211fa | 2019-09-16 19:07:17 +0800 | [diff] [blame] | 184 | std::map<std::string, std::shared_ptr<TaskProfile>> profiles_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 185 | std::map<std::string, std::unique_ptr<ProfileAttribute>> attributes_; | 
|  | 186 |  | 
|  | 187 | TaskProfiles(); | 
|  | 188 |  | 
| Suren Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 189 | bool Load(const CgroupMap& cg_map, const std::string& file_name); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 190 | }; |