From 0dddb78d3ebe426813f1384179a23042e54e6521 Mon Sep 17 00:00:00 2001 From: chenbingbing Date: Mon, 10 Mar 2025 11:58:35 +0800 Subject: [PATCH] add delay dbinder feature Signed-off-by: chenbingbing --- bundle.json | 3 ++- config.gni | 3 +++ services/samgr/native/BUILD.gn | 4 +++ .../native/include/system_ability_manager.h | 11 +++++++- .../collect/device_networking_collect.cpp | 2 ++ .../native/source/system_ability_manager.cpp | 27 +++++++++++++++++++ services/samgr/native/test/unittest/BUILD.gn | 5 ++++ .../src/system_ability_mgr_new_test.cpp | 7 +++++ 8 files changed, 60 insertions(+), 2 deletions(-) diff --git a/bundle.json b/bundle.json index 5a87a468..a33e0526 100644 --- a/bundle.json +++ b/bundle.json @@ -16,7 +16,8 @@ "subsystem": "systemabilitymgr", "features": [ "samgr_feature_coverage", - "samgr_enable_extend_load_timeout" + "samgr_enable_extend_load_timeout", + "samgr_enable_delay_dbinder" ], "adapted_system_type": [ "standard" diff --git a/config.gni b/config.gni index 97b89d8e..de32f131 100644 --- a/config.gni +++ b/config.gni @@ -12,6 +12,9 @@ # limitations under the License. declare_args() { + # enable delay load dbinder service. + samgr_enable_delay_dbinder = true + # enable extend load sa timeout. samgr_enable_extend_load_timeout = false } diff --git a/services/samgr/native/BUILD.gn b/services/samgr/native/BUILD.gn index 5dc900c0..e9acf73f 100644 --- a/services/samgr/native/BUILD.gn +++ b/services/samgr/native/BUILD.gn @@ -148,6 +148,10 @@ ohos_executable("samgr") { if (samgr_enable_extend_load_timeout) { defines += [ "SAMGR_ENABLE_EXTEND_LOAD_TIMEOUT" ] } + + if (samgr_enable_delay_dbinder) { + defines += [ "SAMGR_ENABLE_DELAY_DBINDER" ] + } part_name = "samgr" } diff --git a/services/samgr/native/include/system_ability_manager.h b/services/samgr/native/include/system_ability_manager.h index 715d708d..f4c672d4 100644 --- a/services/samgr/native/include/system_ability_manager.h +++ b/services/samgr/native/include/system_ability_manager.h @@ -255,7 +255,9 @@ public: } } void RemoveOnDemandSaInDiedProc(std::shared_ptr& processContext); +#ifdef SAMGR_ENABLE_DELAY_DBINDER void InitDbinderService(); +#endif private: enum class AbilityState { INIT, @@ -271,7 +273,12 @@ private: OnDemandEvent event; }; - SystemAbilityManager() {} + SystemAbilityManager() + { +#ifndef SAMGR_ENABLE_DELAY_DBINDER + dBinderService_ = DBinderService::GetInstance(); +#endif + } std::string EventToJson(const OnDemandEvent& event); void DoInsertSaData(const std::u16string& name, const sptr& ability, const SAExtraProp& extraProp); int32_t StartOnDemandAbility(int32_t systemAbilityId, bool& isExist) @@ -384,9 +391,11 @@ private: sptr collectManager_; std::shared_ptr rpcCallbackImp_; +#ifdef SAMGR_ENABLE_DELAY_DBINDER std::shared_mutex dBinderServiceLock_; std::list distributedSaList_; bool isDbinderServiceInit_ = false; +#endif // must hold abilityMapLock_ never access other locks std::shared_mutex abilityMapLock_; diff --git a/services/samgr/native/source/collect/device_networking_collect.cpp b/services/samgr/native/source/collect/device_networking_collect.cpp index 81145333..cc4e6c15 100644 --- a/services/samgr/native/source/collect/device_networking_collect.cpp +++ b/services/samgr/native/source/collect/device_networking_collect.cpp @@ -193,7 +193,9 @@ void DeviceInitCallBack::OnRemoteDied() void DeviceStateCallback::OnDeviceOnline(const DmDeviceInfo& deviceInfo) { HILOGI("DeviceNetworkingCollect OnDeviceOnline size %{public}zu", deviceOnlineSet_.size()); +#ifdef SAMGR_ENABLE_DELAY_DBINDER SystemAbilityManager::GetInstance()->InitDbinderService(); +#endif { lock_guard autoLock(deviceOnlineLock_); deviceOnlineSet_.emplace(deviceInfo.networkId); diff --git a/services/samgr/native/source/system_ability_manager.cpp b/services/samgr/native/source/system_ability_manager.cpp index 113ab008..563ea99c 100644 --- a/services/samgr/native/source/system_ability_manager.cpp +++ b/services/samgr/native/source/system_ability_manager.cpp @@ -90,6 +90,7 @@ sptr SystemAbilityManager::instance; void SystemAbilityManager::RegisterDistribute(int32_t systemAbilityId, bool isDistributed) { +#ifdef SAMGR_ENABLE_DELAY_DBINDER if (isDistributed) { std::shared_lock readLock(dBinderServiceLock_); if (dBinderService_ != nullptr) { @@ -109,8 +110,22 @@ void SystemAbilityManager::RegisterDistribute(int32_t systemAbilityId, bool isDi HILOGI("start result is %{public}s", ret ? "succeed" : "fail"); } } +#else + u16string strName = Str8ToStr16(to_string(systemAbilityId)); + if (isDistributed && dBinderService_!= nullptr) { + dBinderService_->RegisterRemoteProxy(strName, systemAbilityId); + HILOGI("AddSystemAbility RegisterRemoteProxy, SA:%{public}d", systemAbilityId); + } + if (systemAbilityId == SOFTBUS_SERVER_SA_ID) { + if (dBinderService_!= nullptr && rpcCallbackImp_!= nullptr) { + bool ret = dBinderService_->StartDBinderService(rpcCallbackImp_); + HILOGI("start result is %{public}s", ret? "succeed" : "fail"); + } + } +#endif } +#ifdef SAMGR_ENABLE_DELAY_DBINDER void SystemAbilityManager::InitDbinderService() { std::unique_lock writeLock(dBinderServiceLock_); @@ -134,6 +149,7 @@ void SystemAbilityManager::InitDbinderService() } } } +#endif void SystemAbilityManager::Init() { @@ -142,6 +158,9 @@ void SystemAbilityManager::Init() abilityStatusDeath_ = sptr(new AbilityStatusDeathRecipient()); abilityCallbackDeath_ = sptr(new AbilityCallbackDeathRecipient()); remoteCallbackDeath_ = sptr(new RemoteCallbackDeathRecipient()); +#ifndef SAMGR_ENABLE_DELAY_DBINDER + rpcCallbackImp_ = make_shared(); +#endif if (workHandler_ == nullptr) { workHandler_ = make_shared("workHandler"); @@ -990,7 +1009,9 @@ void SystemAbilityManager::NotifyRemoteSaDied(const std::u16string& name) std::u16string saName; std::string deviceId; SamgrUtil::ParseRemoteSaName(name, deviceId, saName); +#ifdef SAMGR_ENABLE_DELAY_DBINDER std::shared_lock readLock(dBinderServiceLock_); +#endif if (dBinderService_ != nullptr) { std::string nodeId = SamgrUtil::TransformDeviceId(deviceId, NODE_ID, false); dBinderService_->NoticeServiceDie(saName, nodeId); @@ -1001,7 +1022,9 @@ void SystemAbilityManager::NotifyRemoteSaDied(const std::u16string& name) void SystemAbilityManager::NotifyRemoteDeviceOffline(const std::string& deviceId) { +#ifdef SAMGR_ENABLE_DELAY_DBINDER std::shared_lock readLock(dBinderServiceLock_); +#endif if (dBinderService_ != nullptr) { dBinderService_->NoticeDeviceDie(deviceId); HILOGI("NotifyRemoteDeviceOffline, deviceId:%{public}s", AnonymizeDeviceId(deviceId).c_str()); @@ -1834,7 +1857,9 @@ sptr SystemAbilityManager::DoMakeRemoteBinder(int32_t system DeviceIdToNetworkId(networkId); #endif sptr remoteBinder = nullptr; +#ifdef SAMGR_ENABLE_DELAY_DBINDER std::shared_lock readLock(dBinderServiceLock_); +#endif if (dBinderService_ != nullptr) { string strName = to_string(systemAbilityId); { @@ -1856,7 +1881,9 @@ void SystemAbilityManager::NotifyRpcLoadCompleted(const std::string& srcDeviceId return; } auto notifyTask = [srcDeviceId, systemAbilityId, remoteObject, this]() { +#ifdef SAMGR_ENABLE_DELAY_DBINDER std::shared_lock readLock(dBinderServiceLock_); +#endif if (dBinderService_ != nullptr) { SamgrXCollie samgrXCollie("samgr--LoadSystemAbilityComplete_" + ToString(systemAbilityId)); dBinderService_->LoadSystemAbilityComplete(srcDeviceId, systemAbilityId, remoteObject); diff --git a/services/samgr/native/test/unittest/BUILD.gn b/services/samgr/native/test/unittest/BUILD.gn index a76daa2d..15e8a301 100644 --- a/services/samgr/native/test/unittest/BUILD.gn +++ b/services/samgr/native/test/unittest/BUILD.gn @@ -12,6 +12,7 @@ # limitations under the License. import("//build/test.gni") +import("../../../../../config.gni") import("../../../var.gni") module_output_path = "samgr/samgr" @@ -133,6 +134,10 @@ ohos_unittest("SystemAbilityMgrTest") { defines += [ "HICOLLIE_ENABLE" ] } + if (samgr_enable_delay_dbinder) { + defines += [ "SAMGR_ENABLE_DELAY_DBINDER" ] + } + if (support_common_event) { sources += [ "${samgr_services_dir}/source/collect/common_event_collect.cpp", diff --git a/services/samgr/native/test/unittest/src/system_ability_mgr_new_test.cpp b/services/samgr/native/test/unittest/src/system_ability_mgr_new_test.cpp index 7153d541..9fcf2cba 100644 --- a/services/samgr/native/test/unittest/src/system_ability_mgr_new_test.cpp +++ b/services/samgr/native/test/unittest/src/system_ability_mgr_new_test.cpp @@ -332,7 +332,9 @@ HWTEST_F(SystemAbilityMgrNewTest, NotifyRpcLoadCompleted001, TestSize.Level3) { sptr saMgr = new SystemAbilityManager; InitSaMgr(saMgr); +#ifdef SAMGR_ENABLE_DELAY_DBINDER saMgr->InitDbinderService(); +#endif sptr testAbility = new TestTransactionService(); saMgr->workHandler_ = nullptr; saMgr->NotifyRpcLoadCompleted("", 1, testAbility); @@ -363,7 +365,9 @@ HWTEST_F(SystemAbilityMgrNewTest, NotifyRpcLoadCompleted003, TestSize.Level3) { sptr saMgr = new SystemAbilityManager; InitSaMgr(saMgr); +#ifdef SAMGR_ENABLE_DELAY_DBINDER saMgr->InitDbinderService(); +#endif sptr testAbility = new TestTransactionService(); saMgr->NotifyRpcLoadCompleted("", 1, testAbility); } @@ -1099,6 +1103,7 @@ HWTEST_F(SystemAbilityMgrNewTest, IsDistributedSystemAbility001, TestSize.Level2 EXPECT_FALSE(res); } +#ifdef SAMGR_ENABLE_DELAY_DBINDER HWTEST_F(SystemAbilityMgrNewTest, RegisterDistribute001, TestSize.Level2) { DTEST_LOG<<"RegisterDistribute001 BEGIN"<dBinderService_ = nullptr); DTEST_LOG<<"RegisterDistribute001 END"<