aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBjoern Brandenburg <bbb@mpi-sws.org>2016-07-20 07:40:47 -0400
committerBjoern Brandenburg <bbb@mpi-sws.org>2016-07-20 07:42:41 -0400
commit98510c506a16b2a444eb6615ec8d78bc7c64e0aa (patch)
tree5e22518db1c73d6edf91f83c1ce8850403ec0092
parent7ae970e73bc1d026bbf65a82c066aab79c860fe8 (diff)
Add litmus_clock() helper
Convenient wrapper for CLOCK_MONOTONIC -> lt_t.
-rw-r--r--include/litmus.h8
-rw-r--r--src/clocks.c22
2 files changed, 26 insertions, 4 deletions
diff --git a/include/litmus.h b/include/litmus.h
index 751b5ec..0f0929b 100644
--- a/include/litmus.h
+++ b/include/litmus.h
@@ -340,6 +340,14 @@ int lt_sleep(lt_t timeout);
340 */ 340 */
341void lt_sleep_until(lt_t wake_up_time); 341void lt_sleep_until(lt_t wake_up_time);
342 342
343/** Get the current time used by the LITMUS^RT scheduler.
344 * This is just CLOCK_MONOTONIC and hence the same
345 * as monotime(), but the result is given in nanoseconds
346 * as a value of type lt_t.
347 * @return CLOCK_MONOTONIC time in nanoseconds
348 */
349lt_t litmus_clock(void);
350
343/** 351/**
344 * Obtain CPU time consumed so far 352 * Obtain CPU time consumed so far
345 * @return CPU time in seconds 353 * @return CPU time in seconds
diff --git a/src/clocks.c b/src/clocks.c
index 43838eb..bce618a 100644
--- a/src/clocks.c
+++ b/src/clocks.c
@@ -36,6 +36,20 @@ double monotime(void)
36 return (ts.tv_sec + 1E-9 * ts.tv_nsec); 36 return (ts.tv_sec + 1E-9 * ts.tv_nsec);
37} 37}
38 38
39/* Current time used by the LITMUS^RT scheduler.
40 * This is just CLOCK_MONOTONIC and hence the same
41 * as monotime(), but the result is given in nanoseconds
42 * as a value of type lt_t.*/
43lt_t litmus_clock(void)
44{
45 struct timespec ts;
46 int err;
47 err = clock_gettime(CLOCK_MONOTONIC, &ts);
48 if (err != 0)
49 perror("clock_gettime");
50 return ((lt_t) s2ns(ts.tv_sec)) + (lt_t) ts.tv_nsec;
51}
52
39static void do_sleep_until(struct timespec *ts, clockid_t clock_id) 53static void do_sleep_until(struct timespec *ts, clockid_t clock_id)
40{ 54{
41 int err; 55 int err;
@@ -56,18 +70,18 @@ static void clock_sleep_until(double wake_up_time, clockid_t clock_id)
56 /* convert from double (seconds) */ 70 /* convert from double (seconds) */
57 ts.tv_sec = (time_t) wake_up_time; 71 ts.tv_sec = (time_t) wake_up_time;
58 ts.tv_nsec = (wake_up_time - ts.tv_sec) * 1E9; 72 ts.tv_nsec = (wake_up_time - ts.tv_sec) * 1E9;
59 73
60 do_sleep_until(&ts, clock_id); 74 do_sleep_until(&ts, clock_id);
61} 75}
62 76
63/* Sleep until we've reached wake_up_time (in seconds) on the CLOCK_MONOTONIC 77/* Sleep until we've reached wake_up_time (in seconds) on the CLOCK_MONOTONIC
64 * timeline. */ 78 * timeline. */
65void sleep_until_mono(double wake_up_time) 79void sleep_until_mono(double wake_up_time)
66{ 80{
67 clock_sleep_until(wake_up_time, CLOCK_MONOTONIC); 81 clock_sleep_until(wake_up_time, CLOCK_MONOTONIC);
68} 82}
69 83
70/* Sleep until we've reached wake_up_time (in seconds) on the CLOCK_MONOTONIC 84/* Sleep until we've reached wake_up_time (in seconds) on the CLOCK_MONOTONIC
71 * timeline. */ 85 * timeline. */
72void sleep_until_wc(double wake_up_time) 86void sleep_until_wc(double wake_up_time)
73{ 87{
@@ -83,7 +97,7 @@ void lt_sleep_until(lt_t wake_up_time)
83 /* convert from double (seconds) */ 97 /* convert from double (seconds) */
84 ts.tv_sec = (time_t) ns2s(wake_up_time); 98 ts.tv_sec = (time_t) ns2s(wake_up_time);
85 ts.tv_nsec = (long) (wake_up_time - s2ns(ts.tv_sec)); 99 ts.tv_nsec = (long) (wake_up_time - s2ns(ts.tv_sec));
86 100
87 do_sleep_until(&ts, CLOCK_MONOTONIC); 101 do_sleep_until(&ts, CLOCK_MONOTONIC);
88} 102}
89 103