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