]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/openmp/runtime/src/kmp_stats_timing.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / openmp / runtime / src / kmp_stats_timing.h
1 #ifndef KMP_STATS_TIMING_H
2 #define KMP_STATS_TIMING_H
3
4 /** @file kmp_stats_timing.h
5  * Access to real time clock and timers.
6  */
7
8 //===----------------------------------------------------------------------===//
9 //
10 //                     The LLVM Compiler Infrastructure
11 //
12 // This file is dual licensed under the MIT and the University of Illinois Open
13 // Source Licenses. See LICENSE.txt for details.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "kmp_os.h"
18 #include <limits>
19 #include <stdint.h>
20 #include <string>
21 #if KMP_HAVE_X86INTRIN_H
22 #include <x86intrin.h>
23 #endif
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
34   public:
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
38 #endif
39     double ticks() const { return double(value); }
40     int64_t getValue() const { return value; }
41     tsc_interval_t &operator=(int64_t nvalue) {
42       value = nvalue;
43       return *this;
44     }
45
46     friend class tsc_tick_count;
47
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);
54   };
55
56 #if KMP_HAVE___BUILTIN_READCYCLECOUNTER
57   tsc_tick_count()
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())) {}
61 #else
62 #error Must have high resolution timer defined
63 #endif
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;
68   }
69   tsc_tick_count earlier(tsc_tick_count const other) const {
70     return my_count < other.my_count ? (*this) : other;
71   }
72 #if KMP_HAVE_TICK_TIME
73   static double tick_time(); // returns seconds per cycle (period) of clock
74 #endif
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);
80 };
81
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);
85 }
86
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);
91 }
92
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) {
96   i1.value += i0.value;
97   return i1;
98 }
99
100 #if KMP_HAVE_TICK_TIME
101 inline double tsc_tick_count::tsc_interval_t::seconds() const {
102   return value * tick_time();
103 }
104 #endif
105
106 extern std::string formatSI(double interval, int width, char unit);
107
108 inline std::string formatSeconds(double interval, int width) {
109   return formatSI(interval, width, 'S');
110 }
111
112 inline std::string formatTicks(double interval, int width) {
113   return formatSI(interval, width, 'T');
114 }
115
116 #endif // KMP_STATS_TIMING_H