1 /*
2 * Copyright (c) 2022-2024 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 "res_sched_service_ability.h"
17
18 #include "res_common_util.h"
19 #include "ffrt_inner.h"
20 #include "hisysevent.h"
21 #include "notifier_mgr.h"
22 #include "observer_manager_intf.h"
23 #include "res_sched_log.h"
24 #include "res_sched_mgr.h"
25 #include "res_sched_service.h"
26 #include "event_controller_intf.h"
27 #include "event_listener_mgr.h"
28 #include "system_ability_definition.h"
29 #include "cgroup_sched.h"
30
31 namespace OHOS {
32 namespace ResourceSchedule {
33 const bool REGISTER_RESULT =
34 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<ResSchedServiceAbility>::GetInstance().get());
35
ResSchedServiceAbility()36 ResSchedServiceAbility::ResSchedServiceAbility() : SystemAbility(RES_SCHED_SYS_ABILITY_ID, true)
37 {
38 }
39
~ResSchedServiceAbility()40 ResSchedServiceAbility::~ResSchedServiceAbility()
41 {
42 }
43
OnStart()44 void ResSchedServiceAbility::OnStart()
45 {
46 ResSchedMgr::GetInstance().Init();
47 NotifierMgr::GetInstance().Init();
48 EventListenerMgr::GetInstance().Init();
49 if (!service_) {
50 service_ = new (std::nothrow) ResSchedService();
51 }
52 if (service_ == nullptr) {
53 RESSCHED_LOGE("ResSchedServiceAbility:: New ResSchedService failed.");
54 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
55 "COMPONENT_NAME", "MAIN",
56 "ERR_TYPE", "others",
57 "ERR_MSG", "New ResSchedService object failed!");
58 }
59 if (!Publish(service_)) {
60 RESSCHED_LOGE("ResSchedServiceAbility:: Register service failed.");
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 ResSchedService failed.");
65 }
66 CgroupSchedInit();
67 SystemAbilityListenerInit();
68 EventControllerInit();
69 ObserverManagerInit();
70 ReclaimProcessMemory();
71 RESSCHED_LOGI("ResSchedServiceAbility ::OnStart.");
72 }
73
OnStop()74 void ResSchedServiceAbility::OnStop()
75 {
76 ObserverManagerDisable();
77 EventControllerStop();
78 RemoveSystemAbilityListener(BACKGROUND_TASK_MANAGER_SERVICE_ID);
79 RemoveSystemAbilityListener(WINDOW_MANAGER_SERVICE_ID);
80 RemoveSystemAbilityListener(APP_MGR_SERVICE_ID);
81 RemoveSystemAbilityListener(POWER_MANAGER_SERVICE_ID);
82 ResSchedMgr::GetInstance().Stop();
83 CgroupSchedDeinit();
84 RESSCHED_LOGI("ResSchedServiceAbility::OnStop!");
85 }
86
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)87 void ResSchedServiceAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
88 {
89 if (systemAbilityId == RES_SCHED_EXE_ABILITY_ID) {
90 ResSchedMgr::GetInstance().InitExecutorPlugin();
91 }
92 ReportAbilityStatus(systemAbilityId, deviceId, 1);
93 }
94
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)95 void ResSchedServiceAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
96 {
97 ReportAbilityStatus(systemAbilityId, deviceId, 0);
98 }
99
OnDeviceLevelChanged(int32_t type,int32_t level,std::string & action)100 void ResSchedServiceAbility::OnDeviceLevelChanged(int32_t type, int32_t level, std::string& action)
101 {
102 if (service_ == nullptr) {
103 RESSCHED_LOGE("On Device Level Changed failed due to service nullptr!");
104 return;
105 }
106 service_->OnDeviceLevelChanged(type, level);
107 }
108
ReclaimProcessMemory()109 void ResSchedServiceAbility::ReclaimProcessMemory()
110 {
111 const int32_t delayTime = 60 * 1000 * 1000;
112 ffrt::task_attr taskattr;
113 taskattr.delay(delayTime);
114 ffrt::submit([]() {ResCommonUtil::WriteFileReclaim(getpid());}, {}, {}, {taskattr});
115 }
116
SystemAbilityListenerInit()117 void ResSchedServiceAbility::SystemAbilityListenerInit()
118 {
119 if (!AddSystemAbilityListener(APP_MGR_SERVICE_ID)) {
120 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
121 "COMPONENT_NAME", "MAIN",
122 "ERR_TYPE", "register failure",
123 "ERR_MSG", "Register a listener of app manager service failed.");
124 }
125 if (!AddSystemAbilityListener(WINDOW_MANAGER_SERVICE_ID)) {
126 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
127 "COMPONENT_NAME", "MAIN",
128 "ERR_TYPE", "register failure",
129 "ERR_MSG", "Register a listener of window manager service failed.");
130 }
131 if (!AddSystemAbilityListener(BACKGROUND_TASK_MANAGER_SERVICE_ID)) {
132 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
133 "COMPONENT_NAME", "MAIN",
134 "ERR_TYPE", "register failure",
135 "ERR_MSG", "Register a listener of background task manager service failed.");
136 }
137 if (!AddSystemAbilityListener(RES_SCHED_EXE_ABILITY_ID)) {
138 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
139 "COMPONENT_NAME", "MAIN",
140 "ERR_TYPE", "register failure",
141 "ERR_MSG", "Register a listener of background task manager service failed.");
142 }
143 if (!AddSystemAbilityListener(POWER_MANAGER_SERVICE_ID)) {
144 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::RSS, "INIT_FAULT", HiviewDFX::HiSysEvent::EventType::FAULT,
145 "COMPONENT_NAME", "MAIN",
146 "ERR_TYPE", "register failure",
147 "ERR_MSG", "Register a listener of power manager service failed.");
148 }
149 }
150 } // namespace ResourceSchedule
151 } // namespace OHOS
152
153