diff --git a/node.gyp b/node.gyp index eca0f2dff08104c60ddf4d33b7e18e36528dde8a..2549192534c97bc250a86d1fd57a4073bc1411d6 100644 --- a/node.gyp +++ b/node.gyp @@ -467,6 +467,7 @@ 'src', 'deps/postject', '../../base/startup/init/interfaces/innerkits/include/param', + '../../base/hiviewdfx/hisysevent/interfaces/native/innerkits/hisysevent/include', '<(SHARED_INTERMEDIATE_DIR)' # for node_natives.h ], 'dependencies': [ @@ -721,6 +722,7 @@ 'V8_DEPRECATION_WARNINGS=1', 'NODE_OPENSSL_SYSTEM_CERT_PATH="<(openssl_system_ca_path)"', 'TARGET_OHOS', + 'ENABLE_HISYSEVENT', ], # - "C4244: conversion from 'type1' to 'type2', possible loss of data" @@ -1443,7 +1445,8 @@ 'ldflags' : [ '<(NDK_SYS_ROOT)/resourceschedule/resource_schedule_service/libressched_client.z.so', - '<(NDK_SYS_ROOT)/startup/init/libbegetutil.z.so' + '<(NDK_SYS_ROOT)/startup/init/libbegetutil.z.so', + '<(NDK_SYS_ROOT)/hiviewdfx/hisysevent/libhisysevent.z.so' ], 'sources': [ diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 9bd659c493921eaa39ef48c9ab6dbc04f093ef34..64af87a56074a9dfd09c57bd384fb27710d4fb4f 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -20,6 +20,10 @@ #include "util.h" #include "sourcemap.def" +#ifdef ENABLE_HISYSEVENT +#include "hisysevent.h" +#endif + #define SECARGCNT 2 #define CHECK_MAYBE_NOTHING(env, maybe, status) \ @@ -1349,6 +1353,45 @@ v8::Platform* JSVM_Env__::platform() { return v8impl::g_platform.get(); } +constexpr int MAX_FILE_LENGTH = 32 * 1024 * 1024; + +static bool LoadStringFromFile(const std::string& filePath, std::string& content) +{ + std::ifstream file(filePath.c_str()); + if (!file.is_open()) { + return false; + } + + file.seekg(0, std::ios::end); + const long fileLength = file.tellg(); + if (fileLength > MAX_FILE_LENGTH) { + return false; + } + + content.clear(); + file.seekg(0, std::ios::beg); + std::copy(std::istreambuf_iterator(file), std::istreambuf_iterator(), std::back_inserter(content)); + return true; +} + +static bool ProcessBundleName(std::string& bundleName) +{ + int pid = getprocpid(); + std::string filePath = "/proc/" + std::to_string(pid) + "/cmdline"; + if (!LoadStringFromFile(filePath, bundleName)) { + return false; + } + if (bundleName.empty()) { + return false; + } + auto pos = bundleName.find(":"); + if (pos != std::string::npos) { + bundleName = bundleName.substr(0, pos); + } + bundleName = bundleName.substr(0, strlen(bundleName.c_str())); + return true; +} + JSVM_Status JSVM_CDECL OH_JSVM_Init(const JSVM_InitOptions* options) { static std::atomic initialized(false); @@ -1357,6 +1400,15 @@ OH_JSVM_Init(const JSVM_InitOptions* options) { } initialized.store(true); #ifdef TARGET_OHOS +#ifdef ENABLE_HISYSEVENT + std::string bundleName; + if (!ProcessBundleName(bundleName)) { + bundleName = "INVALID_BUNDLE_NAME"; + } + HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::JSVM_RUNTIME, "APP_STATS", + OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC, + "BUNDLE_NAME", bundleName); +#endif v8impl::ResourceSchedule::ReportKeyThread(getuid(), getprocpid(), getproctid(), v8impl::ResourceSchedule::ResType::ThreadRole::IMPORTANT_DISPLAY); #endif