diff options
Diffstat (limited to 'nacl/cpucycles/monotoniccpuinfo.c')
-rw-r--r-- | nacl/cpucycles/monotoniccpuinfo.c | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/nacl/cpucycles/monotoniccpuinfo.c b/nacl/cpucycles/monotoniccpuinfo.c new file mode 100644 index 00000000..609c6305 --- /dev/null +++ b/nacl/cpucycles/monotoniccpuinfo.c | |||
@@ -0,0 +1,33 @@ | |||
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 | #include "osfreq.c" | ||
8 | |||
9 | static double cpufrequency = 0; | ||
10 | |||
11 | static void init(void) | ||
12 | { | ||
13 | cpufrequency = osfreq(); | ||
14 | } | ||
15 | |||
16 | long long cpucycles_monotoniccpuinfo(void) | ||
17 | { | ||
18 | double result; | ||
19 | struct timespec t; | ||
20 | if (!cpufrequency) init(); | ||
21 | clock_gettime(CLOCK_MONOTONIC,&t); | ||
22 | result = t.tv_nsec; | ||
23 | result *= 0.000000001; | ||
24 | result += (double) t.tv_sec; | ||
25 | result *= cpufrequency; | ||
26 | return result; | ||
27 | } | ||
28 | |||
29 | long long cpucycles_monotoniccpuinfo_persecond(void) | ||
30 | { | ||
31 | if (!cpufrequency) init(); | ||
32 | return cpufrequency; | ||
33 | } | ||