From 86b445e39c3c7eed076b01795b17f34481517288 Mon Sep 17 00:00:00 2001 From: "Bjoern B. Brandenburg" Date: Sun, 30 Jan 2011 12:18:26 -0500 Subject: Export wctime() and cputime() from rtspin to library clients The timing functions are quite handy when building benchmark tasks. Avoid copy&paste reuse by making them available via the library. --- bin/rtspin.c | 16 ---------------- include/litmus.h | 6 ++++++ src/clocks.c | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 src/clocks.c diff --git a/bin/rtspin.c b/bin/rtspin.c index 2de60f5..ae76941 100644 --- a/bin/rtspin.c +++ b/bin/rtspin.c @@ -11,22 +11,6 @@ #include "common.h" -static double cputime() -{ - struct timespec ts; - int err; - err = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); - if (err != 0) - perror("clock_gettime"); - return (ts.tv_sec + 1E-9 * ts.tv_nsec); -} - -static double wctime() -{ - struct timeval tv; - gettimeofday(&tv, NULL); - return (tv.tv_sec + 1E-6 * tv.tv_usec); -} static void usage(char *error) { fprintf(stderr, "Error: %s\n", error); diff --git a/include/litmus.h b/include/litmus.h index 2a26a4a..3b762db 100644 --- a/include/litmus.h +++ b/include/litmus.h @@ -111,6 +111,12 @@ static inline lt_t ms2lt(unsigned long milliseconds) return __NS_PER_MS * milliseconds; } +/* CPU time consumed so far in seconds */ +double cputime(void); + +/* wall-clock time in seconds */ +double wctime(void); + /* semaphore allocation */ static inline int open_fmlp_sem(int fd, int name) diff --git a/src/clocks.c b/src/clocks.c new file mode 100644 index 0000000..fcb94e5 --- /dev/null +++ b/src/clocks.c @@ -0,0 +1,23 @@ +#include + +#include +#include + +/* CPU time consumed so far in seconds */ +double cputime(void) +{ + struct timespec ts; + int err; + err = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); + if (err != 0) + perror("clock_gettime"); + return (ts.tv_sec + 1E-9 * ts.tv_nsec); +} + +/* wall-clock time in seconds */ +double wctime(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return (tv.tv_sec + 1E-6 * tv.tv_usec); +} -- cgit v1.2.2