aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBjoern B. Brandenburg <bbb@cs.unc.edu>2011-01-30 12:18:26 -0500
committerBjoern B. Brandenburg <bbb@cs.unc.edu>2011-02-03 17:40:16 -0500
commit86b445e39c3c7eed076b01795b17f34481517288 (patch)
tree88db1c398fde6882328bcba74fb49945fa4bce24
parent46f3b11a1f49d49120415b56b6f627c66e4b4d1b (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.c16
-rw-r--r--include/litmus.h6
-rw-r--r--src/clocks.c23
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
14static 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
24static double wctime()
25{
26 struct timeval tv;
27 gettimeofday(&tv, NULL);
28 return (tv.tv_sec + 1E-6 * tv.tv_usec);
29}
30 14
31static void usage(char *error) { 15static 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 */
115double cputime(void);
116
117/* wall-clock time in seconds */
118double wctime(void);
119
114/* semaphore allocation */ 120/* semaphore allocation */
115 121
116static inline int open_fmlp_sem(int fd, int name) 122static 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 */
7double 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 */
18double wctime(void)
19{
20 struct timeval tv;
21 gettimeofday(&tv, NULL);
22 return (tv.tv_sec + 1E-6 * tv.tv_usec);
23}