LLVM OpenMP* Runtime Library
kmp_stats_timing.h
1 #ifndef KMP_STATS_TIMING_H
2 #define KMP_STATS_TIMING_H
3 
9 //===----------------------------------------------------------------------===//
10 //
11 // The LLVM Compiler Infrastructure
12 //
13 // This file is dual licensed under the MIT and the University of Illinois Open
14 // Source Licenses. See LICENSE.txt for details.
15 //
16 //===----------------------------------------------------------------------===//
17 
18 
19 #include "kmp_os.h"
20 #include <limits>
21 #include <stdint.h>
22 #include <string>
23 #if KMP_HAVE_X86INTRIN_H
24 #include <x86intrin.h>
25 #endif
26 
27 class tsc_tick_count {
28 private:
29  int64_t my_count;
30 
31 public:
32  class tsc_interval_t {
33  int64_t value;
34  explicit tsc_interval_t(int64_t _value) : value(_value) {}
35 
36  public:
37  tsc_interval_t() : value(0){}; // Construct 0 time duration
38 #if KMP_HAVE_TICK_TIME
39  double seconds() const; // Return the length of a time interval in seconds
40 #endif
41  double ticks() const { return double(value); }
42  int64_t getValue() const { return value; }
43  tsc_interval_t &operator=(int64_t nvalue) {
44  value = nvalue;
45  return *this;
46  }
47 
48  friend class tsc_tick_count;
49 
50  friend tsc_interval_t operator-(const tsc_tick_count &t1,
51  const tsc_tick_count &t0);
52  friend tsc_interval_t operator-(const tsc_tick_count::tsc_interval_t &i1,
53  const tsc_tick_count::tsc_interval_t &i0);
54  friend tsc_interval_t &operator+=(tsc_tick_count::tsc_interval_t &i1,
55  const tsc_tick_count::tsc_interval_t &i0);
56  };
57 
58 #if KMP_HAVE___BUILTIN_READCYCLECOUNTER
59  tsc_tick_count()
60  : my_count(static_cast<int64_t>(__builtin_readcyclecounter())) {}
61 #elif KMP_HAVE___RDTSC
62  tsc_tick_count() : my_count(static_cast<int64_t>(__rdtsc())){};
63 #else
64 #error Must have high resolution timer defined
65 #endif
66  tsc_tick_count(int64_t value) : my_count(value){};
67  int64_t getValue() const { return my_count; }
68  tsc_tick_count later(tsc_tick_count const other) const {
69  return my_count > other.my_count ? (*this) : other;
70  }
71  tsc_tick_count earlier(tsc_tick_count const other) const {
72  return my_count < other.my_count ? (*this) : other;
73  }
74 #if KMP_HAVE_TICK_TIME
75  static double tick_time(); // returns seconds per cycle (period) of clock
76 #endif
77  static tsc_tick_count now() {
78  return tsc_tick_count();
79  } // returns the rdtsc register value
80  friend tsc_tick_count::tsc_interval_t operator-(const tsc_tick_count &t1,
81  const tsc_tick_count &t0);
82 };
83 
84 inline tsc_tick_count::tsc_interval_t operator-(const tsc_tick_count &t1,
85  const tsc_tick_count &t0) {
86  return tsc_tick_count::tsc_interval_t(t1.my_count - t0.my_count);
87 }
88 
89 inline tsc_tick_count::tsc_interval_t
90 operator-(const tsc_tick_count::tsc_interval_t &i1,
91  const tsc_tick_count::tsc_interval_t &i0) {
92  return tsc_tick_count::tsc_interval_t(i1.value - i0.value);
93 }
94 
95 inline tsc_tick_count::tsc_interval_t &
96 operator+=(tsc_tick_count::tsc_interval_t &i1,
97  const tsc_tick_count::tsc_interval_t &i0) {
98  i1.value += i0.value;
99  return i1;
100 }
101 
102 #if KMP_HAVE_TICK_TIME
103 inline double tsc_tick_count::tsc_interval_t::seconds() const {
104  return value * tick_time();
105 }
106 #endif
107 
108 extern std::string formatSI(double interval, int width, char unit);
109 
110 inline std::string formatSeconds(double interval, int width) {
111  return formatSI(interval, width, 'S');
112 }
113 
114 inline std::string formatTicks(double interval, int width) {
115  return formatSI(interval, width, 'T');
116 }
117 
118 #endif // KMP_STATS_TIMING_H