LLVM  3.7.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  /// getCurrentTime - Get the current time and memory usage. If Start is true
35  /// we get the memory usage before the time, otherwise we get time before
36  /// memory usage. This matters if the time to get the memory usage is
37  /// significant and shouldn't 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 
47  // operator< - Allow sorting.
48  bool operator<(const TimeRecord &T) const {
49  // Sort by Wall Time elapsed, as it is the only thing really accurate
50  return WallTime < T.WallTime;
51  }
52 
53  void operator+=(const TimeRecord &RHS) {
54  WallTime += RHS.WallTime;
55  UserTime += RHS.UserTime;
56  SystemTime += RHS.SystemTime;
57  MemUsed += RHS.MemUsed;
58  }
59  void operator-=(const TimeRecord &RHS) {
60  WallTime -= RHS.WallTime;
61  UserTime -= RHS.UserTime;
62  SystemTime -= RHS.SystemTime;
63  MemUsed -= RHS.MemUsed;
64  }
65 
66  /// print - Print the current timer to standard error, and reset the "Started"
67  /// flag.
68  void print(const TimeRecord &Total, raw_ostream &OS) const;
69 };
70 
71 /// Timer - This class is used to track the amount of time spent between
72 /// invocations of its startTimer()/stopTimer() methods. Given appropriate OS
73 /// support it can also keep track of the RSS of the program at various points.
74 /// By default, the Timer will print the amount of time it has captured to
75 /// standard error when the last timer is destroyed, otherwise it is printed
76 /// when its TimerGroup is destroyed. Timers do not print their information
77 /// if they are never started.
78 ///
79 class Timer {
80  TimeRecord Time;
81  std::string Name; // The name of this time variable.
82  bool Started; // Has this time variable ever been started?
83  TimerGroup *TG; // The TimerGroup this Timer is in.
84 
85  Timer **Prev, *Next; // Doubly linked list of timers in the group.
86 public:
87  explicit Timer(StringRef N) : TG(nullptr) { init(N); }
88  Timer(StringRef N, TimerGroup &tg) : TG(nullptr) { init(N, tg); }
89  Timer(const Timer &RHS) : TG(nullptr) {
90  assert(!RHS.TG && "Can only copy uninitialized timers");
91  }
92  const Timer &operator=(const Timer &T) {
93  assert(!TG && !T.TG && "Can only assign uninit timers");
94  return *this;
95  }
96  ~Timer();
97 
98  // Create an uninitialized timer, client must use 'init'.
99  explicit Timer() : TG(nullptr) {}
100  void init(StringRef N);
101  void init(StringRef N, TimerGroup &tg);
102 
103  const std::string &getName() const { return Name; }
104  bool isInitialized() const { return TG != nullptr; }
105 
106  /// startTimer - Start the timer running. Time between calls to
107  /// startTimer/stopTimer is counted by the Timer class. Note that these calls
108  /// must be correctly paired.
109  ///
110  void startTimer();
111 
112  /// stopTimer - Stop the timer.
113  ///
114  void stopTimer();
115 
116 private:
117  friend class TimerGroup;
118 };
119 
120 
121 /// The TimeRegion class is used as a helper class to call the startTimer() and
122 /// stopTimer() methods of the Timer class. When the object is constructed, it
123 /// starts the timer specified as its argument. When it is destroyed, it stops
124 /// the relevant timer. This makes it easy to time a region of code.
125 ///
126 class TimeRegion {
127  Timer *T;
128  TimeRegion(const TimeRegion &) = delete;
129 public:
130  explicit TimeRegion(Timer &t) : T(&t) {
131  T->startTimer();
132  }
133  explicit TimeRegion(Timer *t) : T(t) {
134  if (T) T->startTimer();
135  }
137  if (T) T->stopTimer();
138  }
139 };
140 
141 
142 /// NamedRegionTimer - This class is basically a combination of TimeRegion and
143 /// Timer. It allows you to declare a new timer, AND specify the region to
144 /// time, all in one statement. All timers with the same name are merged. This
145 /// is primarily used for debugging and for hunting performance problems.
146 ///
147 struct NamedRegionTimer : public TimeRegion {
149  bool Enabled = true);
150  explicit NamedRegionTimer(StringRef Name, StringRef GroupName,
151  bool Enabled = true);
152 };
153 
154 
155 /// The TimerGroup class is used to group together related timers into a single
156 /// report that is printed when the TimerGroup is destroyed. It is illegal to
157 /// destroy a TimerGroup object before all of the Timers in it are gone. A
158 /// TimerGroup can be specified for a newly created timer in its constructor.
159 ///
160 class TimerGroup {
161  std::string Name;
162  Timer *FirstTimer; // First timer in the group.
163  std::vector<std::pair<TimeRecord, std::string> > TimersToPrint;
164 
165  TimerGroup **Prev, *Next; // Doubly linked list of TimerGroup's.
166  TimerGroup(const TimerGroup &TG) = delete;
167  void operator=(const TimerGroup &TG) = delete;
168 public:
169  explicit TimerGroup(StringRef name);
170  ~TimerGroup();
171 
172  void setName(StringRef name) { Name.assign(name.begin(), name.end()); }
173 
174  /// print - Print any started timers in this group and zero them.
175  void print(raw_ostream &OS);
176 
177  /// printAll - This static method prints all timers and clears them all out.
178  static void printAll(raw_ostream &OS);
179 
180 private:
181  friend class Timer;
182  void addTimer(Timer &T);
183  void removeTimer(Timer &T);
184  void PrintQueuedTimers(raw_ostream &OS);
185 };
186 
187 } // End llvm namespace
188 
189 #endif
bool isInitialized() const
Definition: Timer.h:104
void print(const TimeRecord &Total, raw_ostream &OS) const
print - Print the current timer to standard error, and reset the "Started" flag.
Definition: Timer.cpp:173
TimeRegion(Timer &t)
Definition: Timer.h:130
const std::string & getName() const
Definition: Timer.h:103
static TimeRecord getCurrentTime(bool Start=true)
getCurrentTime - Get the current time and memory usage.
Definition: Timer.cpp:127
void stopTimer()
stopTimer - Stop the timer.
Definition: Timer.cpp:153
Timer(const Timer &RHS)
Definition: Timer.h:89
NamedRegionTimer(StringRef Name, bool Enabled=true)
Definition: Timer.cpp:235
The TimeRegion class is used as a helper class to call the startTimer() and stopTimer() methods of th...
Definition: Timer.h:126
NamedRegionTimer - This class is basically a combination of TimeRegion and Timer. ...
Definition: Timer.h:147
double getProcessTime() const
Definition: Timer.h:40
static cl::opt< bool > Enabled("stats", cl::desc("Enable statistics output from program (available with Asserts)"))
-stats - Command line option to cause transformations to emit stats about what they did...
double getWallTime() const
Definition: Timer.h:43
void setName(StringRef name)
Definition: Timer.h:172
Timer - This class is used to track the amount of time spent between invocations of its startTimer()/...
Definition: Timer.h:79
Timer(StringRef N, TimerGroup &tg)
Definition: Timer.h:88
double getUserTime() const
Definition: Timer.h:41
void init(StringRef N)
Definition: Timer.cpp:101
iterator begin() const
Definition: StringRef.h:90
Timer()
Definition: Timer.h:99
ssize_t getMemUsed() const
Definition: Timer.h:44
void print(raw_ostream &OS)
print - Print any started timers in this group and zero them.
Definition: Timer.cpp:362
double getSystemTime() const
Definition: Timer.h:42
void startTimer()
startTimer - Start the timer running.
Definition: Timer.cpp:147
void operator+=(const TimeRecord &RHS)
Definition: Timer.h:53
#define N
The TimerGroup class is used to group together related timers into a single report that is printed wh...
Definition: Timer.h:160
TimeRegion(Timer *t)
Definition: Timer.h:133
const Timer & operator=(const Timer &T)
Definition: Timer.h:92
static const char * name
Timer(StringRef N)
Definition: Timer.h:87
bool operator<(const TimeRecord &T) const
Definition: Timer.h:48
static void printAll(raw_ostream &OS)
printAll - This static method prints all timers and clears them all out.
Definition: Timer.cpp:382
void operator-=(const TimeRecord &RHS)
Definition: Timer.h:59
iterator end() const
Definition: StringRef.h:92
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40