blob: 2542a6a2b4cd0b40e18c827af93d261dd4f060f1 [file] [log] [blame]
Chris Lattner6c38a792002-10-01 19:36:54 +00001//===-- Timer.cpp - Interval Timing Support -------------------------------===//
2//
3// Interval Timing implementation.
4//
5//===----------------------------------------------------------------------===//
6
7#include "Support/Timer.h"
Chris Lattner3f398492003-01-30 23:08:50 +00008#include "Support/CommandLine.h"
John Criswell7a73b802003-06-30 21:59:07 +00009
10#include "Config/sys/resource.h"
11#include "Config/sys/time.h"
12#include "Config/unistd.h"
13#include "Config/malloc.h"
14#include "Config/stdio.h"
Chris Lattner6c38a792002-10-01 19:36:54 +000015#include <iostream>
16#include <algorithm>
Chris Lattner9550dc22002-10-27 19:08:03 +000017#include <functional>
Chris Lattnerf205fec2003-05-09 20:05:44 +000018#include <fstream>
Chris Lattnerd5a310e2003-10-06 15:02:31 +000019#include <map>
Chris Lattnerf205fec2003-05-09 20:05:44 +000020
Chris Lattner71336a92003-07-31 19:38:34 +000021// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
22// of constructor/destructor ordering being unspecified by C++. Basically the
23// problem is that a Statistic<> object gets destroyed, which ends up calling
24// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
25// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
26// would get destroyed before the Statistic, causing havoc to ensue. We "fix"
27// this by creating the string the first time it is needed and never destroying
28// it.
29static std::string &getLibSupportInfoOutputFilename() {
30 static std::string *LibSupportInfoOutputFilename = new std::string();
31 return *LibSupportInfoOutputFilename;
32}
Chris Lattner6c38a792002-10-01 19:36:54 +000033
Chris Lattner3f398492003-01-30 23:08:50 +000034namespace {
John Criswell7a73b802003-06-30 21:59:07 +000035#ifdef HAVE_MALLINFO
Chris Lattner3f398492003-01-30 23:08:50 +000036 cl::opt<bool>
37 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
38 "tracking (this may be slow)"),
39 cl::Hidden);
John Criswell7a73b802003-06-30 21:59:07 +000040#endif
Chris Lattnerf205fec2003-05-09 20:05:44 +000041
42 cl::opt<std::string, true>
Chris Lattner96a54db2003-08-01 22:15:15 +000043 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerf205fec2003-05-09 20:05:44 +000044 cl::desc("File to append -stats and -timer output to"),
Chris Lattner71336a92003-07-31 19:38:34 +000045 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Chris Lattner3f398492003-01-30 23:08:50 +000046}
47
Chris Lattner6c38a792002-10-01 19:36:54 +000048static TimerGroup *DefaultTimerGroup = 0;
49static TimerGroup *getDefaultTimerGroup() {
50 if (DefaultTimerGroup) return DefaultTimerGroup;
51 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
52}
53
54Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000055 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000056 Started(false), TG(getDefaultTimerGroup()) {
57 TG->addTimer();
58}
59
60Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000061 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000062 Started(false), TG(&tg) {
63 TG->addTimer();
64}
65
66Timer::Timer(const Timer &T) {
67 TG = T.TG;
68 if (TG) TG->addTimer();
69 operator=(T);
70}
71
72
73// Copy ctor, initialize with no TG member.
74Timer::Timer(bool, const Timer &T) {
75 TG = T.TG; // Avoid assertion in operator=
76 operator=(T); // Copy contents
77 TG = 0;
78}
79
80
81Timer::~Timer() {
82 if (TG) {
83 if (Started) {
84 Started = false;
85 TG->addTimerToPrint(*this);
86 }
87 TG->removeTimer();
88 }
89}
90
Chris Lattner8f0d8242002-11-18 21:47:09 +000091static long getMemUsage() {
John Criswell7a73b802003-06-30 21:59:07 +000092#ifdef HAVE_MALLINFO
Chris Lattner3f398492003-01-30 23:08:50 +000093 if (TrackSpace) {
94 struct mallinfo MI = mallinfo();
Chris Lattnere040f972003-02-13 05:07:53 +000095 return MI.uordblks/*+MI.hblkhd*/;
Chris Lattner3f398492003-01-30 23:08:50 +000096 }
John Criswell7a73b802003-06-30 21:59:07 +000097#endif
Brian Gaeke8c638832003-06-17 19:54:00 +000098 return 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +000099}
100
Chris Lattner6c38a792002-10-01 19:36:54 +0000101struct TimeRecord {
102 double Elapsed, UserTime, SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000103 long MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000104};
105
Chris Lattner8f0d8242002-11-18 21:47:09 +0000106static TimeRecord getTimeRecord(bool Start) {
Chris Lattner6c38a792002-10-01 19:36:54 +0000107 struct rusage RU;
108 struct timeval T;
Chris Lattnerbbe5ac12003-02-05 21:44:28 +0000109 long MemUsed = 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000110 if (Start) {
111 MemUsed = getMemUsage();
112 if (getrusage(RUSAGE_SELF, &RU))
113 perror("getrusage call failed: -time-passes info incorrect!");
114 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000115 gettimeofday(&T, 0);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000116
117 if (!Start) {
118 MemUsed = getMemUsage();
119 if (getrusage(RUSAGE_SELF, &RU))
120 perror("getrusage call failed: -time-passes info incorrect!");
Chris Lattner6c38a792002-10-01 19:36:54 +0000121 }
122
123 TimeRecord Result;
124 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
125 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
126 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000127 Result.MemUsed = MemUsed;
128
Chris Lattner6c38a792002-10-01 19:36:54 +0000129 return Result;
130}
131
Chris Lattner8f0d8242002-11-18 21:47:09 +0000132static std::vector<Timer*> ActiveTimers;
133
Chris Lattner6c38a792002-10-01 19:36:54 +0000134void Timer::startTimer() {
135 Started = true;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000136 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000137 Elapsed -= TR.Elapsed;
138 UserTime -= TR.UserTime;
139 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000140 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000141 PeakMemBase = TR.MemUsed;
142 ActiveTimers.push_back(this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000143}
144
145void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000146 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000147 Elapsed += TR.Elapsed;
148 UserTime += TR.UserTime;
149 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000150 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000151
152 if (ActiveTimers.back() == this) {
153 ActiveTimers.pop_back();
154 } else {
155 std::vector<Timer*>::iterator I =
156 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
157 assert(I != ActiveTimers.end() && "stop but no startTimer?");
158 ActiveTimers.erase(I);
159 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000160}
161
162void Timer::sum(const Timer &T) {
163 Elapsed += T.Elapsed;
164 UserTime += T.UserTime;
165 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000166 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000167 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000168}
169
Chris Lattner8f0d8242002-11-18 21:47:09 +0000170/// addPeakMemoryMeasurement - This method should be called whenever memory
171/// usage needs to be checked. It adds a peak memory measurement to the
172/// currently active timers, which will be printed when the timer group prints
173///
174void Timer::addPeakMemoryMeasurement() {
175 long MemUsed = getMemUsage();
176
177 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
178 E = ActiveTimers.end(); I != E; ++I)
179 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
180}
181
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000182//===----------------------------------------------------------------------===//
183// NamedRegionTimer Implementation
184//===----------------------------------------------------------------------===//
185
186static Timer &getNamedRegionTimer(const std::string &Name) {
187 static std::map<std::string, Timer> NamedTimers;
188
189 std::map<std::string, Timer>::iterator I = NamedTimers.lower_bound(Name);
190 if (I != NamedTimers.end() && I->first == Name)
191 return I->second;
192
193 return NamedTimers.insert(I, std::make_pair(Name, Timer(Name)))->second;
194}
195
196NamedRegionTimer::NamedRegionTimer(const std::string &Name)
197 : TimeRegion(getNamedRegionTimer(Name)) {}
198
Chris Lattner8f0d8242002-11-18 21:47:09 +0000199
Chris Lattner6c38a792002-10-01 19:36:54 +0000200//===----------------------------------------------------------------------===//
201// TimerGroup Implementation
202//===----------------------------------------------------------------------===//
203
Chris Lattnerf205fec2003-05-09 20:05:44 +0000204// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
205// TotalWidth size, and B is the AfterDec size.
206//
207static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
208 std::ostream &OS) {
209 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
210 OS.width(TotalWidth-AfterDec-1);
211 char OldFill = OS.fill();
212 OS.fill(' ');
213 OS << (int)Val; // Integer part;
214 OS << ".";
215 OS.width(AfterDec);
216 OS.fill('0');
217 unsigned ResultFieldSize = 1;
218 while (AfterDec--) ResultFieldSize *= 10;
219 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
220 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000221}
222
Chris Lattnerf205fec2003-05-09 20:05:44 +0000223static void printVal(double Val, double Total, std::ostream &OS) {
224 if (Total < 1e-7) // Avoid dividing by zero...
225 OS << " ----- ";
226 else {
227 OS << " ";
228 printAlignedFP(Val, 4, 7, OS);
229 OS << " (";
230 printAlignedFP(Val*100/Total, 1, 5, OS);
231 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000232 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000233}
234
235void Timer::print(const Timer &Total, std::ostream &OS) {
236 if (Total.UserTime)
237 printVal(UserTime, Total.UserTime, OS);
238 if (Total.SystemTime)
239 printVal(SystemTime, Total.SystemTime, OS);
240 if (Total.getProcessTime())
241 printVal(getProcessTime(), Total.getProcessTime(), OS);
242 printVal(Elapsed, Total.Elapsed, OS);
243
244 OS << " ";
245
246 if (Total.MemUsed) {
247 OS.width(9);
248 OS << MemUsed << " ";
249 }
250 if (Total.PeakMem) {
251 if (PeakMem) {
252 OS.width(9);
253 OS << PeakMem << " ";
254 } else
255 OS << " ";
256 }
257 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000258
259 Started = false; // Once printed, don't print again
260}
261
Chris Lattnerf205fec2003-05-09 20:05:44 +0000262// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
263std::ostream *GetLibSupportInfoOutputFile() {
Chris Lattner71336a92003-07-31 19:38:34 +0000264 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000265 if (LibSupportInfoOutputFilename.empty())
266 return &std::cerr;
267 if (LibSupportInfoOutputFilename == "-")
268 return &std::cout;
269
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()) {
273 std::cerr << "Error opening info-output-file '"
274 << LibSupportInfoOutputFilename << " for appending!\n";
275 delete Result;
276 return &std::cerr;
277 }
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");
298
299 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
300 Total.sum(TimersToPrint[i]);
301
302 // 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, '-')
306 << "===\n Total Execution Time: ";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000307
Chris Lattnerf205fec2003-05-09 20:05:44 +0000308 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
309 *OutStream << " seconds (";
310 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
311 *OutStream << " wall clock)\n\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000312
313 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000314 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000315 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000316 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000317 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000318 *OutStream << " --User+System--";
319 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000320 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000321 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000322 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000323 *OutStream << " -PeakMem-";
324 *OutStream << " --- Name ---\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000325
326 // Loop through all of the timing data, printing it out...
327 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000328 TimersToPrint[i].print(Total, *OutStream);
Chris Lattner6c38a792002-10-01 19:36:54 +0000329
Chris Lattnerf205fec2003-05-09 20:05:44 +0000330 Total.print(Total, *OutStream);
331 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000332 }
333 --NumTimers;
334
335 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000336
337 if (OutStream != &std::cerr && OutStream != &std::cout)
338 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000339 }
340
341 // Delete default timer group!
342 if (NumTimers == 0 && this == DefaultTimerGroup) {
343 delete DefaultTimerGroup;
344 DefaultTimerGroup = 0;
345 }
346}