| 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() {} | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 54 | }; | 
 | 55 |  | 
 | 56 | // Profile actions | 
 | 57 | class 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 Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 69 | // To avoid issues in sdk_mac build | 
 | 70 | #if defined(__ANDROID__) | 
 | 71 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 72 | class 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 Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 84 | #else | 
 | 85 |  | 
 | 86 | class 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 Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 95 | // Set attribute profile element | 
 | 96 | class 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 | 
 | 110 | class SetCgroupAction : public ProfileAction { | 
 | 111 |   public: | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 112 |     SetCgroupAction(const CgroupController& c, const std::string& p); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 113 |  | 
 | 114 |     virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const; | 
 | 115 |     virtual bool ExecuteForTask(int tid) const; | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 116 |     virtual void EnableResourceCaching(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 117 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 118 |     const CgroupController* controller() const { return &controller_; } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 119 |     std::string path() const { return path_; } | 
 | 120 |  | 
 | 121 |   private: | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 122 |     enum FdState { | 
 | 123 |         FDS_INACCESSIBLE = -1, | 
 | 124 |         FDS_APP_DEPENDENT = -2, | 
 | 125 |         FDS_NOT_CACHED = -3, | 
 | 126 |     }; | 
 | 127 |  | 
| Yifan Hong | 53e0deb | 2019-03-22 17:01:08 -0700 | [diff] [blame] | 128 |     CgroupController controller_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 129 |     std::string path_; | 
 | 130 |     android::base::unique_fd fd_; | 
| mtk16036 | 53f79e6 | 2019-05-31 19:05:22 +0800 | [diff] [blame] | 131 |     mutable std::mutex fd_mutex_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 132 |  | 
 | 133 |     static bool IsAppDependentPath(const std::string& path); | 
 | 134 |     static bool AddTidToCgroup(int tid, int fd); | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 135 |  | 
 | 136 |     bool IsFdValid() const { return fd_ > FDS_INACCESSIBLE; } | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 137 | }; | 
 | 138 |  | 
 | 139 | class TaskProfile { | 
 | 140 |   public: | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 141 |     TaskProfile() : res_cached_(false) {} | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 142 |  | 
 | 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 Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 147 |     void EnableResourceCaching(); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 148 |  | 
 | 149 |   private: | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 150 |     bool res_cached_; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 151 |     std::vector<std::unique_ptr<ProfileAction>> elements_; | 
 | 152 | }; | 
 | 153 |  | 
 | 154 | class TaskProfiles { | 
 | 155 |   public: | 
 | 156 |     // Should be used by all users | 
 | 157 |     static TaskProfiles& GetInstance(); | 
 | 158 |  | 
| Suren Baghdasaryan | 8a315d2 | 2019-02-14 14:40:41 -0800 | [diff] [blame] | 159 |     TaskProfile* GetProfile(const std::string& name) const; | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 160 |     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 Baghdasaryan | 05da67c | 2019-02-19 15:01:28 -0800 | [diff] [blame] | 168 |     bool Load(const CgroupMap& cg_map, const std::string& file_name); | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 169 | }; |