1 //===-- llvm/Support/Timer.h - Interval Timing Support ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_SUPPORT_TIMER_H
11 #define LLVM_SUPPORT_TIMER_H
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/Support/DataTypes.h"
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)
32 TimeRecord() : WallTime(0), UserTime(0), SystemTime(0), MemUsed(0) {}
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);
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; }
46 // operator< - Allow sorting.
47 bool operator<(const TimeRecord &T) const {
48 // Sort by Wall Time elapsed, as it is the only thing really accurate
49 return WallTime < T.WallTime;
52 void operator+=(const TimeRecord &RHS) {
53 WallTime += RHS.WallTime;
54 UserTime += RHS.UserTime;
55 SystemTime += RHS.SystemTime;
56 MemUsed += RHS.MemUsed;
58 void operator-=(const TimeRecord &RHS) {
59 WallTime -= RHS.WallTime;
60 UserTime -= RHS.UserTime;
61 SystemTime -= RHS.SystemTime;
62 MemUsed -= RHS.MemUsed;
65 /// Print the current time record to \p OS, with a breakdown showing
66 /// contributions to the \p Total time record.
67 void print(const TimeRecord &Total, raw_ostream &OS) const;
70 /// Timer - This class is used to track the amount of time spent between
71 /// invocations of its startTimer()/stopTimer() methods. Given appropriate OS
72 /// support it can also keep track of the RSS of the program at various points.
73 /// By default, the Timer will print the amount of time it has captured to
74 /// standard error when the last timer is destroyed, otherwise it is printed
75 /// when its TimerGroup is destroyed. Timers do not print their information
76 /// if they are never started.
79 TimeRecord Time; // The total time captured
80 TimeRecord StartTime; // The time startTimer() was last called
81 std::string Name; // The name of this time variable.
82 bool Running; // Is the timer currently running?
83 bool Triggered; // Has the timer ever been triggered?
84 TimerGroup *TG; // The TimerGroup this Timer is in.
86 Timer **Prev, *Next; // Doubly linked list of timers in the group.
88 explicit Timer(StringRef N) : TG(nullptr) { init(N); }
89 Timer(StringRef N, TimerGroup &tg) : TG(nullptr) { init(N, tg); }
90 Timer(const Timer &RHS) : TG(nullptr) {
91 assert(!RHS.TG && "Can only copy uninitialized timers");
93 const Timer &operator=(const Timer &T) {
94 assert(!TG && !T.TG && "Can only assign uninit timers");
99 // Create an uninitialized timer, client must use 'init'.
100 explicit Timer() : TG(nullptr) {}
101 void init(StringRef N);
102 void init(StringRef N, TimerGroup &tg);
104 const std::string &getName() const { return Name; }
105 bool isInitialized() const { return TG != nullptr; }
107 /// Check if startTimer() has ever been called on this timer.
108 bool hasTriggered() const { return Triggered; }
110 /// Start the timer running. Time between calls to startTimer/stopTimer is
111 /// counted by the Timer class. Note that these calls must be correctly
118 /// Clear the timer state.
121 /// Return the duration for which this timer has been running.
122 TimeRecord getTotalTime() const { return Time; }
125 friend class TimerGroup;
128 /// The TimeRegion class is used as a helper class to call the startTimer() and
129 /// stopTimer() methods of the Timer class. When the object is constructed, it
130 /// starts the timer specified as its argument. When it is destroyed, it stops
131 /// the relevant timer. This makes it easy to time a region of code.
135 TimeRegion(const TimeRegion &) = delete;
138 explicit TimeRegion(Timer &t) : T(&t) {
141 explicit TimeRegion(Timer *t) : T(t) {
142 if (T) T->startTimer();
145 if (T) T->stopTimer();
149 /// NamedRegionTimer - This class is basically a combination of TimeRegion and
150 /// Timer. It allows you to declare a new timer, AND specify the region to
151 /// time, all in one statement. All timers with the same name are merged. This
152 /// is primarily used for debugging and for hunting performance problems.
154 struct NamedRegionTimer : public TimeRegion {
155 explicit NamedRegionTimer(StringRef Name,
156 bool Enabled = true);
157 explicit NamedRegionTimer(StringRef Name, StringRef GroupName,
158 bool Enabled = true);
161 /// The TimerGroup class is used to group together related timers into a single
162 /// report that is printed when the TimerGroup is destroyed. It is illegal to
163 /// destroy a TimerGroup object before all of the Timers in it are gone. A
164 /// TimerGroup can be specified for a newly created timer in its constructor.
168 Timer *FirstTimer; // First timer in the group.
169 std::vector<std::pair<TimeRecord, std::string>> TimersToPrint;
171 TimerGroup **Prev, *Next; // Doubly linked list of TimerGroup's.
172 TimerGroup(const TimerGroup &TG) = delete;
173 void operator=(const TimerGroup &TG) = delete;
176 explicit TimerGroup(StringRef name);
179 void setName(StringRef name) { Name.assign(name.begin(), name.end()); }
181 /// print - Print any started timers in this group and zero them.
182 void print(raw_ostream &OS);
184 /// printAll - This static method prints all timers and clears them all out.
185 static void printAll(raw_ostream &OS);
189 void addTimer(Timer &T);
190 void removeTimer(Timer &T);
191 void PrintQueuedTimers(raw_ostream &OS);
194 } // End llvm namespace