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