From 64ca4de9ea82e7ab6caeda46e6862de69787fe66 Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Mon, 29 Sep 2025 11:54:16 +0200 Subject: [PATCH] clean shedulater and timestamp management --- mq.c | 38 +++++++++++++------------------------- mtime.c | 20 ++++++++------------ mtime.h | 14 ++++++++------ mutex.c | 28 ++++++++++------------------ pipe.c | 28 ++++++++-------------------- semaphore.c | 22 ++++++++++++---------- test.c | 21 ++++++++++++++++++++- thread.c | 29 ++++++++++------------------- 8 files changed, 89 insertions(+), 111 deletions(-) diff --git a/mq.c b/mq.c index 7e07f89..b29fd90 100644 --- a/mq.c +++ b/mq.c @@ -6,8 +6,6 @@ #include #include -//#include - #include /* For O_* constants */ #include /* For mode constants */ #include @@ -34,11 +32,10 @@ sec_t receiver (void) { printf ("Waiting for data...\n"); - nstime_t ts = 0, ts_old; for (int i = 0; i < nb_measurements; i++) { - ts_old = ts; - ts = sys_timestamp_ns(); - + mtime_t ts1; + sys_timestamp (&ts1); + char buffer[MAXBUF] = { 0 }; int m = mq_receive (mq, buffer, sizeof(buffer), NULL); if (m == -1) { @@ -47,10 +44,12 @@ sec_t receiver (void) } //printf ("receive '%s'\n", buffer); - deltas[i] = ts - ts_old; + mtime_t ts2; + sys_timestamp (&ts2); + deltas[i] = diff_timestamp (&ts2, &ts1); } - return usec_e; + return nsec_e; } sec_t sender (void) @@ -83,24 +82,13 @@ sec_t test (int64_t *buffer, int nb) /* mutex test */ -/* - pthread_attr_t th_attr; - struct sched_param param; - - pthread_attr_init (&th_attr); - pthread_attr_setschedpolicy (&th_attr, SCHED_FIFO); - param.sched_priority = sched_get_priority_max (SCHED_FIFO); - pthread_attr_setschedparam (&th_attr, ¶m); - pthread_attr_setinheritsched (&th_attr, PTHREAD_EXPLICIT_SCHED); -*/ - - struct mq_attr mq_attr; - mq_attr.mq_flags = 0; - mq_attr.mq_maxmsg = 5; - mq_attr.mq_msgsize = MAXBUF; - mq_attr.mq_curmsgs = 0; + struct mq_attr attr; + attr.mq_flags = 0; + attr.mq_maxmsg = 5; + attr.mq_msgsize = MAXBUF; + attr.mq_curmsgs = 0; - mq = mq_open ("/test_queue", O_CREAT | O_RDWR, 0644, &mq_attr); + mq = mq_open ("/test_queue", O_CREAT | O_RDWR, 0644, &attr); if (mq == -1) { fprintf (stderr, "error: mq_open\n"); return none_e; diff --git a/mtime.c b/mtime.c index f895c4c..e4ceb8c 100644 --- a/mtime.c +++ b/mtime.c @@ -2,23 +2,19 @@ #include "mtime.h" -mstime_t sys_timestamp_ms (void) +void sys_timestamp (mtime_t *ts) { - struct timespec ts; - clock_gettime (CLOCK_REALTIME, &ts); - return ts.tv_sec * 1000 + ts.tv_nsec / 10000000 ; + clock_gettime (CLOCK_REALTIME, ts); } -ustime_t sys_timestamp_us (void) +unsigned int diff_timestamp (mtime_t *ts1, mtime_t *ts2) { - struct timespec ts; - clock_gettime (CLOCK_REALTIME, &ts); - return ts.tv_sec * 1000000 + ts.tv_nsec / 1000 ; + return (ts1->tv_sec - ts2->tv_sec) * 1000000000 + + ts1->tv_nsec - ts2->tv_nsec; } -nstime_t sys_timestamp_ns (void) +void set_timestamp (mtime_t *ts1, mtime_t *ts2) { - struct timespec ts; - clock_gettime (CLOCK_REALTIME, &ts); - return ts.tv_sec * 1000000000 + ts.tv_nsec; + ts1->tv_sec = ts2->tv_sec; + ts1->tv_nsec = ts2->tv_nsec; } diff --git a/mtime.h b/mtime.h index b90fda7..aedd6ff 100644 --- a/mtime.h +++ b/mtime.h @@ -5,16 +5,18 @@ #include "inttypes.h" -typedef int64_t mstime_t; +typedef unsigned int mstime_t; -mstime_t sys_timestamp_ms (void); +typedef unsigned int ustime_t; -typedef int64_t ustime_t; +typedef unsigned int nstime_t; -ustime_t sys_timestamp_us (void); +typedef struct timespec mtime_t; -typedef int64_t nstime_t; +void sys_timestamp (mtime_t *ts); -nstime_t sys_timestamp_ns (void); +unsigned int diff_timestamp (mtime_t *ts1, mtime_t *ts2); + +void set_timestamp (mtime_t *ts1, mtime_t *ts2); #endif /* __MTIME_H__ */ diff --git a/mutex.c b/mutex.c index 9379370..3701b74 100644 --- a/mutex.c +++ b/mutex.c @@ -1,6 +1,6 @@ /* depend: */ /* cflags: */ -/* linker: mtime.o test.o stat.o -lm -lpthread */ +/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */ #include #include @@ -17,7 +17,7 @@ char *message = "mutex"; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int shared_flag = 0; -nstime_t timestamp1, timestamp2; +mtime_t timestamp1, timestamp2; int try = 0; void *mutex_giver_task (__attribute__((unused)) void *arg) @@ -25,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_ns (); + sys_timestamp (×tamp1); pthread_mutex_unlock (&mutex); } pthread_exit (NULL); @@ -37,8 +37,8 @@ void *mutex_taker_task (__attribute__((unused)) void *arg) while (try < nb_measurements) { pthread_mutex_lock (&mutex); if (shared_flag) { - timestamp2 = sys_timestamp_ns (); - deltas[try++] = timestamp2 - timestamp1; + sys_timestamp (×tamp2); + deltas[try++] = diff_timestamp (×tamp2, ×tamp1); shared_flag = 0; } pthread_mutex_unlock (&mutex); @@ -57,24 +57,16 @@ sec_t test (int64_t *buffer, int nb) /* mutex test */ - pthread_t giver_thread, taker_thread; - 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); - shared_flag = 1; - - if (pthread_create (&giver_thread, &attr, mutex_giver_task, NULL) != 0) { + + pthread_t giver_thread; + if (pthread_create (&giver_thread, NULL, mutex_giver_task, NULL) != 0) { fprintf (stderr, "error on pthread_create for giver_task\n"); return none_e; } - if (pthread_create (&taker_thread, &attr, mutex_taker_task, NULL) != 0) { + pthread_t taker_thread; + if (pthread_create (&taker_thread, NULL, mutex_taker_task, NULL) != 0) { fprintf (stderr, "error on pthread_create for taker_task\n"); return none_e; } diff --git a/pipe.c b/pipe.c index 4abbf26..f1c7e00 100644 --- a/pipe.c +++ b/pipe.c @@ -1,13 +1,11 @@ /* depend: */ /* cflags: */ -/* linker: mtime.o test.o stat.o -lm */ +/* linker: mtime.o test.o stat.o -lm -lrt */ #include #include #include -//#include - #include #include #include @@ -28,11 +26,10 @@ sec_t receiver (int fd) { printf ("Waiting for data...\n"); - nstime_t ts = 0, ts_old; for (int i = 0; i < nb_measurements; i++) { - ts_old = ts; - ts = sys_timestamp_ns(); - + mtime_t ts1; + sys_timestamp (&ts1); + char buffer[MAXBUF] = { 0 }; if (read (fd, buffer, sizeof(buffer)) == -1) { fprintf (stderr, "error: read (%d)\n", i); @@ -40,7 +37,9 @@ sec_t receiver (int fd) } //printf ("receive '%s'\n", buffer); - deltas[i] = ts - ts_old; + mtime_t ts2; + sys_timestamp (&ts2); + deltas[i] = diff_timestamp (&ts2, &ts1); } return usec_e; @@ -73,18 +72,7 @@ sec_t test (int64_t *buffer, int nb) deltas = buffer; nb_measurements = nb; - /* mutex test */ - -/* - pthread_attr_t th_attr; - struct sched_param param; - - pthread_attr_init (&th_attr); - pthread_attr_setschedpolicy (&th_attr, SCHED_FIFO); - param.sched_priority = sched_get_priority_max (SCHED_FIFO); - pthread_attr_setschedparam (&th_attr, ¶m); - pthread_attr_setinheritsched (&th_attr, PTHREAD_EXPLICIT_SCHED); -*/ + /* pipe test */ int pipefd[2] = { 0 }; diff --git a/semaphore.c b/semaphore.c index 044fb8c..6c2fecc 100644 --- a/semaphore.c +++ b/semaphore.c @@ -1,6 +1,6 @@ /* depend: */ /* cflags: */ -/* linker: mtime.o test.o stat.o -lm -lpthread */ +/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */ #include #include @@ -13,14 +13,14 @@ /* global variables */ -ustime_t *measure_tab = NULL; +mstime_t *measure_tab = NULL; char *message = "semaphore"; int counter = 0; int try = 0; sem_t sem; -ustime_t ts1, ts2; +mtime_t ts1, ts2; void* trythis (__attribute__((unused)) void *arg) { @@ -30,13 +30,13 @@ void* trythis (__attribute__((unused)) void *arg) /* thread 1 took the lock -> timestamp 1 */ if (counter % 2 != 0) { - ts1 = sys_timestamp_us (); + sys_timestamp (&ts1); } - + /* thread 2 took the lock -> timestamp 2 */ else { - ts2 = sys_timestamp_us (); - measure_tab[try++] = ts2 - ts1; + sys_timestamp (&ts2); + measure_tab[try++] = diff_timestamp (&ts2, &ts1); } sem_post (&sem); @@ -56,12 +56,14 @@ sec_t test (int64_t *buffer, int nb) sem_init (&sem, 0, 1); for (int i = 0; i < nb; i++) { - pthread_t tid1, tid2; + + pthread_t tid1; if (pthread_create (&tid1, NULL, trythis, NULL) != 0) { fprintf (stderr, "error on pthread_create\n"); return none_e; } - + + pthread_t tid2; if (pthread_create (&tid2, NULL, trythis, NULL) != 0) { fprintf (stderr, "error on pthread_create\n"); return none_e; @@ -73,5 +75,5 @@ sec_t test (int64_t *buffer, int nb) sem_destroy (&sem); - return usec_e; + return msec_e; } diff --git a/test.c b/test.c index dce0ef4..d7847f5 100644 --- a/test.c +++ b/test.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -7,6 +8,10 @@ #include "stat.h" #include "test.h" +/* constants */ + +#define RT_PRIO 80 + /* static variables */ char *progname = NULL; @@ -116,6 +121,20 @@ int main (int argc, char *argv[]) } } + /* real-time process */ + struct sched_param param = {0}; + if (sched_getparam (0, ¶m) != 0) { + fprintf (stderr, "error: sched_getparam\n"); + return 1; + } + param.sched_priority = RT_PRIO; + int rc = sched_setscheduler (0, SCHED_FIFO, ¶m); /* non-preemptive */ + // int rc = sched_setscheduler (0, SCHED_RR, ¶m); /* preemptive */ + if (rc != 0) { + fprintf (stderr, "error: sched_setscheduler\n"); + return 1; + } + /* main process */ int64_t *buffer = (int64_t *) calloc (nb, sizeof (int64_t)); @@ -127,7 +146,7 @@ int main (int argc, char *argv[]) if (sec_unit == none_e) { printf ("\033[1;31mKO\033[0;0m\n"); - exit (1); + return 1; } else { printf ("\033[1;32mOK\033[0;0m\n"); } diff --git a/thread.c b/thread.c index 53142fe..2f6c172 100644 --- a/thread.c +++ b/thread.c @@ -1,6 +1,6 @@ /* depend: */ /* cflags: */ -/* linker: mtime.o test.o stat.o -lm -lpthread */ +/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */ #include #include @@ -10,7 +10,7 @@ /* global variables */ -ustime_t *deltas = NULL; +nstime_t *deltas = NULL; int nb_measurements = 0; char *message = "thread"; @@ -30,33 +30,24 @@ sec_t test (int64_t *buffer, int 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); - for (int i = 0; i < nb_measurements; i++) { - ustime_t timestamp1 = sys_timestamp_us (); + mtime_t ts1; + sys_timestamp (&ts1); - if (pthread_create(&posix_t, &attr, dummy_thread, NULL) != 0) { + pthread_t posix_t; + if (pthread_create(&posix_t, NULL, dummy_thread, NULL) != 0) { fprintf (stderr, "error on pthread_create\n"); return none_e; } pthread_join(posix_t, NULL); - ustime_t timestamp2 = sys_timestamp_us (); + mtime_t ts2; + sys_timestamp (&ts2); - deltas[i++] = timestamp2 - timestamp1; + deltas[i++] = diff_timestamp (&ts2, &ts1); } - pthread_attr_destroy(&attr); - - return usec_e; + return nsec_e; } -- 2.30.2