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