blob: 12b90324cd2e5ac0d039466bfd5af83c967332cc [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"
Chris Lattner7597f692010-03-29 21:28:41 +000016#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#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"
Chris Lattner7597f692010-03-29 21:28:41 +000020#include "llvm/System/Mutex.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/System/Process.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#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() {
Chris Lattnerc3546852010-03-29 20:35:01 +000057 TimerGroup *tmp = DefaultTimerGroup;
Owen Anderson1b2ea532009-06-23 17:33:37 +000058 sys::MemoryFence();
Chris Lattnerc3546852010-03-29 20:35:01 +000059 if (tmp) return tmp;
60
61 llvm_acquire_global_lock();
62 tmp = DefaultTimerGroup;
Owen Anderson1b2ea532009-06-23 17:33:37 +000063 if (!tmp) {
Chris Lattnerc3546852010-03-29 20:35:01 +000064 tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
65 sys::MemoryFence();
66 DefaultTimerGroup = tmp;
Owen Anderson1b2ea532009-06-23 17:33:37 +000067 }
Chris Lattnerc3546852010-03-29 20:35:01 +000068 llvm_release_global_lock();
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
Chris Lattnerc3546852010-03-29 20:35:01 +000073//===----------------------------------------------------------------------===//
74// Timer Implementation
75//===----------------------------------------------------------------------===//
76
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077Timer::Timer(const std::string &N)
Chris Lattner7597f692010-03-29 21:28:41 +000078 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), Name(N),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 Started(false), TG(getDefaultTimerGroup()) {
80 TG->addTimer();
81}
82
83Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner7597f692010-03-29 21:28:41 +000084 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), Name(N),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 Started(false), TG(&tg) {
86 TG->addTimer();
87}
88
89Timer::Timer(const Timer &T) {
90 TG = T.TG;
91 if (TG) TG->addTimer();
92 operator=(T);
93}
94
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095// Copy ctor, initialize with no TG member.
96Timer::Timer(bool, const Timer &T) {
97 TG = T.TG; // Avoid assertion in operator=
98 operator=(T); // Copy contents
99 TG = 0;
100}
101
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102Timer::~Timer() {
Chris Lattnerc3546852010-03-29 20:35:01 +0000103 if (!TG) return;
104
105 if (Started) {
106 Started = false;
107 TG->addTimerToPrint(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 }
Chris Lattnerc3546852010-03-29 20:35:01 +0000109 TG->removeTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110}
111
112static inline size_t getMemUsage() {
113 if (TrackSpace)
114 return sys::Process::GetMallocUsage();
115 return 0;
116}
117
118struct TimeRecord {
Owen Andersonac637c62009-06-23 20:17:22 +0000119 double Elapsed, UserTime, SystemTime;
120 ssize_t MemUsed;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121};
122
123static TimeRecord getTimeRecord(bool Start) {
124 TimeRecord Result;
125
126 sys::TimeValue now(0,0);
127 sys::TimeValue user(0,0);
128 sys::TimeValue sys(0,0);
129
Owen Andersonac637c62009-06-23 20:17:22 +0000130 ssize_t MemUsed = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 if (Start) {
132 MemUsed = getMemUsage();
Chris Lattnerc3546852010-03-29 20:35:01 +0000133 sys::Process::GetTimeUsage(now, user, sys);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 } else {
Chris Lattnerc3546852010-03-29 20:35:01 +0000135 sys::Process::GetTimeUsage(now, user, sys);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 MemUsed = getMemUsage();
137 }
138
Chris Lattnerc3546852010-03-29 20:35:01 +0000139 Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0;
140 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
141 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
142 Result.MemUsed = MemUsed;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143 return Result;
144}
145
146static ManagedStatic<std::vector<Timer*> > ActiveTimers;
147
148void Timer::startTimer() {
149 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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156}
157
158void Timer::stopTimer() {
159 TimeRecord TR = getTimeRecord(false);
160 Elapsed += TR.Elapsed;
161 UserTime += TR.UserTime;
162 SystemTime += TR.SystemTime;
163 MemUsed += TR.MemUsed;
164
165 if (ActiveTimers->back() == this) {
166 ActiveTimers->pop_back();
167 } else {
168 std::vector<Timer*>::iterator I =
169 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
170 assert(I != ActiveTimers->end() && "stop but no startTimer?");
171 ActiveTimers->erase(I);
172 }
173}
174
175void Timer::sum(const Timer &T) {
176 Elapsed += T.Elapsed;
177 UserTime += T.UserTime;
178 SystemTime += T.SystemTime;
179 MemUsed += T.MemUsed;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180}
181
Chris Lattner5f52edc2010-03-29 21:24:52 +0000182const Timer &Timer::operator=(const Timer &T) {
183 Elapsed = T.Elapsed;
184 UserTime = T.UserTime;
185 SystemTime = T.SystemTime;
186 MemUsed = T.MemUsed;
Chris Lattner5f52edc2010-03-29 21:24:52 +0000187 Name = T.Name;
188 Started = T.Started;
189 assert(TG == T.TG && "Can only assign timers in the same TimerGroup!");
190 return *this;
191}
192
193
Chris Lattnerc3546852010-03-29 20:35:01 +0000194static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner626ca122010-03-29 20:40:19 +0000195 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerc3546852010-03-29 20:35:01 +0000196 OS << " ----- ";
197 else {
198 OS << " " << format("%7.4f", Val) << " (";
199 OS << format("%5.1f", Val*100/Total) << "%)";
200 }
201}
202
203void Timer::print(const Timer &Total, raw_ostream &OS) {
Chris Lattnerc3546852010-03-29 20:35:01 +0000204 if (Total.UserTime)
205 printVal(UserTime, Total.UserTime, OS);
206 if (Total.SystemTime)
207 printVal(SystemTime, Total.SystemTime, OS);
208 if (Total.getProcessTime())
209 printVal(getProcessTime(), Total.getProcessTime(), OS);
210 printVal(Elapsed, Total.Elapsed, OS);
211
212 OS << " ";
213
Chris Lattner5f52edc2010-03-29 21:24:52 +0000214 if (Total.MemUsed)
Chris Lattnerc3546852010-03-29 20:35:01 +0000215 OS << format("%9lld", (long long)MemUsed) << " ";
Chris Lattner5f52edc2010-03-29 21:24:52 +0000216
Chris Lattnerc3546852010-03-29 20:35:01 +0000217 OS << Name << "\n";
218
219 Started = false; // Once printed, don't print again
220}
221
222
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223//===----------------------------------------------------------------------===//
224// NamedRegionTimer Implementation
225//===----------------------------------------------------------------------===//
226
Dan Gohman368a08b2008-07-14 18:19:29 +0000227typedef std::map<std::string, Timer> Name2Timer;
228typedef std::map<std::string, std::pair<TimerGroup, Name2Timer> > Name2Pair;
229
Dan Gohman368a08b2008-07-14 18:19:29 +0000230static ManagedStatic<Name2Timer> NamedTimers;
Dan Gohman368a08b2008-07-14 18:19:29 +0000231static ManagedStatic<Name2Pair> NamedGroupedTimers;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232
233static Timer &getNamedRegionTimer(const std::string &Name) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000234 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman368a08b2008-07-14 18:19:29 +0000235 Name2Timer::iterator I = NamedTimers->find(Name);
Dan Gohmanb261a982008-07-11 20:58:19 +0000236 if (I != NamedTimers->end())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 return I->second;
238
239 return NamedTimers->insert(I, std::make_pair(Name, Timer(Name)))->second;
240}
241
Dan Gohman368a08b2008-07-14 18:19:29 +0000242static Timer &getNamedRegionTimer(const std::string &Name,
243 const std::string &GroupName) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000244 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman368a08b2008-07-14 18:19:29 +0000245
246 Name2Pair::iterator I = NamedGroupedTimers->find(GroupName);
247 if (I == NamedGroupedTimers->end()) {
248 TimerGroup TG(GroupName);
249 std::pair<TimerGroup, Name2Timer> Pair(TG, Name2Timer());
250 I = NamedGroupedTimers->insert(I, std::make_pair(GroupName, Pair));
251 }
252
253 Name2Timer::iterator J = I->second.second.find(Name);
254 if (J == I->second.second.end())
255 J = I->second.second.insert(J,
256 std::make_pair(Name,
257 Timer(Name,
258 I->second.first)));
259
260 return J->second;
261}
262
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263NamedRegionTimer::NamedRegionTimer(const std::string &Name)
264 : TimeRegion(getNamedRegionTimer(Name)) {}
265
Dan Gohman368a08b2008-07-14 18:19:29 +0000266NamedRegionTimer::NamedRegionTimer(const std::string &Name,
267 const std::string &GroupName)
268 : TimeRegion(getNamedRegionTimer(Name, GroupName)) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269
270//===----------------------------------------------------------------------===//
271// TimerGroup Implementation
272//===----------------------------------------------------------------------===//
273
Chris Lattner626ca122010-03-29 20:40:19 +0000274// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattner5f52edc2010-03-29 21:24:52 +0000275raw_ostream *llvm::GetLibSupportInfoOutputFile() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
277 if (LibSupportInfoOutputFilename.empty())
Chris Lattner5febcae2009-08-23 08:43:55 +0000278 return &errs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 if (LibSupportInfoOutputFilename == "-")
Chris Lattner5febcae2009-08-23 08:43:55 +0000280 return &outs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281
Chris Lattner5febcae2009-08-23 08:43:55 +0000282 std::string Error;
283 raw_ostream *Result = new raw_fd_ostream(LibSupportInfoOutputFilename.c_str(),
284 Error, raw_fd_ostream::F_Append);
285 if (Error.empty())
286 return Result;
Mikhail Glushenkov05858c52009-11-07 06:33:12 +0000287
Chris Lattner5febcae2009-08-23 08:43:55 +0000288 errs() << "Error opening info-output-file '"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 << LibSupportInfoOutputFilename << " for appending!\n";
Chris Lattner5febcae2009-08-23 08:43:55 +0000290 delete Result;
291 return &errs();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292}
293
294
295void TimerGroup::removeTimer() {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000296 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattnerc3546852010-03-29 20:35:01 +0000297 if (--NumTimers != 0 || TimersToPrint.empty())
298 return; // Don't print timing report.
299
300 // Sort the timers in descending order by amount of time taken.
301 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
302 std::greater<Timer>());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303
Chris Lattnerc3546852010-03-29 20:35:01 +0000304 // Figure out how many spaces to indent TimerGroup name.
305 unsigned Padding = (80-Name.length())/2;
306 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307
Chris Lattnerc3546852010-03-29 20:35:01 +0000308 raw_ostream *OutStream = GetLibSupportInfoOutputFile();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309
Chris Lattnerc3546852010-03-29 20:35:01 +0000310 ++NumTimers;
Chris Lattner626ca122010-03-29 20:40:19 +0000311 { // Scope to contain Total timer: don't allow total timer to drop us to
312 // zero timers.
Chris Lattnerc3546852010-03-29 20:35:01 +0000313 Timer Total("TOTAL");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314
Chris Lattnerc3546852010-03-29 20:35:01 +0000315 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
316 Total.sum(TimersToPrint[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317
Chris Lattner626ca122010-03-29 20:40:19 +0000318 // Print out timing header.
Chris Lattnerc3546852010-03-29 20:35:01 +0000319 *OutStream << "===" << std::string(73, '-') << "===\n"
320 << std::string(Padding, ' ') << Name << "\n"
321 << "===" << std::string(73, '-')
322 << "===\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323
Chris Lattnerc3546852010-03-29 20:35:01 +0000324 // If this is not an collection of ungrouped times, print the total time.
325 // Ungrouped timers don't really make sense to add up. We still print the
326 // TOTAL line to make the percentages make sense.
327 if (this != DefaultTimerGroup) {
328 *OutStream << " Total Execution Time: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329
Chris Lattnerc3546852010-03-29 20:35:01 +0000330 *OutStream << format("%5.4f", Total.getProcessTime()) << " seconds (";
331 *OutStream << format("%5.4f", Total.getWallTime()) << " wall clock)\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 }
Chris Lattnerc3546852010-03-29 20:35:01 +0000333 *OutStream << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334
Chris Lattnerc3546852010-03-29 20:35:01 +0000335 if (Total.UserTime)
336 *OutStream << " ---User Time---";
337 if (Total.SystemTime)
338 *OutStream << " --System Time--";
339 if (Total.getProcessTime())
340 *OutStream << " --User+System--";
341 *OutStream << " ---Wall Time---";
342 if (Total.getMemUsed())
343 *OutStream << " ---Mem---";
Chris Lattnerc3546852010-03-29 20:35:01 +0000344 *OutStream << " --- Name ---\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345
Chris Lattner626ca122010-03-29 20:40:19 +0000346 // Loop through all of the timing data, printing it out.
Chris Lattnerc3546852010-03-29 20:35:01 +0000347 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
348 TimersToPrint[i].print(Total, *OutStream);
349
350 Total.print(Total, *OutStream);
351 *OutStream << '\n';
352 OutStream->flush();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 }
Chris Lattnerc3546852010-03-29 20:35:01 +0000354 --NumTimers;
355
356 TimersToPrint.clear();
357
Chris Lattner5f52edc2010-03-29 21:24:52 +0000358 if (OutStream != &errs() && OutStream != &outs())
Chris Lattner626ca122010-03-29 20:40:19 +0000359 delete OutStream; // Close the file.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360}
361
Owen Anderson18463102009-06-23 20:52:29 +0000362void TimerGroup::addTimer() {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000363 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson18463102009-06-23 20:52:29 +0000364 ++NumTimers;
365}
366
367void TimerGroup::addTimerToPrint(const Timer &T) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000368 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson18463102009-06-23 20:52:29 +0000369 TimersToPrint.push_back(Timer(true, T));
370}
371