blob: 96b63f1e94e198300336f967242e4981a6a574e8 [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"
Chris Lattner6c38a792002-10-01 19:36:54 +00009#include <sys/resource.h>
10#include <sys/time.h>
Anand Shukla34008372002-10-04 23:57:01 +000011#include <unistd.h>
Brian Gaeke8c638832003-06-17 19:54:00 +000012#ifndef __FreeBSD__
Chris Lattner9d4ef122002-11-04 00:32:44 +000013#include <malloc.h>
Brian Gaeke8c638832003-06-17 19:54:00 +000014#endif // __FreeBSD__
Chris Lattnerb8c88292003-06-06 21:09:29 +000015#include <stdio.h>
Chris Lattner6c38a792002-10-01 19:36:54 +000016#include <iostream>
17#include <algorithm>
Chris Lattner9550dc22002-10-27 19:08:03 +000018#include <functional>
Chris Lattnerf205fec2003-05-09 20:05:44 +000019#include <fstream>
20
21std::string LibSupportInfoOutputFilename;
Chris Lattner6c38a792002-10-01 19:36:54 +000022
Chris Lattner3f398492003-01-30 23:08:50 +000023namespace {
24 cl::opt<bool>
25 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
26 "tracking (this may be slow)"),
27 cl::Hidden);
Chris Lattnerf205fec2003-05-09 20:05:44 +000028
29 cl::opt<std::string, true>
30 InfoOutputFilename("info-output-file",
31 cl::desc("File to append -stats and -timer output to"),
32 cl::Hidden, cl::location(LibSupportInfoOutputFilename));
Chris Lattner3f398492003-01-30 23:08:50 +000033}
34
Chris Lattner6c38a792002-10-01 19:36:54 +000035static TimerGroup *DefaultTimerGroup = 0;
36static TimerGroup *getDefaultTimerGroup() {
37 if (DefaultTimerGroup) return DefaultTimerGroup;
38 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
39}
40
41Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000042 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000043 Started(false), TG(getDefaultTimerGroup()) {
44 TG->addTimer();
45}
46
47Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000048 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000049 Started(false), TG(&tg) {
50 TG->addTimer();
51}
52
53Timer::Timer(const Timer &T) {
54 TG = T.TG;
55 if (TG) TG->addTimer();
56 operator=(T);
57}
58
59
60// Copy ctor, initialize with no TG member.
61Timer::Timer(bool, const Timer &T) {
62 TG = T.TG; // Avoid assertion in operator=
63 operator=(T); // Copy contents
64 TG = 0;
65}
66
67
68Timer::~Timer() {
69 if (TG) {
70 if (Started) {
71 Started = false;
72 TG->addTimerToPrint(*this);
73 }
74 TG->removeTimer();
75 }
76}
77
Chris Lattner8f0d8242002-11-18 21:47:09 +000078static long getMemUsage() {
Brian Gaeke8c638832003-06-17 19:54:00 +000079#ifndef __FreeBSD__
Chris Lattner3f398492003-01-30 23:08:50 +000080 if (TrackSpace) {
81 struct mallinfo MI = mallinfo();
Chris Lattnere040f972003-02-13 05:07:53 +000082 return MI.uordblks/*+MI.hblkhd*/;
Chris Lattner3f398492003-01-30 23:08:50 +000083 }
Brian Gaeke8c638832003-06-17 19:54:00 +000084#endif // __FreeBSD__
85 return 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +000086}
87
Chris Lattner6c38a792002-10-01 19:36:54 +000088struct TimeRecord {
89 double Elapsed, UserTime, SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +000090 long MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +000091};
92
Chris Lattner8f0d8242002-11-18 21:47:09 +000093static TimeRecord getTimeRecord(bool Start) {
Chris Lattner6c38a792002-10-01 19:36:54 +000094 struct rusage RU;
95 struct timeval T;
Chris Lattnerbbe5ac12003-02-05 21:44:28 +000096 long MemUsed = 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +000097 if (Start) {
98 MemUsed = getMemUsage();
99 if (getrusage(RUSAGE_SELF, &RU))
100 perror("getrusage call failed: -time-passes info incorrect!");
101 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000102 gettimeofday(&T, 0);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000103
104 if (!Start) {
105 MemUsed = getMemUsage();
106 if (getrusage(RUSAGE_SELF, &RU))
107 perror("getrusage call failed: -time-passes info incorrect!");
Chris Lattner6c38a792002-10-01 19:36:54 +0000108 }
109
110 TimeRecord Result;
111 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
112 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
113 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000114 Result.MemUsed = MemUsed;
115
Chris Lattner6c38a792002-10-01 19:36:54 +0000116 return Result;
117}
118
Chris Lattner8f0d8242002-11-18 21:47:09 +0000119static std::vector<Timer*> ActiveTimers;
120
Chris Lattner6c38a792002-10-01 19:36:54 +0000121void Timer::startTimer() {
122 Started = true;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000123 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000124 Elapsed -= TR.Elapsed;
125 UserTime -= TR.UserTime;
126 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000127 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000128 PeakMemBase = TR.MemUsed;
129 ActiveTimers.push_back(this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000130}
131
132void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000133 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000134 Elapsed += TR.Elapsed;
135 UserTime += TR.UserTime;
136 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000137 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000138
139 if (ActiveTimers.back() == this) {
140 ActiveTimers.pop_back();
141 } else {
142 std::vector<Timer*>::iterator I =
143 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
144 assert(I != ActiveTimers.end() && "stop but no startTimer?");
145 ActiveTimers.erase(I);
146 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000147}
148
149void Timer::sum(const Timer &T) {
150 Elapsed += T.Elapsed;
151 UserTime += T.UserTime;
152 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000153 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000154 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000155}
156
Chris Lattner8f0d8242002-11-18 21:47:09 +0000157/// addPeakMemoryMeasurement - This method should be called whenever memory
158/// usage needs to be checked. It adds a peak memory measurement to the
159/// currently active timers, which will be printed when the timer group prints
160///
161void Timer::addPeakMemoryMeasurement() {
162 long MemUsed = getMemUsage();
163
164 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
165 E = ActiveTimers.end(); I != E; ++I)
166 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
167}
168
169
Chris Lattner6c38a792002-10-01 19:36:54 +0000170//===----------------------------------------------------------------------===//
171// TimerGroup Implementation
172//===----------------------------------------------------------------------===//
173
Chris Lattnerf205fec2003-05-09 20:05:44 +0000174// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
175// TotalWidth size, and B is the AfterDec size.
176//
177static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
178 std::ostream &OS) {
179 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
180 OS.width(TotalWidth-AfterDec-1);
181 char OldFill = OS.fill();
182 OS.fill(' ');
183 OS << (int)Val; // Integer part;
184 OS << ".";
185 OS.width(AfterDec);
186 OS.fill('0');
187 unsigned ResultFieldSize = 1;
188 while (AfterDec--) ResultFieldSize *= 10;
189 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
190 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000191}
192
Chris Lattnerf205fec2003-05-09 20:05:44 +0000193static void printVal(double Val, double Total, std::ostream &OS) {
194 if (Total < 1e-7) // Avoid dividing by zero...
195 OS << " ----- ";
196 else {
197 OS << " ";
198 printAlignedFP(Val, 4, 7, OS);
199 OS << " (";
200 printAlignedFP(Val*100/Total, 1, 5, OS);
201 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000202 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000203}
204
205void Timer::print(const Timer &Total, std::ostream &OS) {
206 if (Total.UserTime)
207 printVal(UserTime, Total.UserTime, OS);
208 if (Total.SystemTime)
209 printVal(SystemTime, Total.SystemTime, OS);
210 if (Total.getProcessTime())
211 printVal(getProcessTime(), Total.getProcessTime(), OS);
212 printVal(Elapsed, Total.Elapsed, OS);
213
214 OS << " ";
215
216 if (Total.MemUsed) {
217 OS.width(9);
218 OS << MemUsed << " ";
219 }
220 if (Total.PeakMem) {
221 if (PeakMem) {
222 OS.width(9);
223 OS << PeakMem << " ";
224 } else
225 OS << " ";
226 }
227 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000228
229 Started = false; // Once printed, don't print again
230}
231
Chris Lattnerf205fec2003-05-09 20:05:44 +0000232// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
233std::ostream *GetLibSupportInfoOutputFile() {
234 if (LibSupportInfoOutputFilename.empty())
235 return &std::cerr;
236 if (LibSupportInfoOutputFilename == "-")
237 return &std::cout;
238
239 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
Chris Lattner903c2d12003-06-06 22:13:01 +0000240 std::ios::app);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000241 if (!Result->good()) {
242 std::cerr << "Error opening info-output-file '"
243 << LibSupportInfoOutputFilename << " for appending!\n";
244 delete Result;
245 return &std::cerr;
246 }
247 return Result;
248}
249
Chris Lattner6c38a792002-10-01 19:36:54 +0000250
251void TimerGroup::removeTimer() {
252 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
253 // Sort the timers in descending order by amount of time taken...
254 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
255 std::greater<Timer>());
256
257 // Figure out how many spaces to indent TimerGroup name...
258 unsigned Padding = (80-Name.length())/2;
259 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
260
Chris Lattnerf205fec2003-05-09 20:05:44 +0000261 std::ostream *OutStream = GetLibSupportInfoOutputFile();
262
Chris Lattner6c38a792002-10-01 19:36:54 +0000263 ++NumTimers;
264 { // Scope to contain Total timer... don't allow total timer to drop us to
265 // zero timers...
266 Timer Total("TOTAL");
267
268 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
269 Total.sum(TimersToPrint[i]);
270
271 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000272 *OutStream << "===" << std::string(73, '-') << "===\n"
273 << std::string(Padding, ' ') << Name << "\n"
274 << "===" << std::string(73, '-')
275 << "===\n Total Execution Time: ";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000276
Chris Lattnerf205fec2003-05-09 20:05:44 +0000277 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
278 *OutStream << " seconds (";
279 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
280 *OutStream << " wall clock)\n\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000281
282 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000283 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000284 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000285 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000286 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000287 *OutStream << " --User+System--";
288 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000289 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000290 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000291 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000292 *OutStream << " -PeakMem-";
293 *OutStream << " --- Name ---\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000294
295 // Loop through all of the timing data, printing it out...
296 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000297 TimersToPrint[i].print(Total, *OutStream);
Chris Lattner6c38a792002-10-01 19:36:54 +0000298
Chris Lattnerf205fec2003-05-09 20:05:44 +0000299 Total.print(Total, *OutStream);
300 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000301 }
302 --NumTimers;
303
304 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000305
306 if (OutStream != &std::cerr && OutStream != &std::cout)
307 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000308 }
309
310 // Delete default timer group!
311 if (NumTimers == 0 && this == DefaultTimerGroup) {
312 delete DefaultTimerGroup;
313 DefaultTimerGroup = 0;
314 }
315}