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