1 /*
2 * Copyright (c) 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 "aot_compiler_service.h"
17 #include "aot_compiler_error_utils.h"
18 #include "aot_compiler_impl.h"
19 #include "ecmascript/log_wrapper.h"
20 #include "iservice_registry.h"
21 #include "ipc_skeleton.h"
22 #include "iremote_object.h"
23 #include "system_ability_definition.h"
24
25 #include "power_disconnected_listener.h"
26
27 #include "common_event_data.h"
28 #include "common_event_manager.h"
29 #include "common_event_support.h"
30 #include "common_event_subscriber.h"
31
32 namespace OHOS::ArkCompiler {
33 namespace {
34 const std::string TASK_ID = "UnLoadSA";
35 constexpr int32_t DELAY_TIME = 180000;
36 }
37
38 REGISTER_SYSTEM_ABILITY_BY_ID(AotCompilerService, AOT_COMPILER_SERVICE_ID, false);
39
AotCompilerService()40 AotCompilerService::AotCompilerService()
41 : SystemAbility(AOT_COMPILER_SERVICE_ID, false), state_(ServiceRunningState::STATE_NOT_START)
42 {
43 }
44
AotCompilerService(int32_t systemAbilityId,bool runOnCreate)45 AotCompilerService::AotCompilerService(int32_t systemAbilityId, bool runOnCreate)
46 : SystemAbility(systemAbilityId, runOnCreate), state_(ServiceRunningState::STATE_NOT_START)
47 {
48 }
49
~AotCompilerService()50 AotCompilerService::~AotCompilerService()
51 {
52 }
53
OnStart()54 void AotCompilerService::OnStart()
55 {
56 LOG_SA(INFO) << "aot compiler service is onStart";
57 if (state_ == ServiceRunningState::STATE_RUNNING) {
58 LOG_SA(INFO) << "aot compiler service has already started";
59 return;
60 }
61 if (!Init()) {
62 LOG_SA(INFO) << "init aot compiler service failed";
63 return;
64 }
65 bool ret = Publish(this);
66 if (!ret) {
67 LOG_SA(ERROR) << "publish service failed";
68 return;
69 }
70 state_ = ServiceRunningState::STATE_RUNNING;
71 RegisterPowerDisconnectedListener();
72 RegisterScreenStatusSubscriber();
73 RegisterThermalMgrListener();
74 }
75
Init()76 bool AotCompilerService::Init()
77 {
78 auto runner = AppExecFwk::EventRunner::Create(TASK_ID);
79 if (unLoadHandler_ == nullptr) {
80 unLoadHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
81 }
82 return true;
83 }
84
RemoveUnloadTask(const std::string & taskId)85 void AotCompilerService::RemoveUnloadTask(const std::string &taskId)
86 {
87 if (unLoadHandler_ == nullptr) {
88 LOG_SA(ERROR) << "NULL pointer of unLoadHandler_ error";
89 return;
90 }
91 unLoadHandler_->RemoveTask(taskId);
92 }
93
DelayUnloadTask(const std::string & taskId,const int32_t delayTime)94 void AotCompilerService::DelayUnloadTask(const std::string &taskId, const int32_t delayTime)
95 {
96 if (unLoadHandler_ == nullptr) {
97 LOG_SA(ERROR) << "NULL pointer of unLoadHandler_ error";
98 return;
99 }
100 auto task = []() {
101 sptr<ISystemAbilityManager> samgr =
102 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
103 if (samgr == nullptr) {
104 LOG_SA(ERROR) << "fail to get system ability manager";
105 return;
106 }
107 int32_t ret = samgr->UnloadSystemAbility(AOT_COMPILER_SERVICE_ID);
108 if (ret != ERR_OK) {
109 LOG_SA(ERROR) << "remove system ability failed";
110 return;
111 }
112 };
113 unLoadHandler_->PostTask(task, taskId, delayTime);
114 }
115
OnStop()116 void AotCompilerService::OnStop()
117 {
118 LOG_SA(INFO) << "aot compiler service has been onStop";
119 state_ = ServiceRunningState::STATE_NOT_START;
120 UnRegisterPowerDisconnectedListener();
121 UnRegisterScreenStatusSubscriber();
122 UnRegisterThermalMgrListener();
123 }
124
AotCompiler(const std::unordered_map<std::string,std::string> & argsMap,std::vector<int16_t> & sigData)125 int32_t AotCompilerService::AotCompiler(const std::unordered_map<std::string, std::string> &argsMap,
126 std::vector<int16_t> &sigData)
127 {
128 LOG_SA(DEBUG) << "begin to call aot compiler";
129 RemoveUnloadTask(TASK_ID);
130 int32_t ret = AotCompilerImpl::GetInstance().EcmascriptAotCompiler(argsMap, sigData);
131 LOG_SA(DEBUG) << "finish aot compiler";
132 DelayUnloadTask(TASK_ID, DELAY_TIME);
133 return ret;
134 }
135
GetAOTVersion(std::string & sigData)136 int32_t AotCompilerService::GetAOTVersion(std::string& sigData)
137 {
138 LOG_SA(DEBUG) << "begin to get AOT version";
139 RemoveUnloadTask(TASK_ID);
140 int32_t ret = AotCompilerImpl::GetInstance().GetAOTVersion(sigData);
141 LOG_SA(DEBUG) << "finish get AOT Version";
142 DelayUnloadTask(TASK_ID, DELAY_TIME);
143 return ret;
144 }
145
NeedReCompile(const std::string & args,bool & sigData)146 int32_t AotCompilerService::NeedReCompile(const std::string& args, bool& sigData)
147 {
148 LOG_SA(DEBUG) << "begin to check need to re-compile version";
149 RemoveUnloadTask(TASK_ID);
150 int32_t ret = AotCompilerImpl::GetInstance().NeedReCompile(args, sigData);
151 LOG_SA(DEBUG) << "finish check need re-compile";
152 DelayUnloadTask(TASK_ID, DELAY_TIME);
153 return ret;
154 }
155
StopAotCompiler()156 int32_t AotCompilerService::StopAotCompiler()
157 {
158 LOG_SA(DEBUG) << "stop aot compiler service";
159 RemoveUnloadTask(TASK_ID);
160 int32_t ret = AotCompilerImpl::GetInstance().StopAotCompiler();
161 DelayUnloadTask(TASK_ID, DELAY_TIME);
162 return ret;
163 }
164
RegisterPowerDisconnectedListener()165 void AotCompilerService::RegisterPowerDisconnectedListener()
166 {
167 LOG_SA(DEBUG) << "AotCompilerService::RegisterPowerDisconnectedListener";
168 EventFwk::MatchingSkills matchingSkills;
169 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
170 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
171 powerDisconnectedListener_ = std::make_shared<PowerDisconnectedListener>(subscribeInfo);
172 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(powerDisconnectedListener_)) {
173 LOG_SA(INFO) << "AotCompilerService::RegisterPowerDisconnectedListener failed";
174 powerDisconnectedListener_ = nullptr;
175 } else {
176 LOG_SA(INFO) << "AotCompilerService::RegisterPowerDisconnectedListener success";
177 isPowerEventSubscribered_ = true;
178 }
179 }
180
RegisterScreenStatusSubscriber()181 void AotCompilerService::RegisterScreenStatusSubscriber()
182 {
183 LOG_SA(DEBUG) << "AotCompilerService::RegisterScreenStatusSubscriber";
184 EventFwk::MatchingSkills matchingSkills;
185 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
186 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
187 screenStatusSubscriber_ = std::make_shared<ScreenStatusSubscriber>(subscribeInfo);
188 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(screenStatusSubscriber_)) {
189 LOG_SA(WARN) << "AotCompilerService::RegisterScreenStatusSubscriber failed";
190 screenStatusSubscriber_ = nullptr;
191 } else {
192 LOG_SA(INFO) << "AotCompilerService::RegisterScreenStatusSubscriber success";
193 isScreenStatusSubscribered_ = true;
194 }
195 }
196
RegisterThermalMgrListener()197 void AotCompilerService::RegisterThermalMgrListener()
198 {
199 LOG_SA(DEBUG) << "AotCompilerService::RegisterThermalMgrListener";
200 EventFwk::MatchingSkills matchingSkills;
201 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_THERMAL_LEVEL_CHANGED);
202 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
203 thermalMgrListener_ = std::make_shared<ThermalMgrListener>(subscribeInfo);
204 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(thermalMgrListener_)) {
205 LOG_SA(WARN) << "AotCompilerService::RegisterThermalMgrListener failed";
206 thermalMgrListener_ = nullptr;
207 } else {
208 LOG_SA(INFO) << "AotCompilerService::RegisterThermalMgrListener success";
209 isThermalLevelEventSubscribered_ = true;
210 }
211 }
212
UnRegisterPowerDisconnectedListener()213 void AotCompilerService::UnRegisterPowerDisconnectedListener()
214 {
215 LOG_SA(DEBUG) << "AotCompilerService::UnRegisterPowerDisconnectedListener";
216 if (!isPowerEventSubscribered_) {
217 return;
218 }
219 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(powerDisconnectedListener_)) {
220 LOG_SA(INFO) << "AotCompilerService::UnRegisterPowerDisconnectedListener failed";
221 }
222 powerDisconnectedListener_ = nullptr;
223 isPowerEventSubscribered_ = false;
224 LOG_SA(INFO) << "AotCompilerService::UnRegisterPowerDisconnectedListener done";
225 }
226
UnRegisterScreenStatusSubscriber()227 void AotCompilerService::UnRegisterScreenStatusSubscriber()
228 {
229 LOG_SA(DEBUG) << "AotCompilerService::UnRegisterScreenStatusSubscriber";
230 if (!isScreenStatusSubscribered_) {
231 return;
232 }
233 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(screenStatusSubscriber_)) {
234 LOG_SA(WARN) << "AotCompilerService::UnRegisterScreenStatusSubscriber failed";
235 }
236 screenStatusSubscriber_ = nullptr;
237 isScreenStatusSubscribered_ = false;
238 LOG_SA(INFO) << "AotCompilerService::UnRegisterScreenStatusSubscriber done";
239 }
240
UnRegisterThermalMgrListener()241 void AotCompilerService::UnRegisterThermalMgrListener()
242 {
243 LOG_SA(DEBUG) << "AotCompilerService::UnRegisterThermalMgrListener";
244 if (!isThermalLevelEventSubscribered_) {
245 return;
246 }
247 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(thermalMgrListener_)) {
248 LOG_SA(WARN) << "AotCompilerService::UnRegisterThermalMgrListener failed";
249 }
250 thermalMgrListener_ = nullptr;
251 isThermalLevelEventSubscribered_ = false;
252 LOG_SA(INFO) << "AotCompilerService::UnRegisterThermalMgrListener done";
253 }
254 } // namespace OHOS::ArkCompiler