Intel® OpenMP* Runtime Library
 All Classes Functions Variables Typedefs Enumerations Enumerator Groups Pages
kmp_stats_timing.h
1 #ifndef KMP_STATS_TIMING_H
2 #define KMP_STATS_TIMING_H
3 
8 /* <copyright>
9  Copyright (c) 1997-2015 Intel Corporation. All Rights Reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions
13  are met:
14 
15  * Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17  * Redistributions in binary form must reproduce the above copyright
18  notice, this list of conditions and the following disclaimer in the
19  documentation and/or other materials provided with the distribution.
20  * Neither the name of Intel Corporation nor the names of its
21  contributors may be used to endorse or promote products derived
22  from this software without specific prior written permission.
23 
24  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 
36 </copyright> */
37 
38 
39 #include <stdint.h>
40 #include <string>
41 #include <limits>
42 #include "kmp_os.h"
43 
44 class tsc_tick_count {
45  private:
46  int64_t my_count;
47 
48  public:
49  class tsc_interval_t {
50  int64_t value;
51  explicit tsc_interval_t(int64_t _value) : value(_value) {}
52  public:
53  tsc_interval_t() : value(0) {}; // Construct 0 time duration
54  double seconds() const; // Return the length of a time interval in seconds
55  double ticks() const { return double(value); }
56  int64_t getValue() const { return value; }
57 
58  friend class tsc_tick_count;
59 
60  friend tsc_interval_t operator-(
61  const tsc_tick_count t1, const tsc_tick_count t0);
62  };
63 
64  tsc_tick_count() : my_count(static_cast<int64_t>(__rdtsc())) {};
65  tsc_tick_count(int64_t value) : my_count(value) {};
66  int64_t getValue() const { return my_count; }
67  tsc_tick_count later (tsc_tick_count const other) const {
68  return my_count > other.my_count ? (*this) : other;
69  }
70  tsc_tick_count earlier(tsc_tick_count const other) const {
71  return my_count < other.my_count ? (*this) : other;
72  }
73  static double tick_time(); // returns seconds per cycle (period) of clock
74  static tsc_tick_count now() { return tsc_tick_count(); } // returns the rdtsc register value
75  friend tsc_tick_count::tsc_interval_t operator-(const tsc_tick_count t1, const tsc_tick_count t0);
76 };
77 
78 inline tsc_tick_count::tsc_interval_t operator-(const tsc_tick_count t1, const tsc_tick_count t0)
79 {
80  return tsc_tick_count::tsc_interval_t( t1.my_count-t0.my_count );
81 }
82 
83 inline double tsc_tick_count::tsc_interval_t::seconds() const
84 {
85  return value*tick_time();
86 }
87 
88 extern std::string formatSI(double interval, int width, char unit);
89 
90 inline std::string formatSeconds(double interval, int width)
91 {
92  return formatSI(interval, width, 'S');
93 }
94 
95 inline std::string formatTicks(double interval, int width)
96 {
97  return formatSI(interval, width, 'T');
98 }
99 
100 class timePair
101 {
102  tsc_tick_count KMP_ALIGN_CACHE start;
103  tsc_tick_count end;
104 
105 public:
106  timePair() : start(-std::numeric_limits<int64_t>::max()), end(-std::numeric_limits<int64_t>::max()) {}
107  tsc_tick_count get_start() const { return start; }
108  tsc_tick_count get_end() const { return end; }
109  tsc_tick_count * get_startp() { return &start; }
110  tsc_tick_count * get_endp() { return &end; }
111 
112  void markStart() { start = tsc_tick_count::now(); }
113  void markEnd() { end = tsc_tick_count::now(); }
114  void set_start(tsc_tick_count s) { start = s; }
115  void set_end (tsc_tick_count e) { end = e; }
116 
117  tsc_tick_count::tsc_interval_t duration() const { return end-start; }
118  std::string format() const;
119 
120 };
121 
122 extern tsc_tick_count::tsc_interval_t computeLastInLastOutInterval(timePair * times, int nTimes);
123 #endif // KMP_STATS_TIMING_H