From 4e86cd8a7f5c164fb00a86b31d33ad95470f337f Mon Sep 17 00:00:00 2001 From: jpvillam Date: Fri, 12 Apr 2024 16:08:16 -0400 Subject: [PATCH 1/2] Make a context manager that can be called in a loop --- rpd_tracer/Logger.cpp | 24 ++++++++++++++ rpd_tracer/Logger.h | 2 ++ rpd_tracer/rpdTracerControl.py | 58 ++++++++++++++++++++++++++++++++-- rpd_tracer/rpd_tracer.cpp | 5 +-- 4 files changed, 85 insertions(+), 4 deletions(-) diff --git a/rpd_tracer/Logger.cpp b/rpd_tracer/Logger.cpp index 0d03a24..851f4be 100644 --- a/rpd_tracer/Logger.cpp +++ b/rpd_tracer/Logger.cpp @@ -63,6 +63,20 @@ void rpdflush() Logger::singleton().rpdflush(); } +void rpdFinalize() +{ + Logger::singleton().rpdFinalize(); +} +void rpdInit() +{ + Logger::singleton().rpdInit(); +} + +void rpdResetFinalize() +{ + Logger::singleton().rpdResetFinalize(); +} + void rpd_rangePush(const char *domain, const char *apiName, const char* args) { Logger::singleton().rpd_rangePush(domain, apiName, args); @@ -95,6 +109,10 @@ void Logger::rpdInit() { void Logger::rpdFinalize() { Logger::singleton().finalize(); } +void Logger::rpdResetFinalize() +{ + Logger::singleton().set_finalize_true(); +} void Logger::rpdstart() @@ -249,6 +267,12 @@ void Logger::init() static bool doFinalize = true; std::mutex finalizeMutex; +void Logger::set_finalize_true() +{ + std::lock_guard guard(finalizeMutex); + doFinalize = true; +} + void Logger::finalize() { std::lock_guard guard(finalizeMutex); diff --git a/rpd_tracer/Logger.h b/rpd_tracer/Logger.h index 07a6d21..8d6e966 100644 --- a/rpd_tracer/Logger.h +++ b/rpd_tracer/Logger.h @@ -48,6 +48,7 @@ class Logger void rpdstart(); void rpdstop(); void rpdflush(); + void rpdResetFinalize(); // External maker api void rpd_rangePush(const char *domain, const char *apiName, const char* args); @@ -80,6 +81,7 @@ class Logger void init(); void finalize(); + void set_finalize_true(); std::string m_filename; bool m_writeOverheadRecords {true}; diff --git a/rpd_tracer/rpdTracerControl.py b/rpd_tracer/rpdTracerControl.py index 9bbdc39..6e0ea0d 100644 --- a/rpd_tracer/rpdTracerControl.py +++ b/rpd_tracer/rpdTracerControl.py @@ -65,7 +65,7 @@ def initializeFile(cls): connection.close() # os.environ["RPDT_FILENAME"] = cls.__filename - cls.__initFile = False + cls.__initFile = False # You can set the output filename and optionally append to an exiting file. @@ -87,9 +87,26 @@ def setFilename(cls, name, append = False): os.environ["RPDT_FILENAME"] = cls.__filename cls.__initFile = False - def __init__(self): + @classmethod + def rpdReset(cls): + rpdTracerControl.__rpd.rpdResetFinalize() + rpdTracerControl.__rpd.rpdFinalize() + #cls.__initFile = True + + def __init__(self, file_name = None, nvtx = False): + if file_name != None: + # Force reset filename, if we are getting called in a loop. + rpdTracerControl.__initFile = True + rpdTracerControl.__filename = file_name rpdTracerControl.initializeFile() rpdTracerControl.loadLibrary() + self.nvtx = None + if nvtx: + import torch + self.nvtx = torch.autograd.profiler.emit_nvtx() + if file_name != None: + # Reinit for the new file + rpdTracerControl.__rpd.rpdInit() def __del__(self): pass @@ -105,9 +122,46 @@ def flush(self): def __enter__(self): self.start() + if self.nvtx: + self.nvtx.__enter__() + return self def __exit__(self, exc_type, exc_val, exc_tb): + if self.nvtx: + self.nvtx.__exit__(exc_type, exc_val, exc_tb) + if exc_type != None: + #Propagate exception + return False self.stop() + self.flush() + rpdTracerControl.rpdReset() + + def top_totals(self): + try: + conn = sqlite3.connect(rpdTracerControl.__filename) + cursor = conn.cursor() + cursor.execute("SELECT Name, TotalCalls, TotalDuration, Ave, Percentage FROM top;") + rows = cursor.fetchall() + + if rows: + from prettytable import PrettyTable + import textwrap + table = PrettyTable() + table.field_names = ["Name", "TotalCalls", "TotalDuration", "Ave", "Percentage"] + table.align = "l" + + for row in rows: + wrapped_name = '\n'.join(textwrap.wrap(row[0], 60)) + table.add_row([wrapped_name] + list(row[1:])) + + print(table) + else: + print("No data found in 'top' table.") + + except sqlite3.Error as e: + print(f"Error querying database: {e}") + finally: + conn.close() def rangePush(self, domain: str, apiName: str, args: str): rpdTracerControl.__rpd.rpd_rangePush(bytes(domain, encoding='utf-8'), bytes(apiName, encoding='utf-8'), bytes(args, encoding='utf-8')) diff --git a/rpd_tracer/rpd_tracer.cpp b/rpd_tracer/rpd_tracer.cpp index adaf747..902e95d 100644 --- a/rpd_tracer/rpd_tracer.cpp +++ b/rpd_tracer/rpd_tracer.cpp @@ -112,6 +112,7 @@ static std::mutex activeMutex; void rpdstart() { + fprintf(stderr, "JUAN START\n"); std::unique_lock lock(activeMutex); if (activeCount == 0) { //fprintf(stderr, "rpd_tracer: START\n"); @@ -895,7 +896,7 @@ void hcc_activity_callback(const char* begin, const char* end, void* arg) roctracer_pool_t *hccPool; void init_tracing() { - //printf("# INIT #############################\n"); + printf("# INIT #############################\n"); // roctracer properties // Whatever the hell that means. Magic encantation, thanks. @@ -1031,7 +1032,7 @@ void rpdFinalize() std::lock_guard guard(finalizeMutex); if (doFinalize == true) { doFinalize = false; - //printf("+++++++++++++++++++ rpdFinalize\n"); + printf("+++++++++++++++++++ rpdFinalize\n"); end_tracing(); // Flush recorders From 9216d4ac5e27713ab8cc7d1bd1daee849340cc0d Mon Sep 17 00:00:00 2001 From: jpvillam Date: Fri, 12 Apr 2024 16:12:33 -0400 Subject: [PATCH 2/2] Restore unused file --- rpd_tracer/rpd_tracer.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/rpd_tracer/rpd_tracer.cpp b/rpd_tracer/rpd_tracer.cpp index 902e95d..adaf747 100644 --- a/rpd_tracer/rpd_tracer.cpp +++ b/rpd_tracer/rpd_tracer.cpp @@ -112,7 +112,6 @@ static std::mutex activeMutex; void rpdstart() { - fprintf(stderr, "JUAN START\n"); std::unique_lock lock(activeMutex); if (activeCount == 0) { //fprintf(stderr, "rpd_tracer: START\n"); @@ -896,7 +895,7 @@ void hcc_activity_callback(const char* begin, const char* end, void* arg) roctracer_pool_t *hccPool; void init_tracing() { - printf("# INIT #############################\n"); + //printf("# INIT #############################\n"); // roctracer properties // Whatever the hell that means. Magic encantation, thanks. @@ -1032,7 +1031,7 @@ void rpdFinalize() std::lock_guard guard(finalizeMutex); if (doFinalize == true) { doFinalize = false; - printf("+++++++++++++++++++ rpdFinalize\n"); + //printf("+++++++++++++++++++ rpdFinalize\n"); end_tracing(); // Flush recorders