correct timer units
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Fri, 26 Sep 2025 17:19:13 +0000 (19:19 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Fri, 26 Sep 2025 17:19:13 +0000 (19:19 +0200)
mstime.c
mstime.h
mutex.c
nstime.c [new file with mode: 0644]
nstime.h [new file with mode: 0644]
stat.c
stat.h

index 161b68e91ef1b70abf9f491a82a62caf93ea27e3..e9065b75eb22bf4e9b20835547ce9d38a842d778 100644 (file)
--- 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);
 }
index 959c40a088fd89fe94b58ff4083f88c611b112bf..b007ffd1304597cb713048395700eb11f53efcb0 100644 (file)
--- 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 10b85ef944e89210381f2b7a4ad6146d27d43852..97cf3bcffef9a1770bbaf8676eaa33a1595b5401 100644 (file)
--- 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 <assert.h>
 #include <inttypes.h>
@@ -10,7 +10,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 
-#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 (file)
index 0000000..774d57e
--- /dev/null
+++ b/nstime.c
@@ -0,0 +1,15 @@
+#include <time.h>
+
+#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 (file)
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 72fa6bf3d84f93024ae98cf0bec4205957388534..4e5850ad08d27206c630027e3725c18ae3211e38 100644 (file)
--- a/stat.c
+++ b/stat.c
@@ -1,17 +1,18 @@
-/* Statistic module */
+/* statistic module */
 
 #include <assert.h>
+#include <inttypes.h>
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 
-#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 4ef040f2c7429cc44325f4299c97cd4d1928dbcb..bb51d07ab4eb826701288cabbdf4e338981c5ed1 100644 (file)
--- a/stat.h
+++ b/stat.h
@@ -1,10 +1,10 @@
-/* Statistic module */
+/* statistic module */
 
 #ifndef __STAT_H__
 #define __STAT_H__
 
-#include "mstime.h"
+#include <inttypes.h>
 
-void compute_statistics (mstime_t *points, int nb, int bins);
+void compute_statistics (int64_t *points, int nb, int bins);
 
 #endif /* __STAT_H__ */