blob: 7d32ee66c2bfdb3b9aa5bef1ed95ac8f70c41d32 [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
14#include "llvm/Support/Timer.h"
15#include "llvm/Support/CommandLine.h"
16#include "llvm/Support/ManagedStatic.h"
Chris Lattner5febcae2009-08-23 08:43:55 +000017#include "llvm/Support/raw_ostream.h"
18#include "llvm/Support/Format.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/System/Process.h"
20#include <algorithm>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include <functional>
22#include <map>
23using namespace llvm;
24
25// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattner5febcae2009-08-23 08:43:55 +000026namespace llvm { extern raw_ostream *GetLibSupportInfoOutputFile(); }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027
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.
36static ManagedStatic<std::string> LibSupportInfoOutputFilename;
37static std::string &getLibSupportInfoOutputFilename() {
38 return *LibSupportInfoOutputFilename;
39}
40
Owen Anderson18463102009-06-23 20:52:29 +000041static ManagedStatic<sys::SmartMutex<true> > TimerLock;
42
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043namespace {
Dan Gohmanbbe69222008-04-23 23:15:23 +000044 static cl::opt<bool>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
46 "tracking (this may be slow)"),
47 cl::Hidden);
48
Dan Gohmanbbe69222008-04-23 23:15:23 +000049 static cl::opt<std::string, true>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
51 cl::desc("File to append -stats and -timer output to"),
52 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
53}
54
Owen Anderson56ddb832009-06-23 16:36:10 +000055static TimerGroup *DefaultTimerGroup = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056static TimerGroup *getDefaultTimerGroup() {
Owen Anderson1b2ea532009-06-23 17:33:37 +000057 TimerGroup* tmp = DefaultTimerGroup;
58 sys::MemoryFence();
59 if (!tmp) {
60 llvm_acquire_global_lock();
61 tmp = DefaultTimerGroup;
62 if (!tmp) {
63 tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
64 sys::MemoryFence();
65 DefaultTimerGroup = tmp;
66 }
67 llvm_release_global_lock();
68 }
Mikhail Glushenkov05858c52009-11-07 06:33:12 +000069
Owen Anderson1b2ea532009-06-23 17:33:37 +000070 return tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071}
72
73Timer::Timer(const std::string &N)
74 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
75 Started(false), TG(getDefaultTimerGroup()) {
76 TG->addTimer();
77}
78
79Timer::Timer(const std::string &N, TimerGroup &tg)
80 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
81 Started(false), TG(&tg) {
82 TG->addTimer();
83}
84
85Timer::Timer(const Timer &T) {
86 TG = T.TG;
87 if (TG) TG->addTimer();
88 operator=(T);
89}
90
91
92// Copy ctor, initialize with no TG member.
93Timer::Timer(bool, const Timer &T) {
94 TG = T.TG; // Avoid assertion in operator=
95 operator=(T); // Copy contents
96 TG = 0;
97}
98
99
100Timer::~Timer() {
101 if (TG) {
102 if (Started) {
103 Started = false;
104 TG->addTimerToPrint(*this);
105 }
106 TG->removeTimer();
107 }
108}
109
110static inline size_t getMemUsage() {
111 if (TrackSpace)
112 return sys::Process::GetMallocUsage();
113 return 0;
114}
115
116struct TimeRecord {
Owen Andersonac637c62009-06-23 20:17:22 +0000117 double Elapsed, UserTime, SystemTime;
118 ssize_t MemUsed;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119};
120
121static TimeRecord getTimeRecord(bool Start) {
122 TimeRecord Result;
123
124 sys::TimeValue now(0,0);
125 sys::TimeValue user(0,0);
126 sys::TimeValue sys(0,0);
127
Owen Andersonac637c62009-06-23 20:17:22 +0000128 ssize_t MemUsed = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 if (Start) {
130 MemUsed = getMemUsage();
131 sys::Process::GetTimeUsage(now,user,sys);
132 } else {
133 sys::Process::GetTimeUsage(now,user,sys);
134 MemUsed = getMemUsage();
135 }
136
Owen Andersonac637c62009-06-23 20:17:22 +0000137 Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0;
138 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
139 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 Result.MemUsed = MemUsed;
141
142 return Result;
143}
144
145static ManagedStatic<std::vector<Timer*> > ActiveTimers;
146
147void Timer::startTimer() {
Owen Anderson6171d2b2009-11-17 07:06:10 +0000148 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 Started = true;
Dan Gohmanf6930f92008-06-24 22:07:07 +0000150 ActiveTimers->push_back(this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 TimeRecord TR = getTimeRecord(true);
152 Elapsed -= TR.Elapsed;
153 UserTime -= TR.UserTime;
154 SystemTime -= TR.SystemTime;
155 MemUsed -= TR.MemUsed;
156 PeakMemBase = TR.MemUsed;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157}
158
159void Timer::stopTimer() {
Owen Anderson6171d2b2009-11-17 07:06:10 +0000160 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 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
185/// addPeakMemoryMeasurement - This method should be called whenever memory
186/// usage needs to be checked. It adds a peak memory measurement to the
187/// currently active timers, which will be printed when the timer group prints
188///
189void Timer::addPeakMemoryMeasurement() {
Owen Anderson6171d2b2009-11-17 07:06:10 +0000190 sys::SmartScopedLock<true> L(*TimerLock);
Owen Andersonac637c62009-06-23 20:17:22 +0000191 size_t MemUsed = getMemUsage();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192
193 for (std::vector<Timer*>::iterator I = ActiveTimers->begin(),
Owen Anderson6171d2b2009-11-17 07:06:10 +0000194 E = ActiveTimers->end(); I != E; ++I)
Owen Andersonac637c62009-06-23 20:17:22 +0000195 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196}
197
198//===----------------------------------------------------------------------===//
199// NamedRegionTimer Implementation
200//===----------------------------------------------------------------------===//
201
Dan Gohman368a08b2008-07-14 18:19:29 +0000202namespace {
203
204typedef std::map<std::string, Timer> Name2Timer;
205typedef std::map<std::string, std::pair<TimerGroup, Name2Timer> > Name2Pair;
206
207}
208
209static ManagedStatic<Name2Timer> NamedTimers;
210
211static ManagedStatic<Name2Pair> NamedGroupedTimers;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212
213static Timer &getNamedRegionTimer(const std::string &Name) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000214 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman368a08b2008-07-14 18:19:29 +0000215 Name2Timer::iterator I = NamedTimers->find(Name);
Dan Gohmanb261a982008-07-11 20:58:19 +0000216 if (I != NamedTimers->end())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 return I->second;
218
219 return NamedTimers->insert(I, std::make_pair(Name, Timer(Name)))->second;
220}
221
Dan Gohman368a08b2008-07-14 18:19:29 +0000222static Timer &getNamedRegionTimer(const std::string &Name,
223 const std::string &GroupName) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000224 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman368a08b2008-07-14 18:19:29 +0000225
226 Name2Pair::iterator I = NamedGroupedTimers->find(GroupName);
227 if (I == NamedGroupedTimers->end()) {
228 TimerGroup TG(GroupName);
229 std::pair<TimerGroup, Name2Timer> Pair(TG, Name2Timer());
230 I = NamedGroupedTimers->insert(I, std::make_pair(GroupName, Pair));
231 }
232
233 Name2Timer::iterator J = I->second.second.find(Name);
234 if (J == I->second.second.end())
235 J = I->second.second.insert(J,
236 std::make_pair(Name,
237 Timer(Name,
238 I->second.first)));
239
240 return J->second;
241}
242
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243NamedRegionTimer::NamedRegionTimer(const std::string &Name)
244 : TimeRegion(getNamedRegionTimer(Name)) {}
245
Dan Gohman368a08b2008-07-14 18:19:29 +0000246NamedRegionTimer::NamedRegionTimer(const std::string &Name,
247 const std::string &GroupName)
248 : TimeRegion(getNamedRegionTimer(Name, GroupName)) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249
250//===----------------------------------------------------------------------===//
251// TimerGroup Implementation
252//===----------------------------------------------------------------------===//
253
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254
Chris Lattner5febcae2009-08-23 08:43:55 +0000255static void printVal(double Val, double Total, raw_ostream &OS) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 if (Total < 1e-7) // Avoid dividing by zero...
257 OS << " ----- ";
258 else {
Chris Lattner5febcae2009-08-23 08:43:55 +0000259 OS << " " << format("%7.4f", Val) << " (";
260 OS << format("%5.1f", Val*100/Total) << "%)";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 }
262}
263
Chris Lattner5febcae2009-08-23 08:43:55 +0000264void Timer::print(const Timer &Total, raw_ostream &OS) {
Owen Anderson6171d2b2009-11-17 07:06:10 +0000265 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 if (Total.UserTime)
Owen Andersonac637c62009-06-23 20:17:22 +0000267 printVal(UserTime, Total.UserTime, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 if (Total.SystemTime)
Owen Andersonac637c62009-06-23 20:17:22 +0000269 printVal(SystemTime, Total.SystemTime, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 if (Total.getProcessTime())
Owen Andersonac637c62009-06-23 20:17:22 +0000271 printVal(getProcessTime(), Total.getProcessTime(), OS);
272 printVal(Elapsed, Total.Elapsed, OS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273
274 OS << " ";
275
276 if (Total.MemUsed) {
Chris Lattner5febcae2009-08-23 08:43:55 +0000277 OS << format("%9lld", (long long)MemUsed) << " ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 }
279 if (Total.PeakMem) {
280 if (PeakMem) {
Chris Lattner5febcae2009-08-23 08:43:55 +0000281 OS << format("%9lld", (long long)PeakMem) << " ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282 } else
283 OS << " ";
284 }
285 OS << Name << "\n";
286
287 Started = false; // Once printed, don't print again
288}
289
290// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
Chris Lattner5febcae2009-08-23 08:43:55 +0000291raw_ostream *
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292llvm::GetLibSupportInfoOutputFile() {
293 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
294 if (LibSupportInfoOutputFilename.empty())
Chris Lattner5febcae2009-08-23 08:43:55 +0000295 return &errs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 if (LibSupportInfoOutputFilename == "-")
Chris Lattner5febcae2009-08-23 08:43:55 +0000297 return &outs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298
Mikhail Glushenkov05858c52009-11-07 06:33:12 +0000299
Chris Lattner5febcae2009-08-23 08:43:55 +0000300 std::string Error;
301 raw_ostream *Result = new raw_fd_ostream(LibSupportInfoOutputFilename.c_str(),
302 Error, raw_fd_ostream::F_Append);
303 if (Error.empty())
304 return Result;
Mikhail Glushenkov05858c52009-11-07 06:33:12 +0000305
Chris Lattner5febcae2009-08-23 08:43:55 +0000306 errs() << "Error opening info-output-file '"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 << LibSupportInfoOutputFilename << " for appending!\n";
Chris Lattner5febcae2009-08-23 08:43:55 +0000308 delete Result;
309 return &errs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310}
311
312
313void TimerGroup::removeTimer() {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000314 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 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 Lattner5febcae2009-08-23 08:43:55 +0000324 raw_ostream *OutStream = GetLibSupportInfoOutputFile();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325
326 ++NumTimers;
327 { // Scope to contain Total timer... don't allow total timer to drop us to
328 // zero timers...
329 Timer Total("TOTAL");
330
331 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
332 Total.sum(TimersToPrint[i]);
333
334 // Print out timing header...
335 *OutStream << "===" << std::string(73, '-') << "===\n"
336 << std::string(Padding, ' ') << Name << "\n"
337 << "===" << std::string(73, '-')
338 << "===\n";
339
340 // 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.
Owen Anderson56ddb832009-06-23 16:36:10 +0000343 if (this != DefaultTimerGroup) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 *OutStream << " Total Execution Time: ";
345
Chris Lattner5febcae2009-08-23 08:43:55 +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 }
349 *OutStream << "\n";
350
Owen Andersonac637c62009-06-23 20:17:22 +0000351 if (Total.UserTime)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 *OutStream << " ---User Time---";
Owen Andersonac637c62009-06-23 20:17:22 +0000353 if (Total.SystemTime)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 *OutStream << " --System Time--";
Owen Andersonac637c62009-06-23 20:17:22 +0000355 if (Total.getProcessTime())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 *OutStream << " --User+System--";
357 *OutStream << " ---Wall Time---";
Owen Andersonac637c62009-06-23 20:17:22 +0000358 if (Total.getMemUsed())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 *OutStream << " ---Mem---";
Owen Andersonac637c62009-06-23 20:17:22 +0000360 if (Total.getPeakMem())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 *OutStream << " -PeakMem-";
362 *OutStream << " --- Name ---\n";
363
364 // Loop through all of the timing data, printing it out...
365 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
366 TimersToPrint[i].print(Total, *OutStream);
367
368 Total.print(Total, *OutStream);
Chris Lattner5febcae2009-08-23 08:43:55 +0000369 *OutStream << '\n';
370 OutStream->flush();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371 }
372 --NumTimers;
373
374 TimersToPrint.clear();
375
Chris Lattner5febcae2009-08-23 08:43:55 +0000376 if (OutStream != &errs() && OutStream != &outs())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 delete OutStream; // Close the file...
378 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379}
380
Owen Anderson18463102009-06-23 20:52:29 +0000381void TimerGroup::addTimer() {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000382 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson18463102009-06-23 20:52:29 +0000383 ++NumTimers;
384}
385
386void TimerGroup::addTimerToPrint(const Timer &T) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000387 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson18463102009-06-23 20:52:29 +0000388 TimersToPrint.push_back(Timer(true, T));
389}
390