blob: 84252ec13805446177239bcf96b0a482fbcad49c [file] [log] [blame]
Jim Cownie4cc4bb42014-10-07 16:25:50 +00001#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 Cownie4cc4bb42014-10-07 16:25:50 +00008//===----------------------------------------------------------------------===//
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 Peyton30419822017-05-12 18:01:32 +000017#include "kmp_os.h"
18#include <limits>
Jim Cownie4cc4bb42014-10-07 16:25:50 +000019#include <stdint.h>
20#include <string>
Jonathan Peytonb9e83262015-12-18 16:19:35 +000021#if KMP_HAVE_X86INTRIN_H
Jonathan Peyton30419822017-05-12 18:01:32 +000022#include <x86intrin.h>
Jonathan Peytonb9e83262015-12-18 16:19:35 +000023#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +000024
25class tsc_tick_count {
Jonathan Peyton30419822017-05-12 18:01:32 +000026private:
27 int64_t my_count;
28
29public:
30 class tsc_interval_t {
31 int64_t value;
32 explicit tsc_interval_t(int64_t _value) : value(_value) {}
Jim Cownie4cc4bb42014-10-07 16:25:50 +000033
34 public:
Jonathan Peytonbd3a7632017-09-27 20:36:27 +000035 tsc_interval_t() : value(0) {} // Construct 0 time duration
Jonathan Peyton8b524592015-12-17 17:27:51 +000036#if KMP_HAVE_TICK_TIME
Jonathan Peyton30419822017-05-12 18:01:32 +000037 double seconds() const; // Return the length of a time interval in seconds
Jonathan Peyton8b524592015-12-17 17:27:51 +000038#endif
Jonathan Peyton30419822017-05-12 18:01:32 +000039 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 Cownie4cc4bb42014-10-07 16:25:50 +000045
Jonathan Peyton30419822017-05-12 18:01:32 +000046 friend class tsc_tick_count;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000047
Jonathan Peyton30419822017-05-12 18:01:32 +000048 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 Cownie4cc4bb42014-10-07 16:25:50 +000055
Jonathan Peytonb9e83262015-12-18 16:19:35 +000056#if KMP_HAVE___BUILTIN_READCYCLECOUNTER
Jonathan Peyton30419822017-05-12 18:01:32 +000057 tsc_tick_count()
58 : my_count(static_cast<int64_t>(__builtin_readcyclecounter())) {}
Jonathan Peytonb9e83262015-12-18 16:19:35 +000059#elif KMP_HAVE___RDTSC
Jonathan Peyton94a114f2017-10-20 19:30:57 +000060 tsc_tick_count() : my_count(static_cast<int64_t>(__rdtsc())) {}
Jonathan Peytonb9e83262015-12-18 16:19:35 +000061#else
Jonathan Peyton30419822017-05-12 18:01:32 +000062#error Must have high resolution timer defined
Jonathan Peytonb9e83262015-12-18 16:19:35 +000063#endif
Jonathan Peyton94a114f2017-10-20 19:30:57 +000064 tsc_tick_count(int64_t value) : my_count(value) {}
Jonathan Peyton30419822017-05-12 18:01:32 +000065 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 Peyton8b524592015-12-17 17:27:51 +000072#if KMP_HAVE_TICK_TIME
Jonathan Peyton30419822017-05-12 18:01:32 +000073 static double tick_time(); // returns seconds per cycle (period) of clock
Jonathan Peyton8b524592015-12-17 17:27:51 +000074#endif
Jonathan Peyton30419822017-05-12 18:01:32 +000075 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 Cownie4cc4bb42014-10-07 16:25:50 +000080};
81
Jonathan Peyton30419822017-05-12 18:01:32 +000082inline 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 Cownie4cc4bb42014-10-07 16:25:50 +000085}
86
Jonathan Peyton30419822017-05-12 18:01:32 +000087inline tsc_tick_count::tsc_interval_t
88operator-(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 Peyton11dc82f2016-05-05 16:15:57 +000091}
92
Jonathan Peyton30419822017-05-12 18:01:32 +000093inline tsc_tick_count::tsc_interval_t &
94operator+=(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 Peyton11dc82f2016-05-05 16:15:57 +000098}
99
Jonathan Peyton8b524592015-12-17 17:27:51 +0000100#if KMP_HAVE_TICK_TIME
Jonathan Peyton30419822017-05-12 18:01:32 +0000101inline double tsc_tick_count::tsc_interval_t::seconds() const {
102 return value * tick_time();
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000103}
Jonathan Peyton8b524592015-12-17 17:27:51 +0000104#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000105
106extern std::string formatSI(double interval, int width, char unit);
107
Jonathan Peyton30419822017-05-12 18:01:32 +0000108inline std::string formatSeconds(double interval, int width) {
109 return formatSI(interval, width, 'S');
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000110}
111
Jonathan Peyton30419822017-05-12 18:01:32 +0000112inline std::string formatTicks(double interval, int width) {
113 return formatSI(interval, width, 'T');
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000114}
115
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000116#endif // KMP_STATS_TIMING_H