diff options
author | irungentoo <irungentoo@gmail.com> | 2013-07-02 09:53:34 -0400 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-07-02 09:53:34 -0400 |
commit | e2967396ac73cb7410787886cdaf072a184ffc49 (patch) | |
tree | 527a74d25a4a0705fc641994fd35bfab22662034 /nacl/cpucycles/monotonic.c | |
parent | 8928c817df345f29aa0b194743595aa11bd6a8ba (diff) |
Added NaCl crypto library.
Diffstat (limited to 'nacl/cpucycles/monotonic.c')
-rw-r--r-- | nacl/cpucycles/monotonic.c | 34 |
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 | |||
8 | static double cpufrequency = 0; | ||
9 | |||
10 | static 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 | |||
17 | long 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 | |||
30 | long long cpucycles_monotonic_persecond(void) | ||
31 | { | ||
32 | if (!cpufrequency) init(); | ||
33 | return cpufrequency; | ||
34 | } | ||