blob: 4fac0737f0c070378fb8f0e7ae90866caaefe0d6 [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"
Chris Lattner4485d682010-03-30 05:20:02 +000022#include "llvm/ADT/OwningPtr.h"
Chris Lattner02039942010-03-30 04:03:22 +000023#include "llvm/ADT/StringMap.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024using namespace llvm;
25
Chris Lattner01539ad2010-03-30 05:01:08 +000026// CreateInfoOutputFile - Return a file stream to print our output on.
27namespace llvm { extern raw_ostream *CreateInfoOutputFile(); }
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
Chris Lattner01539ad2010-03-30 05:01:08 +000056// CreateInfoOutputFile - Return a file stream to print our output on.
57raw_ostream *llvm::CreateInfoOutputFile() {
Chris Lattner3a352e72010-03-30 05:34:02 +000058 const std::string &OutputFilename = getLibSupportInfoOutputFilename();
59 if (OutputFilename.empty())
Chris Lattner01539ad2010-03-30 05:01:08 +000060 return new raw_fd_ostream(2, false); // stderr.
Chris Lattner3a352e72010-03-30 05:34:02 +000061 if (OutputFilename == "-")
Chris Lattner01539ad2010-03-30 05:01:08 +000062 return new raw_fd_ostream(1, false); // stdout.
Chris Lattner7641c7b2010-03-29 21:34:06 +000063
64 std::string Error;
Chris Lattner3a352e72010-03-30 05:34:02 +000065 raw_ostream *Result = new raw_fd_ostream(OutputFilename.c_str(),
Chris Lattner7641c7b2010-03-29 21:34:06 +000066 Error, raw_fd_ostream::F_Append);
67 if (Error.empty())
68 return Result;
69
70 errs() << "Error opening info-output-file '"
Chris Lattner3a352e72010-03-30 05:34:02 +000071 << OutputFilename << " for appending!\n";
Chris Lattner7641c7b2010-03-29 21:34:06 +000072 delete Result;
Chris Lattner01539ad2010-03-30 05:01:08 +000073 return new raw_fd_ostream(2, false); // stderr.
Chris Lattner7641c7b2010-03-29 21:34:06 +000074}
75
76
Owen Anderson56ddb832009-06-23 16:36:10 +000077static TimerGroup *DefaultTimerGroup = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078static TimerGroup *getDefaultTimerGroup() {
Chris Lattnerc3546852010-03-29 20:35:01 +000079 TimerGroup *tmp = DefaultTimerGroup;
Owen Anderson1b2ea532009-06-23 17:33:37 +000080 sys::MemoryFence();
Chris Lattnerc3546852010-03-29 20:35:01 +000081 if (tmp) return tmp;
82
83 llvm_acquire_global_lock();
84 tmp = DefaultTimerGroup;
Owen Anderson1b2ea532009-06-23 17:33:37 +000085 if (!tmp) {
Chris Lattnerc3546852010-03-29 20:35:01 +000086 tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
87 sys::MemoryFence();
88 DefaultTimerGroup = tmp;
Owen Anderson1b2ea532009-06-23 17:33:37 +000089 }
Chris Lattnerc3546852010-03-29 20:35:01 +000090 llvm_release_global_lock();
Mikhail Glushenkov05858c52009-11-07 06:33:12 +000091
Owen Anderson1b2ea532009-06-23 17:33:37 +000092 return tmp;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093}
94
Chris Lattnerc3546852010-03-29 20:35:01 +000095//===----------------------------------------------------------------------===//
96// Timer Implementation
97//===----------------------------------------------------------------------===//
98
Chris Lattner3a352e72010-03-30 05:34:02 +000099void Timer::init(StringRef N) {
Chris Lattner02039942010-03-30 04:03:22 +0000100 assert(TG == 0 && "Timer already initialized");
Chris Lattner3a352e72010-03-30 05:34:02 +0000101 Name.assign(N.begin(), N.end());
Chris Lattner02039942010-03-30 04:03:22 +0000102 Started = false;
103 TG = getDefaultTimerGroup();
Chris Lattner9d556112010-03-30 04:40:01 +0000104 TG->addTimer(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105}
106
Chris Lattner3a352e72010-03-30 05:34:02 +0000107void Timer::init(StringRef N, TimerGroup &tg) {
Chris Lattner02039942010-03-30 04:03:22 +0000108 assert(TG == 0 && "Timer already initialized");
Chris Lattner3a352e72010-03-30 05:34:02 +0000109 Name.assign(N.begin(), N.end());
Chris Lattner02039942010-03-30 04:03:22 +0000110 Started = false;
111 TG = &tg;
Chris Lattner9d556112010-03-30 04:40:01 +0000112 TG->addTimer(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113}
114
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115Timer::~Timer() {
Chris Lattner4485d682010-03-30 05:20:02 +0000116 if (!TG) return; // Never initialized, or already cleared.
Chris Lattner9d556112010-03-30 04:40:01 +0000117 TG->removeTimer(*this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118}
119
120static inline size_t getMemUsage() {
Chris Lattner4485d682010-03-30 05:20:02 +0000121 if (!TrackSpace) return 0;
122 return sys::Process::GetMallocUsage();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123}
124
Chris Lattner02039942010-03-30 04:03:22 +0000125TimeRecord TimeRecord::getCurrentTime(bool Start) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 TimeRecord Result;
Chris Lattner4485d682010-03-30 05:20:02 +0000127 sys::TimeValue now(0,0), user(0,0), sys(0,0);
128
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 if (Start) {
Chris Lattner4485d682010-03-30 05:20:02 +0000130 Result.MemUsed = getMemUsage();
Chris Lattnerc3546852010-03-29 20:35:01 +0000131 sys::Process::GetTimeUsage(now, user, sys);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 } else {
Chris Lattnerc3546852010-03-29 20:35:01 +0000133 sys::Process::GetTimeUsage(now, user, sys);
Chris Lattner4485d682010-03-30 05:20:02 +0000134 Result.MemUsed = getMemUsage();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 }
136
Chris Lattner02039942010-03-30 04:03:22 +0000137 Result.WallTime = now.seconds() + now.microseconds() / 1000000.0;
Chris Lattnerc3546852010-03-29 20:35:01 +0000138 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
Chris Lattner02039942010-03-30 04:03:22 +0000139 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 return Result;
141}
142
143static ManagedStatic<std::vector<Timer*> > ActiveTimers;
144
145void Timer::startTimer() {
146 Started = true;
Dan Gohmanf6930f92008-06-24 22:07:07 +0000147 ActiveTimers->push_back(this);
Chris Lattner02039942010-03-30 04:03:22 +0000148 Time -= TimeRecord::getCurrentTime(true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149}
150
151void Timer::stopTimer() {
Chris Lattner02039942010-03-30 04:03:22 +0000152 Time += TimeRecord::getCurrentTime(false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153
154 if (ActiveTimers->back() == this) {
155 ActiveTimers->pop_back();
156 } else {
157 std::vector<Timer*>::iterator I =
158 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
159 assert(I != ActiveTimers->end() && "stop but no startTimer?");
160 ActiveTimers->erase(I);
161 }
162}
163
Chris Lattnerc3546852010-03-29 20:35:01 +0000164static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner626ca122010-03-29 20:40:19 +0000165 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerc3546852010-03-29 20:35:01 +0000166 OS << " ----- ";
167 else {
168 OS << " " << format("%7.4f", Val) << " (";
169 OS << format("%5.1f", Val*100/Total) << "%)";
170 }
171}
172
Chris Lattner02039942010-03-30 04:03:22 +0000173void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
174 if (Total.getUserTime())
175 printVal(getUserTime(), Total.getUserTime(), OS);
176 if (Total.getSystemTime())
177 printVal(getSystemTime(), Total.getSystemTime(), OS);
Chris Lattnerc3546852010-03-29 20:35:01 +0000178 if (Total.getProcessTime())
179 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattner02039942010-03-30 04:03:22 +0000180 printVal(getWallTime(), Total.getWallTime(), OS);
Chris Lattnerc3546852010-03-29 20:35:01 +0000181
182 OS << " ";
183
Chris Lattner02039942010-03-30 04:03:22 +0000184 if (Total.getMemUsed())
185 OS << format("%9lld", (long long)getMemUsed()) << " ";
Chris Lattnerc3546852010-03-29 20:35:01 +0000186}
187
188
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189//===----------------------------------------------------------------------===//
190// NamedRegionTimer Implementation
191//===----------------------------------------------------------------------===//
192
Chris Lattner02039942010-03-30 04:03:22 +0000193typedef StringMap<Timer> Name2TimerMap;
Chris Lattner4485d682010-03-30 05:20:02 +0000194
195class Name2PairMap {
196 StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
197public:
198 ~Name2PairMap() {
199 for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
200 I = Map.begin(), E = Map.end(); I != E; ++I)
201 delete I->second.first;
202 }
203
Chris Lattner3a352e72010-03-30 05:34:02 +0000204 Timer &get(StringRef Name, StringRef GroupName) {
Chris Lattner4485d682010-03-30 05:20:02 +0000205 sys::SmartScopedLock<true> L(*TimerLock);
206
207 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
208
209 if (!GroupEntry.first)
210 GroupEntry.first = new TimerGroup(GroupName);
211
212 Timer &T = GroupEntry.second[Name];
213 if (!T.isInitialized())
214 T.init(Name, *GroupEntry.first);
215 return T;
216 }
217};
Dan Gohman368a08b2008-07-14 18:19:29 +0000218
Chris Lattner02039942010-03-30 04:03:22 +0000219static ManagedStatic<Name2TimerMap> NamedTimers;
220static ManagedStatic<Name2PairMap> NamedGroupedTimers;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221
Chris Lattner3a352e72010-03-30 05:34:02 +0000222static Timer &getNamedRegionTimer(StringRef Name) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000223 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner02039942010-03-30 04:03:22 +0000224
225 Timer &T = (*NamedTimers)[Name];
226 if (!T.isInitialized())
227 T.init(Name);
228 return T;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229}
230
Chris Lattner3a352e72010-03-30 05:34:02 +0000231NamedRegionTimer::NamedRegionTimer(StringRef Name)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 : TimeRegion(getNamedRegionTimer(Name)) {}
233
Chris Lattner3a352e72010-03-30 05:34:02 +0000234NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef GroupName)
Chris Lattner4485d682010-03-30 05:20:02 +0000235 : TimeRegion(NamedGroupedTimers->get(Name, GroupName)) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236
237//===----------------------------------------------------------------------===//
238// TimerGroup Implementation
239//===----------------------------------------------------------------------===//
240
Chris Lattner224e85e2010-03-30 05:27:58 +0000241/// TimerGroupList - This is the global list of TimerGroups, maintained by the
242/// TimerGroup ctor/dtor and is protected by the TimerLock lock.
243static TimerGroup *TimerGroupList = 0;
244
Chris Lattner3a352e72010-03-30 05:34:02 +0000245TimerGroup::TimerGroup(StringRef name)
246 : Name(name.begin(), name.end()), FirstTimer(0) {
Chris Lattner224e85e2010-03-30 05:27:58 +0000247
248 // Add the group to TimerGroupList.
249 sys::SmartScopedLock<true> L(*TimerLock);
250 if (TimerGroupList)
251 TimerGroupList->Prev = &Next;
252 Next = TimerGroupList;
253 Prev = &TimerGroupList;
254 TimerGroupList = this;
255}
256
Chris Lattner73fa93a2010-03-30 04:58:26 +0000257TimerGroup::~TimerGroup() {
258 // If the timer group is destroyed before the timers it owns, accumulate and
259 // print the timing data.
260 while (FirstTimer != 0)
261 removeTimer(*FirstTimer);
Chris Lattner224e85e2010-03-30 05:27:58 +0000262
263 // Remove the group from the TimerGroupList.
264 sys::SmartScopedLock<true> L(*TimerLock);
265 *Prev = Next;
266 if (Next)
267 Next->Prev = Prev;
Chris Lattner73fa93a2010-03-30 04:58:26 +0000268}
269
270
Chris Lattner9d556112010-03-30 04:40:01 +0000271void TimerGroup::removeTimer(Timer &T) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000272 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattnerc3546852010-03-29 20:35:01 +0000273
Chris Lattner9d556112010-03-30 04:40:01 +0000274 // If the timer was started, move its data to TimersToPrint.
Chris Lattner73fa93a2010-03-30 04:58:26 +0000275 if (T.Started)
Chris Lattner9d556112010-03-30 04:40:01 +0000276 TimersToPrint.push_back(std::make_pair(T.Time, T.Name));
Chris Lattner73fa93a2010-03-30 04:58:26 +0000277
278 T.TG = 0;
Chris Lattner9d556112010-03-30 04:40:01 +0000279
280 // Unlink the timer from our list.
281 *T.Prev = T.Next;
282 if (T.Next)
283 T.Next->Prev = T.Prev;
284
285 // Print the report when all timers in this group are destroyed if some of
286 // them were started.
287 if (FirstTimer != 0 || TimersToPrint.empty())
288 return;
289
Chris Lattner01539ad2010-03-30 05:01:08 +0000290 raw_ostream *OutStream = CreateInfoOutputFile();
Chris Lattner9d556112010-03-30 04:40:01 +0000291 PrintQueuedTimers(*OutStream);
Chris Lattner01539ad2010-03-30 05:01:08 +0000292 delete OutStream; // Close the file.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293}
294
Chris Lattner9d556112010-03-30 04:40:01 +0000295void TimerGroup::addTimer(Timer &T) {
Owen Andersonbe44bed2009-07-07 18:33:04 +0000296 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner9d556112010-03-30 04:40:01 +0000297
298 // Add the timer to our list.
299 if (FirstTimer)
300 FirstTimer->Prev = &T.Next;
301 T.Next = FirstTimer;
302 T.Prev = &FirstTimer;
303 FirstTimer = &T;
Owen Anderson18463102009-06-23 20:52:29 +0000304}
305
Chris Lattner9d556112010-03-30 04:40:01 +0000306void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
307 // Sort the timers in descending order by amount of time taken.
308 std::sort(TimersToPrint.begin(), TimersToPrint.end());
309
310 TimeRecord Total;
311 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
312 Total += TimersToPrint[i].first;
313
314 // Print out timing header.
315 OS << "===" << std::string(73, '-') << "===\n";
316 // Figure out how many spaces to indent TimerGroup name.
317 unsigned Padding = (80-Name.length())/2;
318 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
319 OS.indent(Padding) << Name << '\n';
320 OS << "===" << std::string(73, '-') << "===\n";
321
322 // If this is not an collection of ungrouped times, print the total time.
323 // Ungrouped timers don't really make sense to add up. We still print the
324 // TOTAL line to make the percentages make sense.
325 if (this != DefaultTimerGroup) {
326 OS << " Total Execution Time: ";
327 OS << format("%5.4f", Total.getProcessTime()) << " seconds (";
328 OS << format("%5.4f", Total.getWallTime()) << " wall clock)\n";
329 }
330 OS << '\n';
331
332 if (Total.getUserTime())
333 OS << " ---User Time---";
334 if (Total.getSystemTime())
335 OS << " --System Time--";
336 if (Total.getProcessTime())
337 OS << " --User+System--";
338 OS << " ---Wall Time---";
339 if (Total.getMemUsed())
340 OS << " ---Mem---";
341 OS << " --- Name ---\n";
342
343 // Loop through all of the timing data, printing it out.
344 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) {
345 const std::pair<TimeRecord, std::string> &Entry = TimersToPrint[e-i-1];
346 Entry.first.print(Total, OS);
347 OS << Entry.second << '\n';
348 }
349
350 Total.print(Total, OS);
351 OS << "Total\n\n";
352 OS.flush();
353
354 TimersToPrint.clear();
Owen Anderson18463102009-06-23 20:52:29 +0000355}
356
Chris Lattner4485d682010-03-30 05:20:02 +0000357/// print - Print any started timers in this group and zero them.
358void TimerGroup::print(raw_ostream &OS) {
359 sys::SmartScopedLock<true> L(*TimerLock);
360
361 // See if any of our timers were started, if so add them to TimersToPrint and
362 // reset them.
363 for (Timer *T = FirstTimer; T; T = T->Next) {
364 if (!T->Started) continue;
365 TimersToPrint.push_back(std::make_pair(T->Time, T->Name));
366
367 // Clear out the time.
368 T->Started = 0;
369 T->Time = TimeRecord();
370 }
371
372 // If any timers were started, print the group.
373 if (!TimersToPrint.empty())
374 PrintQueuedTimers(OS);
375}
Chris Lattner224e85e2010-03-30 05:27:58 +0000376
377/// printAll - This static method prints all timers and clears them all out.
378void TimerGroup::printAll(raw_ostream &OS) {
379 sys::SmartScopedLock<true> L(*TimerLock);
380
381 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
382 TG->print(OS);
383}