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 // This file defines three classes: Timer, TimeRegion, and TimerGroup,
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_TIMER_H
16 #define LLVM_SUPPORT_TIMER_H
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/ADT/StringRef.h"
33 double WallTime; // Wall clock time elapsed in seconds
34 double UserTime; // User time elapsed
35 double SystemTime; // System time elapsed
36 ssize_t MemUsed; // Memory allocated (in bytes)
38 TimeRecord() : WallTime(0), UserTime(0), SystemTime(0), MemUsed(0) {}
40 /// getCurrentTime - Get the current time and memory usage. If Start is true
41 /// we get the memory usage before the time, otherwise we get time before
42 /// memory usage. This matters if the time to get the memory usage is
43 /// significant and shouldn't be counted as part of a duration.
44 static TimeRecord getCurrentTime(bool Start = true);
46 double getProcessTime() const { return UserTime+SystemTime; }
47 double getUserTime() const { return UserTime; }
48 double getSystemTime() const { return SystemTime; }
49 double getWallTime() const { return WallTime; }
50 ssize_t getMemUsed() const { return MemUsed; }
53 // operator< - Allow sorting.
54 bool operator<(const TimeRecord &T) const {
55 // Sort by Wall Time elapsed, as it is the only thing really accurate
56 return WallTime < T.WallTime;
59 void operator+=(const TimeRecord &RHS) {
60 WallTime += RHS.WallTime;
61 UserTime += RHS.UserTime;
62 SystemTime += RHS.SystemTime;
63 MemUsed += RHS.MemUsed;
65 void operator-=(const TimeRecord &RHS) {
66 WallTime -= RHS.WallTime;
67 UserTime -= RHS.UserTime;
68 SystemTime -= RHS.SystemTime;
69 MemUsed -= RHS.MemUsed;
72 /// print - Print the current timer to standard error, and reset the "Started"
74 void print(const TimeRecord &Total, raw_ostream &OS) const;
77 /// Timer - This class is used to track the amount of time spent between
78 /// invocations of its startTimer()/stopTimer() methods. Given appropriate OS
79 /// support it can also keep track of the RSS of the program at various points.
80 /// By default, the Timer will print the amount of time it has captured to
81 /// standard error when the laster timer is destroyed, otherwise it is printed
82 /// when its TimerGroup is destroyed. Timers do not print their information
83 /// if they are never started.
87 std::string Name; // The name of this time variable.
88 bool Started; // Has this time variable ever been started?
89 TimerGroup *TG; // The TimerGroup this Timer is in.
91 Timer **Prev, *Next; // Doubly linked list of timers in the group.
93 explicit Timer(StringRef N) : TG(0) { init(N); }
94 Timer(StringRef N, TimerGroup &tg) : TG(0) { init(N, tg); }
95 Timer(const Timer &RHS) : TG(0) {
96 assert(RHS.TG == 0 && "Can only copy uninitialized timers");
98 const Timer &operator=(const Timer &T) {
99 assert(TG == 0 && T.TG == 0 && "Can only assign uninit timers");
104 // Create an uninitialized timer, client must use 'init'.
105 explicit Timer() : TG(0) {}
106 void init(StringRef N);
107 void init(StringRef N, TimerGroup &tg);
109 const std::string &getName() const { return Name; }
110 bool isInitialized() const { return TG != 0; }
112 /// startTimer - Start the timer running. Time between calls to
113 /// startTimer/stopTimer is counted by the Timer class. Note that these calls
114 /// must be correctly paired.
118 /// stopTimer - Stop the timer.
123 friend class TimerGroup;
127 /// The TimeRegion class is used as a helper class to call the startTimer() and
128 /// stopTimer() methods of the Timer class. When the object is constructed, it
129 /// starts the timer specified as it's argument. When it is destroyed, it stops
130 /// the relevant timer. This makes it easy to time a region of code.
134 TimeRegion(const TimeRegion &) LLVM_DELETED_FUNCTION;
136 explicit TimeRegion(Timer &t) : T(&t) {
139 explicit TimeRegion(Timer *t) : T(t) {
140 if (T) T->startTimer();
143 if (T) T->stopTimer();
148 /// NamedRegionTimer - This class is basically a combination of TimeRegion and
149 /// Timer. It allows you to declare a new timer, AND specify the region to
150 /// time, all in one statement. All timers with the same name are merged. This
151 /// is primarily used for debugging and for hunting performance problems.
153 struct NamedRegionTimer : public TimeRegion {
154 explicit NamedRegionTimer(StringRef Name,
155 bool Enabled = true);
156 explicit NamedRegionTimer(StringRef Name, StringRef GroupName,
157 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) LLVM_DELETED_FUNCTION;
173 void operator=(const TimerGroup &TG) LLVM_DELETED_FUNCTION;
175 explicit TimerGroup(StringRef name);
178 void setName(StringRef name) { Name.assign(name.begin(), name.end()); }
180 /// print - Print any started timers in this group and zero them.
181 void print(raw_ostream &OS);
183 /// printAll - This static method prints all timers and clears them all out.
184 static void printAll(raw_ostream &OS);
188 void addTimer(Timer &T);
189 void removeTimer(Timer &T);
190 void PrintQueuedTimers(raw_ostream &OS);
193 } // End llvm namespace