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