From 07e698c14232348ff56d29f8f14f689e7025e7ef Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Sat, 27 Sep 2025 22:12:50 +0200 Subject: [PATCH] cleaning test framework --- mstime.c | 15 --------------- mstime.h | 12 ------------ mtime.c | 24 ++++++++++++++++++++++++ mtime.h | 20 ++++++++++++++++++++ mustime.c | 15 --------------- mustime.h | 12 ------------ mutex.c | 17 +++++++++-------- nstime.c | 15 --------------- nstime.h | 12 ------------ semaphore.c | 34 +++++++++++++++++++++++----------- test.c | 13 ++++++++++++- test.h | 5 +++-- thread.c | 33 ++++++++++++++++++++------------- 13 files changed, 111 insertions(+), 116 deletions(-) delete mode 100644 mstime.c delete mode 100644 mstime.h create mode 100644 mtime.c create mode 100644 mtime.h delete mode 100644 mustime.c delete mode 100644 mustime.h delete mode 100644 nstime.c delete mode 100644 nstime.h diff --git a/mstime.c b/mstime.c deleted file mode 100644 index d92d0eb..0000000 --- a/mstime.c +++ /dev/null @@ -1,15 +0,0 @@ -#include - -#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 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 index 0000000..f895c4c --- /dev/null +++ b/mtime.c @@ -0,0 +1,24 @@ +#include + +#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 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 index 0983278..0000000 --- a/mustime.c +++ /dev/null @@ -1,15 +0,0 @@ -#include - -#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 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 2afddc3..9379370 100644 --- 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 #include -#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 index 774d57e..0000000 --- a/nstime.c +++ /dev/null @@ -1,15 +0,0 @@ -#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 deleted file mode 100644 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__ */ diff --git a/semaphore.c b/semaphore.c index 4a7e331..044fb8c 100644 --- a/semaphore.c +++ b/semaphore.c @@ -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 #include +#include -#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 b67524f..4c10ee9 100644 --- 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 6fb35af..18b0fcc 100644 --- a/test.h +++ b/test.h @@ -6,9 +6,10 @@ #include typedef enum { - sec_e = 0, + none_e = 0, + sec_e, msec_e, - musec_e, + usec_e, nsec_e } sec_t; diff --git a/thread.c b/thread.c index 70306a7..53142fe 100644 --- 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 #include -#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, ¶m); - 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, ¶m); + 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; } -- 2.30.2