rework on pipe latency tets
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Fri, 3 Oct 2025 15:10:25 +0000 (17:10 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Fri, 3 Oct 2025 15:10:25 +0000 (17:10 +0200)
pipe_lat.c

index 438ff0018f2217e69c401aa905a868ae6bc929e9..b4137b15d7c298f63b05e7fbe2cc3b7418e358b6 100644 (file)
@@ -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 <stdio.h>
 #include <stdlib.h>
@@ -22,13 +22,12 @@ void (*usage_ext) (FILE *) = NULL;
 int (*parse_arg_ext) (char *) = NULL;
 
 #define MAXBUF 1024
-
-ts_t ts1;
-ts_t ts2;
-int rc = 0;
-int pipefd[2] = { 0 };
+#define TIMER 100
 
 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+ts_t ts1, ts2;
+int pipefd[2] = { 0 };
+int rc = 0;
 
 void *ping (__attribute__((unused)) void *arg)
 {
@@ -36,39 +35,34 @@ void *ping (__attribute__((unused)) void *arg)
     int fdin = dup (pipefd[0]);
     int fdout = dup (pipefd[1]);
 
-    pthread_mutex_lock (&mutex);
-    pthread_mutex_unlock (&mutex);
+    /* main loop */
 
     printf ("Sending ping...\n");
+
+    pthread_mutex_unlock (&mutex);
+    usleep (TIMER);
+
     for (int i = 0; i < nb_measurements; i++) {
 
         char buffer[MAXBUF] = { 0 };
         sprintf (buffer, "ping %d", i);
 
-        sys_timestamp (&ts1);
+        pthread_mutex_lock (&mutex);
+        pthread_mutex_unlock (&mutex);
 
+        sys_timestamp (&ts1);
         if (write (fdout, buffer, strlen (buffer) + 1) == -1) {
             fprintf (stderr, "ping error: write (%d)\n", i);
             rc = 1;
-            return NULL;
-        }
-        //printf ("write '%s'\n", buffer);
-
-        if (read (fdin, buffer, sizeof(buffer)) == -1) {
-            fprintf (stderr, "ping error: read (%d)\n", i);
-            rc = 1;
-            return NULL;
+            pthread_exit (NULL);
         }
-        //printf ("read '%s'\n", buffer);
-
-        sys_timestamp (&ts2);
-        deltas[i] = diff_timestamp (&ts2, &ts1);
+        usleep (TIMER);
     }
 
     close (fdin);
     close (fdout);
 
-    return NULL;
+    pthread_exit (NULL);
 }
 
 void *pong (__attribute__((unused)) void *arg)
@@ -77,37 +71,30 @@ void *pong (__attribute__((unused)) void *arg)
     int fdin = dup (pipefd[0]);
     int fdout = dup (pipefd[1]);
 
-    pthread_mutex_unlock (&mutex);
-
     printf ("Responding pong...\n");
+
     for (int i = 0; i < nb_measurements; i++) {
 
-        char buffer[MAXBUF] = { 0 };
+        pthread_mutex_unlock (&mutex);
 
+        char buffer[MAXBUF] = { 0 };
         if (read (fdin, buffer, sizeof(buffer)) == -1) {
             fprintf (stderr, "ping error: read (%d)\n", i);
             rc = 1;
-            return NULL;
+            pthread_exit (NULL);
         }
-        //printf ("read '%s'\n", buffer);
 
         sys_timestamp (&ts2);
         deltas[i] = diff_timestamp (&ts2, &ts1);
 
-        sys_timestamp (&ts1);
-
-        if (write (fdout, buffer, strlen (buffer) + 1) == -1) {
-            fprintf (stderr, "ping error: write (%d)\n", i);
-            rc = 1;
-            return NULL;
-        }
-        //printf ("write '%s'\n", buffer);
+        pthread_mutex_lock (&mutex);
+        usleep (TIMER);
     }
 
     close (fdin);
     close (fdout);
 
-    return NULL;
+    pthread_exit (NULL);
 }
 
 int test (dts_t *buffer, int nb)
@@ -133,6 +120,8 @@ int test (dts_t *buffer, int nb)
         return 1;
     }
 
+    pthread_mutex_lock (&mutex);
+
     pthread_t tid2;
     if (pthread_create (&tid2, NULL, pong, NULL) != 0) {
         fprintf (stderr, "error on pthread_create\n");