2 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "event2/event-config.h"
28 #include "evconfig-private.h"
32 #define WIN32_LEAN_AND_MEAN
34 #undef WIN32_LEAN_AND_MEAN
37 #include <sys/types.h>
38 #ifdef EVENT__HAVE_STDLIB_H
43 #ifndef EVENT__HAVE_GETTIMEOFDAY
44 #include <sys/timeb.h>
46 #if !defined(EVENT__HAVE_NANOSLEEP) && !defined(EVENT__HAVE_USLEEP) && \
48 #include <sys/select.h>
54 /** evutil_usleep_() */
56 #elif defined(EVENT__HAVE_NANOSLEEP)
57 #elif defined(EVENT__HAVE_USLEEP)
61 #include "event2/util.h"
62 #include "util-internal.h"
63 #include "log-internal.h"
64 #include "mm-internal.h"
66 #ifndef EVENT__HAVE_GETTIMEOFDAY
67 /* No gettimeofday; this must be windows. */
69 typedef void (WINAPI *GetSystemTimePreciseAsFileTime_fn_t) (LPFILETIME);
72 evutil_gettimeofday(struct timeval *tv, struct timezone *tz)
74 static GetSystemTimePreciseAsFileTime_fn_t GetSystemTimePreciseAsFileTime_fn = NULL;
75 static int check_precise = 1;
78 #define U64_LITERAL(n) n##ui64
80 #define U64_LITERAL(n) n##llu
83 /* Conversion logic taken from Tor, which in turn took it
84 * from Perl. GetSystemTimeAsFileTime returns its value as
85 * an unaligned (!) 64-bit value containing the number of
86 * 100-nanosecond intervals since 1 January 1601 UTC. */
87 #define EPOCH_BIAS U64_LITERAL(116444736000000000)
88 #define UNITS_PER_SEC U64_LITERAL(10000000)
89 #define USEC_PER_SEC U64_LITERAL(1000000)
90 #define UNITS_PER_USEC U64_LITERAL(10)
99 if (EVUTIL_UNLIKELY(check_precise)) {
100 HMODULE h = evutil_load_windows_system_library_(TEXT("kernel32.dll"));
102 GetSystemTimePreciseAsFileTime_fn =
103 (GetSystemTimePreciseAsFileTime_fn_t)
104 GetProcAddress(h, "GetSystemTimePreciseAsFileTime");
108 if (GetSystemTimePreciseAsFileTime_fn != NULL)
109 GetSystemTimePreciseAsFileTime_fn(&ft.ft_ft);
111 GetSystemTimeAsFileTime(&ft.ft_ft);
113 if (EVUTIL_UNLIKELY(ft.ft_64 < EPOCH_BIAS)) {
114 /* Time before the unix epoch. */
117 ft.ft_64 -= EPOCH_BIAS;
118 tv->tv_sec = (long) (ft.ft_64 / UNITS_PER_SEC);
119 tv->tv_usec = (long) ((ft.ft_64 / UNITS_PER_USEC) % USEC_PER_SEC);
124 #define MAX_SECONDS_IN_MSEC_LONG \
125 (((LONG_MAX) - 999) / 1000)
128 evutil_tv_to_msec_(const struct timeval *tv)
130 if (tv->tv_usec > 1000000 || tv->tv_sec > MAX_SECONDS_IN_MSEC_LONG)
133 return (tv->tv_sec * 1000) + ((tv->tv_usec + 999) / 1000);
137 Replacement for usleep on platforms that don't have one. Not guaranteed to
138 be any more finegrained than 1 msec.
141 evutil_usleep_(const struct timeval *tv)
151 usec = tv->tv_sec * 1000000LL + tv->tv_usec;
155 li.QuadPart = -10LL * usec;
156 timer = CreateWaitableTimer(NULL, TRUE, NULL);
160 SetWaitableTimer(timer, &li, 0, NULL, NULL, 0);
161 WaitForSingleObject(timer, INFINITE);
164 #elif defined(EVENT__HAVE_NANOSLEEP)
167 ts.tv_sec = tv->tv_sec;
168 ts.tv_nsec = tv->tv_usec*1000;
169 nanosleep(&ts, NULL);
171 #elif defined(EVENT__HAVE_USLEEP)
172 /* Some systems don't like to usleep more than 999999 usec */
177 struct timeval tv2 = *tv;
178 select(0, NULL, NULL, NULL, &tv2);
184 evutil_date_rfc1123(char *date, const size_t datelen, const struct tm *tm)
186 static const char *DAYS[] =
187 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
188 static const char *MONTHS[] =
189 { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
191 time_t t = time(NULL);
193 #if defined(EVENT__HAVE__GMTIME64_S) || !defined(_WIN32)
197 /* If `tm` is null, set system's current time. */
202 /** detect _gmtime64()/_gmtime64_s() */
203 #elif defined(EVENT__HAVE__GMTIME64_S)
205 err = _gmtime64_s(&sys, &t);
207 event_errx(1, "Invalid argument to _gmtime64_s");
211 #elif defined(EVENT__HAVE__GMTIME64)
218 return evutil_snprintf(
219 date, datelen, "%s, %02d %s %4d %02d:%02d:%02d GMT",
220 DAYS[tm->tm_wday], tm->tm_mday, MONTHS[tm->tm_mon],
221 1900 + tm->tm_year, tm->tm_hour, tm->tm_min, tm->tm_sec);
225 This function assumes it's called repeatedly with a
226 not-actually-so-monotonic time source whose outputs are in 'tv'. It
227 implements a trivial ratcheting mechanism so that the values never go
231 adjust_monotonic_time(struct evutil_monotonic_timer *base,
234 evutil_timeradd(tv, &base->adjust_monotonic_clock, tv);
236 if (evutil_timercmp(tv, &base->last_time, <)) {
237 /* Guess it wasn't monotonic after all. */
238 struct timeval adjust;
239 evutil_timersub(&base->last_time, tv, &adjust);
240 evutil_timeradd(&adjust, &base->adjust_monotonic_clock,
241 &base->adjust_monotonic_clock);
242 *tv = base->last_time;
244 base->last_time = *tv;
248 Allocate a new struct evutil_monotonic_timer
250 struct evutil_monotonic_timer *
251 evutil_monotonic_timer_new(void)
253 struct evutil_monotonic_timer *p = NULL;
255 p = mm_malloc(sizeof(*p));
258 memset(p, 0, sizeof(*p));
265 Free a struct evutil_monotonic_timer
268 evutil_monotonic_timer_free(struct evutil_monotonic_timer *timer)
276 Set up a struct evutil_monotonic_timer for initial use
279 evutil_configure_monotonic_time(struct evutil_monotonic_timer *timer,
282 return evutil_configure_monotonic_time_(timer, flags);
286 Query the current monotonic time
289 evutil_gettime_monotonic(struct evutil_monotonic_timer *timer,
292 return evutil_gettime_monotonic_(timer, tp);
296 #if defined(HAVE_POSIX_MONOTONIC)
298 The POSIX clock_gettime() interface provides a few ways to get at a
299 monotonic clock. CLOCK_MONOTONIC is most widely supported. Linux also
300 provides a CLOCK_MONOTONIC_COARSE with accuracy of about 1-4 msec.
302 On all platforms I'm aware of, CLOCK_MONOTONIC really is monotonic.
303 Platforms don't agree about whether it should jump on a sleep/resume.
307 evutil_configure_monotonic_time_(struct evutil_monotonic_timer *base,
310 /* CLOCK_MONOTONIC exists on FreeBSD, Linux, and Solaris. You need to
311 * check for it at runtime, because some older kernel versions won't
312 * have it working. */
313 #ifdef CLOCK_MONOTONIC_COARSE
314 const int precise = flags & EV_MONOT_PRECISE;
316 const int fallback = flags & EV_MONOT_FALLBACK;
319 #ifdef CLOCK_MONOTONIC_COARSE
320 if (CLOCK_MONOTONIC_COARSE < 0) {
321 /* Technically speaking, nothing keeps CLOCK_* from being
322 * negative (as far as I know). This check and the one below
323 * make sure that it's safe for us to use -1 as an "unset"
325 event_errx(1,"I didn't expect CLOCK_MONOTONIC_COARSE to be < 0");
327 if (! precise && ! fallback) {
328 if (clock_gettime(CLOCK_MONOTONIC_COARSE, &ts) == 0) {
329 base->monotonic_clock = CLOCK_MONOTONIC_COARSE;
334 if (!fallback && clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
335 base->monotonic_clock = CLOCK_MONOTONIC;
339 if (CLOCK_MONOTONIC < 0) {
340 event_errx(1,"I didn't expect CLOCK_MONOTONIC to be < 0");
343 base->monotonic_clock = -1;
348 evutil_gettime_monotonic_(struct evutil_monotonic_timer *base,
353 if (base->monotonic_clock < 0) {
354 if (evutil_gettimeofday(tp, NULL) < 0)
356 adjust_monotonic_time(base, tp);
360 if (clock_gettime(base->monotonic_clock, &ts) == -1)
362 tp->tv_sec = ts.tv_sec;
363 tp->tv_usec = ts.tv_nsec / 1000;
369 #if defined(HAVE_MACH_MONOTONIC)
371 Apple is a little late to the POSIX party. And why not? Instead of
372 clock_gettime(), they provide mach_absolute_time(). Its units are not
373 fixed; we need to use mach_timebase_info() to get the right functions to
374 convert its units into nanoseconds.
376 To all appearances, mach_absolute_time() seems to be honest-to-goodness
377 monotonic. Whether it stops during sleep or not is unspecified in
378 principle, and dependent on CPU architecture in practice.
382 evutil_configure_monotonic_time_(struct evutil_monotonic_timer *base,
385 const int fallback = flags & EV_MONOT_FALLBACK;
386 struct mach_timebase_info mi;
387 memset(base, 0, sizeof(*base));
388 /* OSX has mach_absolute_time() */
390 mach_timebase_info(&mi) == 0 &&
391 mach_absolute_time() != 0) {
392 /* mach_timebase_info tells us how to convert
393 * mach_absolute_time() into nanoseconds, but we
394 * want to use microseconds instead. */
396 memcpy(&base->mach_timebase_units, &mi, sizeof(mi));
398 base->mach_timebase_units.numer = 0;
404 evutil_gettime_monotonic_(struct evutil_monotonic_timer *base,
407 ev_uint64_t abstime, usec;
408 if (base->mach_timebase_units.numer == 0) {
409 if (evutil_gettimeofday(tp, NULL) < 0)
411 adjust_monotonic_time(base, tp);
415 abstime = mach_absolute_time();
416 usec = (abstime * base->mach_timebase_units.numer)
417 / (base->mach_timebase_units.denom);
418 tp->tv_sec = usec / 1000000;
419 tp->tv_usec = usec % 1000000;
425 #if defined(HAVE_WIN32_MONOTONIC)
427 Turn we now to Windows. Want monontonic time on Windows?
429 Windows has QueryPerformanceCounter(), which gives time most high-
430 resolution time. It's a pity it's not so monotonic in practice; it's
431 also got some fun bugs, especially: with older Windowses, under
432 virtualizations, with funny hardware, on multiprocessor systems, and so
433 on. PEP418 [1] has a nice roundup of the issues here.
435 There's GetTickCount64() on Vista and later, which gives a number of 1-msec
436 ticks since startup. The accuracy here might be as bad as 10-20 msec, I
437 hear. There's an undocumented function (NtSetTimerResolution) that
438 allegedly increases the accuracy. Good luck!
440 There's also GetTickCount(), which is only 32 bits, but seems to be
441 supported on pre-Vista versions of Windows. Apparently, you can coax
442 another 14 bits out of it, giving you 2231 years before rollover.
444 The less said about timeGetTime() the better.
446 "We don't care. We don't have to. We're the Phone Company."
449 Our strategy, if precise timers are turned off, is to just use the best
450 GetTickCount equivalent available. If we've been asked for precise timing,
451 then we mostly[2] assume that GetTickCount is monotonic, and correct
452 GetPerformanceCounter to approximate it.
454 [1] http://www.python.org/dev/peps/pep-0418
455 [2] Of course, we feed the Windows stuff into adjust_monotonic_time()
456 anyway, just in case it isn't.
460 Parts of our logic in the win32 timer code here are closely based on
461 BitTorrent's libUTP library. That code is subject to the following
464 Copyright (c) 2010 BitTorrent, Inc.
466 Permission is hereby granted, free of charge, to any person obtaining a
467 copy of this software and associated documentation files (the
468 "Software"), to deal in the Software without restriction, including
469 without limitation the rights to use, copy, modify, merge, publish,
470 distribute, sublicense, and/or sell copies of the Software, and to
471 permit persons to whom the Software is furnished to do so, subject to
472 the following conditions:
474 The above copyright notice and this permission notice shall be included
475 in all copies or substantial portions of the Software.
477 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
478 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
479 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
480 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
481 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
482 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
483 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
487 evutil_GetTickCount_(struct evutil_monotonic_timer *base)
489 if (base->GetTickCount64_fn) {
490 /* Let's just use GetTickCount64 if we can. */
491 return base->GetTickCount64_fn();
492 } else if (base->GetTickCount_fn) {
493 /* Greg Hazel assures me that this works, that BitTorrent has
494 * done it for years, and this it won't turn around and
495 * bite us. He says they found it on some game programmers'
496 * forum some time around 2007.
498 ev_uint64_t v = base->GetTickCount_fn();
499 return (DWORD)v | ((v >> 18) & 0xFFFFFFFF00000000);
501 /* Here's the fallback implementation. We have to use
502 * GetTickCount() with its given signature, so we only get
503 * 32 bits worth of milliseconds, which will roll ove every
505 DWORD ticks = GetTickCount();
506 if (ticks < base->last_tick_count) {
507 base->adjust_tick_count += ((ev_uint64_t)1) << 32;
509 base->last_tick_count = ticks;
510 return ticks + base->adjust_tick_count;
515 evutil_configure_monotonic_time_(struct evutil_monotonic_timer *base,
518 const int precise = flags & EV_MONOT_PRECISE;
519 const int fallback = flags & EV_MONOT_FALLBACK;
521 memset(base, 0, sizeof(*base));
523 h = evutil_load_windows_system_library_(TEXT("kernel32.dll"));
524 if (h != NULL && !fallback) {
525 base->GetTickCount64_fn = (ev_GetTickCount_func)GetProcAddress(h, "GetTickCount64");
526 base->GetTickCount_fn = (ev_GetTickCount_func)GetProcAddress(h, "GetTickCount");
529 base->first_tick = base->last_tick_count = evutil_GetTickCount_(base);
530 if (precise && !fallback) {
532 if (QueryPerformanceFrequency(&freq)) {
533 LARGE_INTEGER counter;
534 QueryPerformanceCounter(&counter);
535 base->first_counter = counter.QuadPart;
536 base->usec_per_count = 1.0e6 / freq.QuadPart;
537 base->use_performance_counter = 1;
544 static inline ev_int64_t
547 return i < 0 ? -i : i;
552 evutil_gettime_monotonic_(struct evutil_monotonic_timer *base,
555 ev_uint64_t ticks = evutil_GetTickCount_(base);
556 if (base->use_performance_counter) {
557 /* Here's a trick we took from BitTorrent's libutp, at Greg
558 * Hazel's recommendation. We use QueryPerformanceCounter for
559 * our high-resolution timer, but use GetTickCount*() to keep
560 * it sane, and adjust_monotonic_time() to keep it monotonic.
562 LARGE_INTEGER counter;
563 ev_int64_t counter_elapsed, counter_usec_elapsed, ticks_elapsed;
564 QueryPerformanceCounter(&counter);
565 counter_elapsed = (ev_int64_t)
566 (counter.QuadPart - base->first_counter);
567 ticks_elapsed = ticks - base->first_tick;
568 /* TODO: This may upset VC6. If you need this to work with
569 * VC6, please supply an appropriate patch. */
570 counter_usec_elapsed = (ev_int64_t)
571 (counter_elapsed * base->usec_per_count);
573 if (abs64(ticks_elapsed*1000 - counter_usec_elapsed) > 1000000) {
574 /* It appears that the QueryPerformanceCounter()
575 * result is more than 1 second away from
576 * GetTickCount() result. Let's adjust it to be as
577 * accurate as we can; adjust_monotnonic_time() below
578 * will keep it monotonic. */
579 counter_usec_elapsed = ticks_elapsed * 1000;
580 base->first_counter = (ev_uint64_t) (counter.QuadPart - counter_usec_elapsed / base->usec_per_count);
582 tp->tv_sec = (time_t) (counter_usec_elapsed / 1000000);
583 tp->tv_usec = counter_usec_elapsed % 1000000;
586 /* We're just using GetTickCount(). */
587 tp->tv_sec = (time_t) (ticks / 1000);
588 tp->tv_usec = (ticks % 1000) * 1000;
590 adjust_monotonic_time(base, tp);
596 #if defined(HAVE_FALLBACK_MONOTONIC)
598 And if none of the other options work, let's just use gettimeofday(), and
599 ratchet it forward so that it acts like a monotonic timer, whether it
604 evutil_configure_monotonic_time_(struct evutil_monotonic_timer *base,
607 memset(base, 0, sizeof(*base));
612 evutil_gettime_monotonic_(struct evutil_monotonic_timer *base,
615 if (evutil_gettimeofday(tp, NULL) < 0)
617 adjust_monotonic_time(base, tp);