1 #ifndef KMP_STATS_TIMING_H
2 #define KMP_STATS_TIMING_H
4 /** @file kmp_stats_timing.h
5 * Access to real time clock and timers.
8 //===----------------------------------------------------------------------===//
10 // The LLVM Compiler Infrastructure
12 // This file is dual licensed under the MIT and the University of Illinois Open
13 // Source Licenses. See LICENSE.txt for details.
15 //===----------------------------------------------------------------------===//
21 #if KMP_HAVE_X86INTRIN_H
22 #include <x86intrin.h>
25 class tsc_tick_count {
30 class tsc_interval_t {
32 explicit tsc_interval_t(int64_t _value) : value(_value) {}
35 tsc_interval_t() : value(0) {} // Construct 0 time duration
36 #if KMP_HAVE_TICK_TIME
37 double seconds() const; // Return the length of a time interval in seconds
39 double ticks() const { return double(value); }
40 int64_t getValue() const { return value; }
41 tsc_interval_t &operator=(int64_t nvalue) {
46 friend class tsc_tick_count;
48 friend tsc_interval_t operator-(const tsc_tick_count &t1,
49 const tsc_tick_count &t0);
50 friend tsc_interval_t operator-(const tsc_tick_count::tsc_interval_t &i1,
51 const tsc_tick_count::tsc_interval_t &i0);
52 friend tsc_interval_t &operator+=(tsc_tick_count::tsc_interval_t &i1,
53 const tsc_tick_count::tsc_interval_t &i0);
56 #if KMP_HAVE___BUILTIN_READCYCLECOUNTER
58 : my_count(static_cast<int64_t>(__builtin_readcyclecounter())) {}
59 #elif KMP_HAVE___RDTSC
60 tsc_tick_count() : my_count(static_cast<int64_t>(__rdtsc())) {}
62 #error Must have high resolution timer defined
64 tsc_tick_count(int64_t value) : my_count(value) {}
65 int64_t getValue() const { return my_count; }
66 tsc_tick_count later(tsc_tick_count const other) const {
67 return my_count > other.my_count ? (*this) : other;
69 tsc_tick_count earlier(tsc_tick_count const other) const {
70 return my_count < other.my_count ? (*this) : other;
72 #if KMP_HAVE_TICK_TIME
73 static double tick_time(); // returns seconds per cycle (period) of clock
75 static tsc_tick_count now() {
76 return tsc_tick_count();
77 } // returns the rdtsc register value
78 friend tsc_tick_count::tsc_interval_t operator-(const tsc_tick_count &t1,
79 const tsc_tick_count &t0);
82 inline tsc_tick_count::tsc_interval_t operator-(const tsc_tick_count &t1,
83 const tsc_tick_count &t0) {
84 return tsc_tick_count::tsc_interval_t(t1.my_count - t0.my_count);
87 inline tsc_tick_count::tsc_interval_t
88 operator-(const tsc_tick_count::tsc_interval_t &i1,
89 const tsc_tick_count::tsc_interval_t &i0) {
90 return tsc_tick_count::tsc_interval_t(i1.value - i0.value);
93 inline tsc_tick_count::tsc_interval_t &
94 operator+=(tsc_tick_count::tsc_interval_t &i1,
95 const tsc_tick_count::tsc_interval_t &i0) {
100 #if KMP_HAVE_TICK_TIME
101 inline double tsc_tick_count::tsc_interval_t::seconds() const {
102 return value * tick_time();
106 extern std::string formatSI(double interval, int width, char unit);
108 inline std::string formatSeconds(double interval, int width) {
109 return formatSI(interval, width, 'S');
112 inline std::string formatTicks(double interval, int width) {
113 return formatSI(interval, width, 'T');
116 #endif // KMP_STATS_TIMING_H