3a2a457681f3e7eb0e85575ea001598faa8238dd
[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 // documented below.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef SUPPORT_TIMER_H
9 #define SUPPORT_TIMER_H
10
11 #include <string>
12 #include <vector>
13 #include <iosfwd>
14 #include <cassert>
15
16 class TimerGroup;
17
18 /// Timer - This class is used to track the amount of time spent between
19 /// invocations of it's startTimer()/stopTimer() methods.  Given appropriate OS
20 /// support it can also keep track of the RSS of the program at various points.
21 /// By default, the Timer will print the amount of time it has captured to
22 /// standard error when the laster timer is destroyed, otherwise it is printed
23 /// when it's TimerGroup is destroyed.  Timer's do not print their information
24 /// if they are never started.
25 ///
26 class Timer {
27   double Elapsed;        // Wall clock time elapsed in seconds
28   double UserTime;       // User time elapsed
29   double SystemTime;     // System time elapsed
30   long   MemUsed;        // Memory allocated (in bytes)
31   long   PeakMem;        // Peak memory used
32   long   PeakMemBase;    // Temporary for peak calculation...
33   std::string Name;      // The name of this time variable
34   bool Started;          // Has this time variable ever been started?
35   TimerGroup *TG;        // The TimerGroup this Timer is in.
36 public:
37   Timer(const std::string &N);
38   Timer(const std::string &N, TimerGroup &tg);
39   Timer(const Timer &T);
40   ~Timer();
41
42   double getProcessTime() const { return UserTime+SystemTime; }
43   double getWallTime() const { return Elapsed; }
44   long getMemUsed() const { return MemUsed; }
45   long getPeakMem() const { return PeakMem; }
46   std::string getName() const { return Name; }
47
48   const Timer &operator=(const Timer &T) {
49     Elapsed = T.Elapsed;
50     UserTime = T.UserTime;
51     SystemTime = T.SystemTime;
52     MemUsed = T.MemUsed;
53     PeakMem = T.PeakMem;
54     PeakMemBase = T.PeakMemBase;
55     Name = T.Name;
56     Started = T.Started;
57     assert(TG == T.TG && "Can only assign timers in the same TimerGroup!");
58     return *this;
59   }
60
61   // operator< - Allow sorting...
62   bool operator<(const Timer &T) const {
63     // Sort by Wall Time elapsed, as it is the only thing really accurate
64     return Elapsed < T.Elapsed;
65   }
66   bool operator>(const Timer &T) const { return T.operator<(*this); }
67   
68   /// startTimer - Start the timer running.  Time between calls to
69   /// startTimer/stopTimer is counted by the Timer class.  Note that these calls
70   /// must be correctly paired.
71   ///
72   void startTimer();
73
74   /// stopTimer - Stop the timer.
75   ///
76   void stopTimer();
77
78   /// addPeakMemoryMeasurement - This method should be called whenever memory
79   /// usage needs to be checked.  It adds a peak memory measurement to the
80   /// currently active timers, which will be printed when the timer group prints
81   ///
82   static void addPeakMemoryMeasurement();
83
84   /// print - Print the current timer to standard error, and reset the "Started"
85   /// flag.
86   void print(const Timer &Total, std::ostream &OS);
87
88 private:
89   friend class TimerGroup;
90
91   // Copy ctor, initialize with no TG member.
92   Timer(bool, const Timer &T);
93
94   /// sum - Add the time accumulated in the specified timer into this timer.
95   ///
96   void sum(const Timer &T);
97 };
98
99
100 /// The TimeRegion class is used as a helper class to call the startTimer() and
101 /// stopTimer() methods of the Timer class.  When the object is constructed, it
102 /// starts the timer specified as it's argument.  When it is destroyed, it stops
103 /// the relevant timer.  This makes it easy to time a region of code.
104 ///
105 class TimeRegion {
106   Timer &T;
107   TimeRegion(const TimeRegion &); // DO NOT IMPLEMENT
108 public:
109   TimeRegion(Timer &t) : T(t) {
110     T.startTimer();
111   }
112   ~TimeRegion() {
113     T.stopTimer();
114   }
115 };
116
117
118 /// NamedRegionTimer - This class is basically a combination of TimeRegion and
119 /// Timer.  It allows you to declare a new timer, AND specify the region to
120 /// time, all in one statement.  All timers with the same name are merged.  This
121 /// is primarily used for debugging and for hunting performance problems.
122 ///
123 struct NamedRegionTimer : public TimeRegion {
124   NamedRegionTimer(const std::string &Name);
125 };
126
127
128 /// The TimerGroup class is used to group together related timers into a single
129 /// report that is printed when the TimerGroup is destroyed.  It is illegal to
130 /// destroy a TimerGroup object before all of the Timers in it are gone.  A
131 /// TimerGroup can be specified for a newly created timer in its constructor.
132 ///
133 class TimerGroup {
134   std::string Name;
135   unsigned NumTimers;
136   std::vector<Timer> TimersToPrint;
137 public:
138   TimerGroup(const std::string &name) : Name(name), NumTimers(0) {}
139   ~TimerGroup() {
140     assert(NumTimers == 0 &&
141            "TimerGroup destroyed before all contained timers!");
142   }
143
144 private:
145   friend class Timer;
146   void addTimer() { ++NumTimers; }
147   void removeTimer();
148   void addTimerToPrint(const Timer &T) {
149     TimersToPrint.push_back(Timer(true, T));
150   }
151 };
152
153 #endif