summaryrefslogtreecommitdiff
path: root/nacl/cpucycles/monotonic.c
diff options
context:
space:
mode:
Diffstat (limited to 'nacl/cpucycles/monotonic.c')
-rw-r--r--nacl/cpucycles/monotonic.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/nacl/cpucycles/monotonic.c b/nacl/cpucycles/monotonic.c
new file mode 100644
index 00000000..412a44fb
--- /dev/null
+++ b/nacl/cpucycles/monotonic.c
@@ -0,0 +1,34 @@
1#include <time.h>
2#include <stdio.h>
3#include <unistd.h>
4#include <sys/time.h>
5#include <sys/types.h>
6#include <sys/sysctl.h>
7
8static double cpufrequency = 0;
9
10static void init(void)
11{
12 long result = 0; size_t resultlen = sizeof(long);
13 sysctlbyname("machdep.tsc_freq",&result,&resultlen,0,0);
14 cpufrequency = result;
15}
16
17long long cpucycles_monotonic(void)
18{
19 double result;
20 struct timespec t;
21 if (!cpufrequency) init();
22 clock_gettime(CLOCK_MONOTONIC,&t);
23 result = t.tv_nsec;
24 result *= 0.000000001;
25 result += (double) t.tv_sec;
26 result *= cpufrequency;
27 return result;
28}
29
30long long cpucycles_monotonic_persecond(void)
31{
32 if (!cpufrequency) init();
33 return cpufrequency;
34}