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 #include <hisysevent.h>
16 #include <ipc_skeleton.h>
17
18 #include "work_queue_manager.h"
19 #include "work_scheduler_service.h"
20 #include "work_sched_hilog.h"
21 #include "work_sched_utils.h"
22
23 using namespace std;
24
25 namespace OHOS {
26 namespace WorkScheduler {
27 const uint32_t TIME_CYCLE = 20 * 60 * 1000; // 20min
28 static int32_t g_timeRetrigger = INT32_MAX;
29
WorkQueueManager(const wptr<WorkSchedulerService> & wss)30 WorkQueueManager::WorkQueueManager(const wptr<WorkSchedulerService>& wss) : wss_(wss)
31 {
32 timeCycle_ = TIME_CYCLE;
33 }
34
Init()35 bool WorkQueueManager::Init()
36 {
37 return true;
38 }
39
AddListener(WorkCondition::Type type,shared_ptr<IConditionListener> listener)40 bool WorkQueueManager::AddListener(WorkCondition::Type type, shared_ptr<IConditionListener> listener)
41 {
42 std::lock_guard<std::mutex> lock(mutex_);
43 if (listenerMap_.count(type) > 0) {
44 return false;
45 }
46 listenerMap_.emplace(type, listener);
47 return true;
48 }
49
AddWork(shared_ptr<WorkStatus> workStatus)50 bool WorkQueueManager::AddWork(shared_ptr<WorkStatus> workStatus)
51 {
52 if (!workStatus || !workStatus->workInfo_ || !workStatus->workInfo_->GetConditionMap()) {
53 return false;
54 }
55 WS_HILOGD("workStatus ID: %{public}s", workStatus->workId_.c_str());
56 std::lock_guard<std::mutex> lock(mutex_);
57 auto map = workStatus->workInfo_->GetConditionMap();
58 for (auto it : *map) {
59 if (queueMap_.count(it.first) == 0) {
60 queueMap_.emplace(it.first, make_shared<WorkQueue>());
61 if (listenerMap_.count(it.first) != 0) {
62 listenerMap_.at(it.first)->Start();
63 }
64 }
65 queueMap_.at(it.first)->Push(workStatus);
66 }
67 if (WorkSchedUtils::IsSystemApp()) {
68 WS_HILOGI("Is system app, default group is active.");
69 workStatus->workInfo_->SetCallBySystemApp(true);
70 }
71 return true;
72 }
73
RemoveWork(shared_ptr<WorkStatus> workStatus)74 bool WorkQueueManager::RemoveWork(shared_ptr<WorkStatus> workStatus)
75 {
76 std::lock_guard<std::mutex> lock(mutex_);
77 WS_HILOGD("workStatus ID: %{public}s", workStatus->workId_.c_str());
78 auto map = workStatus->workInfo_->GetConditionMap();
79 for (auto it : *map) {
80 if (queueMap_.count(it.first) > 0) {
81 queueMap_.at(it.first)->Remove(workStatus);
82 }
83 if (queueMap_.count(it.first) == 0) {
84 listenerMap_.at(it.first)->Stop();
85 }
86 }
87 return true;
88 }
89
CancelWork(shared_ptr<WorkStatus> workStatus)90 bool WorkQueueManager::CancelWork(shared_ptr<WorkStatus> workStatus)
91 {
92 std::lock_guard<std::mutex> lock(mutex_);
93 WS_HILOGD("workStatus ID: %{public}s", workStatus->workId_.c_str());
94 for (auto it : queueMap_) {
95 it.second->CancelWork(workStatus);
96 if (queueMap_.count(it.first) == 0) {
97 listenerMap_.at(it.first)->Stop();
98 }
99 }
100 // Notify work remove event to battery statistics
101 int32_t pid = IPCSkeleton::GetCallingPid();
102 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::WORK_SCHEDULER,
103 "WORK_REMOVE", HiviewDFX::HiSysEvent::EventType::STATISTIC, "UID", workStatus->uid_,
104 "PID", pid, "NAME", workStatus->bundleName_, "WORKID", workStatus->workId_);
105 return true;
106 }
107
GetReayQueue(WorkCondition::Type conditionType,shared_ptr<DetectorValue> conditionVal)108 vector<shared_ptr<WorkStatus>> WorkQueueManager::GetReayQueue(WorkCondition::Type conditionType,
109 shared_ptr<DetectorValue> conditionVal)
110 {
111 vector<shared_ptr<WorkStatus>> result;
112 std::lock_guard<std::mutex> lock(mutex_);
113 if (conditionType != WorkCondition::Type::GROUP && queueMap_.count(conditionType) > 0) {
114 shared_ptr<WorkQueue> workQueue = queueMap_.at(conditionType);
115 result = workQueue->OnConditionChanged(conditionType, conditionVal);
116 }
117 if (conditionType == WorkCondition::Type::GROUP || conditionType == WorkCondition::Type::STANDBY) {
118 for (auto it : queueMap_) {
119 shared_ptr<WorkQueue> workQueue = it.second;
120 result = workQueue->OnConditionChanged(conditionType, conditionVal);
121 }
122 }
123 auto it = result.begin();
124 while (it != result.end()) {
125 if ((*it)->needRetrigger_) {
126 if (conditionType != WorkCondition::Type::TIMER
127 && conditionType != WorkCondition::Type::GROUP) {
128 WS_HILOGD("Need retrigger, start group listener.");
129 SetTimeRetrigger((*it)->timeRetrigger_);
130 listenerMap_.at(WorkCondition::Type::GROUP)->Start();
131 }
132 (*it)->needRetrigger_ = false;
133 (*it)->timeRetrigger_ = INT32_MAX;
134 it = result.erase(it);
135 } else {
136 ++it;
137 }
138 }
139 return result;
140 }
141
OnConditionChanged(WorkCondition::Type conditionType,shared_ptr<DetectorValue> conditionVal)142 void WorkQueueManager::OnConditionChanged(WorkCondition::Type conditionType,
143 shared_ptr<DetectorValue> conditionVal)
144 {
145 vector<shared_ptr<WorkStatus>> readyWorkVector = GetReayQueue(conditionType, conditionVal);
146 if (readyWorkVector.size() == 0) {
147 return;
148 }
149 for (auto it : readyWorkVector) {
150 it->MarkStatus(WorkStatus::Status::CONDITION_READY);
151 }
152 auto ws = wss_.promote();
153 ws->OnConditionReady(make_shared<vector<shared_ptr<WorkStatus>>>(readyWorkVector));
154 }
155
StopAndClearWorks(list<shared_ptr<WorkStatus>> workList)156 bool WorkQueueManager::StopAndClearWorks(list<shared_ptr<WorkStatus>> workList)
157 {
158 for (auto &it : workList) {
159 CancelWork(it);
160 }
161 return true;
162 }
163
Dump(string & result)164 void WorkQueueManager::Dump(string& result)
165 {
166 std::lock_guard<std::mutex> lock(mutex_);
167 string conditionType[] = {"network", "charger", "battery_status", "battery_level",
168 "storage", "timer", "unknown"};
169 uint32_t size = sizeof(conditionType);
170 for (auto it : queueMap_) {
171 if (it.first < size) {
172 result.append(conditionType[it.first]);
173 } else {
174 result.append(conditionType[size - 1]);
175 }
176 result.append(" : ");
177 result.append("[");
178 string workIdStr;
179 it.second->GetWorkIdStr(workIdStr);
180 result.append(workIdStr);
181 result.append("]\n");
182 }
183 }
184
SetTimeCycle(uint32_t time)185 void WorkQueueManager::SetTimeCycle(uint32_t time)
186 {
187 timeCycle_ = time;
188 }
189
GetTimeCycle()190 uint32_t WorkQueueManager::GetTimeCycle()
191 {
192 return timeCycle_;
193 }
194
SetTimeRetrigger(int32_t time)195 void WorkQueueManager::SetTimeRetrigger(int32_t time)
196 {
197 g_timeRetrigger = time;
198 }
199
GetTimeRetrigger()200 int32_t WorkQueueManager::GetTimeRetrigger()
201 {
202 return g_timeRetrigger;
203 }
204
SetMinIntervalByDump(int64_t interval)205 void WorkQueueManager::SetMinIntervalByDump(int64_t interval)
206 {
207 for (auto it : queueMap_) {
208 it.second->SetMinIntervalByDump(interval);
209 }
210 }
211 } // namespace WorkScheduler
212 } // namespace OHOS