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 #include "cgroup_event_handler.h"
17 #include <cinttypes>
18 #include "app_mgr_constants.h"
19 #include "cgroup_adjuster.h"
20 #include "cgroup_sched_common.h"
21 #include "cgroup_sched_log.h"
22 #include "hisysevent.h"
23 #include "ressched_utils.h"
24 #include "res_type.h"
25 #include "sched_controller.h"
26 #include "sched_policy.h"
27 #include "system_ability_definition.h"
28
29 namespace OHOS {
30 namespace ResourceSchedule {
31 namespace {
32 constexpr HiviewDFX::HiLogLabel LOG_LABEL = {LOG_CORE, LOG_TAG_DOMAIN_ID_RMS, "CgroupEventHandler"};
33 constexpr uint32_t EVENT_ID_REG_APP_STATE_OBSERVER = 1;
34 constexpr uint32_t EVENT_ID_REG_BGTASK_OBSERVER = 2;
35 constexpr uint32_t DELAYED_RETRY_REGISTER_DURATION = 100;
36 constexpr uint32_t MAX_RETRY_TIMES = 100;
37
38 const std::string MMI_SERVICE_NAME = "mmi_service";
39 }
40
41 using OHOS::AppExecFwk::ApplicationState;
42 using OHOS::AppExecFwk::AbilityState;
43 using OHOS::AppExecFwk::ExtensionState;
44 using OHOS::AppExecFwk::ProcessType;
45
CgroupEventHandler(const std::shared_ptr<EventRunner> & runner)46 CgroupEventHandler::CgroupEventHandler(const std::shared_ptr<EventRunner> &runner)
47 : EventHandler(runner)
48 {}
49
~CgroupEventHandler()50 CgroupEventHandler::~CgroupEventHandler()
51 {
52 supervisor_ = nullptr;
53 }
54
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)55 void CgroupEventHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
56 {
57 CGS_LOGD("%{public}s : eventId:%{public}d param:%{public}" PRIu64,
58 __func__, event->GetInnerEventId(), event->GetParam());
59 switch (event->GetInnerEventId()) {
60 case EVENT_ID_REG_APP_STATE_OBSERVER: {
61 int64_t retry = event->GetParam();
62 if (!SchedController::GetInstance().SubscribeAppState() &&
63 retry < MAX_RETRY_TIMES) {
64 auto event = AppExecFwk::InnerEvent::Get(EVENT_ID_REG_APP_STATE_OBSERVER, retry + 1);
65 this->SendEvent(event, DELAYED_RETRY_REGISTER_DURATION);
66 if(retry + 1 == static_cast<int64_t>(MAX_RETRY_TIMES)) {
67 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
68 "COMPONENT_NAME", "MAIN",
69 "ERR_TYPE", "register failure",
70 "ERR_MSG", "Subscribe app status change observer failed.");
71 }
72 }
73 break;
74 }
75 case EVENT_ID_REG_BGTASK_OBSERVER: {
76 int64_t retry = event->GetParam();
77 if (!SchedController::GetInstance().SubscribeBackgroundTask() &&
78 retry < MAX_RETRY_TIMES) {
79 auto event = AppExecFwk::InnerEvent::Get(EVENT_ID_REG_BGTASK_OBSERVER, retry + 1);
80 this->SendEvent(event, DELAYED_RETRY_REGISTER_DURATION);
81 if(retry + 1 == static_cast<int64_t>(MAX_RETRY_TIMES)) {
82 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
83 "COMPONENT_NAME", "MAIN",
84 "ERR_TYPE", "register failure",
85 "ERR_MSG", "Subscribe background task observer failed.");
86 }
87 }
88 break;
89 }
90 default:
91 break;
92 }
93 }
94
SetSupervisor(std::shared_ptr<Supervisor> supervisor)95 void CgroupEventHandler::SetSupervisor(std::shared_ptr<Supervisor> supervisor)
96 {
97 supervisor_ = supervisor;
98 }
99
HandleAbilityAdded(int32_t saId,const std::string & deviceId)100 void CgroupEventHandler::HandleAbilityAdded(int32_t saId, const std::string& deviceId)
101 {
102 switch (saId) {
103 case APP_MGR_SERVICE_ID:
104 this->RemoveEvent(EVENT_ID_REG_APP_STATE_OBSERVER);
105 if (!SchedController::GetInstance().SubscribeAppState()) {
106 auto event = AppExecFwk::InnerEvent::Get(EVENT_ID_REG_APP_STATE_OBSERVER, 0);
107 this->SendEvent(event, DELAYED_RETRY_REGISTER_DURATION);
108 }
109 break;
110 case WINDOW_MANAGER_SERVICE_ID:
111 SchedController::GetInstance().SubscribeWindowState();
112 break;
113 case BACKGROUND_TASK_MANAGER_SERVICE_ID:
114 this->RemoveEvent(EVENT_ID_REG_BGTASK_OBSERVER);
115 if (!SchedController::GetInstance().SubscribeBackgroundTask()) {
116 auto event = AppExecFwk::InnerEvent::Get(EVENT_ID_REG_BGTASK_OBSERVER, 0);
117 this->SendEvent(event, DELAYED_RETRY_REGISTER_DURATION);
118 }
119 break;
120 default:
121 break;
122 }
123 }
124
HandleAbilityRemoved(int32_t saId,const std::string & deviceId)125 void CgroupEventHandler::HandleAbilityRemoved(int32_t saId, const std::string& deviceId)
126 {
127 switch (saId) {
128 case APP_MGR_SERVICE_ID:
129 this->RemoveEvent(EVENT_ID_REG_APP_STATE_OBSERVER);
130 SchedController::GetInstance().UnsubscribeAppState();
131 break;
132 case WINDOW_MANAGER_SERVICE_ID:
133 SchedController::GetInstance().UnsubscribeWindowState();
134 break;
135 case BACKGROUND_TASK_MANAGER_SERVICE_ID:
136 this->RemoveEvent(EVENT_ID_REG_BGTASK_OBSERVER);
137 SchedController::GetInstance().UnsubscribeBackgroundTask();
138 break;
139 default:
140 break;
141 }
142 }
143
HandleForegroundApplicationChanged(uid_t uid,pid_t pid,const std::string & bundleName,int32_t state)144 void CgroupEventHandler::HandleForegroundApplicationChanged(uid_t uid, pid_t pid,
145 const std::string& bundleName, int32_t state)
146 {
147 if (!supervisor_) {
148 CGS_LOGE("%{public}s : supervisor nullptr!", __func__);
149 return;
150 }
151 CGS_LOGD("%{public}s : %{public}d, %{public}s, %{public}d", __func__, uid, bundleName.c_str(), state);
152 ChronoScope cs("HandleForegroundApplicationChanged");
153 std::shared_ptr<Application> app = supervisor_->GetAppRecordNonNull(uid);
154 std::shared_ptr<ProcessRecord> procRecord = app->GetProcessRecordNonNull(pid);
155 app->SetName(bundleName);
156 app->state_ = state;
157 app->SetMainProcess(procRecord);
158 CgroupAdjuster::GetInstance().AdjustAllProcessGroup(*(app.get()), AdjustSource::ADJS_FG_APP_CHANGE);
159 }
160
HandleApplicationStateChanged(uid_t uid,pid_t pid,const std::string & bundleName,int32_t state)161 void CgroupEventHandler::HandleApplicationStateChanged(uid_t uid, pid_t pid,
162 const std::string& bundleName, int32_t state)
163 {
164 if (!supervisor_) {
165 CGS_LOGE("%{public}s : supervisor nullptr!", __func__);
166 return;
167 }
168 CGS_LOGD("%{public}s : %{public}d, %{public}s, %{public}d", __func__, uid, bundleName.c_str(), state);
169 ChronoScope cs("HandleApplicationStateChanged");
170 // remove terminated application
171 if (state == (int32_t)(ApplicationState::APP_STATE_TERMINATED)) {
172 supervisor_->RemoveApplication(uid);
173 return;
174 }
175 std::shared_ptr<Application> app = supervisor_->GetAppRecordNonNull(uid);
176 std::shared_ptr<ProcessRecord> procRecord = app->GetProcessRecordNonNull(pid);
177 app->SetName(bundleName);
178 app->state_ = state;
179 app->SetMainProcess(procRecord);
180 }
181
HandleAbilityStateChanged(uid_t uid,pid_t pid,const std::string & bundleName,const std::string & abilityName,uintptr_t token,int32_t abilityState,int32_t abilityType)182 void CgroupEventHandler::HandleAbilityStateChanged(uid_t uid, pid_t pid, const std::string& bundleName,
183 const std::string& abilityName, uintptr_t token, int32_t abilityState, int32_t abilityType)
184 {
185 if (!supervisor_) {
186 CGS_LOGE("%{public}s : supervisor nullptr!", __func__);
187 return;
188 }
189 CGS_LOGD("%{public}s : %{public}d, %{public}d, %{public}s, %{public}s, %{public}d, %{public}d",
190 __func__, uid, pid, bundleName.c_str(), abilityName.c_str(), abilityState, abilityType);
191 ChronoScope cs("HandleAbilityStateChanged");
192 if (abilityState == (int32_t)(AbilityState::ABILITY_STATE_TERMINATED)) {
193 auto app = supervisor_->GetAppRecord(uid);
194 if (app) {
195 auto procRecord = app->GetProcessRecord(pid);
196 if (procRecord) {
197 procRecord->RemoveAbilityByToken(token);
198 CgroupAdjuster::GetInstance().AdjustProcessGroup(*(app.get()), *(procRecord.get()),
199 AdjustSource::ADJS_ABILITY_STATE);
200 }
201 }
202 return;
203 }
204 auto app = supervisor_->GetAppRecordNonNull(uid);
205 app->SetName(bundleName);
206 auto procRecord = app->GetProcessRecordNonNull(pid);
207 auto abiInfo = procRecord->GetAbilityInfoNonNull(token);
208 abiInfo->name_ = abilityName;
209 abiInfo->state_ = abilityState;
210 abiInfo->type_ = abilityType;
211 CgroupAdjuster::GetInstance().AdjustProcessGroup(*(app.get()), *(procRecord.get()),
212 AdjustSource::ADJS_ABILITY_STATE);
213 }
214
HandleExtensionStateChanged(uid_t uid,pid_t pid,const std::string & bundleName,const std::string & abilityName,uintptr_t token,int32_t extensionState,int32_t abilityType)215 void CgroupEventHandler::HandleExtensionStateChanged(uid_t uid, pid_t pid, const std::string& bundleName,
216 const std::string& abilityName, uintptr_t token, int32_t extensionState, int32_t abilityType)
217 {
218 if (!supervisor_) {
219 CGS_LOGE("%{public}s : supervisor nullptr!", __func__);
220 return;
221 }
222 CGS_LOGD("%{public}s : %{public}d, %{public}d, %{public}s, %{public}s, %{public}d, %{public}d",
223 __func__, uid, pid, bundleName.c_str(), abilityName.c_str(), extensionState, abilityType);
224 ChronoScope cs("HandleExtensionStateChanged");
225 if (extensionState == (int32_t)(ExtensionState::EXTENSION_STATE_TERMINATED)) {
226 auto app = supervisor_->GetAppRecord(uid);
227 if (app) {
228 auto procRecord = app->GetProcessRecord(pid);
229 if (procRecord) {
230 procRecord->RemoveAbilityByToken(token);
231 CgroupAdjuster::GetInstance().AdjustProcessGroup(*(app.get()), *(procRecord.get()),
232 AdjustSource::ADJS_EXTENSION_STATE);
233 }
234 }
235 return;
236 }
237 auto app = supervisor_->GetAppRecordNonNull(uid);
238 app->SetName(bundleName);
239 auto procRecord = app->GetProcessRecordNonNull(pid);
240 auto abiInfo = procRecord->GetAbilityInfoNonNull(token);
241 abiInfo->name_ = abilityName;
242 abiInfo->estate_ = extensionState;
243 abiInfo->type_ = abilityType;
244 CgroupAdjuster::GetInstance().AdjustProcessGroup(*(app.get()), *(procRecord.get()),
245 AdjustSource::ADJS_EXTENSION_STATE);
246 }
247
HandleProcessCreated(uid_t uid,pid_t pid,int32_t processType,const std::string & bundleName)248 void CgroupEventHandler::HandleProcessCreated(uid_t uid, pid_t pid, int32_t processType,
249 const std::string& bundleName)
250 {
251 if (!supervisor_) {
252 CGS_LOGE("%{public}s : supervisor nullptr!", __func__);
253 return;
254 }
255 CGS_LOGD("%{public}s : %{public}d, %{public}d, %{public}s", __func__, uid, pid, bundleName.c_str());
256 ChronoScope cs("HandleProcessCreated");
257 std::shared_ptr<Application> app = supervisor_->GetAppRecordNonNull(uid);
258 std::shared_ptr<ProcessRecord> procRecord = app->GetProcessRecordNonNull(pid);
259 app->SetName(bundleName);
260 if (processType == static_cast<int32_t>(ProcessType::NORMAL)) {
261 app->SetMainProcess(procRecord);
262 } else if (processType == static_cast<int32_t>(ProcessType::RENDER)) {
263 procRecord->isRenderProcess_ = true;
264 }
265 CgroupAdjuster::GetInstance().AdjustProcessGroup(*(app.get()), *(procRecord.get()),
266 AdjustSource::ADJS_PROCESS_CREATE);
267 }
268
HandleProcessDied(uid_t uid,pid_t pid,const std::string & bundleName)269 void CgroupEventHandler::HandleProcessDied(uid_t uid, pid_t pid, const std::string& bundleName)
270 {
271 if (!supervisor_) {
272 CGS_LOGE("%{public}s : supervisor nullptr!", __func__);
273 return;
274 }
275 CGS_LOGD("%{public}s : %{public}d, %{public}d, %{public}s", __func__, uid, pid, bundleName.c_str());
276 std::shared_ptr<Application> app = supervisor_->GetAppRecord(uid);
277 if (!app) {
278 CGS_LOGE("%{public}s : application %{public}s not exist!", __func__, bundleName.c_str());
279 return;
280 }
281 app->RemoveProcessRecord(pid);
282 // if all processes died, remove current app
283 if (app->GetPidsMap().size() == 0) {
284 supervisor_->RemoveApplication(uid);
285 }
286 }
287
HandleTransientTaskStart(uid_t uid,pid_t pid,const std::string & packageName)288 void CgroupEventHandler::HandleTransientTaskStart(uid_t uid, pid_t pid, const std::string& packageName)
289 {
290 if (!supervisor_) {
291 CGS_LOGE("%{public}s : supervisor nullptr!", __func__);
292 return;
293 }
294 CGS_LOGD("%{public}s : %{public}d, %{public}d, %{public}s", __func__, uid, pid, packageName.c_str());
295 auto app = supervisor_->GetAppRecordNonNull(uid);
296 app->SetName(packageName);
297 auto procRecord = app->GetProcessRecord(pid);
298 if (!procRecord) {
299 return;
300 }
301 procRecord->runningTransientTask_ = true;
302 }
303
HandleTransientTaskEnd(uid_t uid,pid_t pid,const std::string & packageName)304 void CgroupEventHandler::HandleTransientTaskEnd(uid_t uid, pid_t pid, const std::string& packageName)
305 {
306 if (!supervisor_) {
307 CGS_LOGE("%{public}s : supervisor nullptr!", __func__);
308 return;
309 }
310 CGS_LOGD("%{public}s : %{public}d, %{public}d, %{public}s", __func__, uid, pid, packageName.c_str());
311 auto app = supervisor_->GetAppRecordNonNull(uid);
312 app->SetName(packageName);
313 auto procRecord = app->GetProcessRecord(pid);
314 if (!procRecord) {
315 return;
316 }
317 procRecord->runningTransientTask_ = false;
318 }
319
HandleContinuousTaskStart(uid_t uid,pid_t pid,int32_t typeId,const std::string & abilityName)320 void CgroupEventHandler::HandleContinuousTaskStart(uid_t uid, pid_t pid, int32_t typeId,
321 const std::string& abilityName)
322 {
323 if (!supervisor_) {
324 CGS_LOGE("%{public}s : supervisor nullptr!", __func__);
325 return;
326 }
327 CGS_LOGD("%{public}s : %{public}d, %{public}d, %{public}d, %{public}s",
328 __func__, uid, pid, typeId, abilityName.c_str());
329 ChronoScope cs("HandleContinuousTaskStart");
330 auto app = supervisor_->GetAppRecordNonNull(uid);
331 auto procRecord = app->GetProcessRecordNonNull(pid);
332 procRecord->continuousTaskFlag_ |= (1U << typeId);
333 CgroupAdjuster::GetInstance().AdjustProcessGroup(*(app.get()), *(procRecord.get()),
334 AdjustSource::ADJS_CONTINUOUS_BEGIN);
335 }
336
HandleContinuousTaskCancel(uid_t uid,pid_t pid,int32_t typeId,const std::string & abilityName)337 void CgroupEventHandler::HandleContinuousTaskCancel(uid_t uid, pid_t pid, int32_t typeId,
338 const std::string& abilityName)
339 {
340 if (!supervisor_) {
341 CGS_LOGE("%{public}s : supervisor nullptr!", __func__);
342 return;
343 }
344 CGS_LOGD("%{public}s : %{public}d, %{public}d, %{public}d, %{public}s",
345 __func__, uid, pid, typeId, abilityName.c_str());
346 ChronoScope cs("HandleContinuousTaskCancel");
347 auto app = supervisor_->GetAppRecordNonNull(uid);
348 auto procRecord = app->GetProcessRecord(pid);
349 if (!procRecord) {
350 return;
351 }
352 procRecord->continuousTaskFlag_ &= ~(1U << typeId);
353 CgroupAdjuster::GetInstance().AdjustProcessGroup(*(app.get()), *(procRecord.get()),
354 AdjustSource::ADJS_CONTINUOUS_END);
355 }
356
HandleFocusedWindow(uint32_t windowId,uintptr_t abilityToken,WindowType windowType,uint64_t displayId,int32_t pid,int32_t uid)357 void CgroupEventHandler::HandleFocusedWindow(uint32_t windowId, uintptr_t abilityToken,
358 WindowType windowType, uint64_t displayId, int32_t pid, int32_t uid)
359 {
360 if (!supervisor_) {
361 CGS_LOGE("%{public}s : supervisor nullptr!", __func__);
362 return;
363 }
364 CGS_LOGD("%{public}s : %{public}d, %{public}d, %{public}" PRIu64 ", %{public}d, %{public}d",
365 __func__, windowId, windowType, displayId, pid, uid);
366 if (!abilityToken) {
367 CGS_LOGW("%{public}s : abilityToken nullptr!", __func__);
368 }
369 std::shared_ptr<Application> app = nullptr;
370 std::shared_ptr<ProcessRecord> procRecord = nullptr;
371 {
372 ChronoScope cs("HandleFocusedWindow");
373 app = supervisor_->GetAppRecordNonNull(uid);
374 procRecord = app->GetProcessRecordNonNull(pid);
375 auto win = procRecord->GetWindowInfoNonNull(windowId);
376 auto abi = procRecord->GetAbilityInfo(abilityToken);
377 win->windowType_ = (int32_t)(windowType);
378 win->isFocused_ = true;
379 win->displayId_ = displayId;
380 win->ability_ = abi;
381
382 app->focusedProcess_ = procRecord;
383 auto lastFocusApp = supervisor_->focusedApp_;
384 if (lastFocusApp && lastFocusApp != app) {
385 lastFocusApp->focusedProcess_ = nullptr;
386 CgroupAdjuster::GetInstance().AdjustAllProcessGroup(*(lastFocusApp.get()),
387 AdjustSource::ADJS_FOCUSED_WINDOW);
388 }
389 supervisor_->focusedApp_ = app;
390 CgroupAdjuster::GetInstance().AdjustAllProcessGroup(*(app.get()), AdjustSource::ADJS_FOCUSED_WINDOW);
391 }
392 if (app->GetName().empty()) {
393 app->SetName(SchedController::GetInstance().GetBundleNameByUid(uid));
394 }
395 }
396
HandleUnfocusedWindow(uint32_t windowId,uintptr_t abilityToken,WindowType windowType,uint64_t displayId,int32_t pid,int32_t uid)397 void CgroupEventHandler::HandleUnfocusedWindow(uint32_t windowId, uintptr_t abilityToken,
398 WindowType windowType, uint64_t displayId, int32_t pid, int32_t uid)
399 {
400 if (!supervisor_) {
401 CGS_LOGE("%{public}s : supervisor nullptr!", __func__);
402 return;
403 }
404 CGS_LOGD("%{public}s : %{public}d, %{public}d, %{public}" PRIu64 ", %{public}d, %{public}d",
405 __func__, windowId, windowType, displayId, pid, uid);
406 if (!abilityToken) {
407 CGS_LOGW("%{public}s : abilityToken nullptr!", __func__);
408 }
409 std::shared_ptr<Application> app = nullptr;
410 std::shared_ptr<ProcessRecord> procRecord = nullptr;
411 {
412 ChronoScope cs("HandleUnfocusedWindow");
413 app = supervisor_->GetAppRecord(uid);
414 procRecord = app ? app->GetProcessRecord(pid) : nullptr;
415 if (!app || !procRecord) {
416 return;
417 }
418 auto win = procRecord->GetWindowInfoNonNull(windowId);
419 auto abi = procRecord->GetAbilityInfo(abilityToken);
420 win->windowType_ = (int32_t)(windowType);
421 win->isFocused_ = false;
422 win->displayId_ = displayId;
423 win->ability_ = abi;
424
425 if (app->focusedProcess_ == procRecord) {
426 app->focusedProcess_ = nullptr;
427 }
428 CgroupAdjuster::GetInstance().AdjustAllProcessGroup(*(app.get()), AdjustSource::ADJS_UNFOCUSED_WINDOW);
429 }
430 if (app->GetName().empty()) {
431 app->SetName(SchedController::GetInstance().GetBundleNameByUid(uid));
432 }
433 }
434
HandleWindowVisibilityChanged(uint32_t windowId,bool isVisible,WindowType windowType,int32_t pid,int32_t uid)435 void CgroupEventHandler::HandleWindowVisibilityChanged(
436 uint32_t windowId, bool isVisible, WindowType windowType, int32_t pid, int32_t uid)
437 {
438 if (!supervisor_) {
439 CGS_LOGE("%{public}s : supervisor nullptr!", __func__);
440 return;
441 }
442 CGS_LOGD("%{public}s : %{public}d, %{public}d, %{public}d, %{public}d, %{public}d", __func__, windowId,
443 isVisible, (int32_t)windowType, pid, uid);
444
445 std::shared_ptr<Application> app = nullptr;
446 std::shared_ptr<ProcessRecord> procRecord = nullptr;
447 if (isVisible) {
448 app = supervisor_->GetAppRecordNonNull(uid);
449 procRecord = app->GetProcessRecordNonNull(pid);
450 } else {
451 app = supervisor_->GetAppRecord(uid);
452 if (app) {
453 procRecord = app->GetProcessRecord(pid);
454 }
455 }
456 if (!procRecord) {
457 return;
458 }
459 auto windowInfo = procRecord->GetWindowInfoNonNull(windowId);
460 windowInfo->isVisible_ = isVisible;
461 windowInfo->windowType_ = (int32_t)windowType;
462
463 CgroupAdjuster::GetInstance().AdjustProcessGroup(*(app.get()), *(procRecord.get()),
464 AdjustSource::ADJS_WINDOW_VISIBILITY_CHANGED);
465 }
466
HandleReportMMIProcess(uint32_t resType,int64_t value,const nlohmann::json & payload)467 void CgroupEventHandler::HandleReportMMIProcess(uint32_t resType, int64_t value, const nlohmann::json& payload)
468 {
469 int32_t uid = 0;
470 int32_t pid = 0;
471 int32_t mmi_service;
472
473 if (!supervisor_) {
474 CGS_LOGE("%{public}s : supervisor nullptr!", __func__);
475 return;
476 }
477
478 if (!ParsePayload(uid, pid, mmi_service, value, payload)) {
479 return;
480 }
481
482 CGS_LOGD("%{public}s : %{public}u, %{public}d, %{public}d, %{public}d",
483 __func__, resType, uid, pid, mmi_service);
484 if (uid <= 0 || pid <= 0 || mmi_service <= 0) {
485 return;
486 }
487
488 auto app = supervisor_->GetAppRecordNonNull(uid);
489 app->SetName(MMI_SERVICE_NAME);
490 auto procRecord = app->GetProcessRecordNonNull(mmi_service);
491 CgroupAdjuster::GetInstance().AdjustProcessGroup(*(app.get()), *(procRecord.get()),
492 AdjustSource::ADJS_REPORT_MMI_SERVICE_THREAD);
493 }
494
HandleReportRenderThread(uint32_t resType,int64_t value,const nlohmann::json & payload)495 void CgroupEventHandler::HandleReportRenderThread(uint32_t resType, int64_t value, const nlohmann::json& payload)
496 {
497 int32_t uid = 0;
498 int32_t pid = 0;
499 int32_t render = 0;
500
501 if (!supervisor_) {
502 CGS_LOGE("%{public}s : supervisor nullptr!", __func__);
503 return;
504 }
505
506 if (!ParsePayload(uid, pid, render, value, payload)) {
507 return;
508 }
509
510 CGS_LOGD("%{public}s : %{public}u, %{public}d, %{public}d, %{public}d",
511 __func__, resType, uid, pid, render);
512 if (uid <= 0 || pid <= 0 || render <= 0) {
513 return;
514 }
515
516 auto app = supervisor_->GetAppRecordNonNull(uid);
517 auto procRecord = app->GetProcessRecordNonNull(pid);
518 procRecord->renderTid_ = render;
519 CgroupAdjuster::GetInstance().AdjustProcessGroup(*(app.get()), *(procRecord.get()),
520 AdjustSource::ADJS_REPORT_RENDER_THREAD);
521 }
522
ParsePayload(int32_t & uid,int32_t & pid,int32_t & tid,int64_t value,const nlohmann::json & payload)523 bool CgroupEventHandler::ParsePayload(int32_t& uid, int32_t& pid, int32_t& tid,
524 int64_t value, const nlohmann::json& payload)
525 {
526 if (payload.contains("uid") && payload.at("uid").is_string()) {
527 uid = atoi(payload["uid"].get<std::string>().c_str());
528 }
529
530 if (payload.contains("pid") && payload.at("pid").is_string()) {
531 pid = atoi(payload["pid"].get<std::string>().c_str());
532 }
533 tid = static_cast<int32_t>(value);
534 return true;
535 }
536 } // namespace ResourceSchedule
537 } // namespace OHOS
538