Skip to content

Commit

Permalink
code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
hariharan-devarajan committed Oct 8, 2023
1 parent 2fbff6b commit 574945a
Show file tree
Hide file tree
Showing 15 changed files with 237 additions and 214 deletions.
13 changes: 7 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,20 @@ set(DLIO_PROFILER_CORE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/brahma/
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/brahma/stdio.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/writer/chrome_writer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/utils/posix_internal.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/dlio_profiler.cpp)
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/dlio_profiler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/core/dlio_profiler_main.cpp)
set(DLIO_PROFILER_CORE_PUBLIC_INCLUDE
${CMAKE_CURRENT_SOURCE_DIR}/include/dlio_profiler/dlio_profiler.h)
set(DLIO_PROFILER_CORE_PRIVATE_INCLUDE
${CMAKE_CURRENT_SOURCE_DIR}/include/dlio_profiler/typedef.h
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/core/dlio_profiler_main.h
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/core/constants.h
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/utils/posix_internal.h
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/utils/utils.h
${CMAKE_CURRENT_SOURCE_DIR}/include/dlio_profiler/macro.h
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/brahma/posix.h
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/brahma/stdio.h
${CMAKE_CURRENT_SOURCE_DIR}/include/dlio_profiler/dlio_profiler_preload.h)
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/core/dlio_profiler_main.h
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/core/constants.h
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/core/macro.h
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/core/singleton.h
${CMAKE_CURRENT_SOURCE_DIR}/src/dlio_profiler/core/typedef.h)
add_library(${PROJECT_NAME} SHARED)
target_link_libraries(${PROJECT_NAME} ${DEPENDENCY_LIB})
add_library(${PROJECT_NAME}_preload SHARED)
Expand Down
File renamed without changes.
70 changes: 70 additions & 0 deletions include/dlio_profiler/core/dlio_profiler_main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// 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 <cpp-logger/logger.h>
#include <dlio_profiler/core/constants.h>
#include <dlio_profiler/core/macro.h>
#include <brahma/brahma.h>
#include <execinfo.h>
#include <dlio_profiler/core/singleton.h>
#include <dlio_profiler/core/enumeration.h>
#include <dlio_profiler/core/error.h>
#include <any>
#include <csignal>
#include "typedef.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 DLIOProfilerCore {
private:
bool is_enabled;
int gotcha_priority;
cpplogger::LoggerType logger_level;
std::string log_file;
std::string data_dirs;
int process_id;
bool is_initialized;
bool bind;
inline void bind_signals() {
signal(SIGSEGV, handler);
}
void initlialize(bool is_init, bool _bind, const char *_log_file = nullptr, const char *_data_dirs = nullptr, const int *_process_id = nullptr);
public:
DLIOProfilerCore(ProfilerStage stage, ProfileType type, const char *log_file = nullptr, const char *data_dirs = nullptr, const int *process_id = nullptr);
inline bool is_active() {
return is_enabled;
}
TimeResolution get_time();
void log(const char* event_name, const char* category,
TimeResolution start_time, TimeResolution duration,
std::unordered_map<std::string, std::any> &metadata);

bool finalize();
};
} // namespace dlio_profiler

#define DLIO_PROFILER_MAIN_SINGLETON_INIT(stage, type, ...) \
dlio_profiler::Singleton<dlio_profiler::DLIOProfilerCore>::get_instance(stage, type, __VA_ARGS__)

#define DLIO_PROFILER_MAIN_SINGLETON(stage, type) \
dlio_profiler::Singleton<dlio_profiler::DLIOProfilerCore>::get_instance(stage, type)
#endif //DLIO_PROFILER_DLIO_PROFILER_MAIN_H
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion include/dlio_profiler/dlio_profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* CPP Only
*/
// Internal Headers
#include <dlio_profiler/dlio_logger.h>
#include <dlio_profiler/core/dlio_profiler_main.h>
#include <dlio_profiler/core/enumeration.h>

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def build_extension(self, ext: CMakeExtension) -> None:
build_type = os.environ.get("CMAKE_BUILD_TYPE", "Release")
cmake_args += [f"-DCMAKE_BUILD_TYPE={build_type}"]
enable_tests = os.environ.get("DLIO_PROFILER_ENABLE_TESTS", "Off")
cmake_args += [f"-DLIO_PROFILER_ENABLE_TESTS={enable_tests}"]
cmake_args += [f"-DDLIO_PROFILER_ENABLE_TESTS={enable_tests}"]

# CMake lets you override the generator - we need to check this.
# Can be set with Conda-Build, for example.
Expand Down
1 change: 0 additions & 1 deletion src/dlio_profiler/brahma/posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//
#include <cpp-logger/logger.h>
#include <dlio_profiler/brahma/posix.h>
#include <dlio_profiler/dlio_logger.h>

#define CATEGORY "POSIX"

Expand Down
2 changes: 1 addition & 1 deletion src/dlio_profiler/brahma/posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

#include <dlio_profiler/utils/utils.h>
#include <brahma/brahma.h>
#include <dlio_profiler/dlio_logger.h>
#include <dlio_profiler/core/macro.h>
#include <dlio_profiler/dlio_logger.h>
#include <fcntl.h>
#include <filesystem>
#include <fstream>
Expand Down
157 changes: 157 additions & 0 deletions src/dlio_profiler/core/dlio_profiler_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
//
// Created by haridev on 10/8/23.
//
#include <dlio_profiler/core/dlio_profiler_main.h>


