blob: 4dbb34c8a9432bf86d728305d75b7d339a775b1b [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() {
Chris Lattnerc3546852010-03-29 20:35:01 +000058 TimerGroup *tmp = DefaultTimerGroup;
Owen Anderson1b2ea532009-06-23 17:33:37 +000059 sys::MemoryFence();
Chris Lattnerc3546852010-03-29 20:35:01 +000060 if (tmp) return tmp;
61
62 llvm_acquire_global_lock();
63 tmp = DefaultTimerGroup;
Owen Anderson1b2ea532009-06-23 17:33:37 +000064 if (!tmp) {
Chris Lattnerc3546852010-03-29 20:35:01 +000065 tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
66 sys::MemoryFence();
67 DefaultTimerGroup = tmp;
Owen Anderson1b2ea532009-06-23 17:33:37 +000068 }
Chris Lattnerc3546852010-03-29 20:35:01 +000069 llvm_release_global_lock();
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
Chris Lattnerc3546852010-03-29 20:35:01 +000074//===----------------------------------------------------------------------===//
75// Timer Implementation
76//===----------------------------------------------------------------------===//
77
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078Timer::Timer(const std::string &N)
79 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
80 Started(false), TG(getDefaultTimerGroup()) {
81 TG->addTimer();
82}
83
84Timer::Timer(const std::string &N, TimerGroup &tg)
85 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
86 Started(false), TG(&tg) {
87 TG->addTimer();
88}
89
90Timer::Timer(const Timer &T) {
91 TG = T.TG;
92 if (TG) TG->addTimer();
93 operator=(T);
94}
95
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096// Copy ctor, initialize with no TG member.
97Timer::Timer(bool, const Timer &T) {
98 TG = T.TG; // Avoid assertion in operator=
99 operator=(T); // Copy contents
100 TG = 0;
101}
102
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000103Timer::~Timer() {
Chris Lattnerc3546852010-03-29 20:35:01 +0000104 if (!TG) return;
105
106 if (Started) {
107 Started = false;
108 TG->addTimerToPrint(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 }
Chris Lattnerc3546852010-03-29 20:35:01 +0000110 TG->removeTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111}
112
113static inline size_t getMemUsage() {
114 if (TrackSpace)
115 return sys::Process::GetMallocUsage();
116 return 0;
117}
118
119struct TimeRecord {
Owen Andersonac637c62009-06-23 20:17:22 +0000120 double Elapsed, UserTime, SystemTime;
121 ssize_t MemUsed;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122};
123
124static TimeRecord getTimeRecord(bool Start) {
125 TimeRecord Result;
126
127 sys::TimeValue now(0,0);
128 sys::TimeValue user(0,0);
129 sys::TimeValue sys(0,0);
130
Owen Andersonac637c62009-06-23 20:17:22 +0000131 ssize_t MemUsed = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 if (Start) {
133 MemUsed = getMemUsage();
Chris Lattnerc3546852010-03-29 20:35:01 +0000134 sys::Process::GetTimeUsage(now, user, sys);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 } else {
Chris Lattnerc3546852010-03-29 20:35:01 +0000136 sys::Process::GetTimeUsage(now, user, sys);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 MemUsed = getMemUsage();
138 }
139
Chris Lattnerc3546852010-03-29 20:35:01 +0000140 Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0;
141 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
142 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
143 Result.MemUsed = MemUsed;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 return Result;
145}
146
147static ManagedStatic<std::vector<Timer*> > ActiveTimers;
148
149void Timer::startTimer() {
Owen Anderson6171d2b2009-11-17 07:06:10 +0000150 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 Started = true;
Dan Gohmanf6930f92008-06-24 22:07:07 +0000152 ActiveTimers->push_back(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 TimeRecord TR = getTimeRecord(true);
154 Elapsed -= TR.Elapsed;
155 UserTime -= TR.UserTime;
156 SystemTime -= TR.SystemTime;
157 MemUsed -= TR.MemUsed;
158 PeakMemBase = TR.MemUsed;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159}
160
161void Timer::stopTimer() {
Owen Anderson6171d2b2009-11-17 07:06:10 +0000162 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 TimeRecord TR = getTimeRecord(false);
164 Elapsed += TR.Elapsed;
165 UserTime += TR.UserTime;
166 SystemTime += TR.SystemTime;
167 MemUsed += TR.MemUsed;
168
169 if (ActiveTimers->back() == this) {
170 ActiveTimers->pop_back();
171 } else {
172 std::vector<Timer*>::iterator I =
173 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
174 assert(I != ActiveTimers->end() && "stop but no startTimer?");
175 ActiveTimers->erase(I);
176 }
177}
178
179void Timer::sum(const Timer &T) {
180 Elapsed += T.Elapsed;
181 UserTime += T.UserTime;
182 SystemTime += T.SystemTime;
183 MemUsed += T.MemUsed;
184 PeakMem += T.PeakMem;
185}
186
187/// addPeakMemoryMeasurement - This method should be called whenever memory
188/// usage needs to be checked. It adds a peak memory measurement to the
189/// currently active timers, which will be printed when the timer group prints
190///
191void Timer::addPeakMemoryMeasurement() {
Owen Anderson6171d2b2009-11-17 07:06:10 +0000192 sys::SmartScopedLock<true> L(*TimerLock);
Owen Andersonac637c62009-06-23 20:17:22 +0000193 size_t MemUsed = getMemUsage();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194
195 for (std::vector<Timer*>::iterator I = ActiveTimers->begin(),
Owen Anderson6171d2b2009-11-17 07:06:10 +0000196 E = ActiveTimers->end(); I != E; ++I)
Owen Andersonac637c62009-06-23 20:17:22 +0000197 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198}
199
Chris Lattnerc3546852010-03-29 20:35:01 +0000200
201static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner626ca122010-03-29 20:40:19 +0000202 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerc3546852010-03-29 20:35:01 +0000203 OS << " ----- ";
204 else {
205 OS << " " << format("%7.4f", Val) << " (";
206 OS << format("%5.1f", Val*100/Total) << "%)";
207 }
208}
209
210void Timer::print(const Timer &Total, raw_ostream &OS) {
211 sys::SmartScopedLock<true> L(*TimerLock);
212 if (Total.UserTime)
213 printVal(UserTime, Total.UserTime, OS);
214 if (Total.SystemTime)
215 printVal(SystemTime, Total.SystemTime, OS);
216 if (Total.getProcessTime())
217 printVal(getProcessTime(), Total.getProcessTime(), OS);
218 printVal(Elapsed, Total.Elapsed, OS);
219
220 OS << " ";
221
222 if (Total.MemUsed) {
223 OS << format("%9lld", (long long)MemUsed) << " ";
224 }
225 if (Total.PeakMem) {
226 if (PeakMem) {
227 OS << format("%9lld", (long long)PeakMem) << " ";
228 } else
229 OS << " ";
230 }
231 OS << Name << "\n";
232
233 Started = false; // Once printed, don't print again
234}
235
236
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237//===----------------------------------------------------------------------===//
238// NamedRegionTimer Implementation
239//===----------------------------------------------------------------------===//
240
Dan Gohman368a08b2008-07-14 18:19:29 +0000241typedef std::map<std::string, Timer> Name2Timer;
242typedef std::map<std::string, std::pair<TimerGroup, Name2Timer> > Name2Pair;
243
Dan Gohman368a08b2008-07-14 18:19:29 +0000244static ManagedStatic<Name2Timer> NamedTimers;
Dan Gohman368a08b2008-07-14 18:19:29 +0000245static ManagedStatic<Name2Pair> NamedGroupedTimers;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246
247static Timer &getNamedRegionTimer(const std::string &Name) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000248 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman368a08b2008-07-14 18:19:29 +0000249 Name2Timer::iterator I = NamedTimers->find(Name);
Dan Gohmanb261a982008-07-11 20:58:19 +0000250 if (I != NamedTimers->end())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 return I->second;
252
253 return NamedTimers->insert(I, std::make_pair(Name, Timer(Name)))->second;
254}
255
Dan Gohman368a08b2008-07-14 18:19:29 +0000256static Timer &getNamedRegionTimer(const std::string &Name,
257 const std::string &GroupName) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000258 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman368a08b2008-07-14 18:19:29 +0000259
260 Name2Pair::iterator I = NamedGroupedTimers->find(GroupName);
261 if (I == NamedGroupedTimers->end()) {
262 TimerGroup TG(GroupName);
263 std::pair<TimerGroup, Name2Timer> Pair(TG, Name2Timer());
264 I = NamedGroupedTimers->insert(I, std::make_pair(GroupName, Pair));
265 }
266
267 Name2Timer::iterator J = I->second.second.find(Name);
268 if (J == I->second.second.end())
269 J = I->second.second.insert(J,
270 std::make_pair(Name,
271 Timer(Name,
272 I->second.first)));
273
274 return J->second;
275}
276
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277NamedRegionTimer::NamedRegionTimer(const std::string &Name)
278 : TimeRegion(getNamedRegionTimer(Name)) {}
279
Dan Gohman368a08b2008-07-14 18:19:29 +0000280NamedRegionTimer::NamedRegionTimer(const std::string &Name,
281 const std::string &GroupName)
282 : TimeRegion(getNamedRegionTimer(Name, GroupName)) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283
284//===----------------------------------------------------------------------===//
285// TimerGroup Implementation
286//===----------------------------------------------------------------------===//
287
Chris Lattner626ca122010-03-29 20:40:19 +0000288// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattner5febcae2009-08-23 08:43:55 +0000289raw_ostream *
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290llvm::GetLibSupportInfoOutputFile() {
291 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
292 if (LibSupportInfoOutputFilename.empty())
Chris Lattner5febcae2009-08-23 08:43:55 +0000293 return &errs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 if (LibSupportInfoOutputFilename == "-")
Chris Lattner5febcae2009-08-23 08:43:55 +0000295 return &outs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296
Mikhail Glushenkov05858c52009-11-07 06:33:12 +0000297
Chris Lattner5febcae2009-08-23 08:43:55 +0000298 std::string Error;
299 raw_ostream *Result = new raw_fd_ostream(LibSupportInfoOutputFilename.c_str(),
300 Error, raw_fd_ostream::F_Append);
301 if (Error.empty())
302 return Result;
Mikhail Glushenkov05858c52009-11-07 06:33:12 +0000303
Chris Lattner5febcae2009-08-23 08:43:55 +0000304 errs() << "Error opening info-output-file '"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000305 << LibSupportInfoOutputFilename << " for appending!\n";
Chris Lattner5febcae2009-08-23 08:43:55 +0000306 delete Result;
307 return &errs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308}
309
310
311void TimerGroup::removeTimer() {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000312 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattnerc3546852010-03-29 20:35:01 +0000313 if (--NumTimers != 0 || TimersToPrint.empty())
314 return; // Don't print timing report.
315
316 // Sort the timers in descending order by amount of time taken.
317 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
318 std::greater<Timer>());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319
Chris Lattnerc3546852010-03-29 20:35:01 +0000320 // 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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323
Chris Lattnerc3546852010-03-29 20:35:01 +0000324 raw_ostream *OutStream = GetLibSupportInfoOutputFile();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325
Chris Lattnerc3546852010-03-29 20:35:01 +0000326 ++NumTimers;
Chris Lattner626ca122010-03-29 20:40:19 +0000327 { // Scope to contain Total timer: don't allow total timer to drop us to
328 // zero timers.
Chris Lattnerc3546852010-03-29 20:35:01 +0000329 Timer Total("TOTAL");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330
Chris Lattnerc3546852010-03-29 20:35:01 +0000331 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
332 Total.sum(TimersToPrint[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333
Chris Lattner626ca122010-03-29 20:40:19 +0000334 // Print out timing header.
Chris Lattnerc3546852010-03-29 20:35:01 +0000335 *OutStream << "===" << std::string(73, '-') << "===\n"
336 << std::string(Padding, ' ') << Name << "\n"
337 << "===" << std::string(73, '-')
338 << "===\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339
Chris Lattnerc3546852010-03-29 20:35:01 +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: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345
Chris Lattnerc3546852010-03-29 20:35:01 +0000346 *OutStream << format("%5.4f", Total.getProcessTime()) << " seconds (";
347 *OutStream << format("%5.4f", Total.getWallTime()) << " wall clock)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 }
Chris Lattnerc3546852010-03-29 20:35:01 +0000349 *OutStream << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350
Chris Lattnerc3546852010-03-29 20:35:01 +0000351 if (Total.UserTime)
352 *OutStream << " ---User Time---";
353 if (Total.SystemTime)
354 *OutStream << " --System Time--";
355 if (Total.getProcessTime())
356 *OutStream << " --User+System--";
357 *OutStream << " ---Wall Time---";
358 if (Total.getMemUsed())
359 *OutStream << " ---Mem---";
360 if (Total.getPeakMem())
361 *OutStream << " -PeakMem-";
362 *OutStream << " --- Name ---\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363
Chris Lattner626ca122010-03-29 20:40:19 +0000364 // Loop through all of the timing data, printing it out.
Chris Lattnerc3546852010-03-29 20:35:01 +0000365 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
366 TimersToPrint[i].print(Total, *OutStream);
367
368 Total.print(Total, *OutStream);
369 *OutStream << '\n';
370 OutStream->flush();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371 }
Chris Lattnerc3546852010-03-29 20:35:01 +0000372 --NumTimers;
373
374 TimersToPrint.clear();
375
376 if (OutStream != &errs() && OutStream != &outs() && OutStream != &dbgs())
Chris Lattner626ca122010-03-29 20:40:19 +0000377 delete OutStream; // Close the file.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378}
379
Owen Anderson18463102009-06-23 20:52:29 +0000380void TimerGroup::addTimer() {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000381 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson18463102009-06-23 20:52:29 +0000382 ++NumTimers;
383}
384
385void TimerGroup::addTimerToPrint(const Timer &T) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000386 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson18463102009-06-23 20:52:29 +0000387 TimersToPrint.push_back(Timer(true, T));
388}
389