From 69a14e14fd5dc4aa4825411b45c32377747ec062 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Fri, 26 Sep 2025 23:15:29 +0200 Subject: [PATCH] generic test --- mstime.c | 6 +-- mstime.h | 2 +- mustime.c | 15 +++++++ mustime.h | 12 +++++ mutex.c | 116 +++++------------------------------------------- test.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ test.h | 17 +++++++ 7 files changed, 188 insertions(+), 110 deletions(-) create mode 100644 mustime.c create mode 100644 mustime.h create mode 100644 test.c create mode 100644 test.h diff --git a/mstime.c b/mstime.c index e9065b7..d92d0eb 100644 --- a/mstime.c +++ b/mstime.c @@ -2,14 +2,14 @@ #include "mstime.h" -mstime_t timespec_as_microseconds (struct timespec ts) +mstime_t timespec_as_milliseconds (struct timespec ts) { - return ts.tv_sec * 1000000 + ts.tv_nsec / 1000 ; + return ts.tv_sec * 1000 + ts.tv_nsec / 10000000 ; } mstime_t sys_timestamp (void) { struct timespec ts; clock_gettime (CLOCK_REALTIME, &ts); - return timespec_as_microseconds (ts); + return timespec_as_milliseconds (ts); } diff --git a/mstime.h b/mstime.h index b007ffd..4c46aa3 100644 --- a/mstime.h +++ b/mstime.h @@ -1,4 +1,4 @@ -/* microsecond time module */ +/* millisecond time module */ #ifndef __MSTIME_H__ #define __MSTIME_H__ diff --git a/mustime.c b/mustime.c new file mode 100644 index 0000000..0983278 --- /dev/null +++ b/mustime.c @@ -0,0 +1,15 @@ +#include + +#include "mustime.h" + +mustime_t timespec_as_microseconds (struct timespec ts) +{ + return ts.tv_sec * 1000000 + ts.tv_nsec / 1000 ; +} + +mustime_t sys_timestamp (void) +{ + struct timespec ts; + clock_gettime (CLOCK_REALTIME, &ts); + return timespec_as_microseconds (ts); +} diff --git a/mustime.h b/mustime.h new file mode 100644 index 0000000..070790d --- /dev/null +++ b/mustime.h @@ -0,0 +1,12 @@ +/* microsecond time module */ + +#ifndef __MUSTIME_H__ +#define __MUSTIME_H__ + +#include "inttypes.h" + +typedef int64_t mustime_t; + +mustime_t sys_timestamp (void); + +#endif /* __MUSTIME_H__ */ diff --git a/mutex.c b/mutex.c index eb60dcb..8c3db8d 100644 --- a/mutex.c +++ b/mutex.c @@ -1,28 +1,19 @@ /* depend: */ /* cflags: */ -/* linker: nstime.o stat.o -lpthread -lm */ +/* linker: nstime.o test.o stat.o -lpthread -lm */ -#include -#include #include #include -#include #include "nstime.h" #include "stat.h" - -/* static variables */ - -char *progname = NULL; -char *version = "1.1"; - -int nb_measurements = 1000; -int do_stat = 0; -int hist_bin = 10; -char *output = NULL; +#include "test.h" /* global variables */ +int64_t *deltas = NULL; +int nb_measurements = 0; + pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int shared_flag = 0; nstime_t timestamp1, timestamp2; @@ -41,8 +32,6 @@ void *mutex_giver_task (__attribute__((unused)) void *arg) void *mutex_taker_task (__attribute__((unused)) void *arg) { - nstime_t *deltas = (nstime_t *) calloc (nb_measurements, sizeof (nstime_t)); - assert (deltas); while (try < nb_measurements) { pthread_mutex_lock (&mutex); @@ -54,101 +43,16 @@ void *mutex_taker_task (__attribute__((unused)) void *arg) pthread_mutex_unlock(&mutex); } - if (output) { - FILE *fd = fopen (output, "w"); - assert (fd); - for (int i = 0; i < nb_measurements; i++) { - fprintf (fd, "%ld\n", deltas[i]); - } - fclose (fd); - } - - if (do_stat) { - printf ("Results are in nanosecond\n"); - compute_statistics (deltas, nb_measurements, hist_bin); - } - - free (deltas); - pthread_exit(NULL); } -/* usage function */ - -int usage (int ret) -{ - FILE *fd = ret ? stderr : stdout; - fprintf (fd, "usage: %s [-b int] [-h] [-n int] [-o file] [-s]\n", progname); - fprintf (fd, " -b: nb bins for histogram (%d)\n", hist_bin); - fprintf (fd, " -h: help message\n"); - fprintf (fd, " -n: nb measurements (%d)\n", nb_measurements); - fprintf (fd, " -o: output raw data (%s)\n", (output) ? output : "none"); - fprintf (fd, " -s: display statistics (%s)\n", (do_stat) ? "yes" : "no"); - fprintf (fd, "%s version %s\n", progname, version); - - return ret; -} - -/* main function */ - -int main (int argc, char *argv[]) +sec_t test (int64_t *buffer, int nb) { - /* get basename */ + /* set global variables */ - char *pt = progname = argv[0]; - while (*pt) { - if ((*pt == '/') || (*pt == '\\')) { - progname = pt + 1; - } - pt++; - } - - /* process arguments */ - - while (argc-- > 1) { - char *arg = *(++argv); - if (arg[0] != '-') { - fprintf (stderr, "%s: invalid option -- %s\n", progname, arg); - return usage (1); - } - char c = arg[1]; - switch (c) { - case 'b': - arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; - if (arg == NULL) { - fprintf (stderr, "%s: no number of bins specified\n", progname); - return usage (1); - } - hist_bin = atoi (arg); - break; - case 'n': - arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; - if (arg == NULL) { - fprintf (stderr, "%s: no number of measurements specified\n", progname); - return usage (1); - } - nb_measurements = atoi (arg); - break; - case 'o': - arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; - if (arg == NULL) { - fprintf (stderr, "%s: no output file specified\n", progname); - return usage (1); - } - output = arg; - break; - case 's': - do_stat = 1; - break; - case 'v': - printf ("version: %s\n", version); - break; - case 'h': - default: - return usage (c != 'h'); - } - } + deltas = buffer; + nb_measurements = nb; /* main process */ @@ -179,5 +83,5 @@ int main (int argc, char *argv[]) pthread_mutex_destroy(&mutex); - return 0; + return nsec_e; } diff --git a/test.c b/test.c new file mode 100644 index 0000000..b67524f --- /dev/null +++ b/test.c @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include + +#include "stat.h" +#include "test.h" + +/* static variables */ + +char *progname = NULL; +char *version = "1.1"; + +int nb = 1000; +int do_stat = 0; +int hist_bin = 10; +char *output = NULL; + +/* usage function */ + +int usage (int ret) +{ + FILE *fd = ret ? stderr : stdout; + fprintf (fd, "usage: %s [-b int] [-h] [-n int] [-o file] [-s]\n", progname); + fprintf (fd, " -b: nb bins for histogram (%d)\n", hist_bin); + fprintf (fd, " -h: help message\n"); + 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"); + fprintf (fd, "%s version %s\n", progname, version); + + return ret; +} + +/* main function */ + +int main (int argc, char *argv[]) +{ + + /* get basename */ + + char *pt = progname = argv[0]; + while (*pt) { + if ((*pt == '/') || (*pt == '\\')) { + progname = pt + 1; + } + pt++; + } + + /* process arguments */ + + while (argc-- > 1) { + char *arg = *(++argv); + if (arg[0] != '-') { + fprintf (stderr, "%s: invalid option -- %s\n", progname, arg); + return usage (1); + } + char c = arg[1]; + switch (c) { + case 'b': + arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; + if (arg == NULL) { + fprintf (stderr, "%s: no number of bins specified\n", progname); + return usage (1); + } + hist_bin = atoi (arg); + break; + case 'n': + arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; + if (arg == NULL) { + fprintf (stderr, "%s: no number of measurements specified\n", progname); + return usage (1); + } + nb = atoi (arg); + break; + case 'o': + arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; + if (arg == NULL) { + fprintf (stderr, "%s: no output file specified\n", progname); + return usage (1); + } + output = arg; + break; + case 's': + do_stat = 1; + break; + case 'v': + printf ("version: %s\n", version); + break; + case 'h': + default: + return usage (c != 'h'); + } + } + + /* main process */ + + int64_t *buffer = (int64_t *) calloc (nb, sizeof (int64_t)); + assert (buffer); + + sec_t sec_unit = test (buffer, nb); + + if (output) { + FILE *fd = fopen (output, "w"); + assert (fd); + for (int i = 0; i < nb; i++) { + fprintf (fd, "%ld\n", buffer[i]); + } + fclose (fd); + } + + if (do_stat) { + char unit[32] = { 0 }; + switch (sec_unit) { + case sec_e : strcpy (unit, "second"); break; + case msec_e : strcpy (unit, "millisecond"); break; + case musec_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); + } + + free (buffer); + + return 0; +} diff --git a/test.h b/test.h new file mode 100644 index 0000000..6fb35af --- /dev/null +++ b/test.h @@ -0,0 +1,17 @@ +/* test module */ + +#ifndef __TEST_H__ +#define __TEST_H__ + +#include + +typedef enum { + sec_e = 0, + msec_e, + musec_e, + nsec_e +} sec_t; + +sec_t test (int64_t *buffer, int nb); + +#endif /* __TEST_H__ */ -- 2.30.2