blob: 077995d9fb8993a21d4a07120d20e1c366b0854a [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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 Lattner90aa8392006-10-04 21:52:35 +000016#include "llvm/Support/ManagedStatic.h"
Bill Wendlingbcd24982006-12-07 20:28:15 +000017#include "llvm/Support/Streams.h"
Reid Spencerdf52c9a2004-12-20 00:59:04 +000018#include "llvm/System/Process.h"
Reid Spencer0255abb2004-12-20 03:59:23 +000019#include <algorithm>
Chris Lattnerb4db5f32004-06-07 19:34:51 +000020#include <fstream>
Reid Spencer0255abb2004-12-20 03:59:23 +000021#include <functional>
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 Lattnerb6d465f2003-12-14 21:27:33 +000026namespace llvm { extern std::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
Chris Lattner3f398492003-01-30 23:08:50 +000041namespace {
42 cl::opt<bool>
43 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
44 "tracking (this may be slow)"),
45 cl::Hidden);
Chris Lattnerf205fec2003-05-09 20:05:44 +000046
47 cl::opt<std::string, true>
Chris Lattner96a54db2003-08-01 22:15:15 +000048 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerf205fec2003-05-09 20:05:44 +000049 cl::desc("File to append -stats and -timer output to"),
Chris Lattner71336a92003-07-31 19:38:34 +000050 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Chris Lattner3f398492003-01-30 23:08:50 +000051}
52
Chris Lattner6c38a792002-10-01 19:36:54 +000053static TimerGroup *DefaultTimerGroup = 0;
54static TimerGroup *getDefaultTimerGroup() {
55 if (DefaultTimerGroup) return DefaultTimerGroup;
56 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
57}
58
59Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000060 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000061 Started(false), TG(getDefaultTimerGroup()) {
62 TG->addTimer();
63}
64
65Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000066 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000067 Started(false), TG(&tg) {
68 TG->addTimer();
69}
70
71Timer::Timer(const Timer &T) {
72 TG = T.TG;
73 if (TG) TG->addTimer();
74 operator=(T);
75}
76
77
78// Copy ctor, initialize with no TG member.
79Timer::Timer(bool, const Timer &T) {
80 TG = T.TG; // Avoid assertion in operator=
81 operator=(T); // Copy contents
82 TG = 0;
83}
84
85
86Timer::~Timer() {
87 if (TG) {
88 if (Started) {
89 Started = false;
90 TG->addTimerToPrint(*this);
91 }
92 TG->removeTimer();
93 }
94}
95
Jeff Cohene269a1a2005-01-08 20:15:57 +000096static inline size_t getMemUsage() {
Reid Spenceraeb47b82004-12-27 08:03:04 +000097 if (TrackSpace)
Jeff Cohene269a1a2005-01-08 20:15:57 +000098 return sys::Process::GetMallocUsage();
Reid Spenceraeb47b82004-12-27 08:03:04 +000099 return 0;
100}
101
Chris Lattner6c38a792002-10-01 19:36:54 +0000102struct TimeRecord {
103 double Elapsed, UserTime, SystemTime;
Chris Lattner6cfbd622005-01-29 05:21:16 +0000104 ssize_t MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000105};
106
Chris Lattner8f0d8242002-11-18 21:47:09 +0000107static TimeRecord getTimeRecord(bool Start) {
Chris Lattnerb4db5f32004-06-07 19:34:51 +0000108 TimeRecord Result;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000109
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000110 sys::TimeValue now(0,0);
111 sys::TimeValue user(0,0);
112 sys::TimeValue sys(0,0);
Chris Lattner6c38a792002-10-01 19:36:54 +0000113
Chris Lattner6cfbd622005-01-29 05:21:16 +0000114 ssize_t MemUsed = 0;
Reid Spencer7d055632004-12-20 21:44:27 +0000115 if (Start) {
Reid Spenceraeb47b82004-12-27 08:03:04 +0000116 MemUsed = getMemUsage();
Chris Lattnerfed1b272005-03-22 03:20:38 +0000117 sys::Process::GetTimeUsage(now,user,sys);
Reid Spencer7d055632004-12-20 21:44:27 +0000118 } else {
Reid Spencer7d055632004-12-20 21:44:27 +0000119 sys::Process::GetTimeUsage(now,user,sys);
Chris Lattnerfed1b272005-03-22 03:20:38 +0000120 MemUsed = getMemUsage();
Reid Spencer7d055632004-12-20 21:44:27 +0000121 }
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000122
Reid Spencer7d055632004-12-20 21:44:27 +0000123 Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0;
124 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
125 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
126 Result.MemUsed = MemUsed;
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000127
Chris Lattner6c38a792002-10-01 19:36:54 +0000128 return Result;
129}
130
Chris Lattner90aa8392006-10-04 21:52:35 +0000131static ManagedStatic<std::vector<Timer*> > ActiveTimers;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000132
Chris Lattner6c38a792002-10-01 19:36:54 +0000133void Timer::startTimer() {
134 Started = true;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000135 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000136 Elapsed -= TR.Elapsed;
137 UserTime -= TR.UserTime;
138 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000139 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000140 PeakMemBase = TR.MemUsed;
Chris Lattner90aa8392006-10-04 21:52:35 +0000141 ActiveTimers->push_back(this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000142}
143
144void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000145 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000146 Elapsed += TR.Elapsed;
147 UserTime += TR.UserTime;
148 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000149 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000150
Chris Lattner90aa8392006-10-04 21:52:35 +0000151 if (ActiveTimers->back() == this) {
152 ActiveTimers->pop_back();
Chris Lattner8f0d8242002-11-18 21:47:09 +0000153 } else {
154 std::vector<Timer*>::iterator I =
Chris Lattner90aa8392006-10-04 21:52:35 +0000155 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
156 assert(I != ActiveTimers->end() && "stop but no startTimer?");
157 ActiveTimers->erase(I);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000158 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000159}
160
161void Timer::sum(const Timer &T) {
162 Elapsed += T.Elapsed;
163 UserTime += T.UserTime;
164 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000165 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000166 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000167}
168
Chris Lattner8f0d8242002-11-18 21:47:09 +0000169/// addPeakMemoryMeasurement - This method should be called whenever memory
170/// usage needs to be checked. It adds a peak memory measurement to the
171/// currently active timers, which will be printed when the timer group prints
172///
173void Timer::addPeakMemoryMeasurement() {
Jeff Cohene269a1a2005-01-08 20:15:57 +0000174 size_t MemUsed = getMemUsage();
Chris Lattner8f0d8242002-11-18 21:47:09 +0000175
Chris Lattner90aa8392006-10-04 21:52:35 +0000176 for (std::vector<Timer*>::iterator I = ActiveTimers->begin(),
177 E = ActiveTimers->end(); I != E; ++I)
Chris Lattner8f0d8242002-11-18 21:47:09 +0000178 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
179}
180
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000181//===----------------------------------------------------------------------===//
182// NamedRegionTimer Implementation
183//===----------------------------------------------------------------------===//
184
Chris Lattner90aa8392006-10-04 21:52:35 +0000185static ManagedStatic<std::map<std::string, Timer> > NamedTimers;
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000186
Chris Lattner90aa8392006-10-04 21:52:35 +0000187static Timer &getNamedRegionTimer(const std::string &Name) {
188 std::map<std::string, Timer>::iterator I = NamedTimers->lower_bound(Name);
189 if (I != NamedTimers->end() && I->first == Name)
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000190 return I->second;
191
Chris Lattner90aa8392006-10-04 21:52:35 +0000192 return NamedTimers->insert(I, std::make_pair(Name, Timer(Name)))->second;
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000193}
194
195NamedRegionTimer::NamedRegionTimer(const std::string &Name)
196 : TimeRegion(getNamedRegionTimer(Name)) {}
197
Chris Lattner8f0d8242002-11-18 21:47:09 +0000198
Chris Lattner6c38a792002-10-01 19:36:54 +0000199//===----------------------------------------------------------------------===//
200// TimerGroup Implementation
201//===----------------------------------------------------------------------===//
202
Chris Lattnerf205fec2003-05-09 20:05:44 +0000203// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
204// TotalWidth size, and B is the AfterDec size.
205//
206static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
207 std::ostream &OS) {
208 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
209 OS.width(TotalWidth-AfterDec-1);
210 char OldFill = OS.fill();
211 OS.fill(' ');
212 OS << (int)Val; // Integer part;
213 OS << ".";
214 OS.width(AfterDec);
215 OS.fill('0');
216 unsigned ResultFieldSize = 1;
217 while (AfterDec--) ResultFieldSize *= 10;
218 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
219 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000220}
221
Chris Lattnerf205fec2003-05-09 20:05:44 +0000222static void printVal(double Val, double Total, std::ostream &OS) {
223 if (Total < 1e-7) // Avoid dividing by zero...
224 OS << " ----- ";
225 else {
226 OS << " ";
227 printAlignedFP(Val, 4, 7, OS);
228 OS << " (";
229 printAlignedFP(Val*100/Total, 1, 5, OS);
230 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000231 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000232}
233
234void Timer::print(const Timer &Total, std::ostream &OS) {
235 if (Total.UserTime)
236 printVal(UserTime, Total.UserTime, OS);
237 if (Total.SystemTime)
238 printVal(SystemTime, Total.SystemTime, OS);
239 if (Total.getProcessTime())
240 printVal(getProcessTime(), Total.getProcessTime(), OS);
241 printVal(Elapsed, Total.Elapsed, OS);
Misha Brukmanf976c852005-04-21 22:55:34 +0000242
Chris Lattnerf205fec2003-05-09 20:05:44 +0000243 OS << " ";
244
245 if (Total.MemUsed) {
246 OS.width(9);
247 OS << MemUsed << " ";
248 }
249 if (Total.PeakMem) {
250 if (PeakMem) {
251 OS.width(9);
252 OS << PeakMem << " ";
253 } else
254 OS << " ";
255 }
256 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000257
258 Started = false; // Once printed, don't print again
259}
260
Chris Lattnerf205fec2003-05-09 20:05:44 +0000261// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
Brian Gaeked0fde302003-11-11 22:41:34 +0000262std::ostream *
Chris Lattnerb6d465f2003-12-14 21:27:33 +0000263llvm::GetLibSupportInfoOutputFile() {
Chris Lattner71336a92003-07-31 19:38:34 +0000264 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000265 if (LibSupportInfoOutputFilename.empty())
Bill Wendlingbcd24982006-12-07 20:28:15 +0000266 return cerr.stream();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000267 if (LibSupportInfoOutputFilename == "-")
Bill Wendlingbcd24982006-12-07 20:28:15 +0000268 return cout.stream();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000269
270 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
Chris Lattner903c2d12003-06-06 22:13:01 +0000271 std::ios::app);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000272 if (!Result->good()) {
Bill Wendlingbcd24982006-12-07 20:28:15 +0000273 cerr << "Error opening info-output-file '"
274 << LibSupportInfoOutputFilename << " for appending!\n";
Chris Lattnerf205fec2003-05-09 20:05:44 +0000275 delete Result;
Bill Wendlingbcd24982006-12-07 20:28:15 +0000276 return cerr.stream();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000277 }
278 return Result;
279}
280
Chris Lattner6c38a792002-10-01 19:36:54 +0000281
282void TimerGroup::removeTimer() {
283 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
284 // Sort the timers in descending order by amount of time taken...
285 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
286 std::greater<Timer>());
287
288 // Figure out how many spaces to indent TimerGroup name...
289 unsigned Padding = (80-Name.length())/2;
290 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
291
Chris Lattnerf205fec2003-05-09 20:05:44 +0000292 std::ostream *OutStream = GetLibSupportInfoOutputFile();
293
Chris Lattner6c38a792002-10-01 19:36:54 +0000294 ++NumTimers;
295 { // Scope to contain Total timer... don't allow total timer to drop us to
296 // zero timers...
297 Timer Total("TOTAL");
Misha Brukmanf976c852005-04-21 22:55:34 +0000298
Chris Lattner6c38a792002-10-01 19:36:54 +0000299 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
300 Total.sum(TimersToPrint[i]);
Misha Brukmanf976c852005-04-21 22:55:34 +0000301
Chris Lattner6c38a792002-10-01 19:36:54 +0000302 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000303 *OutStream << "===" << std::string(73, '-') << "===\n"
304 << std::string(Padding, ' ') << Name << "\n"
305 << "===" << std::string(73, '-')
Chris Lattner3ac96052005-02-09 18:41:32 +0000306 << "===\n";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000307
Chris Lattner3ac96052005-02-09 18:41:32 +0000308 // If this is not an collection of ungrouped times, print the total time.
309 // Ungrouped timers don't really make sense to add up. We still print the
310 // TOTAL line to make the percentages make sense.
311 if (this != DefaultTimerGroup) {
312 *OutStream << " Total Execution Time: ";
313
314 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
315 *OutStream << " seconds (";
316 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
317 *OutStream << " wall clock)\n";
318 }
319 *OutStream << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000320
321 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000322 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000323 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000324 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000325 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000326 *OutStream << " --User+System--";
327 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000328 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000329 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000330 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000331 *OutStream << " -PeakMem-";
332 *OutStream << " --- Name ---\n";
Misha Brukmanf976c852005-04-21 22:55:34 +0000333
Chris Lattner6c38a792002-10-01 19:36:54 +0000334 // Loop through all of the timing data, printing it out...
335 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000336 TimersToPrint[i].print(Total, *OutStream);
Misha Brukmanf976c852005-04-21 22:55:34 +0000337
Chris Lattnerf205fec2003-05-09 20:05:44 +0000338 Total.print(Total, *OutStream);
339 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000340 }
341 --NumTimers;
342
343 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000344
Bill Wendlingbcd24982006-12-07 20:28:15 +0000345 if (OutStream != cerr.stream() && OutStream != cout.stream())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000346 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000347 }
348
349 // Delete default timer group!
350 if (NumTimers == 0 && this == DefaultTimerGroup) {
351 delete DefaultTimerGroup;
352 DefaultTimerGroup = 0;
353 }
354}
Brian Gaeked0fde302003-11-11 22:41:34 +0000355