2 This file is part of GNUnet.
3 Copyright (C) 2001-2013, 2018 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
23 * @author Christian Grothoff
24 * @brief functions for handling time and time arithmetic
27 #include "gnunet_util_lib.h"
28 #if __STDC_NO_ATOMICS__
31 #ifdef HAVE_STDATOMIC_H
32 #include <stdatomic.h>
33 #define ATOMIC _Atomic
35 #define __STDC_NO_ATOMICS__ 1
40 #define LOG(kind, ...) GNUNET_log_from (kind, "util-time", __VA_ARGS__)
43 * Variable used to simulate clock skew. Used for testing, never in production.
45 static long long timestamp_offset;
48 * Set the timestamp offset for this instance.
50 * @param offset the offset to skew the locale time by
53 GNUNET_TIME_set_offset (long long offset)
55 timestamp_offset = offset;
60 * Get the timestamp offset for this instance.
62 * @return the offset we currently skew the locale time by
65 GNUNET_TIME_get_offset ()
67 return timestamp_offset;
72 * Round a time value so that it is suitable for transmission
75 * @param at time to round
76 * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
77 * it was just now rounded
80 GNUNET_TIME_round_abs (struct GNUNET_TIME_Absolute *at)
82 if (at->abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
84 if (0 == at->abs_value_us % 1000000)
86 at->abs_value_us -= at->abs_value_us % 1000000;
92 * Round a time value so that it is suitable for transmission
95 * @param rt time to round
96 * @return #GNUNET_OK if time was already rounded, #GNUNET_NO if
97 * it was just now rounded
100 GNUNET_TIME_round_rel (struct GNUNET_TIME_Relative *rt)
102 if (rt->rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
104 if (0 == rt->rel_value_us % 1000000)
106 rt->rel_value_us -= rt->rel_value_us % 1000000;
112 * Get the current time (works just as "time", just that we use the
113 * unit of time that the cron-jobs use (and is 64 bit)).
115 * @return the current time
117 struct GNUNET_TIME_Absolute
118 GNUNET_TIME_absolute_get ()
120 struct GNUNET_TIME_Absolute ret;
123 gettimeofday (&tv, NULL);
124 ret.abs_value_us = (uint64_t) (((uint64_t) tv.tv_sec * 1000LL * 1000LL)
125 + ((uint64_t) tv.tv_usec))
132 * Return relative time of 0ms.
134 struct GNUNET_TIME_Relative
135 GNUNET_TIME_relative_get_zero_ ()
137 static struct GNUNET_TIME_Relative zero;
144 * Return absolute time of 0ms.
146 struct GNUNET_TIME_Absolute
147 GNUNET_TIME_absolute_get_zero_ ()
149 static struct GNUNET_TIME_Absolute zero;
156 * Return relative time of 1us.
158 struct GNUNET_TIME_Relative
159 GNUNET_TIME_relative_get_unit_ ()
161 static struct GNUNET_TIME_Relative one = { 1 };
168 * Return relative time of 1ms.
170 struct GNUNET_TIME_Relative
171 GNUNET_TIME_relative_get_millisecond_ ()
173 static struct GNUNET_TIME_Relative one = { 1000 };
180 * Return relative time of 1s.
182 struct GNUNET_TIME_Relative
183 GNUNET_TIME_relative_get_second_ ()
185 static struct GNUNET_TIME_Relative one = { 1000 * 1000LL };
192 * Return relative time of 1 minute.
194 struct GNUNET_TIME_Relative
195 GNUNET_TIME_relative_get_minute_ ()
197 static struct GNUNET_TIME_Relative one = { 60 * 1000 * 1000LL };
204 * Return relative time of 1 hour.
206 struct GNUNET_TIME_Relative
207 GNUNET_TIME_relative_get_hour_ ()
209 static struct GNUNET_TIME_Relative one = { 60 * 60 * 1000 * 1000LL };
218 struct GNUNET_TIME_Relative
219 GNUNET_TIME_relative_get_forever_ ()
221 static struct GNUNET_TIME_Relative forever = { UINT64_MAX };
230 struct GNUNET_TIME_Absolute
231 GNUNET_TIME_absolute_get_forever_ ()
233 static struct GNUNET_TIME_Absolute forever = { UINT64_MAX };
240 * Convert relative time to an absolute time in the
243 * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
245 struct GNUNET_TIME_Absolute
246 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
248 struct GNUNET_TIME_Absolute ret;
250 if (rel.rel_value_us == UINT64_MAX)
251 return GNUNET_TIME_UNIT_FOREVER_ABS;
252 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
254 if (rel.rel_value_us + now.abs_value_us < rel.rel_value_us)
256 GNUNET_break (0); /* overflow... */
257 return GNUNET_TIME_UNIT_FOREVER_ABS;
259 ret.abs_value_us = rel.rel_value_us + now.abs_value_us;
265 * Return the minimum of two relative time values.
267 * @param t1 first timestamp
268 * @param t2 other timestamp
269 * @return timestamp that is smaller
271 struct GNUNET_TIME_Relative
272 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
273 struct GNUNET_TIME_Relative t2)
275 return (t1.rel_value_us < t2.rel_value_us) ? t1 : t2;
280 * Return the maximum of two relative time values.
282 * @param t1 first timestamp
283 * @param t2 other timestamp
284 * @return timestamp that is larger
286 struct GNUNET_TIME_Relative
287 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
288 struct GNUNET_TIME_Relative t2)
290 return (t1.rel_value_us > t2.rel_value_us) ? t1 : t2;
295 * Return the minimum of two relative time values.
297 * @param t1 first timestamp
298 * @param t2 other timestamp
299 * @return timestamp that is smaller
301 struct GNUNET_TIME_Absolute
302 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
303 struct GNUNET_TIME_Absolute t2)
305 return (t1.abs_value_us < t2.abs_value_us) ? t1 : t2;
310 * Return the maximum of two relative time values.
312 * @param t1 first timestamp
313 * @param t2 other timestamp
314 * @return timestamp that is bigger
316 struct GNUNET_TIME_Absolute
317 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
318 struct GNUNET_TIME_Absolute t2)
320 return (t1.abs_value_us > t2.abs_value_us) ? t1 : t2;
325 * Given a timestamp in the future, how much time
326 * remains until then?
328 * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
330 struct GNUNET_TIME_Relative
331 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future)
333 struct GNUNET_TIME_Relative ret;
335 if (future.abs_value_us == UINT64_MAX)
336 return GNUNET_TIME_UNIT_FOREVER_REL;
337 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
339 if (now.abs_value_us > future.abs_value_us)
340 return GNUNET_TIME_UNIT_ZERO;
341 ret.rel_value_us = future.abs_value_us - now.abs_value_us;
347 * Compute the time difference between the given start and end times.
348 * Use this function instead of actual subtraction to ensure that
349 * "FOREVER" and overflows are handled correctly.
351 * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
353 struct GNUNET_TIME_Relative
354 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
355 struct GNUNET_TIME_Absolute end)
357 struct GNUNET_TIME_Relative ret;
359 if (end.abs_value_us == UINT64_MAX)
360 return GNUNET_TIME_UNIT_FOREVER_REL;
361 if (end.abs_value_us < start.abs_value_us)
362 return GNUNET_TIME_UNIT_ZERO;
363 ret.rel_value_us = end.abs_value_us - start.abs_value_us;
369 * Get the duration of an operation as the
370 * difference of the current time and the given start time "whence".
372 * @return 0 if whence > now, otherwise now-whence.
374 struct GNUNET_TIME_Relative
375 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence)
377 struct GNUNET_TIME_Absolute now;
378 struct GNUNET_TIME_Relative ret;
380 now = GNUNET_TIME_absolute_get ();
381 if (whence.abs_value_us > now.abs_value_us)
382 return GNUNET_TIME_UNIT_ZERO;
383 ret.rel_value_us = now.abs_value_us - whence.abs_value_us;
389 * Add a given relative duration to the
392 * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
394 struct GNUNET_TIME_Absolute
395 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
396 struct GNUNET_TIME_Relative duration)
398 struct GNUNET_TIME_Absolute ret;
400 if ((start.abs_value_us == UINT64_MAX) ||
401 (duration.rel_value_us == UINT64_MAX))
402 return GNUNET_TIME_UNIT_FOREVER_ABS;
403 if (start.abs_value_us + duration.rel_value_us < start.abs_value_us)
406 return GNUNET_TIME_UNIT_FOREVER_ABS;
408 ret.abs_value_us = start.abs_value_us + duration.rel_value_us;
414 * Subtract a given relative duration from the
417 * @param start some absolute time
418 * @param duration some relative time to subtract
419 * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
421 struct GNUNET_TIME_Absolute
422 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
423 struct GNUNET_TIME_Relative duration)
425 struct GNUNET_TIME_Absolute ret;
427 if (start.abs_value_us <= duration.rel_value_us)
428 return GNUNET_TIME_UNIT_ZERO_ABS;
429 if (start.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
430 return GNUNET_TIME_UNIT_FOREVER_ABS;
431 ret.abs_value_us = start.abs_value_us - duration.rel_value_us;
437 * Multiply relative time by a given factor.
439 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
441 struct GNUNET_TIME_Relative
442 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
443 unsigned long long factor)
445 struct GNUNET_TIME_Relative ret;
448 return GNUNET_TIME_UNIT_ZERO;
449 if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
450 return GNUNET_TIME_UNIT_FOREVER_REL;
451 ret.rel_value_us = rel.rel_value_us * factor;
452 if (ret.rel_value_us / factor != rel.rel_value_us)
455 return GNUNET_TIME_UNIT_FOREVER_REL;
462 * Multiply relative time by a given floating-point factor. The factor must be
465 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
467 struct GNUNET_TIME_Relative
468 relative_multiply_double (struct GNUNET_TIME_Relative rel, double factor)
470 struct GNUNET_TIME_Relative out;
473 GNUNET_assert (0 <= factor);
476 return GNUNET_TIME_UNIT_ZERO;
477 if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
478 return GNUNET_TIME_UNIT_FOREVER_REL;
480 m = ((double) rel.rel_value_us) * factor;
482 if (m >= (double) (GNUNET_TIME_UNIT_FOREVER_REL).rel_value_us)
485 return GNUNET_TIME_UNIT_FOREVER_REL;
488 out.rel_value_us = (uint64_t) m;
494 * Saturating multiply relative time by a given factor.
496 * @param rel some duration
497 * @param factor integer to multiply with
498 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
500 struct GNUNET_TIME_Relative
501 GNUNET_TIME_relative_saturating_multiply (struct GNUNET_TIME_Relative rel,
502 unsigned long long factor)
504 struct GNUNET_TIME_Relative ret;
507 return GNUNET_TIME_UNIT_ZERO;
508 if (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
509 return GNUNET_TIME_UNIT_FOREVER_REL;
510 ret.rel_value_us = rel.rel_value_us * factor;
511 if (ret.rel_value_us / factor != rel.rel_value_us)
513 return GNUNET_TIME_UNIT_FOREVER_REL;
520 * Divide relative time by a given factor.
522 * @param rel some duration
523 * @param factor integer to divide by
524 * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
526 struct GNUNET_TIME_Relative
527 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
528 unsigned long long factor)
530 struct GNUNET_TIME_Relative ret;
533 (rel.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us))
534 return GNUNET_TIME_UNIT_FOREVER_REL;
535 ret.rel_value_us = rel.rel_value_us / factor;
541 * Calculate the estimate time of arrival/completion
544 * @param start when did the operation start?
545 * @param finished how much has been done?
546 * @param total how much must be done overall (same unit as for "finished")
547 * @return remaining duration for the operation,
548 * assuming it continues at the same speed
550 struct GNUNET_TIME_Relative
551 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start,
555 struct GNUNET_TIME_Relative dur;
557 struct GNUNET_TIME_Relative ret;
559 GNUNET_break (finished <= total);
560 if (finished >= total)
561 return GNUNET_TIME_UNIT_ZERO;
563 return GNUNET_TIME_UNIT_FOREVER_REL;
564 dur = GNUNET_TIME_absolute_get_duration (start);
565 exp = ((double) dur.rel_value_us) * ((double) total) / ((double) finished);
566 ret.rel_value_us = ((uint64_t) exp) - dur.rel_value_us;
572 * Add relative times together.
574 * @param a1 first timestamp
575 * @param a2 second timestamp
576 * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
578 struct GNUNET_TIME_Relative
579 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
580 struct GNUNET_TIME_Relative a2)
582 struct GNUNET_TIME_Relative ret;
584 if ((a1.rel_value_us == UINT64_MAX) || (a2.rel_value_us == UINT64_MAX))
585 return GNUNET_TIME_UNIT_FOREVER_REL;
586 if (a1.rel_value_us + a2.rel_value_us < a1.rel_value_us)
589 return GNUNET_TIME_UNIT_FOREVER_REL;
591 ret.rel_value_us = a1.rel_value_us + a2.rel_value_us;
597 * Subtract relative timestamp from the other.
599 * @param a1 first timestamp
600 * @param a2 second timestamp
601 * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
603 struct GNUNET_TIME_Relative
604 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
605 struct GNUNET_TIME_Relative a2)
607 struct GNUNET_TIME_Relative ret;
609 if (a2.rel_value_us >= a1.rel_value_us)
610 return GNUNET_TIME_UNIT_ZERO;
611 if (a1.rel_value_us == UINT64_MAX)
612 return GNUNET_TIME_UNIT_FOREVER_REL;
613 ret.rel_value_us = a1.rel_value_us - a2.rel_value_us;
619 * Convert relative time to network byte order.
621 * @param a time to convert
622 * @return time in network byte order
624 struct GNUNET_TIME_RelativeNBO
625 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a)
627 struct GNUNET_TIME_RelativeNBO ret;
629 ret.rel_value_us__ = GNUNET_htonll (a.rel_value_us);
635 * Convert relative time from network byte order.
637 * @param a time to convert
638 * @return time in host byte order
640 struct GNUNET_TIME_Relative
641 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a)
643 struct GNUNET_TIME_Relative ret;
645 ret.rel_value_us = GNUNET_ntohll (a.rel_value_us__);
651 * Convert absolute time to network byte order.
653 * @param a time to convert
654 * @return time in network byte order
656 struct GNUNET_TIME_AbsoluteNBO
657 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a)
659 struct GNUNET_TIME_AbsoluteNBO ret;
661 ret.abs_value_us__ = GNUNET_htonll (a.abs_value_us);
667 * Convert absolute time from network byte order.
669 * @param a time to convert
670 * @return time in host byte order
672 struct GNUNET_TIME_Absolute
673 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
675 struct GNUNET_TIME_Absolute ret;
677 ret.abs_value_us = GNUNET_ntohll (a.abs_value_us__);
683 * Return the current year (i.e. '2011').
686 GNUNET_TIME_get_current_year ()
695 return t->tm_year + 1900;
700 * Convert an expiration time to the respective year (rounds)
702 * @param at absolute time
703 * @return year a year (after 1970), 0 on error
706 GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at)
711 tp = at.abs_value_us / 1000LL / 1000LL; /* microseconds to seconds */
715 return t->tm_year + 1900;
720 * Convert a year to an expiration time of January 1st of that year.
722 * @param year a year (after 1970, please ;-)).
723 * @return absolute time for January 1st of that year.
725 struct GNUNET_TIME_Absolute
726 GNUNET_TIME_year_to_time (unsigned int year)
728 struct GNUNET_TIME_Absolute ret;
732 memset (&t, 0, sizeof(t));
736 return GNUNET_TIME_absolute_get (); /* now */
738 t.tm_year = year - 1900;
744 GNUNET_break (tp != (time_t) -1);
745 ret.abs_value_us = tp * 1000LL * 1000LL; /* seconds to microseconds */
751 * Randomized exponential back-off, starting at 1 ms
752 * and going up by a factor of 2+r, where 0 <= r <= 0.5, up
753 * to a maximum of the given threshold.
755 * @param r current backoff time, initially zero
756 * @param threshold maximum value for backoff
757 * @return the next backoff time
759 struct GNUNET_TIME_Relative
760 GNUNET_TIME_randomized_backoff (struct GNUNET_TIME_Relative rt,
761 struct GNUNET_TIME_Relative threshold)
763 double r = (rand () % 500) / 1000.0;
764 struct GNUNET_TIME_Relative t;
766 t = relative_multiply_double (
767 GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, rt),
769 return GNUNET_TIME_relative_min (threshold, t);
774 * Return a random time value between 0.5*r and 1.5*r.
776 * @param r input time for scaling
777 * @return randomized time
779 struct GNUNET_TIME_Relative
780 GNUNET_TIME_randomize (struct GNUNET_TIME_Relative r)
782 double d = ((rand () % 1001) - 500) / 1000.0;
784 return relative_multiply_double (r, d);
789 * Obtain the current time and make sure it is monotonically
790 * increasing. Guards against systems without an RTC or
791 * clocks running backwards and other nasty surprises. Does
792 * not guarantee that the returned time is near the current
793 * time returned by #GNUNET_TIME_absolute_get(). Two
794 * subsequent calls (within a short time period) may return the
795 * same value. Persists the last returned time on disk to
796 * ensure that time never goes backwards. As a result, the
797 * resulting value can be used to check if a message is the
798 * "most recent" value and replays of older messages (from
799 * the same origin) would be discarded.
801 * @param cfg configuration, used to determine where to
802 * store the time; user can also insist RTC is working
803 * nicely and disable the feature
804 * @return monotonically increasing time
806 struct GNUNET_TIME_Absolute
807 GNUNET_TIME_absolute_get_monotonic (
808 const struct GNUNET_CONFIGURATION_Handle *cfg)
810 static const struct GNUNET_CONFIGURATION_Handle *last_cfg;
811 static struct GNUNET_TIME_Absolute last_time;
812 static struct GNUNET_DISK_MapHandle *map_handle;
813 static ATOMIC volatile uint64_t *map;
814 struct GNUNET_TIME_Absolute now;
816 now = GNUNET_TIME_absolute_get ();
821 if (NULL != map_handle)
823 GNUNET_DISK_file_unmap (map_handle);
831 GNUNET_CONFIGURATION_get_value_filename (cfg,
833 "MONOTONIC_TIME_FILENAME",
836 struct GNUNET_DISK_FileHandle *fh;
838 fh = GNUNET_DISK_file_open (filename,
839 GNUNET_DISK_OPEN_READWRITE
840 | GNUNET_DISK_OPEN_CREATE,
841 GNUNET_DISK_PERM_USER_WRITE
842 | GNUNET_DISK_PERM_GROUP_WRITE
843 | GNUNET_DISK_PERM_USER_READ
844 | GNUNET_DISK_PERM_GROUP_READ);
847 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
848 _ ("Failed to map `%s', cannot assure monotonic time!\n"),
856 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_handle_size (fh, &size));
857 if (size < (off_t) sizeof(*map))
859 struct GNUNET_TIME_AbsoluteNBO o;
861 o = GNUNET_TIME_absolute_hton (now);
862 if (sizeof(o) != GNUNET_DISK_file_write (fh, &o, sizeof(o)))
867 if (size == sizeof(*map))
869 map = GNUNET_DISK_file_map (fh,
871 GNUNET_DISK_MAP_TYPE_READWRITE,
874 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
876 "Failed to map `%s', cannot assure monotonic time!\n"),
882 GNUNET_ERROR_TYPE_WARNING,
884 "Failed to setup monotonic time file `%s', cannot assure monotonic time!\n"),
888 GNUNET_DISK_file_close (fh);
889 GNUNET_free (filename);
894 struct GNUNET_TIME_AbsoluteNBO mt;
896 #if __STDC_NO_ATOMICS__
898 mt.abs_value_us__ = __sync_fetch_and_or (map, 0);
900 mt.abs_value_us__ = *map; /* godspeed, pray this is atomic */
903 mt.abs_value_us__ = atomic_load (map);
906 GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_ntoh (mt), last_time);
908 if (now.abs_value_us <= last_time.abs_value_us)
909 now.abs_value_us = last_time.abs_value_us + 1;
913 uint64_t val = GNUNET_TIME_absolute_hton (now).abs_value_us__;
914 #if __STDC_NO_ATOMICS__
916 (void) __sync_lock_test_and_set (map, val);
918 *map = val; /* godspeed, pray this is atomic */
921 atomic_store (map, val);
931 void __attribute__ ((destructor))
932 GNUNET_util_time_fini ()
934 (void) GNUNET_TIME_absolute_get_monotonic (NULL);