blob: d14a225fcf26c7aba4e1743b4e79e945cdc04495 [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
Brian Gaeked0fde302003-11-11 22:41:34 +000026namespace llvm {
27
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
30// problem is that a Statistic<> object gets destroyed, which ends up calling
31// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
32// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
33// 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.
36static std::string &getLibSupportInfoOutputFilename() {
37 static std::string *LibSupportInfoOutputFilename = new std::string();
38 return *LibSupportInfoOutputFilename;
39}
Chris Lattner6c38a792002-10-01 19:36:54 +000040
Chris Lattner3f398492003-01-30 23:08:50 +000041namespace {
John Criswell7a73b802003-06-30 21:59:07 +000042#ifdef HAVE_MALLINFO
Chris Lattner3f398492003-01-30 23:08:50 +000043 cl::opt<bool>
44 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
45 "tracking (this may be slow)"),
46 cl::Hidden);
John Criswell7a73b802003-06-30 21:59:07 +000047#endif
Chris Lattnerf205fec2003-05-09 20:05:44 +000048
49 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 Lattner6c38a792002-10-01 19:36:54 +000055static TimerGroup *DefaultTimerGroup = 0;
56static TimerGroup *getDefaultTimerGroup() {
57 if (DefaultTimerGroup) return DefaultTimerGroup;
58 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
59}
60
61Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000062 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000063 Started(false), TG(getDefaultTimerGroup()) {
64 TG->addTimer();
65}
66
67Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000068 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000069 Started(false), TG(&tg) {
70 TG->addTimer();
71}
72
73Timer::Timer(const Timer &T) {
74 TG = T.TG;
75 if (TG) TG->addTimer();
76 operator=(T);
77}
78
79
80// Copy ctor, initialize with no TG member.
81Timer::Timer(bool, const Timer &T) {
82 TG = T.TG; // Avoid assertion in operator=
83 operator=(T); // Copy contents
84 TG = 0;
85}
86
87
88Timer::~Timer() {
89 if (TG) {
90 if (Started) {
91 Started = false;
92 TG->addTimerToPrint(*this);
93 }
94 TG->removeTimer();
95 }
96}
97
Chris Lattner8f0d8242002-11-18 21:47:09 +000098static long getMemUsage() {
John Criswell7a73b802003-06-30 21:59:07 +000099#ifdef HAVE_MALLINFO
Chris Lattner3f398492003-01-30 23:08:50 +0000100 if (TrackSpace) {
101 struct mallinfo MI = mallinfo();
Chris Lattnere040f972003-02-13 05:07:53 +0000102 return MI.uordblks/*+MI.hblkhd*/;
Chris Lattner3f398492003-01-30 23:08:50 +0000103 }
John Criswell7a73b802003-06-30 21:59:07 +0000104#endif
Brian Gaeke8c638832003-06-17 19:54:00 +0000105 return 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000106}
107
Chris Lattner6c38a792002-10-01 19:36:54 +0000108struct TimeRecord {
109 double Elapsed, UserTime, SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000110 long MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000111};
112
Chris Lattner8f0d8242002-11-18 21:47:09 +0000113static TimeRecord getTimeRecord(bool Start) {
Chris Lattner6c38a792002-10-01 19:36:54 +0000114 struct rusage RU;
115 struct timeval T;
Chris Lattnerbbe5ac12003-02-05 21:44:28 +0000116 long MemUsed = 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000117 if (Start) {
118 MemUsed = getMemUsage();
119 if (getrusage(RUSAGE_SELF, &RU))
120 perror("getrusage call failed: -time-passes info incorrect!");
121 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000122 gettimeofday(&T, 0);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000123
124 if (!Start) {
125 MemUsed = getMemUsage();
126 if (getrusage(RUSAGE_SELF, &RU))
127 perror("getrusage call failed: -time-passes info incorrect!");
Chris Lattner6c38a792002-10-01 19:36:54 +0000128 }
129
130 TimeRecord Result;
131 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
132 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
133 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000134 Result.MemUsed = MemUsed;
135
Chris Lattner6c38a792002-10-01 19:36:54 +0000136 return Result;
137}
138
Chris Lattner8f0d8242002-11-18 21:47:09 +0000139static std::vector<Timer*> ActiveTimers;
140
Chris Lattner6c38a792002-10-01 19:36:54 +0000141void Timer::startTimer() {
142 Started = true;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000143 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000144 Elapsed -= TR.Elapsed;
145 UserTime -= TR.UserTime;
146 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000147 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000148 PeakMemBase = TR.MemUsed;
149 ActiveTimers.push_back(this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000150}
151
152void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000153 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000154 Elapsed += TR.Elapsed;
155 UserTime += TR.UserTime;
156 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000157 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000158
159 if (ActiveTimers.back() == this) {
160 ActiveTimers.pop_back();
161 } else {
162 std::vector<Timer*>::iterator I =
163 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
164 assert(I != ActiveTimers.end() && "stop but no startTimer?");
165 ActiveTimers.erase(I);
166 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000167}
168
169void Timer::sum(const Timer &T) {
170 Elapsed += T.Elapsed;
171 UserTime += T.UserTime;
172 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000173 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000174 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000175}
176
Chris Lattner8f0d8242002-11-18 21:47:09 +0000177/// addPeakMemoryMeasurement - This method should be called whenever memory
178/// usage needs to be checked. It adds a peak memory measurement to the
179/// currently active timers, which will be printed when the timer group prints
180///
181void Timer::addPeakMemoryMeasurement() {
182 long MemUsed = getMemUsage();
183
184 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
185 E = ActiveTimers.end(); I != E; ++I)
186 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
187}
188
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000189//===----------------------------------------------------------------------===//
190// NamedRegionTimer Implementation
191//===----------------------------------------------------------------------===//
192
193static Timer &getNamedRegionTimer(const std::string &Name) {
194 static std::map<std::string, Timer> NamedTimers;
195
196 std::map<std::string, Timer>::iterator I = NamedTimers.lower_bound(Name);
197 if (I != NamedTimers.end() && I->first == Name)
198 return I->second;
199
200 return NamedTimers.insert(I, std::make_pair(Name, Timer(Name)))->second;
201}
202
203NamedRegionTimer::NamedRegionTimer(const std::string &Name)
204 : TimeRegion(getNamedRegionTimer(Name)) {}
205
Chris Lattner8f0d8242002-11-18 21:47:09 +0000206
Chris Lattner6c38a792002-10-01 19:36:54 +0000207//===----------------------------------------------------------------------===//
208// TimerGroup Implementation
209//===----------------------------------------------------------------------===//
210
Chris Lattnerf205fec2003-05-09 20:05:44 +0000211// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
212// TotalWidth size, and B is the AfterDec size.
213//
214static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
215 std::ostream &OS) {
216 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
217 OS.width(TotalWidth-AfterDec-1);
218 char OldFill = OS.fill();
219 OS.fill(' ');
220 OS << (int)Val; // Integer part;
221 OS << ".";
222 OS.width(AfterDec);
223 OS.fill('0');
224 unsigned ResultFieldSize = 1;
225 while (AfterDec--) ResultFieldSize *= 10;
226 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
227 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000228}
229
Chris Lattnerf205fec2003-05-09 20:05:44 +0000230static void printVal(double Val, double Total, std::ostream &OS) {
231 if (Total < 1e-7) // Avoid dividing by zero...
232 OS << " ----- ";
233 else {
234 OS << " ";
235 printAlignedFP(Val, 4, 7, OS);
236 OS << " (";
237 printAlignedFP(Val*100/Total, 1, 5, OS);
238 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000239 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000240}
241
242void Timer::print(const Timer &Total, std::ostream &OS) {
243 if (Total.UserTime)
244 printVal(UserTime, Total.UserTime, OS);
245 if (Total.SystemTime)
246 printVal(SystemTime, Total.SystemTime, OS);
247 if (Total.getProcessTime())
248 printVal(getProcessTime(), Total.getProcessTime(), OS);
249 printVal(Elapsed, Total.Elapsed, OS);
250
251 OS << " ";
252
253 if (Total.MemUsed) {
254 OS.width(9);
255 OS << MemUsed << " ";
256 }
257 if (Total.PeakMem) {
258 if (PeakMem) {
259 OS.width(9);
260 OS << PeakMem << " ";
261 } else
262 OS << " ";
263 }
264 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000265
266 Started = false; // Once printed, don't print again
267}
268
Chris Lattnerf205fec2003-05-09 20:05:44 +0000269// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
Brian Gaeked0fde302003-11-11 22:41:34 +0000270std::ostream *
271GetLibSupportInfoOutputFile() {
Chris Lattner71336a92003-07-31 19:38:34 +0000272 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000273 if (LibSupportInfoOutputFilename.empty())
274 return &std::cerr;
275 if (LibSupportInfoOutputFilename == "-")
276 return &std::cout;
277
278 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
Chris Lattner903c2d12003-06-06 22:13:01 +0000279 std::ios::app);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000280 if (!Result->good()) {
281 std::cerr << "Error opening info-output-file '"
282 << LibSupportInfoOutputFilename << " for appending!\n";
283 delete Result;
284 return &std::cerr;
285 }
286 return Result;
287}
288
Chris Lattner6c38a792002-10-01 19:36:54 +0000289
290void TimerGroup::removeTimer() {
291 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
292 // Sort the timers in descending order by amount of time taken...
293 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
294 std::greater<Timer>());
295
296 // Figure out how many spaces to indent TimerGroup name...
297 unsigned Padding = (80-Name.length())/2;
298 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
299
Chris Lattnerf205fec2003-05-09 20:05:44 +0000300 std::ostream *OutStream = GetLibSupportInfoOutputFile();
301
Chris Lattner6c38a792002-10-01 19:36:54 +0000302 ++NumTimers;
303 { // Scope to contain Total timer... don't allow total timer to drop us to
304 // zero timers...
305 Timer Total("TOTAL");
306
307 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
308 Total.sum(TimersToPrint[i]);
309
310 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000311 *OutStream << "===" << std::string(73, '-') << "===\n"
312 << std::string(Padding, ' ') << Name << "\n"
313 << "===" << std::string(73, '-')
314 << "===\n Total Execution Time: ";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000315
Chris Lattnerf205fec2003-05-09 20:05:44 +0000316 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
317 *OutStream << " seconds (";
318 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
319 *OutStream << " wall clock)\n\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";
Chris Lattner6c38a792002-10-01 19:36:54 +0000333
334 // 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);
Chris Lattner6c38a792002-10-01 19:36:54 +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
345 if (OutStream != &std::cerr && OutStream != &std::cout)
346 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
356} // End llvm namespace