blob: 79d4c1cfe82748f39262f2302a96db629fa232f7 [file] [log] [blame]
Chris Lattner6c38a792002-10-01 19:36:54 +00001//===-- Timer.cpp - Interval Timing Support -------------------------------===//
2//
3// Interval Timing implementation.
4//
5//===----------------------------------------------------------------------===//
6
John Criswell7a73b802003-06-30 21:59:07 +00007#include "Config/malloc.h"
8
Chris Lattner6c38a792002-10-01 19:36:54 +00009#include "Support/Timer.h"
Chris Lattner3f398492003-01-30 23:08:50 +000010#include "Support/CommandLine.h"
John Criswell7a73b802003-06-30 21:59:07 +000011
12#include "Config/sys/resource.h"
13#include "Config/sys/time.h"
14#include "Config/unistd.h"
15#include "Config/malloc.h"
16#include "Config/stdio.h"
Chris Lattner6c38a792002-10-01 19:36:54 +000017#include <iostream>
18#include <algorithm>
Chris Lattner9550dc22002-10-27 19:08:03 +000019#include <functional>
Chris Lattnerf205fec2003-05-09 20:05:44 +000020#include <fstream>
21
22std::string LibSupportInfoOutputFilename;
Chris Lattner6c38a792002-10-01 19:36:54 +000023
Chris Lattner3f398492003-01-30 23:08:50 +000024namespace {
John Criswell7a73b802003-06-30 21:59:07 +000025#ifdef HAVE_MALLINFO
Chris Lattner3f398492003-01-30 23:08:50 +000026 cl::opt<bool>
27 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
28 "tracking (this may be slow)"),
29 cl::Hidden);
John Criswell7a73b802003-06-30 21:59:07 +000030#endif
Chris Lattnerf205fec2003-05-09 20:05:44 +000031
32 cl::opt<std::string, true>
33 InfoOutputFilename("info-output-file",
34 cl::desc("File to append -stats and -timer output to"),
35 cl::Hidden, cl::location(LibSupportInfoOutputFilename));
Chris Lattner3f398492003-01-30 23:08:50 +000036}
37
Chris Lattner6c38a792002-10-01 19:36:54 +000038static TimerGroup *DefaultTimerGroup = 0;
39static TimerGroup *getDefaultTimerGroup() {
40 if (DefaultTimerGroup) return DefaultTimerGroup;
41 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
42}
43
44Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000045 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000046 Started(false), TG(getDefaultTimerGroup()) {
47 TG->addTimer();
48}
49
50Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000051 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000052 Started(false), TG(&tg) {
53 TG->addTimer();
54}
55
56Timer::Timer(const Timer &T) {
57 TG = T.TG;
58 if (TG) TG->addTimer();
59 operator=(T);
60}
61
62
63// Copy ctor, initialize with no TG member.
64Timer::Timer(bool, const Timer &T) {
65 TG = T.TG; // Avoid assertion in operator=
66 operator=(T); // Copy contents
67 TG = 0;
68}
69
70
71Timer::~Timer() {
72 if (TG) {
73 if (Started) {
74 Started = false;
75 TG->addTimerToPrint(*this);
76 }
77 TG->removeTimer();
78 }
79}
80
Chris Lattner8f0d8242002-11-18 21:47:09 +000081static long getMemUsage() {
John Criswell7a73b802003-06-30 21:59:07 +000082#ifdef HAVE_MALLINFO
Chris Lattner3f398492003-01-30 23:08:50 +000083 if (TrackSpace) {
84 struct mallinfo MI = mallinfo();
Chris Lattnere040f972003-02-13 05:07:53 +000085 return MI.uordblks/*+MI.hblkhd*/;
Chris Lattner3f398492003-01-30 23:08:50 +000086 }
John Criswell7a73b802003-06-30 21:59:07 +000087#endif
Brian Gaeke8c638832003-06-17 19:54:00 +000088 return 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +000089}
90
Chris Lattner6c38a792002-10-01 19:36:54 +000091struct TimeRecord {
92 double Elapsed, UserTime, SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +000093 long MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +000094};
95
Chris Lattner8f0d8242002-11-18 21:47:09 +000096static TimeRecord getTimeRecord(bool Start) {
Chris Lattner6c38a792002-10-01 19:36:54 +000097 struct rusage RU;
98 struct timeval T;
Chris Lattnerbbe5ac12003-02-05 21:44:28 +000099 long MemUsed = 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000100 if (Start) {
101 MemUsed = getMemUsage();
102 if (getrusage(RUSAGE_SELF, &RU))
103 perror("getrusage call failed: -time-passes info incorrect!");
104 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000105 gettimeofday(&T, 0);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000106
107 if (!Start) {
108 MemUsed = getMemUsage();
109 if (getrusage(RUSAGE_SELF, &RU))
110 perror("getrusage call failed: -time-passes info incorrect!");
Chris Lattner6c38a792002-10-01 19:36:54 +0000111 }
112
113 TimeRecord Result;
114 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
115 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
116 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000117 Result.MemUsed = MemUsed;
118
Chris Lattner6c38a792002-10-01 19:36:54 +0000119 return Result;
120}
121
Chris Lattner8f0d8242002-11-18 21:47:09 +0000122static std::vector<Timer*> ActiveTimers;
123
Chris Lattner6c38a792002-10-01 19:36:54 +0000124void Timer::startTimer() {
125 Started = true;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000126 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000127 Elapsed -= TR.Elapsed;
128 UserTime -= TR.UserTime;
129 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000130 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000131 PeakMemBase = TR.MemUsed;
132 ActiveTimers.push_back(this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000133}
134
135void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000136 TimeRecord TR = getTimeRecord(false);
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
142 if (ActiveTimers.back() == this) {
143 ActiveTimers.pop_back();
144 } else {
145 std::vector<Timer*>::iterator I =
146 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
147 assert(I != ActiveTimers.end() && "stop but no startTimer?");
148 ActiveTimers.erase(I);
149 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000150}
151
152void Timer::sum(const Timer &T) {
153 Elapsed += T.Elapsed;
154 UserTime += T.UserTime;
155 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000156 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000157 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000158}
159
Chris Lattner8f0d8242002-11-18 21:47:09 +0000160/// addPeakMemoryMeasurement - This method should be called whenever memory
161/// usage needs to be checked. It adds a peak memory measurement to the
162/// currently active timers, which will be printed when the timer group prints
163///
164void Timer::addPeakMemoryMeasurement() {
165 long MemUsed = getMemUsage();
166
167 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
168 E = ActiveTimers.end(); I != E; ++I)
169 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
170}
171
172
Chris Lattner6c38a792002-10-01 19:36:54 +0000173//===----------------------------------------------------------------------===//
174// TimerGroup Implementation
175//===----------------------------------------------------------------------===//
176
Chris Lattnerf205fec2003-05-09 20:05:44 +0000177// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
178// TotalWidth size, and B is the AfterDec size.
179//
180static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
181 std::ostream &OS) {
182 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
183 OS.width(TotalWidth-AfterDec-1);
184 char OldFill = OS.fill();
185 OS.fill(' ');
186 OS << (int)Val; // Integer part;
187 OS << ".";
188 OS.width(AfterDec);
189 OS.fill('0');
190 unsigned ResultFieldSize = 1;
191 while (AfterDec--) ResultFieldSize *= 10;
192 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
193 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000194}
195
Chris Lattnerf205fec2003-05-09 20:05:44 +0000196static void printVal(double Val, double Total, std::ostream &OS) {
197 if (Total < 1e-7) // Avoid dividing by zero...
198 OS << " ----- ";
199 else {
200 OS << " ";
201 printAlignedFP(Val, 4, 7, OS);
202 OS << " (";
203 printAlignedFP(Val*100/Total, 1, 5, OS);
204 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000205 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000206}
207
208void Timer::print(const Timer &Total, std::ostream &OS) {
209 if (Total.UserTime)
210 printVal(UserTime, Total.UserTime, OS);
211 if (Total.SystemTime)
212 printVal(SystemTime, Total.SystemTime, OS);
213 if (Total.getProcessTime())
214 printVal(getProcessTime(), Total.getProcessTime(), OS);
215 printVal(Elapsed, Total.Elapsed, OS);
216
217 OS << " ";
218
219 if (Total.MemUsed) {
220 OS.width(9);
221 OS << MemUsed << " ";
222 }
223 if (Total.PeakMem) {
224 if (PeakMem) {
225 OS.width(9);
226 OS << PeakMem << " ";
227 } else
228 OS << " ";
229 }
230 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000231
232 Started = false; // Once printed, don't print again
233}
234
Chris Lattnerf205fec2003-05-09 20:05:44 +0000235// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
236std::ostream *GetLibSupportInfoOutputFile() {
237 if (LibSupportInfoOutputFilename.empty())
238 return &std::cerr;
239 if (LibSupportInfoOutputFilename == "-")
240 return &std::cout;
241
242 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
Chris Lattner903c2d12003-06-06 22:13:01 +0000243 std::ios::app);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000244 if (!Result->good()) {
245 std::cerr << "Error opening info-output-file '"
246 << LibSupportInfoOutputFilename << " for appending!\n";
247 delete Result;
248 return &std::cerr;
249 }
250 return Result;
251}
252
Chris Lattner6c38a792002-10-01 19:36:54 +0000253
254void TimerGroup::removeTimer() {
255 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
256 // Sort the timers in descending order by amount of time taken...
257 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
258 std::greater<Timer>());
259
260 // Figure out how many spaces to indent TimerGroup name...
261 unsigned Padding = (80-Name.length())/2;
262 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
263
Chris Lattnerf205fec2003-05-09 20:05:44 +0000264 std::ostream *OutStream = GetLibSupportInfoOutputFile();
265
Chris Lattner6c38a792002-10-01 19:36:54 +0000266 ++NumTimers;
267 { // Scope to contain Total timer... don't allow total timer to drop us to
268 // zero timers...
269 Timer Total("TOTAL");
270
271 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
272 Total.sum(TimersToPrint[i]);
273
274 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000275 *OutStream << "===" << std::string(73, '-') << "===\n"
276 << std::string(Padding, ' ') << Name << "\n"
277 << "===" << std::string(73, '-')
278 << "===\n Total Execution Time: ";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000279
Chris Lattnerf205fec2003-05-09 20:05:44 +0000280 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
281 *OutStream << " seconds (";
282 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
283 *OutStream << " wall clock)\n\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000284
285 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000286 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000287 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000288 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000289 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000290 *OutStream << " --User+System--";
291 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000292 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000293 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000294 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000295 *OutStream << " -PeakMem-";
296 *OutStream << " --- Name ---\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000297
298 // Loop through all of the timing data, printing it out...
299 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000300 TimersToPrint[i].print(Total, *OutStream);
Chris Lattner6c38a792002-10-01 19:36:54 +0000301
Chris Lattnerf205fec2003-05-09 20:05:44 +0000302 Total.print(Total, *OutStream);
303 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000304 }
305 --NumTimers;
306
307 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000308
309 if (OutStream != &std::cerr && OutStream != &std::cout)
310 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000311 }
312
313 // Delete default timer group!
314 if (NumTimers == 0 && this == DefaultTimerGroup) {
315 delete DefaultTimerGroup;
316 DefaultTimerGroup = 0;
317 }
318}