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 }
74
Init()75 bool AotCompilerService::Init()
76 {
77 auto runner = AppExecFwk::EventRunner::Create(TASK_ID);
78 if (unLoadHandler_ == nullptr) {
79 unLoadHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
80 }
81 return true;
82 }
83
RemoveUnloadTask(const std::string & taskId)84 void AotCompilerService::RemoveUnloadTask(const std::string &taskId)
85 {
86 if (unLoadHandler_ == nullptr) {
87 LOG_SA(ERROR) << "NULL pointer of unLoadHandler_ error";
88 return;
89 }
90 unLoadHandler_->RemoveTask(taskId);
91 }
92
DelayUnloadTask(const std::string & taskId,const int32_t delayTime)93 void AotCompilerService::DelayUnloadTask(const std::string &taskId, const int32_t delayTime)
94 {
95 if (unLoadHandler_ == nullptr) {
96 LOG_SA(ERROR) << "NULL pointer of unLoadHandler_ error";
97 return;
98 }
99 auto task = []() {
100 sptr<ISystemAbilityManager> samgr =
101 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
102 if (samgr == nullptr) {
103 LOG_SA(ERROR) << "fail to get system ability manager";
104 return;
105 }
106 int32_t ret = samgr->UnloadSystemAbility(AOT_COMPILER_SERVICE_ID);
107 if (ret != ERR_OK) {
108 LOG_SA(ERROR) << "remove system ability failed";
109 return;
110 }
111 };
112 unLoadHandler_->PostTask(task, taskId, delayTime);
113 }
114
OnStop()115 void AotCompilerService::OnStop()
116 {
117 LOG_SA(INFO) << "aot compiler service has been onStop";
118 state_ = ServiceRunningState::STATE_NOT_START;
119 UnRegisterPowerDisconnectedListener();
120 UnRegisterScreenStatusSubscriber();
121 }
122
AotCompiler(const std::unordered_map<std::string,std::string> & argsMap,std::vector<int16_t> & sigData)123 int32_t AotCompilerService::AotCompiler(const std::unordered_map<std::string, std::string> &argsMap,
124 std::vector<int16_t> &sigData)
125 {
126 LOG_SA(DEBUG) << "begin to call aot compiler";
127 RemoveUnloadTask(TASK_ID);
128 int32_t ret = AotCompilerImpl::GetInstance().EcmascriptAotCompiler(argsMap, sigData);
129 LOG_SA(DEBUG) << "finish aot compiler";
130 DelayUnloadTask(TASK_ID, DELAY_TIME);
131 return ret;
132 }
133
GetAOTVersion(std::string & sigData)134 int32_t AotCompilerService::GetAOTVersion(std::string& sigData)
135 {
136 LOG_SA(DEBUG) << "begin to get AOT version";
137 RemoveUnloadTask(TASK_ID);
138 int32_t ret = AotCompilerImpl::GetInstance().GetAOTVersion(sigData);
139 LOG_SA(DEBUG) << "finish get AOT Version";
140 DelayUnloadTask(TASK_ID, DELAY_TIME);
141 return ret;
142 }
143
NeedReCompile(const std::string & args,bool & sigData)144 int32_t AotCompilerService::NeedReCompile(const std::string& args, bool& sigData)
145 {
146 LOG_SA(DEBUG) << "begin to check need to re-compile version";
147 RemoveUnloadTask(TASK_ID);
148 int32_t ret = AotCompilerImpl::GetInstance().NeedReCompile(args, sigData);
149 LOG_SA(DEBUG) << "finish check need re-compile";
150 DelayUnloadTask(TASK_ID, DELAY_TIME);
151 return ret;
152 }
153
StopAotCompiler()154 int32_t AotCompilerService::StopAotCompiler()
155 {
156 LOG_SA(DEBUG) << "stop aot compiler service";
157 RemoveUnloadTask(TASK_ID);
158 int32_t ret = AotCompilerImpl::GetInstance().StopAotCompiler();
159 DelayUnloadTask(TASK_ID, DELAY_TIME);
160 return ret;
161 }
162
RegisterPowerDisconnectedListener()163 void AotCompilerService::RegisterPowerDisconnectedListener()
164 {
165 LOG_SA(DEBUG) << "AotCompilerService::RegisterPowerDisconnectedListener";
166 EventFwk::MatchingSkills matchingSkills;
167 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED);
168 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
169 powerDisconnectedListener_ = std::make_shared<PowerDisconnectedListener>(subscribeInfo);
170 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(powerDisconnectedListener_)) {
171 LOG_SA(INFO) << "AotCompilerService::RegisterPowerDisconnectedListener failed";
172 powerDisconnectedListener_ = nullptr;
173 } else {
174 LOG_SA(INFO) << "AotCompilerService::RegisterPowerDisconnectedListener success";
175 isPowerEventSubscribered_ = true;
176 }
177 }
178
RegisterScreenStatusSubscriber()179 void AotCompilerService::RegisterScreenStatusSubscriber()
180 {
181 LOG_SA(DEBUG) << "AotCompilerService::RegisterScreenStatusSubscriber";
182 EventFwk::MatchingSkills matchingSkills;
183 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
184 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
185 screenStatusSubscriber_ = std::make_shared<ScreenStatusSubscriber>(subscribeInfo);
186 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(screenStatusSubscriber_)) {
187 LOG_SA(INFO) << "AotCompilerService::RegisterScreenStatusSubscriber failed";
188 screenStatusSubscriber_ = nullptr;
189 } else {
190 LOG_SA(INFO) << "AotCompilerService::RegisterScreenStatusSubscriber success";
191 isScreenStatusSubscribered_ = true;
192 }
193 }
194
UnRegisterPowerDisconnectedListener()195 void AotCompilerService::UnRegisterPowerDisconnectedListener()
196 {
197 LOG_SA(DEBUG) << "AotCompilerService::UnRegisterPowerDisconnectedListener";
198 if (!isPowerEventSubscribered_) {
199 return;
200 }
201 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(powerDisconnectedListener_)) {
202 LOG_SA(INFO) << "AotCompilerService::UnRegisterPowerDisconnectedListener failed";
203 }
204 powerDisconnectedListener_ = nullptr;
205 isPowerEventSubscribered_ = false;
206 LOG_SA(INFO) << "AotCompilerService::UnRegisterPowerDisconnectedListener done";
207 }
208
UnRegisterScreenStatusSubscriber()209 void AotCompilerService::UnRegisterScreenStatusSubscriber()
210 {
211 LOG_SA(DEBUG) << "AotCompilerService::UnRegisterScreenStatusSubscriber";
212 if (!isScreenStatusSubscribered_) {
213 return;
214 }
215 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(screenStatusSubscriber_)) {
216 LOG_SA(INFO) << "AotCompilerService::UnRegisterScreenStatusSubscriber failed";
217 }
218 screenStatusSubscriber_ = nullptr;
219 isScreenStatusSubscribered_ = false;
220 LOG_SA(INFO) << "AotCompilerService::UnRegisterScreenStatusSubscriber done";
221 }
222 } // namespace OHOS::ArkCompiler