From 8cb31c2cdcb3fd3def8480f1bd331e3872bd1134 Mon Sep 17 00:00:00 2001 From: lidongrui Date: Mon, 4 Aug 2025 15:15:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=85=88=E6=9D=80=E5=90=8E?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E7=9A=84=E6=9F=A5=E6=9D=80=E5=9C=BA=E6=99=AF?= =?UTF-8?q?=20Signed-off-by:=20lidongrui=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ib3f38b2ccd8480a0b6f6175c62ca33d12fafb23e --- .../ability_manager/include/exit_reason.h | 1 + .../include/lifecycle_state_info.h | 1 + .../include/appmgr/ams_mgr_interface.h | 3 ++- .../include/appmgr/ams_mgr_proxy.h | 3 ++- .../include/appmgr/app_mgr_client.h | 2 +- .../app_manager/src/appmgr/ams_mgr_proxy.cpp | 6 +++++- .../app_manager/src/appmgr/ams_mgr_stub.cpp | 3 ++- .../app_manager/src/appmgr/app_mgr_client.cpp | 4 ++-- .../include/ability_manager_service.h | 4 ++-- services/abilitymgr/include/ability_record.h | 11 ++++++++++ services/abilitymgr/include/app_scheduler.h | 2 +- .../ui_ability_lifecycle_manager.h | 2 +- .../src/ability_manager_service.cpp | 20 +++++++++++-------- services/abilitymgr/src/app_scheduler.cpp | 5 +++-- services/abilitymgr/src/exit_reason.cpp | 2 ++ .../ui_ability_lifecycle_manager.cpp | 17 +++++++++------- services/appmgr/include/ams_mgr_scheduler.h | 3 ++- .../appmgr/include/app_mgr_service_inner.h | 7 ++++--- services/appmgr/include/app_running_record.h | 11 ++++++++++ services/appmgr/src/ams_mgr_scheduler.cpp | 4 ++-- services/appmgr/src/app_mgr_service_inner.cpp | 11 +++++----- services/appmgr/src/app_running_manager.cpp | 6 +++--- .../include/mock_ams_mgr_scheduler.h | 3 ++- .../mock_ui_ability_lifecycle_manager.h | 2 +- .../src/mock_ui_ability_lifecycle_manager.cpp | 2 +- .../mock_ui_ability_lifecycle_manager.h | 2 +- .../src/mock_ui_ability_lifecycle_manager.cpp | 2 +- .../app_mgr_service_inner_test.cpp | 2 +- ..._ability_lifecycle_manager_second_test.cpp | 6 ++++-- 29 files changed, 97 insertions(+), 50 deletions(-) diff --git a/interfaces/inner_api/ability_manager/include/exit_reason.h b/interfaces/inner_api/ability_manager/include/exit_reason.h index 54edf45414..8c3cd623f7 100644 --- a/interfaces/inner_api/ability_manager/include/exit_reason.h +++ b/interfaces/inner_api/ability_manager/include/exit_reason.h @@ -32,6 +32,7 @@ struct ExitReason : public Parcelable { int32_t subReason = -1; std::string exitMsg = ""; bool shouldKillForeground = true; + bool shouldSkipKillInStartup = false; // set true in low-memory, return ERR_KILL_APP_WHILE_STARTING and do not kill bool ReadFromParcel(Parcel &parcel); virtual bool Marshalling(Parcel &parcel) const override; diff --git a/interfaces/inner_api/ability_manager/include/lifecycle_state_info.h b/interfaces/inner_api/ability_manager/include/lifecycle_state_info.h index e325164bec..d6ce30cddc 100644 --- a/interfaces/inner_api/ability_manager/include/lifecycle_state_info.h +++ b/interfaces/inner_api/ability_manager/include/lifecycle_state_info.h @@ -51,6 +51,7 @@ enum class ErrorLifecycleState { ABILITY_STATE_DIED, ABILITY_STATE_PERMISSION_UPDATE, ABILITY_STATE_LOW_MEMORY_KILL, + ABILITY_STATE_SKIP_KILL_IN_STARTUP, }; /** diff --git a/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_interface.h b/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_interface.h index 681ecfd876..f262db3cdb 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_interface.h +++ b/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_interface.h @@ -118,7 +118,8 @@ public: * @return ERR_OK, return back success, others fail. */ virtual int32_t KillProcessesByPids(const std::vector &pids, - const std::string &reason = "KillProcessesByPids", bool subProcess = false) = 0; + const std::string &reason = "KillProcessesByPids", bool subProcess = false, + bool isKillPrecedeStart = false) = 0; /** * Set child and parent relationship diff --git a/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_proxy.h b/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_proxy.h index e116664f24..432c6fdb49 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_proxy.h +++ b/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_proxy.h @@ -106,7 +106,8 @@ public: * @return ERR_OK, return back success, others fail. */ virtual int32_t KillProcessesByPids(const std::vector &pids, - const std::string &reason = "KillProcessesByPids", bool subProcess = false) override; + const std::string &reason = "KillProcessesByPids", bool subProcess = false, + bool isKillPrecedeStart = false) override; /** * Set child and parent relationship diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_client.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_client.h index 79cf2e45f7..89149bd4b8 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_client.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_client.h @@ -139,7 +139,7 @@ public: * @param subProcess, kill SubProcess or not. */ virtual AppMgrResultCode KillProcessesByPids(const std::vector &pids, - const std::string &reason = "KillProcessesByPids", bool subProcess = false); + const std::string &reason = "KillProcessesByPids", bool subProcess = false, bool isKillPrecedeStart = false); /** * Set child and parent relationship diff --git a/interfaces/inner_api/app_manager/src/appmgr/ams_mgr_proxy.cpp b/interfaces/inner_api/app_manager/src/appmgr/ams_mgr_proxy.cpp index 7a6ebbccd0..efeb449774 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/ams_mgr_proxy.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/ams_mgr_proxy.cpp @@ -276,7 +276,7 @@ void AmsMgrProxy::KillProcessesByUserId(int32_t userId, bool isNeedSendAppSpawnM } int32_t AmsMgrProxy::KillProcessesByPids(const std::vector &pids, const std::string &reason, - bool subProcess) + bool subProcess, bool isKillPrecedeStart) { TAG_LOGI(AAFwkTag::APPMGR, "start"); MessageParcel data; @@ -305,6 +305,10 @@ int32_t AmsMgrProxy::KillProcessesByPids(const std::vector &pids, const TAG_LOGE(AAFwkTag::APPMGR, "Write subProcess failed"); return ERR_FLATTEN_OBJECT; } + if (!data.WriteBool(isKillPrecedeStart)) { + TAG_LOGE(AAFwkTag::APPMGR, "Write isKillPrecedeStart failed"); + return ERR_FLATTEN_OBJECT; + } int32_t ret = SendTransactCmd(static_cast(IAmsMgr::Message::KILL_PROCESSES_BY_PIDS), data, reply, option); if (ret != NO_ERROR) { diff --git a/interfaces/inner_api/app_manager/src/appmgr/ams_mgr_stub.cpp b/interfaces/inner_api/app_manager/src/appmgr/ams_mgr_stub.cpp index aa7d774a34..40aa1fc3be 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/ams_mgr_stub.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/ams_mgr_stub.cpp @@ -344,7 +344,8 @@ ErrCode AmsMgrStub::HandleKillProcessesByPids(MessageParcel &data, MessageParcel } std::string reason = data.ReadString(); bool subProcess = data.ReadBool(); - int32_t ret = KillProcessesByPids(pids, reason, subProcess); + bool isKillPrecedeStart = data.ReadBool(); + int32_t ret = KillProcessesByPids(pids, reason, subProcess, isKillPrecedeStart); reply.WriteInt32(ret); return NO_ERROR; } diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp index adc3efbdbf..e27824f685 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp @@ -262,13 +262,13 @@ AppMgrResultCode AppMgrClient::KillProcessesByUserId(int32_t userId, bool isNeed } AppMgrResultCode AppMgrClient::KillProcessesByPids(const std::vector &pids, const std::string &reason, - bool subProcess) + bool subProcess, bool isKillPrecedeStart) { sptr service = iface_cast(mgrHolder_->GetRemoteObject()); if (service != nullptr) { sptr amsService = service->GetAmsMgr(); if (amsService != nullptr) { - int32_t ret = amsService->KillProcessesByPids(pids, reason, subProcess); + int32_t ret = amsService->KillProcessesByPids(pids, reason, subProcess, isKillPrecedeStart); if (ret == ERR_OK) { return AppMgrResultCode::RESULT_OK; } diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index 823be80a20..887a2ae737 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -2707,7 +2707,7 @@ private: void StartKeepAliveAppsInner(int32_t userId); - bool ProcessLowMemoryKill(int32_t pid, const ExitReason &reason); + bool ProcessLowMemoryKill(int32_t pid, const ExitReason &reason, bool isKillPrecedeStart); struct StartSelfUIAbilityParam { Want want; @@ -2817,7 +2817,7 @@ private: int32_t OpenLinkInner(const Want &want, sptr callerToken, int32_t userId, int requestCode, bool removeInsightIntentFlag); - int32_t KillProcessWithReasonInner(int32_t pid, const ExitReason &reason); + int32_t KillProcessWithReasonInner(int32_t pid, const ExitReason &reason, bool isKillPrecedeStart); #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE std::shared_ptr bgtaskObserver_; #endif diff --git a/services/abilitymgr/include/ability_record.h b/services/abilitymgr/include/ability_record.h index 072c88ce3e..a4b8da73ec 100644 --- a/services/abilitymgr/include/ability_record.h +++ b/services/abilitymgr/include/ability_record.h @@ -598,6 +598,16 @@ public: return killReason_; } + inline void SetIsKillPrecedeStart(bool isKillPrecedeStart) + { + isKillPrecedeStart_.store(isKillPrecedeStart); + } + + inline bool IsKillPrecedeStart() + { + return isKillPrecedeStart_.load(); + } + void PostCancelStartingWindowHotTask(); /** @@ -1517,6 +1527,7 @@ private: bool isPrepareTerminate_ = false; std::string killReason_ = ""; + std::atomic_bool isKillPrecedeStart_ = false; std::shared_ptr launchWant_ = nullptr; std::shared_ptr lastWant_ = nullptr; std::atomic_bool isLastWantBackgroundDriven_ = false; diff --git a/services/abilitymgr/include/app_scheduler.h b/services/abilitymgr/include/app_scheduler.h index f59cc3328d..d8722122d0 100644 --- a/services/abilitymgr/include/app_scheduler.h +++ b/services/abilitymgr/include/app_scheduler.h @@ -241,7 +241,7 @@ public: * @return ERR_OK, return back success, others fail. */ int32_t KillProcessesByPids(const std::vector &pids, - const std::string &reason = "KillProcessesByPids", bool subProcess = false); + const std::string &reason = "KillProcessesByPids", bool subProcess = false, bool isKillPrecedeStart = false); /** * Set child and parent relationship diff --git a/services/abilitymgr/include/scene_board/ui_ability_lifecycle_manager.h b/services/abilitymgr/include/scene_board/ui_ability_lifecycle_manager.h index bc756ebdd2..c87a19c60f 100644 --- a/services/abilitymgr/include/scene_board/ui_ability_lifecycle_manager.h +++ b/services/abilitymgr/include/scene_board/ui_ability_lifecycle_manager.h @@ -391,7 +391,7 @@ public: bool IsBundleStarting(pid_t pid); - void RecordPidKilling(pid_t pid, const std::string &reason); + void RecordPidKilling(pid_t pid, const std::string &reason, bool isKillPrecedeStart); int32_t NotifyStartupExceptionBySCB(int32_t requestId, const std::string &reason); diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 57e0212db6..fe7173a2b9 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -11833,10 +11833,10 @@ int32_t AbilityManagerService::KillProcessWithPrepareTerminate(const std::vector return uiAbilityManager->TryPrepareTerminateByPids(pids); } -bool AbilityManagerService::ProcessLowMemoryKill(int32_t pid, const ExitReason &reason) +bool AbilityManagerService::ProcessLowMemoryKill(int32_t pid, const ExitReason &reason, bool isKillPrecedeStart) { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); - if (reason.reason != Reason::REASON_RESOURCE_CONTROL || reason.exitMsg != GlobalConstant::LOW_MEMORY_KILL) { + if (!isKillPrecedeStart) { return false; } auto uiAbilityManager = GetUIAbilityManagerByUid(IPCSkeleton::GetCallingUid()); @@ -11848,7 +11848,7 @@ bool AbilityManagerService::ProcessLowMemoryKill(int32_t pid, const ExitReason & return true; } // set ability record kill reason - uiAbilityManager->RecordPidKilling(pid, GlobalConstant::LOW_MEMORY_KILL); + uiAbilityManager->RecordPidKilling(pid, reason.exitMsg, isKillPrecedeStart); return false; } @@ -11856,21 +11856,25 @@ int32_t AbilityManagerService::KillProcessWithReason(int32_t pid, const ExitReas { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); XCOLLIE_TIMER_LESS(__PRETTY_FUNCTION__); + bool isKillPrecedeStart = + (reason.reason == Reason::REASON_RESOURCE_CONTROL && reason.exitMsg == GlobalConstant::LOW_MEMORY_KILL) || + reason.shouldSkipKillInStartup; EventInfo eventInfo; eventInfo.callerPid = IPCSkeleton::GetCallingPid(); eventInfo.pid = pid; eventInfo.exitMsg = reason.exitMsg; eventInfo.shouldKillForeground = reason.shouldKillForeground; - auto ret = KillProcessWithReasonInner(pid, reason); + auto ret = KillProcessWithReasonInner(pid, reason, isKillPrecedeStart); TAG_LOGE(AAFwkTag::ABILITYMGR, "KillProcessWithReason ret: %{public}d, reason: %{public}s", ret, reason.exitMsg.c_str()); - if (reason.reason == Reason::REASON_RESOURCE_CONTROL && reason.exitMsg == GlobalConstant::LOW_MEMORY_KILL) { + if (isKillPrecedeStart) { eventHelper_.SendKillProcessWithReasonEvent(ret, "KillProcessWithReason", eventInfo); } return ret; } -int32_t AbilityManagerService::KillProcessWithReasonInner(int32_t pid, const ExitReason &reason) +int32_t AbilityManagerService::KillProcessWithReasonInner(int32_t pid, const ExitReason &reason, + bool isKillPrecedeStart) { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); bool supportShell = AmsConfigurationParameter::GetInstance().IsSupportAAKillWithReason(); @@ -11891,7 +11895,7 @@ int32_t AbilityManagerService::KillProcessWithReasonInner(int32_t pid, const Exi } } - if (ProcessLowMemoryKill(pid, reason)) { + if (ProcessLowMemoryKill(pid, reason, isKillPrecedeStart)) { // if app is already starting, return TAG_LOGI(AAFwkTag::ABILITYMGR, "%{public}d is starting", pid); return ERR_KILL_APP_WHILE_STARTING; @@ -11903,7 +11907,7 @@ int32_t AbilityManagerService::KillProcessWithReasonInner(int32_t pid, const Exi } std::vector pidToBeKilled = { pid }; return IN_PROCESS_CALL(DelayedSingleton::GetInstance()->KillProcessesByPids(pidToBeKilled, - reason.exitMsg, true)); + reason.exitMsg, true, isKillPrecedeStart)); } int32_t AbilityManagerService::RegisterAutoStartupSystemCallback(const sptr &callback) diff --git a/services/abilitymgr/src/app_scheduler.cpp b/services/abilitymgr/src/app_scheduler.cpp index 5fb6f97263..71b1c35fc9 100644 --- a/services/abilitymgr/src/app_scheduler.cpp +++ b/services/abilitymgr/src/app_scheduler.cpp @@ -160,11 +160,12 @@ void AppScheduler::KillProcessesByUserId(int32_t userId, bool isNeedSendAppSpawn appMgrClient_->KillProcessesByUserId(userId, isNeedSendAppSpawnMsg, callback); } -int32_t AppScheduler::KillProcessesByPids(const std::vector &pids, const std::string &reason, bool subProcess) +int32_t AppScheduler::KillProcessesByPids(const std::vector &pids, const std::string &reason, bool subProcess, + bool isKillPrecedeStart) { TAG_LOGI(AAFwkTag::ABILITYMGR, "call"); CHECK_POINTER_AND_RETURN(appMgrClient_, INNER_ERR); - int32_t ret = appMgrClient_->KillProcessesByPids(pids, reason, subProcess); + int32_t ret = appMgrClient_->KillProcessesByPids(pids, reason, subProcess, isKillPrecedeStart); if (ret != ERR_OK) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to KillProcessesByPids"); return ret; diff --git a/services/abilitymgr/src/exit_reason.cpp b/services/abilitymgr/src/exit_reason.cpp index 4c35271231..7d3f226973 100644 --- a/services/abilitymgr/src/exit_reason.cpp +++ b/services/abilitymgr/src/exit_reason.cpp @@ -43,6 +43,7 @@ bool ExitReason::ReadFromParcel(Parcel &parcel) subReason = reasonData; exitMsg = Str16ToStr8(parcel.ReadString16()); READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, shouldKillForeground); + READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, shouldSkipKillInStartup); return true; } @@ -67,6 +68,7 @@ bool ExitReason::Marshalling(Parcel &parcel) const WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, subReason); WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(exitMsg)); WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, shouldKillForeground); + WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Bool, parcel, shouldSkipKillInStartup); return true; } } // namespace AppExecFwk diff --git a/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp b/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp index 14a3194b26..c07358f10d 100644 --- a/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp +++ b/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp @@ -164,12 +164,13 @@ void UIAbilityLifecycleManager::RemoveStartingPid(pid_t pid) TAG_LOGW(AAFwkTag::ABILITYMGR, "%{public}d not found", pid); } -void UIAbilityLifecycleManager::RecordPidKilling(pid_t pid, const std::string &reason) +void UIAbilityLifecycleManager::RecordPidKilling(pid_t pid, const std::string &reason, bool isKillPrecedeStart) { std::lock_guard guard(sessionLock_); for (const auto& [first, second] : sessionAbilityMap_) { if (second && pid == second->GetPid()) { second->SetKillReason(reason); + second->SetIsKillPrecedeStart(isKillPrecedeStart); } } } @@ -277,7 +278,7 @@ std::shared_ptr UIAbilityLifecycleManager::GenerateAbilityRecord( std::shared_ptr uiAbilityRecord = nullptr; auto iter = sessionAbilityMap_.find(sessionInfo->persistentId); bool isLowMemKill = (iter != sessionAbilityMap_.end()) && - (iter->second != nullptr) && (iter->second->GetKillReason() == GlobalConstant::LOW_MEMORY_KILL); + (iter->second != nullptr) && (iter->second->IsKillPrecedeStart()); if (iter == sessionAbilityMap_.end() || isLowMemKill) { uiAbilityRecord = FindRecordFromTmpMap(abilityRequest); auto abilityInfo = abilityRequest.abilityInfo; @@ -2191,6 +2192,7 @@ void UIAbilityLifecycleManager::NotifySCBToHandleException(const std::shared_ptr sptr info = abilityRecord->GetSessionInfo(); info->errorCode = errorCode; info->errorReason = errorReason; + info->shouldSkipKillInStartup = abilityRecord->IsKillPrecedeStart(); Rosen::ExceptionInfo exceptionInfo; exceptionInfo.needClearCallerLink = needClearCallerLink; session->NotifySessionException(info, exceptionInfo); @@ -2273,11 +2275,12 @@ void UIAbilityLifecycleManager::OnAbilityDied(std::shared_ptr abi NotifySCBToHandleException(abilityRecord, static_cast(ErrorLifecycleState::ABILITY_STATE_PERMISSION_UPDATE), "kill process for permission update", needClearCallerLink); - } else if (abilityRecord->GetKillReason() == GlobalConstant::LOW_MEMORY_KILL) { - TAG_LOGI(AAFwkTag::ABILITYMGR, "kill by low memory"); - NotifySCBToHandleException(abilityRecord, - static_cast(ErrorLifecycleState::ABILITY_STATE_LOW_MEMORY_KILL), - abilityRecord->GetKillReason()); + } else if (abilityRecord->IsKillPrecedeStart()) { + TAG_LOGI(AAFwkTag::ABILITYMGR, "Killing processes before application startup"); + auto errCode = abilityRecord->GetKillReason() == GlobalConstant::LOW_MEMORY_KILL ? + static_cast(ErrorLifecycleState::ABILITY_STATE_LOW_MEMORY_KILL) : + static_cast(ErrorLifecycleState::ABILITY_STATE_SKIP_KILL_IN_STARTUP); + NotifySCBToHandleException(abilityRecord, errCode, abilityRecord->GetKillReason()); } else if (!abilityRecord->GetRestartAppFlag()) { NotifySCBToHandleException(abilityRecord, static_cast(ErrorLifecycleState::ABILITY_STATE_DIED), "onAbilityDied"); diff --git a/services/appmgr/include/ams_mgr_scheduler.h b/services/appmgr/include/ams_mgr_scheduler.h index c466fe1194..c25df4d8e0 100644 --- a/services/appmgr/include/ams_mgr_scheduler.h +++ b/services/appmgr/include/ams_mgr_scheduler.h @@ -120,7 +120,8 @@ public: * @return ERR_OK, return back success, others fail. */ virtual int32_t KillProcessesByPids(const std::vector &pids, - const std::string &reason = "KillProcessesByPids", bool subProcess = false) override; + const std::string &reason = "KillProcessesByPids", bool subProcess = false, + bool isKillPrecedeStart = false) override; /** * Set child and parent relationship diff --git a/services/appmgr/include/app_mgr_service_inner.h b/services/appmgr/include/app_mgr_service_inner.h index ba2fd4ab26..3aa160cf0f 100644 --- a/services/appmgr/include/app_mgr_service_inner.h +++ b/services/appmgr/include/app_mgr_service_inner.h @@ -210,7 +210,7 @@ public: * @return ERR_OK, return back success, others fail. */ virtual int32_t KillProcessesByPids(const std::vector &pids, - const std::string &reason = "KillProcessesByPids", bool subProcess = false); + const std::string &reason = "KillProcessesByPids", bool subProcess = false, bool isKillPrecedeStart = false); /** * KillProcessesInBatch, kill processes in batch; @@ -982,7 +982,8 @@ public: int32_t NotifyAppStatusByCommonEventName(const std::string &bundleName, const std::string &eventName, const Want &want); - int32_t KillProcessByPid(const pid_t pid, const std::string& reason = "foundation"); + int32_t KillProcessByPid(const pid_t pid, const std::string& reason = "foundation", + bool isKillPrecedeStart = false); int32_t KillSubProcessBypidInner(const pid_t pid, const std::string &reason, AAFwk::EventInfo &eventInfo); @@ -2074,7 +2075,7 @@ private: int32_t GetAllRunningInstanceKeysByBundleNameInner(const std::string &bundleName, std::vector &instanceKeys, int32_t userId); int32_t KillProcessByPidInner(const pid_t pid, const std::string& reason, - const std::string& killReason, std::shared_ptr appRecord); + const std::string& killReason, std::shared_ptr appRecord, bool isKillPrecedeStart); void SetKilledEventInfo(std::shared_ptr appRecord, AAFwk::EventInfo &eventInfo); void AddToKillProcessMap(const std::string &processName); bool IsAllowedNWebPreload(const std::string &processName); diff --git a/services/appmgr/include/app_running_record.h b/services/appmgr/include/app_running_record.h index 685291ff96..802ca02ad3 100644 --- a/services/appmgr/include/app_running_record.h +++ b/services/appmgr/include/app_running_record.h @@ -1054,6 +1054,16 @@ public: return killReason_; } + inline void SetIsKillPrecedeStart(bool isKillPrecedeStart) + { + isKillPrecedeStart_.store(isKillPrecedeStart); + } + + inline bool IsKillPrecedeStart() const + { + return isKillPrecedeStart_.load(); + } + void AddAppLifecycleEvent(const std::string &msg); void SetNWebPreload(const bool isAllowedNWebPreload); @@ -1297,6 +1307,7 @@ private: sptr browserHost_; std::shared_ptr delayConfiguration_ = std::make_shared(); std::string killReason_ = ""; + std::atomic_bool isKillPrecedeStart_ = false; int32_t rssValue_ = 0; int32_t pssValue_ = 0; bool reasonExist_ = false; diff --git a/services/appmgr/src/ams_mgr_scheduler.cpp b/services/appmgr/src/ams_mgr_scheduler.cpp index a5ac6fed15..c2c35f0cc4 100644 --- a/services/appmgr/src/ams_mgr_scheduler.cpp +++ b/services/appmgr/src/ams_mgr_scheduler.cpp @@ -243,7 +243,7 @@ void AmsMgrScheduler::KillProcessesByUserId(int32_t userId, bool isNeedSendAppSp } int32_t AmsMgrScheduler::KillProcessesByPids(const std::vector &pids, const std::string &reason, - bool subProcess) + bool subProcess, bool isKillPrecedeStart) { if (!IsReady()) { return ERR_INVALID_OPERATION; @@ -256,7 +256,7 @@ int32_t AmsMgrScheduler::KillProcessesByPids(const std::vector &pids, c return ERR_PERMISSION_DENIED; } - return amsMgrServiceInner_->KillProcessesByPids(pids, reason, subProcess); + return amsMgrServiceInner_->KillProcessesByPids(pids, reason, subProcess, isKillPrecedeStart); } void AmsMgrScheduler::AttachPidToParent(const sptr &token, const sptr &callerToken) diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index 66dd689fce..30acca2233 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -2964,7 +2964,7 @@ void AppMgrServiceInner::GetChildrenProcesses(const std::shared_ptrSetExitMsg(killReason); } TAG_LOGI(AAFwkTag::APPMGR, "kill reason=%{public}s, pid=%{public}d", reason.c_str(), pid); - return KillProcessByPidInner(pid, reason, killReason, appRecord); + return KillProcessByPidInner(pid, reason, killReason, appRecord, isKillPrecedeStart); } int32_t AppMgrServiceInner::KillProcessByPidInner(const pid_t pid, const std::string& reason, - const std::string& killReason, std::shared_ptr appRecord) + const std::string& killReason, std::shared_ptr appRecord, bool isKillPrecedeStart) { HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); AAFwk::RecordCostTimeUtil timeRecord("KillProcessByPidInner"); @@ -3008,6 +3008,7 @@ int32_t AppMgrServiceInner::KillProcessByPidInner(const pid_t pid, const std::st AAFwk::EventInfo eventInfo; SetKilledEventInfo(appRecord, eventInfo); appRecord->SetKillReason(reason); + appRecord->SetIsKillPrecedeStart(isKillPrecedeStart); if (ret >= 0) { AddToKillProcessMap(appRecord->GetProcessName()); } @@ -3570,14 +3571,14 @@ int32_t AppMgrServiceInner::KillSubProcessBypid(const pid_t pid, const std::stri } int32_t AppMgrServiceInner::KillProcessesByPids(const std::vector &pids, const std::string &reason, - bool subProcess) + bool subProcess, bool isKillPrecedeStart) { HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); int32_t ret = ERR_OK; for (const auto& pid: pids) { auto appRecord = GetAppRunningRecordByPid(pid); if (appRecord != nullptr) { - ret = KillProcessByPid(pid, reason); + ret = KillProcessByPid(pid, reason, isKillPrecedeStart); if (ret != ERR_OK) { TAG_LOGW(AAFwkTag::APPMGR, "fail, pid:%{public}d", pid); return ret; diff --git a/services/appmgr/src/app_running_manager.cpp b/services/appmgr/src/app_running_manager.cpp index e78da739dc..d548962a55 100644 --- a/services/appmgr/src/app_running_manager.cpp +++ b/services/appmgr/src/app_running_manager.cpp @@ -141,7 +141,7 @@ std::shared_ptr AppRunningManager::CheckAppRunningRecordIsExis AppRunningManager::CheckAppProcessNameIsSame(pair.second, processName) && (pair.second->GetJointUserId() == jointUserId) && !(pair.second->IsTerminating()) && !(pair.second->IsKilling()) && !(pair.second->GetRestartAppFlag()) && - (pair.second->GetKillReason() != AbilityRuntime::GlobalConstant::LOW_MEMORY_KILL); + !(pair.second->IsKillPrecedeStart()); }; auto appRunningMap = GetAppRunningRecordMap(); if (!jointUserId.empty()) { @@ -157,7 +157,7 @@ std::shared_ptr AppRunningManager::CheckAppRunningRecordIsExis !(appRecord->IsTerminating()) && !(appRecord->IsKilling()) && !(appRecord->GetRestartAppFlag()) && !(appRecord->IsUserRequestCleaning()) && !(appRecord->IsCaching() && appRecord->GetProcessCacheBlocked()) && - appRecord->GetKillReason() != AbilityRuntime::GlobalConstant::LOW_MEMORY_KILL) { + !(appRecord->IsKillPrecedeStart())) { auto appInfoList = appRecord->GetAppInfoList(); TAG_LOGD(AAFwkTag::APPMGR, "appInfoList: %{public}zu, processName: %{public}s, specifiedProcessFlag: %{public}s, \ @@ -204,7 +204,7 @@ std::shared_ptr AppRunningManager::CheckAppRunningRecordForSpe !(appRecord->IsTerminating()) && !(appRecord->IsKilling()) && !(appRecord->GetRestartAppFlag()) && !(appRecord->IsUserRequestCleaning()) && !(appRecord->IsCaching() && appRecord->GetProcessCacheBlocked()) && - appRecord->GetKillReason() != AbilityRuntime::GlobalConstant::LOW_MEMORY_KILL) { + !(appRecord->IsKillPrecedeStart())) { return appRecord; } } diff --git a/test/mock/services_appmgr_test/include/mock_ams_mgr_scheduler.h b/test/mock/services_appmgr_test/include/mock_ams_mgr_scheduler.h index fb978b3d83..5a8c6a0b84 100644 --- a/test/mock/services_appmgr_test/include/mock_ams_mgr_scheduler.h +++ b/test/mock/services_appmgr_test/include/mock_ams_mgr_scheduler.h @@ -69,7 +69,8 @@ public: MOCK_METHOD1(IsWaitingDebugApp, bool(const std::string &bundleName)); MOCK_METHOD0(ClearNonPersistWaitingDebugFlag, void()); MOCK_METHOD0(IsMemorySizeSufficent, bool()); - MOCK_METHOD3(KillProcessesByPids, int32_t(const std::vector &, const std::string &, bool subProcess)); + MOCK_METHOD4(KillProcessesByPids, int32_t(const std::vector &pids, const std::string &reason, + bool subProcess, bool isKillPrecedeStart)); MockAmsMgrScheduler() : AmsMgrStub() {}; virtual ~MockAmsMgrScheduler() {}; diff --git a/test/unittest/ability_manager_service_fourteenth_test/mock/include/mock_ui_ability_lifecycle_manager.h b/test/unittest/ability_manager_service_fourteenth_test/mock/include/mock_ui_ability_lifecycle_manager.h index 3cfde80cc1..095aed13a7 100644 --- a/test/unittest/ability_manager_service_fourteenth_test/mock/include/mock_ui_ability_lifecycle_manager.h +++ b/test/unittest/ability_manager_service_fourteenth_test/mock/include/mock_ui_ability_lifecycle_manager.h @@ -389,7 +389,7 @@ public: bool IsBundleStarting(pid_t pid); - void RecordPidKilling(pid_t pid, const std::string &reason); + void RecordPidKilling(pid_t pid, const std::string &reason, bool isKillPrecedeStart); int32_t NotifyStartupExceptionBySCB(int32_t requestId, const std::string &reason); diff --git a/test/unittest/ability_manager_service_fourteenth_test/mock/src/mock_ui_ability_lifecycle_manager.cpp b/test/unittest/ability_manager_service_fourteenth_test/mock/src/mock_ui_ability_lifecycle_manager.cpp index c443a5f753..d66f86a5a2 100644 --- a/test/unittest/ability_manager_service_fourteenth_test/mock/src/mock_ui_ability_lifecycle_manager.cpp +++ b/test/unittest/ability_manager_service_fourteenth_test/mock/src/mock_ui_ability_lifecycle_manager.cpp @@ -41,7 +41,7 @@ void UIAbilityLifecycleManager::RemoveStartingPid(pid_t pid) { } -void UIAbilityLifecycleManager::RecordPidKilling(pid_t pid, const std::string &reason) +void UIAbilityLifecycleManager::RecordPidKilling(pid_t pid, const std::string &reason, bool isKillPrecedeStart) { } diff --git a/test/unittest/ability_manager_service_thirteenth_test/mock/include/mock_ui_ability_lifecycle_manager.h b/test/unittest/ability_manager_service_thirteenth_test/mock/include/mock_ui_ability_lifecycle_manager.h index 0585523d2a..228d5c0602 100644 --- a/test/unittest/ability_manager_service_thirteenth_test/mock/include/mock_ui_ability_lifecycle_manager.h +++ b/test/unittest/ability_manager_service_thirteenth_test/mock/include/mock_ui_ability_lifecycle_manager.h @@ -391,7 +391,7 @@ public: bool IsBundleStarting(pid_t pid); - void RecordPidKilling(pid_t pid, const std::string &reason); + void RecordPidKilling(pid_t pid, const std::string &reason, bool isKillPrecedeStart); int32_t NotifyStartupExceptionBySCB(int32_t requestId, const std::string &reason); diff --git a/test/unittest/ability_manager_service_thirteenth_test/mock/src/mock_ui_ability_lifecycle_manager.cpp b/test/unittest/ability_manager_service_thirteenth_test/mock/src/mock_ui_ability_lifecycle_manager.cpp index 958c2d71cd..10a12975cc 100644 --- a/test/unittest/ability_manager_service_thirteenth_test/mock/src/mock_ui_ability_lifecycle_manager.cpp +++ b/test/unittest/ability_manager_service_thirteenth_test/mock/src/mock_ui_ability_lifecycle_manager.cpp @@ -44,7 +44,7 @@ void UIAbilityLifecycleManager::RemoveStartingPid(pid_t pid) { } -void UIAbilityLifecycleManager::RecordPidKilling(pid_t pid, const std::string &reason) +void UIAbilityLifecycleManager::RecordPidKilling(pid_t pid, const std::string &reason, bool isKillPrecedeStart) { } diff --git a/test/unittest/app_mgr_service_inner_test/app_mgr_service_inner_test.cpp b/test/unittest/app_mgr_service_inner_test/app_mgr_service_inner_test.cpp index 89ebf10c59..5e71b1afae 100644 --- a/test/unittest/app_mgr_service_inner_test/app_mgr_service_inner_test.cpp +++ b/test/unittest/app_mgr_service_inner_test/app_mgr_service_inner_test.cpp @@ -1209,7 +1209,7 @@ HWTEST_F(AppMgrServiceInnerTest, KillProcessByPidInner_001, TestSize.Level2) ASSERT_NE(appMgrServiceInner, nullptr); pid_t pid = 99999999; - auto ret = appMgrServiceInner->KillProcessByPidInner(pid, "ReasonTdd", "KillReasonTdd", nullptr); + auto ret = appMgrServiceInner->KillProcessByPidInner(pid, "ReasonTdd", "KillReasonTdd", nullptr, false); EXPECT_EQ(ret, AAFwk::ERR_KILL_PROCESS_NOT_EXIST); TAG_LOGI(AAFwkTag::TEST, "KillProcessByPidInner_001 end"); } diff --git a/test/unittest/ui_ability_lifecycle_manager_second_test/ui_ability_lifecycle_manager_second_test.cpp b/test/unittest/ui_ability_lifecycle_manager_second_test/ui_ability_lifecycle_manager_second_test.cpp index 54dc9abbfb..0ea039f3b2 100644 --- a/test/unittest/ui_ability_lifecycle_manager_second_test/ui_ability_lifecycle_manager_second_test.cpp +++ b/test/unittest/ui_ability_lifecycle_manager_second_test/ui_ability_lifecycle_manager_second_test.cpp @@ -1109,9 +1109,11 @@ HWTEST_F(UIAbilityLifecycleManagerSecondTest, RecordPidKilling_001, TestSize.Lev mgr->sessionAbilityMap_ = sessionAbilityMap; pid_t pid = 1; std::string reason = "HelloWorld"; - - mgr->RecordPidKilling(pid, reason); + bool isKillPrecedeStart = true; + + mgr->RecordPidKilling(pid, reason, isKillPrecedeStart); EXPECT_EQ(mgr->sessionAbilityMap_[1]->killReason_, "HelloWorld"); + EXPECT_EQ(mgr->sessionAbilityMap_[1]->isKillPrecedeStart_, true); } /**