From 2d131dbc71e897f652dd45c3df4723252f032c53 Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Fri, 26 Sep 2025 19:19:13 +0200 Subject: [PATCH] correct timer units --- mstime.c | 8 ++++---- mstime.h | 2 +- mutex.c | 9 +++++---- nstime.c | 15 +++++++++++++++ nstime.h | 12 ++++++++++++ stat.c | 19 ++++++++++--------- stat.h | 6 +++--- 7 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 nstime.c create mode 100644 nstime.h diff --git a/mstime.c b/mstime.c index 161b68e..e9065b7 100644 --- a/mstime.c +++ b/mstime.c @@ -2,14 +2,14 @@ #include "mstime.h" -int64_t timespec_as_nanoseconds (struct timespec ts) +mstime_t timespec_as_microseconds (struct timespec ts) { - return ts.tv_sec * 1000000000 + ts.tv_nsec ; + return ts.tv_sec * 1000000 + ts.tv_nsec / 1000 ; } -int64_t sys_timestamp (void) +mstime_t sys_timestamp (void) { struct timespec ts; clock_gettime (CLOCK_REALTIME, &ts); - return timespec_as_nanoseconds (ts); + return timespec_as_microseconds (ts); } diff --git a/mstime.h b/mstime.h index 959c40a..b007ffd 100644 --- a/mstime.h +++ b/mstime.h @@ -1,4 +1,4 @@ -/* microsecond time */ +/* microsecond time module */ #ifndef __MSTIME_H__ #define __MSTIME_H__ diff --git a/mutex.c b/mutex.c index 10b85ef..97cf3bc 100644 --- a/mutex.c +++ b/mutex.c @@ -1,6 +1,6 @@ /* depend: */ /* cflags: */ -/* linker: mstime.o stat.o -lpthread -lm */ +/* linker: nstime.o stat.o -lpthread -lm */ #include #include @@ -10,7 +10,7 @@ #include #include -#include "mstime.h" +#include "nstime.h" #include "stat.h" /* static variables */ @@ -27,7 +27,7 @@ char *output = NULL; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int shared_flag = 0; -mstime_t timestamp1, timestamp2; +nstime_t timestamp1, timestamp2; int try = 0; void *mutex_giver_task (__attribute__((unused)) void *arg) @@ -43,7 +43,7 @@ void *mutex_giver_task (__attribute__((unused)) void *arg) void *mutex_taker_task (__attribute__((unused)) void *arg) { - mstime_t *deltas = (mstime_t *) calloc (nb_measurements, sizeof (mstime_t)); + nstime_t *deltas = (nstime_t *) calloc (nb_measurements, sizeof (nstime_t)); assert (deltas); while (try < nb_measurements) { @@ -66,6 +66,7 @@ void *mutex_taker_task (__attribute__((unused)) void *arg) } if (do_stat) { + printf ("Results are in nanosecond\n"); compute_statistics (deltas, nb_measurements, hist_bin); } diff --git a/nstime.c b/nstime.c new file mode 100644 index 0000000..774d57e --- /dev/null +++ b/nstime.c @@ -0,0 +1,15 @@ +#include + +#include "nstime.h" + +nstime_t timespec_as_nanoseconds (struct timespec ts) +{ + return ts.tv_sec * 1000000000 + ts.tv_nsec; +} + +nstime_t sys_timestamp (void) +{ + struct timespec ts; + clock_gettime (CLOCK_REALTIME, &ts); + return timespec_as_nanoseconds (ts); +} diff --git a/nstime.h b/nstime.h new file mode 100644 index 0000000..91c0d9e --- /dev/null +++ b/nstime.h @@ -0,0 +1,12 @@ +/* nanosecond time module */ + +#ifndef __NSTIME_H__ +#define __NSTIME_H__ + +#include "inttypes.h" + +typedef int64_t nstime_t; + +nstime_t sys_timestamp (void); + +#endif /* __NSTIME_H__ */ diff --git a/stat.c b/stat.c index 72fa6bf..4e5850a 100644 --- a/stat.c +++ b/stat.c @@ -1,17 +1,18 @@ -/* Statistic module */ +/* statistic module */ #include +#include #include #include #include -#include "mstime.h" - #include "stat.h" -void compute_statistics (mstime_t *points, int nb, int bins) +void compute_statistics (int64_t *points, int nb, int bins) { + printf ("Nb of points = %d\n", nb); + /* compute average, minimum and maximum */ double avg = 0; @@ -49,7 +50,7 @@ void compute_statistics (mstime_t *points, int nb, int bins) int change_done = 0; for (int i = 1; i < nb - 1; i++) { if (points[i + 1] < points[i]) { - mstime_t tmp = points[i]; + int64_t tmp = points[i]; points[i] = points[i + 1]; points[i + 1] = tmp; change_done = 1; @@ -82,15 +83,15 @@ void compute_statistics (mstime_t *points, int nb, int bins) assert (hist); double gap = (max - min) / bins; - for (int i = 0; i < nb; i++) { - hist[(int)((points[i] - min) / gap)]++; + for (int i = 0; i < nb - 1; i++) { + hist[(int)((points[i] - min) * bins / (max -min))]++; } + hist[bins - 1]++; printf ("Histogram\n"); - for (int i = 0; i < bins - 1; i++) { + for (int i = 0; i < bins; i++) { printf (" [%.2lf - %.2lf] = %d\n", min + i * gap, min + (i + 1) * gap, hist[i]); } - printf (" [%.2lf - %.2lf] = %d\n", min + (bins - 1) * gap, max, hist[bins - 1]); free (hist); diff --git a/stat.h b/stat.h index 4ef040f..bb51d07 100644 --- a/stat.h +++ b/stat.h @@ -1,10 +1,10 @@ -/* Statistic module */ +/* statistic module */ #ifndef __STAT_H__ #define __STAT_H__ -#include "mstime.h" +#include -void compute_statistics (mstime_t *points, int nb, int bins); +void compute_statistics (int64_t *points, int nb, int bins); #endif /* __STAT_H__ */ -- 2.30.2