blob: 317d5ac9c54f6629fa3c901ab29096e63bf33fa5 [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
Chris Lattner7641c7b2010-03-29 21:34:06 +000055// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
56raw_ostream *llvm::GetLibSupportInfoOutputFile() {
57 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
58 if (LibSupportInfoOutputFilename.empty())
59 return &errs();
60 if (LibSupportInfoOutputFilename == "-")
61 return &outs();
62
63 std::string Error;
64 raw_ostream *Result = new raw_fd_ostream(LibSupportInfoOutputFilename.c_str(),
65 Error, raw_fd_ostream::F_Append);
66 if (Error.empty())
67 return Result;
68
69 errs() << "Error opening info-output-file '"
70 << LibSupportInfoOutputFilename << " for appending!\n";
71 delete Result;
72 return &errs();
73}
74
75
Owen Anderson56ddb832009-06-23 16:36:10 +000076static TimerGroup *DefaultTimerGroup = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077static TimerGroup *getDefaultTimerGroup() {
Chris Lattnerc3546852010-03-29 20:35:01 +000078 TimerGroup *tmp = DefaultTimerGroup;
Owen Anderson1b2ea532009-06-23 17:33:37 +000079 sys::MemoryFence();
Chris Lattnerc3546852010-03-29 20:35:01 +000080 if (tmp) return tmp;
81
82 llvm_acquire_global_lock();
83 tmp = DefaultTimerGroup;
Owen Anderson1b2ea532009-06-23 17:33:37 +000084 if (!tmp) {
Chris Lattnerc3546852010-03-29 20:35:01 +000085 tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
86 sys::MemoryFence();
87 DefaultTimerGroup = tmp;
Owen Anderson1b2ea532009-06-23 17:33:37 +000088 }
Chris Lattnerc3546852010-03-29 20:35:01 +000089 llvm_release_global_lock();
Mikhail Glushenkov05858c52009-11-07 06:33:12 +000090
Owen Anderson1b2ea532009-06-23 17:33:37 +000091 return tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092}
93
Chris Lattnerc3546852010-03-29 20:35:01 +000094//===----------------------------------------------------------------------===//
95// Timer Implementation
96//===----------------------------------------------------------------------===//
97
Chris Lattner1b53d742010-03-30 03:57:00 +000098Timer::Timer(const std::string &N)
99 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), Name(N),
100 Started(false), TG(getDefaultTimerGroup()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 TG->addTimer();
102}
103
Chris Lattner1b53d742010-03-30 03:57:00 +0000104Timer::Timer(const std::string &N, TimerGroup &tg)
105 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), Name(N),
106 Started(false), TG(&tg) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 TG->addTimer();
108}
109
Chris Lattner1b53d742010-03-30 03:57:00 +0000110Timer::Timer(const Timer &T) {
111 TG = T.TG;
112 if (TG) TG->addTimer();
113 operator=(T);
114}
115
116// Copy ctor, initialize with no TG member.
117Timer::Timer(bool, const Timer &T) {
118 TG = T.TG; // Avoid assertion in operator=
119 operator=(T); // Copy contents
120 TG = 0;
121}
122
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123Timer::~Timer() {
Chris Lattner1b53d742010-03-30 03:57:00 +0000124 if (!TG) return;
Chris Lattnerc3546852010-03-29 20:35:01 +0000125
126 if (Started) {
127 Started = false;
Chris Lattner1b53d742010-03-30 03:57:00 +0000128 TG->addTimerToPrint(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 }
Chris Lattnerc3546852010-03-29 20:35:01 +0000130 TG->removeTimer();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131}
132
133static inline size_t getMemUsage() {
134 if (TrackSpace)
135 return sys::Process::GetMallocUsage();
136 return 0;
137}
138
Chris Lattner1b53d742010-03-30 03:57:00 +0000139struct TimeRecord {
140 double Elapsed, UserTime, SystemTime;
141 ssize_t MemUsed;
142};
143
144static TimeRecord getTimeRecord(bool Start) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 TimeRecord Result;
146
147 sys::TimeValue now(0,0);
148 sys::TimeValue user(0,0);
149 sys::TimeValue sys(0,0);
150
Owen Andersonac637c62009-06-23 20:17:22 +0000151 ssize_t MemUsed = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 if (Start) {
153 MemUsed = getMemUsage();
Chris Lattnerc3546852010-03-29 20:35:01 +0000154 sys::Process::GetTimeUsage(now, user, sys);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 } else {
Chris Lattnerc3546852010-03-29 20:35:01 +0000156 sys::Process::GetTimeUsage(now, user, sys);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 MemUsed = getMemUsage();
158 }
159
Chris Lattner1b53d742010-03-30 03:57:00 +0000160 Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0;
Chris Lattnerc3546852010-03-29 20:35:01 +0000161 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
Chris Lattner1b53d742010-03-30 03:57:00 +0000162 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
Chris Lattnerc3546852010-03-29 20:35:01 +0000163 Result.MemUsed = MemUsed;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 return Result;
165}
166
167static ManagedStatic<std::vector<Timer*> > ActiveTimers;
168
169void Timer::startTimer() {
170 Started = true;
Dan Gohmanf6930f92008-06-24 22:07:07 +0000171 ActiveTimers->push_back(this);
Chris Lattner1b53d742010-03-30 03:57:00 +0000172 TimeRecord TR = getTimeRecord(true);
173 Elapsed -= TR.Elapsed;
174 UserTime -= TR.UserTime;
175 SystemTime -= TR.SystemTime;
176 MemUsed -= TR.MemUsed;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177}
178
179void Timer::stopTimer() {
Chris Lattner1b53d742010-03-30 03:57:00 +0000180 TimeRecord TR = getTimeRecord(false);
181 Elapsed += TR.Elapsed;
182 UserTime += TR.UserTime;
183 SystemTime += TR.SystemTime;
184 MemUsed += TR.MemUsed;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185
186 if (ActiveTimers->back() == this) {
187 ActiveTimers->pop_back();
188 } else {
189 std::vector<Timer*>::iterator I =
190 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
191 assert(I != ActiveTimers->end() && "stop but no startTimer?");
192 ActiveTimers->erase(I);
193 }
194}
195
Chris Lattner1b53d742010-03-30 03:57:00 +0000196void Timer::sum(const Timer &T) {
197 Elapsed += T.Elapsed;
198 UserTime += T.UserTime;
199 SystemTime += T.SystemTime;
200 MemUsed += T.MemUsed;
201}
202
203const Timer &Timer::operator=(const Timer &T) {
204 Elapsed = T.Elapsed;
205 UserTime = T.UserTime;
206 SystemTime = T.SystemTime;
207 MemUsed = T.MemUsed;
208 Name = T.Name;
209 Started = T.Started;
210 assert(TG == T.TG && "Can only assign timers in the same TimerGroup!");
211 return *this;
212}
213
214
Chris Lattnerc3546852010-03-29 20:35:01 +0000215static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner626ca122010-03-29 20:40:19 +0000216 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerc3546852010-03-29 20:35:01 +0000217 OS << " ----- ";
218 else {
219 OS << " " << format("%7.4f", Val) << " (";
220 OS << format("%5.1f", Val*100/Total) << "%)";
221 }
222}
223
Chris Lattner1b53d742010-03-30 03:57:00 +0000224void Timer::print(const Timer &Total, raw_ostream &OS) {
225 if (Total.UserTime)
226 printVal(UserTime, Total.UserTime, OS);
227 if (Total.SystemTime)
228 printVal(SystemTime, Total.SystemTime, OS);
Chris Lattnerc3546852010-03-29 20:35:01 +0000229 if (Total.getProcessTime())
230 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattner1b53d742010-03-30 03:57:00 +0000231 printVal(Elapsed, Total.Elapsed, OS);
Chris Lattnerc3546852010-03-29 20:35:01 +0000232
233 OS << " ";
234
Chris Lattner1b53d742010-03-30 03:57:00 +0000235 if (Total.MemUsed)
236 OS << format("%9lld", (long long)MemUsed) << " ";
237
238 OS << Name << "\n";
239
240 Started = false; // Once printed, don't print again
Chris Lattnerc3546852010-03-29 20:35:01 +0000241}
242
243
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244//===----------------------------------------------------------------------===//
245// NamedRegionTimer Implementation
246//===----------------------------------------------------------------------===//
247
Chris Lattner1b53d742010-03-30 03:57:00 +0000248typedef std::map<std::string, Timer> Name2Timer;
249typedef std::map<std::string, std::pair<TimerGroup, Name2Timer> > Name2Pair;
Dan Gohman368a08b2008-07-14 18:19:29 +0000250
Chris Lattner1b53d742010-03-30 03:57:00 +0000251static ManagedStatic<Name2Timer> NamedTimers;
252static ManagedStatic<Name2Pair> NamedGroupedTimers;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253
254static Timer &getNamedRegionTimer(const std::string &Name) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000255 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner1b53d742010-03-30 03:57:00 +0000256 Name2Timer::iterator I = NamedTimers->find(Name);
257 if (I != NamedTimers->end())
258 return I->second;
259
260 return NamedTimers->insert(I, std::make_pair(Name, Timer(Name)))->second;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261}
262
Dan Gohman368a08b2008-07-14 18:19:29 +0000263static Timer &getNamedRegionTimer(const std::string &Name,
264 const std::string &GroupName) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000265 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman368a08b2008-07-14 18:19:29 +0000266
Chris Lattner1b53d742010-03-30 03:57:00 +0000267 Name2Pair::iterator I = NamedGroupedTimers->find(GroupName);
268 if (I == NamedGroupedTimers->end()) {
269 TimerGroup TG(GroupName);
270 std::pair<TimerGroup, Name2Timer> Pair(TG, Name2Timer());
271 I = NamedGroupedTimers->insert(I, std::make_pair(GroupName, Pair));
272 }
Dan Gohman368a08b2008-07-14 18:19:29 +0000273
Chris Lattner1b53d742010-03-30 03:57:00 +0000274 Name2Timer::iterator J = I->second.second.find(Name);
275 if (J == I->second.second.end())
276 J = I->second.second.insert(J,
277 std::make_pair(Name,
278 Timer(Name,
279 I->second.first)));
Dan Gohman368a08b2008-07-14 18:19:29 +0000280
Chris Lattner1b53d742010-03-30 03:57:00 +0000281 return J->second;
Dan Gohman368a08b2008-07-14 18:19:29 +0000282}
283
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284NamedRegionTimer::NamedRegionTimer(const std::string &Name)
285 : TimeRegion(getNamedRegionTimer(Name)) {}
286
Dan Gohman368a08b2008-07-14 18:19:29 +0000287NamedRegionTimer::NamedRegionTimer(const std::string &Name,
288 const std::string &GroupName)
289 : TimeRegion(getNamedRegionTimer(Name, GroupName)) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290
291//===----------------------------------------------------------------------===//
292// TimerGroup Implementation
293//===----------------------------------------------------------------------===//
294
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295void 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.
Chris Lattner1b53d742010-03-30 03:57:00 +0000301 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 Lattner1b53d742010-03-30 03:57:00 +0000310 ++NumTimers;
311 { // Scope to contain Total timer: don't allow total timer to drop us to
312 // zero timers.
313 Timer Total("TOTAL");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314
Chris Lattner1b53d742010-03-30 03:57:00 +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 Lattner1b53d742010-03-30 03:57:00 +0000318 // Print out timing header.
319 *OutStream << "===" << std::string(73, '-') << "===\n"
320 << std::string(Padding, ' ') << Name << "\n"
321 << "===" << std::string(73, '-')
322 << "===\n";
323
324 // 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: ";
329 *OutStream << format("%5.4f", Total.getProcessTime()) << " seconds (";
330 *OutStream << format("%5.4f", Total.getWallTime()) << " wall clock)\n";
331 }
332 *OutStream << "\n";
333
334 if (Total.UserTime)
335 *OutStream << " ---User Time---";
336 if (Total.SystemTime)
337 *OutStream << " --System Time--";
338 if (Total.getProcessTime())
339 *OutStream << " --User+System--";
340 *OutStream << " ---Wall Time---";
341 if (Total.getMemUsed())
342 *OutStream << " ---Mem---";
343 *OutStream << " --- Name ---\n";
344
345 // Loop through all of the timing data, printing it out.
346 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
347 TimersToPrint[i].print(Total, *OutStream);
348
349 Total.print(Total, *OutStream);
350 *OutStream << '\n';
351 OutStream->flush();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 }
Chris Lattner1b53d742010-03-30 03:57:00 +0000353 --NumTimers;
Chris Lattnerc3546852010-03-29 20:35:01 +0000354
355 TimersToPrint.clear();
356
Chris Lattner5f52edc2010-03-29 21:24:52 +0000357 if (OutStream != &errs() && OutStream != &outs())
Chris Lattner626ca122010-03-29 20:40:19 +0000358 delete OutStream; // Close the file.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359}
360
Owen Anderson18463102009-06-23 20:52:29 +0000361void TimerGroup::addTimer() {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000362 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson18463102009-06-23 20:52:29 +0000363 ++NumTimers;
364}
365
Chris Lattner1b53d742010-03-30 03:57:00 +0000366void TimerGroup::addTimerToPrint(const Timer &T) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000367 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner1b53d742010-03-30 03:57:00 +0000368 TimersToPrint.push_back(Timer(true, T));
Owen Anderson18463102009-06-23 20:52:29 +0000369}
370