Fix "clock error" due to machine reboot.
[darkstat] / now.c
1 /* darkstat 3
2 * copyright (c) 2012-2014 Emil Mikulic.
3 *
4 * now.c: a cache of the current time.
5 *
6 * Permission to use, copy, modify, and distribute this file for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 #include "err.h"
19 #include "now.h"
20 #include "str.h"
21
22 #include <assert.h>
23 #include <string.h>
24 #include <time.h>
25
26 #ifdef __MACH__ /* Fake up clock_gettime() on OS X. */
27 # include <sys/time.h>
28 # include <inttypes.h>
29 # include <mach/mach.h>
30 # include <mach/mach_time.h>
31
32 typedef int clockid_t;
33 # define CLOCK_REALTIME 0
34 # define CLOCK_MONOTONIC 1
35
36 static uint64_t mono_first = 0;
37
38 int clock_gettime(clockid_t clk_id, struct timespec *tp) {
39 if (clk_id == CLOCK_REALTIME) {
40 struct timeval tv;
41 gettimeofday(&tv, NULL);
42 tp->tv_sec = tv.tv_sec;
43 tp->tv_nsec = tv.tv_usec * 1000;
44 return 0;
45 }
46 if (clk_id == CLOCK_MONOTONIC) {
47 uint64_t t = mach_absolute_time();
48 mach_timebase_info_data_t timebase;
49 mach_timebase_info(&timebase);
50 if (!mono_first) {
51 mono_first = t;
52 }
53 uint64_t tdiff = (t - mono_first) * timebase.numer / timebase.denom;
54 tp->tv_sec = tdiff / 1000000000;
55 tp->tv_nsec = tdiff % 1000000000;
56 return 0;
57 }
58 return -1;
59 }
60 #endif /* __MACH__ */
61
62 static struct timespec clock_real, clock_mono;
63 static int now_initialized = 0;
64
65 time_t now_real(void) {
66 assert(now_initialized);
67 return clock_real.tv_sec;
68 }
69
70 time_t now_mono(void) {
71 assert(now_initialized);
72 return clock_mono.tv_sec;
73 }
74
75 static int before(const struct timespec *a, const struct timespec *b) {
76 if (a->tv_sec < b->tv_sec)
77 return 1;
78 if (a->tv_sec == b->tv_sec && a->tv_nsec < b->tv_nsec)
79 return 1;
80 return 0;
81 }
82
83 static void warn_backwards(const char *name,
84 const struct timespec * const t0,
85 const struct timespec * const t1) {
86 verbosef("%s clock went backwards from %lld.%09lld to %lld.%09lld",
87 name,
88 (lld)t0->tv_sec,
89 (lld)t0->tv_nsec,
90 (lld)t1->tv_sec,
91 (lld)t1->tv_nsec);
92 }
93
94 static void clock_update(const clockid_t clk_id,
95 struct timespec *dest,
96 const char *name) {
97 struct timespec t;
98
99 clock_gettime(clk_id, &t);
100 if (now_initialized && before(&t, dest)) {
101 warn_backwards(name, &t, dest);
102 }
103 memcpy(dest, &t, sizeof(t));
104 }
105
106 static void all_clocks_update(void) {
107 clock_update(CLOCK_REALTIME, &clock_real, "realtime");
108 clock_update(CLOCK_MONOTONIC, &clock_mono, "monotonic");
109 }
110
111 void now_init(void) {
112 assert(!now_initialized);
113 all_clocks_update();
114 now_initialized = 1;
115 }
116
117 void now_update(void) {
118 assert(now_initialized);
119 all_clocks_update();
120 }
121
122 time_t mono_to_real(const int64_t t) {
123 assert(now_initialized);
124 return (time_t)(t - (int64_t)clock_mono.tv_sec + (int64_t)clock_real.tv_sec);
125 }
126
127 int64_t real_to_mono(const time_t t) {
128 assert(now_initialized);
129 return (int64_t)(t - clock_real.tv_sec + clock_mono.tv_sec);
130 }
131
132 void timer_start(struct timespec *t) {
133 clock_gettime(CLOCK_MONOTONIC, t);
134 }
135
136 static int64_t ts_diff(const struct timespec * const a,
137 const struct timespec * const b) {
138 return (int64_t)(a->tv_sec - b->tv_sec) * 1000000000 +
139 a->tv_nsec - b->tv_nsec;
140 }
141
142 void timer_stop(const struct timespec * const t0,
143 const int64_t nsec,
144 const char *warning) {
145 struct timespec t1;
146 int64_t diff;
147
148 clock_gettime(CLOCK_MONOTONIC, &t1);
149 if (before(&t1, t0)) {
150 warn_backwards("monotonic timer", t0, &t1);
151 return;
152 }
153 diff = ts_diff(&t1, t0);
154 if (diff > nsec) {
155 warnx("%s (took %lld nsec, over threshold of %lld nsec)",
156 warning,
157 (lld)diff,
158 (lld)nsec);
159 }
160 }
161
162 /* vim:set ts=3 sw=3 tw=80 et: */