aboutsummaryrefslogtreecommitdiffstats
path: root/checks/clock.cpp
blob: 73f2c0e39520143f1be3344a39260d730ce913cf (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
#include <botan/botan.h>
using namespace Botan;

#include "common.h"
#include <time.h>

/*
  Using clock() or similiar is bad news when using a hardware-based Engine,
  as all the stuff is offloaded and we use zero CPU time, which makes the
  benchmarks and such take forever.
*/

#define USE_CLOCK         0
#define USE_TIMES         0
#define USE_POSIX_GETTIME 0
#define USE_RDTSC         1

/* If using USE_RDTSC, set to your CPU's Mhz */
#define CPU_MHZ 2400

#if USE_CLOCK

   u64bit get_clock() { return clock(); }
   u64bit get_ticks() { return CLOCKS_PER_SEC; }

#elif USE_TIMES

  #include <sys/times.h>
  #include <unistd.h>
  u64bit get_clock() { return times(0); }
  u64bit get_ticks() { return sysconf(_SC_CLK_TCK); }

#elif USE_POSIX_GETTIME

u64bit get_clock()
   {
   struct timespec tv;
   clock_gettime(CLOCK_REALTIME, &tv);

   return (tv.tv_sec * 1000000000 + tv.tv_nsec) / 1000;
   }

u64bit get_ticks() { return 1000000; }
#elif USE_RDTSC

  u64bit get_clock()
     {
     u64bit rtc = 0;
     u32bit rtc_low = 0, rtc_high = 0;
     asm volatile("rdtsc" : "=d" (rtc_high), "=a" (rtc_low));
     rtc = ((u64bit)rtc_high << 32) | rtc_low;
     return rtc / 1000;
     }

  u64bit get_ticks() { return CPU_MHZ * 1000; }
#else
  #error "Must choose a timing method!"
#endif