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 
20 #include <stdint.h>
21 #include <string>
22 #include <limits>
23 #include "kmp_os.h"
24 #if KMP_HAVE_X86INTRIN_H
25 # include <x86intrin.h>
26 #endif
27 
28 class tsc_tick_count {
29  private:
30  int64_t my_count;
31 
32  public:
33  class tsc_interval_t {
34  int64_t value;
35  explicit tsc_interval_t(int64_t _value) : value(_value) {}
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 
44  friend class tsc_tick_count;
45 
46  friend tsc_interval_t operator-(
47  const tsc_tick_count t1, const tsc_tick_count t0);
48  };
49 
50 #if KMP_HAVE___BUILTIN_READCYCLECOUNTER
51  tsc_tick_count() : my_count(static_cast<int64_t>(__builtin_readcyclecounter())) {}
52 #elif KMP_HAVE___RDTSC
53  tsc_tick_count() : my_count(static_cast<int64_t>(__rdtsc())) {};
54 #else
55 # error Must have high resolution timer defined
56 #endif
57  tsc_tick_count(int64_t value) : my_count(value) {};
58  int64_t getValue() const { return my_count; }
59  tsc_tick_count later (tsc_tick_count const other) const {
60  return my_count > other.my_count ? (*this) : other;
61  }
62  tsc_tick_count earlier(tsc_tick_count const other) const {
63  return my_count < other.my_count ? (*this) : other;
64  }
65 #if KMP_HAVE_TICK_TIME
66  static double tick_time(); // returns seconds per cycle (period) of clock
67 #endif
68  static tsc_tick_count now() { return tsc_tick_count(); } // returns the rdtsc register value
69  friend tsc_tick_count::tsc_interval_t operator-(const tsc_tick_count t1, const tsc_tick_count t0);
70 };
71 
72 inline tsc_tick_count::tsc_interval_t operator-(const tsc_tick_count t1, const tsc_tick_count t0)
73 {
74  return tsc_tick_count::tsc_interval_t( t1.my_count-t0.my_count );
75 }
76 
77 #if KMP_HAVE_TICK_TIME
78 inline double tsc_tick_count::tsc_interval_t::seconds() const
79 {
80  return value*tick_time();
81 }
82 #endif
83 
84 extern std::string formatSI(double interval, int width, char unit);
85 
86 inline std::string formatSeconds(double interval, int width)
87 {
88  return formatSI(interval, width, 'S');
89 }
90 
91 inline std::string formatTicks(double interval, int width)
92 {
93  return formatSI(interval, width, 'T');
94 }
95 
96 class timePair
97 {
98  tsc_tick_count KMP_ALIGN_CACHE start;
99  tsc_tick_count end;
100 
101 public:
102  timePair() : start(-std::numeric_limits<int64_t>::max()), end(-std::numeric_limits<int64_t>::max()) {}
103  tsc_tick_count get_start() const { return start; }
104  tsc_tick_count get_end() const { return end; }
105  tsc_tick_count * get_startp() { return &start; }
106  tsc_tick_count * get_endp() { return &end; }
107 
108  void markStart() { start = tsc_tick_count::now(); }
109  void markEnd() { end = tsc_tick_count::now(); }
110  void set_start(tsc_tick_count s) { start = s; }
111  void set_end (tsc_tick_count e) { end = e; }
112 
113  tsc_tick_count::tsc_interval_t duration() const { return end-start; }
114  std::string format() const;
115 
116 };
117 
118 extern tsc_tick_count::tsc_interval_t computeLastInLastOutInterval(timePair * times, int nTimes);
119 #endif // KMP_STATS_TIMING_H