mirror of
https://github.com/openharmony/ability_ability_runtime.git
synced 2026-08-24 12:43:16 -04:00
killproceewithreason and set rss pss
Signed-off-by: hanchenZz <wanghanchen1@huawei.com>
This commit is contained in:
@@ -1001,6 +1001,15 @@ public:
|
||||
|
||||
ErrCode KillProcessWithPrepareTerminate(const std::vector<int32_t>& pids);
|
||||
|
||||
/**
|
||||
* kill the process with reason
|
||||
*
|
||||
* @param pid id of process.
|
||||
* @param reason, kill process reason.
|
||||
* @return Returns ERR_OK on success, others on failure.
|
||||
*/
|
||||
ErrCode KillProcessWithReason(int32_t pid, const ExitReason &reason);
|
||||
|
||||
/**
|
||||
* @brief Register auto start up callback for system api.
|
||||
* @param callback The point of JsAbilityAutoStartupCallBack.
|
||||
|
||||
@@ -1535,6 +1535,11 @@ public:
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual int32_t KillProcessWithReason(int32_t pid, const ExitReason &reason)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Register auto start up callback for system api.
|
||||
* @param callback The point of JsAbilityAutoStartupCallBack.
|
||||
|
||||
@@ -517,6 +517,8 @@ enum class AbilityManagerInterfaceCode {
|
||||
REGISTER_STATUS_BAR_DELEGATE = 5100,
|
||||
KILL_PROCESS_WITH_PREPARE_TERMINATE = 5101,
|
||||
|
||||
KILL_PROCESS_WITH_REASON = 5200,
|
||||
|
||||
FORCE_EXIT_APP = 6001,
|
||||
RECORD_APP_EXIT_REASON = 6002,
|
||||
RECORD_PROCESS_EXIT_REASON = 6003,
|
||||
|
||||
@@ -59,7 +59,9 @@ enum Reason {
|
||||
REASON_PERFORMANCE_CONTROL,
|
||||
REASON_RESOURCE_CONTROL,
|
||||
REASON_UPGRADE,
|
||||
REASON_MAX = REASON_UPGRADE,
|
||||
REASON_USER_REQUEST,
|
||||
REASON_SIGNAL,
|
||||
REASON_MAX = REASON_SIGNAL,
|
||||
};
|
||||
|
||||
enum UserStatus {
|
||||
|
||||
@@ -26,8 +26,10 @@ namespace AAFwk {
|
||||
struct ExitReason : public Parcelable {
|
||||
ExitReason() = default;
|
||||
ExitReason(const Reason reason, const std::string &exitMsg);
|
||||
ExitReason(const Reason &reason, int32_t subReason, const std::string &exitMsg);
|
||||
|
||||
Reason reason = Reason::REASON_UNKNOWN;
|
||||
int32_t subReason = -1;
|
||||
std::string exitMsg = "";
|
||||
|
||||
bool ReadFromParcel(Parcel &parcel);
|
||||
|
||||
@@ -102,6 +102,7 @@ ohos_shared_library("app_manager") {
|
||||
"src/appmgr/priority_object.cpp",
|
||||
"src/appmgr/process_data.cpp",
|
||||
"src/appmgr/process_info.cpp",
|
||||
"src/appmgr/process_memory_state.cpp",
|
||||
"src/appmgr/profile.cpp",
|
||||
"src/appmgr/quick_fix_callback_proxy.cpp",
|
||||
"src/appmgr/quick_fix_callback_stub.cpp",
|
||||
|
||||
@@ -200,6 +200,14 @@ public:
|
||||
virtual AppMgrResultCode KillApplicationSelf(const bool clearPageStack = false,
|
||||
const std::string& reason = "KillApplicationSelf");
|
||||
|
||||
/**
|
||||
* update process rss and pss value.
|
||||
*
|
||||
* @param procMemStates, the memory states of all apps.
|
||||
* @return ERR_OK, return back success, others fail.
|
||||
*/
|
||||
virtual int32_t UpdateProcessMemoryState(const std::vector<ProcessMemoryState> &procMemState);
|
||||
|
||||
/**
|
||||
* ClearUpApplicationData, call ClearUpApplicationData() through proxy project,
|
||||
* clear the application data.
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
#include "irender_state_observer.h"
|
||||
#include "memory_level_info.h"
|
||||
#include "page_state_data.h"
|
||||
#include "process_memory_state.h"
|
||||
#include "render_process_info.h"
|
||||
#include "running_process_info.h"
|
||||
#include "system_memory_attr.h"
|
||||
@@ -912,6 +913,18 @@ public:
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* UpdateProcessMemoryState, call UpdateProcessMemoryState() through proxy object,
|
||||
* update process rss and pss value.
|
||||
*
|
||||
* @param procMemStates, the memory states of all apps.
|
||||
* @return ERR_OK, return back success, others fail.
|
||||
*/
|
||||
virtual int32_t UpdateProcessMemoryState(const std::vector<ProcessMemoryState> &procMemState)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
} // namespace AppExecFwk
|
||||
} // namespace OHOS
|
||||
|
||||
@@ -123,6 +123,7 @@ enum class AppMgrInterfaceCode {
|
||||
IS_APP_RUNNING_BY_BUNDLE_NAME_AND_USER_ID = 97,
|
||||
HAS_APP_RECORD = 98,
|
||||
GET_RUNNING_PROCESS_INFO_BY_CHILD_PROCESS_PID = 99,
|
||||
UPDATE_PROCESS_MEMORY_STATE = 100,
|
||||
};
|
||||
} // AppExecFwk
|
||||
} // OHOS
|
||||
|
||||
@@ -782,6 +782,15 @@ public:
|
||||
virtual void UpdateInstanceKeyBySpecifiedId(int32_t specifiedId, std::string &instanceKey) override;
|
||||
|
||||
int32_t HasAppRecord(const AAFwk::Want &want, const AbilityInfo &abilityInfo, bool &result) override;
|
||||
|
||||
/**
|
||||
* UpdateProcessMemoryState, call UpdateProcessMemoryState() through proxy object,
|
||||
* update process rss and pss value.
|
||||
*
|
||||
* @param procMemStates, the memory states of all apps.
|
||||
* @return ERR_OK, return back success, others fail.
|
||||
*/
|
||||
virtual int32_t UpdateProcessMemoryState(const std::vector<ProcessMemoryState> &procMemState) override;
|
||||
private:
|
||||
bool SendTransactCmd(AppMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply);
|
||||
bool WriteInterfaceToken(MessageParcel &data);
|
||||
|
||||
@@ -186,6 +186,7 @@ private:
|
||||
int32_t OnRemoteRequestInnerSeventh(uint32_t code, MessageParcel &data,
|
||||
MessageParcel &reply, MessageOption &option);
|
||||
int32_t HandleGetSupportedProcessCachePids(MessageParcel &data, MessageParcel &reply);
|
||||
int32_t HandleUpdateProcessMemoryState(MessageParcel &data, MessageParcel &reply);
|
||||
DISALLOW_COPY_AND_MOVE(AppMgrStub);
|
||||
};
|
||||
} // namespace AppExecFwk
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (c) 2025 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_ABILITY_RUNTIME_PROCESS_MEMORY_STATE_H
|
||||
#define OHOS_ABILITY_RUNTIME_PROCESS_MEMORY_STATE_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "parcel.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace AppExecFwk {
|
||||
struct ProcessMemoryState : public Parcelable {
|
||||
int32_t pid = 0;
|
||||
int32_t rssValue = 0;
|
||||
int32_t pssValue = 0;
|
||||
|
||||
bool ReadFromParcel(Parcel &parcel);
|
||||
virtual bool Marshalling(Parcel &parcel) const override;
|
||||
static ProcessMemoryState *Unmarshalling(Parcel &parcel);
|
||||
};
|
||||
} // namespace AppExecFwk
|
||||
} // namespace OHOS
|
||||
|
||||
#endif // OHOS_ABILITY_RUNTIME_PROCESS_MEMORY_STATE_H
|
||||
@@ -67,7 +67,8 @@ struct RunningProcessInfo : public Parcelable {
|
||||
std::int32_t appCloneIndex = -1;
|
||||
std::string instanceKey = "";
|
||||
AppExecFwk::MultiAppModeType appMode = AppExecFwk::MultiAppModeType::UNSPECIFIED;
|
||||
|
||||
std::int32_t rssValue = 0;
|
||||
std::int32_t pssValue = 0;
|
||||
bool ReadFromParcel(Parcel &parcel);
|
||||
virtual bool Marshalling(Parcel &parcel) const override;
|
||||
static RunningProcessInfo *Unmarshalling(Parcel &parcel);
|
||||
|
||||
@@ -356,6 +356,16 @@ AppMgrResultCode AppMgrClient::KillApplicationSelf(const bool clearPageStack, co
|
||||
return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
|
||||
}
|
||||
|
||||
int32_t AppMgrClient::UpdateProcessMemoryState(const std::vector<AppExecFwk::ProcessMemoryState> &procMemState)
|
||||
{
|
||||
sptr<IAppMgr> service = iface_cast<IAppMgr>(mgrHolder_->GetRemoteObject());
|
||||
if (service == nullptr) {
|
||||
TAG_LOGE(AAFwkTag::APPMGR, "Service is nullptr.");
|
||||
return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED;
|
||||
}
|
||||
return service->UpdateProcessMemoryState(procMemState);
|
||||
}
|
||||
|
||||
AppMgrResultCode AppMgrClient::ClearUpApplicationData(const std::string &bundleName, int32_t appCloneIndex,
|
||||
int32_t userId)
|
||||
{
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
namespace OHOS {
|
||||
namespace AppExecFwk {
|
||||
constexpr int32_t CYCLE_LIMIT = 1000;
|
||||
constexpr int32_t MAX_PROCESS_STATE_COUNT = 1000;
|
||||
AppMgrProxy::AppMgrProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IAppMgr>(impl)
|
||||
{}
|
||||
|
||||
@@ -2201,5 +2202,37 @@ int32_t AppMgrProxy::HasAppRecord(const AAFwk::Want &want, const AbilityInfo &ab
|
||||
result = reply.ReadBool();
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
int32_t AppMgrProxy::UpdateProcessMemoryState(const std::vector<ProcessMemoryState> &procMemState)
|
||||
{
|
||||
MessageParcel data;
|
||||
MessageParcel reply;
|
||||
|
||||
auto size = procMemState.size();
|
||||
if (size == 0 || size > MAX_PROCESS_STATE_COUNT) {
|
||||
TAG_LOGE(AAFwkTag::APPMGR, "Invalid size");
|
||||
return ERR_FLATTEN_OBJECT;
|
||||
}
|
||||
if (!WriteInterfaceToken(data)) {
|
||||
TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
|
||||
return IPC_PROXY_ERR;
|
||||
}
|
||||
if (!data.WriteUint32(size)) {
|
||||
TAG_LOGE(AAFwkTag::APPMGR, "Write size failed");
|
||||
return ERR_FLATTEN_OBJECT;
|
||||
}
|
||||
for (const auto &perState: procMemState) {
|
||||
if (!data.WriteParcelable(&perState)) {
|
||||
TAG_LOGE(AAFwkTag::APPMGR, "Write perState failed");
|
||||
return ERR_FLATTEN_OBJECT;
|
||||
}
|
||||
}
|
||||
int32_t ret = SendTransactCmd(AppMgrInterfaceCode::UPDATE_PROCESS_MEMORY_STATE, data, reply);
|
||||
if (ret != NO_ERROR) {
|
||||
TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
|
||||
return ret;
|
||||
}
|
||||
return reply.ReadInt32();
|
||||
}
|
||||
} // namespace AppExecFwk
|
||||
} // namespace OHOS
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
namespace OHOS {
|
||||
namespace AppExecFwk {
|
||||
constexpr int32_t CYCLE_LIMIT = 1000;
|
||||
constexpr int32_t MAX_PROCESS_STATE_COUNT = 1000;
|
||||
|
||||
AppMgrStub::AppMgrStub() {}
|
||||
|
||||
@@ -367,6 +368,8 @@ int32_t AppMgrStub::OnRemoteRequestInnerSeventh(uint32_t code, MessageParcel &da
|
||||
return HandleKillAppSelfWithInstanceKey(data, reply);
|
||||
case static_cast<uint32_t>(AppMgrInterfaceCode::UPDATE_INSTANCE_KEY_BY_SPECIFIED_ID):
|
||||
return HandleUpdateInstanceKeyBySpecifiedId(data, reply);
|
||||
case static_cast<uint32_t>(AppMgrInterfaceCode::UPDATE_PROCESS_MEMORY_STATE):
|
||||
return HandleUpdateProcessMemoryState(data, reply);
|
||||
}
|
||||
return INVALID_FD;
|
||||
}
|
||||
@@ -1810,5 +1813,28 @@ int32_t AppMgrStub::HandleHasAppRecord(MessageParcel &data, MessageParcel &reply
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ErrCode AppMgrStub::HandleUpdateProcessMemoryState(MessageParcel &data, MessageParcel &reply)
|
||||
{
|
||||
HITRACE_METER(HITRACE_TAG_APP);
|
||||
TAG_LOGD(AAFwkTag::APPMGR, "HandleUpdateProcessMemoryState,callingPid=%{public}d", IPCSkeleton::GetCallingPid());
|
||||
std::vector<ProcessMemoryState> states;
|
||||
auto size = data.ReadUint32();
|
||||
if (size <= 0 || size > MAX_PROCESS_STATE_COUNT) {
|
||||
TAG_LOGE(AAFwkTag::APPMGR, "Invalid size");
|
||||
return ERR_INVALID_VALUE;
|
||||
}
|
||||
for (uint32_t i = 0; i < size; i++) {
|
||||
std::unique_ptr<ProcessMemoryState> tmpState(data.ReadParcelable<ProcessMemoryState>());
|
||||
if (tmpState == nullptr) {
|
||||
TAG_LOGE(AAFwkTag::APPMGR, "tmpState is nullptr.");
|
||||
return ERR_INVALID_VALUE;
|
||||
}
|
||||
states.emplace_back(*tmpState);
|
||||
}
|
||||
auto ret = UpdateProcessMemoryState(states);
|
||||
reply.WriteInt32(ret);
|
||||
return NO_ERROR;
|
||||
}
|
||||
} // namespace AppExecFwk
|
||||
} // namespace OHOS
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 2025 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 "process_memory_state.h"
|
||||
|
||||
#include "hilog_tag_wrapper.h"
|
||||
#include "parcel_macro_base.h"
|
||||
|
||||
namespace OHOS {
|
||||
namespace AppExecFwk {
|
||||
|
||||
bool ProcessMemoryState::ReadFromParcel(Parcel &parcel)
|
||||
{
|
||||
int32_t pidData;
|
||||
READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, pidData);
|
||||
pid = static_cast<int32_t>(pidData);
|
||||
int32_t rssValueData;
|
||||
READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, rssValueData);
|
||||
rssValue = static_cast<int32_t>(rssValueData);
|
||||
int32_t pssValueData;
|
||||
READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, pssValueData);
|
||||
pssValue = static_cast<int32_t>(pssValueData);
|
||||
return true;
|
||||
}
|
||||
|
||||
ProcessMemoryState *ProcessMemoryState::Unmarshalling(Parcel &parcel)
|
||||
{
|
||||
ProcessMemoryState *state = new (std::nothrow) ProcessMemoryState();
|
||||
if (state && !state->ReadFromParcel(parcel)) {
|
||||
TAG_LOGW(AAFwkTag::APPMGR, "read from parcel failed");
|
||||
delete state;
|
||||
state = nullptr;
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
bool ProcessMemoryState::Marshalling(Parcel &parcel) const
|
||||
{
|
||||
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(pid));
|
||||
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(rssValue));
|
||||
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(pssValue));
|
||||
return true;
|
||||
}
|
||||
} // namespace AppExecFwk
|
||||
} // namespace OHOS
|
||||
@@ -65,6 +65,12 @@ bool RunningProcessInfo::ReadFromParcel(Parcel &parcel)
|
||||
int32_t appModeType;
|
||||
READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appModeType);
|
||||
appMode = static_cast<AppExecFwk::MultiAppModeType>(appModeType);
|
||||
int32_t rssData;
|
||||
READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, rssData);
|
||||
rssValue = static_cast<int32_t>(rssData);
|
||||
int32_t pssData;
|
||||
READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, pssData);
|
||||
pssValue = static_cast<int32_t>(pssData);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -101,6 +107,8 @@ bool RunningProcessInfo::Marshalling(Parcel &parcel) const
|
||||
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appCloneIndex);
|
||||
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(instanceKey));
|
||||
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(appMode));
|
||||
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(rssValue));
|
||||
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(pssValue));
|
||||
return true;
|
||||
}
|
||||
} // namespace AppExecFwk
|
||||
|
||||
@@ -1199,6 +1199,14 @@ public:
|
||||
|
||||
virtual int32_t KillProcessWithPrepareTerminate(const std::vector<int32_t>& pids) override;
|
||||
|
||||
/**
|
||||
* @brief the process with reason
|
||||
* @param pid id of process.
|
||||
* @param reason, kill process reason.
|
||||
* @return Returns ERR_OK on success, others on failure.
|
||||
*/
|
||||
virtual int32_t KillProcessWithReason(int32_t pid, const ExitReason &reason) override;
|
||||
|
||||
/**
|
||||
* @brief Register auto start up callback for system api.
|
||||
* @param callback The point of JsAbilityAutoStartupCallBack.
|
||||
|
||||
@@ -1546,6 +1546,8 @@ public:
|
||||
|
||||
virtual int32_t KillProcessWithPrepareTerminate(const std::vector<int32_t>& pids) override;
|
||||
|
||||
virtual int32_t KillProcessWithReason(int32_t pid, const ExitReason &reason) override;
|
||||
|
||||
/**
|
||||
* @brief Register auto start up callback for system api.
|
||||
* @param callback The point of JsAbilityAutoStartupCallBack.
|
||||
|
||||
@@ -262,6 +262,8 @@ private:
|
||||
int32_t RegisterStatusBarDelegateInner(MessageParcel &data, MessageParcel &reply);
|
||||
int32_t KillProcessWithPrepareTerminateInner(MessageParcel &data, MessageParcel &reply);
|
||||
|
||||
int32_t KillProcessWithReasonInner(MessageParcel &data, MessageParcel &reply);
|
||||
|
||||
int32_t RegisterAutoStartupSystemCallbackInner(MessageParcel &data, MessageParcel &reply);
|
||||
int32_t UnregisterAutoStartupSystemCallbackInner(MessageParcel &data, MessageParcel &reply);
|
||||
int32_t SetApplicationAutoStartupInner(MessageParcel &data, MessageParcel &reply);
|
||||
|
||||
@@ -63,6 +63,9 @@ public:
|
||||
|
||||
bool GetUIExtensionAbilityExitReason(const std::string &keyEx, AAFwk::ExitReason &exitReason);
|
||||
|
||||
int32_t SetProcessExitDetailInfo(const AAFwk::ExitReason &exitReason,
|
||||
const AppExecFwk::RunningProcessInfo &processInfo);
|
||||
|
||||
private:
|
||||
DistributedKv::Status GetKvStore();
|
||||
bool CheckKvStore();
|
||||
@@ -71,6 +74,8 @@ private:
|
||||
void ConvertAppExitReasonInfoFromValue(const DistributedKv::Value &value, AAFwk::ExitReason &exitReason,
|
||||
int64_t &time_stamp, std::vector<std::string> &abilityList);
|
||||
void ConvertAccessTokenIdFromValue(const DistributedKv::Value &value, uint32_t &accessTokenId);
|
||||
DistributedKv::Value ConvertProcessExitDetailInfoToValue(const AAFwk::ExitReason &exitReason,
|
||||
const AppExecFwk::RunningProcessInfo &processInfo);
|
||||
void UpdateAppExitReason(uint32_t accessTokenId, const std::vector<std::string> &abilityList,
|
||||
const AAFwk::ExitReason &exitReason);
|
||||
void InnerDeleteAppExitReason(const std::string &keyName);
|
||||
@@ -90,6 +95,7 @@ private:
|
||||
|
||||
DistributedKv::Key GetSessionIdKey(const int32_t sessionId);
|
||||
DistributedKv::Value ConvertAccessTokenIdToValue(uint32_t accessTokenId);
|
||||
DistributedKv::Status RestoreKvStore(DistributedKv::Status status);
|
||||
|
||||
const DistributedKv::AppId appId_ { "app_exit_reason_storage" };
|
||||
const DistributedKv::StoreId storeId_ { "app_exit_reason_infos" };
|
||||
|
||||
@@ -31,6 +31,7 @@ public:
|
||||
~AppExitReasonHelper() = default;
|
||||
|
||||
int32_t RecordAppExitReason(const ExitReason &exitReason);
|
||||
int32_t RecordAppExitReason(const ExitReason &exitReason, int32_t pid);
|
||||
int32_t RecordAppExitReason(const std::string &bundleName, int32_t uid, int32_t appIndex,
|
||||
const ExitReason &exitReason);
|
||||
int32_t RecordProcessExtensionExitReason(
|
||||
|
||||
@@ -1748,6 +1748,14 @@ ErrCode AbilityManagerClient::KillProcessWithPrepareTerminate(const std::vector<
|
||||
return abms->KillProcessWithPrepareTerminate(pids);
|
||||
}
|
||||
|
||||
ErrCode AbilityManagerClient::KillProcessWithReason(int32_t pid, const ExitReason &reason)
|
||||
{
|
||||
TAG_LOGI(AAFwkTag::ABILITYMGR, "call, pid:%{public}d", pid);
|
||||
auto abms = GetAbilityManager();
|
||||
CHECK_POINTER_RETURN_NOT_CONNECTED(abms);
|
||||
return abms->KillProcessWithReason(pid, reason);
|
||||
}
|
||||
|
||||
ErrCode AbilityManagerClient::RegisterAutoStartupSystemCallback(sptr<IRemoteObject> callback)
|
||||
{
|
||||
TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
|
||||
|
||||
@@ -4669,6 +4669,32 @@ int32_t AbilityManagerProxy::KillProcessWithPrepareTerminate(const std::vector<i
|
||||
return ret;
|
||||
}
|
||||
|
||||
int32_t AbilityManagerProxy::KillProcessWithReason(int32_t pid, const ExitReason &reason)
|
||||
{
|
||||
MessageParcel data;
|
||||
MessageParcel reply;
|
||||
MessageOption option;
|
||||
if (!WriteInterfaceToken(data)) {
|
||||
TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
|
||||
return ERR_NATIVE_IPC_PARCEL_FAILED;
|
||||
}
|
||||
if (!data.WriteInt32(pid)) {
|
||||
TAG_LOGE(AAFwkTag::APPMGR, "parcel pid failed");
|
||||
return ERR_NATIVE_IPC_PARCEL_FAILED;
|
||||
}
|
||||
if (!data.WriteParcelable(&reason)) {
|
||||
TAG_LOGE(AAFwkTag::APPMGR, "Write reason failed");
|
||||
return ERR_NATIVE_IPC_PARCEL_FAILED;
|
||||
}
|
||||
int32_t ret =
|
||||
SendRequest(AbilityManagerInterfaceCode::KILL_PROCESS_WITH_REASON, data, reply, option);
|
||||
if (ret != NO_ERROR) {
|
||||
TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
|
||||
return ret;
|
||||
}
|
||||
return reply.ReadInt32();
|
||||
}
|
||||
|
||||
int32_t AbilityManagerProxy::RegisterAutoStartupSystemCallback(const sptr<IRemoteObject> &callback)
|
||||
{
|
||||
MessageParcel data;
|
||||
|
||||
@@ -10742,6 +10742,29 @@ int32_t AbilityManagerService::KillProcessWithPrepareTerminate(const std::vector
|
||||
return uiAbilityManager->TryPrepareTerminateByPids(pids);
|
||||
}
|
||||
|
||||
int32_t AbilityManagerService::KillProcessWithReason(int32_t pid, const ExitReason &reason)
|
||||
{
|
||||
auto isShellCall = PermissionVerification::GetInstance()->IsShellCall();
|
||||
auto isCallingPerm = PermissionVerification::GetInstance()->VerifyCallingPermission(
|
||||
AAFwk::PermissionConstants::PERMISSION_KILL_APP_PROCESSES);
|
||||
if (!isCallingPerm && !isShellCall) {
|
||||
TAG_LOGE(AAFwkTag::APPMGR, "permission verification fail");
|
||||
return ERR_PERMISSION_DENIED;
|
||||
}
|
||||
|
||||
TAG_LOGI(AAFwkTag::ABILITYMGR, "pid:%{public}d, reason:%{public}d, subReason:%{public}d, killMsg:%{public}s",
|
||||
pid, reason.reason, reason.subReason, reason.exitMsg.c_str());
|
||||
CHECK_POINTER_AND_RETURN(appExitReasonHelper_, ERR_NULL_OBJECT);
|
||||
auto ret = appExitReasonHelper_->RecordAppExitReason(reason, pid);
|
||||
if (ret != ERR_OK) {
|
||||
TAG_LOGE(AAFwkTag::ABILITYMGR, "RecordAppExitReason failed, ret:%{public}d", ret);
|
||||
return ret;
|
||||
}
|
||||
std::vector<int32_t> pidToBeKilled = { pid };
|
||||
IN_PROCESS_CALL_WITHOUT_RET(DelayedSingleton<AppScheduler>::GetInstance()->KillProcessesByPids(pidToBeKilled));
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
int32_t AbilityManagerService::RegisterAutoStartupSystemCallback(const sptr<IRemoteObject> &callback)
|
||||
{
|
||||
if (abilityAutoStartupService_ == nullptr) {
|
||||
|
||||
@@ -666,6 +666,9 @@ int AbilityManagerStub::OnRemoteRequestInnerSeventeenth(uint32_t code, MessagePa
|
||||
if (interfaceCode == AbilityManagerInterfaceCode::KILL_PROCESS_WITH_PREPARE_TERMINATE) {
|
||||
return KillProcessWithPrepareTerminateInner(data, reply);
|
||||
}
|
||||
if (interfaceCode == AbilityManagerInterfaceCode::KILL_PROCESS_WITH_REASON) {
|
||||
return KillProcessWithReasonInner(data, reply);
|
||||
}
|
||||
if (interfaceCode == AbilityManagerInterfaceCode::REGISTER_AUTO_STARTUP_SYSTEM_CALLBACK) {
|
||||
return RegisterAutoStartupSystemCallbackInner(data, reply);
|
||||
}
|
||||
@@ -3462,6 +3465,22 @@ int32_t AbilityManagerStub::KillProcessWithPrepareTerminateInner(MessageParcel &
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int32_t AbilityManagerStub::KillProcessWithReasonInner(MessageParcel &data, MessageParcel &reply)
|
||||
{
|
||||
int32_t pid = data.ReadInt32();
|
||||
std::unique_ptr<ExitReason> reason(data.ReadParcelable<ExitReason>());
|
||||
if (reason == nullptr) {
|
||||
TAG_LOGE(AAFwkTag::APPMGR, "reason null");
|
||||
return ERR_INVALID_VALUE;
|
||||
}
|
||||
int32_t result = KillProcessWithReason(pid, *reason);
|
||||
if (!reply.WriteInt32(result)) {
|
||||
TAG_LOGE(AAFwkTag::ABILITYMGR, "write result fail");
|
||||
return ERR_INVALID_VALUE;
|
||||
}
|
||||
return NO_ERROR;
|
||||
}
|
||||
|
||||
int32_t AbilityManagerStub::RegisterAutoStartupSystemCallbackInner(MessageParcel &data, MessageParcel &reply)
|
||||
{
|
||||
sptr<IRemoteObject> callback = data.ReadRemoteObject();
|
||||
|
||||
@@ -36,6 +36,16 @@ const std::string JSON_KEY_SESSION_ID_LIST = "session_id_list";
|
||||
const std::string JSON_KEY_EXTENSION_NAME = "extension_name";
|
||||
const std::string JSON_KEY_ACCESSTOKENId = "access_token_id";
|
||||
const std::string SEPARATOR = ":";
|
||||
const std::string JSON_KEY_PROCESS_EXIT_DETAIL_INFO_LIST = "process_exit_detail_info_list";
|
||||
const std::string KEY_KILL_PROCESS_REASON_PREFIX = "process_exit_detail_info";
|
||||
const std::string JSON_KEY_KILL_REASON = "kill_reason";
|
||||
const std::string JSON_KEY_SUB_KILL_REASON = "sub_kill_reason";
|
||||
const std::string JSON_KEY_KILL_MSG = "kill_msg";
|
||||
const std::string JSON_KEY_PID = "pid";
|
||||
const std::string JSON_KEY_UID = "uid";
|
||||
const std::string JSON_KEY_PROCESS_NAME = "process_name";
|
||||
const std::string JSON_KEY_PSS_VALUE = "pss_value";
|
||||
const std::string JSON_KEY_RSS_VALUE = "rss_value";
|
||||
} // namespace
|
||||
AppExitReasonDataManager::AppExitReasonDataManager() {}
|
||||
|
||||
@@ -816,5 +826,82 @@ DistributedKv::Value AppExitReasonDataManager::ConvertAppExitReasonInfoToValueOf
|
||||
DistributedKv::Value value(jsonObject.dump());
|
||||
return value;
|
||||
}
|
||||
|
||||
DistributedKv::Value AppExitReasonDataManager::ConvertProcessExitDetailInfoToValue(
|
||||
const AAFwk::ExitReason &exitReason, const AppExecFwk::RunningProcessInfo &processInfo)
|
||||
{
|
||||
std::chrono::milliseconds nowMs =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
|
||||
nlohmann::json jsonObject = nlohmann::json {
|
||||
{ JSON_KEY_PID, processInfo.pid_ },
|
||||
{ JSON_KEY_UID, processInfo.uid_ },
|
||||
{ JSON_KEY_KILL_REASON, exitReason.reason },
|
||||
{ JSON_KEY_SUB_KILL_REASON, exitReason.subReason },
|
||||
{ JSON_KEY_KILL_MSG, exitReason.exitMsg },
|
||||
{ JSON_KEY_RSS_VALUE, processInfo.rssValue },
|
||||
{ JSON_KEY_PSS_VALUE, processInfo.pssValue },
|
||||
{ JSON_KEY_PROCESS_NAME, processInfo.processName_ },
|
||||
{ JSON_KEY_TIME_STAMP, nowMs.count() },
|
||||
};
|
||||
|
||||
DistributedKv::Value value(jsonObject.dump());
|
||||
TAG_LOGI(AAFwkTag::ABILITYMGR, "value: %{public}s", value.ToString().c_str());
|
||||
return value;
|
||||
}
|
||||
|
||||
DistributedKv::Status AppExitReasonDataManager::RestoreKvStore(DistributedKv::Status status)
|
||||
{
|
||||
if (status != DistributedKv::Status::DATA_CORRUPTED) {
|
||||
return status;
|
||||
}
|
||||
DistributedKv::Options options = {
|
||||
.createIfMissing = true,
|
||||
.encrypt = false,
|
||||
.autoSync = true,
|
||||
.syncable = false,
|
||||
.securityLevel = DistributedKv::SecurityLevel::S2,
|
||||
.area = DistributedKv::EL1,
|
||||
.kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION,
|
||||
.baseDir = APP_EXIT_REASON_STORAGE_DIR,
|
||||
};
|
||||
TAG_LOGI(AAFwkTag::ABILITYMGR, "corrupted, deleting db");
|
||||
dataManager_.DeleteKvStore(appId_, storeId_, options.baseDir);
|
||||
TAG_LOGI(AAFwkTag::ABILITYMGR, "deleted corrupted db, recreating db");
|
||||
status = dataManager_.GetSingleKvStore(options, appId_, storeId_, kvStorePtr_);
|
||||
TAG_LOGI(AAFwkTag::ABILITYMGR, "recreate db result:%{public}d", status);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
int32_t AppExitReasonDataManager::SetProcessExitDetailInfo(const AAFwk::ExitReason &exitReason,
|
||||
const AppExecFwk::RunningProcessInfo &processInfo)
|
||||
{
|
||||
TAG_LOGI(AAFwkTag::ABILITYMGR,
|
||||
"SetProcessExitDetailInfo processName %{public}s pid %{public}d reason %{public}d",
|
||||
processInfo.processName_.c_str(), processInfo.pid_, exitReason.reason);
|
||||
std::string keyStr = KEY_KILL_PROCESS_REASON_PREFIX + SEPARATOR + processInfo.processName_ +
|
||||
SEPARATOR + std::to_string(processInfo.pid_);
|
||||
DistributedKv::Key key(keyStr);
|
||||
DistributedKv::Value value = ConvertProcessExitDetailInfoToValue(exitReason, processInfo);
|
||||
DistributedKv::Status status = DistributedKv::SUCCESS;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
|
||||
if (!CheckKvStore()) {
|
||||
TAG_LOGE(AAFwkTag::ABILITYMGR, "null kvStore");
|
||||
return ERR_NO_INIT;
|
||||
}
|
||||
status = kvStorePtr_->Put(key, value);
|
||||
}
|
||||
if (status != DistributedKv::Status::SUCCESS) {
|
||||
TAG_LOGE(AAFwkTag::ABILITYMGR, "error : %{public}d", status);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
|
||||
status = RestoreKvStore(status);
|
||||
}
|
||||
return ERR_INVALID_OPERATION;
|
||||
}
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
} // namespace AbilityRuntime
|
||||
} // namespace OHOS
|
||||
|
||||
@@ -80,6 +80,14 @@ int32_t AppExitReasonHelper::RecordAppExitReason(const ExitReason &exitReason)
|
||||
accessTokenId, abilityList, exitReason);
|
||||
}
|
||||
|
||||
int32_t AppExitReasonHelper::RecordAppExitReason(const ExitReason &exitReason, int32_t pid)
|
||||
{
|
||||
AppExecFwk::RunningProcessInfo processInfo;
|
||||
DelayedSingleton<AppScheduler>::GetInstance()->GetRunningProcessInfoByPid(static_cast<pid_t>(pid), processInfo);
|
||||
return DelayedSingleton<AbilityRuntime::AppExitReasonDataManager>::GetInstance()->
|
||||
SetProcessExitDetailInfo(exitReason, processInfo);
|
||||
}
|
||||
|
||||
int32_t AppExitReasonHelper::RecordProcessExitReason(const int32_t pid, const ExitReason &exitReason)
|
||||
{
|
||||
AppExecFwk::ApplicationInfo application;
|
||||
|
||||
@@ -27,12 +27,20 @@ ExitReason::ExitReason(const Reason reason, const std::string &exitMsg)
|
||||
this->exitMsg = exitMsg;
|
||||
}
|
||||
|
||||
ExitReason::ExitReason(const Reason &reason, int32_t subReason, const std::string &exitMsg)
|
||||
{
|
||||
this->reason = reason;
|
||||
this->subReason = subReason;
|
||||
this->exitMsg = exitMsg;
|
||||
}
|
||||
|
||||
bool ExitReason::ReadFromParcel(Parcel &parcel)
|
||||
{
|
||||
int32_t reasonData;
|
||||
READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, reasonData);
|
||||
reason = static_cast<Reason>(reasonData);
|
||||
|
||||
READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, reasonData);
|
||||
subReason = reasonData;
|
||||
exitMsg = Str16ToStr8(parcel.ReadString16());
|
||||
return true;
|
||||
}
|
||||
@@ -55,6 +63,7 @@ ExitReason *ExitReason::Unmarshalling(Parcel &parcel)
|
||||
bool ExitReason::Marshalling(Parcel &parcel) const
|
||||
{
|
||||
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, static_cast<int32_t>(reason));
|
||||
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, subReason);
|
||||
WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(exitMsg));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -805,6 +805,14 @@ public:
|
||||
virtual void UpdateInstanceKeyBySpecifiedId(int32_t specifiedId, std::string &instanceKey) override;
|
||||
|
||||
int32_t HasAppRecord(const AAFwk::Want &want, const AbilityInfo &abilityInfo, bool &result) override;
|
||||
|
||||
/**
|
||||
* update process rss and pss value.
|
||||
*
|
||||
* @param procMemStates, the memory states of all apps.
|
||||
* @return ERR_OK, return back success, others fail.
|
||||
*/
|
||||
virtual int32_t UpdateProcessMemoryState(const std::vector<ProcessMemoryState> &procMemState) override;
|
||||
private:
|
||||
/**
|
||||
* Init, Initialize application services.
|
||||
|
||||
@@ -63,6 +63,7 @@
|
||||
#include "istart_specified_ability_response.h"
|
||||
#include "kia_interceptor_interface.h"
|
||||
#include "kill_process_config.h"
|
||||
#include "process_memory_state.h"
|
||||
#include "record_query_result.h"
|
||||
#include "refbase.h"
|
||||
#include "remote_client_manager.h"
|
||||
@@ -344,6 +345,14 @@ public:
|
||||
virtual int32_t KillApplicationByUserId(const std::string &bundleName, int32_t appCloneIndex, int userId,
|
||||
const bool clearPageStack = false, const std::string& reason = "KillApplicationByUserId");
|
||||
|
||||
/**
|
||||
* update process rss and pss value.
|
||||
*
|
||||
* @param procMemStates, the memory states of all apps.
|
||||
* @return ERR_OK, return back success, others fail.
|
||||
*/
|
||||
virtual int32_t UpdateProcessMemoryState(const std::vector<ProcessMemoryState> &procMemState);
|
||||
|
||||
/**
|
||||
* ClearUpApplicationData, clear the application data.
|
||||
*
|
||||
|
||||
@@ -1016,6 +1016,27 @@ public:
|
||||
bool IsUnSetPermission();
|
||||
|
||||
void UnSetPolicy();
|
||||
|
||||
inline void SetRssValue(int32_t rssValue)
|
||||
{
|
||||
rssValue_ = rssValue;
|
||||
}
|
||||
|
||||
inline int32_t GetRssValue() const
|
||||
{
|
||||
return rssValue_;
|
||||
}
|
||||
|
||||
inline void SetPssValue(int32_t pssValue)
|
||||
{
|
||||
pssValue_ = pssValue;
|
||||
}
|
||||
|
||||
inline int32_t GetPssValue() const
|
||||
{
|
||||
return pssValue_;
|
||||
}
|
||||
|
||||
private:
|
||||
/**
|
||||
* SearchTheModuleInfoNeedToUpdated, Get an uninitialized abilityStage data.
|
||||
@@ -1188,6 +1209,8 @@ private:
|
||||
bool isAllowedNWebPreload_ = false;
|
||||
bool isUnSetPermission_ = false;
|
||||
std::string killReason_ = "";
|
||||
int32_t rssValue_ = 0;
|
||||
int32_t pssValue_ = 0;
|
||||
};
|
||||
|
||||
} // namespace AppExecFwk
|
||||
|
||||
@@ -75,6 +75,7 @@ constexpr const char* TASK_EXIT_CHILD_PROCESS_SAFELY = "ExitChildProcessSafelyTa
|
||||
#endif // SUPPORT_CHILD_PROCESS
|
||||
constexpr const char* FOUNDATION_PROCESS = "foundation";
|
||||
constexpr int32_t USER_UID = 2000;
|
||||
constexpr const char* HIVIEW_PROCESS_NAME = "hiview";
|
||||
} // namespace
|
||||
|
||||
REGISTER_SYSTEM_ABILITY_BY_ID(AppMgrService, APP_MGR_SERVICE_ID, true);
|
||||
@@ -1776,5 +1777,27 @@ int32_t AppMgrService::HasAppRecord(const AAFwk::Want &want, const AbilityInfo &
|
||||
result = appMgrServiceInner_->HasAppRecord(want, abilityInfo);
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
int32_t AppMgrService::UpdateProcessMemoryState(const std::vector<ProcessMemoryState> &procMemState)
|
||||
{
|
||||
TAG_LOGI(AAFwkTag::APPMGR, "procMemState.size = %{public}zu", procMemState.size());
|
||||
if (!IsReady()) {
|
||||
return ERR_INVALID_OPERATION;
|
||||
}
|
||||
if (!AAFwk::PermissionVerification::GetInstance()->IsSACall() &&
|
||||
!AAFwk::PermissionVerification::GetInstance()->IsShellCall()) {
|
||||
TAG_LOGE(AAFwkTag::APPMGR, "caller is not sa or shell");
|
||||
return ERR_PERMISSION_DENIED;
|
||||
}
|
||||
if (!AAFwk::PermissionVerification::GetInstance()->CheckSpecificSystemAbilityAccessPermission(
|
||||
HIVIEW_PROCESS_NAME)) {
|
||||
TAG_LOGW(AAFwkTag::APPMGR, "caller not sa dfx");
|
||||
}
|
||||
if (!appMgrServiceInner_) {
|
||||
TAG_LOGE(AAFwkTag::APPMGR, "appMgrServiceInner_ is nullptr");
|
||||
return ERR_INVALID_VALUE;
|
||||
}
|
||||
return appMgrServiceInner_->UpdateProcessMemoryState(procMemState);
|
||||
}
|
||||
} // namespace AppExecFwk
|
||||
} // namespace OHOS
|
||||
|
||||
@@ -1668,6 +1668,20 @@ int32_t AppMgrServiceInner::KillApplicationByUid(const std::string &bundleName,
|
||||
return result;
|
||||
}
|
||||
|
||||
int32_t AppMgrServiceInner::UpdateProcessMemoryState(const std::vector<ProcessMemoryState> &procMemState)
|
||||
{
|
||||
for (const auto &state : procMemState) {
|
||||
auto appRecord = GetAppRunningRecordByPid(state.pid);
|
||||
if (!appRecord) {
|
||||
TAG_LOGW(AAFwkTag::APPMGR, "no appRecord, pid:%{public}d", state.pid);
|
||||
continue;
|
||||
}
|
||||
appRecord->SetRssValue(state.rssValue);
|
||||
appRecord->SetPssValue(state.pssValue);
|
||||
}
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
void AppMgrServiceInner::SendProcessExitEventTask(
|
||||
const std::shared_ptr<AppRunningRecord> &appRecord, time_t exitTime, int32_t count)
|
||||
{
|
||||
|
||||
@@ -878,6 +878,8 @@ int32_t AppRunningManager::AssignRunningProcessInfoByAppRecord(
|
||||
}
|
||||
info.appCloneIndex = appRecord->GetAppIndex();
|
||||
info.instanceKey = appRecord->GetInstanceKey();
|
||||
info.rssValue = appRecord->GetRssValue();
|
||||
info.pssValue = appRecord->GetPssValue();
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user