From 839448bcde4872016c59b7645e07c70c5fc29720 Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Fri, 3 Oct 2025 17:10:25 +0200 Subject: [PATCH] rework on pipe latency tets --- pipe_lat.c | 61 ++++++++++++++++++++++-------------------------------- 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/pipe_lat.c b/pipe_lat.c index 438ff00..b4137b1 100644 --- a/pipe_lat.c +++ b/pipe_lat.c @@ -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 #include @@ -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"); -- 2.30.2