blob: 5d39fc0f12eee33f53370d0d7e928a8fe669ba35 [file] [log] [blame]
Chris Lattner6dad11f2002-10-01 19:36:54 +00001//===-- Support/Timer.h - Interval Timing Support ---------------*- C++ -*-===//
2//
3// This file defines three classes: Timer, TimeRegion, and TimerGroup.
4//
5// The Timer class is used to track the amount of time spent between invocations
6// of it's startTimer()/stopTimer() methods. Given appropriate OS support it
7// can also keep track of the RSS of the program at various points. By default,
8// the Timer will print the amount of time it has captured to standard error
9// when the laster timer is destroyed, otherwise it is printed when it's
10// TimerGroup is destroyed. Timer's do not print their information if they are
11// never started.
12//
13// The TimeRegion class is used as a helper class to call the startTimer() and
14// stopTimer() methods of the Timer class. When the object is constructed, it
15// starts the timer specified as it's argument. When it is destroyed, it stops
16// the relevant timer. This makes it easy to time a region of code.
17//
18// The TimerGroup class is used to group together related timers into a single
19// report that is printed when the TimerGroup is destroyed. It is illegal to
20// destroy a TimerGroup object before all of the Timers in it are gone. A
21// TimerGroup can be specified for a newly created timer in its constructor.
22//
23//===----------------------------------------------------------------------===//
24
25#ifndef SUPPORT_TIMER_H
26#define SUPPORT_TIMER_H
27
28#include <string>
29#include <vector>
30
31class TimerGroup;
32
33class Timer {
34 double Elapsed; // Wall clock time elapsed in seconds
35 double UserTime; // User time elapsed
36 double SystemTime; // System time elapsed
37 unsigned long MaxRSS; // Maximum resident set size (in bytes)
38 unsigned long RSSTemp; // Temp for calculating maxrss
39 std::string Name; // The name of this time variable
40 bool Started; // Has this time variable ever been started?
41 TimerGroup *TG; // The TimerGroup this Timer is in.
42public:
43 Timer(const std::string &N);
44 Timer(const std::string &N, TimerGroup &tg);
45 Timer(const Timer &T);
46 ~Timer();
47
48 double getProcessTime() const { return UserTime+SystemTime; }
49 double getWallTime() const { return Elapsed; }
50 unsigned long getMaxRSS() const { return MaxRSS; }
51 std::string getName() const { return Name; }
52
53 const Timer &operator=(const Timer &T) {
54 Elapsed = T.Elapsed;
55 UserTime = T.UserTime;
56 SystemTime = T.SystemTime;
57 MaxRSS = T.MaxRSS;
58 RSSTemp = T.RSSTemp;
59 Name = T.Name;
60 Started = T.Started;
61 assert (TG == T.TG && "Can only assign timers in the same TimerGroup!");
62 return *this;
63 }
64
65 // operator< - Allow sorting...
66 bool operator<(const Timer &T) const {
Chris Lattner9a389b32002-10-03 21:08:20 +000067 // Sort by Wall Time elapsed, as it is the only thing really accurate
Chris Lattner6dad11f2002-10-01 19:36:54 +000068 return Elapsed < T.Elapsed;
69 }
70 bool operator>(const Timer &T) const { return T.operator<(*this); }
71
72 /// startTimer - Start the timer running. Time between calls to
73 /// startTimer/stopTimer is counted by the Timer class. Note that these calls
74 /// must be correctly paired.
75 ///
76 void startTimer();
77
78 /// stopTimer - Stop the timer.
79 ///
80 void stopTimer();
81
82 /// print - Print the current timer to standard error, and reset the "Started"
83 /// flag.
84 void print(const Timer &Total);
85
86private:
87 friend class TimerGroup;
88
89 // Copy ctor, initialize with no TG member.
90 Timer(bool, const Timer &T);
91
92 /// sum - Add the time accumulated in the specified timer into this timer.
93 ///
94 void sum(const Timer &T);
95};
96
97
98class TimeRegion {
99 Timer &T;
100 TimeRegion(const TimeRegion &); // DO NOT IMPLEMENT
101public:
102 TimeRegion(Timer &t) : T(t) {
103 T.startTimer();
104 }
105 ~TimeRegion() {
106 T.stopTimer();
107 }
108};
109
110class TimerGroup {
111 std::string Name;
112 unsigned NumTimers;
113 std::vector<Timer> TimersToPrint;
114public:
115 TimerGroup(const std::string &name) : Name(name), NumTimers(0) {}
116 ~TimerGroup() {
117 assert(NumTimers == 0 &&
118 "TimerGroup destroyed before all contained timers!");
119 }
120
121private:
122 friend class Timer;
123 void addTimer() { ++NumTimers; }
124 void removeTimer();
125 void addTimerToPrint(const Timer &T) {
126 TimersToPrint.push_back(Timer(true, T));
127 }
128};
129
130#endif