aboutsummaryrefslogtreecommitdiffstats
path: root/checks/clock.cpp
blob: e5565ff9c2e2139f3fca1c84426b9bf9c0cb1986 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "timer.h"
#include <botan/build.h>

/*************************************************
* Platform specific settings for which clock type to use
*/

#if !defined(USE_CLOCK_GETTIME) && defined(BOTAN_TARGET_OS_IS_LINUX)
  #define USE_GETTIMEOFDAY 1
#endif

#if !defined(USE_GETTIMEOFDAY) && defined(BOTAN_TARGET_OS_IS_FREEBSD)
  #define USE_GETTIMEOFDAY 1
#endif

/*************************************************
  Set the default, which is clock
*/
#ifndef USE_CLOCK_GETTIME
  #define USE_CLOCK_GETTIME 0
#endif

#ifndef USE_GETTIMEOFDAY
  #define USE_GETTIMEOFDAY 0
#endif

#ifndef USE_TIMES
  #define USE_TIMES 0
#endif

/* the default: ANSI/ISO clock */
#ifndef USE_CLOCK
  #define USE_CLOCK 1
#endif

#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

/* The implementation: */
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
   }