From e098717474d355ea362b20e7804118fb8250bc7d Mon Sep 17 00:00:00 2001 From: Roberto Rosmaninho Date: Fri, 9 Aug 2024 16:08:56 -0300 Subject: [PATCH] Adding Time Hooks for measuring semantics performance (#1131) This PR introduces `hook_timer_start` and `hook_timer_stop`, which can be used to measure the execution time of an instruction or an entire semantics by getting the current timestamp when starting the timer and then when stopping it, we get the current timestamp again, and save the difference into a default file `hooks_time.txt` in the current directory. --------- Co-authored-by: Theodoros Kasampalis --- bin/llvm-kompile-clang | 1 + include/runtime/timer.h | 25 +++++++++++++++++++++++++ runtime/CMakeLists.txt | 1 + runtime/timer/CMakeLists.txt | 8 ++++++++ runtime/timer/timer.cpp | 25 +++++++++++++++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 include/runtime/timer.h create mode 100644 runtime/timer/CMakeLists.txt create mode 100644 runtime/timer/timer.cpp diff --git a/bin/llvm-kompile-clang b/bin/llvm-kompile-clang index 138e65150..7adce5a1d 100644 --- a/bin/llvm-kompile-clang +++ b/bin/llvm-kompile-clang @@ -263,6 +263,7 @@ if $link; then "$LIBDIR"/libcollect.a "$LIBDIR"/libmeta.a "$LIBDIR"/libjson.a + "$LIBDIR"/libtimer.a ) components=( diff --git a/include/runtime/timer.h b/include/runtime/timer.h new file mode 100644 index 000000000..910954ed7 --- /dev/null +++ b/include/runtime/timer.h @@ -0,0 +1,25 @@ +#ifndef TIMER_H +#define TIMER_H + +#include + +// Macro to register a new timer. +// Timers are implemented using the std::chrono::high_resolution_clock. +// The unit should be one of the duration types provided in std::chrono, +// e.g. seconds, microseconds, etc. +#define REGISTER_TIMER(name, unit) \ + static std::chrono::high_resolution_clock::time_point name##_clock_start; \ + static std::chrono::high_resolution_clock::time_point name##_clock_stop; \ + void name##_timer_start() { \ + name##_clock_start = std::chrono::high_resolution_clock::now(); \ + } \ + void name##_timer_stop() { \ + name##_clock_stop = std::chrono::high_resolution_clock::now(); \ + } \ + uint64_t name##_timer_measurement() { \ + return std::chrono::duration_cast( \ + name##_clock_stop - name##_clock_start) \ + .count(); \ + } + +#endif // TIMER_H diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 7cb635ccd..d89eb3815 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -19,3 +19,4 @@ add_subdirectory(lto) add_subdirectory(meta) add_subdirectory(strings) add_subdirectory(util) +add_subdirectory(timer) diff --git a/runtime/timer/CMakeLists.txt b/runtime/timer/CMakeLists.txt new file mode 100644 index 000000000..11d9abb2a --- /dev/null +++ b/runtime/timer/CMakeLists.txt @@ -0,0 +1,8 @@ +add_library( timer STATIC + timer.cpp +) + +install( + TARGETS timer + ARCHIVE DESTINATION lib/kllvm +) diff --git a/runtime/timer/timer.cpp b/runtime/timer/timer.cpp new file mode 100644 index 000000000..8068c4ae3 --- /dev/null +++ b/runtime/timer/timer.cpp @@ -0,0 +1,25 @@ +#include + +#include "runtime/header.h" +#include "runtime/timer.h" + +REGISTER_TIMER(hook, nanoseconds); + +extern "C" { + +block *hook_TIMER_timerStart(void) { + hook_timer_start(); + + return dot_k(); +} + +block *hook_TIMER_timerStop(void) { + hook_timer_stop(); + + std::ofstream times_file; + times_file.open("hook_times.txt", std::ios_base::app); + times_file << hook_timer_measurement() << std::endl; + + return dot_k(); +} +}