-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make LD_PRELOAD style of DLIO Profiler.
- Loading branch information
1 parent
e81d135
commit 97310e7
Showing
8 changed files
with
204 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// Created by haridev on 10/5/23. | ||
// | ||
|
||
#ifndef DLIO_PROFILER_CONSTANTS_H | ||
#define DLIO_PROFILER_CONSTANTS_H | ||
#define DLIO_PROFILER_ENABLE "DLIO_PROFILER_ENABLE" | ||
#define DLIO_PROFILER_GOTCHA_PRIORITY "DLIO_PROFILER_GOTCHA_PRIORITY" | ||
#define DLIO_PROFILER_LOG_LEVEL "DLIO_PROFILER_LOG_LEVEL" | ||
#define DLIO_PROFILER_LOG_FILE "DLIO_PROFILER_LOG_FILE" | ||
#define DLIO_PROFILER_INIT "DLIO_PROFILER_INIT" | ||
#define DLIO_PROFILER_DATA_DIR "DLIO_PROFILER_DATA_DIR" | ||
#endif //DLIO_PROFILER_CONSTANTS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
// | ||
// Created by haridev on 10/5/23. | ||
// | ||
|
||
#ifndef DLIO_PROFILER_DLIO_PROFILER_MAIN_H | ||
#define DLIO_PROFILER_DLIO_PROFILER_MAIN_H | ||
#include <cstring> | ||
#include <thread> | ||
#include <stdexcept> | ||
#include <execution> | ||
|
||
#include <cpp-logger/logger.h> | ||
#include <dlio_profiler/core/constants.h> | ||
#include <dlio_profiler/macro.h> | ||
#include <dlio_profiler/brahma/posix.h> | ||
#include <dlio_profiler/brahma/stdio.h> | ||
#include <dlio_profiler/dlio_logger.h> | ||
#include <brahma/brahma.h> | ||
#include <execinfo.h> | ||
#include "singleton.h" | ||
|
||
static void handler(int sig) { | ||
void *array[10]; | ||
size_t size; | ||
|
||
// get void*'s for all entries on the stack | ||
size = backtrace(array, 10); | ||
|
||
// print out all the frames to stderr | ||
fprintf(stderr, "Error: signal %d:\n", sig); | ||
backtrace_symbols_fd(array, size, STDERR_FILENO); | ||
exit(1); | ||
} | ||
|
||
namespace dlio_profiler { | ||
class DLIOProfiler { | ||
private: | ||
bool is_enabled; | ||
int gotcha_priority; | ||
cpplogger::LoggerType logger_level; | ||
char* log_file; | ||
char* data_dirs; | ||
int process_id; | ||
bool is_init; | ||
public: | ||
DLIOProfiler(bool is_init, char *log_file = nullptr, char *data_dirs = nullptr, int *process_id = nullptr) : is_enabled( | ||
false), gotcha_priority(1), logger_level(cpplogger::LoggerType::LOG_ERROR), log_file(nullptr), data_dirs( | ||
nullptr), is_init(is_init) { | ||
signal(SIGSEGV, handler); | ||
if (this->is_init) { | ||
char *dlio_profiler_enable = getenv(DLIO_PROFILER_ENABLE); | ||
if (dlio_profiler_enable != nullptr && strcmp(dlio_profiler_enable, "1") == 0) { | ||
is_enabled = true; | ||
} | ||
char *dlio_profiler_priority_str = getenv(DLIO_PROFILER_GOTCHA_PRIORITY); | ||
if (dlio_profiler_priority_str != nullptr) { | ||
gotcha_priority = atoi(dlio_profiler_priority_str); | ||
} | ||
char *dlio_profiler_log_level = getenv(DLIO_PROFILER_LOG_LEVEL); | ||
if (dlio_profiler_log_level == nullptr) { | ||
logger_level = cpplogger::LoggerType::LOG_ERROR; | ||
} else { | ||
if (strcmp(dlio_profiler_log_level, "ERROR") == 0) { | ||
logger_level = cpplogger::LoggerType::LOG_ERROR; | ||
} else if (strcmp(dlio_profiler_log_level, "INFO") == 0) { | ||
logger_level = cpplogger::LoggerType::LOG_INFO; | ||
} else if (strcmp(dlio_profiler_log_level, "DEBUG") == 0) { | ||
logger_level = cpplogger::LoggerType::LOG_WARN; | ||
} | ||
} | ||
DLIO_PROFILER_LOGGER->level = logger_level; | ||
DLIO_PROFILER_LOGINFO("Enabling logging level %d", logger_level); | ||
|
||
if (log_file == nullptr) { | ||
char *dlio_profiler_log = getenv(DLIO_PROFILER_LOG_FILE); | ||
if (dlio_profiler_log != nullptr) { | ||
this->log_file = dlio_profiler_log; | ||
} else { | ||
const char *message = "log_file not defined. Please define env variable DLIO_PROFILER_LOG_FILE"; | ||
DLIO_PROFILER_LOGERROR(message, ""); | ||
throw std::runtime_error(message); | ||
} | ||
} | ||
|
||
if (data_dirs == nullptr) { | ||
char *dlio_profiler_data_dirs = getenv(DLIO_PROFILER_DATA_DIR); | ||
if (dlio_profiler_data_dirs != nullptr) { | ||
this->data_dirs = dlio_profiler_data_dirs; | ||
} else { | ||
const char *message = "data_dirs not defined. Please define env variable DLIO_PROFILER_DATA_DIR"; | ||
DLIO_PROFILER_LOGERROR(message, ""); | ||
throw std::runtime_error(message); | ||
} | ||
} | ||
|
||
if (process_id == nullptr) { | ||
this->process_id = getpid(); | ||
} else { | ||
this->process_id = *process_id; | ||
} | ||
|
||
dlio_profiler::Singleton<DLIOLogger>::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); | ||
stdio_instance->untrace(this->log_file); | ||
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::thread::id>{}(std::this_thread::get_id()); | ||
DLIO_PROFILER_LOGINFO("Running DLIO Profiler on thread %ld and pid %ld", thread_hash, this->process_id); | ||
} | ||
} | ||
bool finalize() { | ||
if (is_init) { | ||
DLIO_PROFILER_LOGINFO("Calling finalize", ""); | ||
dlio_profiler::Singleton<DLIOLogger>::get_instance(false)->finalize(); | ||
free_bindings(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
}; | ||
} // namespace dlio_profiler | ||
|
||
|
||
#endif //DLIO_PROFILER_DLIO_PROFILER_MAIN_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// | ||
// Created by haridev on 10/5/23. | ||
// | ||
|
||
#ifndef DLIO_PROFILER_ERROR_H | ||
#define DLIO_PROFILER_ERROR_H | ||
|
||
#endif //DLIO_PROFILER_ERROR_H |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// Created by haridev on 3/28/23. | ||
// | ||
|
||
#include <dlio_profiler/dlio_profiler.h> | ||
#include <cstring> | ||
#include <iostream> | ||
#include <fstream> | ||
#include<algorithm> | ||
#include <dlio_profiler/dlio_logger.h> | ||
#include <dlio_profiler/utils/utils.h> | ||
#include <dlio_profiler/core/constants.h> | ||
#include <dlio_profiler/core/dlio_profiler_main.h> | ||
|
||
namespace dlio_profiler { | ||
bool init = false; | ||
} | ||
|
||
bool is_init() {return dlio_profiler::init;} | ||
void set_init(bool _init) { dlio_profiler::init = _init;} | ||
void dlio_profiler_init(void) { | ||
char *init_type = getenv(DLIO_PROFILER_INIT); | ||
if (!is_init() && init_type != nullptr && strcmp(init_type, "PRELOAD")) { | ||
dlio_profiler::Singleton<dlio_profiler::DLIOProfiler>::get_instance(true); | ||
DLIO_PROFILER_LOGINFO("Running initialize within constructor", ""); | ||
set_init(true); | ||
} | ||
|
||
} | ||
void dlio_profiler_fini(void) { | ||
char *init_type = getenv(DLIO_PROFILER_INIT); | ||
if (is_init() && init_type != nullptr && strcmp(init_type, "PRELOAD")) { | ||
dlio_profiler::Singleton<dlio_profiler::DLIOProfiler>::get_instance(false)->finalize(); | ||
set_init(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters