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 
25 class tsc_tick_count {
26  private:
27  int64_t my_count;
28 
29  public:
30  class tsc_interval_t {
31  int64_t value;
32  explicit tsc_interval_t(int64_t _value) : value(_value) {}
33  public:
34  tsc_interval_t() : value(0) {}; // Construct 0 time duration
35  double seconds() const; // Return the length of a time interval in seconds
36  double ticks() const { return double(value); }
37  int64_t getValue() const { return value; }
38 
39  friend class tsc_tick_count;
40 
41  friend tsc_interval_t operator-(
42  const tsc_tick_count t1, const tsc_tick_count t0);
43  };
44 
45  tsc_tick_count() : my_count(static_cast<int64_t>(__rdtsc())) {};
46  tsc_tick_count(int64_t value) : my_count(value) {};
47  int64_t getValue() const { return my_count; }
48  tsc_tick_count later (tsc_tick_count const other) const {
49  return my_count > other.my_count ? (*this) : other;
50  }
51  tsc_tick_count earlier(tsc_tick_count const other) const {
52  return my_count < other.my_count ? (*this) : other;
53  }
54  static double tick_time(); // returns seconds per cycle (period) of clock
55  static tsc_tick_count now() { return tsc_tick_count(); } // returns the rdtsc register value
56  friend tsc_tick_count::tsc_interval_t operator-(const tsc_tick_count t1, const tsc_tick_count t0);
57 };
58 
59 inline tsc_tick_count::tsc_interval_t operator-(const tsc_tick_count t1, const tsc_tick_count t0)
60 {
61  return tsc_tick_count::tsc_interval_t( t1.my_count-t0.my_count );
62 }
63 
64 inline double tsc_tick_count::tsc_interval_t::seconds() const
65 {
66  return value*tick_time();
67 }
68 
69 extern std::string formatSI(double interval, int width, char unit);
70 
71 inline std::string formatSeconds(double interval, int width)
72 {
73  return formatSI(interval, width, 'S');
74 }
75 
76 inline std::string formatTicks(double interval, int width)
77 {
78  return formatSI(interval, width, 'T');
79 }
80 
81 class timePair
82 {
83  tsc_tick_count KMP_ALIGN_CACHE start;
84  tsc_tick_count end;
85 
86 public:
87  timePair() : start(-std::numeric_limits<int64_t>::max()), end(-std::numeric_limits<int64_t>::max()) {}
88  tsc_tick_count get_start() const { return start; }
89  tsc_tick_count get_end() const { return end; }
90  tsc_tick_count * get_startp() { return &start; }
91  tsc_tick_count * get_endp() { return &end; }
92 
93  void markStart() { start = tsc_tick_count::now(); }
94  void markEnd() { end = tsc_tick_count::now(); }
95  void set_start(tsc_tick_count s) { start = s; }
96  void set_end (tsc_tick_count e) { end = e; }
97 
98  tsc_tick_count::tsc_interval_t duration() const { return end-start; }
99  std::string format() const;
100 
101 };
102 
103 extern tsc_tick_count::tsc_interval_t computeLastInLastOutInterval(timePair * times, int nTimes);
104 #endif // KMP_STATS_TIMING_H