1 /* Licensed under BSD-MIT - see LICENSE file for details */
6 #if HAVE_STRUCT_TIMESPEC
10 time_t tv_sec; /* seconds */
11 long tv_nsec; /* nanoseconds */
18 #include <ccan/str/str.h>
19 #define TIME_CHECK(t) \
20 time_check_((t), __FILE__ ":" stringify(__LINE__) " (" stringify(t) ") ")
21 #define TIMEREL_CHECK(t) \
22 timerel_check((t), __FILE__ ":" stringify(__LINE__) " (" stringify(t) ") ")
23 #define TIMEABS_CHECK(t) \
24 timeabs_check((t), __FILE__ ":" stringify(__LINE__) " (" stringify(t) ") ")
26 #define TIME_CHECK(t) (t)
27 #define TIMEREL_CHECK(t) (t)
28 #define TIMEABS_CHECK(t) (t)
32 * struct timerel - a relative time.
33 * @ts: the actual timespec value.
35 * For example, 1 second: ts.tv_sec = 1, ts.tv_nsec = 0
42 * struct timeabs - an absolue time.
43 * @ts: the actual timespec value.
45 * For example, Midnight UTC January 1st, 1970: ts.tv_sec = 0, ts.tv_nsec = 0
52 * struct timemono - a monotonic time.
53 * @ts: the actual timespec value.
55 * This comes from the monotonic clock (if available), so it's useful
56 * for measuring intervals as it won't change even if the system clock
57 * is moved for some reason.
64 * TIME_HAVE_MONOTONIC - defined if we really have a monotonic clock.
66 * Otherwise time_mono() just refers to time_now(). Your code might
67 * test this if you really need a monotonic clock.
69 #if HAVE_CLOCK_GETTIME && defined(CLOCK_MONOTONIC)
70 #define TIME_HAVE_MONOTONIC 1
73 struct timespec time_check_(struct timespec in, const char *abortstr);
76 * timerel_check - check if a relative time is malformed.
77 * @in: the relative time to check (returned)
78 * @abortstr: the string to print to stderr before aborting (if set).
80 * This can be used to make sure a time isn't negative and doesn't
81 * have a tv_nsec >= 1000000000. If it is, and @abortstr is non-NULL,
82 * that will be printed and abort() is called. Otherwise, if
83 * @abortstr is NULL then the returned timerel will be normalized and
84 * tv_sec set to 0 if it was negative.
86 * Note that if ccan/time is compiled with DEBUG, then it will call this
87 * for all passed and returned times.
90 * printf("Time to calc this was %lu nanoseconds\n",
91 * (long)timerel_check(time_between(time_now(), time_now()),
92 * "time_now() failed?").ts.tv_nsec);
94 struct timerel timerel_check(struct timerel in, const char *abortstr);
97 * timeabs_check - check if an absolute time is malformed.
98 * @in: the relative time to check (returned)
99 * @abortstr: the string to print to stderr before aborting (if set).
101 * This can be used to make sure a time isn't negative and doesn't
102 * have a tv_nsec >= 1000000000. If it is, and @abortstr is non-NULL,
103 * that will be printed and abort() is called. Otherwise, if
104 * @abortstr is NULL then the returned timeabs will be normalized and
105 * tv_sec set to 0 if it was negative.
107 * Note that if ccan/time is compiled with DEBUG, then it will call this
108 * for all passed and returned times.
111 * printf("Now is %lu seconds since epoch\n",
112 * (long)timeabs_check(time_now(), "time_now failed?").ts.tv_sec);
114 struct timeabs timeabs_check(struct timeabs in, const char *abortstr);
117 * time_now - return the current time
120 * printf("Now is %lu seconds since epoch\n", (long)time_now().ts.tv_sec);
122 struct timeabs time_now(void);
125 * time_mono - return the current monotonic time
127 * This value is only really useful for measuring time intervals.
132 struct timemono time_mono(void);
134 static inline bool time_greater_(struct timespec a, struct timespec b)
136 if (TIME_CHECK(a).tv_sec > TIME_CHECK(b).tv_sec)
138 else if (a.tv_sec < b.tv_sec)
141 return a.tv_nsec > b.tv_nsec;
145 * time_after - is a after b?
147 * @b: another abstime.
150 * static bool timed_out(const struct timeabs *start)
152 * #define TIMEOUT time_from_msec(1000)
153 * return time_after(time_now(), timeabs_add(*start, TIMEOUT));
156 static inline bool time_after(struct timeabs a, struct timeabs b)
158 return time_greater_(a.ts, b.ts);
162 * time_greater - is a greater than b?
164 * @b: another reltime.
166 static inline bool time_greater(struct timerel a, struct timerel b)
168 return time_greater_(a.ts, b.ts);
171 static inline bool time_less_(struct timespec a, struct timespec b)
173 if (TIME_CHECK(a).tv_sec < TIME_CHECK(b).tv_sec)
175 else if (a.tv_sec > b.tv_sec)
178 return a.tv_nsec < b.tv_nsec;
182 * time_before - is a before b?
183 * @a: one absolute time.
184 * @b: another absolute time.
187 * static bool still_valid(const struct timeabs *start)
189 * #define TIMEOUT time_from_msec(1000)
190 * return time_before(time_now(), timeabs_add(*start, TIMEOUT));
193 static inline bool time_before(struct timeabs a, struct timeabs b)
195 return time_less_(a.ts, b.ts);
199 * time_less - is a before b?
200 * @a: one relative time.
201 * @b: another relative time.
203 static inline bool time_less(struct timerel a, struct timerel b)
205 return time_less_(a.ts, b.ts);
209 * timeabs_eq - is a equal to b?
210 * @a: one absolute time.
211 * @b: another absolute time.
214 * #include <sys/types.h>
215 * #include <sys/wait.h>
217 * // Can we fork in under a nanosecond?
218 * static bool fast_fork(void)
220 * struct timeabs start = time_now();
225 * return timeabs_eq(start, time_now());
228 static inline bool timeabs_eq(struct timeabs a, struct timeabs b)
230 return TIMEABS_CHECK(a).ts.tv_sec == TIMEABS_CHECK(b).ts.tv_sec
231 && a.ts.tv_nsec == b.ts.tv_nsec;
235 * timerel_eq - is a equal to b?
236 * @a: one relative time.
237 * @b: another relative time.
240 * #include <sys/types.h>
241 * #include <sys/wait.h>
243 * // Can we fork in under a nanosecond?
244 * static bool fast_fork(void)
246 * struct timeabs start = time_now();
247 * struct timerel diff, zero = { .ts = { 0, 0 } };
252 * diff = time_between(time_now(), start);
253 * return timerel_eq(diff, zero);
256 static inline bool timerel_eq(struct timerel a, struct timerel b)
258 return TIMEREL_CHECK(a).ts.tv_sec == TIMEREL_CHECK(b).ts.tv_sec
259 && a.ts.tv_nsec == b.ts.tv_nsec;
262 static inline struct timespec time_sub_(struct timespec recent,
265 struct timespec diff;
267 diff.tv_sec = TIME_CHECK(recent).tv_sec - TIME_CHECK(old).tv_sec;
268 if (old.tv_nsec > recent.tv_nsec) {
270 diff.tv_nsec = 1000000000 + recent.tv_nsec - old.tv_nsec;
272 diff.tv_nsec = recent.tv_nsec - old.tv_nsec;
274 return TIME_CHECK(diff);
278 * time_sub - subtract two relative times
279 * @a: the larger time.
280 * @b: the smaller time.
282 * This returns a well formed struct timerel of @a - @b.
284 static inline struct timerel time_sub(struct timerel a, struct timerel b)
288 t.ts = time_sub_(a.ts, b.ts);
293 * time_between - time between two absolute times
294 * @recent: the larger time.
295 * @old: the smaller time.
297 * This returns a well formed struct timerel of @a - @b.
299 static inline struct timerel time_between(struct timeabs recent, struct timeabs old)
303 t.ts = time_sub_(recent.ts, old.ts);
308 * timemono_between - time between two monotonic times
309 * @recent: the larger time.
310 * @old: the smaller time.
312 * This returns a well formed struct timerel of @recent - @old.
314 static inline struct timerel timemono_between(struct timemono recent,
319 t.ts = time_sub_(recent.ts, old.ts);
324 * timemono_since - elapsed monotonic time since @old
325 * @old: a monotonic time from the past.
327 static inline struct timerel timemono_since(struct timemono old)
329 struct timemono now = time_mono();
331 return timemono_between(now, old);
335 * timeabs_sub - subtract a relative time from an absolute time
336 * @abs: the absolute time.
337 * @rel: the relative time.
339 * This returns a well formed struct timeabs of @a - @b.
342 * // We do one every second.
343 * static struct timeabs previous_time(void)
345 * return timeabs_sub(time_now(), time_from_msec(1000));
348 static inline struct timeabs timeabs_sub(struct timeabs abs, struct timerel rel)
352 t.ts = time_sub_(abs.ts, rel.ts);
356 static inline struct timespec time_add_(struct timespec a, struct timespec b)
360 sum.tv_sec = TIME_CHECK(a).tv_sec + TIME_CHECK(b).tv_sec;
361 sum.tv_nsec = a.tv_nsec + b.tv_nsec;
362 if (sum.tv_nsec >= 1000000000) {
364 sum.tv_nsec -= 1000000000;
366 return TIME_CHECK(sum);
370 * timeabs_add - add a relative to an absolute time
371 * @a: the absolute time.
372 * @b: a relative time.
374 * The times must not overflow, or the results are undefined.
377 * // We do one every second.
378 * static struct timeabs next_time(void)
380 * return timeabs_add(time_now(), time_from_msec(1000));
383 static inline struct timeabs timeabs_add(struct timeabs a, struct timerel b)
387 t.ts = time_add_(a.ts, b.ts);
392 * timemono_add - add a relative to a monotonic time
393 * @a: the monotonic time.
394 * @b: a relative time.
396 * The times must not overflow, or the results are undefined.
399 * // We do one every second.
400 * static struct timemono next_timem(void)
402 * return timemono_add(time_mono(), time_from_msec(1000));
405 static inline struct timemono timemono_add(struct timemono a, struct timerel b)
409 t.ts = time_add_(a.ts, b.ts);
414 * timerel_add - add two relative times
415 * @a: one relative time.
416 * @b: another relative time.
418 * The times must not overflow, or the results are undefined.
421 * static struct timerel double_time(struct timerel a)
423 * return timerel_add(a, a);
426 static inline struct timerel timerel_add(struct timerel a, struct timerel b)
430 t.ts = time_add_(a.ts, b.ts);
435 * time_divide - divide a time by a value.
437 * @div: number to divide it by.
440 * // How long does it take to do a fork?
441 * static struct timerel forking_time(void)
443 * struct timeabs start = time_now();
446 * for (i = 0; i < 1000; i++) {
452 * return time_divide(time_between(time_now(), start), i);
455 struct timerel time_divide(struct timerel t, unsigned long div);
458 * time_multiply - multiply a time by a value.
459 * @t: a relative time.
460 * @mult: number to multiply it by.
464 * printf("Time to do 100000 forks would be %u sec\n",
465 * (unsigned)time_multiply(forking_time(), 1000000).ts.tv_sec);
467 struct timerel time_multiply(struct timerel t, unsigned long mult);
470 * time_to_sec - return number of seconds
473 * It's often more convenient to deal with time values as seconds.
474 * Note that this will fit into an unsigned 32-bit variable if it's a
475 * time of less than about 136 years.
479 * printf("Forking time is %u sec\n",
480 * (unsigned)time_to_sec(forking_time()));
482 static inline uint64_t time_to_sec(struct timerel t)
488 * time_to_msec - return number of milliseconds
489 * @t: a relative time
491 * It's often more convenient to deal with time values as
492 * milliseconds. Note that this will fit into a 32-bit variable if
493 * it's a time difference of less than ~7 weeks.
497 * printf("Forking time is %u msec\n",
498 * (unsigned)time_to_msec(forking_time()));
500 static inline uint64_t time_to_msec(struct timerel t)
504 msec = TIMEREL_CHECK(t).ts.tv_nsec/1000000 + (uint64_t)t.ts.tv_sec*1000;
509 * time_to_usec - return number of microseconds
510 * @t: a relative time
512 * It's often more convenient to deal with time values as
513 * microseconds. Note that this will fit into a 32-bit variable if
514 * it's a time difference of less than ~1 hour.
518 * printf("Forking time is %u usec\n",
519 * (unsigned)time_to_usec(forking_time()));
522 static inline uint64_t time_to_usec(struct timerel t)
526 usec = TIMEREL_CHECK(t).ts.tv_nsec/1000 + (uint64_t)t.ts.tv_sec*1000000;
531 * time_to_nsec - return number of nanoseconds
532 * @t: a relative time
534 * It's sometimes more convenient to deal with time values as
535 * nanoseconds. Note that this will fit into a 32-bit variable if
536 * it's a time difference of less than ~4 seconds.
540 * printf("Forking time is %u nsec\n",
541 * (unsigned)time_to_nsec(forking_time()));
544 static inline uint64_t time_to_nsec(struct timerel t)
548 nsec = TIMEREL_CHECK(t).ts.tv_nsec + (uint64_t)t.ts.tv_sec * 1000000000;
553 * time_from_sec - convert seconds to a relative time
554 * @msec: time in seconds
557 * // 1 minute timeout
558 * #define TIMEOUT time_from_sec(60)
560 static inline struct timerel time_from_sec(uint64_t sec)
566 return TIMEREL_CHECK(t);
570 * time_from_msec - convert milliseconds to a relative time
571 * @msec: time in milliseconds
574 * // 1/2 second timeout
575 * #define TIMEOUT time_from_msec(500)
577 static inline struct timerel time_from_msec(uint64_t msec)
581 t.ts.tv_nsec = (msec % 1000) * 1000000;
582 t.ts.tv_sec = msec / 1000;
583 return TIMEREL_CHECK(t);
587 * time_from_usec - convert microseconds to a relative time
588 * @usec: time in microseconds
591 * // 1/2 second timeout
592 * #define TIMEOUT time_from_usec(500000)
594 static inline struct timerel time_from_usec(uint64_t usec)
598 t.ts.tv_nsec = (usec % 1000000) * 1000;
599 t.ts.tv_sec = usec / 1000000;
600 return TIMEREL_CHECK(t);
604 * time_from_nsec - convert nanoseconds to a relative time
605 * @nsec: time in nanoseconds
608 * // 1/2 second timeout
609 * #define TIMEOUT time_from_nsec(500000000)
611 static inline struct timerel time_from_nsec(uint64_t nsec)
615 t.ts.tv_nsec = nsec % 1000000000;
616 t.ts.tv_sec = nsec / 1000000000;
617 return TIMEREL_CHECK(t);
620 static inline struct timeval timespec_to_timeval(struct timespec ts)
623 tv.tv_sec = ts.tv_sec;
624 tv.tv_usec = ts.tv_nsec / 1000;
629 * timerel_to_timeval - convert a relative time to a timeval.
630 * @t: a relative time.
633 * struct timerel t = { { 100, 0 } }; // 100 seconds
636 * tv = timerel_to_timeval(t);
637 * printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec);
639 static inline struct timeval timerel_to_timeval(struct timerel t)
641 return timespec_to_timeval(t.ts);
645 * timeabs_to_timeval - convert an absolute time to a timeval.
646 * @t: an absolute time.
651 * tv = timeabs_to_timeval(time_now());
652 * printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec);
654 static inline struct timeval timeabs_to_timeval(struct timeabs t)
656 return timespec_to_timeval(t.ts);
659 static inline struct timespec timeval_to_timespec(struct timeval tv)
662 ts.tv_sec = tv.tv_sec;
663 ts.tv_nsec = tv.tv_usec * 1000;
668 * timeval_to_timerel - convert a timeval to a relative time.
672 * struct timeval tv = { 0, 500 };
675 * t = timeval_to_timerel(tv);
676 * printf("timerel = %lu.%09lu\n", (long)t.ts.tv_sec, (long)t.ts.tv_nsec);
678 static inline struct timerel timeval_to_timerel(struct timeval tv)
681 t.ts = timeval_to_timespec(tv);
682 return TIMEREL_CHECK(t);
686 * timeval_to_timeabs - convert a timeval to an absolute time.
690 * struct timeval tv = { 1401762008, 500 };
693 * t = timeval_to_timeabs(tv);
694 * printf("timeabs = %lu.%09lu\n", (long)t.ts.tv_sec, (long)t.ts.tv_nsec);
696 static inline struct timeabs timeval_to_timeabs(struct timeval tv)
699 t.ts = timeval_to_timespec(tv);
700 return TIMEABS_CHECK(t);
702 #endif /* CCAN_TIME_H */