blob: 317d5ac9c54f6629fa3c901ab29096e63bf33fa5 [file] [log] [blame]
Chris Lattner6c38a792002-10-01 19:36:54 +00001//===-- Timer.cpp - Interval Timing Support -------------------------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner6c38a792002-10-01 19:36:54 +00009//
10// Interval Timing implementation.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer551ccae2004-09-01 22:55:40 +000014#include "llvm/Support/Timer.h"
15#include "llvm/Support/CommandLine.h"
Chris Lattnerfc86c3c2010-03-29 21:28:41 +000016#include "llvm/Support/Debug.h"
Chris Lattner90aa8392006-10-04 21:52:35 +000017#include "llvm/Support/ManagedStatic.h"
Chris Lattnerd9ea85a2009-08-23 08:43:55 +000018#include "llvm/Support/raw_ostream.h"
19#include "llvm/Support/Format.h"
Chris Lattnerfc86c3c2010-03-29 21:28:41 +000020#include "llvm/System/Mutex.h"
Reid Spencerdf52c9a2004-12-20 00:59:04 +000021#include "llvm/System/Process.h"
Chris Lattnerb4db5f32004-06-07 19:34:51 +000022#include <map>
Chris Lattnerb6d465f2003-12-14 21:27:33 +000023using namespace llvm;
Chris Lattnerf205fec2003-05-09 20:05:44 +000024
Chris Lattnerb4db5f32004-06-07 19:34:51 +000025// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattnerd9ea85a2009-08-23 08:43:55 +000026namespace llvm { extern raw_ostream *GetLibSupportInfoOutputFile(); }
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattner71336a92003-07-31 19:38:34 +000028// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
29// of constructor/destructor ordering being unspecified by C++. Basically the
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000030// problem is that a Statistic object gets destroyed, which ends up calling
Chris Lattner71336a92003-07-31 19:38:34 +000031// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
32// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
Reid Spencerf6e5a252004-12-14 03:55:21 +000033// 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.
Chris Lattner90aa8392006-10-04 21:52:35 +000036static ManagedStatic<std::string> LibSupportInfoOutputFilename;
Chris Lattner71336a92003-07-31 19:38:34 +000037static std::string &getLibSupportInfoOutputFilename() {
Reid Spencerf6e5a252004-12-14 03:55:21 +000038 return *LibSupportInfoOutputFilename;
Chris Lattner71336a92003-07-31 19:38:34 +000039}
Chris Lattner6c38a792002-10-01 19:36:54 +000040
Owen Anderson46d9a642009-06-23 20:52:29 +000041static ManagedStatic<sys::SmartMutex<true> > TimerLock;
42
Chris Lattner3f398492003-01-30 23:08:50 +000043namespace {
Dan Gohman3c02aca2008-04-23 23:15:23 +000044 static cl::opt<bool>
Chris Lattner3f398492003-01-30 23:08:50 +000045 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
46 "tracking (this may be slow)"),
47 cl::Hidden);
Chris Lattnerf205fec2003-05-09 20:05:44 +000048
Dan Gohman3c02aca2008-04-23 23:15:23 +000049 static cl::opt<std::string, true>
Chris Lattner96a54db2003-08-01 22:15:15 +000050 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerf205fec2003-05-09 20:05:44 +000051 cl::desc("File to append -stats and -timer output to"),
Chris Lattner71336a92003-07-31 19:38:34 +000052 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Chris Lattner3f398492003-01-30 23:08:50 +000053}
54
Chris Lattnerf8df3e02010-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 Anderson200aa6d2009-06-23 16:36:10 +000076static TimerGroup *DefaultTimerGroup = 0;
Chris Lattner6c38a792002-10-01 19:36:54 +000077static TimerGroup *getDefaultTimerGroup() {
Chris Lattner9bb110f2010-03-29 20:35:01 +000078 TimerGroup *tmp = DefaultTimerGroup;
Owen Anderson3b8d1352009-06-23 17:33:37 +000079 sys::MemoryFence();
Chris Lattner9bb110f2010-03-29 20:35:01 +000080 if (tmp) return tmp;
81
82 llvm_acquire_global_lock();
83 tmp = DefaultTimerGroup;
Owen Anderson3b8d1352009-06-23 17:33:37 +000084 if (!tmp) {
Chris Lattner9bb110f2010-03-29 20:35:01 +000085 tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
86 sys::MemoryFence();
87 DefaultTimerGroup = tmp;
Owen Anderson3b8d1352009-06-23 17:33:37 +000088 }
Chris Lattner9bb110f2010-03-29 20:35:01 +000089 llvm_release_global_lock();
Mikhail Glushenkovc11e84d2009-11-07 06:33:12 +000090
Owen Anderson3b8d1352009-06-23 17:33:37 +000091 return tmp;
Chris Lattner6c38a792002-10-01 19:36:54 +000092}
93
Chris Lattner9bb110f2010-03-29 20:35:01 +000094//===----------------------------------------------------------------------===//
95// Timer Implementation
96//===----------------------------------------------------------------------===//
97
Chris Lattner6c38a792002-10-01 19:36:54 +000098Timer::Timer(const std::string &N)
Chris Lattnerfc86c3c2010-03-29 21:28:41 +000099 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +0000100 Started(false), TG(getDefaultTimerGroup()) {
101 TG->addTimer();
102}
103
104Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattnerfc86c3c2010-03-29 21:28:41 +0000105 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +0000106 Started(false), TG(&tg) {
107 TG->addTimer();
108}
109
110Timer::Timer(const Timer &T) {
111 TG = T.TG;
112 if (TG) TG->addTimer();
113 operator=(T);
114}
115
Chris Lattner6c38a792002-10-01 19:36:54 +0000116// 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
Chris Lattner6c38a792002-10-01 19:36:54 +0000123Timer::~Timer() {
Chris Lattner9bb110f2010-03-29 20:35:01 +0000124 if (!TG) return;
125
126 if (Started) {
127 Started = false;
128 TG->addTimerToPrint(*this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000129 }
Chris Lattner9bb110f2010-03-29 20:35:01 +0000130 TG->removeTimer();
Chris Lattner6c38a792002-10-01 19:36:54 +0000131}
132
Jeff Cohene269a1a2005-01-08 20:15:57 +0000133static inline size_t getMemUsage() {
Reid Spenceraeb47b82004-12-27 08:03:04 +0000134 if (TrackSpace)
Jeff Cohene269a1a2005-01-08 20:15:57 +0000135 return sys::Process::GetMallocUsage();
Reid Spenceraeb47b82004-12-27 08:03:04 +0000136 return 0;
137}
138
Chris Lattner6c38a792002-10-01 19:36:54 +0000139struct TimeRecord {
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000140 double Elapsed, UserTime, SystemTime;
141 ssize_t MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000142};
143
Chris Lattner8f0d8242002-11-18 21:47:09 +0000144static TimeRecord getTimeRecord(bool Start) {
Chris Lattnerb4db5f32004-06-07 19:34:51 +0000145 TimeRecord Result;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000146
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000147 sys::TimeValue now(0,0);
148 sys::TimeValue user(0,0);
149 sys::TimeValue sys(0,0);
Chris Lattner6c38a792002-10-01 19:36:54 +0000150
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000151 ssize_t MemUsed = 0;
Reid Spencer7d055632004-12-20 21:44:27 +0000152 if (Start) {
Reid Spenceraeb47b82004-12-27 08:03:04 +0000153 MemUsed = getMemUsage();
Chris Lattner9bb110f2010-03-29 20:35:01 +0000154 sys::Process::GetTimeUsage(now, user, sys);
Reid Spencer7d055632004-12-20 21:44:27 +0000155 } else {
Chris Lattner9bb110f2010-03-29 20:35:01 +0000156 sys::Process::GetTimeUsage(now, user, sys);
Chris Lattnerfed1b272005-03-22 03:20:38 +0000157 MemUsed = getMemUsage();
Reid Spencer7d055632004-12-20 21:44:27 +0000158 }
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000159
Chris Lattner9bb110f2010-03-29 20:35:01 +0000160 Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0;
161 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
162 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
163 Result.MemUsed = MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000164 return Result;
165}
166
Chris Lattner90aa8392006-10-04 21:52:35 +0000167static ManagedStatic<std::vector<Timer*> > ActiveTimers;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000168
Chris Lattner6c38a792002-10-01 19:36:54 +0000169void Timer::startTimer() {
170 Started = true;
Dan Gohman153d28a2008-06-24 22:07:07 +0000171 ActiveTimers->push_back(this);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000172 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000173 Elapsed -= TR.Elapsed;
174 UserTime -= TR.UserTime;
175 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000176 MemUsed -= TR.MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000177}
178
179void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000180 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000181 Elapsed += TR.Elapsed;
182 UserTime += TR.UserTime;
183 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000184 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000185
Chris Lattner90aa8392006-10-04 21:52:35 +0000186 if (ActiveTimers->back() == this) {
187 ActiveTimers->pop_back();
Chris Lattner8f0d8242002-11-18 21:47:09 +0000188 } else {
189 std::vector<Timer*>::iterator I =
Chris Lattner90aa8392006-10-04 21:52:35 +0000190 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
191 assert(I != ActiveTimers->end() && "stop but no startTimer?");
192 ActiveTimers->erase(I);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000193 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000194}
195
196void Timer::sum(const Timer &T) {
197 Elapsed += T.Elapsed;
198 UserTime += T.UserTime;
199 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000200 MemUsed += T.MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000201}
202
Chris Lattner0ea86bc2010-03-29 21:24:52 +0000203const Timer &Timer::operator=(const Timer &T) {
204 Elapsed = T.Elapsed;
205 UserTime = T.UserTime;
206 SystemTime = T.SystemTime;
207 MemUsed = T.MemUsed;
Chris Lattner0ea86bc2010-03-29 21:24:52 +0000208 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 Lattner9bb110f2010-03-29 20:35:01 +0000215static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner0613edc2010-03-29 20:40:19 +0000216 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattner9bb110f2010-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
224void Timer::print(const Timer &Total, raw_ostream &OS) {
Chris Lattner9bb110f2010-03-29 20:35:01 +0000225 if (Total.UserTime)
226 printVal(UserTime, Total.UserTime, OS);
227 if (Total.SystemTime)
228 printVal(SystemTime, Total.SystemTime, OS);
229 if (Total.getProcessTime())
230 printVal(getProcessTime(), Total.getProcessTime(), OS);
231 printVal(Elapsed, Total.Elapsed, OS);
232
233 OS << " ";
234
Chris Lattner0ea86bc2010-03-29 21:24:52 +0000235 if (Total.MemUsed)
Chris Lattner9bb110f2010-03-29 20:35:01 +0000236 OS << format("%9lld", (long long)MemUsed) << " ";
Chris Lattner0ea86bc2010-03-29 21:24:52 +0000237
Chris Lattner9bb110f2010-03-29 20:35:01 +0000238 OS << Name << "\n";
239
240 Started = false; // Once printed, don't print again
241}
242
243
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000244//===----------------------------------------------------------------------===//
245// NamedRegionTimer Implementation
246//===----------------------------------------------------------------------===//
247
Dan Gohman5e843682008-07-14 18:19:29 +0000248typedef std::map<std::string, Timer> Name2Timer;
249typedef std::map<std::string, std::pair<TimerGroup, Name2Timer> > Name2Pair;
250
Dan Gohman5e843682008-07-14 18:19:29 +0000251static ManagedStatic<Name2Timer> NamedTimers;
Dan Gohman5e843682008-07-14 18:19:29 +0000252static ManagedStatic<Name2Pair> NamedGroupedTimers;
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000253
Chris Lattner90aa8392006-10-04 21:52:35 +0000254static Timer &getNamedRegionTimer(const std::string &Name) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000255 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman5e843682008-07-14 18:19:29 +0000256 Name2Timer::iterator I = NamedTimers->find(Name);
Dan Gohmanc418bf32008-07-11 20:58:19 +0000257 if (I != NamedTimers->end())
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000258 return I->second;
259
Chris Lattner90aa8392006-10-04 21:52:35 +0000260 return NamedTimers->insert(I, std::make_pair(Name, Timer(Name)))->second;
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000261}
262
Dan Gohman5e843682008-07-14 18:19:29 +0000263static Timer &getNamedRegionTimer(const std::string &Name,
264 const std::string &GroupName) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000265 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman5e843682008-07-14 18:19:29 +0000266
267 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 }
273
274 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)));
280
281 return J->second;
282}
283
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000284NamedRegionTimer::NamedRegionTimer(const std::string &Name)
285 : TimeRegion(getNamedRegionTimer(Name)) {}
286
Dan Gohman5e843682008-07-14 18:19:29 +0000287NamedRegionTimer::NamedRegionTimer(const std::string &Name,
288 const std::string &GroupName)
289 : TimeRegion(getNamedRegionTimer(Name, GroupName)) {}
Chris Lattner8f0d8242002-11-18 21:47:09 +0000290
Chris Lattner6c38a792002-10-01 19:36:54 +0000291//===----------------------------------------------------------------------===//
292// TimerGroup Implementation
293//===----------------------------------------------------------------------===//
294
Chris Lattner6c38a792002-10-01 19:36:54 +0000295void TimerGroup::removeTimer() {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000296 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner9bb110f2010-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>());
Chris Lattner6c38a792002-10-01 19:36:54 +0000303
Chris Lattner9bb110f2010-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
Chris Lattner6c38a792002-10-01 19:36:54 +0000307
Chris Lattner9bb110f2010-03-29 20:35:01 +0000308 raw_ostream *OutStream = GetLibSupportInfoOutputFile();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000309
Chris Lattner9bb110f2010-03-29 20:35:01 +0000310 ++NumTimers;
Chris Lattner0613edc2010-03-29 20:40:19 +0000311 { // Scope to contain Total timer: don't allow total timer to drop us to
312 // zero timers.
Chris Lattner9bb110f2010-03-29 20:35:01 +0000313 Timer Total("TOTAL");
Misha Brukmanf976c852005-04-21 22:55:34 +0000314
Chris Lattner9bb110f2010-03-29 20:35:01 +0000315 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
316 Total.sum(TimersToPrint[i]);
Misha Brukmanf976c852005-04-21 22:55:34 +0000317
Chris Lattner0613edc2010-03-29 20:40:19 +0000318 // Print out timing header.
Chris Lattner9bb110f2010-03-29 20:35:01 +0000319 *OutStream << "===" << std::string(73, '-') << "===\n"
320 << std::string(Padding, ' ') << Name << "\n"
321 << "===" << std::string(73, '-')
322 << "===\n";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000323
Chris Lattner9bb110f2010-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: ";
Chris Lattner9bb110f2010-03-29 20:35:01 +0000329 *OutStream << format("%5.4f", Total.getProcessTime()) << " seconds (";
330 *OutStream << format("%5.4f", Total.getWallTime()) << " wall clock)\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000331 }
Chris Lattner9bb110f2010-03-29 20:35:01 +0000332 *OutStream << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000333
Chris Lattner9bb110f2010-03-29 20:35:01 +0000334 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---";
Chris Lattner9bb110f2010-03-29 20:35:01 +0000343 *OutStream << " --- Name ---\n";
Chris Lattnerf205fec2003-05-09 20:05:44 +0000344
Chris Lattner0613edc2010-03-29 20:40:19 +0000345 // Loop through all of the timing data, printing it out.
Chris Lattner9bb110f2010-03-29 20:35:01 +0000346 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();
Chris Lattner6c38a792002-10-01 19:36:54 +0000352 }
Chris Lattner9bb110f2010-03-29 20:35:01 +0000353 --NumTimers;
354
355 TimersToPrint.clear();
356
Chris Lattner0ea86bc2010-03-29 21:24:52 +0000357 if (OutStream != &errs() && OutStream != &outs())
Chris Lattner0613edc2010-03-29 20:40:19 +0000358 delete OutStream; // Close the file.
Chris Lattner6c38a792002-10-01 19:36:54 +0000359}
Brian Gaeked0fde302003-11-11 22:41:34 +0000360
Owen Anderson46d9a642009-06-23 20:52:29 +0000361void TimerGroup::addTimer() {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000362 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson46d9a642009-06-23 20:52:29 +0000363 ++NumTimers;
364}
365
366void TimerGroup::addTimerToPrint(const Timer &T) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000367 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson46d9a642009-06-23 20:52:29 +0000368 TimersToPrint.push_back(Timer(true, T));
369}
370