From f6356a0e3bcb1e51e12d3db73174a630a73f8744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=98=89=E5=AE=9D?= Date: Tue, 8 Oct 2024 19:57:11 +0800 Subject: [PATCH] =?UTF-8?q?commit=20msg=20=EF=BC=88cherry=20picked=20commi?= =?UTF-8?q?t=20from=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../schedule/system_ability_state_context.h | 1 + .../schedule/system_ability_state_scheduler.h | 1 + .../system_ability_state_scheduler.cpp | 22 ++++++ .../native/source/system_ability_manager.cpp | 1 + .../system_ability_state_scheduler_test.cpp | 71 +++++++++++++++++++ 5 files changed, 96 insertions(+) diff --git a/services/samgr/native/include/schedule/system_ability_state_context.h b/services/samgr/native/include/schedule/system_ability_state_context.h index 88fb3503..f185920c 100644 --- a/services/samgr/native/include/schedule/system_ability_state_context.h +++ b/services/samgr/native/include/schedule/system_ability_state_context.h @@ -86,6 +86,7 @@ struct SystemAbilityContext { std::shared_ptr unloadRequest; int32_t systemAbilityId = -1; int32_t delayUnloadTime = 0; + int64_t lastStartTime = 0; SystemAbilityState state = SystemAbilityState::NOT_LOADED; PendingEvent pendingEvent = PendingEvent::NO_EVENT; bool isAutoRestart = false; diff --git a/services/samgr/native/include/schedule/system_ability_state_scheduler.h b/services/samgr/native/include/schedule/system_ability_state_scheduler.h index a1c46462..dee24b72 100644 --- a/services/samgr/native/include/schedule/system_ability_state_scheduler.h +++ b/services/samgr/native/include/schedule/system_ability_state_scheduler.h @@ -65,6 +65,7 @@ public: int32_t CheckStartEnableOnce(const OnDemandEvent& event, const SaControlInfo& saControl, sptr callback); int32_t CheckStopEnableOnce(const OnDemandEvent& event, const SaControlInfo& saControl); + void UpdateLimitDelayUnloadTime(int32_t systemAbilityId); private: void InitStateContext(const std::list& saProfiles); 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 7e1e8125..801d2ca4 100644 --- a/services/samgr/native/source/schedule/system_ability_state_scheduler.cpp +++ b/services/samgr/native/source/schedule/system_ability_state_scheduler.cpp @@ -39,6 +39,8 @@ constexpr int32_t MAX_SUBSCRIBE_COUNT = 256; constexpr int32_t UNLOAD_TIMEOUT_TIME = 5 * 1000; constexpr const char* LOCAL_DEVICE = "local"; constexpr int32_t MAX_DELAY_TIME = 5 * 60 * 1000; +constexpr int32_t MAX_DURATION = 10 * 60 * 1000; // ms +constexpr int32_t ONCE_DELAY_TIME = 10 * 1000; // ms constexpr const char* CANCEL_UNLOAD = "cancelUnload"; constexpr const char* KEY_EVENT_ID = "eventId"; constexpr const char* KEY_NAME = "name"; @@ -173,6 +175,26 @@ bool SystemAbilityStateScheduler::GetSystemAbilityContext(int32_t systemAbilityI return true; } +void SystemAbilityStateScheduler::UpdateLimitDelayUnloadTime(int32_t systemAbilityId) +{ + std::shared_ptr abilityContext; + if (!GetSystemAbilityContext(systemAbilityId, abilityContext)) { + return; + } + std::lock_guard autoLock(abilityContext->ownProcessContext->processLock); + if (abilityContext->lastStartTime != 0) { + int64_t begin = abilityContext->lastStartTime; + int64_t end = GetTickCount(); + if (end - begin <= MAX_DURATION) { + int64_t onceDelayTime = abilityContext->delayUnloadTime; + onceDelayTime += ONCE_DELAY_TIME; + abilityContext->delayUnloadTime = LimitDelayUnloadTime(onceDelayTime); + HILOGI("DelayUnloadTime is %{public}d, SA:%{public}d", abilityContext->delayUnloadTime, systemAbilityId); + } + } + abilityContext->lastStartTime = GetTickCount(); +} + bool SystemAbilityStateScheduler::GetSystemProcessContext(const std::u16string& processName, std::shared_ptr& processContext) { diff --git a/services/samgr/native/source/system_ability_manager.cpp b/services/samgr/native/source/system_ability_manager.cpp index bb93a68f..1e1aea5b 100644 --- a/services/samgr/native/source/system_ability_manager.cpp +++ b/services/samgr/native/source/system_ability_manager.cpp @@ -1065,6 +1065,7 @@ int32_t SystemAbilityManager::AddSystemAbility(int32_t systemAbilityId, const sp HILOGE("abilityStateScheduler is nullptr"); return ERR_INVALID_VALUE; } + abilityStateScheduler_->UpdateLimitDelayUnloadTime(systemAbilityId); SystemAbilityInvalidateCache(systemAbilityId); abilityStateScheduler_->SendAbilityStateEvent(systemAbilityId, AbilityStateEvent::ABILITY_LOAD_SUCCESS_EVENT); SendSystemAbilityAddedMsg(systemAbilityId, ability); diff --git a/services/samgr/native/test/unittest/src/system_ability_state_scheduler_test.cpp b/services/samgr/native/test/unittest/src/system_ability_state_scheduler_test.cpp index 5f461ffe..5fb9d916 100644 --- a/services/samgr/native/test/unittest/src/system_ability_state_scheduler_test.cpp +++ b/services/samgr/native/test/unittest/src/system_ability_state_scheduler_test.cpp @@ -44,6 +44,8 @@ constexpr int32_t MAX_DELAY_TIME_TEST = 5 * 60 * 1000; constexpr int32_t BEYOND_DELAY_TIME_TEST = 5 * 60 * 1000 + 1; constexpr int32_t TEST_SYSTEM_ABILITY1 = 1491; constexpr int32_t TEST_SYSTEM_ABILITY2 = 1492; +constexpr int32_t ONCE_DELAY_TIME = 10 * 1000; //ms +constexpr int32_t MAX_DURATION = 11 * 60 * 1000; // ms const std::u16string process = u"test"; const std::u16string process_invalid = u"test_invalid"; const std::string LOCAL_DEVICE = "local"; @@ -2454,6 +2456,75 @@ HWTEST_F(SystemAbilityStateSchedulerTest, LimitDelayUnloadTime002, TestSize.Leve EXPECT_EQ(ret, MAX_DELAY_TIME_TEST); } +/** + * @tc.name: UpdateLimitDelayUnloadTime001 + * @tc.desc: test UpdateLimitDelayUnloadTime with saId invalid + * @tc.type: FUNC + * @tc.require: I6FDNZ + */ + +HWTEST_F(SystemAbilityStateSchedulerTest, UpdateLimitDelayUnloadTime001, TestSize.Level3) +{ + std::shared_ptr systemAbilityStateScheduler = + std::make_shared(); + std::shared_ptr abilityContext = std::make_shared(); + abilityContext->systemAbilityId = -1; + abilityContext->lastStartTime = 0; + systemAbilityStateScheduler->UpdateLimitDelayUnloadTime(abilityContext->systemAbilityId); + EXPECT_EQ(abilityContext->lastStartTime, 0); +} + +/** + * @tc.name: UpdateLimitDelayUnloadTime002 + * @tc.desc: test UpdateLimitDelayUnloadTime with lastStartTime not equal to 0 + * @tc.type: FUNC + * @tc.require: I6FDNZ + */ + +HWTEST_F(SystemAbilityStateSchedulerTest, UpdateLimitDelayUnloadTime002, TestSize.Level3) +{ + std::shared_ptr systemAbilityStateScheduler = + std::make_shared(); + int said = 401; + std::list saProfiles; + systemAbilityStateScheduler->Init(saProfiles); + std::shared_ptr abilityContext = std::make_shared(); + std::shared_ptr systemProcessContext = std::make_shared(); + systemAbilityStateScheduler->abilityContextMap_.clear(); + abilityContext->ownProcessContext = systemProcessContext; + systemAbilityStateScheduler->abilityContextMap_[said] = abilityContext; + abilityContext->lastStartTime = GetTickCount(); + int32_t delaytime = abilityContext->delayUnloadTime; + systemAbilityStateScheduler->UpdateLimitDelayUnloadTime(said); + delaytime += ONCE_DELAY_TIME; + EXPECT_EQ(delaytime, abilityContext->delayUnloadTime); +} + +/** + * @tc.name: UpdateLimitDelayUnloadTime002 + * @tc.desc: test UpdateLimitDelayUnloadTime with duation greater than duration + * @tc.type: FUNC + * @tc.require: I6FDNZ + */ + +HWTEST_F(SystemAbilityStateSchedulerTest, UpdateLimitDelayUnloadTime003, TestSize.Level3) +{ + std::shared_ptr systemAbilityStateScheduler = + std::make_shared(); + int said = 401; + std::list saProfiles; + systemAbilityStateScheduler->Init(saProfiles); + std::shared_ptr abilityContext = std::make_shared(); + std::shared_ptr systemProcessContext = std::make_shared(); + systemAbilityStateScheduler->abilityContextMap_.clear(); + abilityContext->ownProcessContext = systemProcessContext; + systemAbilityStateScheduler->abilityContextMap_[said] = abilityContext; + abilityContext->lastStartTime = GetTickCount() - MAX_DURATION; + int32_t delaytime = abilityContext->delayUnloadTime; + systemAbilityStateScheduler->UpdateLimitDelayUnloadTime(said); + EXPECT_EQ(delaytime, abilityContext->delayUnloadTime); +} + /** * @tc.name: PostUnloadTimeoutTask001 * @tc.desc: test PostUnloadTimeoutTask with state is SystemProcessState::STOPPING -- Gitee