From 22466ea56ffc2bdf0d40adffde4f1188c6050828 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sat, 9 Sep 2023 21:25:57 +0100 Subject: [PATCH] Add time_in_seconds function --- haversine_02/include/profiler/timer.h | 2 ++ haversine_02/src/profiler/timer.c | 9 +++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/haversine_02/include/profiler/timer.h b/haversine_02/include/profiler/timer.h index aab8572..f0f8fde 100644 --- a/haversine_02/include/profiler/timer.h +++ b/haversine_02/include/profiler/timer.h @@ -61,6 +61,8 @@ u64 read_cpu_timer(void); // CPU frequency in hz/sec u64 get_cpu_freq(u64 milliseconds); +f64 time_in_seconds(u64 cpu_time, u64 cpu_freq); + void profile_start(u64 count); void profile_end(); diff --git a/haversine_02/src/profiler/timer.c b/haversine_02/src/profiler/timer.c index 80066e8..52c9be5 100644 --- a/haversine_02/src/profiler/timer.c +++ b/haversine_02/src/profiler/timer.c @@ -38,12 +38,13 @@ u64 read_cpu_timer(void) { return __rdtsc(); } u64 get_cpu_freq(u64 milliseconds) { u64 os_freq = get_os_frequency(); - u64 os_start = get_os_time(); - u64 cpu_start = read_cpu_timer(); u64 os_end = 0; u64 os_elapsed = 0; u64 os_wait_time = os_freq * milliseconds / 1000; + u64 os_start = get_os_time(); + u64 cpu_start = read_cpu_timer(); + while (os_elapsed < os_wait_time) { os_end = get_os_time(); os_elapsed = os_end - os_start; @@ -61,6 +62,10 @@ u64 get_cpu_freq(u64 milliseconds) { return cpu_freq; } +f64 time_in_seconds(u64 cpu_time, u64 cpu_freq) { + return (f64)cpu_time / cpu_freq; +} + void profile_start(u64 count) { profiler.cpu_freq = get_cpu_freq(1000); profiler.start = read_cpu_timer();