| 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: | 
|  | 30 | ProfileAttribute(const CgroupController* controller, const std::string& file_name) | 
|  | 31 | : controller_(controller), file_name_(file_name) {} | 
|  | 32 |  | 
|  | 33 | const CgroupController* controller() const { return controller_; } | 
|  | 34 | const std::string& file_name() const { return file_name_; } | 
|  | 35 |  | 
|  | 36 | bool GetPathForTask(int tid, std::string* path) const; | 
|  | 37 |  | 
|  | 38 | private: | 
|  | 39 | const CgroupController* controller_; | 
|  | 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 | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 51 | }; | 
|  | 52 |  | 
|  | 53 | // Profile actions | 
|  | 54 | class SetClampsAction : public ProfileAction { | 
|  | 55 | public: | 
|  | 56 | SetClampsAction(int boost, int clamp) noexcept : boost_(boost), clamp_(clamp) {} | 
|  | 57 |  | 
|  | 58 | virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const; | 
|  | 59 | virtual bool ExecuteForTask(int tid) const; | 
|  | 60 |  | 
|  | 61 | protected: | 
|  | 62 | int boost_; | 
|  | 63 | int clamp_; | 
|  | 64 | }; | 
|  | 65 |  | 
| Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 66 | // To avoid issues in sdk_mac build | 
|  | 67 | #if defined(__ANDROID__) | 
|  | 68 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 69 | class SetTimerSlackAction : public ProfileAction { | 
|  | 70 | public: | 
|  | 71 | SetTimerSlackAction(unsigned long slack) noexcept : slack_(slack) {} | 
|  | 72 |  | 
|  | 73 | virtual bool ExecuteForTask(int tid) const; | 
|  | 74 |  | 
|  | 75 | private: | 
|  | 76 | unsigned long slack_; | 
|  | 77 |  | 
|  | 78 | static bool IsTimerSlackSupported(int tid); | 
|  | 79 | }; | 
|  | 80 |  | 
| Suren Baghdasaryan | eca87cb | 2019-02-02 14:19:41 -0800 | [diff] [blame] | 81 | #else | 
|  | 82 |  | 
|  | 83 | class SetTimerSlackAction : public ProfileAction { | 
|  | 84 | public: | 
|  | 85 | SetTimerSlackAction(unsigned long) noexcept {} | 
|  | 86 |  | 
|  | 87 | virtual bool ExecuteForTask(int) const { return true; } | 
|  | 88 | }; | 
|  | 89 |  | 
|  | 90 | #endif | 
|  | 91 |  | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 92 | // Set attribute profile element | 
|  | 93 | class SetAttributeAction : public ProfileAction { | 
|  | 94 | public: | 
|  | 95 | SetAttributeAction(const ProfileAttribute* attribute, const std::string& value) | 
|  | 96 | : attribute_(attribute), value_(value) {} | 
|  | 97 |  | 
|  | 98 | virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const; | 
|  | 99 | virtual bool ExecuteForTask(int tid) const; | 
|  | 100 |  | 
|  | 101 | private: | 
|  | 102 | const ProfileAttribute* attribute_; | 
|  | 103 | std::string value_; | 
|  | 104 | }; | 
|  | 105 |  | 
|  | 106 | // Set cgroup profile element | 
|  | 107 | class SetCgroupAction : public ProfileAction { | 
|  | 108 | public: | 
|  | 109 | SetCgroupAction(const CgroupController* c, const std::string& p); | 
|  | 110 |  | 
|  | 111 | virtual bool ExecuteForProcess(uid_t uid, pid_t pid) const; | 
|  | 112 | virtual bool ExecuteForTask(int tid) const; | 
|  | 113 |  | 
|  | 114 | const CgroupController* controller() const { return controller_; } | 
|  | 115 | std::string path() const { return path_; } | 
|  | 116 |  | 
|  | 117 | private: | 
|  | 118 | const CgroupController* controller_; | 
|  | 119 | std::string path_; | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 120 | #ifdef CACHE_FILE_DESCRIPTORS | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 121 | android::base::unique_fd fd_; | 
| Suren Baghdasaryan | bee9f57 | 2019-02-05 16:44:22 -0800 | [diff] [blame] | 122 | #endif | 
| Suren Baghdasaryan | 82b72a5 | 2018-12-21 11:41:50 -0800 | [diff] [blame] | 123 |  | 
|  | 124 | static bool IsAppDependentPath(const std::string& path); | 
|  | 125 | static bool AddTidToCgroup(int tid, int fd); | 
|  | 126 | }; | 
|  | 127 |  | 
|  | 128 | class TaskProfile { | 
|  | 129 | public: | 
|  | 130 | TaskProfile() {} | 
|  | 131 |  | 
|  | 132 | void Add(std::unique_ptr<ProfileAction> e) { elements_.push_back(std::move(e)); } | 
|  | 133 |  | 
|  | 134 | bool ExecuteForProcess(uid_t uid, pid_t pid) const; | 
|  | 135 | bool ExecuteForTask(int tid) const; | 
|  | 136 |  | 
|  | 137 | private: | 
|  | 138 | std::vector<std::unique_ptr<ProfileAction>> elements_; | 
|  | 139 | }; | 
|  | 140 |  | 
|  | 141 | class TaskProfiles { | 
|  | 142 | public: | 
|  | 143 | // Should be used by all users | 
|  | 144 | static TaskProfiles& GetInstance(); | 
|  | 145 |  | 
|  | 146 | const TaskProfile* GetProfile(const std::string& name) const; | 
|  | 147 | const ProfileAttribute* GetAttribute(const std::string& name) const; | 
|  | 148 |  | 
|  | 149 | private: | 
|  | 150 | std::map<std::string, std::unique_ptr<TaskProfile>> profiles_; | 
|  | 151 | std::map<std::string, std::unique_ptr<ProfileAttribute>> attributes_; | 
|  | 152 |  | 
|  | 153 | TaskProfiles(); | 
|  | 154 |  | 
|  | 155 | bool Load(const CgroupMap& cg_map); | 
|  | 156 | }; |