1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef CGROUP_SCHED_FRAMEWORK_SCHED_CONTROLLER_INCLUDE_SUPERVISOR_H_ 17 #define CGROUP_SCHED_FRAMEWORK_SCHED_CONTROLLER_INCLUDE_SUPERVISOR_H_ 18 19 #include <iostream> 20 #include <map> 21 #include <sys/types.h> 22 #include <string> 23 #include <vector> 24 #include <set> 25 #include <unordered_set> 26 27 #include "app_mgr_interface.h" 28 #include "sched_policy.h" 29 30 namespace OHOS { 31 namespace ResourceSchedule { 32 using OHOS::ResourceSchedule::CgroupSetting::SchedPolicy; 33 using OHOS::ResourceSchedule::CgroupSetting::SP_DEFAULT; 34 using OHOS::ResourceSchedule::CgroupSetting::SP_BACKGROUND; 35 using OHOS::ResourceSchedule::CgroupSetting::SP_FOREGROUND; 36 using OHOS::ResourceSchedule::CgroupSetting::SP_SYSTEM_BACKGROUND; 37 using OHOS::ResourceSchedule::CgroupSetting::SP_TOP_APP; 38 using OHOS::ResourceSchedule::CgroupSetting::SP_UPPER_LIMIT; 39 40 enum ProcRecordType : int32_t { 41 NORMAL = 0, 42 EXTENSION, 43 RENDER, 44 GPU, 45 LINUX, 46 CHILD, 47 }; 48 49 class AbilityInfo; 50 class WindowInfo { 51 public: WindowInfo(int32_t windowId)52 explicit WindowInfo(int32_t windowId) : windowId_(windowId) {} ~WindowInfo()53 ~WindowInfo() 54 { 55 ability_ = nullptr; 56 } 57 58 uint32_t windowId_; 59 uint32_t visibilityState_ = 2; 60 bool isVisible_ = false; 61 bool isFocused_ = false; 62 bool drawingContentState_ = false; 63 int32_t windowType_ = 0; 64 uint64_t displayId_ = 0; 65 std::shared_ptr<AbilityInfo> ability_ = nullptr; 66 // webview render app corresponding with top tab page in this window 67 uid_t topWebviewRenderUid_ = 0; 68 }; 69 70 class AbilityInfo { 71 public: AbilityInfo(uintptr_t token)72 AbilityInfo(uintptr_t token) : token_(token) {} ~AbilityInfo()73 ~AbilityInfo() {} 74 75 int32_t state_ = -1; // normal ability state 76 int32_t estate_ = -1; // extension state 77 int32_t type_ = -1; // ability type 78 uintptr_t token_ = 0; 79 std::string name_; 80 }; 81 82 class ProcessRecord { 83 public: ProcessRecord(uid_t uid,pid_t pid)84 ProcessRecord(uid_t uid, pid_t pid) : uid_(uid), pid_(pid) {} ~ProcessRecord()85 ~ProcessRecord() 86 { 87 abilities_.clear(); 88 windows_.clear(); 89 }; 90 91 std::shared_ptr<AbilityInfo> GetAbilityInfoNonNull(uintptr_t token); 92 std::shared_ptr<AbilityInfo> GetAbilityInfo(uintptr_t token); 93 std::shared_ptr<WindowInfo> GetWindowInfoNonNull(uint32_t windowId); 94 void RemoveAbilityByToken(uintptr_t token); 95 bool HasAbility(uintptr_t token) const; 96 bool HasServiceExtension() const; 97 bool IsVisible() const; 98 std::set<int32_t> GetKeyTidSetByRole(int64_t role); 99 GetPid()100 inline pid_t GetPid() const 101 { 102 return pid_; 103 } 104 GetUid()105 inline uid_t GetUid() const 106 { 107 return uid_; 108 } 109 110 SchedPolicy lastSchedGroup_ = SP_UPPER_LIMIT; 111 SchedPolicy curSchedGroup_ = SP_UPPER_LIMIT; 112 SchedPolicy setSchedGroup_ = SP_UPPER_LIMIT; 113 bool runningTransientTask_ = false; 114 bool isActive_ {false}; 115 bool inSelfRenderCgroup_ = false; 116 bool isNapState_ = false; 117 bool processDrawingState_ = false; 118 bool screenCaptureState_ = false; 119 bool videoState_ = false; 120 121 int32_t processType_ = ProcRecordType::NORMAL; 122 uint32_t continuousTaskFlag_ = 0; 123 int32_t audioPlayingState_ = -1; 124 int32_t renderTid_ = 0; 125 int32_t processState_ = 0; 126 int32_t linkedWindowId_ {-1}; 127 int32_t serialNum_ {-1}; 128 int32_t extensionType_ = -1; 129 int32_t mmiStatus_ {-1}; 130 int32_t cameraState_ = -1; 131 int32_t bluetoothState_ = -1; 132 int32_t wifiState_ = -1; 133 int32_t hostPid_ = -1; 134 uint32_t suppressState_ = 0; 135 136 std::map<uint32_t, bool> runningLockState_; 137 std::map<int32_t, bool> avCodecState_; 138 std::unordered_map<int32_t, std::vector<uint32_t>> abilityIdAndContinuousTaskFlagMap_; 139 std::vector<std::shared_ptr<AbilityInfo>> abilities_; 140 std::vector<std::shared_ptr<WindowInfo>> windows_; 141 142 std::map<int32_t, int32_t> keyThreadRoleMap_ {}; // items in keyThreadMap_ is (tid, role) 143 144 std::unordered_map<int32_t, SchedPolicy> specialSchedThread_; 145 private: 146 uid_t uid_; 147 pid_t pid_; 148 }; 149 150 class Application { 151 public: Application(uid_t uid)152 Application(uid_t uid) : uid_(uid) {} 153 ~Application() = default; 154 155 std::shared_ptr<ProcessRecord> AddProcessRecord(std::shared_ptr<ProcessRecord> pr); 156 void RemoveProcessRecord(pid_t pid); 157 std::shared_ptr<ProcessRecord> GetProcessRecord(pid_t pid); 158 std::shared_ptr<ProcessRecord> GetProcessRecordNonNull(pid_t pid); 159 std::shared_ptr<ProcessRecord> FindProcessRecordByToken(uintptr_t token); 160 std::shared_ptr<ProcessRecord> FindProcessRecordByWindowId(uint32_t windowId); 161 void SetName(const std::string& name); 162 void AddHostProcess(int32_t hostPid); 163 bool IsHostProcess(int32_t hostPid) const; 164 GetUid()165 inline uid_t GetUid() const 166 { 167 return uid_; 168 } 169 GetPidsMap()170 inline std::map<pid_t, std::shared_ptr<ProcessRecord>> GetPidsMap() const 171 { 172 return pidsMap_; 173 } 174 GetName()175 const std::string& GetName() const 176 { 177 return name_; 178 } 179 180 int32_t state_ = -1; 181 std::shared_ptr<ProcessRecord> focusedProcess_ = nullptr; 182 SchedPolicy lastSchedGroup_ = SP_UPPER_LIMIT; 183 SchedPolicy curSchedGroup_ = SP_UPPER_LIMIT; 184 SchedPolicy setSchedGroup_ = SP_UPPER_LIMIT; 185 186 private: 187 uid_t uid_; 188 std::string name_; 189 std::map<pid_t, std::shared_ptr<ProcessRecord>> pidsMap_; 190 std::unordered_set<int32_t> hostPidsSet_; 191 }; 192 193 class Supervisor { 194 public: 195 std::shared_ptr<Application> GetAppRecord(int32_t uid); 196 std::shared_ptr<Application> GetAppRecordNonNull(int32_t uid); 197 std::shared_ptr<ProcessRecord> FindProcessRecord(pid_t pid); 198 void RemoveApplication(int32_t uid); 199 void SearchAbilityToken(std::shared_ptr<Application> &app, std::shared_ptr<ProcessRecord> &procRecord, 200 uintptr_t token); 201 void SearchWindowId(std::shared_ptr<Application> &application, std::shared_ptr<ProcessRecord> &procRecord, 202 uint32_t windowId); 203 void SetSystemLoadLevelState(int32_t level); 204 int32_t GetSystemLoadLevel(); 205 void InitSuperVisorContent(); 206 207 int32_t sceneBoardPid_ = -1; 208 int32_t installsPid_ = -1; 209 int32_t installsUid_ = -1; 210 std::shared_ptr<Application> focusedApp_ = nullptr; 211 GetUidsMap()212 inline std::map<int32_t, std::shared_ptr<Application>> GetUidsMap() const 213 { 214 return uidsMap_; 215 } 216 private: 217 void ConnectAppManagerService(); 218 void ReloadApplication(); 219 void ReloadChildProcess(); 220 private: 221 std::map<int32_t, std::shared_ptr<Application>> uidsMap_; 222 int32_t systemLoadLevel_ = -1; 223 sptr<OHOS::AppExecFwk::IAppMgr> appManager_ = nullptr; 224 }; 225 } // namespace ResourceSchedule 226 } // namespace OHOS 227 #endif // CGROUP_SCHED_FRAMEWORK_SCHED_CONTROLLER_INCLUDE_SUPERVISOR_H_ 228