From 2fa05eebf1a9a61018efefea5c5cfe42a9fd5b7f Mon Sep 17 00:00:00 2001 From: "j-h.choi" Date: Wed, 24 Jan 2024 15:25:09 +0900 Subject: [PATCH] Sets the ZapDisable env according to the Alternative Sampling value of the coreprofiler Change-Id: Iba9dfd40023c15b0d59a9cc8032c0e9390d7278d --- NativeLauncher/dotnet.debugger | 2 - NativeLauncher/launcher/lib/core_runtime.cc | 4 +- NativeLauncher/launcher/lib/injection.cc | 67 ++++++++++++++++----- NativeLauncher/launcher/lib/injection.h | 2 +- 4 files changed, 54 insertions(+), 21 deletions(-) diff --git a/NativeLauncher/dotnet.debugger b/NativeLauncher/dotnet.debugger index 12ccfc9..1f8f89a 100644 --- a/NativeLauncher/dotnet.debugger +++ b/NativeLauncher/dotnet.debugger @@ -67,7 +67,6 @@ DEFAULT_OPT PROF_TRACE_FILENAME=/home/owner/share/tmp/sdk_tools/coreprofiler/tra DEFAULT_OPT PROF_CONFIG_FILENAME=/home/owner/share/tmp/sdk_tools/coreprofiler/profiler.config DEFAULT_OPT CORECLR_ENABLE_PROFILING=1 DEFAULT_OPT COMPlus_EnableEventLog=1 -DEFAULT_OPT COMPlus_ZapDisable=1 DEFAULT_OPT -c DEFAULT_OPT 6001 DEFAULT_OPT -d @@ -94,7 +93,6 @@ DEFAULT_OPT PROF_TRACE_FILENAME=/home/owner/share/tmp/sdk_tools/coreprofiler/tra DEFAULT_OPT PROF_CONFIG_FILENAME=/home/owner/share/tmp/sdk_tools/coreprofiler/profiler.config DEFAULT_OPT CORECLR_ENABLE_PROFILING=1 DEFAULT_OPT COMPlus_EnableEventLog=1 -DEFAULT_OPT COMPlus_ZapDisable=1 DEFAULT_OPT -c DEFAULT_OPT 6001 DEFAULT_OPT -d diff --git a/NativeLauncher/launcher/lib/core_runtime.cc b/NativeLauncher/launcher/lib/core_runtime.cc index ba874aa..33e3298 100644 --- a/NativeLauncher/launcher/lib/core_runtime.cc +++ b/NativeLauncher/launcher/lib/core_runtime.cc @@ -318,9 +318,9 @@ int CoreRuntime::initialize(const char* appType, LaunchMode launchMode) return -1; } - // checkInjection checks dotnet-launcher run mode + // checkProfilerInjection checks dotnet-launcher run mode // At the moment, this mechanism is used only when the Memory Profiler is started. - int res = checkInjection(); + int res = checkProfilerInjection(); if (res != 0) { _ERR("Failed to initnialize Memory Profiler"); return -1; diff --git a/NativeLauncher/launcher/lib/injection.cc b/NativeLauncher/launcher/lib/injection.cc index 819af59..2c722a4 100644 --- a/NativeLauncher/launcher/lib/injection.cc +++ b/NativeLauncher/launcher/lib/injection.cc @@ -15,17 +15,24 @@ */ #include "log.h" +#include "utils.h" #include #include #include +#include +#include #define HT_PATH "/home/owner/share/tmp/sdk_tools/heaptrack/" #define HT_LIB_PATH HT_PATH "libprofiler.so" #define HT_INJECTION_LIB_PATH HT_PATH "libheaptrack_inject.so" -static int injectLibrary(const char path[]) +#define CP_PATH "/home/owner/share/tmp/sdk_tools/coreprofiler/" +#define CP_LIB_PATH CP_PATH "libcoreprof.so" +#define CP_INHECTION_CONFIG CP_PATH "profiler.config" + +static int injectHeaptrackLibrary() { typedef int inject_func(); @@ -34,10 +41,10 @@ static int injectLibrary(const char path[]) inject_func *inject = nullptr; const char *inject_sym = "dotnet_launcher_inject"; - _INFO("Inject %s library", path); + _INFO("Inject %s library", HT_INJECTION_LIB_PATH); // Current implementation of heaptrack CLR profiler requires RTLD_GLOBAL - lib = dlopen(path, RTLD_NOW | RTLD_GLOBAL); + lib = dlopen(HT_INJECTION_LIB_PATH, RTLD_NOW | RTLD_GLOBAL); if (lib == nullptr) { _ERR("%s", dlerror()); return res; @@ -45,7 +52,7 @@ static int injectLibrary(const char path[]) inject = reinterpret_cast(dlsym(lib, inject_sym)); if (inject == nullptr) { - _ERR("%s is not found in the %s", inject_sym, path); + _ERR("%s is not found in the %s", inject_sym, HT_INJECTION_LIB_PATH); return res; } @@ -53,7 +60,32 @@ static int injectLibrary(const char path[]) return res; } -int checkInjection() +static int injectCoreprofilerConfig() +{ + if (!exist(CP_INHECTION_CONFIG)) { + return -1; + } + + _INFO("Inject %s", CP_INHECTION_CONFIG); + + std::ifstream ifs(CP_INHECTION_CONFIG); + std::string get_conf; + if (ifs.is_open()) { + while (getline(ifs, get_conf)) { + if (!strcmp(get_conf.c_str(), "PROF_USE_ALTERNATIVE_SAMPLING_EXECUTION_TRACING=1")) { + setenv("COMPlus_ZapDisable", "0", 1); + break; + } else if (!strcmp(get_conf.c_str(), "PROF_USE_ALTERNATIVE_SAMPLING_EXECUTION_TRACING=0")) { + setenv("COMPlus_ZapDisable", "1", 1); + break; + } + } + ifs.close(); + } + return 0; +} + +int checkProfilerInjection() { char *env = nullptr; @@ -62,18 +94,21 @@ int checkInjection() return 0; } - // At the moment, this mechanism is used only when the Memory Profiler is started. - if (strcmp(env, HT_LIB_PATH) != 0) { - return 0; - } - - _INFO("##### Perform injection #########"); + _INFO("##### Perform profiler injection #########"); - if (injectLibrary(HT_INJECTION_LIB_PATH) != 0) { - _INFO("##### Injection failed #########"); - return -1; + // At the moment, this mechanism is used only when the Memory Profiler is started. + if (!strcmp(env, HT_LIB_PATH)) { + if (injectHeaptrackLibrary() != 0) { + _INFO("##### Heaptrack injection failed #########"); + return -1; + } + } else if (!strcmp(env, CP_LIB_PATH)) { + if (injectCoreprofilerConfig() != 0) { + _INFO("##### Coreprofiler injection failed #########"); + return -1; + } } - - _INFO("##### Injection finished #########"); + _INFO("##### Injection profiler finished #########"); return 0; } + diff --git a/NativeLauncher/launcher/lib/injection.h b/NativeLauncher/launcher/lib/injection.h index 6bcf111..042fe42 100644 --- a/NativeLauncher/launcher/lib/injection.h +++ b/NativeLauncher/launcher/lib/injection.h @@ -17,6 +17,6 @@ #ifndef __INJETION_INTERFACE_H__ #define __INJETION_INTERFACE_H__ -int checkInjection(); +int checkProfilerInjection(); #endif // __INJETION_INTERFACE_H__ -- 2.34.1