blob: 52b616fa2c42cbce5ea2efe3bdc4576e20bbf76f [file] [log] [blame]
Chris Lattner6c38a792002-10-01 19:36:54 +00001//===-- Timer.cpp - Interval Timing Support -------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner6c38a792002-10-01 19:36:54 +00009//
10// Interval Timing implementation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Support/Timer.h"
Chris Lattner3f398492003-01-30 23:08:50 +000015#include "Support/CommandLine.h"
Chris Lattnerb4db5f32004-06-07 19:34:51 +000016#include <algorithm>
17#include <iostream>
18#include <functional>
19#include <fstream>
20#include <map>
John Criswell7a73b802003-06-30 21:59:07 +000021#include "Config/sys/resource.h"
22#include "Config/sys/time.h"
23#include "Config/unistd.h"
24#include "Config/malloc.h"
Chris Lattnerb4db5f32004-06-07 19:34:51 +000025#include "Config/windows.h"
Chris Lattnerb6d465f2003-12-14 21:27:33 +000026using namespace llvm;
Chris Lattnerf205fec2003-05-09 20:05:44 +000027
Chris Lattnerb4db5f32004-06-07 19:34:51 +000028// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattnerb6d465f2003-12-14 21:27:33 +000029namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner71336a92003-07-31 19:38:34 +000031// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
32// of constructor/destructor ordering being unspecified by C++. Basically the
33// problem is that a Statistic<> object gets destroyed, which ends up calling
34// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
35// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
36// would get destroyed before the Statistic, causing havoc to ensue. We "fix"
37// this by creating the string the first time it is needed and never destroying
38// it.
39static std::string &getLibSupportInfoOutputFilename() {
40 static std::string *LibSupportInfoOutputFilename = new std::string();
41 return *LibSupportInfoOutputFilename;
42}
Chris Lattner6c38a792002-10-01 19:36:54 +000043
Chris Lattner3f398492003-01-30 23:08:50 +000044namespace {
John Criswell7a73b802003-06-30 21:59:07 +000045#ifdef HAVE_MALLINFO
Chris Lattner3f398492003-01-30 23:08:50 +000046 cl::opt<bool>
47 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
48 "tracking (this may be slow)"),
49 cl::Hidden);
John Criswell7a73b802003-06-30 21:59:07 +000050#endif
Chris Lattnerf205fec2003-05-09 20:05:44 +000051
52 cl::opt<std::string, true>
Chris Lattner96a54db2003-08-01 22:15:15 +000053 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerf205fec2003-05-09 20:05:44 +000054 cl::desc("File to append -stats and -timer output to"),
Chris Lattner71336a92003-07-31 19:38:34 +000055 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Chris Lattner3f398492003-01-30 23:08:50 +000056}
57
Chris Lattner6c38a792002-10-01 19:36:54 +000058static TimerGroup *DefaultTimerGroup = 0;
59static TimerGroup *getDefaultTimerGroup() {
60 if (DefaultTimerGroup) return DefaultTimerGroup;
61 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
62}
63
64Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000065 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000066 Started(false), TG(getDefaultTimerGroup()) {
67 TG->addTimer();
68}
69
70Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000071 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000072 Started(false), TG(&tg) {
73 TG->addTimer();
74}
75
76Timer::Timer(const Timer &T) {
77 TG = T.TG;
78 if (TG) TG->addTimer();
79 operator=(T);
80}
81
82
83// Copy ctor, initialize with no TG member.
84Timer::Timer(bool, const Timer &T) {
85 TG = T.TG; // Avoid assertion in operator=
86 operator=(T); // Copy contents
87 TG = 0;
88}
89
90
91Timer::~Timer() {
92 if (TG) {
93 if (Started) {
94 Started = false;
95 TG->addTimerToPrint(*this);
96 }
97 TG->removeTimer();
98 }
99}
100
Chris Lattner8f0d8242002-11-18 21:47:09 +0000101static long getMemUsage() {
John Criswell7a73b802003-06-30 21:59:07 +0000102#ifdef HAVE_MALLINFO
Chris Lattner3f398492003-01-30 23:08:50 +0000103 if (TrackSpace) {
104 struct mallinfo MI = mallinfo();
Chris Lattnere040f972003-02-13 05:07:53 +0000105 return MI.uordblks/*+MI.hblkhd*/;
Chris Lattner3f398492003-01-30 23:08:50 +0000106 }
John Criswell7a73b802003-06-30 21:59:07 +0000107#endif
Brian Gaeke8c638832003-06-17 19:54:00 +0000108 return 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000109}
110
Chris Lattner6c38a792002-10-01 19:36:54 +0000111struct TimeRecord {
112 double Elapsed, UserTime, SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000113 long MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000114};
115
Chris Lattner8f0d8242002-11-18 21:47:09 +0000116static TimeRecord getTimeRecord(bool Start) {
Chris Lattnerb4db5f32004-06-07 19:34:51 +0000117#if defined(HAVE_WINDOWS_H)
118 unsigned __int64 ProcCreate, ProcExit, KernelTime, UserTime, CurTime;
119
120 GetProcessTimes(GetCurrentProcess(), (FILETIME*)&ProcCreate,
121 (FILETIME*)&ProcExit, (FILETIME*)&KernelTime,
122 (FILETIME*)&UserTime);
123 GetSystemTimeAsFileTime((FILETIME*)&CurTime);
124
125 // FILETIME's are # of 100 nanosecond ticks.
126 double ScaleFactor = 1.0/(10*1000*1000);
127
128 TimeRecord Result;
129 Result.Elapsed = (CurTime-ProcCreate)*ScaleFactor; // Wall time
130 Result.UserTime = UserTime*ScaleFactor;
131 Result.SystemTime = KernelTime*ScaleFactor;
132 return Result;
133#elif defined(HAVE_GETRUSAGE)
Chris Lattner6c38a792002-10-01 19:36:54 +0000134 struct rusage RU;
135 struct timeval T;
Chris Lattnerbbe5ac12003-02-05 21:44:28 +0000136 long MemUsed = 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000137 if (Start) {
138 MemUsed = getMemUsage();
139 if (getrusage(RUSAGE_SELF, &RU))
140 perror("getrusage call failed: -time-passes info incorrect!");
141 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000142 gettimeofday(&T, 0);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000143
144 if (!Start) {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000145 if (getrusage(RUSAGE_SELF, &RU))
146 perror("getrusage call failed: -time-passes info incorrect!");
Chris Lattnerb6d465f2003-12-14 21:27:33 +0000147 MemUsed = getMemUsage();
Chris Lattner6c38a792002-10-01 19:36:54 +0000148 }
149
150 TimeRecord Result;
151 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
152 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
153 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000154 Result.MemUsed = MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000155 return Result;
Chris Lattnerb4db5f32004-06-07 19:34:51 +0000156#else
157 // Can't get resource usage.
158 return TimeRecord();
159#endif
Chris Lattner6c38a792002-10-01 19:36:54 +0000160}
161
Chris Lattner8f0d8242002-11-18 21:47:09 +0000162static std::vector<Timer*> ActiveTimers;
163
Chris Lattner6c38a792002-10-01 19:36:54 +0000164void Timer::startTimer() {
165 Started = true;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000166 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000167 Elapsed -= TR.Elapsed;
168 UserTime -= TR.UserTime;
169 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000170 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000171 PeakMemBase = TR.MemUsed;
172 ActiveTimers.push_back(this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000173}
174
175void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000176 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000177 Elapsed += TR.Elapsed;
178 UserTime += TR.UserTime;
179 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000180 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000181
182 if (ActiveTimers.back() == this) {
183 ActiveTimers.pop_back();
184 } else {
185 std::vector<Timer*>::iterator I =
186 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
187 assert(I != ActiveTimers.end() && "stop but no startTimer?");
188 ActiveTimers.erase(I);
189 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000190}
191
192void Timer::sum(const Timer &T) {
193 Elapsed += T.Elapsed;
194 UserTime += T.UserTime;
195 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000196 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000197 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000198}
199
Chris Lattner8f0d8242002-11-18 21:47:09 +0000200/// addPeakMemoryMeasurement - This method should be called whenever memory
201/// usage needs to be checked. It adds a peak memory measurement to the
202/// currently active timers, which will be printed when the timer group prints
203///
204void Timer::addPeakMemoryMeasurement() {
205 long MemUsed = getMemUsage();
206
207 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
208 E = ActiveTimers.end(); I != E; ++I)
209 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
210}
211
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000212//===----------------------------------------------------------------------===//
213// NamedRegionTimer Implementation
214//===----------------------------------------------------------------------===//
215
216static Timer &getNamedRegionTimer(const std::string &Name) {
217 static std::map<std::string, Timer> NamedTimers;
218
219 std::map<std::string, Timer>::iterator I = NamedTimers.lower_bound(Name);
220 if (I != NamedTimers.end() && I->first == Name)
221 return I->second;
222
223 return NamedTimers.insert(I, std::make_pair(Name, Timer(Name)))->second;
224}
225
226NamedRegionTimer::NamedRegionTimer(const std::string &Name)
227 : TimeRegion(getNamedRegionTimer(Name)) {}
228
Chris Lattner8f0d8242002-11-18 21:47:09 +0000229
Chris Lattner6c38a792002-10-01 19:36:54 +0000230//===----------------------------------------------------------------------===//
231// TimerGroup Implementation
232//===----------------------------------------------------------------------===//
233
Chris Lattnerf205fec2003-05-09 20:05:44 +0000234// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
235// TotalWidth size, and B is the AfterDec size.
236//
237static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
238 std::ostream &OS) {
239 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
240 OS.width(TotalWidth-AfterDec-1);
241 char OldFill = OS.fill();
242 OS.fill(' ');
243 OS << (int)Val; // Integer part;
244 OS << ".";
245 OS.width(AfterDec);
246 OS.fill('0');
247 unsigned ResultFieldSize = 1;
248 while (AfterDec--) ResultFieldSize *= 10;
249 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
250 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000251}
252
Chris Lattnerf205fec2003-05-09 20:05:44 +0000253static void printVal(double Val, double Total, std::ostream &OS) {
254 if (Total < 1e-7) // Avoid dividing by zero...
255 OS << " ----- ";
256 else {
257 OS << " ";
258 printAlignedFP(Val, 4, 7, OS);
259 OS << " (";
260 printAlignedFP(Val*100/Total, 1, 5, OS);
261 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000262 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000263}
264
265void Timer::print(const Timer &Total, std::ostream &OS) {
266 if (Total.UserTime)
267 printVal(UserTime, Total.UserTime, OS);
268 if (Total.SystemTime)
269 printVal(SystemTime, Total.SystemTime, OS);
270 if (Total.getProcessTime())
271 printVal(getProcessTime(), Total.getProcessTime(), OS);
272 printVal(Elapsed, Total.Elapsed, OS);
273
274 OS << " ";
275
276 if (Total.MemUsed) {
277 OS.width(9);
278 OS << MemUsed << " ";
279 }
280 if (Total.PeakMem) {
281 if (PeakMem) {
282 OS.width(9);
283 OS << PeakMem << " ";
284 } else
285 OS << " ";
286 }
287 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000288
289 Started = false; // Once printed, don't print again
290}
291
Chris Lattnerf205fec2003-05-09 20:05:44 +0000292// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
Brian Gaeked0fde302003-11-11 22:41:34 +0000293std::ostream *
Chris Lattnerb6d465f2003-12-14 21:27:33 +0000294llvm::GetLibSupportInfoOutputFile() {
Chris Lattner71336a92003-07-31 19:38:34 +0000295 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000296 if (LibSupportInfoOutputFilename.empty())
297 return &std::cerr;
298 if (LibSupportInfoOutputFilename == "-")
299 return &std::cout;
300
301 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
Chris Lattner903c2d12003-06-06 22:13:01 +0000302 std::ios::app);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000303 if (!Result->good()) {
304 std::cerr << "Error opening info-output-file '"
305 << LibSupportInfoOutputFilename << " for appending!\n";
306 delete Result;
307 return &std::cerr;
308 }
309 return Result;
310}
311
Chris Lattner6c38a792002-10-01 19:36:54 +0000312
313void TimerGroup::removeTimer() {
314 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
315 // Sort the timers in descending order by amount of time taken...
316 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
317 std::greater<Timer>());
318
319 // Figure out how many spaces to indent TimerGroup name...
320 unsigned Padding = (80-Name.length())/2;
321 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
322
Chris Lattnerf205fec2003-05-09 20:05:44 +0000323 std::ostream *OutStream = GetLibSupportInfoOutputFile();
324
Chris Lattner6c38a792002-10-01 19:36:54 +0000325 ++NumTimers;
326 { // Scope to contain Total timer... don't allow total timer to drop us to
327 // zero timers...
328 Timer Total("TOTAL");
329
330 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
331 Total.sum(TimersToPrint[i]);
332
333 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000334 *OutStream << "===" << std::string(73, '-') << "===\n"
335 << std::string(Padding, ' ') << Name << "\n"
336 << "===" << std::string(73, '-')
337 << "===\n Total Execution Time: ";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000338
Chris Lattnerf205fec2003-05-09 20:05:44 +0000339 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
340 *OutStream << " seconds (";
341 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
342 *OutStream << " wall clock)\n\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000343
344 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000345 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000346 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000347 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000348 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000349 *OutStream << " --User+System--";
350 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000351 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000352 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000353 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000354 *OutStream << " -PeakMem-";
355 *OutStream << " --- Name ---\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000356
357 // Loop through all of the timing data, printing it out...
358 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000359 TimersToPrint[i].print(Total, *OutStream);
Chris Lattner6c38a792002-10-01 19:36:54 +0000360
Chris Lattnerf205fec2003-05-09 20:05:44 +0000361 Total.print(Total, *OutStream);
362 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000363 }
364 --NumTimers;
365
366 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000367
368 if (OutStream != &std::cerr && OutStream != &std::cout)
369 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000370 }
371
372 // Delete default timer group!
373 if (NumTimers == 0 && this == DefaultTimerGroup) {
374 delete DefaultTimerGroup;
375 DefaultTimerGroup = 0;
376 }
377}
Brian Gaeked0fde302003-11-11 22:41:34 +0000378