Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 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 | // |
| 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 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 19 | #include "kmp_os.h" |
| 20 | #include <limits> |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | #include <string> |
Jonathan Peyton | b9e8326 | 2015-12-18 16:19:35 +0000 | [diff] [blame] | 23 | #if KMP_HAVE_X86INTRIN_H |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 24 | #include <x86intrin.h> |
Jonathan Peyton | b9e8326 | 2015-12-18 16:19:35 +0000 | [diff] [blame] | 25 | #endif |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 26 | |
| 27 | class tsc_tick_count { |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 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) {} |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 35 | |
| 36 | public: |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame^] | 37 | tsc_interval_t() : value(0) {} // Construct 0 time duration |
Jonathan Peyton | 8b52459 | 2015-12-17 17:27:51 +0000 | [diff] [blame] | 38 | #if KMP_HAVE_TICK_TIME |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 39 | double seconds() const; // Return the length of a time interval in seconds |
Jonathan Peyton | 8b52459 | 2015-12-17 17:27:51 +0000 | [diff] [blame] | 40 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 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 | } |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 47 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 48 | friend class tsc_tick_count; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 49 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 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 | }; |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 57 | |
Jonathan Peyton | b9e8326 | 2015-12-18 16:19:35 +0000 | [diff] [blame] | 58 | #if KMP_HAVE___BUILTIN_READCYCLECOUNTER |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 59 | tsc_tick_count() |
| 60 | : my_count(static_cast<int64_t>(__builtin_readcyclecounter())) {} |
Jonathan Peyton | b9e8326 | 2015-12-18 16:19:35 +0000 | [diff] [blame] | 61 | #elif KMP_HAVE___RDTSC |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame^] | 62 | tsc_tick_count() : my_count(static_cast<int64_t>(__rdtsc())){} |
Jonathan Peyton | b9e8326 | 2015-12-18 16:19:35 +0000 | [diff] [blame] | 63 | #else |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 64 | #error Must have high resolution timer defined |
Jonathan Peyton | b9e8326 | 2015-12-18 16:19:35 +0000 | [diff] [blame] | 65 | #endif |
Jonathan Peyton | bd3a763 | 2017-09-27 20:36:27 +0000 | [diff] [blame^] | 66 | tsc_tick_count(int64_t value) : my_count(value){} |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 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 | } |
Jonathan Peyton | 8b52459 | 2015-12-17 17:27:51 +0000 | [diff] [blame] | 74 | #if KMP_HAVE_TICK_TIME |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 75 | static double tick_time(); // returns seconds per cycle (period) of clock |
Jonathan Peyton | 8b52459 | 2015-12-17 17:27:51 +0000 | [diff] [blame] | 76 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 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); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 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); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 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); |
Jonathan Peyton | 11dc82f | 2016-05-05 16:15:57 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 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; |
Jonathan Peyton | 11dc82f | 2016-05-05 16:15:57 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Jonathan Peyton | 8b52459 | 2015-12-17 17:27:51 +0000 | [diff] [blame] | 102 | #if KMP_HAVE_TICK_TIME |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 103 | inline double tsc_tick_count::tsc_interval_t::seconds() const { |
| 104 | return value * tick_time(); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 105 | } |
Jonathan Peyton | 8b52459 | 2015-12-17 17:27:51 +0000 | [diff] [blame] | 106 | #endif |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 107 | |
| 108 | extern std::string formatSI(double interval, int width, char unit); |
| 109 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 110 | inline std::string formatSeconds(double interval, int width) { |
| 111 | return formatSI(interval, width, 'S'); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 114 | inline std::string formatTicks(double interval, int width) { |
| 115 | return formatSI(interval, width, 'T'); |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Jim Cownie | 4cc4bb4 | 2014-10-07 16:25:50 +0000 | [diff] [blame] | 118 | #endif // KMP_STATS_TIMING_H |