blob: 5e84f38852b722d3ffb5c75566b0ba2050e9011b [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"
John Criswell7a73b802003-06-30 21:59:07 +000016#include "Config/sys/resource.h"
17#include "Config/sys/time.h"
18#include "Config/unistd.h"
19#include "Config/malloc.h"
Chris Lattner6c38a792002-10-01 19:36:54 +000020#include <iostream>
21#include <algorithm>
Chris Lattner9550dc22002-10-27 19:08:03 +000022#include <functional>
Chris Lattnerf205fec2003-05-09 20:05:44 +000023#include <fstream>
Chris Lattnerd5a310e2003-10-06 15:02:31 +000024#include <map>
Chris Lattnerf205fec2003-05-09 20:05:44 +000025
Chris Lattner71336a92003-07-31 19:38:34 +000026// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
27// of constructor/destructor ordering being unspecified by C++. Basically the
28// problem is that a Statistic<> object gets destroyed, which ends up calling
29// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
30// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
31// would get destroyed before the Statistic, causing havoc to ensue. We "fix"
32// this by creating the string the first time it is needed and never destroying
33// it.
34static std::string &getLibSupportInfoOutputFilename() {
35 static std::string *LibSupportInfoOutputFilename = new std::string();
36 return *LibSupportInfoOutputFilename;
37}
Chris Lattner6c38a792002-10-01 19:36:54 +000038
Chris Lattner3f398492003-01-30 23:08:50 +000039namespace {
John Criswell7a73b802003-06-30 21:59:07 +000040#ifdef HAVE_MALLINFO
Chris Lattner3f398492003-01-30 23:08:50 +000041 cl::opt<bool>
42 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
43 "tracking (this may be slow)"),
44 cl::Hidden);
John Criswell7a73b802003-06-30 21:59:07 +000045#endif
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
Chris Lattner8f0d8242002-11-18 21:47:09 +000096static long getMemUsage() {
John Criswell7a73b802003-06-30 21:59:07 +000097#ifdef HAVE_MALLINFO
Chris Lattner3f398492003-01-30 23:08:50 +000098 if (TrackSpace) {
99 struct mallinfo MI = mallinfo();
Chris Lattnere040f972003-02-13 05:07:53 +0000100 return MI.uordblks/*+MI.hblkhd*/;
Chris Lattner3f398492003-01-30 23:08:50 +0000101 }
John Criswell7a73b802003-06-30 21:59:07 +0000102#endif
Brian Gaeke8c638832003-06-17 19:54:00 +0000103 return 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000104}
105
Chris Lattner6c38a792002-10-01 19:36:54 +0000106struct TimeRecord {
107 double Elapsed, UserTime, SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000108 long MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000109};
110
Chris Lattner8f0d8242002-11-18 21:47:09 +0000111static TimeRecord getTimeRecord(bool Start) {
Chris Lattner6c38a792002-10-01 19:36:54 +0000112 struct rusage RU;
113 struct timeval T;
Chris Lattnerbbe5ac12003-02-05 21:44:28 +0000114 long MemUsed = 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000115 if (Start) {
116 MemUsed = getMemUsage();
117 if (getrusage(RUSAGE_SELF, &RU))
118 perror("getrusage call failed: -time-passes info incorrect!");
119 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000120 gettimeofday(&T, 0);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000121
122 if (!Start) {
123 MemUsed = getMemUsage();
124 if (getrusage(RUSAGE_SELF, &RU))
125 perror("getrusage call failed: -time-passes info incorrect!");
Chris Lattner6c38a792002-10-01 19:36:54 +0000126 }
127
128 TimeRecord Result;
129 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
130 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
131 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000132 Result.MemUsed = MemUsed;
133
Chris Lattner6c38a792002-10-01 19:36:54 +0000134 return Result;
135}
136
Chris Lattner8f0d8242002-11-18 21:47:09 +0000137static std::vector<Timer*> ActiveTimers;
138
Chris Lattner6c38a792002-10-01 19:36:54 +0000139void Timer::startTimer() {
140 Started = true;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000141 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000142 Elapsed -= TR.Elapsed;
143 UserTime -= TR.UserTime;
144 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000145 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000146 PeakMemBase = TR.MemUsed;
147 ActiveTimers.push_back(this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000148}
149
150void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000151 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000152 Elapsed += TR.Elapsed;
153 UserTime += TR.UserTime;
154 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000155 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000156
157 if (ActiveTimers.back() == this) {
158 ActiveTimers.pop_back();
159 } else {
160 std::vector<Timer*>::iterator I =
161 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
162 assert(I != ActiveTimers.end() && "stop but no startTimer?");
163 ActiveTimers.erase(I);
164 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000165}
166
167void Timer::sum(const Timer &T) {
168 Elapsed += T.Elapsed;
169 UserTime += T.UserTime;
170 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000171 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000172 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000173}
174
Chris Lattner8f0d8242002-11-18 21:47:09 +0000175/// addPeakMemoryMeasurement - This method should be called whenever memory
176/// usage needs to be checked. It adds a peak memory measurement to the
177/// currently active timers, which will be printed when the timer group prints
178///
179void Timer::addPeakMemoryMeasurement() {
180 long MemUsed = getMemUsage();
181
182 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
183 E = ActiveTimers.end(); I != E; ++I)
184 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
185}
186
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000187//===----------------------------------------------------------------------===//
188// NamedRegionTimer Implementation
189//===----------------------------------------------------------------------===//
190
191static Timer &getNamedRegionTimer(const std::string &Name) {
192 static std::map<std::string, Timer> NamedTimers;
193
194 std::map<std::string, Timer>::iterator I = NamedTimers.lower_bound(Name);
195 if (I != NamedTimers.end() && I->first == Name)
196 return I->second;
197
198 return NamedTimers.insert(I, std::make_pair(Name, Timer(Name)))->second;
199}
200
201NamedRegionTimer::NamedRegionTimer(const std::string &Name)
202 : TimeRegion(getNamedRegionTimer(Name)) {}
203
Chris Lattner8f0d8242002-11-18 21:47:09 +0000204
Chris Lattner6c38a792002-10-01 19:36:54 +0000205//===----------------------------------------------------------------------===//
206// TimerGroup Implementation
207//===----------------------------------------------------------------------===//
208
Chris Lattnerf205fec2003-05-09 20:05:44 +0000209// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
210// TotalWidth size, and B is the AfterDec size.
211//
212static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
213 std::ostream &OS) {
214 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
215 OS.width(TotalWidth-AfterDec-1);
216 char OldFill = OS.fill();
217 OS.fill(' ');
218 OS << (int)Val; // Integer part;
219 OS << ".";
220 OS.width(AfterDec);
221 OS.fill('0');
222 unsigned ResultFieldSize = 1;
223 while (AfterDec--) ResultFieldSize *= 10;
224 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
225 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000226}
227
Chris Lattnerf205fec2003-05-09 20:05:44 +0000228static void printVal(double Val, double Total, std::ostream &OS) {
229 if (Total < 1e-7) // Avoid dividing by zero...
230 OS << " ----- ";
231 else {
232 OS << " ";
233 printAlignedFP(Val, 4, 7, OS);
234 OS << " (";
235 printAlignedFP(Val*100/Total, 1, 5, OS);
236 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000237 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000238}
239
240void Timer::print(const Timer &Total, std::ostream &OS) {
241 if (Total.UserTime)
242 printVal(UserTime, Total.UserTime, OS);
243 if (Total.SystemTime)
244 printVal(SystemTime, Total.SystemTime, OS);
245 if (Total.getProcessTime())
246 printVal(getProcessTime(), Total.getProcessTime(), OS);
247 printVal(Elapsed, Total.Elapsed, OS);
248
249 OS << " ";
250
251 if (Total.MemUsed) {
252 OS.width(9);
253 OS << MemUsed << " ";
254 }
255 if (Total.PeakMem) {
256 if (PeakMem) {
257 OS.width(9);
258 OS << PeakMem << " ";
259 } else
260 OS << " ";
261 }
262 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000263
264 Started = false; // Once printed, don't print again
265}
266
Chris Lattnerf205fec2003-05-09 20:05:44 +0000267// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
268std::ostream *GetLibSupportInfoOutputFile() {
Chris Lattner71336a92003-07-31 19:38:34 +0000269 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000270 if (LibSupportInfoOutputFilename.empty())
271 return &std::cerr;
272 if (LibSupportInfoOutputFilename == "-")
273 return &std::cout;
274
275 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
Chris Lattner903c2d12003-06-06 22:13:01 +0000276 std::ios::app);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000277 if (!Result->good()) {
278 std::cerr << "Error opening info-output-file '"
279 << LibSupportInfoOutputFilename << " for appending!\n";
280 delete Result;
281 return &std::cerr;
282 }
283 return Result;
284}
285
Chris Lattner6c38a792002-10-01 19:36:54 +0000286
287void TimerGroup::removeTimer() {
288 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
289 // Sort the timers in descending order by amount of time taken...
290 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
291 std::greater<Timer>());
292
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
Chris Lattnerf205fec2003-05-09 20:05:44 +0000297 std::ostream *OutStream = GetLibSupportInfoOutputFile();
298
Chris Lattner6c38a792002-10-01 19:36:54 +0000299 ++NumTimers;
300 { // Scope to contain Total timer... don't allow total timer to drop us to
301 // zero timers...
302 Timer Total("TOTAL");
303
304 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
305 Total.sum(TimersToPrint[i]);
306
307 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000308 *OutStream << "===" << std::string(73, '-') << "===\n"
309 << std::string(Padding, ' ') << Name << "\n"
310 << "===" << std::string(73, '-')
311 << "===\n Total Execution Time: ";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000312
Chris Lattnerf205fec2003-05-09 20:05:44 +0000313 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
314 *OutStream << " seconds (";
315 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
316 *OutStream << " wall clock)\n\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000317
318 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000319 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000320 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000321 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000322 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000323 *OutStream << " --User+System--";
324 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000325 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000326 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000327 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000328 *OutStream << " -PeakMem-";
329 *OutStream << " --- Name ---\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000330
331 // Loop through all of the timing data, printing it out...
332 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000333 TimersToPrint[i].print(Total, *OutStream);
Chris Lattner6c38a792002-10-01 19:36:54 +0000334
Chris Lattnerf205fec2003-05-09 20:05:44 +0000335 Total.print(Total, *OutStream);
336 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000337 }
338 --NumTimers;
339
340 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000341
342 if (OutStream != &std::cerr && OutStream != &std::cout)
343 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000344 }
345
346 // Delete default timer group!
347 if (NumTimers == 0 && this == DefaultTimerGroup) {
348 delete DefaultTimerGroup;
349 DefaultTimerGroup = 0;
350 }
351}