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/System/DataTypes.h"
19 #include "llvm/ADT/StringRef.h"
32 double WallTime; // Wall clock time elapsed in seconds
33 double UserTime; // User time elapsed
34 double SystemTime; // System time elapsed
35 ssize_t MemUsed; // Memory allocated (in bytes)
37 TimeRecord() : WallTime(0), UserTime(0), SystemTime(0), MemUsed(0) {}
39 /// getCurrentTime - Get the current time and memory usage. If Start is true
40 /// we get the memory usage before the time, otherwise we get time before
41 /// memory usage. This matters if the time to get the memory usage is
42 /// significant and shouldn't be counted as part of a duration.
43 static TimeRecord getCurrentTime(bool Start = true);
45 double getProcessTime() const { return UserTime+SystemTime; }
46 double getUserTime() const { return UserTime; }
47 double getSystemTime() const { return SystemTime; }
48 double getWallTime() const { return WallTime; }
49 ssize_t getMemUsed() const { return MemUsed; }
52 // operator< - Allow sorting.
53 bool operator<(const TimeRecord &T) const {
54 // Sort by Wall Time elapsed, as it is the only thing really accurate
55 return WallTime < T.WallTime;
58 void operator+=(const TimeRecord &RHS) {
59 WallTime += RHS.WallTime;
60 UserTime += RHS.UserTime;
61 SystemTime += RHS.SystemTime;
62 MemUsed += RHS.MemUsed;
64 void operator-=(const TimeRecord &RHS) {
65 WallTime -= RHS.WallTime;
66 UserTime -= RHS.UserTime;
67 SystemTime -= RHS.SystemTime;
68 MemUsed -= RHS.MemUsed;
71 /// print - Print the current timer to standard error, and reset the "Started"
73 void print(const TimeRecord &Total, raw_ostream &OS) const;
76 /// Timer - This class is used to track the amount of time spent between
77 /// invocations of its startTimer()/stopTimer() methods. Given appropriate OS
78 /// support it can also keep track of the RSS of the program at various points.
79 /// By default, the Timer will print the amount of time it has captured to
80 /// standard error when the laster timer is destroyed, otherwise it is printed
81 /// when its TimerGroup is destroyed. Timers do not print their information
82 /// if they are never started.
86 std::string Name; // The name of this time variable.
87 bool Started; // Has this time variable ever been started?
88 TimerGroup *TG; // The TimerGroup this Timer is in.
90 Timer **Prev, *Next; // Doubly linked list of timers in the group.
92 explicit Timer(StringRef N) : TG(0) { init(N); }
93 Timer(StringRef N, TimerGroup &tg) : TG(0) { init(N, tg); }
94 Timer(const Timer &RHS) : TG(0) {
95 assert(RHS.TG == 0 && "Can only copy uninitialized timers");
97 const Timer &operator=(const Timer &T) {
98 assert(TG == 0 && T.TG == 0 && "Can only assign uninit timers");
103 // Create an uninitialized timer, client must use 'init'.
104 explicit Timer() : TG(0) {}
105 void init(StringRef N);
106 void init(StringRef N, TimerGroup &tg);
108 const std::string &getName() const { return Name; }
109 bool isInitialized() const { return TG != 0; }
111 /// startTimer - Start the timer running. Time between calls to
112 /// startTimer/stopTimer is counted by the Timer class. Note that these calls
113 /// must be correctly paired.
117 /// stopTimer - Stop the timer.
122 friend class TimerGroup;
126 /// The TimeRegion class is used as a helper class to call the startTimer() and
127 /// stopTimer() methods of the Timer class. When the object is constructed, it
128 /// starts the timer specified as it's argument. When it is destroyed, it stops
129 /// the relevant timer. This makes it easy to time a region of code.
133 TimeRegion(const TimeRegion &); // DO NOT IMPLEMENT
135 explicit TimeRegion(Timer &t) : T(&t) {
138 explicit TimeRegion(Timer *t) : T(t) {
139 if (T) T->startTimer();
142 if (T) T->stopTimer();
147 /// NamedRegionTimer - This class is basically a combination of TimeRegion and
148 /// Timer. It allows you to declare a new timer, AND specify the region to
149 /// time, all in one statement. All timers with the same name are merged. This
150 /// is primarily used for debugging and for hunting performance problems.
152 struct NamedRegionTimer : public TimeRegion {
153 explicit NamedRegionTimer(StringRef Name,
154 bool Enabled = true);
155 explicit NamedRegionTimer(StringRef Name, StringRef GroupName,
156 bool Enabled = true);
160 /// The TimerGroup class is used to group together related timers into a single
161 /// report that is printed when the TimerGroup is destroyed. It is illegal to
162 /// destroy a TimerGroup object before all of the Timers in it are gone. A
163 /// TimerGroup can be specified for a newly created timer in its constructor.
167 Timer *FirstTimer; // First timer in the group.
168 std::vector<std::pair<TimeRecord, std::string> > TimersToPrint;
170 TimerGroup **Prev, *Next; // Doubly linked list of TimerGroup's.
171 TimerGroup(const TimerGroup &TG); // DO NOT IMPLEMENT
172 void operator=(const TimerGroup &TG); // DO NOT IMPLEMENT
174 explicit TimerGroup(StringRef name);
177 void setName(StringRef name) { Name.assign(name.begin(), name.end()); }
179 /// print - Print any started timers in this group and zero them.
180 void print(raw_ostream &OS);
182 /// printAll - This static method prints all timers and clears them all out.
183 static void printAll(raw_ostream &OS);
187 void addTimer(Timer &T);
188 void removeTimer(Timer &T);
189 void PrintQueuedTimers(raw_ostream &OS);
192 } // End llvm namespace