LLVM API Documentation
00001 //===-- llvm/Support/Timer.h - Interval Timing Support ----------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines three classes: Timer, TimeRegion, and TimerGroup, 00011 // documented below. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_SUPPORT_TIMER_H 00016 #define LLVM_SUPPORT_TIMER_H 00017 00018 #include "llvm/Support/DataTypes.h" 00019 #include "llvm/System/Mutex.h" 00020 #include <string> 00021 #include <vector> 00022 #include <iosfwd> 00023 #include <cassert> 00024 00025 namespace llvm { 00026 00027 class TimerGroup; 00028 00029 /// Timer - This class is used to track the amount of time spent between 00030 /// invocations of it's startTimer()/stopTimer() methods. Given appropriate OS 00031 /// support it can also keep track of the RSS of the program at various points. 00032 /// By default, the Timer will print the amount of time it has captured to 00033 /// standard error when the laster timer is destroyed, otherwise it is printed 00034 /// when its TimerGroup is destroyed. Timers do not print their information 00035 /// if they are never started. 00036 /// 00037 class Timer { 00038 double Elapsed; // Wall clock time elapsed in seconds 00039 double UserTime; // User time elapsed 00040 double SystemTime; // System time elapsed 00041 ssize_t MemUsed; // Memory allocated (in bytes) 00042 size_t PeakMem; // Peak memory used 00043 size_t PeakMemBase; // Temporary for peak calculation... 00044 std::string Name; // The name of this time variable 00045 bool Started; // Has this time variable ever been started? 00046 TimerGroup *TG; // The TimerGroup this Timer is in. 00047 mutable sys::SmartMutex<true> Lock; // Mutex for the contents of this Timer. 00048 public: 00049 explicit Timer(const std::string &N); 00050 Timer(const std::string &N, TimerGroup &tg); 00051 Timer(const Timer &T); 00052 ~Timer(); 00053 00054 double getProcessTime() const { return UserTime+SystemTime; } 00055 double getWallTime() const { return Elapsed; } 00056 ssize_t getMemUsed() const { return MemUsed; } 00057 size_t getPeakMem() const { return PeakMem; } 00058 std::string getName() const { return Name; } 00059 00060 const Timer &operator=(const Timer &T) { 00061 if (&T < this) { 00062 T.Lock.acquire(); 00063 Lock.acquire(); 00064 } else { 00065 Lock.acquire(); 00066 T.Lock.acquire(); 00067 } 00068 00069 Elapsed = T.Elapsed; 00070 UserTime = T.UserTime; 00071 SystemTime = T.SystemTime; 00072 MemUsed = T.MemUsed; 00073 PeakMem = T.PeakMem; 00074 PeakMemBase = T.PeakMemBase; 00075 Name = T.Name; 00076 Started = T.Started; 00077 assert(TG == T.TG && "Can only assign timers in the same TimerGroup!"); 00078 00079 if (&T < this) { 00080 T.Lock.release(); 00081 Lock.release(); 00082 } else { 00083 Lock.release(); 00084 T.Lock.release(); 00085 } 00086 00087 return *this; 00088 } 00089 00090 // operator< - Allow sorting... 00091 bool operator<(const Timer &T) const { 00092 // Sort by Wall Time elapsed, as it is the only thing really accurate 00093 return Elapsed < T.Elapsed; 00094 } 00095 bool operator>(const Timer &T) const { return T.operator<(*this); } 00096 00097 /// startTimer - Start the timer running. Time between calls to 00098 /// startTimer/stopTimer is counted by the Timer class. Note that these calls 00099 /// must be correctly paired. 00100 /// 00101 void startTimer(); 00102 00103 /// stopTimer - Stop the timer. 00104 /// 00105 void stopTimer(); 00106 00107 /// addPeakMemoryMeasurement - This method should be called whenever memory 00108 /// usage needs to be checked. It adds a peak memory measurement to the 00109 /// currently active timers, which will be printed when the timer group prints 00110 /// 00111 static void addPeakMemoryMeasurement(); 00112 00113 /// print - Print the current timer to standard error, and reset the "Started" 00114 /// flag. 00115 void print(const Timer &Total, std::ostream &OS); 00116 00117 private: 00118 friend class TimerGroup; 00119 00120 // Copy ctor, initialize with no TG member. 00121 Timer(bool, const Timer &T); 00122 00123 /// sum - Add the time accumulated in the specified timer into this timer. 00124 /// 00125 void sum(const Timer &T); 00126 }; 00127 00128 00129 /// The TimeRegion class is used as a helper class to call the startTimer() and 00130 /// stopTimer() methods of the Timer class. When the object is constructed, it 00131 /// starts the timer specified as it's argument. When it is destroyed, it stops 00132 /// the relevant timer. This makes it easy to time a region of code. 00133 /// 00134 class TimeRegion { 00135 Timer *T; 00136 TimeRegion(const TimeRegion &); // DO NOT IMPLEMENT 00137 public: 00138 explicit TimeRegion(Timer &t) : T(&t) { 00139 T->startTimer(); 00140 } 00141 explicit TimeRegion(Timer *t) : T(t) { 00142 if (T) 00143 T->startTimer(); 00144 } 00145 ~TimeRegion() { 00146 if (T) 00147 T->stopTimer(); 00148 } 00149 }; 00150 00151 00152 /// NamedRegionTimer - This class is basically a combination of TimeRegion and 00153 /// Timer. It allows you to declare a new timer, AND specify the region to 00154 /// time, all in one statement. All timers with the same name are merged. This 00155 /// is primarily used for debugging and for hunting performance problems. 00156 /// 00157 struct NamedRegionTimer : public TimeRegion { 00158 explicit NamedRegionTimer(const std::string &Name); 00159 explicit NamedRegionTimer(const std::string &Name, 00160 const std::string &GroupName); 00161 }; 00162 00163 00164 /// The TimerGroup class is used to group together related timers into a single 00165 /// report that is printed when the TimerGroup is destroyed. It is illegal to 00166 /// destroy a TimerGroup object before all of the Timers in it are gone. A 00167 /// TimerGroup can be specified for a newly created timer in its constructor. 00168 /// 00169 class TimerGroup { 00170 std::string Name; 00171 unsigned NumTimers; 00172 std::vector<Timer> TimersToPrint; 00173 public: 00174 explicit TimerGroup(const std::string &name) : Name(name), NumTimers(0) {} 00175 ~TimerGroup() { 00176 assert(NumTimers == 0 && 00177 "TimerGroup destroyed before all contained timers!"); 00178 } 00179 00180 private: 00181 friend class Timer; 00182 void addTimer(); 00183 void removeTimer(); 00184 void addTimerToPrint(const Timer &T); 00185 }; 00186 00187 } // End llvm namespace 00188 00189 #endif