• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     uint32_t windowId_;
56     bool isVisible_ = false;
57     bool isFocused_ = false;
58     bool drawingContentState_ = false;
59     int32_t windowType_ = 0;
60     uint64_t displayId_ = 0;
61     // webview app corresponding with top tab page in this window
62     uid_t topWebviewRenderUid_ = 0;
63 };
64 
65 class AbilityInfo {
66 public:
AbilityInfo(int32_t recordId)67     AbilityInfo(int32_t recordId) : recordId_(recordId) {}
~AbilityInfo()68     ~AbilityInfo() {}
69 
70     int32_t state_ = -1; // normal ability state
71     int32_t estate_ = -1; // extension state
72     int32_t type_ = -1; // ability type
73     int32_t recordId_ = -1;
74     std::string name_;
75 };
76 
77 class ProcessRecord {
78 public:
ProcessRecord(uid_t uid,pid_t pid)79     ProcessRecord(uid_t uid, pid_t pid) : uid_(uid), pid_(pid) {}
~ProcessRecord()80     ~ProcessRecord()
81     {
82         abilities_.clear();
83         windows_.clear();
84     };
85 
86     void SetName(const std::string& name);
87     std::shared_ptr<AbilityInfo> GetAbilityInfoNonNull(int32_t recordId);
88     std::shared_ptr<AbilityInfo> GetAbilityInfo(int32_t recordId);
89     std::shared_ptr<WindowInfo> GetWindowInfoNonNull(uint32_t windowId);
90     void RemoveAbilityByRecordId(int32_t recordId);
91     bool HasAbility(int32_t recordId) const;
92     bool HasServiceExtension() const;
93     bool IsVisible() const;
94     std::set<int32_t> GetKeyTidSetByRole(int64_t role);
95 
GetPid()96     inline pid_t GetPid() const
97     {
98         return pid_;
99     }
100 
GetUid()101     inline uid_t GetUid() const
102     {
103         return uid_;
104     }
105 
GetName()106     const std::string& GetName() const
107     {
108         return processName_;
109     }
110 
111     SchedPolicy lastSchedGroup_ = SP_UPPER_LIMIT;
112     SchedPolicy curSchedGroup_ = SP_UPPER_LIMIT;
113     SchedPolicy setSchedGroup_ = SP_UPPER_LIMIT;
114     SchedPolicy specialSchedGroup_ = SP_UPPER_LIMIT;
115     bool runningTransientTask_ = false;
116     bool isActive_ {false};
117     bool inSelfRenderCgroup_ = false;
118     bool isNapState_ = false;
119     bool processDrawingState_ = false;
120     bool screenCaptureState_ = false;
121     bool videoState_ = false;
122     bool isLoadState_ = false;
123 
124     int32_t processType_ = ProcRecordType::NORMAL;
125     uint32_t continuousTaskFlag_ = 0;
126     int32_t audioPlayingState_ = -1;
127     int32_t renderTid_ = 0;
128     int32_t processState_ = 0;
129     int32_t linkedWindowId_ {-1};
130     int32_t serialNum_ {-1};
131     int32_t extensionType_ = -1;
132     int32_t cameraState_ = -1;
133     int32_t bluetoothState_ = -1;
134     int32_t wifiState_ = -1;
135     int32_t mmiStatus_ {-1};
136     int32_t hostPid_ = -1;
137     uint64_t suppressState_ = 0;
138     bool isReload_ = false;
139 
140     std::map<uint32_t, bool> runningLockState_;
141     std::map<int32_t, bool> avCodecState_;
142     std::unordered_map<int32_t, std::vector<uint32_t>> abilityIdAndContinuousTaskFlagMap_;
143     std::vector<std::shared_ptr<AbilityInfo>> abilities_;
144     std::vector<std::shared_ptr<WindowInfo>> windows_;
145 
146     std::map<int32_t, int32_t> keyThreadRoleMap_ {}; // items in keyThreadMap_ is (tid, role)
147 
148     std::unordered_map<int32_t, SchedPolicy> specialSchedThread_;
149     std::unordered_map<uint32_t, SchedPolicy> policyBeforUnlimitedSuppress_;
150 
151     std::string processName_;
152 private:
153     uid_t uid_;
154     pid_t pid_;
155 };
156 
157 class Application {
158 public:
Application(uid_t uid)159     Application(uid_t uid) : uid_(uid) {}
160     ~Application() = default;
161 
162     std::shared_ptr<ProcessRecord> AddProcessRecord(std::shared_ptr<ProcessRecord> pr);
163     void RemoveProcessRecord(pid_t pid);
164     std::shared_ptr<ProcessRecord> GetProcessRecord(pid_t pid);
165     std::shared_ptr<ProcessRecord> GetProcessRecordNonNull(pid_t pid);
166     std::shared_ptr<ProcessRecord> FindProcessRecordByRecordId(int32_t recordId);
167     std::shared_ptr<ProcessRecord> FindProcessRecordByWindowId(uint32_t windowId);
168     void SetName(const std::string& name);
169     void AddHostProcess(int32_t hostPid);
170     bool IsHostProcess(int32_t hostPid) const;
171 
GetUid()172     inline uid_t GetUid() const
173     {
174         return uid_;
175     }
176 
GetPidsMap()177     inline std::map<pid_t, std::shared_ptr<ProcessRecord>> GetPidsMap() const
178     {
179         return pidsMap_;
180     }
181 
GetName()182     const std::string& GetName() const
183     {
184         return name_;
185     }
186 
187     int32_t state_ = -1;
188     std::shared_ptr<ProcessRecord> focusedProcess_ = nullptr;
189     SchedPolicy lastSchedGroup_ = SP_UPPER_LIMIT;
190     SchedPolicy curSchedGroup_ = SP_UPPER_LIMIT;
191     SchedPolicy setSchedGroup_ = SP_UPPER_LIMIT;
192     bool isCosmicCubeStateHide_ = false;
193 
194 private:
195     uid_t uid_;
196     std::string name_;
197     std::map<pid_t, std::shared_ptr<ProcessRecord>> pidsMap_;
198     std::unordered_set<int32_t> hostPidsSet_;
199 };
200 
201 class Supervisor {
202 public:
203     std::shared_ptr<Application> GetAppRecord(int32_t uid);
204     std::shared_ptr<Application> GetAppRecordNonNull(int32_t uid);
205     std::shared_ptr<ProcessRecord> FindProcessRecord(pid_t pid);
206     void RemoveApplication(int32_t uid);
207     void SearchAbilityRecordId(std::shared_ptr<Application> &app, std::shared_ptr<ProcessRecord> &procRecord,
208         int32_t recordId);
209     void SearchWindowId(std::shared_ptr<Application> &application, std::shared_ptr<ProcessRecord> &procRecord,
210         uint32_t windowId);
211     void SetSystemLoadLevelState(int32_t level);
212     int32_t GetSystemLoadLevel();
213     void InitSuperVisorContent();
214     void ConnectAppManagerService();
215 
216     int32_t sceneBoardUid_ = -1;
217     int32_t sceneBoardPid_ = -1;
218     int32_t installsPid_ = -1;
219     int32_t installsUid_ = -1;
220     std::shared_ptr<Application> focusedApp_ = nullptr;
221 
GetUidsMap()222     inline std::map<int32_t, std::shared_ptr<Application>> GetUidsMap() const
223     {
224         return uidsMap_;
225     }
226 private:
227     void ReloadApplication();
228 #ifdef SUPPORT_CHILD_PROCESS
229     void ReloadChildProcess();
230 #endif // SUPPORT_CHILD_PROCESS
231 private:
232     std::map<int32_t, std::shared_ptr<Application>> uidsMap_;
233     int32_t systemLoadLevel_ = -1;
234     sptr<OHOS::AppExecFwk::IAppMgr> appManager_ = nullptr;
235 };
236 } // namespace ResourceSchedule
237 } // namespace OHOS
238 #endif // CGROUP_SCHED_FRAMEWORK_SCHED_CONTROLLER_INCLUDE_SUPERVISOR_H_
239