diff --git a/hidebug/interfaces/js/kits/napi/BUILD.gn b/hidebug/interfaces/js/kits/napi/BUILD.gn index c8efaca6f..0d1cbae2e 100644 --- a/hidebug/interfaces/js/kits/napi/BUILD.gn +++ b/hidebug/interfaces/js/kits/napi/BUILD.gn @@ -39,6 +39,7 @@ ohos_shared_library("hidebug") { "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", + "ffrt:libffrt", "hiappevent:hiappevent_innerapi", "hidumper:lib_dump_usage", "hilog:libhilog", diff --git a/hidebug/interfaces/js/kits/napi/napi_hidebug.cpp b/hidebug/interfaces/js/kits/napi/napi_hidebug.cpp index 15fddc73f..780219818 100644 --- a/hidebug/interfaces/js/kits/napi/napi_hidebug.cpp +++ b/hidebug/interfaces/js/kits/napi/napi_hidebug.cpp @@ -447,7 +447,6 @@ napi_value DumpJsHeapData(napi_env env, napi_callback_info info) napi_value GetPss(napi_env env, napi_callback_info info) { - ApiInvokeRecorder apiInvokeRecorder("getPss"); napi_value pss; std::shared_ptr collector = UCollectUtil::MemoryCollector::Create(); if (collector != nullptr) { @@ -506,7 +505,6 @@ napi_value GetCpuUsage(napi_env env, napi_callback_info info) napi_value GetNativeHeapSize(napi_env env, napi_callback_info info) { - ApiInvokeRecorder apiInvokeRecorder("getNativeHeapSize"); struct mallinfo mi = mallinfo(); napi_value nativeHeapSize; napi_create_bigint_uint64(env, uint64_t(mi.uordblks + mi.fordblks), &nativeHeapSize); @@ -1121,6 +1119,7 @@ napi_value GetGraphicsMemorySync(napi_env env, napi_callback_info info) napi_value DeclareHiDebugInterface(napi_env env, napi_value exports) { + ApiInvokeRecorder::InitProcessor(); napi_property_descriptor desc[] = { DECLARE_NAPI_FUNCTION("startProfiling", StartProfiling), DECLARE_NAPI_FUNCTION("stopProfiling", StopProfiling), diff --git a/hidebug/interfaces/js/kits/napi/util/hiappevent_util.cpp b/hidebug/interfaces/js/kits/napi/util/hiappevent_util.cpp index 578a6c077..03752bb84 100644 --- a/hidebug/interfaces/js/kits/napi/util/hiappevent_util.cpp +++ b/hidebug/interfaces/js/kits/napi/util/hiappevent_util.cpp @@ -19,6 +19,7 @@ #include "app_event.h" #include "app_event_processor_mgr.h" +#include "ffrt.h" #include "hidebug_util.h" #include "hilog/log.h" @@ -30,25 +31,32 @@ namespace HiviewDFX { #undef LOG_TAG #define LOG_TAG "HIAPPEVENT_UTIL" +int64_t ApiInvokeRecorder::processId_ = -1; + ApiInvokeRecorder::ApiInvokeRecorder(std::string apiName) : apiName_(std::move(apiName)), beginTime_(GetNanoSecondsTimestamp()) {} ApiInvokeRecorder::~ApiInvokeRecorder() { - static int64_t processId = AddProcessor(); - if (processId < 0) { + if (processId_ < 0) { return; } - HiAppEvent::Event event("api_diagnostic", "api_exec_end", HiAppEvent::BEHAVIOR); - event.AddParam("trans_id", std::string("transId_") + std::to_string(beginTime_)); - event.AddParam("api_name", apiName_); - event.AddParam("sdk_name", std::string("PerformanceAnalysisKit")); - constexpr int milliSecondsToNanoseconds = 1000 * 1000; - event.AddParam("begin_time", beginTime_ / milliSecondsToNanoseconds); - event.AddParam("end_time", GetNanoSecondsTimestamp() / milliSecondsToNanoseconds); - event.AddParam("result", static_cast(errorCode_ == 0)); - event.AddParam("error_code", errorCode_); - Write(event); + std::string apiName(std::move(apiName_)); + int64_t beginTime(beginTime_); + int errorCode(errorCode_); + auto task = [apiName, beginTime, errorCode] { + HiAppEvent::Event event("api_diagnostic", "api_exec_end", HiAppEvent::BEHAVIOR); + event.AddParam("trans_id", std::string("transId_") + std::to_string(beginTime)); + event.AddParam("api_name", apiName); + event.AddParam("sdk_name", std::string("PerformanceAnalysisKit")); + constexpr int milliSecondsToNanoseconds = 1000 * 1000; + event.AddParam("begin_time", beginTime / milliSecondsToNanoseconds); + event.AddParam("end_time", GetNanoSecondsTimestamp() / milliSecondsToNanoseconds); + event.AddParam("result", static_cast(errorCode == 0)); + event.AddParam("error_code", errorCode); + Write(event); + }; + ffrt::submit(task, {}, {}); } void ApiInvokeRecorder::SetErrorCode(int errorCode) @@ -56,7 +64,7 @@ void ApiInvokeRecorder::SetErrorCode(int errorCode) errorCode_ = errorCode; } -int64_t ApiInvokeRecorder::AddProcessor() +void ApiInvokeRecorder::InitProcessor() { using namespace HiAppEvent; ReportConfig config; @@ -83,11 +91,10 @@ int64_t ApiInvokeRecorder::AddProcessor() .name = "api_called_stat_cnt", .isRealTime = true, }); - int64_t processId = AppEventProcessorMgr::AddProcessor(config); - if (processId < 0) { - HILOG_ERROR(LOG_CORE, "failed to init processor and ret: %{public}" PRId64, processId); + processId_ = AppEventProcessorMgr::AddProcessor(config); + if (processId_ < 0) { + HILOG_ERROR(LOG_CORE, "failed to init processor and ret: %{public}" PRId64, processId_); } - return processId; } } } \ No newline at end of file diff --git a/hidebug/interfaces/js/kits/napi/util/hiappevent_util.h b/hidebug/interfaces/js/kits/napi/util/hiappevent_util.h index fc6f9719f..dd0c68b94 100644 --- a/hidebug/interfaces/js/kits/napi/util/hiappevent_util.h +++ b/hidebug/interfaces/js/kits/napi/util/hiappevent_util.h @@ -24,11 +24,12 @@ public: explicit ApiInvokeRecorder(std::string apiName); ~ApiInvokeRecorder(); void SetErrorCode(int errorCode); + static void InitProcessor(); private: - int64_t AddProcessor(); std::string apiName_; int errorCode_{0}; int64_t beginTime_; + static int64_t processId_; }; } }