Updates to match recent timer updates
[oota-llvm.git] / include / Support / Timer.h
1 //===-- 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 #include <iosfwd>
31
32 class TimerGroup;
33
34 class Timer {
35   double Elapsed;        // Wall clock time elapsed in seconds
36   double UserTime;       // User time elapsed
37   double SystemTime;     // System time elapsed
38   long   MemUsed;        // Memory allocated (in bytes)
39   long   PeakMem;        // Peak memory used
40   long   PeakMemBase;    // Temporary for peak calculation...
41   std::string Name;      // The name of this time variable
42   bool Started;          // Has this time variable ever been started?
43   TimerGroup *TG;        // The TimerGroup this Timer is in.
44 public:
45   Timer(const std::string &N);
46   Timer(const std::string &N, TimerGroup &tg);
47   Timer(const Timer &T);
48   ~Timer();
49
50   double getProcessTime() const { return UserTime+SystemTime; }
51   double getWallTime() const { return Elapsed; }
52   long getMemUsed() const { return MemUsed; }
53   long getPeakMem() const { return PeakMem; }
54   std::string   getName() const { return Name; }
55
56   const Timer &operator=(const Timer &T) {
57     Elapsed = T.Elapsed;
58     UserTime = T.UserTime;
59     SystemTime = T.SystemTime;
60     MemUsed = T.MemUsed;
61     PeakMem = T.PeakMem;
62     PeakMemBase = T.PeakMemBase;
63     Name = T.Name;
64     Started = T.Started;
65     assert (TG == T.TG && "Can only assign timers in the same TimerGroup!");
66     return *this;
67   }
68
69   // operator< - Allow sorting...
70   bool operator<(const Timer &T) const {
71     // Sort by Wall Time elapsed, as it is the only thing really accurate
72     return Elapsed < T.Elapsed;
73   }
74   bool operator>(const Timer &T) const { return T.operator<(*this); }
75   
76   /// startTimer - Start the timer running.  Time between calls to
77   /// startTimer/stopTimer is counted by the Timer class.  Note that these calls
78   /// must be correctly paired.
79   ///
80   void startTimer();
81
82   /// stopTimer - Stop the timer.
83   ///
84   void stopTimer();
85
86   /// addPeakMemoryMeasurement - This method should be called whenever memory
87   /// usage needs to be checked.  It adds a peak memory measurement to the
88   /// currently active timers, which will be printed when the timer group prints
89   ///
90   static void addPeakMemoryMeasurement();
91
92   /// print - Print the current timer to standard error, and reset the "Started"
93   /// flag.
94   void print(const Timer &Total, std::ostream &OS);
95
96 private:
97   friend class TimerGroup;
98
99   // Copy ctor, initialize with no TG member.
100   Timer(bool, const Timer &T);
101
102   /// sum - Add the time accumulated in the specified timer into this timer.
103   ///
104   void sum(const Timer &T);
105 };
106
107
108 class TimeRegion {
109   Timer &T;
110   TimeRegion(const TimeRegion &); // DO NOT IMPLEMENT
111 public:
112   TimeRegion(Timer &t) : T(t) {
113     T.startTimer();
114   }
115   ~TimeRegion() {
116     T.stopTimer();
117   }
118 };
119
120 class TimerGroup {
121   std::string Name;
122   unsigned NumTimers;
123   std::vector<Timer> TimersToPrint;
124 public:
125   TimerGroup(const std::string &name) : Name(name), NumTimers(0) {}
126   ~TimerGroup() {
127     assert(NumTimers == 0 &&
128            "TimerGroup destroyed before all contained timers!");
129   }
130
131 private:
132   friend class Timer;
133   void addTimer() { ++NumTimers; }
134   void removeTimer();
135   void addTimerToPrint(const Timer &T) {
136     TimersToPrint.push_back(Timer(true, T));
137   }
138 };
139
140 #endif