blob: 69195b91821412664b12454961899a8049b59a82 [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
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 Peyton30419822017-05-12 18:01:32 +000019#include "kmp_os.h"
20#include <limits>
Jim Cownie4cc4bb42014-10-07 16:25:50 +000021#include <stdint.h>
22#include <string>
Jonathan Peytonb9e83262015-12-18 16:19:35 +000023#if KMP_HAVE_X86INTRIN_H
Jonathan Peyton30419822017-05-12 18:01:32 +000024#include <x86intrin.h>
Jonathan Peytonb9e83262015-12-18 16:19:35 +000025#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +000026
27class tsc_tick_count {
Jonathan Peyton30419822017-05-12 18:01:32 +000028private:
29 int64_t my_count;
30
31public:
32 class tsc_interval_t {
33 int64_t value;
34 explicit tsc_interval_t(int64_t _value) : value(_value) {}
Jim Cownie4cc4bb42014-10-07 16:25:50 +000035
36 public:
Jonathan Peyton30419822017-05-12 18:01:32 +000037 tsc_interval_t() : value(0){}; // Construct 0 time duration
Jonathan Peyton8b524592015-12-17 17:27:51 +000038#if KMP_HAVE_TICK_TIME
Jonathan Peyton30419822017-05-12 18:01:32 +000039 double seconds() const; // Return the length of a time interval in seconds
Jonathan Peyton8b524592015-12-17 17:27:51 +000040#endif
Jonathan Peyton30419822017-05-12 18:01:32 +000041 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 Cownie4cc4bb42014-10-07 16:25:50 +000047
Jonathan Peyton30419822017-05-12 18:01:32 +000048 friend class tsc_tick_count;
Jim Cownie4cc4bb42014-10-07 16:25:50 +000049
Jonathan Peyton30419822017-05-12 18:01:32 +000050 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 Cownie4cc4bb42014-10-07 16:25:50 +000057
Jonathan Peytonb9e83262015-12-18 16:19:35 +000058#if KMP_HAVE___BUILTIN_READCYCLECOUNTER
Jonathan Peyton30419822017-05-12 18:01:32 +000059 tsc_tick_count()
60 : my_count(static_cast<int64_t>(__builtin_readcyclecounter())) {}
Jonathan Peytonb9e83262015-12-18 16:19:35 +000061#elif KMP_HAVE___RDTSC
Jonathan Peyton30419822017-05-12 18:01:32 +000062 tsc_tick_count() : my_count(static_cast<int64_t>(__rdtsc())){};
Jonathan Peytonb9e83262015-12-18 16:19:35 +000063#else
Jonathan Peyton30419822017-05-12 18:01:32 +000064#error Must have high resolution timer defined
Jonathan Peytonb9e83262015-12-18 16:19:35 +000065#endif
Jonathan Peyton30419822017-05-12 18:01:32 +000066 tsc_tick_count(int64_t value) : my_count(value){};
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 Peyton8b524592015-12-17 17:27:51 +000074#if KMP_HAVE_TICK_TIME
Jonathan Peyton30419822017-05-12 18:01:32 +000075 static double tick_time(); // returns seconds per cycle (period) of clock
Jonathan Peyton8b524592015-12-17 17:27:51 +000076#endif
Jonathan Peyton30419822017-05-12 18:01:32 +000077 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 Cownie4cc4bb42014-10-07 16:25:50 +000082};
83
Jonathan Peyton30419822017-05-12 18:01:32 +000084inline 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 Cownie4cc4bb42014-10-07 16:25:50 +000087}
88
Jonathan Peyton30419822017-05-12 18:01:32 +000089inline tsc_tick_count::tsc_interval_t
90operator-(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 Peyton11dc82f2016-05-05 16:15:57 +000093}
94
Jonathan Peyton30419822017-05-12 18:01:32 +000095inline tsc_tick_count::tsc_interval_t &
96operator+=(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 Peyton11dc82f2016-05-05 16:15:57 +0000100}
101
Jonathan Peyton8b524592015-12-17 17:27:51 +0000102#if KMP_HAVE_TICK_TIME
Jonathan Peyton30419822017-05-12 18:01:32 +0000103inline double tsc_tick_count::tsc_interval_t::seconds() const {
104 return value * tick_time();
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000105}
Jonathan Peyton8b524592015-12-17 17:27:51 +0000106#endif
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000107
108extern std::string formatSI(double interval, int width, char unit);
109
Jonathan Peyton30419822017-05-12 18:01:32 +0000110inline std::string formatSeconds(double interval, int width) {
111 return formatSI(interval, width, 'S');
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000112}
113
Jonathan Peyton30419822017-05-12 18:01:32 +0000114inline std::string formatTicks(double interval, int width) {
115 return formatSI(interval, width, 'T');
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000116}
117
Jim Cownie4cc4bb42014-10-07 16:25:50 +0000118#endif // KMP_STATS_TIMING_H