blob: c0bc750d5962998c6ae4c1c669dc3f02f5894b79 [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 Lattnera782e752010-03-30 04:03:22 +000022#include "llvm/ADT/StringMap.h"
Chris Lattnerb6d465f2003-12-14 21:27:33 +000023using namespace llvm;
Chris Lattnerf205fec2003-05-09 20:05:44 +000024
Chris Lattner49a2bb22010-03-30 05:01:08 +000025// CreateInfoOutputFile - Return a file stream to print our output on.
26namespace llvm { extern raw_ostream *CreateInfoOutputFile(); }
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 Lattner49a2bb22010-03-30 05:01:08 +000055// CreateInfoOutputFile - Return a file stream to print our output on.
56raw_ostream *llvm::CreateInfoOutputFile() {
Chris Lattnerf8df3e02010-03-29 21:34:06 +000057 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
58 if (LibSupportInfoOutputFilename.empty())
Chris Lattner49a2bb22010-03-30 05:01:08 +000059 return new raw_fd_ostream(2, false); // stderr.
Chris Lattnerf8df3e02010-03-29 21:34:06 +000060 if (LibSupportInfoOutputFilename == "-")
Chris Lattner49a2bb22010-03-30 05:01:08 +000061 return new raw_fd_ostream(1, false); // stdout.
Chris Lattnerf8df3e02010-03-29 21:34:06 +000062
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;
Chris Lattner49a2bb22010-03-30 05:01:08 +000072 return new raw_fd_ostream(2, false); // stderr.
Chris Lattnerf8df3e02010-03-29 21:34:06 +000073}
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 Lattnera782e752010-03-30 04:03:22 +000098void Timer::init(const std::string &N) {
99 assert(TG == 0 && "Timer already initialized");
100 Name = N;
101 Started = false;
102 TG = getDefaultTimerGroup();
Chris Lattnerb9312692010-03-30 04:40:01 +0000103 TG->addTimer(*this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000104}
105
Chris Lattnera782e752010-03-30 04:03:22 +0000106void Timer::init(const std::string &N, TimerGroup &tg) {
107 assert(TG == 0 && "Timer already initialized");
108 Name = N;
109 Started = false;
110 TG = &tg;
Chris Lattnerb9312692010-03-30 04:40:01 +0000111 TG->addTimer(*this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000112}
113
Chris Lattner6c38a792002-10-01 19:36:54 +0000114Timer::~Timer() {
Chris Lattnera782e752010-03-30 04:03:22 +0000115 if (!TG) return; // Never initialized.
Chris Lattnerb9312692010-03-30 04:40:01 +0000116 TG->removeTimer(*this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000117}
118
Jeff Cohene269a1a2005-01-08 20:15:57 +0000119static inline size_t getMemUsage() {
Reid Spenceraeb47b82004-12-27 08:03:04 +0000120 if (TrackSpace)
Jeff Cohene269a1a2005-01-08 20:15:57 +0000121 return sys::Process::GetMallocUsage();
Reid Spenceraeb47b82004-12-27 08:03:04 +0000122 return 0;
123}
124
Chris Lattnera782e752010-03-30 04:03:22 +0000125TimeRecord TimeRecord::getCurrentTime(bool Start) {
Chris Lattnerb4db5f32004-06-07 19:34:51 +0000126 TimeRecord Result;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000127
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000128 sys::TimeValue now(0,0);
129 sys::TimeValue user(0,0);
130 sys::TimeValue sys(0,0);
Chris Lattner6c38a792002-10-01 19:36:54 +0000131
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000132 ssize_t MemUsed = 0;
Reid Spencer7d055632004-12-20 21:44:27 +0000133 if (Start) {
Reid Spenceraeb47b82004-12-27 08:03:04 +0000134 MemUsed = getMemUsage();
Chris Lattner9bb110f2010-03-29 20:35:01 +0000135 sys::Process::GetTimeUsage(now, user, sys);
Reid Spencer7d055632004-12-20 21:44:27 +0000136 } else {
Chris Lattner9bb110f2010-03-29 20:35:01 +0000137 sys::Process::GetTimeUsage(now, user, sys);
Chris Lattnerfed1b272005-03-22 03:20:38 +0000138 MemUsed = getMemUsage();
Reid Spencer7d055632004-12-20 21:44:27 +0000139 }
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000140
Chris Lattnera782e752010-03-30 04:03:22 +0000141 Result.WallTime = now.seconds() + now.microseconds() / 1000000.0;
Chris Lattner9bb110f2010-03-29 20:35:01 +0000142 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
Chris Lattnera782e752010-03-30 04:03:22 +0000143 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
Chris Lattner9bb110f2010-03-29 20:35:01 +0000144 Result.MemUsed = MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000145 return Result;
146}
147
Chris Lattner90aa8392006-10-04 21:52:35 +0000148static ManagedStatic<std::vector<Timer*> > ActiveTimers;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000149
Chris Lattner6c38a792002-10-01 19:36:54 +0000150void Timer::startTimer() {
151 Started = true;
Dan Gohman153d28a2008-06-24 22:07:07 +0000152 ActiveTimers->push_back(this);
Chris Lattnera782e752010-03-30 04:03:22 +0000153 Time -= TimeRecord::getCurrentTime(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000154}
155
156void Timer::stopTimer() {
Chris Lattnera782e752010-03-30 04:03:22 +0000157 Time += TimeRecord::getCurrentTime(false);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000158
Chris Lattner90aa8392006-10-04 21:52:35 +0000159 if (ActiveTimers->back() == this) {
160 ActiveTimers->pop_back();
Chris Lattner8f0d8242002-11-18 21:47:09 +0000161 } else {
162 std::vector<Timer*>::iterator I =
Chris Lattner90aa8392006-10-04 21:52:35 +0000163 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
164 assert(I != ActiveTimers->end() && "stop but no startTimer?");
165 ActiveTimers->erase(I);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000166 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000167}
168
Chris Lattner9bb110f2010-03-29 20:35:01 +0000169static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner0613edc2010-03-29 20:40:19 +0000170 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattner9bb110f2010-03-29 20:35:01 +0000171 OS << " ----- ";
172 else {
173 OS << " " << format("%7.4f", Val) << " (";
174 OS << format("%5.1f", Val*100/Total) << "%)";
175 }
176}
177
Chris Lattnera782e752010-03-30 04:03:22 +0000178void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
179 if (Total.getUserTime())
180 printVal(getUserTime(), Total.getUserTime(), OS);
181 if (Total.getSystemTime())
182 printVal(getSystemTime(), Total.getSystemTime(), OS);
Chris Lattner9bb110f2010-03-29 20:35:01 +0000183 if (Total.getProcessTime())
184 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattnera782e752010-03-30 04:03:22 +0000185 printVal(getWallTime(), Total.getWallTime(), OS);
Chris Lattner9bb110f2010-03-29 20:35:01 +0000186
187 OS << " ";
188
Chris Lattnera782e752010-03-30 04:03:22 +0000189 if (Total.getMemUsed())
190 OS << format("%9lld", (long long)getMemUsed()) << " ";
Chris Lattner9bb110f2010-03-29 20:35:01 +0000191}
192
193
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000194//===----------------------------------------------------------------------===//
195// NamedRegionTimer Implementation
196//===----------------------------------------------------------------------===//
197
Chris Lattnera782e752010-03-30 04:03:22 +0000198typedef StringMap<Timer> Name2TimerMap;
199typedef StringMap<std::pair<TimerGroup, Name2TimerMap> > Name2PairMap;
Dan Gohman5e843682008-07-14 18:19:29 +0000200
Chris Lattnera782e752010-03-30 04:03:22 +0000201static ManagedStatic<Name2TimerMap> NamedTimers;
202static ManagedStatic<Name2PairMap> NamedGroupedTimers;
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000203
Chris Lattner90aa8392006-10-04 21:52:35 +0000204static Timer &getNamedRegionTimer(const std::string &Name) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000205 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattnera782e752010-03-30 04:03:22 +0000206
207 Timer &T = (*NamedTimers)[Name];
208 if (!T.isInitialized())
209 T.init(Name);
210 return T;
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000211}
212
Dan Gohman5e843682008-07-14 18:19:29 +0000213static Timer &getNamedRegionTimer(const std::string &Name,
214 const std::string &GroupName) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000215 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman5e843682008-07-14 18:19:29 +0000216
Chris Lattnera782e752010-03-30 04:03:22 +0000217 std::pair<TimerGroup, Name2TimerMap> &GroupEntry =
218 (*NamedGroupedTimers)[GroupName];
Dan Gohman5e843682008-07-14 18:19:29 +0000219
Chris Lattnera782e752010-03-30 04:03:22 +0000220 if (GroupEntry.second.empty())
221 GroupEntry.first.setName(GroupName);
Dan Gohman5e843682008-07-14 18:19:29 +0000222
Chris Lattnera782e752010-03-30 04:03:22 +0000223 Timer &T = GroupEntry.second[Name];
224 if (!T.isInitialized())
225 T.init(Name);
226 return T;
Dan Gohman5e843682008-07-14 18:19:29 +0000227}
228
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000229NamedRegionTimer::NamedRegionTimer(const std::string &Name)
230 : TimeRegion(getNamedRegionTimer(Name)) {}
231
Dan Gohman5e843682008-07-14 18:19:29 +0000232NamedRegionTimer::NamedRegionTimer(const std::string &Name,
233 const std::string &GroupName)
234 : TimeRegion(getNamedRegionTimer(Name, GroupName)) {}
Chris Lattner8f0d8242002-11-18 21:47:09 +0000235
Chris Lattner6c38a792002-10-01 19:36:54 +0000236//===----------------------------------------------------------------------===//
237// TimerGroup Implementation
238//===----------------------------------------------------------------------===//
239
Chris Lattner9f9f6d12010-03-30 04:58:26 +0000240TimerGroup::~TimerGroup() {
241 // If the timer group is destroyed before the timers it owns, accumulate and
242 // print the timing data.
243 while (FirstTimer != 0)
244 removeTimer(*FirstTimer);
245}
246
247
Chris Lattnerb9312692010-03-30 04:40:01 +0000248void TimerGroup::removeTimer(Timer &T) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000249 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner9bb110f2010-03-29 20:35:01 +0000250
Chris Lattnerb9312692010-03-30 04:40:01 +0000251 // If the timer was started, move its data to TimersToPrint.
Chris Lattner9f9f6d12010-03-30 04:58:26 +0000252 if (T.Started)
Chris Lattnerb9312692010-03-30 04:40:01 +0000253 TimersToPrint.push_back(std::make_pair(T.Time, T.Name));
Chris Lattner9f9f6d12010-03-30 04:58:26 +0000254
255 T.TG = 0;
Chris Lattnerb9312692010-03-30 04:40:01 +0000256
257 // Unlink the timer from our list.
258 *T.Prev = T.Next;
259 if (T.Next)
260 T.Next->Prev = T.Prev;
261
262 // Print the report when all timers in this group are destroyed if some of
263 // them were started.
264 if (FirstTimer != 0 || TimersToPrint.empty())
265 return;
266
Chris Lattner49a2bb22010-03-30 05:01:08 +0000267 raw_ostream *OutStream = CreateInfoOutputFile();
Chris Lattnerb9312692010-03-30 04:40:01 +0000268 PrintQueuedTimers(*OutStream);
Chris Lattner49a2bb22010-03-30 05:01:08 +0000269 delete OutStream; // Close the file.
Chris Lattner6c38a792002-10-01 19:36:54 +0000270}
Brian Gaeked0fde302003-11-11 22:41:34 +0000271
Chris Lattnerb9312692010-03-30 04:40:01 +0000272void TimerGroup::addTimer(Timer &T) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000273 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattnerb9312692010-03-30 04:40:01 +0000274
275 // Add the timer to our list.
276 if (FirstTimer)
277 FirstTimer->Prev = &T.Next;
278 T.Next = FirstTimer;
279 T.Prev = &FirstTimer;
280 FirstTimer = &T;
Owen Anderson46d9a642009-06-23 20:52:29 +0000281}
282
Chris Lattnerb9312692010-03-30 04:40:01 +0000283void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
284 // Sort the timers in descending order by amount of time taken.
285 std::sort(TimersToPrint.begin(), TimersToPrint.end());
286
287 TimeRecord Total;
288 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
289 Total += TimersToPrint[i].first;
290
291 // Print out timing header.
292 OS << "===" << std::string(73, '-') << "===\n";
293 // Figure out how many spaces to indent TimerGroup name.
294 unsigned Padding = (80-Name.length())/2;
295 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
296 OS.indent(Padding) << Name << '\n';
297 OS << "===" << std::string(73, '-') << "===\n";
298
299 // If this is not an collection of ungrouped times, print the total time.
300 // Ungrouped timers don't really make sense to add up. We still print the
301 // TOTAL line to make the percentages make sense.
302 if (this != DefaultTimerGroup) {
303 OS << " Total Execution Time: ";
304 OS << format("%5.4f", Total.getProcessTime()) << " seconds (";
305 OS << format("%5.4f", Total.getWallTime()) << " wall clock)\n";
306 }
307 OS << '\n';
308
309 if (Total.getUserTime())
310 OS << " ---User Time---";
311 if (Total.getSystemTime())
312 OS << " --System Time--";
313 if (Total.getProcessTime())
314 OS << " --User+System--";
315 OS << " ---Wall Time---";
316 if (Total.getMemUsed())
317 OS << " ---Mem---";
318 OS << " --- Name ---\n";
319
320 // Loop through all of the timing data, printing it out.
321 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) {
322 const std::pair<TimeRecord, std::string> &Entry = TimersToPrint[e-i-1];
323 Entry.first.print(Total, OS);
324 OS << Entry.second << '\n';
325 }
326
327 Total.print(Total, OS);
328 OS << "Total\n\n";
329 OS.flush();
330
331 TimersToPrint.clear();
Owen Anderson46d9a642009-06-23 20:52:29 +0000332}
333