a7418827ca3239cfe2564188c090b1bb3637c84e
[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/Support/Compiler.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/ADT/StringRef.h"
21 #include <cassert>
22 #include <string>
23 #include <vector>
24 #include <utility>
25
26 namespace llvm {
27
28 class Timer;
29 class TimerGroup;
30 class raw_ostream;
31
32 class TimeRecord {
33   double WallTime;       // Wall clock time elapsed in seconds
34   double UserTime;       // User time elapsed
35   double SystemTime;     // System time elapsed
36   ssize_t MemUsed;       // Memory allocated (in bytes)
37 public:
38   TimeRecord() : WallTime(0), UserTime(0), SystemTime(0), MemUsed(0) {}
39   
40   /// getCurrentTime - Get the current time and memory usage.  If Start is true
41   /// we get the memory usage before the time, otherwise we get time before
42   /// memory usage.  This matters if the time to get the memory usage is
43   /// significant and shouldn't be counted as part of a duration.
44   static TimeRecord getCurrentTime(bool Start = true);
45   
46   double getProcessTime() const { return UserTime+SystemTime; }
47   double getUserTime() const { return UserTime; }
48   double getSystemTime() const { return SystemTime; }
49   double getWallTime() const { return WallTime; }
50   ssize_t getMemUsed() const { return MemUsed; }
51   
52   
53   // operator< - Allow sorting.
54   bool operator<(const TimeRecord &T) const {
55     // Sort by Wall Time elapsed, as it is the only thing really accurate
56     return WallTime < T.WallTime;
57   }
58   
59   void operator+=(const TimeRecord &RHS) {
60     WallTime   += RHS.WallTime;
61     UserTime   += RHS.UserTime;
62     SystemTime += RHS.SystemTime;
63     MemUsed    += RHS.MemUsed;
64   }
65   void operator-=(const TimeRecord &RHS) {
66     WallTime   -= RHS.WallTime;
67     UserTime   -= RHS.UserTime;
68     SystemTime -= RHS.SystemTime;
69     MemUsed    -= RHS.MemUsed;
70   }
71   
72   /// print - Print the current timer to standard error, and reset the "Started"
73   /// flag.
74   void print(const TimeRecord &Total, raw_ostream &OS) const;
75 };
76   
77 /// Timer - This class is used to track the amount of time spent between
78 /// invocations of its startTimer()/stopTimer() methods.  Given appropriate OS
79 /// support it can also keep track of the RSS of the program at various points.
80 /// By default, the Timer will print the amount of time it has captured to
81 /// standard error when the laster timer is destroyed, otherwise it is printed
82 /// when its TimerGroup is destroyed.  Timers do not print their information
83 /// if they are never started.
84 ///
85 class Timer {
86   TimeRecord Time;
87   std::string Name;      // The name of this time variable.
88   bool Started;          // Has this time variable ever been started?
89   TimerGroup *TG;        // The TimerGroup this Timer is in.
90   
91   Timer **Prev, *Next;   // Doubly linked list of timers in the group.
92 public:
93   explicit Timer(StringRef N) : TG(0) { init(N); }
94   Timer(StringRef N, TimerGroup &tg) : TG(0) { init(N, tg); }
95   Timer(const Timer &RHS) : TG(0) {
96     assert(RHS.TG == 0 && "Can only copy uninitialized timers");
97   }
98   const Timer &operator=(const Timer &T) {
99     assert(TG == 0 && T.TG == 0 && "Can only assign uninit timers");
100     return *this;
101   }
102   ~Timer();
103
104   // Create an uninitialized timer, client must use 'init'.
105   explicit Timer() : TG(0) {}
106   void init(StringRef N);
107   void init(StringRef N, TimerGroup &tg);
108   
109   const std::string &getName() const { return Name; }
110   bool isInitialized() const { return TG != 0; }
111   
112   /// startTimer - Start the timer running.  Time between calls to
113   /// startTimer/stopTimer is counted by the Timer class.  Note that these calls
114   /// must be correctly paired.
115   ///
116   void startTimer();
117
118   /// stopTimer - Stop the timer.
119   ///
120   void stopTimer();
121
122 private:
123   friend class TimerGroup;
124 };
125
126
127 /// The TimeRegion class is used as a helper class to call the startTimer() and
128 /// stopTimer() methods of the Timer class.  When the object is constructed, it
129 /// starts the timer specified as it's argument.  When it is destroyed, it stops
130 /// the relevant timer.  This makes it easy to time a region of code.
131 ///
132 class TimeRegion {
133   Timer *T;
134   TimeRegion(const TimeRegion &) LLVM_DELETED_FUNCTION;
135 public:
136   explicit TimeRegion(Timer &t) : T(&t) {
137     T->startTimer();
138   }
139   explicit TimeRegion(Timer *t) : T(t) {
140     if (T) T->startTimer();
141   }
142   ~TimeRegion() {
143     if (T) T->stopTimer();
144   }
145 };
146
147
148 /// NamedRegionTimer - This class is basically a combination of TimeRegion and
149 /// Timer.  It allows you to declare a new timer, AND specify the region to
150 /// time, all in one statement.  All timers with the same name are merged.  This
151 /// is primarily used for debugging and for hunting performance problems.
152 ///
153 struct NamedRegionTimer : public TimeRegion {
154   explicit NamedRegionTimer(StringRef Name,
155                             bool Enabled = true);
156   explicit NamedRegionTimer(StringRef Name, StringRef GroupName,
157                             bool Enabled = true);
158 };
159
160
161 /// The TimerGroup class is used to group together related timers into a single
162 /// report that is printed when the TimerGroup is destroyed.  It is illegal to
163 /// destroy a TimerGroup object before all of the Timers in it are gone.  A
164 /// TimerGroup can be specified for a newly created timer in its constructor.
165 ///
166 class TimerGroup {
167   std::string Name;
168   Timer *FirstTimer;   // First timer in the group.
169   std::vector<std::pair<TimeRecord, std::string> > TimersToPrint;
170   
171   TimerGroup **Prev, *Next; // Doubly linked list of TimerGroup's.
172   TimerGroup(const TimerGroup &TG) LLVM_DELETED_FUNCTION;
173   void operator=(const TimerGroup &TG) LLVM_DELETED_FUNCTION;
174 public:
175   explicit TimerGroup(StringRef name);
176   ~TimerGroup();
177
178   void setName(StringRef name) { Name.assign(name.begin(), name.end()); }
179
180   /// print - Print any started timers in this group and zero them.
181   void print(raw_ostream &OS);
182   
183   /// printAll - This static method prints all timers and clears them all out.
184   static void printAll(raw_ostream &OS);
185   
186 private:
187   friend class Timer;
188   void addTimer(Timer &T);
189   void removeTimer(Timer &T);
190   void PrintQueuedTimers(raw_ostream &OS);
191 };
192
193 } // End llvm namespace
194
195 #endif