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 "hisysevent.h"
17 #include "res_sched_service_ability.h"
18 #include "observer_manager_intf.h"
19 #include "res_sched_log.h"
20 #include "res_sched_mgr.h"
21 #include "res_sched_service.h"
22 #include "event_controller_intf.h"
23 #include "system_ability_definition.h"
24 #include "cgroup_sched.h"
25
26 namespace OHOS {
27 namespace ResourceSchedule {
28 const bool REGISTER_RESULT =
29 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<ResSchedServiceAbility>::GetInstance().get());
30
ResSchedServiceAbility()31 ResSchedServiceAbility::ResSchedServiceAbility() : SystemAbility(RES_SCHED_SYS_ABILITY_ID, true)
32 {
33 }
34
~ResSchedServiceAbility()35 ResSchedServiceAbility::~ResSchedServiceAbility()
36 {
37 }
38
OnStart()39 void ResSchedServiceAbility::OnStart()
40 {
41 ResSchedMgr::GetInstance().Init();
42 if (!service_) {
43 service_ = new (std::nothrow) ResSchedService();
44 }
45 if (service_ == nullptr) {
46 RESSCHED_LOGE("ResSchedServiceAbility:: New ResSchedService failed.");
47 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
48 "COMPONENT_NAME", "MAIN",
49 "ERR_TYPE", "others",
50 "ERR_MSG", "New ResSchedService object failed!");
51 }
52 if (!Publish(service_)) {
53 RESSCHED_LOGE("ResSchedServiceAbility:: Register service failed.");
54 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
55 "COMPONENT_NAME", "MAIN",
56 "ERR_TYPE", "register failure",
57 "ERR_MSG", "Register ResSchedService failed.");
58 }
59 CgroupSchedInit();
60 if (!AddSystemAbilityListener(APP_MGR_SERVICE_ID)) {
61 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
62 "COMPONENT_NAME", "MAIN",
63 "ERR_TYPE", "register failure",
64 "ERR_MSG", "Register a listener of app manager service failed.");
65 }
66 if (!AddSystemAbilityListener(WINDOW_MANAGER_SERVICE_ID)) {
67 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
68 "COMPONENT_NAME", "MAIN",
69 "ERR_TYPE", "register failure",
70 "ERR_MSG", "Register a listener of window manager service failed.");
71 }
72 if (!AddSystemAbilityListener(BACKGROUND_TASK_MANAGER_SERVICE_ID)) {
73 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
74 "COMPONENT_NAME", "MAIN",
75 "ERR_TYPE", "register failure",
76 "ERR_MSG", "Register a listener of background task manager service failed.");
77 }
78 EventControllerInit();
79 ObserverManagerInit();
80 RESSCHED_LOGI("ResSchedServiceAbility ::OnStart.");
81 }
82
OnStop()83 void ResSchedServiceAbility::OnStop()
84 {
85 ObserverManagerDisable();
86 EventControllerStop();
87 RemoveSystemAbilityListener(BACKGROUND_TASK_MANAGER_SERVICE_ID);
88 RemoveSystemAbilityListener(WINDOW_MANAGER_SERVICE_ID);
89 RemoveSystemAbilityListener(APP_MGR_SERVICE_ID);
90 ResSchedMgr::GetInstance().Stop();
91 CgroupSchedDeinit();
92 RESSCHED_LOGI("ResSchedServiceAbility::OnStop!");
93 }
94
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)95 void ResSchedServiceAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
96 {
97 ReportAbilityStatus(systemAbilityId, deviceId, 1);
98 }
99
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)100 void ResSchedServiceAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
101 {
102 ReportAbilityStatus(systemAbilityId, deviceId, 0);
103 }
104 } // namespace ResourceSchedule
105 } // namespace OHOS
106
107