Changes to update Tomato RAF.
[tomato.git] / release / src / router / dnscrypt / src / libnacl / cpucycles / monotonic.c
blob321afd9a759ffa2bed8d4f0b250841fa345ea7dc
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/param.h>
7 #include <sys/sysctl.h>
9 static double cpufrequency = 0;
11 static void init(void)
13 long result = 0; size_t resultlen = sizeof(long);
14 sysctlbyname("machdep.tsc_freq",&result,&resultlen,0,0);
15 cpufrequency = result;
18 long long cpucycles_monotonic(void)
20 double result;
21 struct timespec t;
22 if (!cpufrequency) init();
23 clock_gettime(CLOCK_MONOTONIC,&t);
24 result = t.tv_nsec;
25 result *= 0.000000001;
26 result += (double) t.tv_sec;
27 result *= cpufrequency;
28 return result;
31 long long cpucycles_monotonic_persecond(void)
33 if (!cpufrequency) init();
34 return cpufrequency;