blob: 4bdfac298cc0c74eaab2c96f4575bc450a17bef3 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- Timer.cpp - Interval Timing Support -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// Interval Timing implementation.
11//
12//===----------------------------------------------------------------------===//
13
David Greene40dd8172010-01-05 01:28:58 +000014#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#include "llvm/Support/Timer.h"
16#include "llvm/Support/CommandLine.h"
17#include "llvm/Support/ManagedStatic.h"
Chris Lattner5febcae2009-08-23 08:43:55 +000018#include "llvm/Support/raw_ostream.h"
19#include "llvm/Support/Format.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/System/Process.h"
21#include <algorithm>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include <functional>
23#include <map>
24using namespace llvm;
25
26// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattner5febcae2009-08-23 08:43:55 +000027namespace llvm { extern raw_ostream *GetLibSupportInfoOutputFile(); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028
29// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
30// of constructor/destructor ordering being unspecified by C++. Basically the
31// problem is that a Statistic object gets destroyed, which ends up calling
32// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
33// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
34// would get destroyed before the Statistic, causing havoc to ensue. We "fix"
35// this by creating the string the first time it is needed and never destroying
36// it.
37static ManagedStatic<std::string> LibSupportInfoOutputFilename;
38static std::string &getLibSupportInfoOutputFilename() {
39 return *LibSupportInfoOutputFilename;
40}
41
Owen Anderson18463102009-06-23 20:52:29 +000042static ManagedStatic<sys::SmartMutex<true> > TimerLock;
43
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044namespace {
Dan Gohmanbbe69222008-04-23 23:15:23 +000045 static cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
47 "tracking (this may be slow)"),
48 cl::Hidden);
49
Dan Gohmanbbe69222008-04-23 23:15:23 +000050 static cl::opt<std::string, true>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
52 cl::desc("File to append -stats and -timer output to"),
53 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
54}
55
Owen Anderson56ddb832009-06-23 16:36:10 +000056static TimerGroup *DefaultTimerGroup = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057static TimerGroup *getDefaultTimerGroup() {
Owen Anderson1b2ea532009-06-23 17:33:37 +000058 TimerGroup* tmp = DefaultTimerGroup;
59 sys::MemoryFence();
60 if (!tmp) {
61 llvm_acquire_global_lock();
62 tmp = DefaultTimerGroup;
63 if (!tmp) {
64 tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
65 sys::MemoryFence();
66 DefaultTimerGroup = tmp;
67 }
68 llvm_release_global_lock();
69 }
Mikhail Glushenkov05858c52009-11-07 06:33:12 +000070
Owen Anderson1b2ea532009-06-23 17:33:37 +000071 return tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072}
73
74Timer::Timer(const std::string &N)
75 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
76 Started(false), TG(getDefaultTimerGroup()) {
77 TG->addTimer();
78}
79
80Timer::Timer(const std::string &N, TimerGroup &tg)
81 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
82 Started(false), TG(&tg) {
83 TG->addTimer();
84}
85
86Timer::Timer(const Timer &T) {
87 TG = T.TG;
88 if (TG) TG->addTimer();
89 operator=(T);
90}
91
92
93// Copy ctor, initialize with no TG member.
94Timer::Timer(bool, const Timer &T) {
95 TG = T.TG; // Avoid assertion in operator=
96 operator=(T); // Copy contents
97 TG = 0;
98}
99
100
101Timer::~Timer() {
102 if (TG) {
103 if (Started) {
104 Started = false;
105 TG->addTimerToPrint(*this);
106 }
107 TG->removeTimer();
108 }
109}
110
111static inline size_t getMemUsage() {
112 if (TrackSpace)
113 return sys::Process::GetMallocUsage();
114 return 0;
115}
116
117struct TimeRecord {
Owen Andersonac637c62009-06-23 20:17:22 +0000118 double Elapsed, UserTime, SystemTime;
119 ssize_t MemUsed;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120};
121
122static TimeRecord getTimeRecord(bool Start) {
123 TimeRecord Result;
124
125 sys::TimeValue now(0,0);
126 sys::TimeValue user(0,0);
127 sys::TimeValue sys(0,0);
128
Owen Andersonac637c62009-06-23 20:17:22 +0000129 ssize_t MemUsed = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 if (Start) {
131 MemUsed = getMemUsage();
132 sys::Process::GetTimeUsage(now,user,sys);
133 } else {
134 sys::Process::GetTimeUsage(now,user,sys);
135 MemUsed = getMemUsage();
136 }
137
Owen Andersonac637c62009-06-23 20:17:22 +0000138 Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0;
139 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
140 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 Result.MemUsed = MemUsed;
142
143 return Result;
144}
145
146static ManagedStatic<std::vector<Timer*> > ActiveTimers;
147
148void Timer::startTimer() {
Owen Anderson6171d2b2009-11-17 07:06:10 +0000149 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 Started = true;
Dan Gohmanf6930f92008-06-24 22:07:07 +0000151 ActiveTimers->push_back(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 TimeRecord TR = getTimeRecord(true);
153 Elapsed -= TR.Elapsed;
154 UserTime -= TR.UserTime;
155 SystemTime -= TR.SystemTime;
156 MemUsed -= TR.MemUsed;
157 PeakMemBase = TR.MemUsed;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158}
159
160void Timer::stopTimer() {
Owen Anderson6171d2b2009-11-17 07:06:10 +0000161 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 TimeRecord TR = getTimeRecord(false);
163 Elapsed += TR.Elapsed;
164 UserTime += TR.UserTime;
165 SystemTime += TR.SystemTime;
166 MemUsed += TR.MemUsed;
167
168 if (ActiveTimers->back() == this) {
169 ActiveTimers->pop_back();
170 } else {
171 std::vector<Timer*>::iterator I =
172 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
173 assert(I != ActiveTimers->end() && "stop but no startTimer?");
174 ActiveTimers->erase(I);
175 }
176}
177
178void Timer::sum(const Timer &T) {
179 Elapsed += T.Elapsed;
180 UserTime += T.UserTime;
181 SystemTime += T.SystemTime;
182 MemUsed += T.MemUsed;
183 PeakMem += T.PeakMem;
184}
185
186/// addPeakMemoryMeasurement - This method should be called whenever memory
187/// usage needs to be checked. It adds a peak memory measurement to the
188/// currently active timers, which will be printed when the timer group prints
189///
190void Timer::addPeakMemoryMeasurement() {
Owen Anderson6171d2b2009-11-17 07:06:10 +0000191 sys::SmartScopedLock<true> L(*TimerLock);
Owen Andersonac637c62009-06-23 20:17:22 +0000192 size_t MemUsed = getMemUsage();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193
194 for (std::vector<Timer*>::iterator I = ActiveTimers->begin(),
Owen Anderson6171d2b2009-11-17 07:06:10 +0000195 E = ActiveTimers->end(); I != E; ++I)
Owen Andersonac637c62009-06-23 20:17:22 +0000196 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197}
198
199//===----------------------------------------------------------------------===//
200// NamedRegionTimer Implementation
201//===----------------------------------------------------------------------===//
202
Dan Gohman368a08b2008-07-14 18:19:29 +0000203namespace {
204
205typedef std::map<std::string, Timer> Name2Timer;
206typedef std::map<std::string, std::pair<TimerGroup, Name2Timer> > Name2Pair;
207
208}
209
210static ManagedStatic<Name2Timer> NamedTimers;
211
212static ManagedStatic<Name2Pair> NamedGroupedTimers;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213
214static Timer &getNamedRegionTimer(const std::string &Name) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000215 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman368a08b2008-07-14 18:19:29 +0000216 Name2Timer::iterator I = NamedTimers->find(Name);
Dan Gohmanb261a982008-07-11 20:58:19 +0000217 if (I != NamedTimers->end())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 return I->second;
219
220 return NamedTimers->insert(I, std::make_pair(Name, Timer(Name)))->second;
221}
222
Dan Gohman368a08b2008-07-14 18:19:29 +0000223static Timer &getNamedRegionTimer(const std::string &Name,
224 const std::string &GroupName) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000225 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman368a08b2008-07-14 18:19:29 +0000226
227 Name2Pair::iterator I = NamedGroupedTimers->find(GroupName);
228 if (I == NamedGroupedTimers->end()) {
229 TimerGroup TG(GroupName);
230 std::pair<TimerGroup, Name2Timer> Pair(TG, Name2Timer());
231 I = NamedGroupedTimers->insert(I, std::make_pair(GroupName, Pair));
232 }
233
234 Name2Timer::iterator J = I->second.second.find(Name);
235 if (J == I->second.second.end())
236 J = I->second.second.insert(J,
237 std::make_pair(Name,
238 Timer(Name,
239 I->second.first)));
240
241 return J->second;
242}
243
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244NamedRegionTimer::NamedRegionTimer(const std::string &Name)
245 : TimeRegion(getNamedRegionTimer(Name)) {}
246
Dan Gohman368a08b2008-07-14 18:19:29 +0000247NamedRegionTimer::NamedRegionTimer(const std::string &Name,
248 const std::string &GroupName)
249 : TimeRegion(getNamedRegionTimer(Name, GroupName)) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250
251//===----------------------------------------------------------------------===//
252// TimerGroup Implementation
253//===----------------------------------------------------------------------===//
254
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255
Chris Lattner5febcae2009-08-23 08:43:55 +0000256static void printVal(double Val, double Total, raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 if (Total < 1e-7) // Avoid dividing by zero...
258 OS << " ----- ";
259 else {
Chris Lattner5febcae2009-08-23 08:43:55 +0000260 OS << " " << format("%7.4f", Val) << " (";
261 OS << format("%5.1f", Val*100/Total) << "%)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 }
263}
264
Chris Lattner5febcae2009-08-23 08:43:55 +0000265void Timer::print(const Timer &Total, raw_ostream &OS) {
Owen Anderson6171d2b2009-11-17 07:06:10 +0000266 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 if (Total.UserTime)
Owen Andersonac637c62009-06-23 20:17:22 +0000268 printVal(UserTime, Total.UserTime, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 if (Total.SystemTime)
Owen Andersonac637c62009-06-23 20:17:22 +0000270 printVal(SystemTime, Total.SystemTime, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 if (Total.getProcessTime())
Owen Andersonac637c62009-06-23 20:17:22 +0000272 printVal(getProcessTime(), Total.getProcessTime(), OS);
273 printVal(Elapsed, Total.Elapsed, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274
275 OS << " ";
276
277 if (Total.MemUsed) {
Chris Lattner5febcae2009-08-23 08:43:55 +0000278 OS << format("%9lld", (long long)MemUsed) << " ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 }
280 if (Total.PeakMem) {
281 if (PeakMem) {
Chris Lattner5febcae2009-08-23 08:43:55 +0000282 OS << format("%9lld", (long long)PeakMem) << " ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 } else
284 OS << " ";
285 }
286 OS << Name << "\n";
287
288 Started = false; // Once printed, don't print again
289}
290
291// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
Chris Lattner5febcae2009-08-23 08:43:55 +0000292raw_ostream *
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293llvm::GetLibSupportInfoOutputFile() {
294 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
295 if (LibSupportInfoOutputFilename.empty())
Chris Lattner5febcae2009-08-23 08:43:55 +0000296 return &errs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 if (LibSupportInfoOutputFilename == "-")
Chris Lattner5febcae2009-08-23 08:43:55 +0000298 return &outs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299
Mikhail Glushenkov05858c52009-11-07 06:33:12 +0000300
Chris Lattner5febcae2009-08-23 08:43:55 +0000301 std::string Error;
302 raw_ostream *Result = new raw_fd_ostream(LibSupportInfoOutputFilename.c_str(),
303 Error, raw_fd_ostream::F_Append);
304 if (Error.empty())
305 return Result;
Mikhail Glushenkov05858c52009-11-07 06:33:12 +0000306
Chris Lattner5febcae2009-08-23 08:43:55 +0000307 errs() << "Error opening info-output-file '"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 << LibSupportInfoOutputFilename << " for appending!\n";
Chris Lattner5febcae2009-08-23 08:43:55 +0000309 delete Result;
310 return &errs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311}
312
313
314void TimerGroup::removeTimer() {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000315 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
317 // Sort the timers in descending order by amount of time taken...
318 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
319 std::greater<Timer>());
320
321 // Figure out how many spaces to indent TimerGroup name...
322 unsigned Padding = (80-Name.length())/2;
323 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
324
Chris Lattner5febcae2009-08-23 08:43:55 +0000325 raw_ostream *OutStream = GetLibSupportInfoOutputFile();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326
327 ++NumTimers;
328 { // Scope to contain Total timer... don't allow total timer to drop us to
329 // zero timers...
330 Timer Total("TOTAL");
331
332 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
333 Total.sum(TimersToPrint[i]);
334
335 // Print out timing header...
336 *OutStream << "===" << std::string(73, '-') << "===\n"
337 << std::string(Padding, ' ') << Name << "\n"
338 << "===" << std::string(73, '-')
339 << "===\n";
340
341 // If this is not an collection of ungrouped times, print the total time.
342 // Ungrouped timers don't really make sense to add up. We still print the
343 // TOTAL line to make the percentages make sense.
Owen Anderson56ddb832009-06-23 16:36:10 +0000344 if (this != DefaultTimerGroup) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 *OutStream << " Total Execution Time: ";
346
Chris Lattner5febcae2009-08-23 08:43:55 +0000347 *OutStream << format("%5.4f", Total.getProcessTime()) << " seconds (";
348 *OutStream << format("%5.4f", Total.getWallTime()) << " wall clock)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 }
350 *OutStream << "\n";
351
Owen Andersonac637c62009-06-23 20:17:22 +0000352 if (Total.UserTime)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 *OutStream << " ---User Time---";
Owen Andersonac637c62009-06-23 20:17:22 +0000354 if (Total.SystemTime)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 *OutStream << " --System Time--";
Owen Andersonac637c62009-06-23 20:17:22 +0000356 if (Total.getProcessTime())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 *OutStream << " --User+System--";
358 *OutStream << " ---Wall Time---";
Owen Andersonac637c62009-06-23 20:17:22 +0000359 if (Total.getMemUsed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360 *OutStream << " ---Mem---";
Owen Andersonac637c62009-06-23 20:17:22 +0000361 if (Total.getPeakMem())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 *OutStream << " -PeakMem-";
363 *OutStream << " --- Name ---\n";
364
365 // Loop through all of the timing data, printing it out...
366 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
367 TimersToPrint[i].print(Total, *OutStream);
368
369 Total.print(Total, *OutStream);
Chris Lattner5febcae2009-08-23 08:43:55 +0000370 *OutStream << '\n';
371 OutStream->flush();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 }
373 --NumTimers;
374
375 TimersToPrint.clear();
376
David Greene40dd8172010-01-05 01:28:58 +0000377 if (OutStream != &errs() && OutStream != &outs() && OutStream != &dbgs())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 delete OutStream; // Close the file...
379 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000380}
381
Owen Anderson18463102009-06-23 20:52:29 +0000382void TimerGroup::addTimer() {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000383 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson18463102009-06-23 20:52:29 +0000384 ++NumTimers;
385}
386
387void TimerGroup::addTimerToPrint(const Timer &T) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000388 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson18463102009-06-23 20:52:29 +0000389 TimersToPrint.push_back(Timer(true, T));
390}
391