cleaning test framework
authorLaurent Mazet <mazet@softndesign.org>
Sat, 27 Sep 2025 20:12:50 +0000 (22:12 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sat, 27 Sep 2025 21:20:17 +0000 (23:20 +0200)
13 files changed:
mstime.c [deleted file]
mstime.h [deleted file]
mtime.c [new file with mode: 0644]
mtime.h [new file with mode: 0644]
mustime.c [deleted file]
mustime.h [deleted file]
mutex.c
nstime.c [deleted file]
nstime.h [deleted file]
semaphore.c
test.c
test.h
thread.c

diff --git a/mstime.c b/mstime.c
deleted file mode 100644 (file)
index d92d0eb..0000000
--- a/mstime.c
+++ /dev/null
@@ -1,15 +0,0 @@
-#include <time.h>
-
-#include "mstime.h"
-
-mstime_t timespec_as_milliseconds (struct timespec ts)
-{
-    return ts.tv_sec * 1000 + ts.tv_nsec / 10000000 ;
-}
-
-mstime_t sys_timestamp (void)
-{
-    struct timespec ts;
-    clock_gettime (CLOCK_REALTIME, &ts);
-    return timespec_as_milliseconds (ts);
-}
diff --git a/mstime.h b/mstime.h
deleted file mode 100644 (file)
index 4c46aa3..0000000
--- a/mstime.h
+++ /dev/null
@@ -1,12 +0,0 @@
-/* millisecond time module */
-
-#ifndef __MSTIME_H__
-#define __MSTIME_H__
-
-#include "inttypes.h"
-
-typedef int64_t mstime_t;
-
-mstime_t sys_timestamp (void);
-
-#endif /* __MSTIME_H__ */
diff --git a/mtime.c b/mtime.c
new file mode 100644 (file)
index 0000000..f895c4c
--- /dev/null
+++ b/mtime.c
@@ -0,0 +1,24 @@
+#include <time.h>
+
+#include "mtime.h"
+
+mstime_t sys_timestamp_ms (void)
+{
+    struct timespec ts;
+    clock_gettime (CLOCK_REALTIME, &ts);
+    return ts.tv_sec * 1000 + ts.tv_nsec / 10000000 ;
+}
+
+ustime_t sys_timestamp_us (void)
+{
+    struct timespec ts;
+    clock_gettime (CLOCK_REALTIME, &ts);
+    return ts.tv_sec * 1000000 + ts.tv_nsec / 1000 ;
+}
+
+nstime_t sys_timestamp_ns (void)
+{
+    struct timespec ts;
+    clock_gettime (CLOCK_REALTIME, &ts);
+    return ts.tv_sec * 1000000000 + ts.tv_nsec;
+}
diff --git a/mtime.h b/mtime.h
new file mode 100644 (file)
index 0000000..b90fda7
--- /dev/null
+++ b/mtime.h
@@ -0,0 +1,20 @@
+/* machine time module */
+
+#ifndef __MTIME_H__
+#define __MTIME_H__
+
+#include "inttypes.h"
+
+typedef int64_t mstime_t;
+
+mstime_t sys_timestamp_ms (void);
+
+typedef int64_t ustime_t;
+
+ustime_t sys_timestamp_us (void);
+
+typedef int64_t nstime_t;
+
+nstime_t sys_timestamp_ns (void);
+
+#endif /* __MTIME_H__ */
diff --git a/mustime.c b/mustime.c
deleted file mode 100644 (file)
index 0983278..0000000
--- a/mustime.c
+++ /dev/null
@@ -1,15 +0,0 @@
-#include <time.h>
-
-#include "mustime.h"
-
-mustime_t timespec_as_microseconds (struct timespec ts)
-{
-    return ts.tv_sec * 1000000 + ts.tv_nsec / 1000 ;
-}
-
-mustime_t sys_timestamp (void)
-{
-    struct timespec ts;
-    clock_gettime (CLOCK_REALTIME, &ts);
-    return timespec_as_microseconds (ts);
-}
diff --git a/mustime.h b/mustime.h
deleted file mode 100644 (file)
index 070790d..0000000
--- a/mustime.h
+++ /dev/null
@@ -1,12 +0,0 @@
-/* microsecond time module */
-
-#ifndef __MUSTIME_H__
-#define __MUSTIME_H__
-
-#include "inttypes.h"
-
-typedef int64_t mustime_t;
-
-mustime_t sys_timestamp (void);
-
-#endif /* __MUSTIME_H__ */
diff --git a/mutex.c b/mutex.c
index 2afddc3b89befdfc8a8d1cb8008a3e7b23c838ed..937937082400d6be474eaf2a67cd352c8c3256be 100644 (file)
--- a/mutex.c
+++ b/mutex.c
@@ -1,12 +1,11 @@
 /* depend: */
 /* cflags: */
-/* linker: nstime.o test.o stat.o -lm -lpthread */
+/* linker: mtime.o test.o stat.o -lm -lpthread */
 
 #include <pthread.h>
 #include <stdio.h>
 
-#include "nstime.h"
-#include "stat.h"
+#include "mtime.h"
 #include "test.h"
 
 /* global variables */
@@ -14,6 +13,8 @@
 nstime_t *deltas = NULL;
 int nb_measurements = 0;
 
+char *message = "mutex";
+
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
 int shared_flag = 0;
 nstime_t timestamp1, timestamp2;
@@ -24,7 +25,7 @@ void *mutex_giver_task (__attribute__((unused)) void *arg)
     while (try < nb_measurements) {
         pthread_mutex_lock (&mutex);
         shared_flag = 1;
-        timestamp1 = sys_timestamp (); 
+        timestamp1 = sys_timestamp_ns (); 
         pthread_mutex_unlock (&mutex);
     }
     pthread_exit (NULL);
@@ -36,7 +37,7 @@ void *mutex_taker_task (__attribute__((unused)) void *arg)
     while (try < nb_measurements) {
         pthread_mutex_lock (&mutex);
         if (shared_flag) {
-            timestamp2 = sys_timestamp ();
+            timestamp2 = sys_timestamp_ns ();
             deltas[try++] = timestamp2 - timestamp1;
             shared_flag = 0;
         }
@@ -54,7 +55,7 @@ sec_t test (int64_t *buffer, int nb)
     deltas = buffer;
     nb_measurements = nb;
 
-    /* main process */
+    /* mutex test */
 
     pthread_t giver_thread, taker_thread;
     pthread_attr_t attr;
@@ -70,12 +71,12 @@ sec_t test (int64_t *buffer, int nb)
     
     if (pthread_create (&giver_thread, &attr, mutex_giver_task, NULL) != 0) {
         fprintf (stderr, "error on pthread_create for giver_task\n");
-        return 1;
+        return none_e;
     }
 
     if (pthread_create (&taker_thread, &attr, mutex_taker_task, NULL) != 0) {
         fprintf (stderr, "error on pthread_create for taker_task\n");
-        return 1;
+        return none_e;
     }
 
     pthread_join (giver_thread, NULL);
diff --git a/nstime.c b/nstime.c
deleted file mode 100644 (file)
index 774d57e..0000000
--- a/nstime.c
+++ /dev/null
@@ -1,15 +0,0 @@
-#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
deleted file mode 100644 (file)
index 91c0d9e..0000000
--- a/nstime.h
+++ /dev/null
@@ -1,12 +0,0 @@
-/* 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__ */
index 4a7e331ea544bb146a4e818c2b065ee2f941c605..044fb8c91ba4790bcef1ad81d1a97b867d0111ab 100644 (file)
@@ -1,22 +1,26 @@
 /* depend: */
 /* cflags: */
-/* linker: mustime.o test.o stat.o -lm -lpthread */
+/* linker: mtime.o test.o stat.o -lm -lpthread */
 
 #include <pthread.h>
 #include <semaphore.h>
+#include <stdio.h>
 
-#include "mustime.h"
-#include "stat.h"
+#include "mtime.h"
 #include "test.h"
 
 #define NT 2
 
-mustime_t *measure_tab;
+/* global variables */
+
+ustime_t *measure_tab = NULL;
+
+char *message = "semaphore";
 
 int counter = 0;
 int try = 0;
 sem_t sem;
-mustime_t ts1, ts2;
+ustime_t ts1, ts2;
 
 void* trythis (__attribute__((unused)) void *arg)
 {
@@ -26,12 +30,12 @@ void* trythis (__attribute__((unused)) void *arg)
 
     /* thread 1 took the lock -> timestamp 1 */
     if (counter % 2 != 0) {
-        ts1 = sys_timestamp ();
+        ts1 = sys_timestamp_us ();
     }
   
     /* thread 2 took the lock -> timestamp 2 */
     else {
-        ts2 = sys_timestamp ();
+        ts2 = sys_timestamp_us ();
         measure_tab[try++] = ts2 - ts1;
     }
 
@@ -47,13 +51,21 @@ sec_t test (int64_t *buffer, int nb)
 
     measure_tab = buffer;
 
-    /* main process */
+    /* semaphore test */
+
     sem_init (&sem, 0, 1);
 
     for (int i = 0; i < nb; i++) {
         pthread_t tid1, tid2;
-        pthread_create (&tid1, NULL, trythis, NULL);
-        pthread_create (&tid2, NULL, trythis, NULL);
+        if (pthread_create (&tid1, NULL, trythis, NULL) != 0) {
+            fprintf (stderr, "error on pthread_create\n");
+            return none_e;
+        }
+        
+        if (pthread_create (&tid2, NULL, trythis, NULL) != 0) {
+            fprintf (stderr, "error on pthread_create\n");
+            return none_e;
+        }
 
         pthread_join (tid1, NULL);
         pthread_join (tid2, NULL);
@@ -61,5 +73,5 @@ sec_t test (int64_t *buffer, int nb)
 
     sem_destroy (&sem);
 
-    return musec_e;
+    return usec_e;
 }
diff --git a/test.c b/test.c
index b67524f79dd169ec069690cf4bf8eea323811c4b..4c10ee97bd604efb7da81c03eb8aead8b42f8e18 100644 (file)
--- a/test.c
+++ b/test.c
@@ -17,6 +17,8 @@ int do_stat = 0;
 int hist_bin = 10;
 char *output = NULL;
 
+extern char *message;
+
 /* usage function */
 
 int usage (int ret)
@@ -99,8 +101,17 @@ int main (int argc, char *argv[])
     int64_t *buffer = (int64_t *) calloc (nb, sizeof (int64_t));
     assert (buffer);
 
+    printf ("Test: %s\n", (message) ? message : "unknown");
+
     sec_t sec_unit = test (buffer, nb);
 
+    if (sec_unit == none_e) {
+        printf ("\033[1;31mKO\033[0;0m\n");
+        exit (1);
+    } else {
+        printf ("\033[1;32mOK\033[0;0m\n");
+    }
+
     if (output) {
         FILE *fd = fopen (output, "w");
         assert (fd);
@@ -115,7 +126,7 @@ int main (int argc, char *argv[])
         switch (sec_unit) {
         case sec_e : strcpy (unit, "second"); break;
         case msec_e : strcpy (unit, "millisecond"); break;
-        case musec_e : strcpy (unit, "microsecond"); break;
+        case usec_e : strcpy (unit, "microsecond"); break;
         case nsec_e : strcpy (unit, "nanosecond"); break;
         default: strcpy (unit, "???"); break;
         }
diff --git a/test.h b/test.h
index 6fb35aff34754250345e9d50d139df4457ffd278..18b0fcc34534c23921c3306381075ede40683354 100644 (file)
--- a/test.h
+++ b/test.h
@@ -6,9 +6,10 @@
 #include <inttypes.h>
 
 typedef enum {
-    sec_e = 0,
+    none_e = 0,
+    sec_e,
     msec_e,
-    musec_e,
+    usec_e,
     nsec_e
 } sec_t;
 
index 70306a7ed3ebc244d6ad70ac08363878e07fa979..53142fed82d7e77c85747204bb58c42bede4757e 100644 (file)
--- a/thread.c
+++ b/thread.c
@@ -1,19 +1,20 @@
 /* depend: */
 /* cflags: */
-/* linker: mustime.o test.o stat.o -lm -lpthread */
+/* linker: mtime.o test.o stat.o -lm -lpthread */
 
 #include <pthread.h>
 #include <stdio.h>
 
-#include "mustime.h"
-#include "stat.h"
+#include "mtime.h"
 #include "test.h"
 
 /* global variables */
 
-mustime_t *deltas = NULL;
+ustime_t *deltas = NULL;
 int nb_measurements = 0;
 
+char *message = "thread";
+
 void* dummy_thread(void *arg) {
     (void)arg;
     return NULL;
@@ -27,29 +28,35 @@ sec_t test (int64_t *buffer, int nb)
     deltas = buffer;
     nb_measurements = nb;
 
+    /* thread test */
+
     pthread_t posix_t;
     pthread_attr_t attr;
     struct sched_param param;
 
-    pthread_attr_init(&attr);
-    pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
-    param.sched_priority = sched_get_priority_max(SCHED_FIFO);
-    pthread_attr_setschedparam(&attr, &param);
-    pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
+    pthread_attr_init (&attr);
+    pthread_attr_setschedpolicy (&attr, SCHED_FIFO);
+    param.sched_priority = sched_get_priority_max (SCHED_FIFO);
+    pthread_attr_setschedparam (&attr, &param);
+    pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED);
 
     for (int i = 0; i < nb_measurements; i++) {
 
-        mustime_t timestamp1 = sys_timestamp (); 
+        ustime_t timestamp1 = sys_timestamp_us (); 
+
+        if (pthread_create(&posix_t, &attr, dummy_thread, NULL) != 0) {
+            fprintf (stderr, "error on pthread_create\n");
+            return none_e;
+        }
 
-        pthread_create(&posix_t, &attr, dummy_thread, NULL);
         pthread_join(posix_t, NULL);
 
-        mustime_t timestamp2 = sys_timestamp (); 
+        ustime_t timestamp2 = sys_timestamp_us (); 
 
         deltas[i++] = timestamp2 - timestamp1;
     }
 
     pthread_attr_destroy(&attr);
 
-    return musec_e;
+    return usec_e;
 }