From e2dbc792beccd97d8218657b4675bb179d0faec2 Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Tue, 30 Sep 2025 10:33:38 +0200 Subject: [PATCH] huge cleaning --- mq.c | 36 ++++++++++++++++++----------------- mtime.c | 8 ++++---- mtime.h | 14 +++++--------- mutex.c | 14 ++++++++------ pipe.c | 36 ++++++++++++++++++----------------- semaphore.c | 14 ++++++++------ stat.c | 6 ++++-- stat.h | 4 +++- test.c | 24 +++++++++--------------- test.h | 10 ++-------- thread.c | 14 ++++++++------ udp.c | 54 +++++++++++++++++++++++++++++++---------------------- 12 files changed, 121 insertions(+), 113 deletions(-) diff --git a/mq.c b/mq.c index 5935f58..2f31e39 100644 --- a/mq.c +++ b/mq.c @@ -19,14 +19,16 @@ /* global variables */ -nstime_t *deltas = NULL; +dts_t *deltas = NULL; int nb_measurements = 0; char *message = "message queue"; +void (*usage_ext) (FILE *) = NULL; +int (*parse_arg_ext) (char *) = NULL; #define MAXBUF 1024 -sec_t ping (mqd_t mq) +int ping (mqd_t mq) { printf ("Sending ping...\n"); @@ -35,30 +37,30 @@ sec_t ping (mqd_t mq) char buffer[MAXBUF] = { 0 }; sprintf (buffer, "ping %d", i); - mtime_t ts1; + ts_t ts1; sys_timestamp (&ts1); if (mq_send (mq, buffer, strlen (buffer) + 1, 0) == -1) { fprintf (stderr, "ping error: mq_send (%d)\n", i); - return none_e; + return 1; } //printf ("send '%s'\n", buffer); if (mq_receive (mq, buffer, sizeof(buffer), NULL) == -1) { fprintf (stderr, "ping error: mq_receive (%d)\n", i); - return none_e; + return 1; } //printf ("receive '%s'\n", buffer); - mtime_t ts2; + ts_t ts2; sys_timestamp (&ts2); deltas[i] = diff_timestamp (&ts2, &ts1); } - return nsec_e; + return 0; } -sec_t pong (mqd_t mq) +int pong (mqd_t mq) { printf ("Responding pong...\n"); @@ -68,21 +70,21 @@ sec_t pong (mqd_t mq) if (mq_receive (mq, buffer, sizeof(buffer), NULL) == -1) { fprintf (stderr, "pong error: mq_receive (%d)\n", i); - return none_e; + return 1; } //printf ("receive '%s'\n", buffer); if (mq_send (mq, buffer, strlen (buffer) + 1, 0) == -1) { fprintf (stderr, "pong error: mq_send (%d)\n", i); - return none_e; + return 1; } //printf ("send '%s'\n", buffer); } - return none_e; + return 0; } -sec_t test (unsigned int *buffer, int nb) +int test (dts_t *buffer, int nb) { /* set global variables */ @@ -101,20 +103,20 @@ sec_t test (unsigned int *buffer, int nb) mqd_t mq = mq_open ("/test_queue", O_CREAT | O_RDWR, 0644, &attr); if (mq == -1) { fprintf (stderr, "error: mq_open\n"); - return none_e; + return 1; } pid_t pid = fork (); if (pid == -1) { fprintf (stderr, "error: fork\n"); - return none_e; + return 1; } else if (pid == 0) { - pong (mq); + int rc = pong (mq); mq_close (mq); - exit (0); + exit (rc); } - sec_t rc = ping (mq); + int rc = ping (mq); mq_close (mq); if (kill (pid, SIGTERM) == 0) { diff --git a/mtime.c b/mtime.c index e4ceb8c..220d4d6 100644 --- a/mtime.c +++ b/mtime.c @@ -2,18 +2,18 @@ #include "mtime.h" -void sys_timestamp (mtime_t *ts) +void sys_timestamp (ts_t *ts) { clock_gettime (CLOCK_REALTIME, ts); } -unsigned int diff_timestamp (mtime_t *ts1, mtime_t *ts2) +dts_t diff_timestamp (ts_t *ts1, ts_t *ts2) { - return (ts1->tv_sec - ts2->tv_sec) * 1000000000 + + return (dts_t)(ts1->tv_sec - ts2->tv_sec) * 1000000000 + ts1->tv_nsec - ts2->tv_nsec; } -void set_timestamp (mtime_t *ts1, mtime_t *ts2) +void set_timestamp (ts_t *ts1, ts_t *ts2) { ts1->tv_sec = ts2->tv_sec; ts1->tv_nsec = ts2->tv_nsec; diff --git a/mtime.h b/mtime.h index c07168f..7085bb7 100644 --- a/mtime.h +++ b/mtime.h @@ -5,18 +5,14 @@ #include -typedef unsigned int mstime_t; +typedef struct timespec ts_t; -typedef unsigned int ustime_t; +typedef unsigned int dts_t; -typedef unsigned int nstime_t; +void sys_timestamp (ts_t *ts); -typedef struct timespec mtime_t; +dts_t diff_timestamp (ts_t *ts1, ts_t *ts2); -void sys_timestamp (mtime_t *ts); - -unsigned int diff_timestamp (mtime_t *ts1, mtime_t *ts2); - -void set_timestamp (mtime_t *ts1, mtime_t *ts2); +void set_timestamp (ts_t *ts1, ts_t *ts2); #endif /* __MTIME_H__ */ diff --git a/mutex.c b/mutex.c index 001feab..7382a75 100644 --- a/mutex.c +++ b/mutex.c @@ -10,14 +10,16 @@ /* global variables */ -nstime_t *deltas = NULL; +dts_t *deltas = NULL; int nb_measurements = 0; char *message = "mutex"; +void (*usage_ext) (FILE *) = NULL; +int (*parse_arg_ext) (char *) = NULL; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int shared_flag = 0; -mtime_t timestamp1, timestamp2; +ts_t timestamp1, timestamp2; int try = 0; void *mutex_giver_task (__attribute__((unused)) void *arg) @@ -47,7 +49,7 @@ void *mutex_taker_task (__attribute__((unused)) void *arg) pthread_exit (NULL); } -sec_t test (unsigned int *buffer, int nb) +int test (dts_t *buffer, int nb) { /* set global variables */ @@ -62,13 +64,13 @@ sec_t test (unsigned int *buffer, int nb) pthread_t giver_thread; if (pthread_create (&giver_thread, NULL, mutex_giver_task, NULL) != 0) { fprintf (stderr, "error on pthread_create for giver_task\n"); - return none_e; + return 1; } pthread_t taker_thread; if (pthread_create (&taker_thread, NULL, mutex_taker_task, NULL) != 0) { fprintf (stderr, "error on pthread_create for taker_task\n"); - return none_e; + return 1; } pthread_join (giver_thread, NULL); @@ -76,5 +78,5 @@ sec_t test (unsigned int *buffer, int nb) pthread_mutex_destroy (&mutex); - return nsec_e; + return 0; } diff --git a/pipe.c b/pipe.c index 4aa5542..65684b0 100644 --- a/pipe.c +++ b/pipe.c @@ -15,14 +15,16 @@ /* global variables */ -nstime_t *deltas = NULL; +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 -sec_t ping (int fdin, int fdout) +int ping (int fdin, int fdout) { printf ("Sending ping...\n"); @@ -31,30 +33,30 @@ sec_t ping (int fdin, int fdout) char buffer[MAXBUF] = { 0 }; sprintf (buffer, "ping %d", i); - mtime_t ts1; + ts_t ts1; sys_timestamp (&ts1); if (write (fdout, buffer, strlen (buffer) + 1) == -1) { fprintf (stderr, "ping error: write (%d)\n", i); - return none_e; + return 1; } //printf ("write '%s'\n", buffer); if (read (fdin, buffer, sizeof(buffer)) == -1) { fprintf (stderr, "ping error: read (%d)\n", i); - return none_e; + return 1; } //printf ("read '%s'\n", buffer); - mtime_t ts2; + ts_t ts2; sys_timestamp (&ts2); deltas[i] = diff_timestamp (&ts2, &ts1); } - return nsec_e; + return 0; } -sec_t pong (int fdin, int fdout) +int pong (int fdin, int fdout) { printf ("Responding pong...\n"); @@ -64,21 +66,21 @@ sec_t pong (int fdin, int fdout) if (read (fdin, buffer, sizeof(buffer)) == -1) { fprintf (stderr, "ping error: read (%d)\n", i); - return none_e; + return 1; } //printf ("read '%s'\n", buffer); if (write (fdout, buffer, strlen (buffer) + 1) == -1) { fprintf (stderr, "ping error: write (%d)\n", i); - return none_e; + return 1; } //printf ("write '%s'\n", buffer); } - return none_e; + return 0; } -sec_t test (unsigned int *buffer, int nb) +int test (dts_t *buffer, int nb) { /* set global variables */ @@ -92,21 +94,21 @@ sec_t test (unsigned int *buffer, int nb) if (pipe (pipefd) == -1) { fprintf (stderr, "error: pipe\n"); - return none_e; + return 1; } pid_t pid = fork (); if (pid == -1) { fprintf (stderr, "error: fork\n"); - return none_e; + return 1; } else if (pid == 0) { - pong (pipefd[0], pipefd[1]); + int rc = pong (pipefd[0], pipefd[1]); close (pipefd[0]); close (pipefd[1]); - exit (0); + exit (rc); } - sec_t rc = ping (pipefd[0], pipefd[1]); + int rc = ping (pipefd[0], pipefd[1]); close (pipefd[0]); close (pipefd[1]); diff --git a/semaphore.c b/semaphore.c index ba9e677..04eacf7 100644 --- a/semaphore.c +++ b/semaphore.c @@ -13,14 +13,16 @@ /* global variables */ -mstime_t *measure_tab = NULL; +dts_t *measure_tab = NULL; char *message = "semaphore"; +void (*usage_ext) (FILE *) = NULL; +int (*parse_arg_ext) (char *) = NULL; int counter = 0; int try = 0; sem_t sem; -mtime_t ts1, ts2; +ts_t ts1, ts2; void* trythis (__attribute__((unused)) void *arg) { @@ -44,7 +46,7 @@ void* trythis (__attribute__((unused)) void *arg) return NULL; } -sec_t test (unsigned int *buffer, int nb) +int test (dts_t *buffer, int nb) { /* set global variables */ @@ -60,13 +62,13 @@ sec_t test (unsigned int *buffer, int nb) pthread_t tid1; if (pthread_create (&tid1, NULL, trythis, NULL) != 0) { fprintf (stderr, "error on pthread_create\n"); - return none_e; + return 1; } pthread_t tid2; if (pthread_create (&tid2, NULL, trythis, NULL) != 0) { fprintf (stderr, "error on pthread_create\n"); - return none_e; + return 1; } pthread_join (tid1, NULL); @@ -75,5 +77,5 @@ sec_t test (unsigned int *buffer, int nb) sem_destroy (&sem); - return nsec_e; + return 0; } diff --git a/stat.c b/stat.c index b7f4643..db782e6 100644 --- a/stat.c +++ b/stat.c @@ -9,9 +9,10 @@ #define STEP 20 -void compute_statistics (unsigned int *points, int nb, int bins) +void compute_statistics (dts_t *points, int nb, int bins) { + printf ("Results are in nanosecond\n"); printf ("Nb of points = %d\n", nb); /* compute average, minimum and maximum */ @@ -51,7 +52,7 @@ void compute_statistics (unsigned int *points, int nb, int bins) int change_done = 0; for (int i = 1; i < nb - 1; i++) { if (points[i + 1] < points[i]) { - unsigned int tmp = points[i]; + dts_t tmp = points[i]; points[i] = points[i + 1]; points[i + 1] = tmp; change_done = 1; @@ -103,4 +104,5 @@ void compute_statistics (unsigned int *points, int nb, int bins) free (hist); + return; } diff --git a/stat.h b/stat.h index 82880a6..2ecd021 100644 --- a/stat.h +++ b/stat.h @@ -3,6 +3,8 @@ #ifndef __STAT_H__ #define __STAT_H__ -void compute_statistics (unsigned int *points, int nb, int bins); +#include "mtime.h" + +void compute_statistics (dts_t *points, int nb, int bins); #endif /* __STAT_H__ */ diff --git a/test.c b/test.c index 865389f..1754eb0 100644 --- a/test.c +++ b/test.c @@ -24,6 +24,8 @@ int nb = 1000; char *output = NULL; extern char *message; +extern void (*usage_ext) (FILE *); +extern int (*parse_arg_ext) (char *); /* usage function */ @@ -38,6 +40,10 @@ int usage (int ret) fprintf (fd, " -n: nb measurements (%d)\n", nb); fprintf (fd, " -o: output raw data (%s)\n", (output) ? output : "none"); fprintf (fd, " -s: display statistics (%s)\n", (do_stat) ? "yes" : "no"); + if (usage_ext) { + fprintf (fd, "extension:\n"); + usage_ext (fd); + } fprintf (fd, "%s version %s\n", progname, version); return ret; @@ -62,7 +68,7 @@ int main (int argc, char *argv[]) while (argc-- > 1) { char *arg = *(++argv); - if (arg[0] != '-') { + if ((arg[0] != '-') && ((!parse_arg_ext) || (parse_arg_ext (arg)))) { fprintf (stderr, "%s: invalid option -- %s\n", progname, arg); return usage (1); } @@ -136,14 +142,12 @@ int main (int argc, char *argv[]) /* main process */ - unsigned int *buffer = (unsigned int *) calloc (nb, sizeof (unsigned int)); + dts_t *buffer = (dts_t *) calloc (nb, sizeof (dts_t)); assert (buffer); printf ("Test: %s\n", (message) ? message : "unknown"); - sec_t sec_unit = test (buffer, nb); - - if (sec_unit == none_e) { + if (test (buffer, nb)) { printf ("\033[1;31mKO\033[0;0m\n"); return 1; } else { @@ -192,16 +196,6 @@ int main (int argc, char *argv[]) } } - char unit[32] = { 0 }; - switch (sec_unit) { - case sec_e : strcpy (unit, "second"); break; - case msec_e : strcpy (unit, "millisecond"); break; - case usec_e : strcpy (unit, "microsecond"); break; - case nsec_e : strcpy (unit, "nanosecond"); break; - default: strcpy (unit, "???"); break; - } - - printf ("Results are in %s\n", unit); compute_statistics (buffer, nb, hist_bin); } diff --git a/test.h b/test.h index 2e38473..8faa05d 100644 --- a/test.h +++ b/test.h @@ -3,14 +3,8 @@ #ifndef __TEST_H__ #define __TEST_H__ -typedef enum { - none_e = 0, - sec_e, - msec_e, - usec_e, - nsec_e -} sec_t; +#include "mtime.h" -sec_t test (unsigned int *buffer, int nb); +int test (dts_t *buffer, int nb); #endif /* __TEST_H__ */ diff --git a/thread.c b/thread.c index be55b74..92cec97 100644 --- a/thread.c +++ b/thread.c @@ -10,17 +10,19 @@ /* global variables */ -nstime_t *deltas = NULL; +dts_t *deltas = NULL; int nb_measurements = 0; char *message = "thread"; +void (*usage_ext) (FILE *) = NULL; +int (*parse_arg_ext) (char *) = NULL; void* dummy_thread(void *arg) { (void)arg; return NULL; } -sec_t test (unsigned int *buffer, int nb) +int test (dts_t *buffer, int nb) { /* set global variables */ @@ -32,22 +34,22 @@ sec_t test (unsigned int *buffer, int nb) for (int i = 0; i < nb_measurements; i++) { - mtime_t ts1; + ts_t ts1; sys_timestamp (&ts1); pthread_t posix_t; if (pthread_create(&posix_t, NULL, dummy_thread, NULL) != 0) { fprintf (stderr, "error on pthread_create\n"); - return none_e; + return 1; } pthread_join(posix_t, NULL); - mtime_t ts2; + ts_t ts2; sys_timestamp (&ts2); deltas[i++] = diff_timestamp (&ts2, &ts1); } - return nsec_e; + return 0; } diff --git a/udp.c b/udp.c index ecd4cfb..69a4f45 100644 --- a/udp.c +++ b/udp.c @@ -19,20 +19,30 @@ /* global variables */ -nstime_t *deltas = NULL; +dts_t *deltas = NULL; int nb_measurements = 0; +int mode = 0; char *message = "udp socket"; +void _usage_ext (FILE *fd) +{ + fprintf (fd, " localip localport remoteip remoteport [mode]\n"); + fprintf (fd, " mode (%d): 0 for ping-pong, 1 for ping, 2 for pong\n", mode); +} +void (*usage_ext) (FILE *) = _usage_ext; + +int (*parse_arg_ext) (char *) = NULL; + #define MAXBUF 1024 -sec_t ping (int local_port, int remote_addr, int remote_port) +int ping (int local_port, int remote_addr, int remote_port) { int sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock == -1) { fprintf (stderr, "ping error: socket\n"); - return none_e; + return 1; } struct sockaddr_in local = { 0 }; @@ -42,7 +52,7 @@ sec_t ping (int local_port, int remote_addr, int remote_port) if (bind (sock, (struct sockaddr *)&local, sizeof (local)) == -1) { fprintf (stderr, "ping error: bind\n"); - return none_e; + return 1; } struct sockaddr_in remote = { 0 }; @@ -56,39 +66,39 @@ sec_t ping (int local_port, int remote_addr, int remote_port) char buffer[MAXBUF] = { 0 }; sprintf (buffer, "ping %d", i); - mtime_t ts1; + ts_t ts1; sys_timestamp (&ts1); if (sendto (sock, buffer, strlen (buffer) + 1, 0, (struct sockaddr *)&remote, sizeof (remote)) == -1) { fprintf (stderr, "ping error: sendto (%d)\n", i); - return none_e; + return 1; } //printf ("sendto '%s'\n", buffer); int len = recv (sock, buffer, MAXBUF, 0); if (len == -1) { fprintf (stderr, "ping error: recv_from (%d)\n", i); - return none_e; + return 1; } //printf ("recv_from '%s'\n", buffer); - mtime_t ts2; + ts_t ts2; sys_timestamp (&ts2); deltas[i] = diff_timestamp (&ts2, &ts1); } close (sock); - return nsec_e; + return 0; } -sec_t pong (int local_port, int remote_addr, int remote_port) +int pong (int local_port, int remote_addr, int remote_port) { int sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (sock == -1) { fprintf (stderr, "pong error: socket\n"); - return none_e; + return 1; } struct sockaddr_in local = { 0 }; @@ -98,7 +108,7 @@ sec_t pong (int local_port, int remote_addr, int remote_port) if (bind (sock, (struct sockaddr *)&local, sizeof (local)) == -1) { fprintf (stderr, "pong error: bind\n"); - return none_e; + return 1; } struct sockaddr_in remote = { 0 }; @@ -114,20 +124,20 @@ sec_t pong (int local_port, int remote_addr, int remote_port) int len = recv (sock, buffer, MAXBUF, 0); if (len == -1) { fprintf (stderr, "pong error: recv_from (%d)\n", i); - return none_e; + return 1; } //printf ("recv_from '%s'\n", buffer); if (sendto (sock, buffer, len, 0, (struct sockaddr *)&remote, sizeof (remote)) == -1) { fprintf (stderr, "pong error: sendto (%d)\n", i); - return none_e; + return 1; } //printf ("sendto '%s'\n", buffer); } close (sock); - return none_e; + return 0; } int get_ip (char *hostname) { @@ -140,7 +150,7 @@ int get_ip (char *hostname) { return ntohl (addr_list[0]->s_addr); } -sec_t test (unsigned int *buffer, int nb) +int test (dts_t *buffer, int nb) { char *local_host = "localhost"; int local_port = 1024; @@ -155,23 +165,23 @@ sec_t test (unsigned int *buffer, int nb) /* udp socket test */ int local_ip = get_ip (local_host); if (local_ip == -1) { - return none_e; + return 1; } int remote_ip = get_ip (remote_host); if (remote_ip == -1) { - return none_e; + return 1; } pid_t pid = fork (); if (pid == -1) { fprintf (stderr, "error: fork\n"); - return none_e; + return 1; } else if (pid == 0) { - pong (remote_port, local_ip, local_port); - exit (0); + int rc = pong (remote_port, local_ip, local_port); + exit (rc); } - sec_t rc = ping (local_port, remote_ip, remote_port); + int rc = ping (local_port, remote_ip, remote_port); if (kill (pid, SIGTERM) == 0) { int wstatus; -- 2.30.2