#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);
}
-/* microsecond time module */
+/* millisecond time module */
#ifndef __MSTIME_H__
#define __MSTIME_H__
--- /dev/null
+#include <time.h>
+
+#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);
+}
--- /dev/null
+/* 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__ */
/* depend: */
/* cflags: */
-/* linker: nstime.o stat.o -lpthread -lm */
+/* linker: nstime.o test.o stat.o -lpthread -lm */
-#include <assert.h>
-#include <inttypes.h>
#include <pthread.h>
#include <stdio.h>
-#include <stdlib.h>
#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;
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);
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 */
pthread_mutex_destroy(&mutex);
- return 0;
+ return nsec_e;
}
--- /dev/null
+#include <assert.h>
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#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;
+}
--- /dev/null
+/* test module */
+
+#ifndef __TEST_H__
+#define __TEST_H__
+
+#include <inttypes.h>
+
+typedef enum {
+ sec_e = 0,
+ msec_e,
+ musec_e,
+ nsec_e
+} sec_t;
+
+sec_t test (int64_t *buffer, int nb);
+
+#endif /* __TEST_H__ */