aboutsummaryrefslogtreecommitdiff
path: root/src/timing.c
blob: 25cbba77bf615af8a7c9f969c2b078dfc2282a8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* timing.c -- cross-platform wall time
 * Copyright (C) 2013  Galois, Inc.  All rights reserved. */

#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif
#define _POSIX_C_SOURCE 199309L

#include <stdint.h>

#include "timing.h"
#include "require.h"

#define ignore(var) ((void)(var))

const uint64_t MICRO_PER_BASE = 1000000;
const uint64_t NANO_PER_MICRO = 1000;

// Try to use the POSIX timers, if possible.
#if HAVE_UNISTD_H
#	include <unistd.h>
#endif
#if (HAVE_UNISTD_H && _POSIX_TIMERS > 0)

#	include <time.h>

#	if __linux__
		/* Hopefully, you're running linux >=2.6.28, and
		 * CLOCK_MONOTONIC_RAW exists. */
		static const clockid_t CLOCK_ID = CLOCK_MONOTONIC_RAW;
#	elif _POSIX_MONOTONIC_CLOCK
		// You're not on Linux, but you've got a POSIX monotonic clock.
		static const clockid_t CLOCK_ID = CLOCK_MONOTONIC;
#	else
		// Just use the POSIX wall clock.
		static const clockid_t CLOCK_ID = CLOCK_REALTIME;
#	endif

	int timing_get_wall(uint64_t *const result)
	{
		int r = 0;
		struct timespec now;
		require_zero(clock_gettime(CLOCK_ID, &now), 1);
		*result = (uint64_t)now.tv_sec * MICRO_PER_BASE
			+ (uint64_t)now.tv_nsec / NANO_PER_MICRO;
	done:	return r;
	}

#elif (__APPLE__ && __MACH__)

	/* No POSIX timers, but we're on a Mac, so we can use
	 * 'mach_absolute_time'. */
#	include <mach/mach_time.h>

	int timing_get_wall(uint64_t *const result)
	{
		static mach_timebase_info_data_t timebase_info;
		int r = 0;
		*result = mach_absolute_time();
		/* *result is now in units of Mach ticks.  Convert that to
                 * nanoseconds using the timebase information that Mach
                 * provides.  Note that 'timebase_info' is static, so it got
                 * zeroed out at the start of the program run; if its
                 * denominator is still zero, we've definitely not stored
                 * anything in it yet. */
		if (timebase_info.denom == 0) {
			require_zero(mach_timebase_info(&timebase_info), 1);
		}
		*result = *result * timebase_info.numer / timebase_info.denom;
		// Convert nanoseconds to microseconds.
		*result /= NANO_PER_MICRO;
	done:	return r;
	}

#else

	// I have no clue what wacky platform you're on.
#	error Unsupported timing platform

	int timing_get_wall(uint64_t *const result)
	{
		ignore(result);
		return -1;
	}

#endif