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