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