From de894f523519b1892dd8448dbc90cb70b2f7869c Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Mon, 6 Oct 2025 23:23:23 +0200 Subject: [PATCH] wip on mq latency test --- mq_lat.c | 36 +++++++++++++++++++----------------- pipe_lat.c | 4 +++- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/mq_lat.c b/mq_lat.c index f787397..4d62cbf 100644 --- a/mq_lat.c +++ b/mq_lat.c @@ -28,18 +28,17 @@ int (*parse_arg_ext) (char *) = NULL; #define MSGLEN 128 #define MAXBUF 1024 -#define TIMER 1000 +#define TIMER 10000 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; ts_t ts1, ts2; -int try = 0; int rc = 0; void *ping (__attribute__((unused)) void *arg) { /* open mq */ - + struct mq_attr attr; attr.mq_flags = 0; attr.mq_maxmsg = 5; @@ -60,20 +59,21 @@ void *ping (__attribute__((unused)) void *arg) pthread_mutex_unlock (&mutex); usleep (TIMER); - while (try < nb_measurements) { + for (int i = 0; i < nb_measurements; i++) { + //char *msg = "ping"; char *msg = get_msg (MSGLEN); pthread_mutex_lock (&mutex); pthread_mutex_unlock (&mutex); sys_timestamp (&ts1); - if (mq_send (mq, msg, MSGLEN, 0) == -1) { - fprintf (stderr, "ping error: mq_send (%d)\n", try); + if (mq_send (mq, msg, strlen (msg) + 1, 0) == -1) { + fprintf (stderr, "ping error: mq_send (%d)\n", i); rc = 1; pthread_exit (NULL); } - usleep (TIMER); + usleep (TIMER); } mq_close (mq); @@ -84,10 +84,8 @@ void *ping (__attribute__((unused)) void *arg) void *pong (__attribute__((unused)) void *arg) { - printf ("Responding pong...\n"); - /* open mq */ - + struct mq_attr attr; attr.mq_flags = 0; attr.mq_maxmsg = 5; @@ -96,28 +94,30 @@ void *pong (__attribute__((unused)) void *arg) mqd_t mq = mq_open ("/test_queue", O_CREAT | O_RDWR, 0644, &attr); if (mq == -1) { - fprintf (stderr, "pong: mq_open\n"); + fprintf (stderr, "ping: mq_open\n"); rc = 1; pthread_exit (NULL); } /* main loop */ - while (try < nb_measurements) { + printf ("Responding pong...\n"); + + for (int i = 0; i < nb_measurements; i++) { pthread_mutex_unlock (&mutex); char buffer[MAXBUF] = { 0 }; - if (mq_receive (mq, buffer, sizeof(buffer) - 1, NULL) == -1) { - fprintf (stderr, "pong error: mq_receive (%d)\n", try); + if (mq_receive (mq, buffer, sizeof (buffer) - 1, NULL) == -1) { + fprintf (stderr, "pong error: mq_receive (%d)\n", i); rc = 1; pthread_exit (NULL); } sys_timestamp (&ts2); - deltas[try++] = diff_timestamp (&ts2, &ts1); - + deltas[i] = diff_timestamp (&ts2, &ts1); + pthread_mutex_lock (&mutex); - usleep (TIMER); + usleep (TIMER); } mq_close (mq); @@ -154,6 +154,8 @@ int test (dts_t *buffer, int nb) pthread_join (tid1, NULL); pthread_join (tid2, NULL); + mq_unlink ("/test_queue"); + pthread_mutex_destroy (&mutex); return rc; diff --git a/pipe_lat.c b/pipe_lat.c index 016e770..3d7aae0 100644 --- a/pipe_lat.c +++ b/pipe_lat.c @@ -106,13 +106,15 @@ int test (dts_t *buffer, int nb) deltas = buffer; nb_measurements = nb; - /* pipe test */ + /* open pipe */ if (pipe (pipefd) == -1) { fprintf (stderr, "error: pipe\n"); return 1; } + /* pipe test */ + pthread_mutex_lock (&mutex); pthread_t tid1; -- 2.30.2