add latency test for pipe
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Thu, 2 Oct 2025 17:26:40 +0000 (19:26 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Thu, 2 Oct 2025 17:26:40 +0000 (19:26 +0200)
pipe.c [deleted file]
pipe_lat.c [new file with mode: 0644]
pipe_rtt.c [new file with mode: 0644]

diff --git a/pipe.c b/pipe.c
deleted file mode 100644 (file)
index 65684b0..0000000
--- a/pipe.c
+++ /dev/null
@@ -1,123 +0,0 @@
-/* depend: */
-/* cflags: */
-/* linker: mtime.o test.o stat.o -lm -lrt */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <signal.h>
-#include <sys/wait.h>
-#include <unistd.h>
-
-#include "mtime.h"
-#include "test.h"
-
-/* global variables */
-
-dts_t *deltas = NULL;
-int nb_measurements = 0;
-
-char *message = "pipe";
-void (*usage_ext) (FILE *) = NULL;
-int (*parse_arg_ext) (char *) = NULL;
-
-#define MAXBUF 1024
-
-int ping (int fdin, int fdout)
-{
-
-    printf ("Sending ping...\n");
-    for (int i = 0; i < nb_measurements; i++) {
-
-        char buffer[MAXBUF] = { 0 };
-        sprintf (buffer, "ping %d", i);
-
-        ts_t ts1;
-        sys_timestamp (&ts1);
-
-        if (write (fdout, buffer, strlen (buffer) + 1) == -1) {
-            fprintf (stderr, "ping error: write (%d)\n", i);
-            return 1;
-        }
-        //printf ("write '%s'\n", buffer);
-
-        if (read (fdin, buffer, sizeof(buffer)) == -1) {
-            fprintf (stderr, "ping error: read (%d)\n", i);
-            return 1;
-        }
-        //printf ("read '%s'\n", buffer);
-
-        ts_t ts2;
-        sys_timestamp (&ts2);
-        deltas[i] = diff_timestamp (&ts2, &ts1);
-    }
-
-    return 0;
-}
-
-int pong (int fdin, int fdout)
-{
-
-    printf ("Responding pong...\n");
-    for (int i = 0; i < nb_measurements; i++) {
-
-        char buffer[MAXBUF] = { 0 };
-
-        if (read (fdin, buffer, sizeof(buffer)) == -1) {
-            fprintf (stderr, "ping error: read (%d)\n", i);
-            return 1;
-        }
-        //printf ("read '%s'\n", buffer);
-
-        if (write (fdout, buffer, strlen (buffer) + 1) == -1) {
-            fprintf (stderr, "ping error: write (%d)\n", i);
-            return 1;
-        }
-        //printf ("write '%s'\n", buffer);
-    }
-
-    return 0;
-}
-
-int test (dts_t *buffer, int nb)
-{
-
-    /* set global variables */
-
-    deltas = buffer;
-    nb_measurements = nb;
-
-    /* pipe test */
-
-    int pipefd[2] = { 0 };
-
-    if (pipe (pipefd) == -1) {
-        fprintf (stderr, "error: pipe\n");
-        return 1;
-    }
-
-    pid_t pid = fork ();
-    if (pid == -1) {
-        fprintf (stderr, "error: fork\n");
-        return 1;
-    } else if (pid == 0) {
-        int rc = pong (pipefd[0], pipefd[1]);
-        close (pipefd[0]);
-        close (pipefd[1]);
-        exit (rc);
-    }
-
-    int rc = ping (pipefd[0], pipefd[1]);
-    close (pipefd[0]);
-    close (pipefd[1]);
-
-    if (kill (pid, SIGTERM) == 0) {
-        int wstatus;
-        if (waitpid (pid, &wstatus, WUNTRACED | WCONTINUED) == -1) {
-            fprintf (stderr, "error: waitpid\n");
-        }
-    }
-
-    return rc;
-}
diff --git a/pipe_lat.c b/pipe_lat.c
new file mode 100644 (file)
index 0000000..438ff00
--- /dev/null
@@ -0,0 +1,151 @@
+/* depend: */
+/* cflags: */
+/* linker: mtime.o test.o stat.o -lm -lrt */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <pthread.h>
+#include <unistd.h>
+
+#include "mtime.h"
+#include "test.h"
+
+/* global variables */
+
+dts_t *deltas = NULL;
+int nb_measurements = 0;
+
+char *message = "Pipe latency";
+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 };
+
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+
+void *ping (__attribute__((unused)) void *arg)
+{
+
+    int fdin = dup (pipefd[0]);
+    int fdout = dup (pipefd[1]);
+
+    pthread_mutex_lock (&mutex);
+    pthread_mutex_unlock (&mutex);
+
+    printf ("Sending ping...\n");
+    for (int i = 0; i < nb_measurements; i++) {
+
+        char buffer[MAXBUF] = { 0 };
+        sprintf (buffer, "ping %d", i);
+
+        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;
+        }
+        //printf ("read '%s'\n", buffer);
+
+        sys_timestamp (&ts2);
+        deltas[i] = diff_timestamp (&ts2, &ts1);
+    }
+
+    close (fdin);
+    close (fdout);
+
+    return NULL;
+}
+
+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 };
+
+        if (read (fdin, buffer, sizeof(buffer)) == -1) {
+            fprintf (stderr, "ping error: read (%d)\n", i);
+            rc = 1;
+            return 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);
+    }
+
+    close (fdin);
+    close (fdout);
+
+    return NULL;
+}
+
+int test (dts_t *buffer, int nb)
+{
+
+    /* set global variables */
+
+    deltas = buffer;
+    nb_measurements = nb;
+
+    /* pipe test */
+
+    if (pipe (pipefd) == -1) {
+        fprintf (stderr, "error: pipe\n");
+        return 1;
+    }
+
+    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_t tid2;
+    if (pthread_create (&tid2, NULL, pong, NULL) != 0) {
+        fprintf (stderr, "error on pthread_create\n");
+        return 1;
+    }
+
+    pthread_join (tid1, NULL);
+    pthread_join (tid2, NULL);
+
+    close (pipefd[0]);
+    close (pipefd[1]);
+
+    pthread_mutex_destroy (&mutex);
+
+    return rc;
+}
diff --git a/pipe_rtt.c b/pipe_rtt.c
new file mode 100644 (file)
index 0000000..30a7679
--- /dev/null
@@ -0,0 +1,123 @@
+/* depend: */
+/* cflags: */
+/* linker: mtime.o test.o stat.o -lm -lrt */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <signal.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "mtime.h"
+#include "test.h"
+
+/* global variables */
+
+dts_t *deltas = NULL;
+int nb_measurements = 0;
+
+char *message = "Pipe RTT";
+void (*usage_ext) (FILE *) = NULL;
+int (*parse_arg_ext) (char *) = NULL;
+
+#define MAXBUF 1024
+
+int ping (int fdin, int fdout)
+{
+
+    printf ("Sending ping...\n");
+    for (int i = 0; i < nb_measurements; i++) {
+
+        char buffer[MAXBUF] = { 0 };
+        sprintf (buffer, "ping %d", i);
+
+        ts_t ts1;
+        sys_timestamp (&ts1);
+
+        if (write (fdout, buffer, strlen (buffer) + 1) == -1) {
+            fprintf (stderr, "ping error: write (%d)\n", i);
+            return 1;
+        }
+        //printf ("write '%s'\n", buffer);
+
+        if (read (fdin, buffer, sizeof(buffer)) == -1) {
+            fprintf (stderr, "ping error: read (%d)\n", i);
+            return 1;
+        }
+        //printf ("read '%s'\n", buffer);
+
+        ts_t ts2;
+        sys_timestamp (&ts2);
+        deltas[i] = diff_timestamp (&ts2, &ts1);
+    }
+
+    return 0;
+}
+
+int pong (int fdin, int fdout)
+{
+
+    printf ("Responding pong...\n");
+    for (int i = 0; i < nb_measurements; i++) {
+
+        char buffer[MAXBUF] = { 0 };
+
+        if (read (fdin, buffer, sizeof(buffer)) == -1) {
+            fprintf (stderr, "ping error: read (%d)\n", i);
+            return 1;
+        }
+        //printf ("read '%s'\n", buffer);
+
+        if (write (fdout, buffer, strlen (buffer) + 1) == -1) {
+            fprintf (stderr, "ping error: write (%d)\n", i);
+            return 1;
+        }
+        //printf ("write '%s'\n", buffer);
+    }
+
+    return 0;
+}
+
+int test (dts_t *buffer, int nb)
+{
+
+    /* set global variables */
+
+    deltas = buffer;
+    nb_measurements = nb;
+
+    /* pipe test */
+
+    int pipefd[2] = { 0 };
+
+    if (pipe (pipefd) == -1) {
+        fprintf (stderr, "error: pipe\n");
+        return 1;
+    }
+
+    pid_t pid = fork ();
+    if (pid == -1) {
+        fprintf (stderr, "error: fork\n");
+        return 1;
+    } else if (pid == 0) {
+        int rc = pong (pipefd[0], pipefd[1]);
+        close (pipefd[0]);
+        close (pipefd[1]);
+        exit (rc);
+    }
+
+    int rc = ping (pipefd[0], pipefd[1]);
+    close (pipefd[0]);
+    close (pipefd[1]);
+
+    if (kill (pid, SIGTERM) == 0) {
+        int wstatus;
+        if (waitpid (pid, &wstatus, WUNTRACED | WCONTINUED) == -1) {
+            fprintf (stderr, "error: waitpid\n");
+        }
+    }
+
+    return rc;
+}