#include <dlio_profiler/brahma/posix.h>
#include <dlio_profiler/brahma/stdio.h>
#include <dlio_profiler/dlio_logger.h>

dlio_profiler::DLIOProfilerCore::DLIOProfilerCore(ProfilerStage stage, ProfileType type, const char *log_file,
const char *data_dirs, const int *process_id): is_enabled(
false), gotcha_priority(1), logger_level(cpplogger::LoggerType::LOG_ERROR), log_file(), data_dirs(), is_initialized(false), bind(false) {
const char* user_init_type = getenv(DLIO_PROFILER_INIT);
switch (type) {
case ProfileType::PROFILER_PRELOAD: {
if (stage == ProfilerStage::PROFILER_INIT) {
if (user_init_type != nullptr && strcmp(user_init_type, "PRELOAD") == 0) {
initlialize(true, true, log_file, data_dirs, process_id);
}
DLIO_PROFILER_LOGINFO("Preloading DLIO Profiler with log_file %s data_dir %s and process %d",
this->log_file.c_str(), this->data_dirs.c_str(), this->process_id);
}
break;
}
case ProfileType::PROFILER_PY_APP:
case ProfileType::PROFILER_C_APP:
case ProfileType::PROFILER_CPP_APP: {
if (stage == ProfilerStage::PROFILER_INIT) {
bool bind = false;
if (user_init_type == nullptr || strcmp(user_init_type, "FUNCTION") == 0) {
bind = true;
}
initlialize(true, bind, log_file, data_dirs, process_id);
DLIO_PROFILER_LOGINFO("Initializing DLIO Profiler with log_file %s data_dir %s and process %d",
this->log_file.c_str(), this->data_dirs.c_str(), this->process_id);
}
break;
}
default: {
DLIO_PROFILER_LOGERROR(UNKNOWN_PROFILER_TYPE.message, type);
throw std::runtime_error(UNKNOWN_PROFILER_TYPE.code);
}
}
}

void dlio_profiler::DLIOProfilerCore::log(const char *event_name, const char *category, TimeResolution start_time,
TimeResolution duration,
std::unordered_map<std::string, std::any> &metadata) {
if (this->is_initialized && is_enabled) {
dlio_profiler::Singleton<DLIOLogger>::get_instance(false)->
log(event_name, category, start_time, duration, metadata);
}
}

bool dlio_profiler::DLIOProfilerCore::finalize() {
if (this->is_initialized && is_enabled) {
DLIO_PROFILER_LOGINFO("Calling finalize on pid %d", this->process_id);
dlio_profiler::Singleton<DLIOLogger>::get_instance(false)->finalize();
if (bind) {
free_bindings();
}
this->is_initialized = false;
return true;
}
return false;
}

void
dlio_profiler::DLIOProfilerCore::initlialize(bool is_init, bool _bind, const char *_log_file, const char *_data_dirs,
const int *_process_id) {
this->bind = _bind;
bind_signals();
if (is_init) {
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);

char *dlio_profiler_enable = getenv(DLIO_PROFILER_ENABLE);
if (dlio_profiler_enable != nullptr && strcmp(dlio_profiler_enable, "1") == 0) {
is_enabled = true;
}
if (is_enabled) {
DLIO_PROFILER_LOGINFO("DLIO Profiler enabled", "");
char *dlio_profiler_priority_str = getenv(DLIO_PROFILER_GOTCHA_PRIORITY);
if (dlio_profiler_priority_str != nullptr) {
gotcha_priority = atoi(dlio_profiler_priority_str);
}
if (_process_id == nullptr || *_process_id == -1) {
this->process_id = getpid();
} else {
this->process_id = *_process_id;
}
DLIO_PROFILER_LOGINFO("Setting process_id to %d", this->process_id);
if (_log_file == nullptr) {
char *dlio_profiler_log = getenv(DLIO_PROFILER_LOG_FILE);
if (dlio_profiler_log != nullptr) {
this->log_file = std::string(dlio_profiler_log) + "-" + std::to_string(this->process_id) + ".pfw";
} else {
DLIO_PROFILER_LOGERROR(UNDEFINED_LOG_FILE.message, "");
throw std::runtime_error(UNDEFINED_LOG_FILE.code);
}
} else {
this->log_file = _log_file;
}
DLIO_PROFILER_LOGINFO("Setting log file to %s", this->log_file.c_str());
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 {
DLIO_PROFILER_LOGERROR(UNDEFINED_DATA_DIR.message, "");
throw std::runtime_error(UNDEFINED_DATA_DIR.code);
}
} else {
this->data_dirs = _data_dirs;
}

DLIO_PROFILER_LOGINFO("Setting data_dirs to %s", this->data_dirs.c_str());


dlio_profiler::Singleton<DLIOLogger>::get_instance()->update_log_file(this->log_file, this->process_id);
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());
}
}
}
is_initialized = true;
}
}

TimeResolution dlio_profiler::DLIOProfilerCore::get_time() {
if (this->is_initialized && is_enabled) {
return dlio_profiler::Singleton<DLIOLogger>::get_instance(false)->get_time();
}
return -1;
}
Loading

0 comments on commit 574945a

Please sign in to comment.