1 //===-- Support/Timer.h - Interval Timing Support ---------------*- C++ -*-===//
3 // This file defines three classes: Timer, TimeRegion, and TimerGroup,
6 //===----------------------------------------------------------------------===//
8 #ifndef SUPPORT_TIMER_H
9 #define SUPPORT_TIMER_H
18 /// Timer - This class is used to track the amount of time spent between
19 /// invocations of it's startTimer()/stopTimer() methods. Given appropriate OS
20 /// support it can also keep track of the RSS of the program at various points.
21 /// By default, the Timer will print the amount of time it has captured to
22 /// standard error when the laster timer is destroyed, otherwise it is printed
23 /// when it's TimerGroup is destroyed. Timer's do not print their information
24 /// if they are never started.
27 double Elapsed; // Wall clock time elapsed in seconds
28 double UserTime; // User time elapsed
29 double SystemTime; // System time elapsed
30 long MemUsed; // Memory allocated (in bytes)
31 long PeakMem; // Peak memory used
32 long PeakMemBase; // Temporary for peak calculation...
33 std::string Name; // The name of this time variable
34 bool Started; // Has this time variable ever been started?
35 TimerGroup *TG; // The TimerGroup this Timer is in.
37 Timer(const std::string &N);
38 Timer(const std::string &N, TimerGroup &tg);
39 Timer(const Timer &T);
42 double getProcessTime() const { return UserTime+SystemTime; }
43 double getWallTime() const { return Elapsed; }
44 long getMemUsed() const { return MemUsed; }
45 long getPeakMem() const { return PeakMem; }
46 std::string getName() const { return Name; }
48 const Timer &operator=(const Timer &T) {
50 UserTime = T.UserTime;
51 SystemTime = T.SystemTime;
54 PeakMemBase = T.PeakMemBase;
57 assert(TG == T.TG && "Can only assign timers in the same TimerGroup!");
61 // operator< - Allow sorting...
62 bool operator<(const Timer &T) const {
63 // Sort by Wall Time elapsed, as it is the only thing really accurate
64 return Elapsed < T.Elapsed;
66 bool operator>(const Timer &T) const { return T.operator<(*this); }
68 /// startTimer - Start the timer running. Time between calls to
69 /// startTimer/stopTimer is counted by the Timer class. Note that these calls
70 /// must be correctly paired.
74 /// stopTimer - Stop the timer.
78 /// addPeakMemoryMeasurement - This method should be called whenever memory
79 /// usage needs to be checked. It adds a peak memory measurement to the
80 /// currently active timers, which will be printed when the timer group prints
82 static void addPeakMemoryMeasurement();
84 /// print - Print the current timer to standard error, and reset the "Started"
86 void print(const Timer &Total, std::ostream &OS);
89 friend class TimerGroup;
91 // Copy ctor, initialize with no TG member.
92 Timer(bool, const Timer &T);
94 /// sum - Add the time accumulated in the specified timer into this timer.
96 void sum(const Timer &T);
100 /// The TimeRegion class is used as a helper class to call the startTimer() and
101 /// stopTimer() methods of the Timer class. When the object is constructed, it
102 /// starts the timer specified as it's argument. When it is destroyed, it stops
103 /// the relevant timer. This makes it easy to time a region of code.
107 TimeRegion(const TimeRegion &); // DO NOT IMPLEMENT
109 TimeRegion(Timer &t) : T(t) {
118 /// NamedRegionTimer - This class is basically a combination of TimeRegion and
119 /// Timer. It allows you to declare a new timer, AND specify the region to
120 /// time, all in one statement. All timers with the same name are merged. This
121 /// is primarily used for debugging and for hunting performance problems.
123 struct NamedRegionTimer : public TimeRegion {
124 NamedRegionTimer(const std::string &Name);
128 /// The TimerGroup class is used to group together related timers into a single
129 /// report that is printed when the TimerGroup is destroyed. It is illegal to
130 /// destroy a TimerGroup object before all of the Timers in it are gone. A
131 /// TimerGroup can be specified for a newly created timer in its constructor.
136 std::vector<Timer> TimersToPrint;
138 TimerGroup(const std::string &name) : Name(name), NumTimers(0) {}
140 assert(NumTimers == 0 &&
141 "TimerGroup destroyed before all contained timers!");
146 void addTimer() { ++NumTimers; }
148 void addTimerToPrint(const Timer &T) {
149 TimersToPrint.push_back(Timer(true, T));