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(); +} +}