From afdad82b2d55d2cd582b6ee7a4ede0a5ef6e6006 Mon Sep 17 00:00:00 2001 From: Hariharan Devarajan Date: Thu, 5 Oct 2023 22:51:41 -0700 Subject: [PATCH] dont bind if LDPRELOAD is used. --- src/dlio_profiler/core/dlio_profiler_main.h | 31 ++++++++++++--------- src/dlio_profiler/dlio_profiler.cpp | 4 +-- src/dlio_profiler/dlio_profiler_py.cpp | 6 ++-- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/dlio_profiler/core/dlio_profiler_main.h b/src/dlio_profiler/core/dlio_profiler_main.h index 0e258811..c11b0cd3 100644 --- a/src/dlio_profiler/core/dlio_profiler_main.h +++ b/src/dlio_profiler/core/dlio_profiler_main.h @@ -41,10 +41,11 @@ namespace dlio_profiler { std::string data_dirs; int process_id; bool is_init; + bool bind; public: - DLIOProfiler(bool is_init, const char *log_file = nullptr, const char *data_dirs = nullptr, const int *process_id = nullptr) : is_enabled( + DLIOProfiler(bool is_init, bool bind, const char *log_file = nullptr, const char *data_dirs = nullptr, const int *process_id = nullptr) : is_enabled( false), gotcha_priority(1), logger_level(cpplogger::LoggerType::LOG_ERROR), log_file(), data_dirs( - ), is_init(is_init) { + ), is_init(is_init), bind(bind) { signal(SIGSEGV, handler); if (this->is_init) { char *dlio_profiler_log_level = getenv(DLIO_PROFILER_LOG_LEVEL); @@ -108,16 +109,18 @@ namespace dlio_profiler { DLIO_PROFILER_LOGINFO("Setting process_id to %d", this->process_id); dlio_profiler::Singleton::get_instance()->update_log_file(this->log_file, this->process_id); - brahma_gotcha_wrap("dlio_profiler", this->gotcha_priority); - auto posix_instance = brahma::POSIXDLIOProfiler::get_instance(); - auto stdio_instance = brahma::STDIODLIOProfiler::get_instance(); - auto paths = split(this->data_dirs, ':'); - posix_instance->untrace(this->log_file.c_str()); - stdio_instance->untrace(this->log_file.c_str()); - for (const auto &path:paths) { - DLIO_PROFILER_LOGINFO("Profiler will trace %s\n", path.c_str()); - posix_instance->trace(path.c_str()); - stdio_instance->trace(path.c_str()); + if (bind) { + brahma_gotcha_wrap("dlio_profiler", this->gotcha_priority); + auto posix_instance = brahma::POSIXDLIOProfiler::get_instance(); + auto stdio_instance = brahma::STDIODLIOProfiler::get_instance(); + auto paths = split(this->data_dirs, ':'); + posix_instance->untrace(this->log_file.c_str()); + stdio_instance->untrace(this->log_file.c_str()); + for (const auto &path:paths) { + DLIO_PROFILER_LOGINFO("Profiler will trace %s\n", path.c_str()); + posix_instance->trace(path.c_str()); + stdio_instance->trace(path.c_str()); + } } size_t thread_hash = std::hash{}(std::this_thread::get_id()); DLIO_PROFILER_LOGINFO("Running DLIO Profiler on thread %ld and pid %ld", thread_hash, this->process_id); @@ -132,7 +135,9 @@ namespace dlio_profiler { if (is_init && is_enabled) { DLIO_PROFILER_LOGINFO("Calling finalize", ""); dlio_profiler::Singleton::get_instance(false)->finalize(); - free_bindings(); + if (bind) { + free_bindings(); + } return true; } return false; diff --git a/src/dlio_profiler/dlio_profiler.cpp b/src/dlio_profiler/dlio_profiler.cpp index 165a2ca5..3f93aba1 100644 --- a/src/dlio_profiler/dlio_profiler.cpp +++ b/src/dlio_profiler/dlio_profiler.cpp @@ -22,7 +22,7 @@ void dlio_profiler_init(void) { char *init_type_count = getenv(DLIO_PROFILER_INIT_COUNT); char *init_type = getenv(DLIO_PROFILER_INIT); if (init_type_count == nullptr && init_type != nullptr && strcmp(init_type, "PRELOAD") == 0) { - dlio_profiler::Singleton::get_instance(true); + dlio_profiler::Singleton::get_instance(true, true); DLIO_PROFILER_LOGINFO("Running initialize within constructor %d", getpid()); set_init(true); int val = setenv(DLIO_PROFILER_INIT_COUNT, "1", 1); @@ -34,7 +34,7 @@ void dlio_profiler_fini(void) { char *init_type = getenv(DLIO_PROFILER_INIT); char *init_type_count = getenv(DLIO_PROFILER_INIT_COUNT); if (init_type_count != nullptr && init_type != nullptr && strcmp(init_type, "PRELOAD")) { - dlio_profiler::Singleton::get_instance(false)->finalize(); + dlio_profiler::Singleton::get_instance(false, false)->finalize(); set_init(false); int val = unsetenv(DLIO_PROFILER_INIT_COUNT); (void) val; diff --git a/src/dlio_profiler/dlio_profiler_py.cpp b/src/dlio_profiler/dlio_profiler_py.cpp index 70e0263d..d9e6beeb 100644 --- a/src/dlio_profiler/dlio_profiler_py.cpp +++ b/src/dlio_profiler/dlio_profiler_py.cpp @@ -23,7 +23,9 @@ namespace dlio_profiler { void initialize(std::string &log_file, std::string &data_dirs, int process_id) { char *init_type = getenv(DLIO_PROFILER_INIT); if (init_type == nullptr || strcmp(init_type, "FUNCTION") == 0) { - dlio_profiler::Singleton::get_instance(true, log_file.c_str(), data_dirs.c_str(), &process_id); + dlio_profiler::Singleton::get_instance(true, true, log_file.c_str(), data_dirs.c_str(), &process_id); + } else { + dlio_profiler::Singleton::get_instance(true, false, log_file.c_str(), data_dirs.c_str(), &process_id); } } TimeResolution get_time() { @@ -42,7 +44,7 @@ namespace dlio_profiler { void finalize() { char *init_type = getenv(DLIO_PROFILER_INIT); if (init_type == nullptr || strcmp(init_type, "FUNCTION") == 0) { - dlio_profiler::Singleton::get_instance(false)->finalize(); + dlio_profiler::Singleton::get_instance(false, false)->finalize(); } } } // dlio_profiler