wip on mq latency test
authorLaurent Mazet <mazet@softndesign.org>
Mon, 6 Oct 2025 21:23:23 +0000 (23:23 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Mon, 6 Oct 2025 21:23:23 +0000 (23:23 +0200)
mq_lat.c
pipe_lat.c

index f7873976131a5f560046fce506fe0b9ed654e211..4d62cbfc89239c252bd6a2d8a756a4536eeee329 100644 (file)
--- 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;
index 016e770fbf22bac6597405c68f3df20ec23bcde3..3d7aae0df55c2d380dc71aa48d3285049bfecd52 100644 (file)
@@ -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;