From 77154d20ea3874c74d29a0d60c6ef3087524e2fb Mon Sep 17 00:00:00 2001 From: linxiang8 Date: Mon, 6 May 2024 16:05:48 +0800 Subject: [PATCH] add stop ces event Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/I9LP4Z Signed-off-by: linxiang8 Change-Id: I389a666da600e2dfcc5aa817dc56bd71d145ef41 --- bundle.json | 3 +- compiler_service/BUILD.gn | 2 + compiler_service/compiler_service.cfg | 4 +- compiler_service/include/aot_compiler_impl.h | 6 +++ .../include/aot_compiler_service.h | 7 ++++ .../include/power_disconnected_listener.h | 33 +++++++++++++++ compiler_service/libaot_compiler_service.map | 1 + compiler_service/src/aot_compiler_impl.cpp | 35 +++++++++++++++- compiler_service/src/aot_compiler_service.cpp | 39 ++++++++++++++++++ .../src/power_disconnected_listener.cpp | 41 +++++++++++++++++++ compiler_service/test/BUILD.gn | 1 + 11 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 compiler_service/include/power_disconnected_listener.h create mode 100644 compiler_service/src/power_disconnected_listener.cpp diff --git a/bundle.json b/bundle.json index 5a4b06772e..cad7920b4a 100644 --- a/bundle.json +++ b/bundle.json @@ -43,7 +43,8 @@ "hisysevent", "ipc", "safwk", - "samgr" + "samgr", + "common_event_service" ], "third_party": [ ] diff --git a/compiler_service/BUILD.gn b/compiler_service/BUILD.gn index c355ab560b..1e9e1a2a59 100644 --- a/compiler_service/BUILD.gn +++ b/compiler_service/BUILD.gn @@ -34,12 +34,14 @@ ohos_shared_library("libcompiler_service") { "src/aot_compiler_impl.cpp", "src/aot_compiler_load_callback.cpp", "src/aot_compiler_service.cpp", + "src/power_disconnected_listener.cpp", ] external_deps = [ "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", "c_utils:utils", "code_signature:liblocal_code_sign_sdk", + "common_event_service:cesfwk_innerkits", "eventhandler:libeventhandler", "hilog:libhilog", "hisysevent:libhisysevent", diff --git a/compiler_service/compiler_service.cfg b/compiler_service/compiler_service.cfg index 79408c3390..289573f7d5 100644 --- a/compiler_service/compiler_service.cfg +++ b/compiler_service/compiler_service.cfg @@ -5,8 +5,8 @@ "ondemand" : true, "uid" : "compiler_service", "gid" : ["compiler_service"], - "caps" : ["SETUID", "SETGID"], - "secon" : "u:object_r:compiler_service:s0" + "caps" : ["SETUID", "SETGID", "KILL"], + "secon" : "u:r:compiler_service:s0" } ] } \ No newline at end of file diff --git a/compiler_service/include/aot_compiler_impl.h b/compiler_service/include/aot_compiler_impl.h index 379e963f5e..d90aeabb6b 100644 --- a/compiler_service/include/aot_compiler_impl.h +++ b/compiler_service/include/aot_compiler_impl.h @@ -35,6 +35,8 @@ public: std::vector &sigData); int32_t StopAotCompiler(); + void HandlePowerDisconnected(); + private: void PrepareArgs(const std::unordered_map &argsMap); void DropCapabilities(const int32_t &bundleUid, const int32_t &bundleGid) const; @@ -44,6 +46,9 @@ private: std::vector &sigData); void InitState(const pid_t childPid); void ResetState(); + void PauseAotCompiler(); + void AllowAotCompiler(); + AotCompilerImpl() = default; ~AotCompilerImpl() = default; @@ -65,6 +70,7 @@ private: bool running = false; pid_t childPid = -1; } state_; + bool allowAotCompiler_ {true}; }; } // namespace OHOS::ArkCompiler #endif // OHOS_ARKCOMPILER_AOTCOMPILER_IMPL_H \ No newline at end of file diff --git a/compiler_service/include/aot_compiler_service.h b/compiler_service/include/aot_compiler_service.h index 2776853633..6369ca523a 100644 --- a/compiler_service/include/aot_compiler_service.h +++ b/compiler_service/include/aot_compiler_service.h @@ -26,6 +26,8 @@ #include "singleton.h" #include "system_ability.h" +#include "power_disconnected_listener.h" + namespace OHOS::ArkCompiler { enum class ServiceRunningState { STATE_NOT_START, @@ -45,8 +47,13 @@ protected: void OnStop() override; private: bool Init(); + void RegisterPowerDisconnectedListener(); + void UnRegisterPowerDisconnectedListener(); + std::shared_ptr unLoadHandler_; ServiceRunningState state_; + std::shared_ptr powerDisconnectedListener_; + bool isPowerEventSubscribered_ = false; }; } // namespace OHOS::ArkCompiler #endif // OHOS_ARKCOMPILER_AOTCOMPILER_SERVICE_H \ No newline at end of file diff --git a/compiler_service/include/power_disconnected_listener.h b/compiler_service/include/power_disconnected_listener.h new file mode 100644 index 0000000000..512edd9893 --- /dev/null +++ b/compiler_service/include/power_disconnected_listener.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ARKCOMPILER_POWER_DISCONNECTED_LISTENER_H +#define OHOS_ARKCOMPILER_POWER_DISCONNECTED_LISTENER_H + +#include "common_event_manager.h" +#include "common_event_subscriber.h" +#include "common_event_support.h" + +namespace OHOS { +namespace ArkCompiler { +class PowerDisconnectedListener : public EventFwk::CommonEventSubscriber { +public: + explicit PowerDisconnectedListener(const EventFwk::CommonEventSubscribeInfo &subscribeInfo); + virtual ~PowerDisconnectedListener() = default; + void OnReceiveEvent(const EventFwk::CommonEventData &data) override; +}; +} // namespace ArkCompiler +} // namespace OHOS +#endif // OHOS_ARKCOMPILER_POWER_DISCONNECTED_LISTENER_H \ No newline at end of file diff --git a/compiler_service/libaot_compiler_service.map b/compiler_service/libaot_compiler_service.map index 227a8412f6..0f2e64e081 100644 --- a/compiler_service/libaot_compiler_service.map +++ b/compiler_service/libaot_compiler_service.map @@ -4,6 +4,7 @@ OHOS::ArkCompiler::AotCompilerClient::GetInstance*; OHOS::ArkCompiler::AotCompilerClient::AotCompiler*; OHOS::ArkCompiler::AotCompilerClient::StopAotCompiler*; + OHOS::ArkCompiler::PowerDisconnectedListener::PowerDisconnectedListener*; }; local: *; diff --git a/compiler_service/src/aot_compiler_impl.cpp b/compiler_service/src/aot_compiler_impl.cpp index d53a9735f0..ca2d8a20c2 100644 --- a/compiler_service/src/aot_compiler_impl.cpp +++ b/compiler_service/src/aot_compiler_impl.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include "aot_compiler_constants.h" #include "aot_compiler_error_utils.h" @@ -155,6 +156,9 @@ void AotCompilerImpl::ExecuteInParentProcess(const pid_t childPid, int32_t &ret) int32_t AotCompilerImpl::EcmascriptAotCompiler(const std::unordered_map &argsMap, std::vector &sigData) { + if (!allowAotCompiler_) { + return ERR_AOT_COMPILER_PARAM_FAILED; + } if (argsMap.empty()) { HiviewDFX::HiLog::Error(LABEL, "aot compiler arguments error"); return ERR_AOT_COMPILER_PARAM_FAILED; @@ -209,11 +213,40 @@ int32_t AotCompilerImpl::StopAotCompiler() return ERR_AOT_COMPILER_STOP_FAILED; } HiviewDFX::HiLog::Info(LABEL, "begin to kill child process : %{public}d", state_.childPid); - (void)kill(state_.childPid, SIGKILL); + auto result = kill(state_.childPid, SIGKILL); + if (result != 0) { + HiviewDFX::HiLog::Info(LABEL, "kill child process failed: %{public}d", result); + } else { + HiviewDFX::HiLog::Info(LABEL, "kill child process success"); + } ResetState(); return ERR_OK; } +void AotCompilerImpl::HandlePowerDisconnected() +{ + HiviewDFX::HiLog::Info(LABEL, "AotCompilerImpl::HandlePowerDisconnected"); + PauseAotCompiler(); + auto task = []() { + AotCompilerImpl::GetInstance().StopAotCompiler(); + sleep(30); + AotCompilerImpl::GetInstance().AllowAotCompiler(); + }; + std::thread(task).detach(); +} + +void AotCompilerImpl::PauseAotCompiler() +{ + HiviewDFX::HiLog::Info(LABEL, "AotCompilerImpl::PauseAotCompiler"); + allowAotCompiler_ = false; +} + +void AotCompilerImpl::AllowAotCompiler() +{ + HiviewDFX::HiLog::Info(LABEL, "AotCompilerImpl::AllowAotCompiler"); + allowAotCompiler_ = true; +} + void AotCompilerImpl::InitState(const pid_t childPid) { state_.running = true; diff --git a/compiler_service/src/aot_compiler_service.cpp b/compiler_service/src/aot_compiler_service.cpp index 25c8b8aeb3..77b00c1924 100644 --- a/compiler_service/src/aot_compiler_service.cpp +++ b/compiler_service/src/aot_compiler_service.cpp @@ -22,6 +22,13 @@ #include "iremote_object.h" #include "system_ability_definition.h" +#include "power_disconnected_listener.h" + +#include "common_event_data.h" +#include "common_event_manager.h" +#include "common_event_support.h" +#include "common_event_subscriber.h" + namespace OHOS::ArkCompiler { namespace { const std::string TASK_ID = "UnLoadSA"; @@ -61,6 +68,7 @@ void AotCompilerService::OnStart() return; } state_ = ServiceRunningState::STATE_RUNNING; + RegisterPowerDisconnectedListener(); } bool AotCompilerService::Init() @@ -95,6 +103,7 @@ void AotCompilerService::OnStop() { HiviewDFX::HiLog::Info(LABEL, "aot compiler service has been onStop"); state_ = ServiceRunningState::STATE_NOT_START; + UnRegisterPowerDisconnectedListener(); } int32_t AotCompilerService::AotCompiler(const std::unordered_map &argsMap, @@ -114,4 +123,34 @@ int32_t AotCompilerService::StopAotCompiler() DelayUnloadTask(); return ret; } + +void AotCompilerService::RegisterPowerDisconnectedListener() +{ + HiviewDFX::HiLog::Info(LABEL, "AotCompilerService::RegisterPowerDisconnectedListener"); + EventFwk::MatchingSkills matchingSkills; + matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_POWER_DISCONNECTED); + EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills); + powerDisconnectedListener_ = std::make_shared(subscribeInfo); + if (!EventFwk::CommonEventManager::SubscribeCommonEvent(powerDisconnectedListener_)) { + HiviewDFX::HiLog::Info(LABEL, "AotCompilerService::RegisterPowerDisconnectedListener failed"); + powerDisconnectedListener_ = nullptr; + } else { + HiviewDFX::HiLog::Info(LABEL, "AotCompilerService::RegisterPowerDisconnectedListener success"); + isPowerEventSubscribered_ = true; + } +} + +void AotCompilerService::UnRegisterPowerDisconnectedListener() +{ + HiviewDFX::HiLog::Info(LABEL, "AotCompilerService::UnRegisterPowerDisconnectedListener"); + if (!isPowerEventSubscribered_) { + return; + } + if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(powerDisconnectedListener_)) { + HiviewDFX::HiLog::Info(LABEL, "AotCompilerService::UnRegisterPowerDisconnectedListener failed"); + } + powerDisconnectedListener_ = nullptr; + isPowerEventSubscribered_ = false; + HiviewDFX::HiLog::Info(LABEL, "AotCompilerService::UnRegisterPowerDisconnectedListener done"); +} } // namespace OHOS::ArkCompiler \ No newline at end of file diff --git a/compiler_service/src/power_disconnected_listener.cpp b/compiler_service/src/power_disconnected_listener.cpp new file mode 100644 index 0000000000..2b451b2f53 --- /dev/null +++ b/compiler_service/src/power_disconnected_listener.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "power_disconnected_listener.h" + +#include "aot_compiler_impl.h" + +#include "hilog/log.h" + +namespace OHOS { +namespace ArkCompiler { + +namespace { + constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0xD001800, "aot_compiler_service"}; +} // namespace + +PowerDisconnectedListener::PowerDisconnectedListener(const EventFwk::CommonEventSubscribeInfo &subscribeInfo) + : EventFwk::CommonEventSubscriber(subscribeInfo) +{ +} + +void PowerDisconnectedListener::OnReceiveEvent(const EventFwk::CommonEventData &data) +{ + HiviewDFX::HiLog::Info(LABEL, "PowerDisconnectedListener::OnReceiveEvent"); + AotCompilerImpl::GetInstance().HandlePowerDisconnected(); +} + +} // namespace ArkCompiler +} // namespace OHOS \ No newline at end of file diff --git a/compiler_service/test/BUILD.gn b/compiler_service/test/BUILD.gn index 50b2f38826..3def69203e 100644 --- a/compiler_service/test/BUILD.gn +++ b/compiler_service/test/BUILD.gn @@ -48,6 +48,7 @@ ohos_unittest("AotCompilerServiceTest") { "access_token:libtokenid_sdk", "c_utils:utils", "code_signature:liblocal_code_sign_sdk", + "common_event_service:cesfwk_innerkits", "eventhandler:libeventhandler", "hilog:libhilog", "hisysevent:libhisysevent",