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 25 #include "sched_policy.h" 26 27 namespace OHOS { 28 namespace ResourceSchedule { 29 using OHOS::ResourceSchedule::CgroupSetting::SchedPolicy; 30 using OHOS::ResourceSchedule::CgroupSetting::SP_DEFAULT; 31 using OHOS::ResourceSchedule::CgroupSetting::SP_BACKGROUND; 32 using OHOS::ResourceSchedule::CgroupSetting::SP_FOREGROUND; 33 using OHOS::ResourceSchedule::CgroupSetting::SP_SYSTEM_BACKGROUND; 34 using OHOS::ResourceSchedule::CgroupSetting::SP_TOP_APP; 35 using OHOS::ResourceSchedule::CgroupSetting::SP_UPPER_LIMIT; 36 37 class AbilityInfo; 38 class WindowInfo { 39 public: WindowInfo(int32_t windowId)40 explicit WindowInfo(int32_t windowId) : windowId_(windowId) {} ~WindowInfo()41 ~WindowInfo() 42 { 43 ability_ = nullptr; 44 } 45 46 uint32_t windowId_; 47 bool isVisible_ = false; 48 bool isFocused_ = false; 49 int32_t windowType_ = 0; 50 uint64_t displayId_ = 0; 51 std::shared_ptr<AbilityInfo> ability_ = nullptr; 52 }; 53 54 class AbilityInfo { 55 public: AbilityInfo(uintptr_t token)56 AbilityInfo(uintptr_t token) : token_(token) {} ~AbilityInfo()57 ~AbilityInfo() {} 58 59 int32_t state_ = -1; // normal ability state 60 int32_t estate_ = -1; // extension state 61 int32_t type_ = -1; // ability type 62 uintptr_t token_ = 0; 63 std::string name_; 64 }; 65 66 class ProcessRecord { 67 public: ProcessRecord(uid_t uid,pid_t pid)68 ProcessRecord(uid_t uid, pid_t pid) : uid_(uid), pid_(pid) {} ~ProcessRecord()69 ~ProcessRecord() 70 { 71 abilities_.clear(); 72 windows_.clear(); 73 }; 74 75 std::shared_ptr<AbilityInfo> GetAbilityInfoNonNull(uintptr_t token); 76 std::shared_ptr<AbilityInfo> GetAbilityInfo(uintptr_t token); 77 std::shared_ptr<WindowInfo> GetWindowInfoNonNull(uint32_t windowId); 78 void RemoveAbilityByToken(uintptr_t token); 79 bool HasAbility(uintptr_t token) const; 80 bool HasServiceExtension() const; 81 bool IsVisible() const; 82 GetPid()83 inline pid_t GetPid() const 84 { 85 return pid_; 86 } 87 GetUid()88 inline uid_t GetUid() const 89 { 90 return uid_; 91 } 92 93 SchedPolicy lastSchedGroup_ = SP_UPPER_LIMIT; 94 SchedPolicy curSchedGroup_ = SP_UPPER_LIMIT; 95 SchedPolicy setSchedGroup_ = SP_UPPER_LIMIT; 96 bool isRenderProcess_ = false; 97 bool runningTransientTask_ = false; 98 uint32_t continuousTaskFlag_ = 0; 99 int32_t renderTid_ = 0; 100 int32_t maliTid_ = 0; 101 102 std::vector<std::shared_ptr<AbilityInfo>> abilities_; 103 std::vector<std::shared_ptr<WindowInfo>> windows_; 104 105 private: 106 uid_t uid_; 107 pid_t pid_; 108 }; 109 110 class Application { 111 public: Application(uid_t uid)112 Application(uid_t uid) : uid_(uid) {} 113 ~Application() = default; 114 115 std::shared_ptr<ProcessRecord> AddProcessRecord(std::shared_ptr<ProcessRecord> pr); 116 void RemoveProcessRecord(pid_t pid); 117 std::shared_ptr<ProcessRecord> GetProcessRecord(pid_t pid); 118 std::shared_ptr<ProcessRecord> GetProcessRecordNonNull(pid_t pid); 119 std::shared_ptr<ProcessRecord> FindProcessRecordByToken(uintptr_t token); 120 std::shared_ptr<ProcessRecord> FindProcessRecordByWindowId(uint32_t windowId); 121 void SetName(const std::string& name); 122 void SetMainProcess(std::shared_ptr<ProcessRecord> pr); 123 GetUid()124 inline uid_t GetUid() const 125 { 126 return uid_; 127 } 128 GetPidsMap()129 inline std::map<pid_t, std::shared_ptr<ProcessRecord>> GetPidsMap() const 130 { 131 return pidsMap_; 132 } 133 GetMainProcessRecord()134 std::shared_ptr<ProcessRecord> GetMainProcessRecord() const 135 { 136 return mainProcess_; 137 } 138 GetName()139 const std::string& GetName() const 140 { 141 return name_; 142 } 143 144 int32_t state_ = -1; 145 std::shared_ptr<ProcessRecord> focusedProcess_ = nullptr; 146 SchedPolicy lastSchedGroup_ = SP_UPPER_LIMIT; 147 SchedPolicy curSchedGroup_ = SP_UPPER_LIMIT; 148 SchedPolicy setSchedGroup_ = SP_UPPER_LIMIT; 149 150 private: 151 uid_t uid_; 152 std::string name_; 153 std::shared_ptr<ProcessRecord> mainProcess_ = nullptr; 154 std::map<pid_t, std::shared_ptr<ProcessRecord>> pidsMap_; 155 }; 156 157 class Supervisor { 158 public: 159 std::shared_ptr<Application> GetAppRecord(int32_t uid); 160 std::shared_ptr<Application> GetAppRecordNonNull(int32_t uid); 161 std::shared_ptr<ProcessRecord> FindProcessRecord(pid_t pid); 162 void RemoveApplication(int32_t uid); 163 void SearchAbilityToken(std::shared_ptr<Application> &app, std::shared_ptr<ProcessRecord> &procRecord, 164 uintptr_t token); 165 void SearchWindowId(std::shared_ptr<Application> &application, std::shared_ptr<ProcessRecord> &procRecord, 166 uint32_t windowId); 167 168 std::shared_ptr<Application> focusedApp_ = nullptr; 169 170 private: 171 std::map<int32_t, std::shared_ptr<Application>> uidsMap_; 172 }; 173 } // namespace ResourceSchedule 174 } // namespace OHOS 175 #endif // CGROUP_SCHED_FRAMEWORK_SCHED_CONTROLLER_INCLUDE_SUPERVISOR_H_ 176