Skip to content

Commit

Permalink
fixed multi-thread with spawn.
Browse files Browse the repository at this point in the history
  • Loading branch information
hariharan-devarajan committed Sep 30, 2024
1 parent 9c6bf18 commit 192db2e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
5 changes: 0 additions & 5 deletions src/dftracer/df_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,3 @@ template <>
std::shared_ptr<DFTLogger> dftracer::Singleton<DFTLogger>::instance = nullptr;
template <>
bool dftracer::Singleton<DFTLogger>::stop_creating_instances = false;

thread_local uint32_t DFTLogger::level = 0;
thread_local std::vector<int> DFTLogger::index_stack = std::vector<int>();
thread_local std::unordered_map<std::string, uint16_t>
DFTLogger::computed_hash = std::unordered_map<std::string, uint16_t>();
39 changes: 29 additions & 10 deletions src/dftracer/df_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ typedef std::chrono::high_resolution_clock chrono;

class DFTLogger {
private:
std::shared_mutex mtx;
bool throw_error;
bool is_init, dftracer_tid;
ProcessID process_id;
std::shared_ptr<dftracer::ChromeWriter> writer;
std::atomic_int index;
bool has_entry;
thread_local static uint32_t level;
thread_local static std::vector<int> index_stack;
thread_local static std::unordered_map<std::string, uint16_t> computed_hash;
uint32_t level;
std::vector<int> index_stack;
std::unordered_map<std::string, uint16_t> computed_hash;
#ifdef DFTRACER_MPI_ENABLE
bool mpi_event;
#endif
Expand Down Expand Up @@ -77,6 +78,9 @@ class DFTLogger {
dftracer_tid(false),
index(0),
has_entry(false),
level(0),
index_stack(),
computed_hash(),
#ifdef DFTRACER_MPI_ENABLE
mpi_event(false),
#endif
Expand Down Expand Up @@ -181,6 +185,7 @@ class DFTLogger {
}

inline int enter_event() {
std::unique_lock<std::shared_mutex> lock(mtx);
index++;
level++;
int current_index = index.load();
Expand All @@ -189,24 +194,41 @@ class DFTLogger {
}

inline void exit_event() {
std::unique_lock<std::shared_mutex> lock(mtx);
level--;
index_stack.pop_back();
}

inline int get_parent() {
std::unique_lock<std::shared_mutex> lock(mtx);
if (level > 1 && index_stack.size() > 1) {
return index_stack[level - 2];
}
return -1;
}

inline int get_current() {
std::unique_lock<std::shared_mutex> lock(mtx);
if (level > 0 && index_stack.size() > 0) {
return index_stack[level - 1];
}
return -1;
}

inline uint16_t has_hash(ConstEventNameType key) {
std::unique_lock<std::shared_mutex> lock(mtx);
auto iter = computed_hash.find(key);
if (iter == computed_hash.end())
return 0;
else
iter->second;
}

inline void insert_hash(ConstEventNameType key, uint16_t hash) {
std::unique_lock<std::shared_mutex> lock(mtx);
computed_hash.insert_or_assign(key, hash);
}

inline TimeResolution get_time() {
DFTRACER_LOG_DEBUG("DFTLogger.get_time", "");
struct timeval tv {};
Expand All @@ -231,7 +253,7 @@ class DFTLogger {
metadata->insert_or_assign("level", level);
int parent_index_value = get_parent();
ConstEventNameType pidx = "p_idx";
metadata->insert_or_assign(pidx, parent_index_value);
metadata->emplace(pidx, parent_index_value);
}
#ifdef DFTRACER_MPI_ENABLE
if (!mpi_event) {
Expand Down Expand Up @@ -285,11 +307,10 @@ class DFTLogger {

inline uint16_t hash_and_store_str(char file[PATH_MAX],
ConstEventNameType name) {
auto iter = computed_hash.find(file);
uint16_t hash = 0;
if (iter == computed_hash.end()) {
uint16_t hash = has_hash(file);
if (hash == 0) {
md5String(file, &hash);
computed_hash.insert_or_assign(file, hash);
insert_hash(file, hash);
if (this->writer != nullptr) {
ThreadID tid = 0;
if (dftracer_tid) {
Expand All @@ -301,8 +322,6 @@ class DFTLogger {
this->process_id, tid, false);
this->exit_event();
}
} else {
hash = iter->second;
}
return hash;
}
Expand Down
12 changes: 12 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT DFTRACER_ENABLE=0)
df_add_test(check_file_exists_${test_name} ${BASH_PROGRAM} ${CMAKE_CURRENT_SOURCE_DIR}/check_file_not.sh ${CMAKE_CURRENT_BINARY_DIR}/${test_name}* 0)
set_tests_properties(check_file_exists_${test_name} PROPERTIES DEPENDS ${test_name})

set(test_name test_py_both)
df_add_test(${test_name} ${DFTRACER_PYTHON_EXE} ${CMAKE_CURRENT_SOURCE_DIR}/py/test.py --format=npz --data_dir=${CMAKE_CURRENT_BINARY_DIR}/data)
set_common_properties(${test_name})
set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT PYTHONPATH=$ENV{PYTHONPATH}:${CMAKE_SOURCE_DIR}/venv/${DFTRACER_LIBDIR})
set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/${DFTRACER_LIBDIR}:${CMAKE_SOURCE_DIR}/dependency/.spack-env/view/lib64:${DFTRACER_TEST_LD_LIBRARY_PATH})
set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT DFTRACER_LOG_FILE=${CMAKE_CURRENT_BINARY_DIR}/${test_name}_app)
set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT LD_PRELOAD=${CMAKE_BINARY_DIR}/${DFTRACER_LIBDIR}/libdftracer_preload.so)
set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT DFTRACER_DATA_DIR=${CMAKE_BINARY_DIR})
set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT DFTRACER_LOG_FILE=${CMAKE_CURRENT_BINARY_DIR}/${test_name})
set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT DFTRACER_INIT=PRELOAD)
set_property(TEST ${test_name} APPEND PROPERTY ENVIRONMENT DFTRACER_INC_METADATA=1)

set(TEST_SRC ${CMAKE_CURRENT_SOURCE_DIR}/util.h)
if (ENABLE_DLIO_BENCHMARK_TESTS)

Expand Down

0 comments on commit 192db2e

Please sign in to comment.