• 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 #ifndef FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_SCHED_SERVICES_POLICY_MANAGER_H
16 #define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_SCHED_SERVICES_POLICY_MANAGER_H
17 #include <map>
18 #include <memory>
19 #include <mutex>
20 #include <vector>
21 
22 #include <event_runner.h>
23 #include "policy_type.h"
24 #include "policy/ipolicy_filter.h"
25 #include "work_conn_manager.h"
26 #include "work_queue.h"
27 #include "work_status.h"
28 #include "ffrt.h"
29 
30 namespace OHOS {
31 namespace WorkScheduler {
32 class WorkSchedulerService;
33 class WorkEventHandler;
34 class AppDataClearListener;
35 class Watchdog;
36 class WorkPolicyManager {
37 public:
38     explicit WorkPolicyManager(const std::shared_ptr<WorkSchedulerService>& wss);
39     ~WorkPolicyManager() = default;
40     /**
41      * @brief Init.
42      *
43      * @return True if success,else false.
44      */
45     bool Init(const std::shared_ptr<AppExecFwk::EventRunner>& runner);
46     /**
47      * @brief Add listener.
48      *
49      * @return True if success,else false.
50      */
51     bool AddListener();
52     /**
53      * @brief Add policy filter.
54      *
55      * @param filter The filter.
56      */
57     void AddPolicyFilter(std::shared_ptr<IPolicyFilter> filter);
58     /**
59      * @brief Add work.
60      *
61      * @param workStatus The status of work.
62      * @param uid The uid.
63      * @return True if success,else false.
64      */
65     int32_t AddWork(std::shared_ptr<WorkStatus> workStatus, int32_t uid);
66     /**
67      * @brief Remove work.
68      *
69      * @param workStatus The status of work.
70      * @param uid The uid.
71      * @return True if success,else false.
72      */
73     bool RemoveWork(std::shared_ptr<WorkStatus> workStatus, int32_t uid);
74     /**
75      * @brief Stop work.
76      *
77      * @param workStatus The status of work.
78      * @param uid The uid.
79      * @param needCancel The need cancel.
80      * @param isTimeOut The is time out.
81      * @return True if success,else false.
82      */
83     bool StopWork(std::shared_ptr<WorkStatus> workStatus, int32_t uid, const bool needCancel, bool isTimeOut);
84     /**
85      * @brief Stop and clear works.
86      *
87      * @param uid The uid.
88      * @return True if success,else false.
89      */
90     bool StopAndClearWorks(int32_t uid);
91     /**
92      * @brief The last work time out.
93      *
94      * @param workId The id of work.
95      * @param uid The uid.
96      * @param result True if success,else false.
97      * @return The error code.
98      */
99     int32_t IsLastWorkTimeout(int32_t workId, int32_t uid, bool &result);
100     /**
101      * @brief Find work status.
102      *
103      * @param workInfo The info of work.
104      * @param uid The uid.
105      * @return The uid queue map.
106      */
107     std::shared_ptr<WorkStatus> FindWorkStatus(WorkInfo& workInfo, int32_t uid);
108     /**
109      * @brief Find SA.
110      *
111      * @param saId The id of sa.
112      * @param uid The uid.
113      * @return The SA of uid.
114      */
115     std::shared_ptr<WorkStatus> FindSA(int32_t saId, int32_t uid);
116     /**
117      * @brief The OnConditionReady callback.
118      *
119      * @param workStatusVector The work status vector.
120      */
121     void OnConditionReady(std::shared_ptr<std::vector<std::shared_ptr<WorkStatus>>> workStatusVector);
122     /**
123      * @brief The OnPolicyChanged callback.
124      *
125      * @param policyType The policy type.
126      * @param detectorVal The detector val.
127      */
128     void OnPolicyChanged(PolicyType policyType, std::shared_ptr<DetectorValue> detectorVal);
129     /**
130      * @brief Obtain all works.
131      *
132      * @param uid The uid.
133      * @return All works.
134      */
135     std::vector<WorkInfo> ObtainAllWorks(int32_t &uid);
136     /**
137      * @brief Get the status of work.
138      *
139      * @param uid The uid.
140      * @param workInfo The info of work.
141      * @return The status of work.
142      */
143     std::shared_ptr<WorkInfo> GetWorkStatus(int32_t &uid, int32_t &workInfo);
144     /**
145      * @brief Get all status of work.
146      *
147      * @param uid The uid.
148      * @return All status of work.
149      */
150     std::list<std::shared_ptr<WorkStatus>> GetAllWorkStatus(int32_t &uid);
151 
152     /**
153      * @brief Get the All Running Works object.
154      *
155      * @return All running works;
156      */
157     std::vector<WorkInfo> GetAllRunningWorks();
158 
159     /**
160      * @brief The Dump.
161      *
162      * @param result The result.
163      */
164     void Dump(std::string& result);
165     int32_t currentRunningCount = 0;
166     /**
167      * @brief Check work to run.
168      */
169     void CheckWorkToRun();
170     /**
171      * @brief Send retrigger.
172      *
173      * @param delayTime The delay time.
174      */
175     void SendRetrigger(int32_t delayTime);
176     /**
177      * @brief Add app data clear listener.
178      *
179      * @param listener The listener.
180      */
181     void AddAppDataClearListener(std::shared_ptr<AppDataClearListener> listener);
182     /**
183      * @brief Watchdog time out.
184      *
185      * @param watchdogId The id of watchdog.
186      */
187     void WatchdogTimeOut(uint32_t watchdogId);
188     /**
189      * @brief Set memory by dump.
190      *
191      * @param memory The memory.
192      */
193     void SetMemoryByDump(int32_t memory);
194     /**
195      * @brief Get dump set memory.
196      *
197      * @return Dump set memory.
198      */
199     int32_t GetDumpSetMemory();
200     /**
201      * @brief Set watchdog time by dump.
202      *
203      * @param time The time.
204      */
205     void SetWatchdogTimeByDump(int32_t time);
206     /**
207      * @brief Set watchdog time.
208      *
209      * @param time The time.
210      */
211     void SetWatchdogTime(int32_t time);
212     /**
213      * @brief Get the time of watchdog.
214      *
215      * @return The time of watchdog.
216      */
217     int32_t GetWatchdogTime();
218     /**
219      * @brief Trigger the ide work.
220      */
221     void TriggerIdeWork();
222     /**
223      * @brief Provide a command to exec onStart and onStop of the matched service extension.
224      * @param bunlerName The bundleName.
225      * @param abilityName The abilityName.
226      */
227     void DumpCheckIdeWorkToRun(const std::string &bundleName, const std::string &abilityName);
228     /**
229      * @brief Set cpu by dump.
230      *
231      * @param cpu The cpu.
232      */
233     void SetCpuUsageByDump(int32_t cpu);
234     /**
235      * @brief Get dump set cpu.
236      *
237      * @return Dump set cpu.
238      */
239     int32_t GetDumpSetCpuUsage();
240     /**
241      * @brief Set max running count by dump.
242      *
243      * @param count The count.
244      */
245     void SetMaxRunningCountByDump(int32_t count);
246     /**
247      * @brief Get dump set max running count.
248      *
249      * @return Dump set max running count.
250      */
251     int32_t GetDumpSetMaxRunningCount();
252 
253     /**
254      * @brief Pause Running Works.
255      *
256      * @param uid The uid.
257      * @return The errcode. ERR_OK on success, others on failure.
258      */
259     int32_t PauseRunningWorks(int32_t uid);
260 
261     /**
262      * @brief Resume Paused works.
263      *
264      * @param uid The uid.
265      * @return ErrCode ERR_OK on success, others on failure
266      */
267     int32_t ResumePausedWorks(int32_t uid);
268 
269     /**
270      * @brief Remove watchdog From Queue.
271      *
272      * @param workStatus The workStatus.
273      */
274     void RemoveWatchDog(std::shared_ptr<WorkStatus> workStatus);
275 
276     /**
277      * @brief Get All DeepIdle Works.
278      *
279      * @return All DeepIdle Works.
280      */
281     std::list<std::shared_ptr<WorkStatus>> GetDeepIdleWorks();
282     bool FindWork(int32_t uid);
283     bool FindWork(const int32_t userId, const std::string &bundleName);
284 private:
285     int32_t GetMaxRunningCount(std::string& policyName);
286     int32_t GetRunningCount();
287     void DumpConditionReadyQueue(std::string& result);
288     void DumpUidQueueMap(std::string& result);
289     void RemoveFromUidQueue(std::shared_ptr<WorkStatus> workStatus, int32_t uid);
290     void RemoveFromReadyQueue(std::shared_ptr<WorkStatus> workStatus);
291     void AddToReadyQueue(std::shared_ptr<std::vector<std::shared_ptr<WorkStatus>>> workStatusVector);
292     void RealStartWork(std::shared_ptr<WorkStatus> workStatus);
293     void RealStartSA(std::shared_ptr<WorkStatus> workStatus);
294     void AddToRunningQueue(std::shared_ptr<WorkStatus> workStatus);
295     void RemoveConditionUnReady();
296     std::shared_ptr<WorkStatus> GetWorkToRun();
297     void RemoveAllUnReady();
298     uint32_t NewWatchdogId();
299     void AddWatchdogForWork(std::shared_ptr<WorkStatus> workStatus);
300     std::shared_ptr<WorkStatus> GetWorkFromWatchdog(uint32_t id);
301     void UpdateWatchdogTime(const std::shared_ptr<WorkSchedulerService> &wmsptr,
302         std::shared_ptr<WorkStatus> &topWork);
303     std::list<std::shared_ptr<WorkStatus>> GetAllIdeWorkStatus(const std::string &bundleName,
304         const std::string &abilityName);
305     void SendIdeWorkRetriggerEvent(int32_t delaytime);
306     bool IsSpecialScene(std::shared_ptr<WorkStatus> topWork, int32_t runningCount);
307     std::string GetConditionString(const std::shared_ptr<WorkStatus> workStatus);
308 
309     const std::weak_ptr<WorkSchedulerService> wss_;
310     std::shared_ptr<WorkConnManager> workConnManager_;
311     std::shared_ptr<WorkEventHandler> handler_;
312 
313     ffrt::recursive_mutex uidMapMutex_;
314     std::map<int32_t, std::shared_ptr<WorkQueue>> uidQueueMap_;
315 
316     std::shared_ptr<WorkQueue> conditionReadyQueue_;
317 
318     std::list<std::shared_ptr<IPolicyFilter>> policyFilters_;
319     std::shared_ptr<AppDataClearListener> appDataClearListener_;
320 
321     std::shared_ptr<Watchdog> watchdog_;
322 
323     ffrt::mutex watchdogIdMapMutex_;
324     std::map<uint32_t, std::shared_ptr<WorkStatus>> watchdogIdMap_;
325 
326     uint32_t watchdogId_;
327     int32_t dumpSetMemory_;
328     int32_t watchdogTime_;
329     int32_t dumpSetCpu_;
330     int32_t dumpSetMaxRunningCount_;
331 
332     ffrt::recursive_mutex ideDebugListMutex_;
333     std::list<std::shared_ptr<WorkStatus>> ideDebugList;
334 };
335 } // namespace WorkScheduler
336 } // namespace OHOS
337 
338 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_SCHED_SERVICES_POLICY_MANAGER_H
339