2016-07-08 16:38:59 +02:00
|
|
|
/* Copyright (c) 2003-2004, Roger Dingledine
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2017-03-15 21:13:17 +01:00
|
|
|
* Copyright (c) 2007-2017, The Tor Project, Inc. */
|
2016-07-08 16:38:59 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
/**
|
|
|
|
* \file compat_time.h
|
|
|
|
*
|
|
|
|
* \brief Functions and types for monotonic times.
|
|
|
|
*
|
|
|
|
* monotime_* functions try to provide a high-resolution monotonic timer with
|
|
|
|
* something the best resolution the system provides. monotime_coarse_*
|
|
|
|
* functions run faster (if the operating system gives us a way to do that)
|
|
|
|
* but produce a less accurate timer: accuracy will probably be on the order
|
|
|
|
* of tens of milliseconds.
|
|
|
|
*/
|
|
|
|
|
2016-07-08 16:38:59 +02:00
|
|
|
#ifndef TOR_COMPAT_TIME_H
|
|
|
|
#define TOR_COMPAT_TIME_H
|
|
|
|
|
|
|
|
#include "orconfig.h"
|
2016-07-21 14:09:00 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#undef HAVE_CLOCK_GETTIME
|
|
|
|
#endif
|
2016-07-08 16:38:59 +02:00
|
|
|
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
#if defined(HAVE_CLOCK_GETTIME)
|
|
|
|
/* to ensure definition of CLOCK_MONOTONIC_COARSE if it's there */
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
2017-09-15 22:15:27 +02:00
|
|
|
#if !defined(HAVE_STRUCT_TIMEVAL_TV_SEC)
|
2016-07-08 16:38:59 +02:00
|
|
|
/** Implementation of timeval for platforms that don't have it. */
|
|
|
|
struct timeval {
|
|
|
|
time_t tv_sec;
|
|
|
|
unsigned int tv_usec;
|
|
|
|
};
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* !defined(HAVE_STRUCT_TIMEVAL_TV_SEC) */
|
2016-07-08 16:38:59 +02:00
|
|
|
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
/** Represents a monotonic timer in a platform-dependent way. */
|
|
|
|
typedef struct monotime_t {
|
|
|
|
#ifdef __APPLE__
|
|
|
|
/* On apple, there is a 64-bit counter whose precision we must look up. */
|
|
|
|
uint64_t abstime_;
|
|
|
|
#elif defined(HAVE_CLOCK_GETTIME)
|
|
|
|
/* It sure would be nice to use clock_gettime(). Posix is a nice thing. */
|
|
|
|
struct timespec ts_;
|
|
|
|
#elif defined (_WIN32)
|
|
|
|
/* On Windows, there is a 64-bit counter whose precision we must look up. */
|
|
|
|
int64_t pcount_;
|
|
|
|
#else
|
|
|
|
#define MONOTIME_USING_GETTIMEOFDAY
|
|
|
|
/* Otherwise, we will be stuck using gettimeofday. */
|
|
|
|
struct timeval tv_;
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(__APPLE__) || ... */
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
} monotime_t;
|
|
|
|
|
2017-09-15 22:15:27 +02:00
|
|
|
#if defined(CLOCK_MONOTONIC_COARSE) && \
|
|
|
|
defined(HAVE_CLOCK_GETTIME)
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
#define MONOTIME_COARSE_FN_IS_DIFFERENT
|
|
|
|
#define monotime_coarse_t monotime_t
|
|
|
|
#elif defined(_WIN32)
|
|
|
|
#define MONOTIME_COARSE_FN_IS_DIFFERENT
|
|
|
|
#define MONOTIME_COARSE_TYPE_IS_DIFFERENT
|
|
|
|
/** Represents a coarse monotonic time in a platform-independent way. */
|
|
|
|
typedef struct monotime_coarse_t {
|
|
|
|
uint64_t tick_count_;
|
|
|
|
} monotime_coarse_t;
|
2017-12-08 15:24:02 +01:00
|
|
|
#elif defined(__APPLE__) && defined(HAVE_MACH_APPROXIMATE_TIME)
|
|
|
|
#define MONOTIME_COARSE_FN_IS_DIFFERENT
|
|
|
|
#define monotime_coarse_t monotime_t
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
#else
|
|
|
|
#define monotime_coarse_t monotime_t
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(CLOCK_MONOTONIC_COARSE) && ... || ... */
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the timing subsystem. This function is idempotent.
|
|
|
|
*/
|
|
|
|
void monotime_init(void);
|
|
|
|
/**
|
|
|
|
* Set <b>out</b> to the current time.
|
|
|
|
*/
|
2016-07-21 10:30:21 +02:00
|
|
|
void monotime_get(monotime_t *out);
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
/**
|
|
|
|
* Return the number of nanoseconds between <b>start</b> and <b>end</b>.
|
|
|
|
*/
|
|
|
|
int64_t monotime_diff_nsec(const monotime_t *start, const monotime_t *end);
|
|
|
|
/**
|
|
|
|
* Return the number of microseconds between <b>start</b> and <b>end</b>.
|
|
|
|
*/
|
|
|
|
int64_t monotime_diff_usec(const monotime_t *start, const monotime_t *end);
|
|
|
|
/**
|
|
|
|
* Return the number of milliseconds between <b>start</b> and <b>end</b>.
|
|
|
|
*/
|
|
|
|
int64_t monotime_diff_msec(const monotime_t *start, const monotime_t *end);
|
|
|
|
/**
|
|
|
|
* Return the number of nanoseconds since the timer system was initialized.
|
|
|
|
*/
|
|
|
|
uint64_t monotime_absolute_nsec(void);
|
|
|
|
/**
|
|
|
|
* Return the number of microseconds since the timer system was initialized.
|
|
|
|
*/
|
|
|
|
uint64_t monotime_absolute_usec(void);
|
|
|
|
/**
|
|
|
|
* Return the number of milliseconds since the timer system was initialized.
|
|
|
|
*/
|
|
|
|
uint64_t monotime_absolute_msec(void);
|
|
|
|
|
2017-12-13 14:28:04 +01:00
|
|
|
/**
|
|
|
|
* Set <b>out</b> to zero.
|
|
|
|
*/
|
|
|
|
void monotime_zero(monotime_t *out);
|
|
|
|
/**
|
|
|
|
* Return true iff <b>out</b> is zero
|
|
|
|
*/
|
|
|
|
int monotime_is_zero(const monotime_t *out);
|
|
|
|
|
2017-12-13 14:54:29 +01:00
|
|
|
/**
|
|
|
|
* Set <b>out</b> to N milliseconds after <b>val</b>.
|
|
|
|
*/
|
|
|
|
/* XXXX We should add a more generic function here if we ever need to */
|
|
|
|
void monotime_add_msec(monotime_t *out, const monotime_t *val, uint32_t msec);
|
|
|
|
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
#if defined(MONOTIME_COARSE_FN_IS_DIFFERENT)
|
|
|
|
/**
|
|
|
|
* Set <b>out</b> to the current coarse time.
|
|
|
|
*/
|
2016-07-21 10:30:21 +02:00
|
|
|
void monotime_coarse_get(monotime_coarse_t *out);
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
uint64_t monotime_coarse_absolute_nsec(void);
|
|
|
|
uint64_t monotime_coarse_absolute_usec(void);
|
|
|
|
uint64_t monotime_coarse_absolute_msec(void);
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(MONOTIME_COARSE_FN_IS_DIFFERENT)) */
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
#define monotime_coarse_get monotime_get
|
|
|
|
#define monotime_coarse_absolute_nsec monotime_absolute_nsec
|
|
|
|
#define monotime_coarse_absolute_usec monotime_absolute_usec
|
|
|
|
#define monotime_coarse_absolute_msec monotime_absolute_msec
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(MONOTIME_COARSE_FN_IS_DIFFERENT) */
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
|
2017-11-21 18:28:16 +01:00
|
|
|
/**
|
|
|
|
* Return a "timestamp" approximation for a coarse monotonic timer.
|
|
|
|
* This timestamp is meant to be fast to calculate and easy to
|
|
|
|
* compare, and have a unit of something roughly around 1 msec.
|
|
|
|
*
|
|
|
|
* It will wrap over from time to time.
|
|
|
|
*
|
|
|
|
* It has no defined zero point.
|
|
|
|
*/
|
|
|
|
uint32_t monotime_coarse_to_stamp(const monotime_coarse_t *t);
|
|
|
|
/**
|
|
|
|
* Convert a difference, expressed in the units of monotime_coarse_to_stamp,
|
|
|
|
* into an approximate number of milliseconds.
|
|
|
|
*/
|
|
|
|
uint64_t monotime_coarse_stamp_units_to_approx_msec(uint64_t units);
|
|
|
|
uint32_t monotime_coarse_get_stamp(void);
|
|
|
|
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
#if defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT)
|
|
|
|
int64_t monotime_coarse_diff_nsec(const monotime_coarse_t *start,
|
|
|
|
const monotime_coarse_t *end);
|
|
|
|
int64_t monotime_coarse_diff_usec(const monotime_coarse_t *start,
|
|
|
|
const monotime_coarse_t *end);
|
|
|
|
int64_t monotime_coarse_diff_msec(const monotime_coarse_t *start,
|
|
|
|
const monotime_coarse_t *end);
|
2017-12-13 14:28:04 +01:00
|
|
|
void monotime_coarse_zero(monotime_coarse_t *out);
|
|
|
|
int monotime_coarse_is_zero(const monotime_coarse_t *val);
|
2017-12-13 14:54:29 +01:00
|
|
|
void monotime_coarse_add_msec(monotime_coarse_t *out,
|
|
|
|
const monotime_coarse_t *val, uint32_t msec);
|
2017-09-15 22:24:44 +02:00
|
|
|
#else /* !(defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT)) */
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
#define monotime_coarse_diff_nsec monotime_diff_nsec
|
|
|
|
#define monotime_coarse_diff_usec monotime_diff_usec
|
|
|
|
#define monotime_coarse_diff_msec monotime_diff_msec
|
2017-12-13 14:28:04 +01:00
|
|
|
#define monotime_coarse_zero monotime_zero
|
|
|
|
#define monotime_coarse_is_zero monotime_is_zero
|
2017-12-13 14:54:29 +01:00
|
|
|
#define monotime_coarse_add_msec monotime_add_msec
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(MONOTIME_COARSE_TYPE_IS_DIFFERENT) */
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
|
2016-07-08 16:38:59 +02:00
|
|
|
void tor_gettimeofday(struct timeval *timeval);
|
|
|
|
|
|
|
|
#ifdef TOR_UNIT_TESTS
|
|
|
|
void tor_sleep_msec(int msec);
|
2016-07-21 11:04:22 +02:00
|
|
|
|
|
|
|
void monotime_enable_test_mocking(void);
|
|
|
|
void monotime_disable_test_mocking(void);
|
|
|
|
void monotime_set_mock_time_nsec(int64_t);
|
|
|
|
#if defined(MONOTIME_COARSE_FN_IS_DIFFERENT)
|
|
|
|
void monotime_coarse_set_mock_time_nsec(int64_t);
|
|
|
|
#else
|
|
|
|
#define monotime_coarse_set_mock_time_nsec monotime_set_mock_time_nsec
|
|
|
|
#endif
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(TOR_UNIT_TESTS) */
|
2016-07-08 16:38:59 +02:00
|
|
|
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
#ifdef COMPAT_TIME_PRIVATE
|
2016-07-13 16:18:15 +02:00
|
|
|
#if defined(_WIN32) || defined(TOR_UNIT_TESTS)
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
STATIC int64_t ratchet_performance_counter(int64_t count_raw);
|
|
|
|
STATIC int64_t ratchet_coarse_performance_counter(int64_t count_raw);
|
|
|
|
#endif
|
2016-07-13 16:18:15 +02:00
|
|
|
#if defined(MONOTIME_USING_GETTIMEOFDAY) || defined(TOR_UNIT_TESTS)
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
STATIC void ratchet_timeval(const struct timeval *timeval_raw,
|
|
|
|
struct timeval *out);
|
|
|
|
#endif
|
2016-07-13 16:18:15 +02:00
|
|
|
#ifdef TOR_UNIT_TESTS
|
|
|
|
void monotime_reset_ratchets_for_testing(void);
|
|
|
|
#endif
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* defined(COMPAT_TIME_PRIVATE) */
|
Basic portable monotonic timer implementation
This code uses QueryPerformanceCounter() [**] on Windows,
mach_absolute_time() on OSX, clock_gettime() where available, and
gettimeofday() [*] elsewhere.
Timer types are stored in an opaque OS-specific format; the only
supported operation is to compute the difference between two timers.
[*] As you know, gettimeofday() isn't monotonic, so we include
a simple ratchet function to ensure that it only moves forward.
[**] As you may not know, QueryPerformanceCounter() isn't actually
always as monotonic as you might like it to be, so we ratchet that
one too.
We also include a "coarse monotonic timer" for cases where we don't
actually need high-resolution time. This is GetTickCount{,64}() on
Windows, clock_gettime(CLOCK_MONOTONIC_COARSE) on Linux, and falls
back to regular monotonic time elsewhere.
2016-07-08 18:53:51 +02:00
|
|
|
|
2017-09-15 22:24:44 +02:00
|
|
|
#endif /* !defined(TOR_COMPAT_TIME_H) */
|
2016-07-08 16:38:59 +02:00
|
|
|
|