blob: 98001287d9a03a0cf1a5eeedf32182197900a6de [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>
19
Chris Lattner71336a92003-07-31 19:38:34 +000020// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
21// of constructor/destructor ordering being unspecified by C++. Basically the
22// problem is that a Statistic<> object gets destroyed, which ends up calling
23// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
24// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
25// would get destroyed before the Statistic, causing havoc to ensue. We "fix"
26// this by creating the string the first time it is needed and never destroying
27// it.
28static std::string &getLibSupportInfoOutputFilename() {
29 static std::string *LibSupportInfoOutputFilename = new std::string();
30 return *LibSupportInfoOutputFilename;
31}
Chris Lattner6c38a792002-10-01 19:36:54 +000032
Chris Lattner3f398492003-01-30 23:08:50 +000033namespace {
John Criswell7a73b802003-06-30 21:59:07 +000034#ifdef HAVE_MALLINFO
Chris Lattner3f398492003-01-30 23:08:50 +000035 cl::opt<bool>
36 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
37 "tracking (this may be slow)"),
38 cl::Hidden);
John Criswell7a73b802003-06-30 21:59:07 +000039#endif
Chris Lattnerf205fec2003-05-09 20:05:44 +000040
41 cl::opt<std::string, true>
Chris Lattner96a54db2003-08-01 22:15:15 +000042 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerf205fec2003-05-09 20:05:44 +000043 cl::desc("File to append -stats and -timer output to"),
Chris Lattner71336a92003-07-31 19:38:34 +000044 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Chris Lattner3f398492003-01-30 23:08:50 +000045}
46
Chris Lattner6c38a792002-10-01 19:36:54 +000047static TimerGroup *DefaultTimerGroup = 0;
48static TimerGroup *getDefaultTimerGroup() {
49 if (DefaultTimerGroup) return DefaultTimerGroup;
50 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
51}
52
53Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000054 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000055 Started(false), TG(getDefaultTimerGroup()) {
56 TG->addTimer();
57}
58
59Timer::Timer(const std::string &N, TimerGroup &tg)
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(&tg) {
62 TG->addTimer();
63}
64
65Timer::Timer(const Timer &T) {
66 TG = T.TG;
67 if (TG) TG->addTimer();
68 operator=(T);
69}
70
71
72// Copy ctor, initialize with no TG member.
73Timer::Timer(bool, const Timer &T) {
74 TG = T.TG; // Avoid assertion in operator=
75 operator=(T); // Copy contents
76 TG = 0;
77}
78
79
80Timer::~Timer() {
81 if (TG) {
82 if (Started) {
83 Started = false;
84 TG->addTimerToPrint(*this);
85 }
86 TG->removeTimer();
87 }
88}
89
Chris Lattner8f0d8242002-11-18 21:47:09 +000090static long getMemUsage() {
John Criswell7a73b802003-06-30 21:59:07 +000091#ifdef HAVE_MALLINFO
Chris Lattner3f398492003-01-30 23:08:50 +000092 if (TrackSpace) {
93 struct mallinfo MI = mallinfo();
Chris Lattnere040f972003-02-13 05:07:53 +000094 return MI.uordblks/*+MI.hblkhd*/;
Chris Lattner3f398492003-01-30 23:08:50 +000095 }
John Criswell7a73b802003-06-30 21:59:07 +000096#endif
Brian Gaeke8c638832003-06-17 19:54:00 +000097 return 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +000098}
99
Chris Lattner6c38a792002-10-01 19:36:54 +0000100struct TimeRecord {
101 double Elapsed, UserTime, SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000102 long MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000103};
104
Chris Lattner8f0d8242002-11-18 21:47:09 +0000105static TimeRecord getTimeRecord(bool Start) {
Chris Lattner6c38a792002-10-01 19:36:54 +0000106 struct rusage RU;
107 struct timeval T;
Chris Lattnerbbe5ac12003-02-05 21:44:28 +0000108 long MemUsed = 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000109 if (Start) {
110 MemUsed = getMemUsage();
111 if (getrusage(RUSAGE_SELF, &RU))
112 perror("getrusage call failed: -time-passes info incorrect!");
113 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000114 gettimeofday(&T, 0);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000115
116 if (!Start) {
117 MemUsed = getMemUsage();
118 if (getrusage(RUSAGE_SELF, &RU))
119 perror("getrusage call failed: -time-passes info incorrect!");
Chris Lattner6c38a792002-10-01 19:36:54 +0000120 }
121
122 TimeRecord Result;
123 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
124 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
125 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000126 Result.MemUsed = MemUsed;
127
Chris Lattner6c38a792002-10-01 19:36:54 +0000128 return Result;
129}
130
Chris Lattner8f0d8242002-11-18 21:47:09 +0000131static std::vector<Timer*> ActiveTimers;
132
Chris Lattner6c38a792002-10-01 19:36:54 +0000133void Timer::startTimer() {
134 Started = true;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000135 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000136 Elapsed -= TR.Elapsed;
137 UserTime -= TR.UserTime;
138 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000139 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000140 PeakMemBase = TR.MemUsed;
141 ActiveTimers.push_back(this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000142}
143
144void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000145 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000146 Elapsed += TR.Elapsed;
147 UserTime += TR.UserTime;
148 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000149 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000150
151 if (ActiveTimers.back() == this) {
152 ActiveTimers.pop_back();
153 } else {
154 std::vector<Timer*>::iterator I =
155 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
156 assert(I != ActiveTimers.end() && "stop but no startTimer?");
157 ActiveTimers.erase(I);
158 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000159}
160
161void Timer::sum(const Timer &T) {
162 Elapsed += T.Elapsed;
163 UserTime += T.UserTime;
164 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000165 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000166 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000167}
168
Chris Lattner8f0d8242002-11-18 21:47:09 +0000169/// addPeakMemoryMeasurement - This method should be called whenever memory
170/// usage needs to be checked. It adds a peak memory measurement to the
171/// currently active timers, which will be printed when the timer group prints
172///
173void Timer::addPeakMemoryMeasurement() {
174 long MemUsed = getMemUsage();
175
176 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
177 E = ActiveTimers.end(); I != E; ++I)
178 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
179}
180
181
Chris Lattner6c38a792002-10-01 19:36:54 +0000182//===----------------------------------------------------------------------===//
183// TimerGroup Implementation
184//===----------------------------------------------------------------------===//
185
Chris Lattnerf205fec2003-05-09 20:05:44 +0000186// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
187// TotalWidth size, and B is the AfterDec size.
188//
189static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
190 std::ostream &OS) {
191 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
192 OS.width(TotalWidth-AfterDec-1);
193 char OldFill = OS.fill();
194 OS.fill(' ');
195 OS << (int)Val; // Integer part;
196 OS << ".";
197 OS.width(AfterDec);
198 OS.fill('0');
199 unsigned ResultFieldSize = 1;
200 while (AfterDec--) ResultFieldSize *= 10;
201 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
202 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000203}
204
Chris Lattnerf205fec2003-05-09 20:05:44 +0000205static void printVal(double Val, double Total, std::ostream &OS) {
206 if (Total < 1e-7) // Avoid dividing by zero...
207 OS << " ----- ";
208 else {
209 OS << " ";
210 printAlignedFP(Val, 4, 7, OS);
211 OS << " (";
212 printAlignedFP(Val*100/Total, 1, 5, OS);
213 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000214 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000215}
216
217void Timer::print(const Timer &Total, std::ostream &OS) {
218 if (Total.UserTime)
219 printVal(UserTime, Total.UserTime, OS);
220 if (Total.SystemTime)
221 printVal(SystemTime, Total.SystemTime, OS);
222 if (Total.getProcessTime())
223 printVal(getProcessTime(), Total.getProcessTime(), OS);
224 printVal(Elapsed, Total.Elapsed, OS);
225
226 OS << " ";
227
228 if (Total.MemUsed) {
229 OS.width(9);
230 OS << MemUsed << " ";
231 }
232 if (Total.PeakMem) {
233 if (PeakMem) {
234 OS.width(9);
235 OS << PeakMem << " ";
236 } else
237 OS << " ";
238 }
239 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000240
241 Started = false; // Once printed, don't print again
242}
243
Chris Lattnerf205fec2003-05-09 20:05:44 +0000244// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
245std::ostream *GetLibSupportInfoOutputFile() {
Chris Lattner71336a92003-07-31 19:38:34 +0000246 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000247 if (LibSupportInfoOutputFilename.empty())
248 return &std::cerr;
249 if (LibSupportInfoOutputFilename == "-")
250 return &std::cout;
251
252 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
Chris Lattner903c2d12003-06-06 22:13:01 +0000253 std::ios::app);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000254 if (!Result->good()) {
255 std::cerr << "Error opening info-output-file '"
256 << LibSupportInfoOutputFilename << " for appending!\n";
257 delete Result;
258 return &std::cerr;
259 }
260 return Result;
261}
262
Chris Lattner6c38a792002-10-01 19:36:54 +0000263
264void TimerGroup::removeTimer() {
265 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
266 // Sort the timers in descending order by amount of time taken...
267 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
268 std::greater<Timer>());
269
270 // Figure out how many spaces to indent TimerGroup name...
271 unsigned Padding = (80-Name.length())/2;
272 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
273
Chris Lattnerf205fec2003-05-09 20:05:44 +0000274 std::ostream *OutStream = GetLibSupportInfoOutputFile();
275
Chris Lattner6c38a792002-10-01 19:36:54 +0000276 ++NumTimers;
277 { // Scope to contain Total timer... don't allow total timer to drop us to
278 // zero timers...
279 Timer Total("TOTAL");
280
281 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
282 Total.sum(TimersToPrint[i]);
283
284 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000285 *OutStream << "===" << std::string(73, '-') << "===\n"
286 << std::string(Padding, ' ') << Name << "\n"
287 << "===" << std::string(73, '-')
288 << "===\n Total Execution Time: ";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000289
Chris Lattnerf205fec2003-05-09 20:05:44 +0000290 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
291 *OutStream << " seconds (";
292 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
293 *OutStream << " wall clock)\n\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000294
295 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000296 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000297 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000298 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000299 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000300 *OutStream << " --User+System--";
301 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000302 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000303 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000304 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000305 *OutStream << " -PeakMem-";
306 *OutStream << " --- Name ---\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000307
308 // Loop through all of the timing data, printing it out...
309 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000310 TimersToPrint[i].print(Total, *OutStream);
Chris Lattner6c38a792002-10-01 19:36:54 +0000311
Chris Lattnerf205fec2003-05-09 20:05:44 +0000312 Total.print(Total, *OutStream);
313 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000314 }
315 --NumTimers;
316
317 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000318
319 if (OutStream != &std::cerr && OutStream != &std::cout)
320 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000321 }
322
323 // Delete default timer group!
324 if (NumTimers == 0 && this == DefaultTimerGroup) {
325 delete DefaultTimerGroup;
326 DefaultTimerGroup = 0;
327 }
328}