1 //===-- Timer.cpp - Interval Timing Support -------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file was developed by the LLVM research group and is distributed under 6 // the University of Illinois Open Source License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Interval Timing implementation. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Support/Timer.h" 15 #include "llvm/Support/CommandLine.h" 16 #include "llvm/System/Process.h" 17 #include <algorithm> 18 #include <fstream> 19 #include <functional> 20 #include <iostream> 21 #include <map> 22 23 using namespace llvm; 24 25 // GetLibSupportInfoOutputFile - Return a file stream to print our output on. 26 namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); } 27 28 // getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy 29 // of constructor/destructor ordering being unspecified by C++. Basically the 30 // problem is that a Statistic<> object gets destroyed, which ends up calling 31 // 'GetLibSupportInfoOutputFile()' (below), which calls this function. 32 // LibSupportInfoOutputFilename used to be a global variable, but sometimes it 33 // would get destroyed before the Statistic, causing havoc to ensue. We "fix" 34 // this by creating the string the first time it is needed and never destroying 35 // it. 36 static std::string &getLibSupportInfoOutputFilename() { 37 static std::string *LibSupportInfoOutputFilename = new std::string(); 38 return *LibSupportInfoOutputFilename; 39 } 40 41 namespace { 42 cl::opt<bool> 43 TrackSpace("track-memory", cl::desc("Enable -time-passes memory " 44 "tracking (this may be slow)"), 45 cl::Hidden); 46 47 cl::opt<std::string, true> 48 InfoOutputFilename("info-output-file", cl::value_desc("filename"), 49 cl::desc("File to append -stats and -timer output to"), 50 cl::Hidden, cl::location(getLibSupportInfoOutputFilename())); 51 } 52 53 static TimerGroup *DefaultTimerGroup = 0; 54 static TimerGroup *getDefaultTimerGroup() { 55 if (DefaultTimerGroup) return DefaultTimerGroup; 56 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers"); 57 } 58 59 Timer::Timer(const std::string &N) 60 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N), 61 Started(false), TG(getDefaultTimerGroup()) { 62 TG->addTimer(); 63 } 64 65 Timer::Timer(const std::string &N, TimerGroup &tg) 66 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N), 67 Started(false), TG(&tg) { 68 TG->addTimer(); 69 } 70 71 Timer::Timer(const Timer &T) { 72 TG = T.TG; 73 if (TG) TG->addTimer(); 74 operator=(T); 75 } 76 77 78 // Copy ctor, initialize with no TG member. 79 Timer::Timer(bool, const Timer &T) { 80 TG = T.TG; // Avoid assertion in operator= 81 operator=(T); // Copy contents 82 TG = 0; 83 } 84 85 86 Timer::~Timer() { 87 if (TG) { 88 if (Started) { 89 Started = false; 90 TG->addTimerToPrint(*this); 91 } 92 TG->removeTimer(); 93 } 94 } 95 96 struct TimeRecord { 97 double Elapsed, UserTime, SystemTime; 98 long MemUsed; 99 }; 100 101 static TimeRecord getTimeRecord(bool Start) { 102 TimeRecord Result; 103 104 sys::TimeValue now(0,0); 105 sys::TimeValue user(0,0); 106 sys::TimeValue sys(0,0); 107 108 sys::Process::GetTimeUsage(now,user,sys); 109 110 Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0; 111 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0; 112 Result.UserTime = sys.seconds() + sys.microseconds() / 1000000.0; 113 Result.MemUsed = sys::Process::GetMallocUsage(); 114 115 return Result; 116 } 117 118 static std::vector<Timer*> ActiveTimers; 119 120 void Timer::startTimer() { 121 Started = true; 122 TimeRecord TR = getTimeRecord(true); 123 Elapsed -= TR.Elapsed; 124 UserTime -= TR.UserTime; 125 SystemTime -= TR.SystemTime; 126 MemUsed -= TR.MemUsed; 127 PeakMemBase = TR.MemUsed; 128 ActiveTimers.push_back(this); 129 } 130 131 void Timer::stopTimer() { 132 TimeRecord TR = getTimeRecord(false); 133 Elapsed += TR.Elapsed; 134 UserTime += TR.UserTime; 135 SystemTime += TR.SystemTime; 136 MemUsed += TR.MemUsed; 137 138 if (ActiveTimers.back() == this) { 139 ActiveTimers.pop_back(); 140 } else { 141 std::vector<Timer*>::iterator I = 142 std::find(ActiveTimers.begin(), ActiveTimers.end(), this); 143 assert(I != ActiveTimers.end() && "stop but no startTimer?"); 144 ActiveTimers.erase(I); 145 } 146 } 147 148 void Timer::sum(const Timer &T) { 149 Elapsed += T.Elapsed; 150 UserTime += T.UserTime; 151 SystemTime += T.SystemTime; 152 MemUsed += T.MemUsed; 153 PeakMem += T.PeakMem; 154 } 155 156 /// addPeakMemoryMeasurement - This method should be called whenever memory 157 /// usage needs to be checked. It adds a peak memory measurement to the 158 /// currently active timers, which will be printed when the timer group prints 159 /// 160 void Timer::addPeakMemoryMeasurement() { 161 long MemUsed = sys::Process::GetMallocUsage(); 162 163 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(), 164 E = ActiveTimers.end(); I != E; ++I) 165 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase); 166 } 167 168 //===----------------------------------------------------------------------===// 169 // NamedRegionTimer Implementation 170 //===----------------------------------------------------------------------===// 171 172 static Timer &getNamedRegionTimer(const std::string &Name) { 173 static std::map<std::string, Timer> NamedTimers; 174 175 std::map<std::string, Timer>::iterator I = NamedTimers.lower_bound(Name); 176 if (I != NamedTimers.end() && I->first == Name) 177 return I->second; 178 179 return NamedTimers.insert(I, std::make_pair(Name, Timer(Name)))->second; 180 } 181 182 NamedRegionTimer::NamedRegionTimer(const std::string &Name) 183 : TimeRegion(getNamedRegionTimer(Name)) {} 184 185 186 //===----------------------------------------------------------------------===// 187 // TimerGroup Implementation 188 //===----------------------------------------------------------------------===// 189 190 // printAlignedFP - Simulate the printf "%A.Bf" format, where A is the 191 // TotalWidth size, and B is the AfterDec size. 192 // 193 static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth, 194 std::ostream &OS) { 195 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!"); 196 OS.width(TotalWidth-AfterDec-1); 197 char OldFill = OS.fill(); 198 OS.fill(' '); 199 OS << (int)Val; // Integer part; 200 OS << "."; 201 OS.width(AfterDec); 202 OS.fill('0'); 203 unsigned ResultFieldSize = 1; 204 while (AfterDec--) ResultFieldSize *= 10; 205 OS << (int)(Val*ResultFieldSize) % ResultFieldSize; 206 OS.fill(OldFill); 207 } 208 209 static void printVal(double Val, double Total, std::ostream &OS) { 210 if (Total < 1e-7) // Avoid dividing by zero... 211 OS << " ----- "; 212 else { 213 OS << " "; 214 printAlignedFP(Val, 4, 7, OS); 215 OS << " ("; 216 printAlignedFP(Val*100/Total, 1, 5, OS); 217 OS << "%)"; 218 } 219 } 220 221 void Timer::print(const Timer &Total, std::ostream &OS) { 222 if (Total.UserTime) 223 printVal(UserTime, Total.UserTime, OS); 224 if (Total.SystemTime) 225 printVal(SystemTime, Total.SystemTime, OS); 226 if (Total.getProcessTime()) 227 printVal(getProcessTime(), Total.getProcessTime(), OS); 228 printVal(Elapsed, Total.Elapsed, OS); 229 230 OS << " "; 231 232 if (Total.MemUsed) { 233 OS.width(9); 234 OS << MemUsed << " "; 235 } 236 if (Total.PeakMem) { 237 if (PeakMem) { 238 OS.width(9); 239 OS << PeakMem << " "; 240 } else 241 OS << " "; 242 } 243 OS << Name << "\n"; 244 245 Started = false; // Once printed, don't print again 246 } 247 248 // GetLibSupportInfoOutputFile - Return a file stream to print our output on... 249 std::ostream * 250 llvm::GetLibSupportInfoOutputFile() { 251 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename(); 252 if (LibSupportInfoOutputFilename.empty()) 253 return &std::cerr; 254 if (LibSupportInfoOutputFilename == "-") 255 return &std::cout; 256 257 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(), 258 std::ios::app); 259 if (!Result->good()) { 260 std::cerr << "Error opening info-output-file '" 261 << LibSupportInfoOutputFilename << " for appending!\n"; 262 delete Result; 263 return &std::cerr; 264 } 265 return Result; 266 } 267 268 269 void TimerGroup::removeTimer() { 270 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report... 271 // Sort the timers in descending order by amount of time taken... 272 std::sort(TimersToPrint.begin(), TimersToPrint.end(), 273 std::greater<Timer>()); 274 275 // Figure out how many spaces to indent TimerGroup name... 276 unsigned Padding = (80-Name.length())/2; 277 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers 278 279 std::ostream *OutStream = GetLibSupportInfoOutputFile(); 280 281 ++NumTimers; 282 { // Scope to contain Total timer... don't allow total timer to drop us to 283 // zero timers... 284 Timer Total("TOTAL"); 285 286 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) 287 Total.sum(TimersToPrint[i]); 288 289 // Print out timing header... 290 *OutStream << "===" << std::string(73, '-') << "===\n" 291 << std::string(Padding, ' ') << Name << "\n" 292 << "===" << std::string(73, '-') 293 << "===\n Total Execution Time: "; 294 295 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream); 296 *OutStream << " seconds ("; 297 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream); 298 *OutStream << " wall clock)\n\n"; 299 300 if (Total.UserTime) 301 *OutStream << " ---User Time---"; 302 if (Total.SystemTime) 303 *OutStream << " --System Time--"; 304 if (Total.getProcessTime()) 305 *OutStream << " --User+System--"; 306 *OutStream << " ---Wall Time---"; 307 if (Total.getMemUsed()) 308 *OutStream << " ---Mem---"; 309 if (Total.getPeakMem()) 310 *OutStream << " -PeakMem-"; 311 *OutStream << " --- Name ---\n"; 312 313 // Loop through all of the timing data, printing it out... 314 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) 315 TimersToPrint[i].print(Total, *OutStream); 316 317 Total.print(Total, *OutStream); 318 *OutStream << std::endl; // Flush output 319 } 320 --NumTimers; 321 322 TimersToPrint.clear(); 323 324 if (OutStream != &std::cerr && OutStream != &std::cout) 325 delete OutStream; // Close the file... 326 } 327 328 // Delete default timer group! 329 if (NumTimers == 0 && this == DefaultTimerGroup) { 330 delete DefaultTimerGroup; 331 DefaultTimerGroup = 0; 332 } 333 } 334 335