blob: 3c8879bd06e35da6aad9377c189034c4d7ee79d3 [file] [log] [blame]
Chris Lattner6c38a792002-10-01 19:36:54 +00001//===-- Timer.cpp - Interval Timing Support -------------------------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner6c38a792002-10-01 19:36:54 +00009//
10// Interval Timing implementation.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer551ccae2004-09-01 22:55:40 +000014#include "llvm/Support/Timer.h"
15#include "llvm/Support/CommandLine.h"
Chris Lattner90aa8392006-10-04 21:52:35 +000016#include "llvm/Support/ManagedStatic.h"
Bill Wendlingbcd24982006-12-07 20:28:15 +000017#include "llvm/Support/Streams.h"
Reid Spencerdf52c9a2004-12-20 00:59:04 +000018#include "llvm/System/Process.h"
Reid Spencer0255abb2004-12-20 03:59:23 +000019#include <algorithm>
Chris Lattnerb4db5f32004-06-07 19:34:51 +000020#include <fstream>
Reid Spencer0255abb2004-12-20 03:59:23 +000021#include <functional>
Chris Lattnerb4db5f32004-06-07 19:34:51 +000022#include <map>
Chris Lattnerb6d465f2003-12-14 21:27:33 +000023using namespace llvm;
Chris Lattnerf205fec2003-05-09 20:05:44 +000024
Chris Lattnerb4db5f32004-06-07 19:34:51 +000025// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattnerb6d465f2003-12-14 21:27:33 +000026namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattner71336a92003-07-31 19:38:34 +000028// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
29// of constructor/destructor ordering being unspecified by C++. Basically the
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000030// problem is that a Statistic object gets destroyed, which ends up calling
Chris Lattner71336a92003-07-31 19:38:34 +000031// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
32// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
Reid Spencerf6e5a252004-12-14 03:55:21 +000033// 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.
Chris Lattner90aa8392006-10-04 21:52:35 +000036static ManagedStatic<std::string> LibSupportInfoOutputFilename;
Chris Lattner71336a92003-07-31 19:38:34 +000037static std::string &getLibSupportInfoOutputFilename() {
Reid Spencerf6e5a252004-12-14 03:55:21 +000038 return *LibSupportInfoOutputFilename;
Chris Lattner71336a92003-07-31 19:38:34 +000039}
Chris Lattner6c38a792002-10-01 19:36:54 +000040
Chris Lattner3f398492003-01-30 23:08:50 +000041namespace {
Dan Gohman3c02aca2008-04-23 23:15:23 +000042 static cl::opt<bool>
Chris Lattner3f398492003-01-30 23:08:50 +000043 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
44 "tracking (this may be slow)"),
45 cl::Hidden);
Chris Lattnerf205fec2003-05-09 20:05:44 +000046
Dan Gohman3c02aca2008-04-23 23:15:23 +000047 static cl::opt<std::string, true>
Chris Lattner96a54db2003-08-01 22:15:15 +000048 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerf205fec2003-05-09 20:05:44 +000049 cl::desc("File to append -stats and -timer output to"),
Chris Lattner71336a92003-07-31 19:38:34 +000050 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Chris Lattner3f398492003-01-30 23:08:50 +000051}
52
Chris Lattner6c38a792002-10-01 19:36:54 +000053static TimerGroup *DefaultTimerGroup = 0;
54static TimerGroup *getDefaultTimerGroup() {
55 if (DefaultTimerGroup) return DefaultTimerGroup;
56 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
57}
58
59Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000060 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000061 Started(false), TG(getDefaultTimerGroup()) {
62 TG->addTimer();
63}
64
65Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000066 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000067 Started(false), TG(&tg) {
68 TG->addTimer();
69}
70
71Timer::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.
79Timer::Timer(bool, const Timer &T) {
80 TG = T.TG; // Avoid assertion in operator=
81 operator=(T); // Copy contents
82 TG = 0;
83}
84
85
86Timer::~Timer() {
87 if (TG) {
88 if (Started) {
89 Started = false;
90 TG->addTimerToPrint(*this);
91 }
92 TG->removeTimer();
93 }
94}
95
Jeff Cohene269a1a2005-01-08 20:15:57 +000096static inline size_t getMemUsage() {
Reid Spenceraeb47b82004-12-27 08:03:04 +000097 if (TrackSpace)
Jeff Cohene269a1a2005-01-08 20:15:57 +000098 return sys::Process::GetMallocUsage();
Reid Spenceraeb47b82004-12-27 08:03:04 +000099 return 0;
100}
101
Chris Lattner6c38a792002-10-01 19:36:54 +0000102struct TimeRecord {
103 double Elapsed, UserTime, SystemTime;
Chris Lattner6cfbd622005-01-29 05:21:16 +0000104 ssize_t MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000105};
106
Chris Lattner8f0d8242002-11-18 21:47:09 +0000107static TimeRecord getTimeRecord(bool Start) {
Chris Lattnerb4db5f32004-06-07 19:34:51 +0000108 TimeRecord Result;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000109
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000110 sys::TimeValue now(0,0);
111 sys::TimeValue user(0,0);
112 sys::TimeValue sys(0,0);
Chris Lattner6c38a792002-10-01 19:36:54 +0000113
Chris Lattner6cfbd622005-01-29 05:21:16 +0000114 ssize_t MemUsed = 0;
Reid Spencer7d055632004-12-20 21:44:27 +0000115 if (Start) {
Reid Spenceraeb47b82004-12-27 08:03:04 +0000116 MemUsed = getMemUsage();
Chris Lattnerfed1b272005-03-22 03:20:38 +0000117 sys::Process::GetTimeUsage(now,user,sys);
Reid Spencer7d055632004-12-20 21:44:27 +0000118 } else {
Reid Spencer7d055632004-12-20 21:44:27 +0000119 sys::Process::GetTimeUsage(now,user,sys);
Chris Lattnerfed1b272005-03-22 03:20:38 +0000120 MemUsed = getMemUsage();
Reid Spencer7d055632004-12-20 21:44:27 +0000121 }
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000122
Reid Spencer7d055632004-12-20 21:44:27 +0000123 Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0;
124 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
125 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
126 Result.MemUsed = MemUsed;
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000127
Chris Lattner6c38a792002-10-01 19:36:54 +0000128 return Result;
129}
130
Chris Lattner90aa8392006-10-04 21:52:35 +0000131static ManagedStatic<std::vector<Timer*> > ActiveTimers;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000132
Chris Lattner6c38a792002-10-01 19:36:54 +0000133void Timer::startTimer() {
134 Started = true;
Dan Gohman153d28a2008-06-24 22:07:07 +0000135 ActiveTimers->push_back(this);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000136 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000137 Elapsed -= TR.Elapsed;
138 UserTime -= TR.UserTime;
139 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000140 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000141 PeakMemBase = TR.MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000142}
143
144void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000145 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000146 Elapsed += TR.Elapsed;
147 UserTime += TR.UserTime;
148 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000149 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000150
Chris Lattner90aa8392006-10-04 21:52:35 +0000151 if (ActiveTimers->back() == this) {
152 ActiveTimers->pop_back();
Chris Lattner8f0d8242002-11-18 21:47:09 +0000153 } else {
154 std::vector<Timer*>::iterator I =
Chris Lattner90aa8392006-10-04 21:52:35 +0000155 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
156 assert(I != ActiveTimers->end() && "stop but no startTimer?");
157 ActiveTimers->erase(I);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000158 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000159}
160
161void Timer::sum(const Timer &T) {
162 Elapsed += T.Elapsed;
163 UserTime += T.UserTime;
164 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000165 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000166 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000167}
168
Chris Lattner8f0d8242002-11-18 21:47:09 +0000169/// addPeakMemoryMeasurement - This method should be called whenever memory
170/// usage needs to be checked. It adds a peak memory measurement to the
171/// currently active timers, which will be printed when the timer group prints
172///
173void Timer::addPeakMemoryMeasurement() {
Jeff Cohene269a1a2005-01-08 20:15:57 +0000174 size_t MemUsed = getMemUsage();
Chris Lattner8f0d8242002-11-18 21:47:09 +0000175
Chris Lattner90aa8392006-10-04 21:52:35 +0000176 for (std::vector<Timer*>::iterator I = ActiveTimers->begin(),
177 E = ActiveTimers->end(); I != E; ++I)
Chris Lattner8f0d8242002-11-18 21:47:09 +0000178 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
179}
180
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000181//===----------------------------------------------------------------------===//
182// NamedRegionTimer Implementation
183//===----------------------------------------------------------------------===//
184
Dan Gohman5e843682008-07-14 18:19:29 +0000185namespace {
186
187typedef std::map<std::string, Timer> Name2Timer;
188typedef std::map<std::string, std::pair<TimerGroup, Name2Timer> > Name2Pair;
189
190}
191
192static ManagedStatic<Name2Timer> NamedTimers;
193
194static ManagedStatic<Name2Pair> NamedGroupedTimers;
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000195
Chris Lattner90aa8392006-10-04 21:52:35 +0000196static Timer &getNamedRegionTimer(const std::string &Name) {
Dan Gohman5e843682008-07-14 18:19:29 +0000197 Name2Timer::iterator I = NamedTimers->find(Name);
Dan Gohmanc418bf32008-07-11 20:58:19 +0000198 if (I != NamedTimers->end())
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000199 return I->second;
200
Chris Lattner90aa8392006-10-04 21:52:35 +0000201 return NamedTimers->insert(I, std::make_pair(Name, Timer(Name)))->second;
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000202}
203
Dan Gohman5e843682008-07-14 18:19:29 +0000204static Timer &getNamedRegionTimer(const std::string &Name,
205 const std::string &GroupName) {
206
207 Name2Pair::iterator I = NamedGroupedTimers->find(GroupName);
208 if (I == NamedGroupedTimers->end()) {
209 TimerGroup TG(GroupName);
210 std::pair<TimerGroup, Name2Timer> Pair(TG, Name2Timer());
211 I = NamedGroupedTimers->insert(I, std::make_pair(GroupName, Pair));
212 }
213
214 Name2Timer::iterator J = I->second.second.find(Name);
215 if (J == I->second.second.end())
216 J = I->second.second.insert(J,
217 std::make_pair(Name,
218 Timer(Name,
219 I->second.first)));
220
221 return J->second;
222}
223
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000224NamedRegionTimer::NamedRegionTimer(const std::string &Name)
225 : TimeRegion(getNamedRegionTimer(Name)) {}
226
Dan Gohman5e843682008-07-14 18:19:29 +0000227NamedRegionTimer::NamedRegionTimer(const std::string &Name,
228 const std::string &GroupName)
229 : TimeRegion(getNamedRegionTimer(Name, GroupName)) {}
Chris Lattner8f0d8242002-11-18 21:47:09 +0000230
Chris Lattner6c38a792002-10-01 19:36:54 +0000231//===----------------------------------------------------------------------===//
232// TimerGroup Implementation
233//===----------------------------------------------------------------------===//
234
Chris Lattnerf205fec2003-05-09 20:05:44 +0000235// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
236// TotalWidth size, and B is the AfterDec size.
237//
238static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
239 std::ostream &OS) {
240 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
241 OS.width(TotalWidth-AfterDec-1);
242 char OldFill = OS.fill();
243 OS.fill(' ');
244 OS << (int)Val; // Integer part;
245 OS << ".";
246 OS.width(AfterDec);
247 OS.fill('0');
248 unsigned ResultFieldSize = 1;
249 while (AfterDec--) ResultFieldSize *= 10;
250 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
251 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000252}
253
Chris Lattnerf205fec2003-05-09 20:05:44 +0000254static void printVal(double Val, double Total, std::ostream &OS) {
255 if (Total < 1e-7) // Avoid dividing by zero...
256 OS << " ----- ";
257 else {
258 OS << " ";
259 printAlignedFP(Val, 4, 7, OS);
260 OS << " (";
261 printAlignedFP(Val*100/Total, 1, 5, OS);
262 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000263 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000264}
265
266void Timer::print(const Timer &Total, std::ostream &OS) {
267 if (Total.UserTime)
268 printVal(UserTime, Total.UserTime, OS);
269 if (Total.SystemTime)
270 printVal(SystemTime, Total.SystemTime, OS);
271 if (Total.getProcessTime())
272 printVal(getProcessTime(), Total.getProcessTime(), OS);
273 printVal(Elapsed, Total.Elapsed, OS);
Misha Brukmanf976c852005-04-21 22:55:34 +0000274
Chris Lattnerf205fec2003-05-09 20:05:44 +0000275 OS << " ";
276
277 if (Total.MemUsed) {
278 OS.width(9);
279 OS << MemUsed << " ";
280 }
281 if (Total.PeakMem) {
282 if (PeakMem) {
283 OS.width(9);
284 OS << PeakMem << " ";
285 } else
286 OS << " ";
287 }
288 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000289
290 Started = false; // Once printed, don't print again
291}
292
Chris Lattnerf205fec2003-05-09 20:05:44 +0000293// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
Brian Gaeked0fde302003-11-11 22:41:34 +0000294std::ostream *
Chris Lattnerb6d465f2003-12-14 21:27:33 +0000295llvm::GetLibSupportInfoOutputFile() {
Chris Lattner71336a92003-07-31 19:38:34 +0000296 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000297 if (LibSupportInfoOutputFilename.empty())
Bill Wendlingbcd24982006-12-07 20:28:15 +0000298 return cerr.stream();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000299 if (LibSupportInfoOutputFilename == "-")
Bill Wendlingbcd24982006-12-07 20:28:15 +0000300 return cout.stream();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000301
302 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
Chris Lattner903c2d12003-06-06 22:13:01 +0000303 std::ios::app);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000304 if (!Result->good()) {
Bill Wendlingbcd24982006-12-07 20:28:15 +0000305 cerr << "Error opening info-output-file '"
306 << LibSupportInfoOutputFilename << " for appending!\n";
Chris Lattnerf205fec2003-05-09 20:05:44 +0000307 delete Result;
Bill Wendlingbcd24982006-12-07 20:28:15 +0000308 return cerr.stream();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000309 }
310 return Result;
311}
312
Chris Lattner6c38a792002-10-01 19:36:54 +0000313
314void TimerGroup::removeTimer() {
315 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
316 // Sort the timers in descending order by amount of time taken...
317 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
318 std::greater<Timer>());
319
320 // Figure out how many spaces to indent TimerGroup name...
321 unsigned Padding = (80-Name.length())/2;
322 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
323
Chris Lattnerf205fec2003-05-09 20:05:44 +0000324 std::ostream *OutStream = GetLibSupportInfoOutputFile();
325
Chris Lattner6c38a792002-10-01 19:36:54 +0000326 ++NumTimers;
327 { // Scope to contain Total timer... don't allow total timer to drop us to
328 // zero timers...
329 Timer Total("TOTAL");
Misha Brukmanf976c852005-04-21 22:55:34 +0000330
Chris Lattner6c38a792002-10-01 19:36:54 +0000331 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
332 Total.sum(TimersToPrint[i]);
Misha Brukmanf976c852005-04-21 22:55:34 +0000333
Chris Lattner6c38a792002-10-01 19:36:54 +0000334 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000335 *OutStream << "===" << std::string(73, '-') << "===\n"
336 << std::string(Padding, ' ') << Name << "\n"
337 << "===" << std::string(73, '-')
Chris Lattner3ac96052005-02-09 18:41:32 +0000338 << "===\n";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000339
Chris Lattner3ac96052005-02-09 18:41:32 +0000340 // If this is not an collection of ungrouped times, print the total time.
341 // Ungrouped timers don't really make sense to add up. We still print the
342 // TOTAL line to make the percentages make sense.
343 if (this != DefaultTimerGroup) {
344 *OutStream << " Total Execution Time: ";
345
346 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
347 *OutStream << " seconds (";
348 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
349 *OutStream << " wall clock)\n";
350 }
351 *OutStream << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000352
353 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000354 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000355 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000356 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000357 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000358 *OutStream << " --User+System--";
359 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000360 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000361 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000362 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000363 *OutStream << " -PeakMem-";
364 *OutStream << " --- Name ---\n";
Misha Brukmanf976c852005-04-21 22:55:34 +0000365
Chris Lattner6c38a792002-10-01 19:36:54 +0000366 // Loop through all of the timing data, printing it out...
367 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000368 TimersToPrint[i].print(Total, *OutStream);
Misha Brukmanf976c852005-04-21 22:55:34 +0000369
Chris Lattnerf205fec2003-05-09 20:05:44 +0000370 Total.print(Total, *OutStream);
371 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000372 }
373 --NumTimers;
374
375 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000376
Bill Wendlingbcd24982006-12-07 20:28:15 +0000377 if (OutStream != cerr.stream() && OutStream != cout.stream())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000378 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000379 }
380
381 // Delete default timer group!
382 if (NumTimers == 0 && this == DefaultTimerGroup) {
383 delete DefaultTimerGroup;
384 DefaultTimerGroup = 0;
385 }
386}
Brian Gaeked0fde302003-11-11 22:41:34 +0000387