changing time measurement from milliseconds to microseconds
[oweals/gnunet.git] / src / include / gnunet_time_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2001-2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_time_lib.h
23  * @brief functions related to time
24  *
25  * @author Christian Grothoff
26  */
27
28 #ifndef GNUNET_TIME_LIB_H
29 #define GNUNET_TIME_LIB_H
30
31 #ifdef __cplusplus
32 extern "C"
33 {
34 #if 0                           /* keep Emacsens' auto-indent happy */
35 }
36 #endif
37 #endif
38
39 #include "gnunet_common.h"
40
41 /**
42  * Time for absolute times used by GNUnet, in microseconds.
43  */
44 struct GNUNET_TIME_Absolute
45 {
46   /**
47    * The actual value.
48    */
49   uint64_t abs_value_us;
50 };
51
52 /**
53  * Time for relative time used by GNUnet, in microseconds.
54  * Always positive, so we can only refer to future time.
55  */
56 struct GNUNET_TIME_Relative
57 {
58   /**
59    * The actual value.
60    */
61   uint64_t rel_value_us;
62 };
63
64 GNUNET_NETWORK_STRUCT_BEGIN
65
66 /**
67  * Time for relative time used by GNUnet, in microseconds and in network byte order.
68  */
69 struct GNUNET_TIME_RelativeNBO
70 {
71   /**
72    * The actual value (in network byte order).
73    */
74   uint64_t rel_value_us__ GNUNET_PACKED;
75 };
76
77
78 /**
79  * Time for absolute time used by GNUnet, in microseconds and in network byte order.
80  */
81 struct GNUNET_TIME_AbsoluteNBO
82 {
83   /**
84    * The actual value (in network byte order).
85    */
86   uint64_t abs_value_us__ GNUNET_PACKED;
87 };
88 GNUNET_NETWORK_STRUCT_END
89
90 /**
91  * Relative time zero.
92  */
93 #define GNUNET_TIME_UNIT_ZERO     GNUNET_TIME_relative_get_zero_()
94
95 /**
96  * Absolute time zero.
97  */
98 #define GNUNET_TIME_UNIT_ZERO_ABS GNUNET_TIME_absolute_get_zero_()
99
100 /**
101  * One microsecond, our basic time unit.
102  */
103 #define GNUNET_TIME_UNIT_MICROSECONDS GNUNET_TIME_relative_get_unit_()
104
105 /**
106  * One millisecond.
107  */
108 #define GNUNET_TIME_UNIT_MILLISECONDS GNUNET_TIME_relative_get_millisecond_()
109
110 /**
111  * One second.
112  */
113 #define GNUNET_TIME_UNIT_SECONDS GNUNET_TIME_relative_get_second_()
114
115 /**
116  * One minute.
117  */
118 #define GNUNET_TIME_UNIT_MINUTES GNUNET_TIME_relative_get_minute_()
119
120 /**
121  * One hour.
122  */
123 #define GNUNET_TIME_UNIT_HOURS   GNUNET_TIME_relative_get_hour_()
124
125 /**
126  * One day.
127  */
128 #define GNUNET_TIME_UNIT_DAYS    GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS,   24)
129
130 /**
131  * One week.
132  */
133 #define GNUNET_TIME_UNIT_WEEKS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,     7)
134
135 /**
136  * One month (30 days).
137  */
138 #define GNUNET_TIME_UNIT_MONTHS  GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,    30)
139
140 /**
141  * One year (365 days).
142  */
143 #define GNUNET_TIME_UNIT_YEARS   GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_DAYS,   365)
144
145 /**
146  * Constant used to specify "forever".  This constant
147  * will be treated specially in all time operations.
148  */
149 #define GNUNET_TIME_UNIT_FOREVER_REL GNUNET_TIME_relative_get_forever_ ()
150
151 /**
152  * Constant used to specify "forever".  This constant
153  * will be treated specially in all time operations.
154  */
155 #define GNUNET_TIME_UNIT_FOREVER_ABS GNUNET_TIME_absolute_get_forever_ ()
156
157
158
159 /**
160  * Threshold after which exponential backoff should not increase (15 m).
161  */
162 #define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
163
164
165 /**
166  * Perform our standard exponential back-off calculation, starting at 1 ms
167  * and then going by a factor of 2 up unto a maximum of 15 m.
168  *
169  * @param r current backoff time, initially zero
170  */
171 #define GNUNET_TIME_STD_BACKOFF(r) GNUNET_TIME_relative_min (GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD, \
172    GNUNET_TIME_relative_multiply (GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS, (r)), 2));
173
174 /**
175  * Return relative time of 0ms.
176  */
177 struct GNUNET_TIME_Relative
178 GNUNET_TIME_relative_get_zero_ (void);
179
180
181 /**
182  * Return absolute time of 0ms.
183  */
184 struct GNUNET_TIME_Absolute
185 GNUNET_TIME_absolute_get_zero_ (void);
186
187
188 /**
189  * Return relative time of 1 microsecond.
190  */
191 struct GNUNET_TIME_Relative
192 GNUNET_TIME_relative_get_unit_ (void);
193
194
195 /**
196  * Return relative time of 1ms.
197  */
198 struct GNUNET_TIME_Relative
199 GNUNET_TIME_relative_get_millisecond_ (void);
200
201
202 /**
203  * Return relative time of 1s.
204  */
205 struct GNUNET_TIME_Relative
206 GNUNET_TIME_relative_get_second_ (void);
207
208
209 /**
210  * Return relative time of 1 minute.
211  */
212 struct GNUNET_TIME_Relative
213 GNUNET_TIME_relative_get_minute_ (void);
214
215
216 /**
217  * Return relative time of 1 hour.
218  */
219 struct GNUNET_TIME_Relative
220 GNUNET_TIME_relative_get_hour_ (void);
221
222
223 /**
224  * Return "forever".
225  */
226 struct GNUNET_TIME_Relative
227 GNUNET_TIME_relative_get_forever_ (void);
228
229
230 /**
231  * Return "forever".
232  */
233 struct GNUNET_TIME_Absolute
234 GNUNET_TIME_absolute_get_forever_ (void);
235
236
237 /**
238  * Get the current time.
239  *
240  * @return the current time
241  */
242 struct GNUNET_TIME_Absolute
243 GNUNET_TIME_absolute_get (void);
244
245
246 /**
247  * Convert relative time to an absolute time in the
248  * future.
249  *
250  * @param rel relative time to convert
251  * @return timestamp that is "rel" in the future, or FOREVER if rel==FOREVER (or if we would overflow)
252  */
253 struct GNUNET_TIME_Absolute
254 GNUNET_TIME_relative_to_absolute (struct GNUNET_TIME_Relative rel);
255
256
257 /**
258  * Return the minimum of two relative time values.
259  *
260  * @param t1 first timestamp
261  * @param t2 other timestamp
262  * @return timestamp that is smaller
263  */
264 struct GNUNET_TIME_Relative
265 GNUNET_TIME_relative_min (struct GNUNET_TIME_Relative t1,
266                           struct GNUNET_TIME_Relative t2);
267
268
269
270 /**
271  * Return the maximum of two relative time values.
272  *
273  * @param t1 first timestamp
274  * @param t2 other timestamp
275  * @return timestamp that is larger
276  */
277 struct GNUNET_TIME_Relative
278 GNUNET_TIME_relative_max (struct GNUNET_TIME_Relative t1,
279                           struct GNUNET_TIME_Relative t2);
280
281
282 /**
283  * Return the minimum of two absolute time values.
284  *
285  * @param t1 first timestamp
286  * @param t2 other timestamp
287  * @return timestamp that is smaller
288  */
289 struct GNUNET_TIME_Absolute
290 GNUNET_TIME_absolute_min (struct GNUNET_TIME_Absolute t1,
291                           struct GNUNET_TIME_Absolute t2);
292
293
294 /**
295  * Return the maximum of two absolute time values.
296  *
297  * @param t1 first timestamp
298  * @param t2 other timestamp
299  * @return timestamp that is smaller
300  */
301 struct GNUNET_TIME_Absolute
302 GNUNET_TIME_absolute_max (struct GNUNET_TIME_Absolute t1,
303                           struct GNUNET_TIME_Absolute t2);
304
305
306 /**
307  * Given a timestamp in the future, how much time
308  * remains until then?
309  *
310  * @param future some absolute time, typically in the future
311  * @return future - now, or 0 if now >= future, or FOREVER if future==FOREVER.
312  */
313 struct GNUNET_TIME_Relative
314 GNUNET_TIME_absolute_get_remaining (struct GNUNET_TIME_Absolute future);
315
316
317 /**
318  * Calculate the estimate time of arrival/completion
319  * for an operation.
320  *
321  * @param start when did the operation start?
322  * @param finished how much has been done?
323  * @param total how much must be done overall (same unit as for "finished")
324  * @return remaining duration for the operation,
325  *        assuming it continues at the same speed
326  */
327 struct GNUNET_TIME_Relative
328 GNUNET_TIME_calculate_eta (struct GNUNET_TIME_Absolute start, uint64_t finished,
329                            uint64_t total);
330
331
332 /**
333  * Compute the time difference between the given start and end times.
334  * Use this function instead of actual subtraction to ensure that
335  * "FOREVER" and overflows are handeled correctly.
336  *
337  * @param start some absolute time
338  * @param end some absolute time (typically larger or equal to start)
339  * @return 0 if start >= end; FOREVER if end==FOREVER; otherwise end - start
340  */
341 struct GNUNET_TIME_Relative
342 GNUNET_TIME_absolute_get_difference (struct GNUNET_TIME_Absolute start,
343                                      struct GNUNET_TIME_Absolute end);
344
345
346 /**
347  * Get the duration of an operation as the
348  * difference of the current time and the given start time "hence".
349  *
350  * @param whence some absolute time, typically in the past
351  * @return aborts if hence==FOREVER, 0 if hence > now, otherwise now-hence.
352  */
353 struct GNUNET_TIME_Relative
354 GNUNET_TIME_absolute_get_duration (struct GNUNET_TIME_Absolute whence);
355
356
357 /**
358  * Add a given relative duration to the
359  * given start time.
360  *
361  * @param start some absolute time
362  * @param duration some relative time to add
363  * @return FOREVER if either argument is FOREVER or on overflow; start+duration otherwise
364  */
365 struct GNUNET_TIME_Absolute
366 GNUNET_TIME_absolute_add (struct GNUNET_TIME_Absolute start,
367                           struct GNUNET_TIME_Relative duration);
368
369
370 /**
371  * Subtract a given relative duration from the
372  * given start time.
373  *
374  * @param start some absolute time
375  * @param duration some relative time to subtract
376  * @return ZERO if start <= duration, or FOREVER if start time is FOREVER; start-duration otherwise
377  */
378 struct GNUNET_TIME_Absolute
379 GNUNET_TIME_absolute_subtract (struct GNUNET_TIME_Absolute start,
380                                struct GNUNET_TIME_Relative duration);
381
382
383 /**
384  * Multiply relative time by a given factor.
385  *
386  * @param rel some duration
387  * @param factor integer to multiply with
388  * @return FOREVER if rel=FOREVER or on overflow; otherwise rel*factor
389  */
390 struct GNUNET_TIME_Relative
391 GNUNET_TIME_relative_multiply (struct GNUNET_TIME_Relative rel,
392                                unsigned int factor);
393
394
395 /**
396  * Divide relative time by a given factor.
397  *
398  * @param rel some duration
399  * @param factor integer to divide by
400  * @return FOREVER if rel=FOREVER or factor==0; otherwise rel/factor
401  */
402 struct GNUNET_TIME_Relative
403 GNUNET_TIME_relative_divide (struct GNUNET_TIME_Relative rel,
404                              unsigned int factor);
405
406
407 /**
408  * Add relative times together.
409  *
410  * @param a1 some relative time
411  * @param a2 some other relative time
412  * @return FOREVER if either argument is FOREVER or on overflow; a1+a2 otherwise
413  */
414 struct GNUNET_TIME_Relative
415 GNUNET_TIME_relative_add (struct GNUNET_TIME_Relative a1,
416                           struct GNUNET_TIME_Relative a2);
417
418
419 /**
420  * Subtract relative timestamp from the other.
421  *
422  * @param a1 first timestamp
423  * @param a2 second timestamp
424  * @return ZERO if a2>=a1 (including both FOREVER), FOREVER if a1 is FOREVER, a1-a2 otherwise
425  */
426 struct GNUNET_TIME_Relative
427 GNUNET_TIME_relative_subtract (struct GNUNET_TIME_Relative a1,
428                                struct GNUNET_TIME_Relative a2);
429
430
431 /**
432  * Convert relative time to network byte order.
433  *
434  * @param a time to convert
435  * @return converted time value
436  */
437 struct GNUNET_TIME_RelativeNBO
438 GNUNET_TIME_relative_hton (struct GNUNET_TIME_Relative a);
439
440
441 /**
442  * Convert relative time from network byte order.
443  *
444  * @param a time to convert
445  * @return converted time value
446  */
447 struct GNUNET_TIME_Relative
448 GNUNET_TIME_relative_ntoh (struct GNUNET_TIME_RelativeNBO a);
449
450
451 /**
452  * Convert relative time to network byte order.
453  *
454  * @param a time to convert
455  * @return converted time value
456  */
457 struct GNUNET_TIME_AbsoluteNBO
458 GNUNET_TIME_absolute_hton (struct GNUNET_TIME_Absolute a);
459
460
461 /**
462  * Convert relative time from network byte order.
463  *
464  * @param a time to convert
465  * @return converted time value
466  */
467 struct GNUNET_TIME_Absolute
468 GNUNET_TIME_absolute_ntoh (struct GNUNET_TIME_AbsoluteNBO a);
469
470
471 /**
472  * Set the timestamp offset for this instance.
473  *
474  * @param offset the offset to skew the locale time by
475  */
476 void
477 GNUNET_TIME_set_offset (long long offset);
478
479
480 /**
481  * Get the timestamp offset for this instance.
482  *
483  * @return the offset we currently skew the locale time by
484  */
485 long long 
486 GNUNET_TIME_get_offset (void);
487
488
489 #if 0                           /* keep Emacsens' auto-indent happy */
490 {
491 #endif
492 #ifdef __cplusplus
493 }
494 #endif
495
496 /* ifndef GNUNET_TIME_LIB_H */
497 #endif
498 /* end of gnunet_time_lib.h */