!15790 Message:get app_running_id value optimization

Merge pull request !15790 from 郑冬冬冬/master
This commit is contained in:
openharmony_ci
2025-08-01 04:03:54 +00:00
committed by Gitee
10 changed files with 153 additions and 25 deletions
@@ -89,6 +89,17 @@ bool AppfreezeInner::IsHandleAppfreeze()
return !isAppDebug_;
}
std::string AppfreezeInner::GetProcStatm(int32_t pid)
{
std::string procStatm;
std::ifstream statmStream("/proc/" + std::to_string(pid) + "/statm");
if (statmStream) {
std::getline(statmStream, procStatm);
statmStream.close();
}
return procStatm;
}
void AppfreezeInner::GetMainHandlerDump(std::string& msgContent)
{
msgContent = "\nMain handler dump start time: " + AbilityRuntime::TimeUtil::DefaultCurrentTimeStr() + "\n";
@@ -143,6 +154,7 @@ void AppfreezeInner::AppfreezeHandleOverReportCount(bool isSixSecondEvent)
faultData.timeoutMarkers = "";
if (isSixSecondEvent) {
faultData.errorObject.name = AppFreezeType::THREAD_BLOCK_6S;
faultData.procStatm = GetProcStatm(static_cast<int32_t>(getpid()));
} else {
faultData.errorObject.name = AppFreezeType::THREAD_BLOCK_3S;
}
@@ -224,6 +236,8 @@ int AppfreezeInner::AcquireStack(const FaultData& info, bool onlyMainThread)
faultData.eventId = it->eventId;
faultData.needKillProcess = it->needKillProcess;
faultData.appfreezeInfo = it->appfreezeInfo;
faultData.appRunningUniqueId = it->appRunningUniqueId;
faultData.procStatm = it->procStatm;
ChangeFaultDateInfo(faultData, msgContent);
}
return 0;
@@ -244,6 +258,7 @@ void AppfreezeInner::ThreadBlock(std::atomic_bool& isSixSecondEvent)
#ifdef APP_NO_RESPONSE_DIALOG
isSixSecondEvent.store(false);
#endif
faultData.procStatm = GetProcStatm(static_cast<int32_t>(getpid()));
} else {
faultData.errorObject.name = AppFreezeType::THREAD_BLOCK_3S;
isSixSecondEvent.store(true);
@@ -59,8 +59,9 @@ public:
*/
struct FaultData : public Parcelable {
bool ReadFromParcel(Parcel &parcel);
bool WriteContent(Parcel &parcel) const;
bool ReadContent(Parcel &parcel);
virtual bool Marshalling(Parcel &parcel) const override;
bool WriteContent(Parcel &parcel) const;
static FaultData *Unmarshalling(Parcel &parcel);
// error object
ErrorObject errorObject;
@@ -76,6 +77,8 @@ struct FaultData : public Parcelable {
uint32_t stuckTimeout = 0;
sptr<IRemoteObject> token = nullptr;
std::string appfreezeInfo;
std::string appRunningUniqueId;
std::string procStatm;
};
/**
@@ -84,14 +87,15 @@ struct FaultData : public Parcelable {
*/
struct AppFaultDataBySA : public Parcelable {
bool ReadFromParcel(Parcel &parcel);
bool ReadContent(Parcel &parcel);
virtual bool Marshalling(Parcel &parcel) const override;
bool WriteErrorObject(Parcel &parcel) const;
bool WriteContent(Parcel &parcel) const;
static AppFaultDataBySA *Unmarshalling(Parcel &parcel);
bool waitSaveState = false;
bool notifyApp = false;
bool forceExit = false;
bool needKillProcess = true;
bool WriteErrorObject(Parcel &parcel) const;
virtual bool Marshalling(Parcel &parcel) const override;
static AppFaultDataBySA *Unmarshalling(Parcel &parcel);
// error object
ErrorObject errorObject;
FaultDataType faultType = FaultDataType::UNKNOWN;
@@ -101,6 +105,8 @@ struct AppFaultDataBySA : public Parcelable {
sptr<IRemoteObject> token = nullptr;
std::string timeoutMarkers;
std::string appfreezeInfo;
std::string appRunningUniqueId;
std::string procStatm;
};
} // namespace AppExecFwk
} // namespace OHOS
@@ -71,6 +71,22 @@ bool FaultData::ReadFromParcel(Parcel &parcel)
return false;
}
appfreezeInfo = strValue;
return ReadContent(parcel);
}
bool FaultData::ReadContent(Parcel &parcel)
{
std::string strValue;
if (!parcel.ReadString(strValue)) {
TAG_LOGE(AAFwkTag::APPMGR, "AppRunningUniqueId read string failed.");
return false;
}
appRunningUniqueId = strValue;
if (!parcel.ReadString(strValue)) {
TAG_LOGE(AAFwkTag::APPMGR, "ProcStatm read string failed.");
return false;
}
procStatm = strValue;
return true;
}
@@ -107,6 +123,16 @@ bool FaultData::WriteContent(Parcel &parcel) const
TAG_LOGE(AAFwkTag::APPMGR, "AppfreezeInfo [%{public}s] write string failed.", appfreezeInfo.c_str());
return false;
}
if (!parcel.WriteString(appRunningUniqueId)) {
TAG_LOGE(AAFwkTag::APPMGR, "AppRunningUniqueId [%{public}s] write string failed.", appRunningUniqueId.c_str());
return false;
}
if (!parcel.WriteString(procStatm)) {
TAG_LOGE(AAFwkTag::APPMGR, "ProcStatm [%{public}s] write string failed.", procStatm.c_str());
return false;
}
return true;
}
@@ -230,6 +256,23 @@ bool AppFaultDataBySA::ReadFromParcel(Parcel &parcel)
return false;
}
appfreezeInfo = strValue;
return ReadContent(parcel);
}
bool AppFaultDataBySA::ReadContent(Parcel &parcel)
{
std::string strValue;
if (!parcel.ReadString(strValue)) {
TAG_LOGE(AAFwkTag::APPMGR, "AppRunningUniqueId read string failed.");
return false;
}
appRunningUniqueId = strValue;
if (!parcel.ReadString(strValue)) {
TAG_LOGE(AAFwkTag::APPMGR, "ProcStatm read string failed.");
return false;
}
procStatm = strValue;
return true;
}
@@ -261,6 +304,16 @@ bool AppFaultDataBySA::WriteContent(Parcel &parcel) const
TAG_LOGE(AAFwkTag::APPMGR, "AppfreezeInfo [%{public}s] write string failed.", appfreezeInfo.c_str());
return false;
}
if (!parcel.WriteString(appRunningUniqueId)) {
TAG_LOGE(AAFwkTag::APPMGR, "AppRunningUniqueId [%{public}s] write string failed.", appRunningUniqueId.c_str());
return false;
}
if (!parcel.WriteString(procStatm)) {
TAG_LOGE(AAFwkTag::APPMGR, "ProcStatm [%{public}s] write string failed.", procStatm.c_str());
return false;
}
return true;
}
@@ -54,6 +54,7 @@ private:
int NotifyANR(const FaultData& faultData);
bool IsExitApp(const std::string& name);
bool IsHandleAppfreeze();
std::string GetProcStatm(int32_t pid);
static std::mutex singletonMutex_;
static std::shared_ptr<AppfreezeInner> instance_;
+25 -19
View File
@@ -227,6 +227,8 @@ int AppfreezeManager::AppfreezeHandleWithStack(const FaultData& faultData, const
faultNotifyData.eventId = faultData.eventId;
faultNotifyData.tid = faultData.tid;
faultNotifyData.appfreezeInfo = faultData.appfreezeInfo;
faultNotifyData.appRunningUniqueId = faultData.appRunningUniqueId;
faultNotifyData.procStatm = faultData.procStatm;
HITRACE_METER_FMT(HITRACE_TAG_APP, "AppfreezeHandleWithStack pid:%d-name:%s",
appInfo.pid, faultData.errorObject.name.c_str());
@@ -263,36 +265,40 @@ std::string AppfreezeManager::WriteToFile(const std::string& fileName, std::stri
int AppfreezeManager::LifecycleTimeoutHandle(const ParamInfo& info, FreezeUtil::LifecycleFlow flow)
{
if (info.typeId != AppfreezeManager::TypeAttribute::CRITICAL_TIMEOUT) {
if (info.typeId != AppfreezeManager::TypeAttribute::CRITICAL_TIMEOUT || !IsHandleAppfreeze(info.bundleName)) {
return -1;
}
if (!IsHandleAppfreeze(info.bundleName)) {
if (info.eventName != AppFreezeType::LIFECYCLE_TIMEOUT && info.eventName != AppFreezeType::LIFECYCLE_HALF_TIMEOUT
&& info.eventName != AppFreezeType::LIFECYCLE_TIMEOUT_WARNING
&& info.eventName != AppFreezeType::LIFECYCLE_HALF_TIMEOUT_WARNING) {
return -1;
}
if (info.eventName != AppFreezeType::LIFECYCLE_TIMEOUT &&
info.eventName != AppFreezeType::LIFECYCLE_TIMEOUT_WARNING &&
info.eventName != AppFreezeType::LIFECYCLE_HALF_TIMEOUT &&
info.eventName != AppFreezeType::LIFECYCLE_HALF_TIMEOUT_WARNING) {
return -1;
}
TAG_LOGD(AAFwkTag::APPDFR, "called %{public}s, name_ %{public}s", info.bundleName.c_str(),
name_.c_str());
TAG_LOGD(AAFwkTag::APPDFR, "called %{public}s, name_ %{public}s", info.bundleName.c_str(), name_.c_str());
HITRACE_METER_FMT(HITRACE_TAG_APP, "LifecycleTimeoutHandle:%{public}s bundleName:%{public}s",
info.eventName.c_str(), info.bundleName.c_str());
AppFaultDataBySA faultDataSA;
if (info.eventName.find("HALF") == std::string::npos) {
std::ifstream statmStream("/proc/" + std::to_string(info.pid) + "/statm");
if (statmStream) {
std::string procStatm;
std::getline(statmStream, procStatm);
statmStream.close();
faultDataSA.procStatm = procStatm;
}
}
faultDataSA.errorObject.name = info.eventName;
faultDataSA.errorObject.message = info.msg;
faultDataSA.errorObject.stack = "\nDump tid stack start time:" +
AbilityRuntime::TimeUtil::DefaultCurrentTimeStr() + "\n";
std::string stack = "";
std::string stack;
if (!HiviewDFX::GetBacktraceStringByTidWithMix(stack, info.pid, 0, true)) {
stack = "Failed to dump stacktrace for " + stack;
}
faultDataSA.errorObject.stack += stack + "\nDump tid stack end time:" +
AbilityRuntime::TimeUtil::DefaultCurrentTimeStr() + "\n";
faultDataSA.faultType = FaultDataType::APP_FREEZE;
faultDataSA.timeoutMarkers = "notifyFault" +
std::to_string(info.pid) +
faultDataSA.timeoutMarkers = "notifyFault" + std::to_string(info.pid) +
"-" + std::to_string(AbilityRuntime::TimeUtil::CurrentTimeMillis());
faultDataSA.pid = info.pid;
faultDataSA.needKillProcess = info.needKillProcess;
@@ -314,6 +320,8 @@ FaultData AppfreezeManager::GetFaultNotifyData(const FaultData& faultData, int p
faultNotifyData.eventId = faultData.eventId;
faultNotifyData.tid = (faultData.errorObject.name == AppFreezeType::APP_INPUT_BLOCK) ? pid : faultData.tid;
faultNotifyData.appfreezeInfo = faultData.appfreezeInfo;
faultNotifyData.appRunningUniqueId = faultData.appRunningUniqueId;
faultNotifyData.procStatm = faultData.procStatm;
return faultNotifyData;
}
@@ -419,9 +427,7 @@ std::string AppfreezeManager::ReportAppfreezeCpuInfo(const FaultData& faultData,
int AppfreezeManager::NotifyANR(const FaultData& faultData, const AppfreezeManager::AppInfo& appInfo,
const std::string& binderInfo, const std::string& memoryContent)
{
std::string appRunningUniqueId = "";
DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance()->GetAppRunningUniqueIdByPid(appInfo.pid,
appRunningUniqueId);
std::string appRunningUniqueId = faultData.appRunningUniqueId;
int ret = 0;
this->PerfStart(faultData.errorObject.name);
int64_t startTime = AbilityRuntime::TimeUtil::CurrentTimeMillis();
@@ -431,7 +437,7 @@ int AppfreezeManager::NotifyANR(const FaultData& faultData, const AppfreezeManag
EVENT_PACKAGE_NAME, appInfo.bundleName, EVENT_PROCESS_NAME, appInfo.processName, EVENT_MESSAGE,
faultData.errorObject.message, EVENT_STACK, faultData.errorObject.stack, BINDER_INFO, binderInfo,
APP_RUNNING_UNIQUE_ID, appRunningUniqueId, EVENT_INPUT_ID, faultData.eventId,
FREEZE_MEMORY, memoryContent);
FREEZE_MEMORY, memoryContent + "\n" + faultData.procStatm);
} else if (faultData.errorObject.name == AppFreezeType::THREAD_BLOCK_6S) {
HitraceInfo info;
bool hitraceIsValid = GetHitraceId(info);
@@ -440,7 +446,7 @@ int AppfreezeManager::NotifyANR(const FaultData& faultData, const AppfreezeManag
EVENT_TID, faultData.tid,
EVENT_PACKAGE_NAME, appInfo.bundleName, EVENT_PROCESS_NAME, appInfo.processName, EVENT_MESSAGE,
faultData.errorObject.message, EVENT_STACK, faultData.errorObject.stack, BINDER_INFO, binderInfo,
APP_RUNNING_UNIQUE_ID, appRunningUniqueId, FREEZE_MEMORY, memoryContent,
APP_RUNNING_UNIQUE_ID, appRunningUniqueId, FREEZE_MEMORY, memoryContent + "\n" + faultData.procStatm,
EVENT_TRACE_ID, hitraceIsValid ? info.hiTraceChainId : "",
EVENT_SPAN_ID, hitraceIsValid ? info.spanId : "",
EVENT_PARENT_SPAN_ID, hitraceIsValid ? info.pspanId : "",
@@ -452,7 +458,7 @@ int AppfreezeManager::NotifyANR(const FaultData& faultData, const AppfreezeManag
EVENT_TID, faultData.tid > 0 ? faultData.tid : appInfo.pid,
EVENT_PACKAGE_NAME, appInfo.bundleName, EVENT_PROCESS_NAME, appInfo.processName, EVENT_MESSAGE,
faultData.errorObject.message, EVENT_STACK, faultData.errorObject.stack, BINDER_INFO, binderInfo,
APP_RUNNING_UNIQUE_ID, appRunningUniqueId, FREEZE_MEMORY, memoryContent,
APP_RUNNING_UNIQUE_ID, appRunningUniqueId, FREEZE_MEMORY, memoryContent + "\n" + faultData.procStatm,
FREEZE_INFO_PATH, ReportAppfreezeCpuInfo(faultData, appInfo));
}
TAG_LOGW(AAFwkTag::APPDFR,
@@ -16,6 +16,7 @@
#include "application_anr_listener.h"
#include <sys/time.h>
#include <fstream>
#include "singleton.h"
#include "app_mgr_client.h"
@@ -34,12 +35,19 @@ ApplicationAnrListener::~ApplicationAnrListener() {}
void ApplicationAnrListener::OnAnr(int32_t pid, int32_t eventId) const
{
AppExecFwk::AppFaultDataBySA faultData;
std::ifstream statmStream("/proc/" + std::to_string(pid) + "/statm");
if (statmStream) {
std::string procStatm;
std::getline(statmStream, procStatm);
statmStream.close();
faultData.procStatm = procStatm;
}
faultData.faultType = AppExecFwk::FaultDataType::APP_FREEZE;
faultData.pid = pid;
faultData.errorObject.message = "User input does not respond!";
faultData.errorObject.stack = "\nDump tid stack start time: " +
AbilityRuntime::TimeUtil::DefaultCurrentTimeStr() + "\n";
std::string stack = "";
std::string stack;
if (!HiviewDFX::GetBacktraceStringByTidWithMix(stack, pid, 0, true)) {
stack = "Failed to dump stacktrace for " + std::to_string(pid) + "\n" + stack;
}
@@ -7239,6 +7239,15 @@ int32_t AppMgrServiceInner::NotifyAppFault(const FaultData &faultData)
return ERR_OK;
}
}
if (appRunningManager_ && eventName.find("THREAD_BLOCK_") != std::string::npos) {
std::string appRunningUniqueId;
int32_t ret = appRunningManager_->GetAppRunningUniqueIdByPid(pid, appRunningUniqueId);
TAG_LOGI(AAFwkTag::APPDFR, "ret=%{public}d, appRunningUniqueId=%{public}s", ret, appRunningUniqueId.c_str());
FaultData& nonConstFaultData = const_cast<FaultData&>(faultData);
nonConstFaultData.appRunningUniqueId = appRunningUniqueId;
}
if (SubmitDfxFaultTask(faultData, bundleName, appRecord, pid) != ERR_OK) {
return ERR_INVALID_VALUE;
}
@@ -7434,6 +7443,13 @@ FaultData AppMgrServiceInner::ConvertDataTypes(const AppFaultDataBySA &faultData
newfaultData.eventId = faultData.eventId;
newfaultData.needKillProcess = faultData.needKillProcess;
newfaultData.appfreezeInfo = faultData.appfreezeInfo;
newfaultData.procStatm = faultData.procStatm;
if (appRunningManager_) {
std::string appRunningUniqueId;
int32_t ret = appRunningManager_->GetAppRunningUniqueIdByPid(faultData.pid, appRunningUniqueId);
TAG_LOGI(AAFwkTag::APPDFR, "ret=%{public}d, appRunningUniqueId=%{public}s", ret, appRunningUniqueId.c_str());
newfaultData.appRunningUniqueId = appRunningUniqueId;
}
return newfaultData;
}
@@ -256,5 +256,20 @@ HWTEST_F(AppfreezeInnerTest, AppfreezeInner_AppfreezeHandleOverReportCount_001,
appfreezeInner->AppfreezeHandleOverReportCount(isSixSecondEvent);
EXPECT_TRUE(!isSixSecondEvent);
}
/**
* @tc.number: AppfreezeInner_GetProcStatm_001
* @tc.name: GetProcStatm
* @tc.desc: Verify that function GetProcStatm.
*/
HWTEST_F(AppfreezeInnerTest, AppfreezeInner_GetProcStatm_001, TestSize.Level1)
{
int32_t pid = static_cast<int32_t>(getpid());
std::string procStatm = appfreezeInner->GetProcStatm(pid);
EXPECT_TRUE(!procStatm.empty());
pid = -1;
procStatm = appfreezeInner->GetProcStatm(pid);
EXPECT_TRUE(procStatm.empty());
}
} // namespace AppExecFwk
} // namespace OHOS
@@ -261,7 +261,7 @@ HWTEST_F(AppfreezeManagerTest, AppfreezeManagerTest_AppFreezeFilter_001, TestSiz
int32_t pid = static_cast<int32_t>(getprocpid());
EXPECT_TRUE(!appfreezeManager->CancelAppFreezeDetect(pid, ""));
appfreezeManager->ResetAppfreezeState(pid, "");
EXPECT_TRUE(!appfreezeManager->IsValidFreezeFilter(pid, ""));
EXPECT_TRUE(appfreezeManager->IsValidFreezeFilter(pid, ""));
appfreezeManager->RemoveDeathProcess("");
}
@@ -100,6 +100,8 @@ HWTEST_F(FaultDataTest, ReadFromParcel_001, TestSize.Level1)
messageSixth.WriteUint32(12);
messageSixth.WriteBool(true);
messageSixth.WriteString(helloWord);
messageSixth.WriteString(helloWord);
messageSixth.WriteString(helloWord);
bool retSixth = faultData->ReadFromParcel(messageSixth);
EXPECT_EQ(true, retSixth);
}
@@ -132,6 +134,8 @@ HWTEST_F(FaultDataTest, Unmarshalling_001, TestSize.Level1)
message.WriteUint32(12);
message.WriteBool(true);
message.WriteString(helloWord);
message.WriteString(helloWord);
message.WriteString(helloWord);
auto retSecond = faultData->Unmarshalling(message);
EXPECT_NE(nullptr, retSecond);
}
@@ -207,6 +211,8 @@ HWTEST_F(FaultDataTest, ReadFromParcel_002, TestSize.Level1)
messageSixth.WriteInt32(12);
messageSixth.WriteBool(true);
messageSixth.WriteString(helloWord);
messageSixth.WriteString(helloWord);
messageSixth.WriteString(helloWord);
bool retSixth = appFaultDataBySA->ReadFromParcel(messageSixth);
EXPECT_EQ(true, retSixth);
}
@@ -238,6 +244,8 @@ HWTEST_F(FaultDataTest, Unmarshalling_002, TestSize.Level1)
message.WriteInt32(12);
message.WriteBool(true);
message.WriteString(helloWord);
message.WriteString(helloWord);
message.WriteString(helloWord);
auto retSecond = appFaultDataBySA->Unmarshalling(message);
EXPECT_NE(nullptr, retSecond);
}