diff options
| author | Bjoern B. Brandenburg <bbb@cs.unc.edu> | 2011-01-30 12:18:26 -0500 |
|---|---|---|
| committer | Bjoern B. Brandenburg <bbb@cs.unc.edu> | 2011-02-03 17:40:16 -0500 |
| commit | 86b445e39c3c7eed076b01795b17f34481517288 (patch) | |
| tree | 88db1c398fde6882328bcba74fb49945fa4bce24 | |
| parent | 46f3b11a1f49d49120415b56b6f627c66e4b4d1b (diff) | |
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.
| -rw-r--r-- | bin/rtspin.c | 16 | ||||
| -rw-r--r-- | include/litmus.h | 6 | ||||
| -rw-r--r-- | src/clocks.c | 23 |
3 files changed, 29 insertions, 16 deletions
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 @@ | |||
| 11 | #include "common.h" | 11 | #include "common.h" |
| 12 | 12 | ||
| 13 | 13 | ||
| 14 | static double cputime() | ||
| 15 | { | ||
| 16 | struct timespec ts; | ||
| 17 | int err; | ||
| 18 | err = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); | ||
| 19 | if (err != 0) | ||
| 20 | perror("clock_gettime"); | ||
| 21 | return (ts.tv_sec + 1E-9 * ts.tv_nsec); | ||
| 22 | } | ||
| 23 | |||
| 24 | static double wctime() | ||
| 25 | { | ||
| 26 | struct timeval tv; | ||
| 27 | gettimeofday(&tv, NULL); | ||
| 28 | return (tv.tv_sec + 1E-6 * tv.tv_usec); | ||
| 29 | } | ||
| 30 | 14 | ||
| 31 | static void usage(char *error) { | 15 | static void usage(char *error) { |
| 32 | fprintf(stderr, "Error: %s\n", error); | 16 | 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) | |||
| 111 | return __NS_PER_MS * milliseconds; | 111 | return __NS_PER_MS * milliseconds; |
| 112 | } | 112 | } |
| 113 | 113 | ||
| 114 | /* CPU time consumed so far in seconds */ | ||
| 115 | double cputime(void); | ||
| 116 | |||
| 117 | /* wall-clock time in seconds */ | ||
| 118 | double wctime(void); | ||
| 119 | |||
| 114 | /* semaphore allocation */ | 120 | /* semaphore allocation */ |
| 115 | 121 | ||
| 116 | static inline int open_fmlp_sem(int fd, int name) | 122 | 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 @@ | |||
| 1 | #include <stdio.h> | ||
| 2 | |||
| 3 | #include <sys/time.h> | ||
| 4 | #include <time.h> | ||
| 5 | |||
| 6 | /* CPU time consumed so far in seconds */ | ||
| 7 | double cputime(void) | ||
| 8 | { | ||
| 9 | struct timespec ts; | ||
| 10 | int err; | ||
| 11 | err = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); | ||
| 12 | if (err != 0) | ||
| 13 | perror("clock_gettime"); | ||
| 14 | return (ts.tv_sec + 1E-9 * ts.tv_nsec); | ||
| 15 | } | ||
| 16 | |||
| 17 | /* wall-clock time in seconds */ | ||
| 18 | double wctime(void) | ||
| 19 | { | ||
| 20 | struct timeval tv; | ||
| 21 | gettimeofday(&tv, NULL); | ||
| 22 | return (tv.tv_sec + 1E-6 * tv.tv_usec); | ||
| 23 | } | ||
