-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticks_clock.hpp
executable file
·57 lines (43 loc) · 1.21 KB
/
ticks_clock.hpp
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
#ifndef DISTRIB_BASE_TICKS_CLOCK_HEADER
#define DISTRIB_BASE_TICKS_CLOCK_HEADER
#include <time.h>
#include <inttypes.h>
#include "logging.hpp"
namespace base {
// The TicksClock is a very low overhead to measure elapsed time at
// the nanosecond granularity. It uses the CPU's internal timestamp
// counter.
//
// Usage:
// // Measure how long a given computation takes
// Ticks before = TicksClock::getTicks();
// .... do computation ...
// Ticks duration = TicksClock::getTicks() - before;
//
// double in_seconds = duration / TicksClock::ticksPerSecond();
//
class TicksClock {
public:
typedef uint64_t Ticks;
static double ticksPerSecond();
static Ticks getTicks() {
#if defined(__i386__) || defined(__x86_64__)
uint32_t hi;
uint32_t lo;
__asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
return (uint64_t)hi << 32 | lo;
#else
LOG(LogMessage::ERROR) << "no support yet for ticks clock";
return 0;
#endif
}
private:
static pthread_once_t once_control;
TicksClock() { }
~TicksClock() { }
// Non-copyable, non-assignable
TicksClock(TicksClock&);
TicksClock& operator=(TicksClock&);
};
} // namespace base
#endif // DISTRIB_BASE_TICKS_CLOCK_HEADER