Skip to content

Commit

Permalink
Adding Time Hooks for measuring semantics performance (#1131)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Robertorosmaninho and theo25 authored Aug 9, 2024
1 parent 670ad8f commit e098717
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions bin/llvm-kompile-clang
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ if $link; then
"$LIBDIR"/libcollect.a
"$LIBDIR"/libmeta.a
"$LIBDIR"/libjson.a
"$LIBDIR"/libtimer.a
)

components=(
Expand Down
25 changes: 25 additions & 0 deletions include/runtime/timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef TIMER_H
#define TIMER_H

#include <chrono>

// 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<std::chrono::unit>( \
name##_clock_stop - name##_clock_start) \
.count(); \
}

#endif // TIMER_H
1 change: 1 addition & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ add_subdirectory(lto)
add_subdirectory(meta)
add_subdirectory(strings)
add_subdirectory(util)
add_subdirectory(timer)
8 changes: 8 additions & 0 deletions runtime/timer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
add_library( timer STATIC
timer.cpp
)

install(
TARGETS timer
ARCHIVE DESTINATION lib/kllvm
)
25 changes: 25 additions & 0 deletions runtime/timer/timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <fstream>

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

0 comments on commit e098717

Please sign in to comment.