• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 work status.
110      *
111      * @param uId The uId.
112      * @param workId The workId.
113      * @return The workStatus of workId.
114      */
115     std::shared_ptr<WorkStatus> FindWorkStatus(int32_t uId, int32_t workId);
116     /**
117      * @brief Find SA.
118      *
119      * @param saId The id of sa.
120      * @param uid The uid.
121      * @return The SA of uid.
122      */
123     std::shared_ptr<WorkStatus> FindSA(int32_t saId, int32_t uid);
124     /**
125      * @brief The OnConditionReady callback.
126      *
127      * @param workStatusVector The work status vector.
128      */
129     void OnConditionReady(std::shared_ptr<std::vector<std::shared_ptr<WorkStatus>>> workStatusVector);
130     /**
131      * @brief The OnPolicyChanged callback.
132      *
133      * @param policyType The policy type.
134      * @param detectorVal The detector val.
135      */
136     void OnPolicyChanged(PolicyType policyType, std::shared_ptr<DetectorValue> detectorVal);
137     /**
138      * @brief Obtain all works.
139      *
140      * @param uid The uid.
141      * @return All works.
142      */
143     std::vector<WorkInfo> ObtainAllWorks(int32_t &uid);
144     /**
145      * @brief Get the status of work.
146      *
147      * @param uid The uid.
148      * @param workInfo The info of work.
149      * @return The status of work.
150      */
151     std::shared_ptr<WorkInfo> GetWorkStatus(int32_t &uid, int32_t &workInfo);
152     /**
153      * @brief Get all status of work.
154      *
155      * @param uid The uid.
156      * @return All status of work.
157      */
158     std::list<std::shared_ptr<WorkStatus>> GetAllWorkStatus(int32_t &uid);
159 
160     /**
161      * @brief Get the All Running Works object.
162      *
163      * @return All running works;
164      */
165     std::vector<WorkInfo> GetAllRunningWorks();
166 
167     /**
168      * @brief The Dump.
169      *
170      * @param result The result.
171      */
172     void Dump(std::string& result);
173     int32_t currentRunningCount = 0;
174     /**
175      * @brief Check work to run.
176      */
177     void CheckWorkToRun();
178     /**
179      * @brief Send retrigger.
180      *
181      * @param delayTime The delay time.
182      */
183     void SendRetrigger(int32_t delayTime);
184     /**
185      * @brief Add app data clear listener.
186      *
187      * @param listener The listener.
188      */
189     void AddAppDataClearListener(std::shared_ptr<AppDataClearListener> listener);
190     /**
191      * @brief Watchdog time out.
192      *
193      * @param watchdogId The id of watchdog.
194      */
195     void WatchdogTimeOut(uint32_t watchdogId);
196     /**
197      * @brief Set memory by dump.
198      *
199      * @param memory The memory.
200      */
201     void SetMemoryByDump(int32_t memory);
202     /**
203      * @brief Get dump set memory.
204      *
205      * @return Dump set memory.
206      */
207     int32_t GetDumpSetMemory();
208     /**
209      * @brief Set watchdog time by dump.
210      *
211      * @param time The time.
212      */
213     void SetWatchdogTimeByDump(int32_t time);
214     /**
215      * @brief Set watchdog time.
216      *
217      * @param time The time.
218      */
219     void SetWatchdogTime(int32_t time);
220     /**
221      * @brief Get the time of watchdog.
222      *
223      * @return The time of watchdog.
224      */
225     int32_t GetWatchdogTime();
226     /**
227      * @brief Trigger the ide work.
228      */
229     void TriggerIdeWork();
230     /**
231      * @brief Provide a command to exec onStart and onStop of the matched service extension.
232      * @param bunlerName The bundleName.
233      * @param abilityName The abilityName.
234      */
235     void DumpCheckIdeWorkToRun(const std::string &bundleName, const std::string &abilityName);
236     /**
237      * @brief Provide a command to exec onStart and onStop of the matched service extension.
238      * @param uId The uId.
239      * @param workId The workId.
240      * @param result The result.
241      */
242     void DumpTriggerWork(int32_t uId, int32_t workId, std::string& result);
243     /**
244      * @brief Set cpu by dump.
245      *
246      * @param cpu The cpu.
247      */
248     void SetCpuUsageByDump(int32_t cpu);
249     /**
250      * @brief Get dump set cpu.
251      *
252      * @return Dump set cpu.
253      */
254     int32_t GetDumpSetCpuUsage();
255     /**
256      * @brief Set max running count by dump.
257      *
258      * @param count The count.
259      */
260     void SetMaxRunningCountByDump(int32_t count);
261     /**
262      * @brief Get dump set max running count.
263      *
264      * @return Dump set max running count.
265      */
266     int32_t GetDumpSetMaxRunningCount();
267 
268     /**
269      * @brief Pause Running Works.
270      *
271      * @param uid The uid.
272      * @return The errcode. ERR_OK on success, others on failure.
273      */
274     int32_t PauseRunningWorks(int32_t uid);
275 
276     /**
277      * @brief Resume Paused works.
278      *
279      * @param uid The uid.
280      * @return ErrCode ERR_OK on success, others on failure
281      */
282     int32_t ResumePausedWorks(int32_t uid);
283 
284     /**
285      * @brief Remove watchdog From Queue.
286      *
287      * @param workStatus The workStatus.
288      */
289     void RemoveWatchDog(std::shared_ptr<WorkStatus> workStatus);
290 
291     /**
292      * @brief Get All DeepIdle Works.
293      *
294      * @return All DeepIdle Works.
295      */
296     std::list<std::shared_ptr<WorkStatus>> GetDeepIdleWorks();
297     bool FindWork(int32_t uid);
298     bool FindWork(const int32_t userId, const std::string &bundleName);
299 private:
300     int32_t GetMaxRunningCount(WorkSchedSystemPolicy& systemPolicy);
301     int32_t GetRunningCount();
302     void DumpConditionReadyQueue(std::string& result);
303     void DumpUidQueueMap(std::string& result);
304     void RemoveFromUidQueue(std::shared_ptr<WorkStatus> workStatus, int32_t uid);
305     void RemoveFromReadyQueue(std::shared_ptr<WorkStatus> workStatus);
306     void AddToReadyQueue(std::shared_ptr<std::vector<std::shared_ptr<WorkStatus>>> workStatusVector);
307     void RealStartWork(std::shared_ptr<WorkStatus> workStatus);
308     void RealStartSA(std::shared_ptr<WorkStatus> workStatus);
309     void AddToRunningQueue(std::shared_ptr<WorkStatus> workStatus);
310     void RemoveConditionUnReady();
311     std::shared_ptr<WorkStatus> GetWorkToRun();
312     void RemoveAllUnReady();
313     uint32_t NewWatchdogId();
314     void AddWatchdogForWork(std::shared_ptr<WorkStatus> workStatus);
315     std::shared_ptr<WorkStatus> GetWorkFromWatchdog(uint32_t id);
316     void UpdateWatchdogTime(const std::shared_ptr<WorkSchedulerService> &wmsptr,
317         std::shared_ptr<WorkStatus> &topWork);
318     std::list<std::shared_ptr<WorkStatus>> GetAllIdeWorkStatus(const std::string &bundleName,
319         const std::string &abilityName);
320     void SendIdeWorkRetriggerEvent(int32_t delaytime);
321     bool IsSpecialScene(std::shared_ptr<WorkStatus> topWork, int32_t runningCount);
322     std::string GetConditionString(const std::shared_ptr<WorkStatus> workStatus);
323     bool HasSystemPolicyEventSend() const;
324     void SetSystemPolicyEventSend(bool systemPolicyEventSend);
325 
326     const std::weak_ptr<WorkSchedulerService> wss_;
327     std::shared_ptr<WorkConnManager> workConnManager_;
328     std::shared_ptr<WorkEventHandler> handler_;
329 
330     ffrt::recursive_mutex uidMapMutex_;
331     std::map<int32_t, std::shared_ptr<WorkQueue>> uidQueueMap_;
332 
333     std::shared_ptr<WorkQueue> conditionReadyQueue_;
334 
335     std::list<std::shared_ptr<IPolicyFilter>> policyFilters_;
336     std::shared_ptr<AppDataClearListener> appDataClearListener_;
337 
338     std::shared_ptr<Watchdog> watchdog_;
339 
340     ffrt::mutex watchdogIdMapMutex_;
341     std::map<uint32_t, std::shared_ptr<WorkStatus>> watchdogIdMap_;
342 
343     uint32_t watchdogId_;
344     int32_t dumpSetMemory_;
345     int32_t watchdogTime_;
346     int32_t dumpSetCpu_;
347     int32_t dumpSetMaxRunningCount_;
348 
349     ffrt::recursive_mutex ideDebugListMutex_;
350     std::list<std::shared_ptr<WorkStatus>> ideDebugList;
351     std::atomic<bool> systemPolicyEventSend_ {false};
352 };
353 } // namespace WorkScheduler
354 } // namespace OHOS
355 
356 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_SCHED_SERVICES_POLICY_MANAGER_H
357