]> git.ozlabs.org Git - ccan/blob - ccan/time/time.h
time: time_mono() helpers.
[ccan] / ccan / time / time.h
1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #ifndef CCAN_TIME_H
3 #define CCAN_TIME_H
4 #include "config.h"
5 #include <sys/time.h>
6 #if HAVE_STRUCT_TIMESPEC
7 #include <time.h>
8 #else
9 struct timespec {
10         time_t   tv_sec;        /* seconds */
11         long     tv_nsec;       /* nanoseconds */
12 };
13 #endif
14 #include <stdint.h>
15 #include <stdbool.h>
16
17 #ifdef DEBUG
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) ") ")
25 #else
26 #define TIME_CHECK(t) (t)
27 #define TIMEREL_CHECK(t) (t)
28 #define TIMEABS_CHECK(t) (t)
29 #endif
30
31 /**
32  * struct timerel - a relative time.
33  * @ts: the actual timespec value.
34  *
35  * For example, 1 second: ts.tv_sec = 1, ts.tv_nsec = 0
36  */
37 struct timerel {
38         struct timespec ts;
39 };
40
41 /**
42  * struct timeabs - an absolue time.
43  * @ts: the actual timespec value.
44  *
45  * For example, Midnight UTC January 1st, 1970: ts.tv_sec = 0, ts.tv_nsec = 0
46  */
47 struct timeabs {
48         struct timespec ts;
49 };
50
51 /**
52  * struct timemono - a monotonic time.
53  * @ts: the actual timespec value.
54  *
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.
58  */
59 struct timemono {
60         struct timespec ts;
61 };
62
63 /**
64  * TIME_HAVE_MONOTONIC - defined if we really have a monotonic clock.
65  *
66  * Otherwise time_mono() just refers to time_now().  Your code might
67  * test this if you really need a monotonic clock.
68  */
69 #if (HAVE_CLOCK_GETTIME || HAVE_CLOCK_GETTIME_IN_LIBRT) && defined(CLOCK_MONOTONIC)
70 #define TIME_HAVE_MONOTONIC 1
71 #endif
72
73 struct timespec time_check_(struct timespec in, const char *abortstr);
74
75 /**
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).
79  *
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.
85  *
86  * Note that if ccan/time is compiled with DEBUG, then it will call this
87  * for all passed and returned times.
88  *
89  * Example:
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);
93  */
94 struct timerel timerel_check(struct timerel in, const char *abortstr);
95
96 /**
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).
100  *
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.
106  *
107  * Note that if ccan/time is compiled with DEBUG, then it will call this
108  * for all passed and returned times.
109  *
110  * Example:
111  *      printf("Now is %lu seconds since epoch\n",
112  *              (long)timeabs_check(time_now(), "time_now failed?").ts.tv_sec);
113  */
114 struct timeabs timeabs_check(struct timeabs in, const char *abortstr);
115
116 /**
117  * time_now - return the current time
118  *
119  * Example:
120  *      printf("Now is %lu seconds since epoch\n", (long)time_now().ts.tv_sec);
121  */
122 struct timeabs time_now(void);
123
124 /**
125  * time_mono - return the current monotonic time
126  *
127  * This value is only really useful for measuring time intervals.
128  *
129  * See also:
130  *      time_since()
131  */
132 struct timemono time_mono(void);
133
134 static inline bool time_greater_(struct timespec a, struct timespec b)
135 {
136         if (TIME_CHECK(a).tv_sec > TIME_CHECK(b).tv_sec)
137                 return true;
138         else if (a.tv_sec < b.tv_sec)
139                  return false;
140
141         return a.tv_nsec > b.tv_nsec;
142 }
143
144 /**
145  * time_after - is a after b?
146  * @a: one abstime.
147  * @b: another abstime.
148  *
149  * Example:
150  *      static bool timed_out(const struct timeabs *start)
151  *      {
152  *      #define TIMEOUT time_from_msec(1000)
153  *              return time_after(time_now(), timeabs_add(*start, TIMEOUT));
154  *      }
155  */
156 static inline bool time_after(struct timeabs a, struct timeabs b)
157 {
158         return time_greater_(a.ts, b.ts);
159 }
160
161 /**
162  * time_greater - is a greater than b?
163  * @a: one reltime.
164  * @b: another reltime.
165  */
166 static inline bool time_greater(struct timerel a, struct timerel b)
167 {
168         return time_greater_(a.ts, b.ts);
169 }
170
171 static inline bool time_less_(struct timespec a, struct timespec b)
172 {
173         if (TIME_CHECK(a).tv_sec < TIME_CHECK(b).tv_sec)
174                 return true;
175         else if (a.tv_sec > b.tv_sec)
176                  return false;
177
178         return a.tv_nsec < b.tv_nsec;
179 }
180
181 /**
182  * time_before - is a before b?
183  * @a: one absolute time.
184  * @b: another absolute time.
185  *
186  * Example:
187  *      static bool still_valid(const struct timeabs *start)
188  *      {
189  *      #define TIMEOUT time_from_msec(1000)
190  *              return time_before(time_now(), timeabs_add(*start, TIMEOUT));
191  *      }
192  */
193 static inline bool time_before(struct timeabs a, struct timeabs b)
194 {
195         return time_less_(a.ts, b.ts);
196 }
197
198 /**
199  * time_less - is a before b?
200  * @a: one relative time.
201  * @b: another relative time.
202  */
203 static inline bool time_less(struct timerel a, struct timerel b)
204 {
205         return time_less_(a.ts, b.ts);
206 }
207
208 /**
209  * timeabs_eq - is a equal to b?
210  * @a: one absolute time.
211  * @b: another absolute time.
212  *
213  * Example:
214  *      #include <sys/types.h>
215  *      #include <sys/wait.h>
216  *
217  *      // Can we fork in under a nanosecond?
218  *      static bool fast_fork(void)
219  *      {
220  *              struct timeabs start = time_now();
221  *              if (fork() != 0) {
222  *                      exit(0);
223  *              }
224  *              wait(NULL);
225  *              return timeabs_eq(start, time_now());
226  *      }
227  */
228 static inline bool timeabs_eq(struct timeabs a, struct timeabs b)
229 {
230         return TIMEABS_CHECK(a).ts.tv_sec == TIMEABS_CHECK(b).ts.tv_sec
231                 && a.ts.tv_nsec == b.ts.tv_nsec;
232 }
233
234 /**
235  * timerel_eq - is a equal to b?
236  * @a: one relative time.
237  * @b: another relative time.
238  *
239  * Example:
240  *      #include <sys/types.h>
241  *      #include <sys/wait.h>
242  *
243  *      // Can we fork in under a nanosecond?
244  *      static bool fast_fork(void)
245  *      {
246  *              struct timeabs start = time_now();
247  *              struct timerel diff, zero = { .ts = { 0, 0 } };
248  *              if (fork() != 0) {
249  *                      exit(0);
250  *              }
251  *              wait(NULL);
252  *              diff = time_between(start, time_now());
253  *              return timerel_eq(diff, zero);
254  *      }
255  */
256 static inline bool timerel_eq(struct timerel a, struct timerel b)
257 {
258         return TIMEREL_CHECK(a).ts.tv_sec == TIMEREL_CHECK(b).ts.tv_sec
259                 && a.ts.tv_nsec == b.ts.tv_nsec;
260 }
261
262 static inline struct timespec time_sub_(struct timespec recent,
263                                         struct timespec old)
264 {
265         struct timespec diff;
266
267         diff.tv_sec = TIME_CHECK(recent).tv_sec - TIME_CHECK(old).tv_sec;
268         if (old.tv_nsec > recent.tv_nsec) {
269                 diff.tv_sec--;
270                 diff.tv_nsec = 1000000000 + recent.tv_nsec - old.tv_nsec;
271         } else
272                 diff.tv_nsec = recent.tv_nsec - old.tv_nsec;
273
274         return TIME_CHECK(diff);
275 }
276
277 /**
278  * time_sub - subtract two relative times
279  * @a: the larger time.
280  * @b: the smaller time.
281  *
282  * This returns a well formed struct timerel of @a - @b.
283  */
284 static inline struct timerel time_sub(struct timerel a, struct timerel b)
285 {
286         struct timerel t;
287
288         t.ts = time_sub_(a.ts, b.ts);
289         return t;
290 }
291
292 /**
293  * time_between - time between two absolute times
294  * @recent: the larger time.
295  * @old: the smaller time.
296  *
297  * This returns a well formed struct timerel of @a - @b.
298  */
299 static inline struct timerel time_between(struct timeabs recent, struct timeabs old)
300 {
301         struct timerel t;
302
303         t.ts = time_sub_(recent.ts, old.ts);
304         return t;
305 }
306
307 /**
308  * timemono_between - time between two monotonic times
309  * @recent: the larger time.
310  * @old: the smaller time.
311  *
312  * This returns a well formed struct timerel of @recent - @old.
313  */
314 static inline struct timerel timemono_between(struct timemono recent,
315                                               struct timemono old)
316 {
317         struct timerel t;
318
319         t.ts = time_sub_(recent.ts, old.ts);
320         return t;
321 }
322
323 /**
324  * timeabs_sub - subtract a relative time from an absolute time
325  * @abs: the absolute time.
326  * @rel: the relative time.
327  *
328  * This returns a well formed struct timeabs of @a - @b.
329  *
330  * Example:
331  *      // We do one every second.
332  *      static struct timeabs previous_time(void)
333  *      {
334  *              return timeabs_sub(time_now(), time_from_msec(1000));
335  *      }
336  */
337 static inline struct timeabs timeabs_sub(struct timeabs abs, struct timerel rel)
338 {
339         struct timeabs t;
340
341         t.ts = time_sub_(abs.ts, rel.ts);
342         return t;
343 }
344
345 static inline struct timespec time_add_(struct timespec a, struct timespec b)
346 {
347         struct timespec sum;
348
349         sum.tv_sec = TIME_CHECK(a).tv_sec + TIME_CHECK(b).tv_sec;
350         sum.tv_nsec = a.tv_nsec + b.tv_nsec;
351         if (sum.tv_nsec >= 1000000000) {
352                 sum.tv_sec++;
353                 sum.tv_nsec -= 1000000000;
354         }
355         return TIME_CHECK(sum);
356 }
357
358 /**
359  * timeabs_add - add a relative to an absolute time
360  * @a: the absolute time.
361  * @b: a relative time.
362  *
363  * The times must not overflow, or the results are undefined.
364  *
365  * Example:
366  *      // We do one every second.
367  *      static struct timeabs next_time(void)
368  *      {
369  *              return timeabs_add(time_now(), time_from_msec(1000));
370  *      }
371  */
372 static inline struct timeabs timeabs_add(struct timeabs a, struct timerel b)
373 {
374         struct timeabs t;
375
376         t.ts = time_add_(a.ts, b.ts);
377         return t;
378 }
379
380 /**
381  * timerel_add - add two relative times
382  * @a: one relative time.
383  * @b: another relative time.
384  *
385  * The times must not overflow, or the results are undefined.
386  *
387  * Example:
388  *      static struct timerel double_time(struct timerel a)
389  *      {
390  *              return timerel_add(a, a);
391  *      }
392  */
393 static inline struct timerel timerel_add(struct timerel a, struct timerel b)
394 {
395         struct timerel t;
396
397         t.ts = time_add_(a.ts, b.ts);
398         return t;
399 }
400
401 /**
402  * time_divide - divide a time by a value.
403  * @t: a time.
404  * @div: number to divide it by.
405  *
406  * Example:
407  *      // How long does it take to do a fork?
408  *      static struct timerel forking_time(void)
409  *      {
410  *              struct timeabs start = time_now();
411  *              unsigned int i;
412  *
413  *              for (i = 0; i < 1000; i++) {
414  *                      if (fork() != 0) {
415  *                              exit(0);
416  *                      }
417  *                      wait(NULL);
418  *              }
419  *              return time_divide(time_between(time_now(), start), i);
420  *      }
421  */
422 struct timerel time_divide(struct timerel t, unsigned long div);
423
424 /**
425  * time_multiply - multiply a time by a value.
426  * @t: a relative time.
427  * @mult: number to multiply it by.
428  *
429  * Example:
430  *      ...
431  *      printf("Time to do 100000 forks would be %u sec\n",
432  *             (unsigned)time_multiply(forking_time(), 1000000).ts.tv_sec);
433  */
434 struct timerel time_multiply(struct timerel t, unsigned long mult);
435
436 /**
437  * time_to_sec - return number of seconds
438  * @t: a time
439  *
440  * It's often more convenient to deal with time values as seconds.
441  * Note that this will fit into an unsigned 32-bit variable if it's a
442  * time of less than about 136 years.
443  *
444  * Example:
445  *      ...
446  *      printf("Forking time is %u sec\n",
447  *             (unsigned)time_to_sec(forking_time()));
448  */
449 static inline uint64_t time_to_sec(struct timerel t)
450 {
451         return t.ts.tv_sec;
452 }
453
454 /**
455  * time_to_msec - return number of milliseconds
456  * @t: a relative time
457  *
458  * It's often more convenient to deal with time values as
459  * milliseconds.  Note that this will fit into a 32-bit variable if
460  * it's a time difference of less than ~7 weeks.
461  *
462  * Example:
463  *      ...
464  *      printf("Forking time is %u msec\n",
465  *             (unsigned)time_to_msec(forking_time()));
466  */
467 static inline uint64_t time_to_msec(struct timerel t)
468 {
469         uint64_t msec;
470
471         msec = TIMEREL_CHECK(t).ts.tv_nsec/1000000 + (uint64_t)t.ts.tv_sec*1000;
472         return msec;
473 }
474
475 /**
476  * time_to_usec - return number of microseconds
477  * @t: a relative time
478  *
479  * It's often more convenient to deal with time values as
480  * microseconds.  Note that this will fit into a 32-bit variable if
481  * it's a time difference of less than ~1 hour.
482  *
483  * Example:
484  *      ...
485  *      printf("Forking time is %u usec\n",
486  *             (unsigned)time_to_usec(forking_time()));
487  *
488  */
489 static inline uint64_t time_to_usec(struct timerel t)
490 {
491         uint64_t usec;
492
493         usec = TIMEREL_CHECK(t).ts.tv_nsec/1000 + (uint64_t)t.ts.tv_sec*1000000;
494         return usec;
495 }
496
497 /**
498  * time_to_nsec - return number of nanoseconds
499  * @t: a relative time
500  *
501  * It's sometimes more convenient to deal with time values as
502  * nanoseconds.  Note that this will fit into a 32-bit variable if
503  * it's a time difference of less than ~4 seconds.
504  *
505  * Example:
506  *      ...
507  *      printf("Forking time is %u nsec\n",
508  *             (unsigned)time_to_nsec(forking_time()));
509  *
510  */
511 static inline uint64_t time_to_nsec(struct timerel t)
512 {
513         uint64_t nsec;
514
515         nsec = TIMEREL_CHECK(t).ts.tv_nsec + (uint64_t)t.ts.tv_sec * 1000000000;
516         return nsec;
517 }
518
519 /**
520  * time_from_sec - convert seconds to a relative time
521  * @msec: time in seconds
522  *
523  * Example:
524  *      // 1 minute timeout
525  *      #define TIMEOUT time_from_sec(60)
526  */
527 static inline struct timerel time_from_sec(uint64_t sec)
528 {
529         struct timerel t;
530
531         t.ts.tv_nsec = 0;
532         t.ts.tv_sec = sec;
533         return TIMEREL_CHECK(t);
534 }
535
536 /**
537  * time_from_msec - convert milliseconds to a relative time
538  * @msec: time in milliseconds
539  *
540  * Example:
541  *      // 1/2 second timeout
542  *      #define TIMEOUT time_from_msec(500)
543  */
544 static inline struct timerel time_from_msec(uint64_t msec)
545 {
546         struct timerel t;
547
548         t.ts.tv_nsec = (msec % 1000) * 1000000;
549         t.ts.tv_sec = msec / 1000;
550         return TIMEREL_CHECK(t);
551 }
552
553 /**
554  * time_from_usec - convert microseconds to a relative time
555  * @usec: time in microseconds
556  *
557  * Example:
558  *      // 1/2 second timeout
559  *      #define TIMEOUT time_from_usec(500000)
560  */
561 static inline struct timerel time_from_usec(uint64_t usec)
562 {
563         struct timerel t;
564
565         t.ts.tv_nsec = (usec % 1000000) * 1000;
566         t.ts.tv_sec = usec / 1000000;
567         return TIMEREL_CHECK(t);
568 }
569
570 /**
571  * time_from_nsec - convert nanoseconds to a relative time
572  * @nsec: time in nanoseconds
573  *
574  * Example:
575  *      // 1/2 second timeout
576  *      #define TIMEOUT time_from_nsec(500000000)
577  */
578 static inline struct timerel time_from_nsec(uint64_t nsec)
579 {
580         struct timerel t;
581
582         t.ts.tv_nsec = nsec % 1000000000;
583         t.ts.tv_sec = nsec / 1000000000;
584         return TIMEREL_CHECK(t);
585 }
586
587 static inline struct timeval timespec_to_timeval(struct timespec ts)
588 {
589         struct timeval tv;
590         tv.tv_sec = ts.tv_sec;
591         tv.tv_usec = ts.tv_nsec / 1000;
592         return tv;
593 }
594
595 /**
596  * timerel_to_timeval - convert a relative time to a timeval.
597  * @t: a relative time.
598  *
599  * Example:
600  *      struct timerel t = { { 100, 0 } }; // 100 seconds
601  *      struct timeval tv;
602  *
603  *      tv = timerel_to_timeval(t);
604  *      printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec);
605  */
606 static inline struct timeval timerel_to_timeval(struct timerel t)
607 {
608         return timespec_to_timeval(t.ts);
609 }
610
611 /**
612  * timeabs_to_timeval - convert an absolute time to a timeval.
613  * @t: an absolute time.
614  *
615  * Example:
616  *      struct timeval tv;
617  *
618  *      tv = timeabs_to_timeval(time_now());
619  *      printf("time = %lu.%06u\n", (long)tv.tv_sec, (int)tv.tv_usec);
620  */
621 static inline struct timeval timeabs_to_timeval(struct timeabs t)
622 {
623         return timespec_to_timeval(t.ts);
624 }
625
626 static inline struct timespec timeval_to_timespec(struct timeval tv)
627 {
628         struct timespec ts;
629         ts.tv_sec = tv.tv_sec;
630         ts.tv_nsec = tv.tv_usec * 1000;
631         return ts;
632 }
633
634 /**
635  * timeval_to_timerel - convert a timeval to a relative time.
636  * @tv: a timeval.
637  *
638  * Example:
639  *      struct timeval tv = { 0, 500 };
640  *      struct timerel t;
641  *
642  *      t = timeval_to_timerel(tv);
643  *      printf("timerel = %lu.%09lu\n", (long)t.ts.tv_sec, (long)t.ts.tv_nsec);
644  */
645 static inline struct timerel timeval_to_timerel(struct timeval tv)
646 {
647         struct timerel t;
648         t.ts = timeval_to_timespec(tv);
649         return TIMEREL_CHECK(t);
650 }
651
652 /**
653  * timeval_to_timeabs - convert a timeval to an absolute time.
654  * @tv: a timeval.
655  *
656  * Example:
657  *      struct timeval tv = { 1401762008, 500 };
658  *      struct timeabs t;
659  *
660  *      t = timeval_to_timeabs(tv);
661  *      printf("timeabs = %lu.%09lu\n", (long)t.ts.tv_sec, (long)t.ts.tv_nsec);
662  */
663 static inline struct timeabs timeval_to_timeabs(struct timeval tv)
664 {
665         struct timeabs t;
666         t.ts = timeval_to_timespec(tv);
667         return TIMEABS_CHECK(t);
668 }
669 #endif /* CCAN_TIME_H */