From: Laurent MAZET Date: Fri, 3 Oct 2025 14:56:14 +0000 (+0200) Subject: rework test for mq latency X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=b478d3892c61987f099bd22a810998371535b46f;p=benchmarks.git rework test for mq latency --- diff --git a/mq_lat.c b/mq_lat.c index 08a1fed..15aa7b2 100644 --- a/mq_lat.c +++ b/mq_lat.c @@ -1,6 +1,6 @@ /* depend: */ /* cflags: */ -/* linker: mtime.o test.o stat.o -lm -lrt */ +/* linker: mtime.o test.o stat.o -lm -lpthread -lrt */ #include #include @@ -26,9 +26,11 @@ void (*usage_ext) (FILE *) = NULL; int (*parse_arg_ext) (char *) = NULL; #define MAXBUF 1024 +#define TIMER 1000 -ts_t ts1; -ts_t ts2; +pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; +ts_t ts1, ts2; +int try = 0; int rc = 0; void *ping (__attribute__((unused)) void *arg) @@ -46,42 +48,34 @@ void *ping (__attribute__((unused)) void *arg) if (mq == -1) { fprintf (stderr, "ping: mq_open\n"); rc = 1; - return NULL; + pthread_exit (NULL); } - usleep (10 * 1000); - /* main loop */ printf ("Sending ping...\n"); - for (int i = 0; i < nb_measurements / 2; i++) { - char buffer[MAXBUF] = { 0 }; - sprintf (buffer, "ping %d", i); + pthread_mutex_unlock (&mutex); + usleep (TIMER); - sys_timestamp (&ts1); + while (try < nb_measurements) { - if (mq_send (mq, buffer, strlen (buffer) + 1, 0) == -1) { - fprintf (stderr, "ping error: mq_send (%d)\n", i); - rc = 1; - return NULL; - } - //printf ("send '%s'\n", buffer); + pthread_mutex_lock (&mutex); + pthread_mutex_unlock (&mutex); - if (mq_receive (mq, buffer, sizeof(buffer), NULL) == -1) { - fprintf (stderr, "ping error: mq_receive (%d)\n", i); + sys_timestamp (&ts1); + char *buffer = "ping"; + if (mq_send (mq, buffer, strlen (buffer) + 1, 0) == -1) { + fprintf (stderr, "ping error: mq_send (%d)\n", try); rc = 1; - return NULL; + pthread_exit (NULL); } - //printf ("receive '%s'\n", buffer); - - sys_timestamp (&ts2); - deltas[2 * i + 1] = diff_timestamp (&ts2, &ts1); + usleep (TIMER); } mq_close (mq); - return 0; + pthread_exit (NULL); } void *pong (__attribute__((unused)) void *arg) @@ -101,38 +95,31 @@ void *pong (__attribute__((unused)) void *arg) if (mq == -1) { fprintf (stderr, "pong: mq_open\n"); rc = 1; - return NULL; + pthread_exit (NULL); } /* main loop */ - for (int i = 0; i < nb_measurements / 2; i++) { + while (try < nb_measurements) { - char buffer[MAXBUF] = { 0 }; + pthread_mutex_unlock (&mutex); + char buffer[MAXBUF] = { 0 }; if (mq_receive (mq, buffer, sizeof(buffer), NULL) == -1) { - fprintf (stderr, "pong error: mq_receive (%d)\n", i); + fprintf (stderr, "pong error: mq_receive (%d)\n", try); rc = 1; - return NULL; + pthread_exit (NULL); } - //printf ("receive '%s'\n", buffer); - sys_timestamp (&ts2); - deltas[2 * i] = diff_timestamp (&ts2, &ts1); - - sys_timestamp (&ts1); - - if (mq_send (mq, buffer, strlen (buffer) + 1, 0) == -1) { - fprintf (stderr, "pong error: mq_send (%d)\n", i); - rc = 1; - return NULL; - } - //printf ("send '%s'\n", buffer); + deltas[try++] = diff_timestamp (&ts2, &ts1); + + pthread_mutex_lock (&mutex); + usleep (TIMER); } mq_close (mq); - return NULL; + pthread_exit (NULL); } int test (dts_t *buffer, int nb) @@ -145,12 +132,16 @@ int test (dts_t *buffer, int nb) /* mq latency test */ + pthread_mutex_lock (&mutex); + pthread_t tid1; if (pthread_create (&tid1, NULL, ping, NULL) != 0) { fprintf (stderr, "error on pthread_create\n"); return 1; } + pthread_mutex_lock (&mutex); + pthread_t tid2; if (pthread_create (&tid2, NULL, pong, NULL) != 0) { fprintf (stderr, "error on pthread_create\n"); @@ -159,6 +150,8 @@ int test (dts_t *buffer, int nb) pthread_join (tid1, NULL); pthread_join (tid2, NULL); - + + pthread_mutex_destroy (&mutex); + return rc; }