aboutsummaryrefslogtreecommitdiff
path: root/src/util/time.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/time.c')
-rw-r--r--src/util/time.c125
1 files changed, 61 insertions, 64 deletions
diff --git a/src/util/time.c b/src/util/time.c
index 758921718..85a781aff 100644
--- a/src/util/time.c
+++ b/src/util/time.c
@@ -37,7 +37,7 @@
37#endif 37#endif
38#endif 38#endif
39 39
40#define LOG(kind,...) GNUNET_log_from (kind, "util-time", __VA_ARGS__) 40#define LOG(kind, ...) GNUNET_log_from (kind, "util-time", __VA_ARGS__)
41 41
42/** 42/**
43 * Variable used to simulate clock skew. Used for testing, never in production. 43 * Variable used to simulate clock skew. Used for testing, never in production.
@@ -120,10 +120,10 @@ GNUNET_TIME_absolute_get ()
120 struct GNUNET_TIME_Absolute ret; 120 struct GNUNET_TIME_Absolute ret;
121 struct timeval tv; 121 struct timeval tv;
122 122
123 GETTIMEOFDAY (&tv, NULL); 123 gettimeofday (&tv, NULL);
124 ret.abs_value_us = 124 ret.abs_value_us = (uint64_t) (((uint64_t) tv.tv_sec * 1000LL * 1000LL) +
125 (uint64_t) (((uint64_t) tv.tv_sec * 1000LL * 1000LL) + 125 ((uint64_t) tv.tv_usec)) +
126 ((uint64_t) tv.tv_usec)) + timestamp_offset; 126 timestamp_offset;
127 return ret; 127 return ret;
128} 128}
129 129
@@ -158,7 +158,7 @@ GNUNET_TIME_absolute_get_zero_ ()
158struct GNUNET_TIME_Relative 158struct GNUNET_TIME_Relative
159GNUNET_TIME_relative_get_unit_ () 159GNUNET_TIME_relative_get_unit_ ()
160{ 160{
161 static struct GNUNET_TIME_Relative one = { 1 }; 161 static struct GNUNET_TIME_Relative one = {1};
162 162
163 return one; 163 return one;
164} 164}
@@ -170,7 +170,7 @@ GNUNET_TIME_relative_get_unit_ ()
170struct GNUNET_TIME_Relative 170struct GNUNET_TIME_Relative
171GNUNET_TIME_relative_get_millisecond_ () 171GNUNET_TIME_relative_get_millisecond_ ()
172{ 172{
173 static struct GNUNET_TIME_Relative one = { 1000 }; 173 static struct GNUNET_TIME_Relative one = {1000};
174 174
175 return one; 175 return one;
176} 176}
@@ -182,7 +182,7 @@ GNUNET_TIME_relative_get_millisecond_ ()
182struct GNUNET_TIME_Relative 182struct GNUNET_TIME_Relative
183GNUNET_TIME_relative_get_second_ () 183GNUNET_TIME_relative_get_second_ ()
184{ 184{
185 static struct GNUNET_TIME_Relative one = { 1000 * 1000LL }; 185 static struct GNUNET_TIME_Relative one = {1000 * 1000LL};
186 186
187 return one; 187 return one;
188} 188}
@@ -194,7 +194,7 @@ GNUNET_TIME_relative_get_second_ ()
194struct GNUNET_TIME_Relative 194struct GNUNET_TIME_Relative
195GNUNET_TIME_relative_get_minute_ () 195GNUNET_TIME_relative_get_minute_ ()
196{ 196{
197 static struct GNUNET_TIME_Relative one = { 60 * 1000 * 1000LL }; 197 static struct GNUNET_TIME_Relative one = {60 * 1000 * 1000LL};
198 198
199 return one; 199 return one;
200} 200}
@@ -206,7 +206,7 @@ GNUNET_TIME_relative_get_minute_ ()
206struct GNUNET_TIME_Relative 206struct GNUNET_TIME_Relative
207GNUNET_TIME_relative_get_hour_ () 207GNUNET_TIME_relative_get_hour_ ()
208{ 208{
209 static struct GNUNET_TIME_Relative one = { 60 * 60 * 1000 * 1000LL }; 209 static struct GNUNET_TIME_Relative one = {60 * 60 * 1000 * 1000LL};
210 210
211 return one; 211 return one;
212} 212}
@@ -218,7 +218,7 @@ GNUNET_TIME_relative_get_hour_ ()
218struct GNUNET_TIME_Relative 218struct GNUNET_TIME_Relative
219GNUNET_TIME_relative_get_forever_ () 219GNUNET_TIME_relative_get_forever_ ()
220{ 220{
221 static struct GNUNET_TIME_Relative forever = { UINT64_MAX }; 221 static struct GNUNET_TIME_Relative forever = {UINT64_MAX};
222 222
223 return forever; 223 return forever;
224} 224}
@@ -230,7 +230,7 @@ GNUNET_TIME_relative_get_forever_ ()
230struct GNUNET_TIME_Absolute 230struct GNUNET_TIME_Absolute
231GNUNET_TIME_absolute_get_forever_ () 231GNUNET_TIME_absolute_get_forever_ ()
232{ 232{
233 static struct GNUNET_TIME_Absolute forever = { UINT64_MAX }; 233 static struct GNUNET_TIME_Absolute forever = {UINT64_MAX};
234 return forever; 234 return forever;
235} 235}
236 236
@@ -252,7 +252,7 @@ GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel)
252 252
253 if (rel.rel_value_us + now.abs_value_us < rel.rel_value_us) 253 if (rel.rel_value_us + now.abs_value_us < rel.rel_value_us)
254 { 254 {
255 GNUNET_break (0); /* overflow... */ 255 GNUNET_break (0); /* overflow... */
256 return GNUNET_TIME_UNIT_FOREVER_ABS; 256 return GNUNET_TIME_UNIT_FOREVER_ABS;
257 } 257 }
258 ret.abs_value_us = rel.rel_value_us + now.abs_value_us; 258 ret.abs_value_us = rel.rel_value_us + now.abs_value_us;
@@ -290,7 +290,6 @@ GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
290} 290}
291 291
292 292
293
294/** 293/**
295 * Return the minimum of two relative time values. 294 * Return the minimum of two relative time values.
296 * 295 *
@@ -395,7 +394,8 @@ GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
395{ 394{
396 struct GNUNET_TIME_Absolute ret; 395 struct GNUNET_TIME_Absolute ret;
397 396
398 if ((start.abs_value_us == UINT64_MAX) || (duration.rel_value_us == UINT64_MAX)) 397 if ((start.abs_value_us == UINT64_MAX) ||
398 (duration.rel_value_us == UINT64_MAX))
399 return GNUNET_TIME_UNIT_FOREVER_ABS; 399 return GNUNET_TIME_UNIT_FOREVER_ABS;
400 if (start.abs_value_us + duration.rel_value_us < start.abs_value_us) 400 if (start.abs_value_us + duration.rel_value_us < start.abs_value_us)
401 { 401 {
@@ -462,8 +462,7 @@ GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
462 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor 462 * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
463 */ 463 */
464struct GNUNET_TIME_Relative 464struct GNUNET_TIME_Relative
465relative_multiply_double (struct GNUNET_TIME_Relative rel, 465relative_multiply_double (struct GNUNET_TIME_Relative rel, double factor)
466 double factor)
467{ 466{
468 struct GNUNET_TIME_Relative out; 467 struct GNUNET_TIME_Relative out;
469 double m; 468 double m;
@@ -546,7 +545,8 @@ GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
546 * assuming it continues at the same speed 545 * assuming it continues at the same speed
547 */ 546 */
548struct GNUNET_TIME_Relative 547struct GNUNET_TIME_Relative
549GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished, 548GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start,
549 uint64_t finished,
550 uint64_t total) 550 uint64_t total)
551{ 551{
552 struct GNUNET_TIME_Relative dur; 552 struct GNUNET_TIME_Relative dur;
@@ -673,7 +673,6 @@ GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a)
673 673
674 ret.abs_value_us = GNUNET_ntohll (a.abs_value_us__); 674 ret.abs_value_us = GNUNET_ntohll (a.abs_value_us__);
675 return ret; 675 return ret;
676
677} 676}
678 677
679 678
@@ -706,12 +705,11 @@ GNUNET_TIME_time_to_year (struct GNUNET_TIME_Absolute at)
706 struct tm *t; 705 struct tm *t;
707 time_t tp; 706 time_t tp;
708 707
709 tp = at.abs_value_us / 1000LL / 1000LL; /* microseconds to seconds */ 708 tp = at.abs_value_us / 1000LL / 1000LL; /* microseconds to seconds */
710 t = gmtime (&tp); 709 t = gmtime (&tp);
711 if (t == NULL) 710 if (t == NULL)
712 return 0; 711 return 0;
713 return t->tm_year + 1900; 712 return t->tm_year + 1900;
714
715} 713}
716 714
717 715
@@ -740,8 +738,8 @@ GNUNET_TIME_year_to_time (unsigned int year)
740 t.tm_wday = 1; 738 t.tm_wday = 1;
741 t.tm_yday = 1; 739 t.tm_yday = 1;
742 tp = mktime (&t); 740 tp = mktime (&t);
743 GNUNET_break (tp != (time_t) - 1); 741 GNUNET_break (tp != (time_t) -1);
744 ret.abs_value_us = tp * 1000LL * 1000LL; /* seconds to microseconds */ 742 ret.abs_value_us = tp * 1000LL * 1000LL; /* seconds to microseconds */
745 return ret; 743 return ret;
746} 744}
747 745
@@ -756,16 +754,16 @@ GNUNET_TIME_year_to_time (unsigned int year)
756 * @return the next backoff time 754 * @return the next backoff time
757 */ 755 */
758struct GNUNET_TIME_Relative 756struct GNUNET_TIME_Relative
759GNUNET_TIME_randomized_backoff(struct GNUNET_TIME_Relative rt, struct GNUNET_TIME_Relative threshold) 757GNUNET_TIME_randomized_backoff (struct GNUNET_TIME_Relative rt,
758 struct GNUNET_TIME_Relative threshold)
760{ 759{
761 double r = (rand() % 500) / 1000.0; 760 double r = (rand () % 500) / 1000.0;
762 struct GNUNET_TIME_Relative t; 761 struct GNUNET_TIME_Relative t;
763 762
764 t = relative_multiply_double (GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, 763 t = relative_multiply_double (
765 rt), 764 GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, rt),
766 2 + r); 765 2 + r);
767 return GNUNET_TIME_relative_min (threshold, 766 return GNUNET_TIME_relative_min (threshold, t);
768 t);
769} 767}
770 768
771 769
@@ -778,10 +776,9 @@ GNUNET_TIME_randomized_backoff(struct GNUNET_TIME_Relative rt, struct GNUNET_TIM
778struct GNUNET_TIME_Relative 776struct GNUNET_TIME_Relative
779GNUNET_TIME_randomize (struct GNUNET_TIME_Relative r) 777GNUNET_TIME_randomize (struct GNUNET_TIME_Relative r)
780{ 778{
781 double d = ((rand() % 1001) - 500) / 1000.0; 779 double d = ((rand () % 1001) - 500) / 1000.0;
782 780
783 return relative_multiply_double (r, 781 return relative_multiply_double (r, d);
784 d);
785} 782}
786 783
787 784
@@ -804,7 +801,8 @@ GNUNET_TIME_randomize (struct GNUNET_TIME_Relative r)
804 * @return monotonically increasing time 801 * @return monotonically increasing time
805 */ 802 */
806struct GNUNET_TIME_Absolute 803struct GNUNET_TIME_Absolute
807GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cfg) 804GNUNET_TIME_absolute_get_monotonic (
805 const struct GNUNET_CONFIGURATION_Handle *cfg)
808{ 806{
809 static const struct GNUNET_CONFIGURATION_Handle *last_cfg; 807 static const struct GNUNET_CONFIGURATION_Handle *last_cfg;
810 static struct GNUNET_TIME_Absolute last_time; 808 static struct GNUNET_TIME_Absolute last_time;
@@ -825,23 +823,26 @@ GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cf
825 map = NULL; 823 map = NULL;
826 824
827 last_cfg = cfg; 825 last_cfg = cfg;
828 if ( (NULL != cfg) && 826 if ((NULL != cfg) &&
829 (GNUNET_OK == 827 (GNUNET_OK ==
830 GNUNET_CONFIGURATION_get_value_filename (cfg, 828 GNUNET_CONFIGURATION_get_value_filename (cfg,
831 "util", 829 "util",
832 "MONOTONIC_TIME_FILENAME", 830 "MONOTONIC_TIME_FILENAME",
833 &filename)) ) 831 &filename)))
834 { 832 {
835 struct GNUNET_DISK_FileHandle *fh; 833 struct GNUNET_DISK_FileHandle *fh;
836 834
837 fh = GNUNET_DISK_file_open (filename, 835 fh = GNUNET_DISK_file_open (filename,
838 GNUNET_DISK_OPEN_READWRITE | GNUNET_DISK_OPEN_CREATE, 836 GNUNET_DISK_OPEN_READWRITE |
839 GNUNET_DISK_PERM_USER_WRITE | GNUNET_DISK_PERM_GROUP_WRITE | 837 GNUNET_DISK_OPEN_CREATE,
840 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_GROUP_READ); 838 GNUNET_DISK_PERM_USER_WRITE |
839 GNUNET_DISK_PERM_GROUP_WRITE |
840 GNUNET_DISK_PERM_USER_READ |
841 GNUNET_DISK_PERM_GROUP_READ);
841 if (NULL == fh) 842 if (NULL == fh)
842 { 843 {
843 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 844 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
844 _("Failed to map `%s', cannot assure monotonic time!\n"), 845 _ ("Failed to map `%s', cannot assure monotonic time!\n"),
845 filename); 846 filename);
846 } 847 }
847 else 848 else
@@ -849,18 +850,13 @@ GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cf
849 off_t size; 850 off_t size;
850 851
851 size = 0; 852 size = 0;
852 GNUNET_break (GNUNET_OK == 853 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_handle_size (fh, &size));
853 GNUNET_DISK_file_handle_size (fh,
854 &size));
855 if (size < (off_t) sizeof (*map)) 854 if (size < (off_t) sizeof (*map))
856 { 855 {
857 struct GNUNET_TIME_AbsoluteNBO o; 856 struct GNUNET_TIME_AbsoluteNBO o;
858 857
859 o = GNUNET_TIME_absolute_hton (now); 858 o = GNUNET_TIME_absolute_hton (now);
860 if (sizeof (o) != 859 if (sizeof (o) != GNUNET_DISK_file_write (fh, &o, sizeof (o)))
861 GNUNET_DISK_file_write (fh,
862 &o,
863 sizeof (o)))
864 size = 0; 860 size = 0;
865 else 861 else
866 size = sizeof (o); 862 size = sizeof (o);
@@ -873,14 +869,17 @@ GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cf
873 sizeof (*map)); 869 sizeof (*map));
874 if (NULL == map) 870 if (NULL == map)
875 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 871 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
876 _("Failed to map `%s', cannot assure monotonic time!\n"), 872 _ (
873 "Failed to map `%s', cannot assure monotonic time!\n"),
877 filename); 874 filename);
878 } 875 }
879 else 876 else
880 { 877 {
881 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 878 GNUNET_log (
882 _("Failed to setup monotonic time file `%s', cannot assure monotonic time!\n"), 879 GNUNET_ERROR_TYPE_WARNING,
883 filename); 880 _ (
881 "Failed to setup monotonic time file `%s', cannot assure monotonic time!\n"),
882 filename);
884 } 883 }
885 } 884 }
886 GNUNET_DISK_file_close (fh); 885 GNUNET_DISK_file_close (fh);
@@ -900,11 +899,11 @@ GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cf
900#else 899#else
901 mt.abs_value_us__ = atomic_load (map); 900 mt.abs_value_us__ = atomic_load (map);
902#endif 901#endif
903 last_time = GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_ntoh (mt), 902 last_time =
904 last_time); 903 GNUNET_TIME_absolute_max (GNUNET_TIME_absolute_ntoh (mt), last_time);
905 } 904 }
906 if (now.abs_value_us <= last_time.abs_value_us) 905 if (now.abs_value_us <= last_time.abs_value_us)
907 now.abs_value_us = last_time.abs_value_us+1; 906 now.abs_value_us = last_time.abs_value_us + 1;
908 last_time = now; 907 last_time = now;
909 if (NULL != map) 908 if (NULL != map)
910 { 909 {
@@ -913,11 +912,10 @@ GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cf
913#if __GNUC__ 912#if __GNUC__
914 (void) __sync_lock_test_and_set (map, val); 913 (void) __sync_lock_test_and_set (map, val);
915#else 914#else
916 *map = val; /* godspeed, pray this is atomic */ 915 *map = val; /* godspeed, pray this is atomic */
917#endif 916#endif
918#else 917#else
919 atomic_store (map, 918 atomic_store (map, val);
920 val);
921#endif 919#endif
922 } 920 }
923 return now; 921 return now;
@@ -927,8 +925,7 @@ GNUNET_TIME_absolute_get_monotonic (const struct GNUNET_CONFIGURATION_Handle *cf
927/** 925/**
928 * Destructor 926 * Destructor
929 */ 927 */
930void __attribute__ ((destructor)) 928void __attribute__ ((destructor)) GNUNET_util_time_fini ()
931GNUNET_util_time_fini ()
932{ 929{
933 (void) GNUNET_TIME_absolute_get_monotonic (NULL); 930 (void) GNUNET_TIME_absolute_get_monotonic (NULL);
934} 931}