blob: 16ab55fcbcedea1105f9b4f646e8ef659af7fd50 [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() {
150 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() {
161 TimeRecord TR = getTimeRecord(false);
162 Elapsed += TR.Elapsed;
163 UserTime += TR.UserTime;
164 SystemTime += TR.SystemTime;
165 MemUsed += TR.MemUsed;
166
167 if (ActiveTimers->back() == this) {
168 ActiveTimers->pop_back();
169 } else {
170 std::vector<Timer*>::iterator I =
171 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
172 assert(I != ActiveTimers->end() && "stop but no startTimer?");
173 ActiveTimers->erase(I);
174 }
175}
176
177void Timer::sum(const Timer &T) {
178 Elapsed += T.Elapsed;
179 UserTime += T.UserTime;
180 SystemTime += T.SystemTime;
181 MemUsed += T.MemUsed;
182 PeakMem += T.PeakMem;
183}
184
Chris Lattner5f52edc2010-03-29 21:24:52 +0000185const Timer &Timer::operator=(const Timer &T) {
186 Elapsed = T.Elapsed;
187 UserTime = T.UserTime;
188 SystemTime = T.SystemTime;
189 MemUsed = T.MemUsed;
190 PeakMem = T.PeakMem;
191 PeakMemBase = T.PeakMemBase;
192 Name = T.Name;
193 Started = T.Started;
194 assert(TG == T.TG && "Can only assign timers in the same TimerGroup!");
195 return *this;
196}
197
198
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199/// addPeakMemoryMeasurement - This method should be called whenever memory
200/// usage needs to be checked. It adds a peak memory measurement to the
201/// currently active timers, which will be printed when the timer group prints
202///
203void Timer::addPeakMemoryMeasurement() {
Owen Andersonac637c62009-06-23 20:17:22 +0000204 size_t MemUsed = getMemUsage();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 for (std::vector<Timer*>::iterator I = ActiveTimers->begin(),
Owen Anderson6171d2b2009-11-17 07:06:10 +0000206 E = ActiveTimers->end(); I != E; ++I)
Owen Andersonac637c62009-06-23 20:17:22 +0000207 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208}
209
Chris Lattnerc3546852010-03-29 20:35:01 +0000210
211static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner626ca122010-03-29 20:40:19 +0000212 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerc3546852010-03-29 20:35:01 +0000213 OS << " ----- ";
214 else {
215 OS << " " << format("%7.4f", Val) << " (";
216 OS << format("%5.1f", Val*100/Total) << "%)";
217 }
218}
219
220void Timer::print(const Timer &Total, raw_ostream &OS) {
Chris Lattnerc3546852010-03-29 20:35:01 +0000221 if (Total.UserTime)
222 printVal(UserTime, Total.UserTime, OS);
223 if (Total.SystemTime)
224 printVal(SystemTime, Total.SystemTime, OS);
225 if (Total.getProcessTime())
226 printVal(getProcessTime(), Total.getProcessTime(), OS);
227 printVal(Elapsed, Total.Elapsed, OS);
228
229 OS << " ";
230
Chris Lattner5f52edc2010-03-29 21:24:52 +0000231 if (Total.MemUsed)
Chris Lattnerc3546852010-03-29 20:35:01 +0000232 OS << format("%9lld", (long long)MemUsed) << " ";
Chris Lattner5f52edc2010-03-29 21:24:52 +0000233
Chris Lattnerc3546852010-03-29 20:35:01 +0000234 if (Total.PeakMem) {
Chris Lattner5f52edc2010-03-29 21:24:52 +0000235 if (PeakMem)
Chris Lattnerc3546852010-03-29 20:35:01 +0000236 OS << format("%9lld", (long long)PeakMem) << " ";
Chris Lattner5f52edc2010-03-29 21:24:52 +0000237 else
Chris Lattnerc3546852010-03-29 20:35:01 +0000238 OS << " ";
239 }
240 OS << Name << "\n";
241
242 Started = false; // Once printed, don't print again
243}
244
245
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246//===----------------------------------------------------------------------===//
247// NamedRegionTimer Implementation
248//===----------------------------------------------------------------------===//
249
Dan Gohman368a08b2008-07-14 18:19:29 +0000250typedef std::map<std::string, Timer> Name2Timer;
251typedef std::map<std::string, std::pair<TimerGroup, Name2Timer> > Name2Pair;
252
Dan Gohman368a08b2008-07-14 18:19:29 +0000253static ManagedStatic<Name2Timer> NamedTimers;
Dan Gohman368a08b2008-07-14 18:19:29 +0000254static ManagedStatic<Name2Pair> NamedGroupedTimers;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255
256static Timer &getNamedRegionTimer(const std::string &Name) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000257 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman368a08b2008-07-14 18:19:29 +0000258 Name2Timer::iterator I = NamedTimers->find(Name);
Dan Gohmanb261a982008-07-11 20:58:19 +0000259 if (I != NamedTimers->end())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 return I->second;
261
262 return NamedTimers->insert(I, std::make_pair(Name, Timer(Name)))->second;
263}
264
Dan Gohman368a08b2008-07-14 18:19:29 +0000265static Timer &getNamedRegionTimer(const std::string &Name,
266 const std::string &GroupName) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000267 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman368a08b2008-07-14 18:19:29 +0000268
269 Name2Pair::iterator I = NamedGroupedTimers->find(GroupName);
270 if (I == NamedGroupedTimers->end()) {
271 TimerGroup TG(GroupName);
272 std::pair<TimerGroup, Name2Timer> Pair(TG, Name2Timer());
273 I = NamedGroupedTimers->insert(I, std::make_pair(GroupName, Pair));
274 }
275
276 Name2Timer::iterator J = I->second.second.find(Name);
277 if (J == I->second.second.end())
278 J = I->second.second.insert(J,
279 std::make_pair(Name,
280 Timer(Name,
281 I->second.first)));
282
283 return J->second;
284}
285
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286NamedRegionTimer::NamedRegionTimer(const std::string &Name)
287 : TimeRegion(getNamedRegionTimer(Name)) {}
288
Dan Gohman368a08b2008-07-14 18:19:29 +0000289NamedRegionTimer::NamedRegionTimer(const std::string &Name,
290 const std::string &GroupName)
291 : TimeRegion(getNamedRegionTimer(Name, GroupName)) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292
293//===----------------------------------------------------------------------===//
294// TimerGroup Implementation
295//===----------------------------------------------------------------------===//
296
Chris Lattner626ca122010-03-29 20:40:19 +0000297// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattner5f52edc2010-03-29 21:24:52 +0000298raw_ostream *llvm::GetLibSupportInfoOutputFile() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
300 if (LibSupportInfoOutputFilename.empty())
Chris Lattner5febcae2009-08-23 08:43:55 +0000301 return &errs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 if (LibSupportInfoOutputFilename == "-")
Chris Lattner5febcae2009-08-23 08:43:55 +0000303 return &outs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304
Chris Lattner5febcae2009-08-23 08:43:55 +0000305 std::string Error;
306 raw_ostream *Result = new raw_fd_ostream(LibSupportInfoOutputFilename.c_str(),
307 Error, raw_fd_ostream::F_Append);
308 if (Error.empty())
309 return Result;
Mikhail Glushenkov05858c52009-11-07 06:33:12 +0000310
Chris Lattner5febcae2009-08-23 08:43:55 +0000311 errs() << "Error opening info-output-file '"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 << LibSupportInfoOutputFilename << " for appending!\n";
Chris Lattner5febcae2009-08-23 08:43:55 +0000313 delete Result;
314 return &errs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315}
316
317
318void TimerGroup::removeTimer() {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000319 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattnerc3546852010-03-29 20:35:01 +0000320 if (--NumTimers != 0 || TimersToPrint.empty())
321 return; // Don't print timing report.
322
323 // Sort the timers in descending order by amount of time taken.
324 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
325 std::greater<Timer>());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326
Chris Lattnerc3546852010-03-29 20:35:01 +0000327 // Figure out how many spaces to indent TimerGroup name.
328 unsigned Padding = (80-Name.length())/2;
329 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330
Chris Lattnerc3546852010-03-29 20:35:01 +0000331 raw_ostream *OutStream = GetLibSupportInfoOutputFile();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332
Chris Lattnerc3546852010-03-29 20:35:01 +0000333 ++NumTimers;
Chris Lattner626ca122010-03-29 20:40:19 +0000334 { // Scope to contain Total timer: don't allow total timer to drop us to
335 // zero timers.
Chris Lattnerc3546852010-03-29 20:35:01 +0000336 Timer Total("TOTAL");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337
Chris Lattnerc3546852010-03-29 20:35:01 +0000338 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
339 Total.sum(TimersToPrint[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340
Chris Lattner626ca122010-03-29 20:40:19 +0000341 // Print out timing header.
Chris Lattnerc3546852010-03-29 20:35:01 +0000342 *OutStream << "===" << std::string(73, '-') << "===\n"
343 << std::string(Padding, ' ') << Name << "\n"
344 << "===" << std::string(73, '-')
345 << "===\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346
Chris Lattnerc3546852010-03-29 20:35:01 +0000347 // If this is not an collection of ungrouped times, print the total time.
348 // Ungrouped timers don't really make sense to add up. We still print the
349 // TOTAL line to make the percentages make sense.
350 if (this != DefaultTimerGroup) {
351 *OutStream << " Total Execution Time: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352
Chris Lattnerc3546852010-03-29 20:35:01 +0000353 *OutStream << format("%5.4f", Total.getProcessTime()) << " seconds (";
354 *OutStream << format("%5.4f", Total.getWallTime()) << " wall clock)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 }
Chris Lattnerc3546852010-03-29 20:35:01 +0000356 *OutStream << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357
Chris Lattnerc3546852010-03-29 20:35:01 +0000358 if (Total.UserTime)
359 *OutStream << " ---User Time---";
360 if (Total.SystemTime)
361 *OutStream << " --System Time--";
362 if (Total.getProcessTime())
363 *OutStream << " --User+System--";
364 *OutStream << " ---Wall Time---";
365 if (Total.getMemUsed())
366 *OutStream << " ---Mem---";
367 if (Total.getPeakMem())
368 *OutStream << " -PeakMem-";
369 *OutStream << " --- Name ---\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000370
Chris Lattner626ca122010-03-29 20:40:19 +0000371 // Loop through all of the timing data, printing it out.
Chris Lattnerc3546852010-03-29 20:35:01 +0000372 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
373 TimersToPrint[i].print(Total, *OutStream);
374
375 Total.print(Total, *OutStream);
376 *OutStream << '\n';
377 OutStream->flush();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 }
Chris Lattnerc3546852010-03-29 20:35:01 +0000379 --NumTimers;
380
381 TimersToPrint.clear();
382
Chris Lattner5f52edc2010-03-29 21:24:52 +0000383 if (OutStream != &errs() && OutStream != &outs())
Chris Lattner626ca122010-03-29 20:40:19 +0000384 delete OutStream; // Close the file.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385}
386
Owen Anderson18463102009-06-23 20:52:29 +0000387void TimerGroup::addTimer() {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000388 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson18463102009-06-23 20:52:29 +0000389 ++NumTimers;
390}
391
392void TimerGroup::addTimerToPrint(const Timer &T) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000393 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson18463102009-06-23 20:52:29 +0000394 TimersToPrint.push_back(Timer(true, T));
395}
396