LLVM  4.0.0
Timer.h
Go to the documentation of this file.
1 //===-- llvm/Support/Timer.h - Interval Timing Support ----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_SUPPORT_TIMER_H
11 #define LLVM_SUPPORT_TIMER_H
12 
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/DataTypes.h"
15 #include <cassert>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 namespace llvm {
21 
22 class Timer;
23 class TimerGroup;
24 class raw_ostream;
25 
26 class TimeRecord {
27  double WallTime; ///< Wall clock time elapsed in seconds.
28  double UserTime; ///< User time elapsed.
29  double SystemTime; ///< System time elapsed.
30  ssize_t MemUsed; ///< Memory allocated (in bytes).
31 public:
32  TimeRecord() : WallTime(0), UserTime(0), SystemTime(0), MemUsed(0) {}
33 
34  /// Get the current time and memory usage. If Start is true we get the memory
35  /// usage before the time, otherwise we get time before memory usage. This
36  /// matters if the time to get the memory usage is significant and shouldn't
37  /// be counted as part of a duration.
38  static TimeRecord getCurrentTime(bool Start = true);
39 
40  double getProcessTime() const { return UserTime + SystemTime; }
41  double getUserTime() const { return UserTime; }
42  double getSystemTime() const { return SystemTime; }
43  double getWallTime() const { return WallTime; }
44  ssize_t getMemUsed() const { return MemUsed; }
45 
46  bool operator<(const TimeRecord &T) const {
47  // Sort by Wall Time elapsed, as it is the only thing really accurate
48  return WallTime < T.WallTime;
49  }
50 
51  void operator+=(const TimeRecord &RHS) {
52  WallTime += RHS.WallTime;
53  UserTime += RHS.UserTime;
54  SystemTime += RHS.SystemTime;
55  MemUsed += RHS.MemUsed;
56  }
57  void operator-=(const TimeRecord &RHS) {
58  WallTime -= RHS.WallTime;
59  UserTime -= RHS.UserTime;
60  SystemTime -= RHS.SystemTime;
61  MemUsed -= RHS.MemUsed;
62  }
63 
64  /// Print the current time record to \p OS, with a breakdown showing
65  /// contributions to the \p Total time record.
66  void print(const TimeRecord &Total, raw_ostream &OS) const;
67 };
68 
69 /// This class is used to track the amount of time spent between invocations of
70 /// its startTimer()/stopTimer() methods. Given appropriate OS support it can
71 /// also keep track of the RSS of the program at various points. By default,
72 /// the Timer will print the amount of time it has captured to standard error
73 /// when the last timer is destroyed, otherwise it is printed when its
74 /// TimerGroup is destroyed. Timers do not print their information if they are
75 /// never started.
76 class Timer {
77  TimeRecord Time; ///< The total time captured.
78  TimeRecord StartTime; ///< The time startTimer() was last called.
79  std::string Name; ///< The name of this time variable.
80  std::string Description; ///< Description of this time variable.
81  bool Running; ///< Is the timer currently running?
82  bool Triggered; ///< Has the timer ever been triggered?
83  TimerGroup *TG = nullptr; ///< The TimerGroup this Timer is in.
84 
85  Timer **Prev; ///< Pointer to \p Next of previous timer in group.
86  Timer *Next; ///< Next timer in the group.
87 public:
88  explicit Timer(StringRef Name, StringRef Description) {
89  init(Name, Description);
90  }
91  Timer(StringRef Name, StringRef Description, TimerGroup &tg) {
92  init(Name, Description, tg);
93  }
94  Timer(const Timer &RHS) {
95  assert(!RHS.TG && "Can only copy uninitialized timers");
96  }
97  const Timer &operator=(const Timer &T) {
98  assert(!TG && !T.TG && "Can only assign uninit timers");
99  return *this;
100  }
101  ~Timer();
102 
103  /// Create an uninitialized timer, client must use 'init'.
104  explicit Timer() {}
105  void init(StringRef Name, StringRef Description);
106  void init(StringRef Name, StringRef Description, TimerGroup &tg);
107 
108  const std::string &getName() const { return Name; }
109  const std::string &getDescription() const { return Description; }
110  bool isInitialized() const { return TG != nullptr; }
111 
112  /// Check if the timer is currently running.
113  bool isRunning() const { return Running; }
114 
115  /// Check if startTimer() has ever been called on this timer.
116  bool hasTriggered() const { return Triggered; }
117 
118  /// Start the timer running. Time between calls to startTimer/stopTimer is
119  /// counted by the Timer class. Note that these calls must be correctly
120  /// paired.
121  void startTimer();
122 
123  /// Stop the timer.
124  void stopTimer();
125 
126  /// Clear the timer state.
127  void clear();
128 
129  /// Return the duration for which this timer has been running.
130  TimeRecord getTotalTime() const { return Time; }
131 
132 private:
133  friend class TimerGroup;
134 };
135 
136 /// The TimeRegion class is used as a helper class to call the startTimer() and
137 /// stopTimer() methods of the Timer class. When the object is constructed, it
138 /// starts the timer specified as its argument. When it is destroyed, it stops
139 /// the relevant timer. This makes it easy to time a region of code.
140 class TimeRegion {
141  Timer *T;
142  TimeRegion(const TimeRegion &) = delete;
143 
144 public:
145  explicit TimeRegion(Timer &t) : T(&t) {
146  T->startTimer();
147  }
148  explicit TimeRegion(Timer *t) : T(t) {
149  if (T) T->startTimer();
150  }
152  if (T) T->stopTimer();
153  }
154 };
155 
156 /// This class is basically a combination of TimeRegion and Timer. It allows
157 /// you to declare a new timer, AND specify the region to time, all in one
158 /// statement. All timers with the same name are merged. This is primarily
159 /// used for debugging and for hunting performance problems.
160 struct NamedRegionTimer : public TimeRegion {
161  explicit NamedRegionTimer(StringRef Name, StringRef Description,
162  StringRef GroupName,
163  StringRef GroupDescription, bool Enabled = true);
164 };
165 
166 /// The TimerGroup class is used to group together related timers into a single
167 /// report that is printed when the TimerGroup is destroyed. It is illegal to
168 /// destroy a TimerGroup object before all of the Timers in it are gone. A
169 /// TimerGroup can be specified for a newly created timer in its constructor.
170 class TimerGroup {
171  struct PrintRecord {
172  TimeRecord Time;
173  std::string Name;
174  std::string Description;
175 
176  PrintRecord(const PrintRecord &Other) = default;
177  PrintRecord(const TimeRecord &Time, const std::string &Name,
178  const std::string &Description)
179  : Time(Time), Name(Name), Description(Description) {}
180 
181  bool operator <(const PrintRecord &Other) const {
182  return Time < Other.Time;
183  }
184  };
185  std::string Name;
186  std::string Description;
187  Timer *FirstTimer = nullptr; ///< First timer in the group.
188  std::vector<PrintRecord> TimersToPrint;
189 
190  TimerGroup **Prev; ///< Pointer to Next field of previous timergroup in list.
191  TimerGroup *Next; ///< Pointer to next timergroup in list.
192  TimerGroup(const TimerGroup &TG) = delete;
193  void operator=(const TimerGroup &TG) = delete;
194 
195 public:
196  explicit TimerGroup(StringRef Name, StringRef Description);
197  ~TimerGroup();
198 
199  void setName(StringRef NewName, StringRef NewDescription) {
200  Name.assign(NewName.begin(), NewName.end());
201  Description.assign(NewDescription.begin(), NewDescription.end());
202  }
203 
204  /// Print any started timers in this group and zero them.
205  void print(raw_ostream &OS);
206 
207  /// This static method prints all timers and clears them all out.
208  static void printAll(raw_ostream &OS);
209 
210  /// Ensure global timer group lists are initialized. This function is mostly
211  /// used by the Statistic code to influence the construction and destruction
212  /// order of the global timer lists.
213  static void ConstructTimerLists();
214 private:
215  friend class Timer;
216  friend void PrintStatisticsJSON(raw_ostream &OS);
217  void addTimer(Timer &T);
218  void removeTimer(Timer &T);
219  void prepareToPrintList();
220  void PrintQueuedTimers(raw_ostream &OS);
221  void printJSONValue(raw_ostream &OS, const PrintRecord &R,
222  const char *suffix, double Value);
223  const char *printJSONValues(raw_ostream &OS, const char *delim);
224  static const char *printAllJSONValues(raw_ostream &OS, const char *delim);
225 };
226 
227 } // end namespace llvm
228 
229 #endif
bool isInitialized() const
Definition: Timer.h:110
void print(const TimeRecord &Total, raw_ostream &OS) const
Print the current time record to OS, with a breakdown showing contributions to the Total time record...
Definition: Timer.cpp:165
TimeRegion(Timer &t)
Definition: Timer.h:145
const std::string & getName() const
Definition: Timer.h:108
static TimeRecord getCurrentTime(bool Start=true)
Get the current time and memory usage.
Definition: Timer.cpp:120
void init(StringRef Name, StringRef Description)
Definition: Timer.cpp:97
void stopTimer()
Stop the timer.
Definition: Timer.cpp:146
Timer(const Timer &RHS)
Definition: Timer.h:94
bool isRunning() const
Check if the timer is currently running.
Definition: Timer.h:113
The TimeRegion class is used as a helper class to call the startTimer() and stopTimer() methods of th...
Definition: Timer.h:140
static F t[256]
This class is basically a combination of TimeRegion and Timer.
Definition: Timer.h:160
double getProcessTime() const
Definition: Timer.h:40
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:662
double getWallTime() const
Definition: Timer.h:43
This class is used to track the amount of time spent between invocations of its startTimer()/stopTime...
Definition: Timer.h:76
double getUserTime() const
Definition: Timer.h:41
friend void PrintStatisticsJSON(raw_ostream &OS)
Print statistics in JSON format.
iterator begin() const
Definition: StringRef.h:103
Timer()
Create an uninitialized timer, client must use 'init'.
Definition: Timer.h:104
ssize_t getMemUsed() const
Definition: Timer.h:44
void clear()
Clear the timer state.
Definition: Timer.cpp:153
Timer(StringRef Name, StringRef Description)
Definition: Timer.h:88
const std::string & getDescription() const
Definition: Timer.h:109
Timer(StringRef Name, StringRef Description, TimerGroup &tg)
Definition: Timer.h:91
void print(raw_ostream &OS)
Print any started timers in this group and zero them.
Definition: Timer.cpp:354
TimeRecord getTotalTime() const
Return the duration for which this timer has been running.
Definition: Timer.h:130
double getSystemTime() const
Definition: Timer.h:42
void startTimer()
Start the timer running.
Definition: Timer.cpp:140
static bool Enabled
Definition: Statistic.cpp:49
void setName(StringRef NewName, StringRef NewDescription)
Definition: Timer.h:199
bool hasTriggered() const
Check if startTimer() has ever been called on this timer.
Definition: Timer.h:116
void operator+=(const TimeRecord &RHS)
Definition: Timer.h:51
The TimerGroup class is used to group together related timers into a single report that is printed wh...
Definition: Timer.h:170
TimeRegion(Timer *t)
Definition: Timer.h:148
const Timer & operator=(const Timer &T)
Definition: Timer.h:97
NamedRegionTimer(StringRef Name, StringRef Description, StringRef GroupName, StringRef GroupDescription, bool Enabled=true)
Definition: Timer.cpp:218
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:326
static void ConstructTimerLists()
Ensure global timer group lists are initialized.
Definition: Timer.cpp:402
LLVM Value Representation.
Definition: Value.h:71
bool operator<(const TimeRecord &T) const
Definition: Timer.h:46
static void printAll(raw_ostream &OS)
This static method prints all timers and clears them all out.
Definition: Timer.cpp:364
void operator-=(const TimeRecord &RHS)
Definition: Timer.h:57
iterator end() const
Definition: StringRef.h:105
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47