diff --git a/etc/samgr.para b/etc/samgr.para index ba4719e70a92aac1d1886725c4fd09364f6a7178..5bdbf2353925bea0206d631e26878dd91ceabe70 100644 --- a/etc/samgr.para +++ b/etc/samgr.para @@ -11,4 +11,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -persist.samgr.perf.ondemand = false +persist.samgr.perf.ondemand = true diff --git a/services/samgr/native/BUILD.gn b/services/samgr/native/BUILD.gn index e7a61fcdb670eec090c29d27326d0f811b5131df..d392943b2edd70b0ce370e2dad9f4d4b8858cbb6 100644 --- a/services/samgr/native/BUILD.gn +++ b/services/samgr/native/BUILD.gn @@ -50,6 +50,7 @@ ohos_executable("samgr") { "//foundation/systemabilitymgr/samgr/services/samgr/native/source/memory_guard.cpp", "//foundation/systemabilitymgr/samgr/services/samgr/native/source/rpc_callback_imp.cpp", "//foundation/systemabilitymgr/samgr/services/samgr/native/source/schedule/system_ability_event_handler.cpp", + "//foundation/systemabilitymgr/samgr/services/samgr/native/source/schedule/system_ability_ondemand_switch.cpp", "//foundation/systemabilitymgr/samgr/services/samgr/native/source/schedule/system_ability_state_machine.cpp", "//foundation/systemabilitymgr/samgr/services/samgr/native/source/schedule/system_ability_state_scheduler.cpp", "//foundation/systemabilitymgr/samgr/services/samgr/native/source/system_ability_load_callback_proxy.cpp", diff --git a/services/samgr/native/include/schedule/system_ability_ondemand_switch.h b/services/samgr/native/include/schedule/system_ability_ondemand_switch.h new file mode 100644 index 0000000000000000000000000000000000000000..b4b7bcf307da2ec1849c1853ea51f187712a4fe8 --- /dev/null +++ b/services/samgr/native/include/schedule/system_ability_ondemand_switch.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 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_SYSTEM_ABILITY_MANAGER_SYSTEM_ABILITY_ONDEMAND_SWITCH_H +#define OHOS_SYSTEM_ABILITY_MANAGER_SYSTEM_ABILITY_ONDEMAND_SWITCH_H + +#include +#include "single_instance.h" + +namespace OHOS { +class SystemAbilityOndemandSwitch { + DECLARE_SINGLE_INSTANCE(SystemAbilityOndemandSwitch); +public: + void Init(); + void OnSwitchStateChanged(); + bool IsOndemandSwitchEnable(); +private: + void LoadAllOndemandSA(); + std::shared_mutex ondemandSwitchLock_; + bool isOndemandSwitchEnable_ = true; +}; +} // namespace OHOS + +#endif // !defined(OHOS_SYSTEM_ABILITY_MANAGER_SYSTEM_ABILITY_ONDEMAND_SWITCH_H) \ No newline at end of file diff --git a/services/samgr/native/include/system_ability_manager.h b/services/samgr/native/include/system_ability_manager.h index ca14aff3600e2fb734378e92637dfd8998105610..e7331c64c038c9533bf0bc8940c1e1c44c348dac 100644 --- a/services/samgr/native/include/system_ability_manager.h +++ b/services/samgr/native/include/system_ability_manager.h @@ -90,6 +90,7 @@ public: std::string GetLocalNodeId(); void Init(); void WatchDogInit(); + std::list GetAllOndemandSa(); int32_t AddSystemProcess(const std::u16string& procName, const sptr& procObject) override; int32_t RemoveSystemProcess(const sptr& procObject); @@ -200,9 +201,7 @@ private: void UpdateSaFreMap(int32_t uid, int32_t saId); uint64_t GenerateFreKey(int32_t uid, int32_t saId) const; void ReportGetSAPeriodically(); - void OndemandLoad(); - void OndemandLoadForPerf(); - std::list GetAllOndemandSa(); + void InitOndemandSwitch(); std::string EventToStr(const OnDemandEvent& event); bool CheckCallerProcess(SaProfile& saProfile); bool CheckAllowUpdate(OnDemandPolicyType type, SaProfile& saProfile); diff --git a/services/samgr/native/source/schedule/system_ability_ondemand_switch.cpp b/services/samgr/native/source/schedule/system_ability_ondemand_switch.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f1c48b3b0d2def867e01a5b27182dd6042ca9793 --- /dev/null +++ b/services/samgr/native/source/schedule/system_ability_ondemand_switch.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2023 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 "schedule/system_ability_ondemand_switch.h" + +#include "datetime_ex.h" +#include "parameter.h" +#include "parameters.h" +#include "sam_log.h" +#include "system_ability_manager.h" + +namespace OHOS { +namespace { +constexpr const char* ONDEMAND_SWITCH_PARAM = "persist.samgr.perf.ondemand"; +} + +IMPLEMENT_SINGLE_INSTANCE(SystemAbilityOndemandSwitch); + +void SystemAbilityOndemandSwitch::Init() +{ + auto callback = [](const char *key, const char *value, void *context) { + SystemAbilityOndemandSwitch::GetInstance().OnSwitchStateChanged(); + }; + int ret = WatchParameter(ONDEMAND_SWITCH_PARAM, callback, nullptr); + HILOGD("WatchParameter ret: %{public}d", ret); +} + +void SystemAbilityOndemandSwitch::OnSwitchStateChanged() +{ + std::unique_lock writeLock(ondemandSwitchLock_); + isOndemandSwitchEnable_ = system::GetBoolParameter(ONDEMAND_SWITCH_PARAM, true); + if (!isOndemandSwitchEnable_) { + LoadAllOndemandSA(); + } +} + +void SystemAbilityOndemandSwitch::LoadAllOndemandSA() +{ + int64_t begin = GetTickCount(); + std::list saIds = SystemAbilityManager::GetInstance()->GetAllOndemandSa(); + HILOGI("not loaded ondemand SA size: %{public}zu", saIds.size()); + sptr callback(new SystemAbilityLoadCallbackStub()); + for (auto saId : saIds) { + HILOGI("load ondemand SA: %{public}d", saId); + SystemAbilityManager::GetInstance()->LoadSystemAbility(saId, callback); + } + HILOGI("[PerformanceTest] LoadAllOndemandSA spend %{public}lld ms", GetTickCount() - begin); +} + +bool SystemAbilityOndemandSwitch::IsOndemandSwitchEnable() +{ + std::shared_lock readLock(ondemandSwitchLock_); + return isOndemandSwitchEnable_; +} +} // namespace OHOS \ No newline at end of file diff --git a/services/samgr/native/source/schedule/system_ability_state_scheduler.cpp b/services/samgr/native/source/schedule/system_ability_state_scheduler.cpp index ae811631d4273b6d394a77d27a313aa31a5f651b..d5c0110eb04cacd384cc8bef3fa1a559920742ab 100644 --- a/services/samgr/native/source/schedule/system_ability_state_scheduler.cpp +++ b/services/samgr/native/source/schedule/system_ability_state_scheduler.cpp @@ -13,6 +13,8 @@ * limitations under the License. */ +#include "schedule/system_ability_state_scheduler.h" + #include #include "ability_death_recipient.h" @@ -25,7 +27,7 @@ #include "sam_log.h" #include "string_ex.h" #include "system_ability_manager.h" -#include "schedule/system_ability_state_scheduler.h" +#include "schedule/system_ability_ondemand_switch.h" namespace OHOS { namespace { @@ -467,6 +469,12 @@ int32_t SystemAbilityStateScheduler::TryUnloadAllSystemAbility( HILOGE("[SA Scheduler] process context is nullptr"); return ERR_INVALID_VALUE; } + if (!SystemAbilityOndemandSwitch::GetInstance().IsOndemandSwitchEnable()) { + HILOGI("[SA Scheduler][process: %{public}s] cannot unload all SA: ondemand switch is off", + Str16ToStr8(processContext->processName).c_str()); + return ERR_OK; + } + std::lock_guard autoLock(processContext->processLock); if (CanUnloadAllSystemAbility(processContext)) { return UnloadAllSystemAbilityLocked(processContext); diff --git a/services/samgr/native/source/system_ability_manager.cpp b/services/samgr/native/source/system_ability_manager.cpp index 43fb9a905985efaf6d7f57e0f66aacb2e49b9cf0..5a9e954971aeda8715fa1adf5db0ed1e7998534b 100644 --- a/services/samgr/native/source/system_ability_manager.cpp +++ b/services/samgr/native/source/system_ability_manager.cpp @@ -33,9 +33,8 @@ #include "local_ability_manager_proxy.h" #include "memory_guard.h" #include "parse_util.h" -#include "parameter.h" -#include "parameters.h" #include "sam_log.h" +#include "schedule/system_ability_ondemand_switch.h" #include "service_control.h" #include "string_ex.h" #include "tools.h" @@ -54,8 +53,6 @@ const string EVENT_EXTRA_DATA_ID = "extraDataId"; const string PKG_NAME = "Samgr_Networking"; const string PREFIX = "/system/profile/"; const string LOCAL_DEVICE = "local"; -const string ONDEMAND_PARAM = "persist.samgr.perf.ondemand"; -constexpr const char* ONDEMAND_PERF_PARAM = "persist.samgr.perf.ondemand"; constexpr const char* ONDEMAND_WORKER = "OndemandLoader"; constexpr uint32_t REPORT_GET_SA_INTERVAL = 24 * 60 * 60 * 1000; // ms and is one day @@ -67,7 +64,7 @@ constexpr int32_t UID_SYSTEM = 1000; constexpr int32_t MAX_SUBSCRIBE_COUNT = 256; constexpr int32_t MAX_SA_FREQUENCY_COUNT = INT32_MAX - 1000000; constexpr int32_t SHFIT_BIT = 32; -constexpr int64_t ONDEMAND_PERF_DELAY_TIME = 60 * 1000; // ms +constexpr int64_t ONDEMAND_PERF_DELAY_TIME = 20 * 1000; // ms constexpr int64_t CHECK_LOADED_DELAY_TIME = 4 * 1000; // ms constexpr int32_t SOFTBUS_SERVER_SA_ID = 4700; } @@ -106,7 +103,7 @@ void SystemAbilityManager::Init() InitSaProfile(); WatchDogInit(); reportEventTimer_ = std::make_unique("DfxReporter"); - OndemandLoadForPerf(); + InitOndemandSwitch(); } void SystemAbilityManager::WatchDogInit() @@ -177,30 +174,18 @@ void SystemAbilityManager::InitSaProfile() HILOGI("[PerformanceTest] InitSaProfile spend %{public}" PRId64 " ms", GetTickCount() - begin); } -void SystemAbilityManager::OndemandLoadForPerf() +void SystemAbilityManager::InitOndemandSwitch() { if (workHandler_ == nullptr) { - HILOGE("LoadForPerf workHandler_ not init!"); + HILOGE("InitOndemandSwitch workHandler_ not init!"); return; } auto callback = [this] () { - OndemandLoad(); + SystemAbilityOndemandSwitch::GetInstance().Init(); }; workHandler_->PostTask(callback, ONDEMAND_PERF_DELAY_TIME); } -void SystemAbilityManager::OndemandLoad() -{ - auto bootEventCallback = [](const char *key, const char *value, void *context) { - int64_t begin = GetTickCount(); - SystemAbilityManager::GetInstance()->DoLoadForPerf(); - HILOGI("[PerformanceTest] DoLoadForPerf spend %{public}" PRId64 " ms", GetTickCount() - begin); - }; - - int ret = WatchParameter(ONDEMAND_PERF_PARAM, bootEventCallback, nullptr); - HILOGD("OndemandLoad ret %{public}d", ret); -} - std::list SystemAbilityManager::GetAllOndemandSa() { std::list ondemandSaids; @@ -217,19 +202,6 @@ std::list SystemAbilityManager::GetAllOndemandSa() return ondemandSaids; } -void SystemAbilityManager::DoLoadForPerf() -{ - bool value = system::GetBoolParameter(ONDEMAND_PARAM, false); - if (value) { - std::list saids = GetAllOndemandSa(); - HILOGD("DoLoadForPerf ondemand size : %{public}zu.", saids.size()); - sptr callback(new SystemAbilityLoadCallbackStub()); - for (auto said : saids) { - LoadSystemAbility(said, callback); - } - } -} - bool SystemAbilityManager::GetSaProfile(int32_t saId, SaProfile& saProfile) { lock_guard autoLock(saProfileMapLock_); diff --git a/services/samgr/native/test/unittest/BUILD.gn b/services/samgr/native/test/unittest/BUILD.gn index 342ef75773ff25008ac238394db491a0f9d98712..07e3ece01c3afdbc7e6bbdfe5c44fa3c32166349 100644 --- a/services/samgr/native/test/unittest/BUILD.gn +++ b/services/samgr/native/test/unittest/BUILD.gn @@ -59,6 +59,7 @@ ohos_unittest("SystemAbilityMgrTest") { "${samgr_services_dir}/source/memory_guard.cpp", "${samgr_services_dir}/source/rpc_callback_imp.cpp", "${samgr_services_dir}/source/schedule/system_ability_event_handler.cpp", + "${samgr_services_dir}/source/schedule/system_ability_ondemand_switch.cpp", "${samgr_services_dir}/source/schedule/system_ability_state_machine.cpp", "${samgr_services_dir}/source/schedule/system_ability_state_scheduler.cpp", "${samgr_services_dir}/source/system_ability_load_callback_proxy.cpp", @@ -172,6 +173,7 @@ ohos_unittest("SystemAbilityMgrStubTest") { "${samgr_services_dir}/source/memory_guard.cpp", "${samgr_services_dir}/source/rpc_callback_imp.cpp", "${samgr_services_dir}/source/schedule/system_ability_event_handler.cpp", + "${samgr_services_dir}/source/schedule/system_ability_ondemand_switch.cpp", "${samgr_services_dir}/source/schedule/system_ability_state_machine.cpp", "${samgr_services_dir}/source/schedule/system_ability_state_scheduler.cpp", "${samgr_services_dir}/source/system_ability_manager.cpp", @@ -324,6 +326,7 @@ ohos_unittest("SystemAbilityStateSchedulerTest") { "${samgr_services_dir}/source/collect/icollect_plugin.cpp", "${samgr_services_dir}/source/memory_guard.cpp", "${samgr_services_dir}/source/schedule/system_ability_event_handler.cpp", + "${samgr_services_dir}/source/schedule/system_ability_ondemand_switch.cpp", "${samgr_services_dir}/source/schedule/system_ability_state_machine.cpp", "${samgr_services_dir}/source/schedule/system_ability_state_scheduler.cpp", "${samgr_services_dir}/source/system_ability_manager.cpp", @@ -420,6 +423,7 @@ ohos_executable("TestTool") { "${samgr_services_dir}/source/memory_guard.cpp", "${samgr_services_dir}/source/rpc_callback_imp.cpp", "${samgr_services_dir}/source/schedule/system_ability_event_handler.cpp", + "${samgr_services_dir}/source/schedule/system_ability_ondemand_switch.cpp", "${samgr_services_dir}/source/schedule/system_ability_state_machine.cpp", "${samgr_services_dir}/source/schedule/system_ability_state_scheduler.cpp", "${samgr_services_dir}/source/system_ability_load_callback_proxy.cpp", diff --git a/services/samgr/native/test/unittest/src/system_ability_mgr_test.cpp b/services/samgr/native/test/unittest/src/system_ability_mgr_test.cpp index a73bffb1d308eef379da7ea399aef382be0e9586..87776969e5bec19ab55fed85914a00efa25116c0 100644 --- a/services/samgr/native/test/unittest/src/system_ability_mgr_test.cpp +++ b/services/samgr/native/test/unittest/src/system_ability_mgr_test.cpp @@ -1858,53 +1858,38 @@ HWTEST_F(SystemAbilityMgrTest, GetParamDebug001, TestSize.Level1) } /** - * @tc.name: Test OndemandLoadForPerf - * @tc.desc: OndemandLoadForPerf001 + * @tc.name: Test InitOndemandSwitch + * @tc.desc: InitOndemandSwitch001 * @tc.type: FUNC * @tc.require: I5KMF7 */ -HWTEST_F(SystemAbilityMgrTest, OndemandLoadForPerf001, TestSize.Level3) +HWTEST_F(SystemAbilityMgrTest, InitOndemandSwitch001, TestSize.Level3) { - DTEST_LOG << " OndemandLoadForPerf001 " << std::endl; + DTEST_LOG << " InitOndemandSwitch001 " << std::endl; sptr saMgr = SystemAbilityManager::GetInstance(); - saMgr->OndemandLoadForPerf(); + saMgr->InitOndemandSwitch(); saMgr->Init(); - saMgr->OndemandLoadForPerf(); + saMgr->InitOndemandSwitch(); usleep(ONDEMAND_SLEEP_TIME); bool value = system::GetBoolParameter(ONDEMAND_PARAM, false); ASSERT_FALSE(value); } /** - * @tc.name: OndemandLoadForPerf002 - * @tc.desc: test OndemandLoadForPerf, workHandler_ is nullptr + * @tc.name: InitOndemandSwitch002 + * @tc.desc: test InitOndemandSwitch, workHandler_ is nullptr * @tc.type: FUNC * @tc.require: I6MO6A */ -HWTEST_F(SystemAbilityMgrTest, OndemandLoadForPerf002, TestSize.Level3) +HWTEST_F(SystemAbilityMgrTest, InitOndemandSwitch002, TestSize.Level3) { DTEST_LOG << " OndemandLoadForPerf002 " << std::endl; sptr saMgr = SystemAbilityManager::GetInstance(); saMgr->workHandler_ = nullptr; - saMgr->OndemandLoadForPerf(); + saMgr->InitOndemandSwitch(); EXPECT_NE(saMgr, nullptr); } -/** - * @tc.name: Test DoLoadForPerf - * @tc.desc: DoLoadForPerf001 - * @tc.type: FUNC - * @tc.require: I5KMF7 - */ -HWTEST_F(SystemAbilityMgrTest, DoLoadForPerf001, TestSize.Level1) -{ - DTEST_LOG << " DoLoadForPerf001 " << std::endl; - sptr saMgr = SystemAbilityManager::GetInstance(); - saMgr->DoLoadForPerf(); - bool value = system::GetBoolParameter(ONDEMAND_PARAM, false); - ASSERT_FALSE(value); -} - /** * @tc.name: Test GetAllOndemandSa001 * @tc.desc: GetAllOndemandSa001 diff --git a/test/fuzztest/systemabilitymanager_fuzzer/BUILD.gn b/test/fuzztest/systemabilitymanager_fuzzer/BUILD.gn index 1bdabe83c01699c92d2fa624adf8fd2d62e2c80d..60e82b230629e6c93820294fd4ec4fbb8c309f7a 100644 --- a/test/fuzztest/systemabilitymanager_fuzzer/BUILD.gn +++ b/test/fuzztest/systemabilitymanager_fuzzer/BUILD.gn @@ -54,6 +54,7 @@ ohos_fuzztest("SystemAbilityManagerFuzzTest") { "${samgr_services_dir}/source/collect/icollect_plugin.cpp", "${samgr_services_dir}/source/memory_guard.cpp", "${samgr_services_dir}/source/schedule/system_ability_event_handler.cpp", + "${samgr_services_dir}/source/schedule/system_ability_ondemand_switch.cpp", "${samgr_services_dir}/source/schedule/system_ability_state_machine.cpp", "${samgr_services_dir}/source/schedule/system_ability_state_scheduler.cpp", "${samgr_services_dir}/source/system_ability_manager.cpp", diff --git a/utils/native/include/single_instance.h b/utils/native/include/single_instance.h new file mode 100644 index 0000000000000000000000000000000000000000..0bd814f559588cceee0a50f6443fe6d1ee88ed4b --- /dev/null +++ b/utils/native/include/single_instance.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 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 SERVICES_SAMGR_INCLUDE_SINGLE_INSTANCE_H +#define SERVICES_SAMGR_INCLUDE_SINGLE_INSTANCE_H + +namespace OHOS { +#define DECLARE_SINGLE_INSTANCE_BASE(className) \ +public: \ + static className& GetInstance(); \ +private: \ + className(const className&) = delete; \ + className& operator= (const className&) = delete; \ + className(className&&) = delete; \ + className& operator= (className&&) = delete; \ + + +#define DECLARE_SINGLE_INSTANCE(className) \ + DECLARE_SINGLE_INSTANCE_BASE(className) \ +private: \ + className() = default; \ + ~className() = default; \ + +#define IMPLEMENT_SINGLE_INSTANCE(className) \ +className& className::GetInstance() \ +{ \ + static auto instance = new className(); \ + return *instance; \ +} +}; // namespace OHOS + +#endif // SERVICES_SAMGR_INCLUDE_SINGLE_INSTANCE_H