diff --git a/include/dftracer/core/constants.h b/include/dftracer/core/constants.h index d8dc893..b6b829e 100644 --- a/include/dftracer/core/constants.h +++ b/include/dftracer/core/constants.h @@ -29,5 +29,6 @@ static const int EVENT_TYPE_SIZE = 128; static const unsigned int DFT_PATH_MAX = 1024 * 4; static const char SEPARATOR = ';'; static const int HASH_OUTPUT = 16; +#define NO_HASH_DEFAULT 0 #endif // DFTRACER_CONSTANTS_H diff --git a/include/dftracer/core/typedef.h b/include/dftracer/core/typedef.h index 6a9f5e3..43b05c5 100644 --- a/include/dftracer/core/typedef.h +++ b/include/dftracer/core/typedef.h @@ -9,4 +9,5 @@ typedef unsigned long int ThreadID; typedef unsigned long int ProcessID; typedef char* EventNameType; typedef const char* ConstEventNameType; +typedef unsigned long long HashType; #endif // DFTRACER_TYPEDEF_H diff --git a/src/dftracer/brahma/posix.h b/src/dftracer/brahma/posix.h index 6130a3b..77e7fe1 100644 --- a/src/dftracer/brahma/posix.h +++ b/src/dftracer/brahma/posix.h @@ -6,7 +6,9 @@ #define DFTRACER_POSIX_H #include +#include #include +#include #include #include #include @@ -25,24 +27,24 @@ class POSIXDFTracer : public POSIX { static bool stop_trace; static std::shared_ptr instance; static const int MAX_FD = 1024; - std::string tracked_fd[MAX_FD]; + HashType tracked_fd[MAX_FD]; std::shared_ptr logger; bool trace_all_files; - inline std::string is_traced(int fd, const char *func) { - if (fd < 0) return std::string(); - std::string trace = tracked_fd[fd % MAX_FD]; - if (trace.empty()) { + inline HashType is_traced(int fd, const char *func) { + if (fd < 0) return NO_HASH_DEFAULT; + HashType trace = tracked_fd[fd % MAX_FD]; + if (trace == NO_HASH_DEFAULT) { DFTRACER_LOG_DEBUG( "Calling POSIXDFTracer.is_traced for %s and" " fd %d trace %d", - func, fd, !trace.empty()); + func, fd, trace != NO_HASH_DEFAULT); } return trace; } - inline std::string is_traced(const char *filename, const char *func) { - if (stop_trace) return std::string(); + inline HashType is_traced(const char *filename, const char *func) { + if (stop_trace) return NO_HASH_DEFAULT; if (trace_all_files) { return logger->hash_and_store(filename, METADATA_NAME_FILE_HASH); } else { @@ -57,7 +59,7 @@ class POSIXDFTracer : public POSIX { } } - inline void trace(int fd, std::string hash) { + inline void trace(int fd, HashType hash) { DFTRACER_LOG_DEBUG("Calling POSIXDFTracer.trace for %d and %d", fd, hash); if (fd == -1) return; tracked_fd[fd % MAX_FD] = hash; @@ -66,13 +68,13 @@ class POSIXDFTracer : public POSIX { inline void remove_trace(int fd) { DFTRACER_LOG_DEBUG("Calling POSIXDFTracer.remove_trace for %d", fd); if (fd == -1) return; - tracked_fd[fd % MAX_FD] = std::string(); + tracked_fd[fd % MAX_FD] = NO_HASH_DEFAULT; } public: POSIXDFTracer(bool trace_all) : POSIX(), trace_all_files(trace_all) { DFTRACER_LOG_DEBUG("POSIX class intercepted", ""); - for (int i = 0; i < MAX_FD; ++i) tracked_fd[i] = std::string(); + for (int i = 0; i < MAX_FD; ++i) tracked_fd[i] = NO_HASH_DEFAULT; logger = DFT_LOGGER_INIT(); } void finalize() { diff --git a/src/dftracer/brahma/stdio.h b/src/dftracer/brahma/stdio.h index dc1a43c..a6b7796 100644 --- a/src/dftracer/brahma/stdio.h +++ b/src/dftracer/brahma/stdio.h @@ -7,6 +7,8 @@ #include #include +#include +#include #include #include #include @@ -22,25 +24,25 @@ class STDIODFTracer : public STDIO { private: static bool stop_trace; static std::shared_ptr instance; - std::unordered_map tracked_fh; + std::unordered_map tracked_fh; std::shared_ptr logger; bool trace_all_files; - inline std::string is_traced(FILE *fh, const char *func) { + inline HashType is_traced(FILE *fh, const char *func) { DFTRACER_LOG_DEBUG("Calling STDIODFTracer.is_traced for %s", func); - if (stop_trace) return std::string(); - if (fh == NULL) return std::string(); + if (stop_trace) return NO_HASH_DEFAULT; + if (fh == NULL) return NO_HASH_DEFAULT; auto iter = tracked_fh.find(fh); if (iter != tracked_fh.end()) { return iter->second; } - return std::string(); + return NO_HASH_DEFAULT; } - inline std::string is_traced(const char *filename, const char *func) { + inline HashType is_traced(const char *filename, const char *func) { DFTRACER_LOG_DEBUG("Calling STDIODFTracer.is_traced with filename for %s", func); - if (stop_trace) return std::string(); + if (stop_trace) return NO_HASH_DEFAULT; if (trace_all_files) return logger->hash_and_store(filename, METADATA_NAME_FILE_HASH); else { @@ -49,7 +51,7 @@ class STDIODFTracer : public STDIO { } } - inline void trace(FILE *fh, std::string hash) { + inline void trace(FILE *fh, HashType hash) { DFTRACER_LOG_DEBUG("Calling STDIODFTracer.trace with hash %d", hash); tracked_fh.insert_or_assign(fh, hash); } diff --git a/src/dftracer/df_logger.h b/src/dftracer/df_logger.h index f851cd6..43556ae 100644 --- a/src/dftracer/df_logger.h +++ b/src/dftracer/df_logger.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -22,6 +23,7 @@ #include #include #include +#include #include #include "core/enumeration.h" @@ -45,7 +47,7 @@ class DFTLogger { std::shared_ptr writer; uint32_t level; std::vector index_stack; - std::unordered_map computed_hash; + std::unordered_map computed_hash; std::atomic_int index; bool has_entry; #ifdef DFTRACER_MPI_ENABLE @@ -104,15 +106,15 @@ class DFTLogger { } ~DFTLogger() {} - inline std::string get_hash(char *name) { + inline HashType get_hash(char *name) { uint8_t result[HASH_OUTPUT]; md5String(name, result); - std::string hash; - hash.reserve(HASH_OUTPUT + 1); + char hash_str[HASH_OUTPUT + 1]; for (int i = 0; i < HASH_OUTPUT; ++i) { - sprintf(hash.data() + i, "%02x", result[i]); + sprintf(hash_str + i, "%02x", result[i]); } - hash.data()[HASH_OUTPUT] = '\0'; + hash_str[HASH_OUTPUT] = '\0'; + HashType hash = std::stoull(hash_str, nullptr, 16); return hash; } @@ -125,15 +127,15 @@ class DFTLogger { tid = df_gettid(); } this->writer = dftracer::Singleton::get_instance(); - std::string hostname_hash; - std::string cmd_hash; - std::string exec_hash; + HashType hostname_hash; + HashType cmd_hash; + HashType exec_hash; if (this->writer != nullptr) { char hostname[256]; gethostname(hostname, 256); hostname_hash = get_hash(hostname); this->writer->initialize(log_file.data(), this->throw_error, - hostname_hash.c_str()); + hostname_hash); hostname_hash = hash_and_store(hostname, METADATA_NAME_HOSTNAME_HASH); char thread_name[128]; auto size = sprintf(thread_name, "%lu", this->process_id); @@ -232,16 +234,16 @@ class DFTLogger { return -1; } - inline std::string has_hash(ConstEventNameType key) { + inline HashType has_hash(ConstEventNameType key) { std::shared_lock lock(map_mtx); auto iter = computed_hash.find(key); - if (iter != computed_hash.end()) return iter->second.c_str(); - return std::string(); + if (iter != computed_hash.end()) return iter->second; + return NO_HASH_DEFAULT; } - inline void insert_hash(ConstEventNameType key, std::string hash) { + inline void insert_hash(ConstEventNameType key, HashType hash) { std::unique_lock lock(map_mtx); - computed_hash.insert_or_assign(key, hash.c_str()); + computed_hash.insert_or_assign(key, hash); } inline TimeResolution get_time() { @@ -331,8 +333,8 @@ class DFTLogger { } } - inline std::string hash_and_store(char *filename, ConstEventNameType name) { - if (filename == NULL) return std::string(); + inline HashType hash_and_store(char *filename, ConstEventNameType name) { + if (filename == NULL) return NO_HASH_DEFAULT; char file[PATH_MAX]; strcpy(file, filename); file[PATH_MAX - 1] = '\0'; @@ -359,12 +361,12 @@ class DFTLogger { } } - inline std::string hash_and_store_str(char file[PATH_MAX], - ConstEventNameType name) { - std::string hash = has_hash(file); - if (hash.empty()) { + inline HashType hash_and_store_str(char file[PATH_MAX], + ConstEventNameType name) { + HashType hash = has_hash(file); + if (hash == NO_HASH_DEFAULT) { hash = get_hash(file); - insert_hash(file, hash.c_str()); + insert_hash(file, hash); if (this->writer != nullptr) { ThreadID tid = 0; if (dftracer_tid) { @@ -372,17 +374,18 @@ class DFTLogger { } fix_str(file, PATH_MAX); int current_index = this->enter_event(); - this->writer->log_metadata(current_index, file, hash.c_str(), name, - this->process_id, tid); + this->writer->log_metadata(current_index, file, + std::to_string(hash).c_str(), name, + this->process_id, tid, false); this->exit_event(); } } return hash; } - inline std::string hash_and_store(const char *filename, - ConstEventNameType name) { - if (filename == NULL) return std::string(); + inline HashType hash_and_store(const char *filename, + ConstEventNameType name) { + if (filename == NULL) return NO_HASH_DEFAULT; char file[PATH_MAX]; strcpy(file, filename); file[PATH_MAX - 1] = '\0'; @@ -416,15 +419,15 @@ class DFTLogger { #define DFT_LOGGER_UPDATE_HASH(value) \ if (trace && this->logger->include_metadata) { \ - std::string value##_hash = \ + HashType value##_hash = \ this->logger->hash_and_store(value, METADATA_NAME_FILE_HASH); \ DFT_LOGGER_UPDATE(value##_hash); \ } #define DFT_LOGGER_START(entity) \ DFTRACER_LOG_DEBUG("Calling function %s", __FUNCTION__); \ - std::string fhash = is_traced(entity, __FUNCTION__); \ - bool trace = !fhash.empty(); \ + HashType fhash = is_traced(entity, __FUNCTION__); \ + bool trace = fhash != NO_HASH_DEFAULT; \ TimeResolution start_time = 0; \ std::unordered_map *metadata = nullptr; \ if (trace) { \ diff --git a/src/dftracer/writer/chrome_writer.cpp b/src/dftracer/writer/chrome_writer.cpp index 9adb7b8..790a529 100644 --- a/src/dftracer/writer/chrome_writer.cpp +++ b/src/dftracer/writer/chrome_writer.cpp @@ -22,7 +22,7 @@ template <> bool dftracer::Singleton::stop_creating_instances = false; void dftracer::ChromeWriter::initialize(char *filename, bool throw_error, - const char *hostname_hash) { + HashType hostname_hash) { this->hostname_hash = hostname_hash; this->throw_error = throw_error; this->filename = filename; @@ -185,6 +185,11 @@ void dftracer::ChromeWriter::convert_json( << "\":" << std::any_cast(item.second) << ""; if (i < meta_size - 1) meta_stream << ","; + } else if (item.second.type() == typeid(HashType)) { + meta_stream << "\"" << item.first + << "\":" << std::any_cast(item.second) << ""; + if (i < meta_size - 1) meta_stream << ","; + } else if (item.second.type() == typeid(long)) { meta_stream << "\"" << item.first << "\":" << std::any_cast(item.second) << ""; @@ -214,10 +219,9 @@ void dftracer::ChromeWriter::convert_json( previous_index = current_index; auto written_size = sprintf( buffer.data() + current_index, - R"(%s{"id":%d,"name":"%s","cat":"%s","pid":%lu,"tid":%lu,"ts":%llu,"dur":%llu,"ph":"X","args":{"hhash":"%s"%s}})", + R"(%s{"id":%d,"name":"%s","cat":"%s","pid":%lu,"tid":%lu,"ts":%llu,"dur":%llu,"ph":"X","args":{"hhash":%llu%s}})", is_first_char, index, event_name, category, process_id, thread_id, - start_time, duration, this->hostname_hash.c_str(), - all_stream.str().c_str()); + start_time, duration, this->hostname_hash, all_stream.str().c_str()); current_index += written_size; buffer[current_index] = '\n'; current_index++; @@ -256,15 +260,15 @@ void dftracer::ChromeWriter::convert_json_metadata( if (is_string) { written_size = sprintf( buffer.data() + current_index, - R"(%s{"id":%d,"name":"%s","cat":"dftracer","pid":%lu,"tid":%lu,"ph":"M","args":{"hhash":"%s","name":"%s","value":"%s"}})", - is_first_char, index, ph, process_id, thread_id, - this->hostname_hash.c_str(), name, value); + R"(%s{"id":%d,"name":"%s","cat":"dftracer","pid":%lu,"tid":%lu,"ph":"M","args":{"hhash":%llu,"name":"%s","value":"%s"}})", + is_first_char, index, ph, process_id, thread_id, this->hostname_hash, + name, value); } else { written_size = sprintf( buffer.data() + current_index, - R"(%s{"id":%d,"name":"%s","cat":"dftracer","pid":%lu,"tid":%lu,"ph":"M","args":{"hhash":"%s","name":"%s","value":%s}})", - is_first_char, index, ph, process_id, thread_id, - this->hostname_hash.c_str(), name, value); + R"(%s{"id":%d,"name":"%s","cat":"dftracer","pid":%lu,"tid":%lu,"ph":"M","args":{"hhash":%llu,"name":"%s","value":%s}})", + is_first_char, index, ph, process_id, thread_id, this->hostname_hash, + name, value); } current_index += written_size; buffer[current_index] = '\n'; diff --git a/src/dftracer/writer/chrome_writer.h b/src/dftracer/writer/chrome_writer.h index 1990ccb..caabe8e 100644 --- a/src/dftracer/writer/chrome_writer.h +++ b/src/dftracer/writer/chrome_writer.h @@ -36,7 +36,7 @@ class ChromeWriter { bool enable_core_affinity; FILE *fh; - std::string hostname_hash; + HashType hostname_hash; static const int MAX_LINE_SIZE = 16 * 1024L; size_t write_buffer_size; @@ -100,7 +100,7 @@ class ChromeWriter { } } ~ChromeWriter() { DFTRACER_LOG_DEBUG("Destructing ChromeWriter", ""); } - void initialize(char *filename, bool throw_error, const char *hostname_hash); + void initialize(char *filename, bool throw_error, HashType hostname_hash); void log(int index, ConstEventNameType event_name, ConstEventNameType category, TimeResolution start_time,