From 98341b7b1b36c35a3b3f1d947b44c5ea90c6f358 Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Thu, 2 Oct 2025 19:26:40 +0200 Subject: [PATCH] add latency test for pipe --- pipe_lat.c | 151 +++++++++++++++++++++++++++++++++++++++++++ pipe.c => pipe_rtt.c | 2 +- 2 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 pipe_lat.c rename pipe.c => pipe_rtt.c (98%) diff --git a/pipe_lat.c b/pipe_lat.c new file mode 100644 index 0000000..438ff00 --- /dev/null +++ b/pipe_lat.c @@ -0,0 +1,151 @@ +/* depend: */ +/* cflags: */ +/* linker: mtime.o test.o stat.o -lm -lrt */ + +#include +#include +#include + +#include +#include + +#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.c b/pipe_rtt.c similarity index 98% rename from pipe.c rename to pipe_rtt.c index 65684b0..30a7679 100644 --- a/pipe.c +++ b/pipe_rtt.c @@ -18,7 +18,7 @@ dts_t *deltas = NULL; int nb_measurements = 0; -char *message = "pipe"; +char *message = "Pipe RTT"; void (*usage_ext) (FILE *) = NULL; int (*parse_arg_ext) (char *) = NULL; -- 2.30.2