aboutsummaryrefslogtreecommitdiffstats
path: root/checks/clock.cpp
blob: 55c1eaf2677ca211fb421b4750ed390a6cce44ae (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
#include "timer.h"
#include <botan/build.h>

#define USE_CLOCK_GETTIME 1
#define USE_GETTIMEOFDAY 0
#define USE_TIMES 0
#define USE_CLOCK 0

#if USE_CLOCK_GETTIME
  #include <time.h>
#elif USE_GETTIMEOFDAY
  #include <sys/time.h>
#elif USE_TIMES
  #include <sys/times.h>
  #include <unistd.h>
#elif USE_CLOCK
  #include <time.h>
#endif

u64bit Timer::get_clock()
   {
   static const u64bit billion = 1000000000;

#if USE_CLOCK_GETTIME
   struct timespec tv;
   clock_gettime(CLOCK_REALTIME, &tv);
   return (billion * tv.tv_sec + tv.tv_nsec);
#elif USE_GETTIMEOFDAY
   struct timeval tv;
   gettimeofday(&tv, 0);
   return (billion * tv.tv_sec + 1000 * tv.tv_usec);
#elif USE_TIMES

   struct tms tms;
   times(&tms);

   static const u64bit clocks_to_nanoseconds =
      (billion / sysconf(_SC_CLK_TCK));

   return (tms.tms_utime * clocks_to_nanoseconds);
#elif USE_CLOCK
   static const u64bit clocks_to_nanoseconds =
      (billion / CLOCKS_PER_SEC);

   return clock() * clocks_to_nanoseconds;
#endif
   }