| 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> | 
|  | 22 | #include <string> | 
|  | 23 | #include <vector> | 
|  | 24 |  | 
|  | 25 | #include <android-base/unique_fd.h> | 
|  | 26 | #include <cgroup_map.h> | 
|  | 27 |  | 
|  | 28 | class ProfileAttribute { | 
|  | 29 | public: | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 30 | ProfileAttribute(const CgroupController& controller, const std::string& file_name) | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 31 | : controller_(controller), file_name_(file_name) {} | 
|  | 32 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 33 | const CgroupController* controller() const { return &controller_; } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 34 | const std::string& file_name() const { return file_name_; } | 
|  | 35 |  | 
|  | 36 | bool GetPathForTask(int tid, std::string* path) const; | 
|  | 37 |  | 
|  | 38 | private: | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 39 | CgroupController controller_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 40 | std::string file_name_; | 
|  | 41 | }; | 
|  | 42 |  | 
|  | 43 | // Abstract profile element | 
|  | 44 | class ProfileAction { | 
|  | 45 | public: | 
|  | 46 | virtual ~ProfileAction() {} | 
|  | 47 |  | 
|  | 48 | // Default implementations will fail | 
| Greg Kaiser | 5c5ed9c | 2019-02-04 06:33:26 -0800 | [diff] [blame] | 49 | virtual bool ExecuteForProcess(uid_t, pid_t) const { return false; }; | 
|  | 50 | virtual bool ExecuteForTask(int) const { return false; }; | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 51 |  | 
|  | 52 | virtual void EnableResourceCaching() {} | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 53 | }; | 
|  | 54 |  | 
|  | 55 | // Profile actions | 
|  | 56 | class 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 Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 68 | // To avoid issues in sdk_mac build | 
|  | 69 | #if defined(__ANDROID__) | 
|  | 70 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 71 | class 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 Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 83 | #else | 
|  | 84 |  | 
|  | 85 | class 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 Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 94 | // Set attribute profile element | 
|  | 95 | class 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 | 
|  | 109 | class SetCgroupAction : public ProfileAction { | 
|  | 110 | public: | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 111 | SetCgroupAction(const CgroupController& c, const std::string& p); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 112 |  | 
|  | 113 | virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const; | 
|  | 114 | virtual bool ExecuteForTask(int tid) const; | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 115 | virtual void EnableResourceCaching(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 116 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 117 | const CgroupController* controller() const { return &controller_; } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 118 | std::string path() const { return path_; } | 
|  | 119 |  | 
|  | 120 | private: | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 121 | enum FdState { | 
|  | 122 | FDS_INACCESSIBLE = -1, | 
|  | 123 | FDS_APP_DEPENDENT = -2, | 
|  | 124 | FDS_NOT_CACHED = -3, | 
|  | 125 | }; | 
|  | 126 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 127 | CgroupController controller_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 128 | std::string path_; | 
|  | 129 | android::base::unique_fd fd_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 130 |  | 
|  | 131 | static bool IsAppDependentPath(const std::string& path); | 
|  | 132 | static bool AddTidToCgroup(int tid, int fd); | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 133 |  | 
|  | 134 | bool IsFdValid() const { return fd_ > FDS_INACCESSIBLE; } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 135 | }; | 
|  | 136 |  | 
|  | 137 | class TaskProfile { | 
|  | 138 | public: | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 139 | TaskProfile() : res_cached_(false) {} | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 140 |  | 
|  | 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 Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 145 | void EnableResourceCaching(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 146 |  | 
|  | 147 | private: | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 148 | bool res_cached_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 149 | std::vector<std::unique_ptr<ProfileAction>> elements_; | 
|  | 150 | }; | 
|  | 151 |  | 
|  | 152 | class TaskProfiles { | 
|  | 153 | public: | 
|  | 154 | // Should be used by all users | 
|  | 155 | static TaskProfiles& GetInstance(); | 
|  | 156 |  | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 157 | TaskProfile* GetProfile(const std::string& name) const; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 158 | 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 Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 166 | bool Load(const CgroupMap& cg_map, const std::string& file_name); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 167 | }; |