LLVM API Documentation
00001 //===-- Timer.cpp - Interval Timing Support -------------------------------===// 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 // Interval Timing implementation. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Support/Timer.h" 00015 #include "llvm/Support/CommandLine.h" 00016 #include "llvm/Support/ManagedStatic.h" 00017 #include "llvm/Support/Streams.h" 00018 #include "llvm/System/Process.h" 00019 #include <algorithm> 00020 #include <fstream> 00021 #include <functional> 00022 #include <map> 00023 using namespace llvm; 00024 00025 // GetLibSupportInfoOutputFile - Return a file stream to print our output on. 00026 namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); } 00027 00028 // getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy 00029 // of constructor/destructor ordering being unspecified by C++. Basically the 00030 // problem is that a Statistic object gets destroyed, which ends up calling 00031 // 'GetLibSupportInfoOutputFile()' (below), which calls this function. 00032 // LibSupportInfoOutputFilename used to be a global variable, but sometimes it 00033 // would get destroyed before the Statistic, causing havoc to ensue. We "fix" 00034 // this by creating the string the first time it is needed and never destroying 00035 // it. 00036 static ManagedStatic<std::string> LibSupportInfoOutputFilename; 00037 static std::string &getLibSupportInfoOutputFilename() { 00038 return *LibSupportInfoOutputFilename; 00039 } 00040 00041 static ManagedStatic<sys::SmartMutex<true> > TimerLock; 00042 00043 namespace { 00044 static cl::opt<bool> 00045 TrackSpace("track-memory", cl::desc("Enable -time-passes memory " 00046 "tracking (this may be slow)"), 00047 cl::Hidden); 00048 00049 static cl::opt<std::string, true> 00050 InfoOutputFilename("info-output-file", cl::value_desc("filename"), 00051 cl::desc("File to append -stats and -timer output to"), 00052 cl::Hidden, cl::location(getLibSupportInfoOutputFilename())); 00053 } 00054 00055 static TimerGroup *DefaultTimerGroup = 0; 00056 static TimerGroup *getDefaultTimerGroup() { 00057 TimerGroup* tmp = DefaultTimerGroup; 00058 sys::MemoryFence(); 00059 if (!tmp) { 00060 llvm_acquire_global_lock(); 00061 tmp = DefaultTimerGroup; 00062 if (!tmp) { 00063 tmp = new TimerGroup("Miscellaneous Ungrouped Timers"); 00064 sys::MemoryFence(); 00065 DefaultTimerGroup = tmp; 00066 } 00067 llvm_release_global_lock(); 00068 } 00069 00070 return tmp; 00071 } 00072 00073 Timer::Timer(const std::string &N) 00074 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N), 00075 Started(false), TG(getDefaultTimerGroup()) { 00076 TG->addTimer(); 00077 } 00078 00079 Timer::Timer(const std::string &N, TimerGroup &tg) 00080 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N), 00081 Started(false), TG(&tg) { 00082 TG->addTimer(); 00083 } 00084 00085 Timer::Timer(const Timer &T) { 00086 TG = T.TG; 00087 if (TG) TG->addTimer(); 00088 operator=(T); 00089 } 00090 00091 00092 // Copy ctor, initialize with no TG member. 00093 Timer::Timer(bool, const Timer &T) { 00094 TG = T.TG; // Avoid assertion in operator= 00095 operator=(T); // Copy contents 00096 TG = 0; 00097 } 00098 00099 00100 Timer::~Timer() { 00101 if (TG) { 00102 if (Started) { 00103 Started = false; 00104 TG->addTimerToPrint(*this); 00105 } 00106 TG->removeTimer(); 00107 } 00108 } 00109 00110 static inline size_t getMemUsage() { 00111 if (TrackSpace) 00112 return sys::Process::GetMallocUsage(); 00113 return 0; 00114 } 00115 00116 struct TimeRecord { 00117 double Elapsed, UserTime, SystemTime; 00118 ssize_t MemUsed; 00119 }; 00120 00121 static TimeRecord getTimeRecord(bool Start) { 00122 TimeRecord Result; 00123 00124 sys::TimeValue now(0,0); 00125 sys::TimeValue user(0,0); 00126 sys::TimeValue sys(0,0); 00127 00128 ssize_t MemUsed = 0; 00129 if (Start) { 00130 MemUsed = getMemUsage(); 00131 sys::Process::GetTimeUsage(now,user,sys); 00132 } else { 00133 sys::Process::GetTimeUsage(now,user,sys); 00134 MemUsed = getMemUsage(); 00135 } 00136 00137 Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0; 00138 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0; 00139 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0; 00140 Result.MemUsed = MemUsed; 00141 00142 return Result; 00143 } 00144 00145 static ManagedStatic<std::vector<Timer*> > ActiveTimers; 00146 00147 void Timer::startTimer() { 00148 sys::SmartScopedLock<true> L(Lock); 00149 Started = true; 00150 ActiveTimers->push_back(this); 00151 TimeRecord TR = getTimeRecord(true); 00152 Elapsed -= TR.Elapsed; 00153 UserTime -= TR.UserTime; 00154 SystemTime -= TR.SystemTime; 00155 MemUsed -= TR.MemUsed; 00156 PeakMemBase = TR.MemUsed; 00157 } 00158 00159 void Timer::stopTimer() { 00160 sys::SmartScopedLock<true> L(Lock); 00161 TimeRecord TR = getTimeRecord(false); 00162 Elapsed += TR.Elapsed; 00163 UserTime += TR.UserTime; 00164 SystemTime += TR.SystemTime; 00165 MemUsed += TR.MemUsed; 00166 00167 if (ActiveTimers->back() == this) { 00168 ActiveTimers->pop_back(); 00169 } else { 00170 std::vector<Timer*>::iterator I = 00171 std::find(ActiveTimers->begin(), ActiveTimers->end(), this); 00172 assert(I != ActiveTimers->end() && "stop but no startTimer?"); 00173 ActiveTimers->erase(I); 00174 } 00175 } 00176 00177 void Timer::sum(const Timer &T) { 00178 if (&T < this) { 00179 T.Lock.acquire(); 00180 Lock.acquire(); 00181 } else { 00182 Lock.acquire(); 00183 T.Lock.acquire(); 00184 } 00185 00186 Elapsed += T.Elapsed; 00187 UserTime += T.UserTime; 00188 SystemTime += T.SystemTime; 00189 MemUsed += T.MemUsed; 00190 PeakMem += T.PeakMem; 00191 00192 if (&T < this) { 00193 T.Lock.release(); 00194 Lock.release(); 00195 } else { 00196 Lock.release(); 00197 T.Lock.release(); 00198 } 00199 } 00200 00201 /// addPeakMemoryMeasurement - This method should be called whenever memory 00202 /// usage needs to be checked. It adds a peak memory measurement to the 00203 /// currently active timers, which will be printed when the timer group prints 00204 /// 00205 void Timer::addPeakMemoryMeasurement() { 00206 size_t MemUsed = getMemUsage(); 00207 00208 for (std::vector<Timer*>::iterator I = ActiveTimers->begin(), 00209 E = ActiveTimers->end(); I != E; ++I) { 00210 (*I)->Lock.acquire(); 00211 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase); 00212 (*I)->Lock.release(); 00213 } 00214 } 00215 00216 //===----------------------------------------------------------------------===// 00217 // NamedRegionTimer Implementation 00218 //===----------------------------------------------------------------------===// 00219 00220 namespace { 00221 00222 typedef std::map<std::string, Timer> Name2Timer; 00223 typedef std::map<std::string, std::pair<TimerGroup, Name2Timer> > Name2Pair; 00224 00225 } 00226 00227 static ManagedStatic<Name2Timer> NamedTimers; 00228 00229 static ManagedStatic<Name2Pair> NamedGroupedTimers; 00230 00231 static Timer &getNamedRegionTimer(const std::string &Name) { 00232 sys::SmartScopedLock<true> L(*TimerLock); 00233 Name2Timer::iterator I = NamedTimers->find(Name); 00234 if (I != NamedTimers->end()) 00235 return I->second; 00236 00237 return NamedTimers->insert(I, std::make_pair(Name, Timer(Name)))->second; 00238 } 00239 00240 static Timer &getNamedRegionTimer(const std::string &Name, 00241 const std::string &GroupName) { 00242 sys::SmartScopedLock<true> L(*TimerLock); 00243 00244 Name2Pair::iterator I = NamedGroupedTimers->find(GroupName); 00245 if (I == NamedGroupedTimers->end()) { 00246 TimerGroup TG(GroupName); 00247 std::pair<TimerGroup, Name2Timer> Pair(TG, Name2Timer()); 00248 I = NamedGroupedTimers->insert(I, std::make_pair(GroupName, Pair)); 00249 } 00250 00251 Name2Timer::iterator J = I->second.second.find(Name); 00252 if (J == I->second.second.end()) 00253 J = I->second.second.insert(J, 00254 std::make_pair(Name, 00255 Timer(Name, 00256 I->second.first))); 00257 00258 return J->second; 00259 } 00260 00261 NamedRegionTimer::NamedRegionTimer(const std::string &Name) 00262 : TimeRegion(getNamedRegionTimer(Name)) {} 00263 00264 NamedRegionTimer::NamedRegionTimer(const std::string &Name, 00265 const std::string &GroupName) 00266 : TimeRegion(getNamedRegionTimer(Name, GroupName)) {} 00267 00268 //===----------------------------------------------------------------------===// 00269 // TimerGroup Implementation 00270 //===----------------------------------------------------------------------===// 00271 00272 // printAlignedFP - Simulate the printf "%A.Bf" format, where A is the 00273 // TotalWidth size, and B is the AfterDec size. 00274 // 00275 static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth, 00276 std::ostream &OS) { 00277 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!"); 00278 OS.width(TotalWidth-AfterDec-1); 00279 char OldFill = OS.fill(); 00280 OS.fill(' '); 00281 OS << (int)Val; // Integer part; 00282 OS << "."; 00283 OS.width(AfterDec); 00284 OS.fill('0'); 00285 unsigned ResultFieldSize = 1; 00286 while (AfterDec--) ResultFieldSize *= 10; 00287 OS << (int)(Val*ResultFieldSize) % ResultFieldSize; 00288 OS.fill(OldFill); 00289 } 00290 00291 static void printVal(double Val, double Total, std::ostream &OS) { 00292 if (Total < 1e-7) // Avoid dividing by zero... 00293 OS << " ----- "; 00294 else { 00295 OS << " "; 00296 printAlignedFP(Val, 4, 7, OS); 00297 OS << " ("; 00298 printAlignedFP(Val*100/Total, 1, 5, OS); 00299 OS << "%)"; 00300 } 00301 } 00302 00303 void Timer::print(const Timer &Total, std::ostream &OS) { 00304 if (&Total < this) { 00305 Total.Lock.acquire(); 00306 Lock.acquire(); 00307 } else { 00308 Lock.acquire(); 00309 Total.Lock.acquire(); 00310 } 00311 00312 if (Total.UserTime) 00313 printVal(UserTime, Total.UserTime, OS); 00314 if (Total.SystemTime) 00315 printVal(SystemTime, Total.SystemTime, OS); 00316 if (Total.getProcessTime()) 00317 printVal(getProcessTime(), Total.getProcessTime(), OS); 00318 printVal(Elapsed, Total.Elapsed, OS); 00319 00320 OS << " "; 00321 00322 if (Total.MemUsed) { 00323 OS.width(9); 00324 OS << MemUsed << " "; 00325 } 00326 if (Total.PeakMem) { 00327 if (PeakMem) { 00328 OS.width(9); 00329 OS << PeakMem << " "; 00330 } else 00331 OS << " "; 00332 } 00333 OS << Name << "\n"; 00334 00335 Started = false; // Once printed, don't print again 00336 00337 if (&Total < this) { 00338 Total.Lock.release(); 00339 Lock.release(); 00340 } else { 00341 Lock.release(); 00342 Total.Lock.release(); 00343 } 00344 } 00345 00346 // GetLibSupportInfoOutputFile - Return a file stream to print our output on... 00347 std::ostream * 00348 llvm::GetLibSupportInfoOutputFile() { 00349 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename(); 00350 if (LibSupportInfoOutputFilename.empty()) 00351 return cerr.stream(); 00352 if (LibSupportInfoOutputFilename == "-") 00353 return cout.stream(); 00354 00355 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(), 00356 std::ios::app); 00357 if (!Result->good()) { 00358 cerr << "Error opening info-output-file '" 00359 << LibSupportInfoOutputFilename << " for appending!\n"; 00360 delete Result; 00361 return cerr.stream(); 00362 } 00363 return Result; 00364 } 00365 00366 00367 void TimerGroup::removeTimer() { 00368 sys::SmartScopedLock<true> L(*TimerLock); 00369 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report... 00370 // Sort the timers in descending order by amount of time taken... 00371 std::sort(TimersToPrint.begin(), TimersToPrint.end(), 00372 std::greater<Timer>()); 00373 00374 // Figure out how many spaces to indent TimerGroup name... 00375 unsigned Padding = (80-Name.length())/2; 00376 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers 00377 00378 std::ostream *OutStream = GetLibSupportInfoOutputFile(); 00379 00380 ++NumTimers; 00381 { // Scope to contain Total timer... don't allow total timer to drop us to 00382 // zero timers... 00383 Timer Total("TOTAL"); 00384 00385 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) 00386 Total.sum(TimersToPrint[i]); 00387 00388 // Print out timing header... 00389 *OutStream << "===" << std::string(73, '-') << "===\n" 00390 << std::string(Padding, ' ') << Name << "\n" 00391 << "===" << std::string(73, '-') 00392 << "===\n"; 00393 00394 // If this is not an collection of ungrouped times, print the total time. 00395 // Ungrouped timers don't really make sense to add up. We still print the 00396 // TOTAL line to make the percentages make sense. 00397 if (this != DefaultTimerGroup) { 00398 *OutStream << " Total Execution Time: "; 00399 00400 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream); 00401 *OutStream << " seconds ("; 00402 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream); 00403 *OutStream << " wall clock)\n"; 00404 } 00405 *OutStream << "\n"; 00406 00407 if (Total.UserTime) 00408 *OutStream << " ---User Time---"; 00409 if (Total.SystemTime) 00410 *OutStream << " --System Time--"; 00411 if (Total.getProcessTime()) 00412 *OutStream << " --User+System--"; 00413 *OutStream << " ---Wall Time---"; 00414 if (Total.getMemUsed()) 00415 *OutStream << " ---Mem---"; 00416 if (Total.getPeakMem()) 00417 *OutStream << " -PeakMem-"; 00418 *OutStream << " --- Name ---\n"; 00419 00420 // Loop through all of the timing data, printing it out... 00421 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) 00422 TimersToPrint[i].print(Total, *OutStream); 00423 00424 Total.print(Total, *OutStream); 00425 *OutStream << std::endl; // Flush output 00426 } 00427 --NumTimers; 00428 00429 TimersToPrint.clear(); 00430 00431 if (OutStream != cerr.stream() && OutStream != cout.stream()) 00432 delete OutStream; // Close the file... 00433 } 00434 } 00435 00436 void TimerGroup::addTimer() { 00437 sys::SmartScopedLock<true> L(*TimerLock); 00438 ++NumTimers; 00439 } 00440 00441 void TimerGroup::addTimerToPrint(const Timer &T) { 00442 sys::SmartScopedLock<true> L(*TimerLock); 00443 TimersToPrint.push_back(Timer(true, T)); 00444 } 00445