Allow memory sizes to be negative, eliminate TmpRSS
[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
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   std::string Name;      // The name of this time variable
39   bool Started;          // Has this time variable ever been started?
40   TimerGroup *TG;        // The TimerGroup this Timer is in.
41 public:
42   Timer(const std::string &N);
43   Timer(const std::string &N, TimerGroup &tg);
44   Timer(const Timer &T);
45   ~Timer();
46
47   double getProcessTime() const { return UserTime+SystemTime; }
48   double getWallTime() const { return Elapsed; }
49   long getMemUsed() const { return MemUsed; }
50   std::string   getName() const { return Name; }
51
52   const Timer &operator=(const Timer &T) {
53     Elapsed = T.Elapsed;
54     UserTime = T.UserTime;
55     SystemTime = T.SystemTime;
56     MemUsed = T.MemUsed;
57     Name = T.Name;
58     Started = T.Started;
59     assert (TG == T.TG && "Can only assign timers in the same TimerGroup!");
60     return *this;
61   }
62
63   // operator< - Allow sorting...
64   bool operator<(const Timer &T) const {
65     // Sort by Wall Time elapsed, as it is the only thing really accurate
66     return Elapsed < T.Elapsed;
67   }
68   bool operator>(const Timer &T) const { return T.operator<(*this); }
69   
70   /// startTimer - Start the timer running.  Time between calls to
71   /// startTimer/stopTimer is counted by the Timer class.  Note that these calls
72   /// must be correctly paired.
73   ///
74   void startTimer();
75
76   /// stopTimer - Stop the timer.
77   ///
78   void stopTimer();
79
80   /// print - Print the current timer to standard error, and reset the "Started"
81   /// flag.
82   void print(const Timer &Total);
83
84 private:
85   friend class TimerGroup;
86
87   // Copy ctor, initialize with no TG member.
88   Timer(bool, const Timer &T);
89
90   /// sum - Add the time accumulated in the specified timer into this timer.
91   ///
92   void sum(const Timer &T);
93 };
94
95
96 class TimeRegion {
97   Timer &T;
98   TimeRegion(const TimeRegion &); // DO NOT IMPLEMENT
99 public:
100   TimeRegion(Timer &t) : T(t) {
101     T.startTimer();
102   }
103   ~TimeRegion() {
104     T.stopTimer();
105   }
106 };
107
108 class TimerGroup {
109   std::string Name;
110   unsigned NumTimers;
111   std::vector<Timer> TimersToPrint;
112 public:
113   TimerGroup(const std::string &name) : Name(name), NumTimers(0) {}
114   ~TimerGroup() {
115     assert(NumTimers == 0 &&
116            "TimerGroup destroyed before all contained timers!");
117   }
118
119 private:
120   friend class Timer;
121   void addTimer() { ++NumTimers; }
122   void removeTimer();
123   void addTimerToPrint(const Timer &T) {
124     TimersToPrint.push_back(Timer(true, T));
125   }
126 };
127
128 #endif