blob: 26f10e77f77a546f47d88a7aee267160097a856a [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>
11#include <sys/unistd.h>
Anand Shukla34008372002-10-04 23:57:01 +000012#include <unistd.h>
Brian Gaeke8c638832003-06-17 19:54:00 +000013#ifndef __FreeBSD__
Chris Lattner9d4ef122002-11-04 00:32:44 +000014#include <malloc.h>
Brian Gaeke8c638832003-06-17 19:54:00 +000015#endif // __FreeBSD__
Chris Lattnerb8c88292003-06-06 21:09:29 +000016#include <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 {
25 cl::opt<bool>
26 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
27 "tracking (this may be slow)"),
28 cl::Hidden);
Chris Lattnerf205fec2003-05-09 20:05:44 +000029
30 cl::opt<std::string, true>
31 InfoOutputFilename("info-output-file",
32 cl::desc("File to append -stats and -timer output to"),
33 cl::Hidden, cl::location(LibSupportInfoOutputFilename));
Chris Lattner3f398492003-01-30 23:08:50 +000034}
35
Chris Lattner6c38a792002-10-01 19:36:54 +000036static TimerGroup *DefaultTimerGroup = 0;
37static TimerGroup *getDefaultTimerGroup() {
38 if (DefaultTimerGroup) return DefaultTimerGroup;
39 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
40}
41
42Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000043 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000044 Started(false), TG(getDefaultTimerGroup()) {
45 TG->addTimer();
46}
47
48Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000049 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000050 Started(false), TG(&tg) {
51 TG->addTimer();
52}
53
54Timer::Timer(const Timer &T) {
55 TG = T.TG;
56 if (TG) TG->addTimer();
57 operator=(T);
58}
59
60
61// Copy ctor, initialize with no TG member.
62Timer::Timer(bool, const Timer &T) {
63 TG = T.TG; // Avoid assertion in operator=
64 operator=(T); // Copy contents
65 TG = 0;
66}
67
68
69Timer::~Timer() {
70 if (TG) {
71 if (Started) {
72 Started = false;
73 TG->addTimerToPrint(*this);
74 }
75 TG->removeTimer();
76 }
77}
78
Chris Lattner8f0d8242002-11-18 21:47:09 +000079static long getMemUsage() {
Brian Gaeke8c638832003-06-17 19:54:00 +000080#ifndef __FreeBSD__
Chris Lattner3f398492003-01-30 23:08:50 +000081 if (TrackSpace) {
82 struct mallinfo MI = mallinfo();
Chris Lattnere040f972003-02-13 05:07:53 +000083 return MI.uordblks/*+MI.hblkhd*/;
Chris Lattner3f398492003-01-30 23:08:50 +000084 }
Brian Gaeke8c638832003-06-17 19:54:00 +000085#endif // __FreeBSD__
86 return 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +000087}
88
Chris Lattner6c38a792002-10-01 19:36:54 +000089struct TimeRecord {
90 double Elapsed, UserTime, SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +000091 long MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +000092};
93
Chris Lattner8f0d8242002-11-18 21:47:09 +000094static TimeRecord getTimeRecord(bool Start) {
Chris Lattner6c38a792002-10-01 19:36:54 +000095 struct rusage RU;
96 struct timeval T;
Chris Lattnerbbe5ac12003-02-05 21:44:28 +000097 long MemUsed = 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +000098 if (Start) {
99 MemUsed = getMemUsage();
100 if (getrusage(RUSAGE_SELF, &RU))
101 perror("getrusage call failed: -time-passes info incorrect!");
102 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000103 gettimeofday(&T, 0);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000104
105 if (!Start) {
106 MemUsed = getMemUsage();
107 if (getrusage(RUSAGE_SELF, &RU))
108 perror("getrusage call failed: -time-passes info incorrect!");
Chris Lattner6c38a792002-10-01 19:36:54 +0000109 }
110
111 TimeRecord Result;
112 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
113 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
114 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000115 Result.MemUsed = MemUsed;
116
Chris Lattner6c38a792002-10-01 19:36:54 +0000117 return Result;
118}
119
Chris Lattner8f0d8242002-11-18 21:47:09 +0000120static std::vector<Timer*> ActiveTimers;
121
Chris Lattner6c38a792002-10-01 19:36:54 +0000122void Timer::startTimer() {
123 Started = true;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000124 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000125 Elapsed -= TR.Elapsed;
126 UserTime -= TR.UserTime;
127 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000128 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000129 PeakMemBase = TR.MemUsed;
130 ActiveTimers.push_back(this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000131}
132
133void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000134 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000135 Elapsed += TR.Elapsed;
136 UserTime += TR.UserTime;
137 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000138 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000139
140 if (ActiveTimers.back() == this) {
141 ActiveTimers.pop_back();
142 } else {
143 std::vector<Timer*>::iterator I =
144 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
145 assert(I != ActiveTimers.end() && "stop but no startTimer?");
146 ActiveTimers.erase(I);
147 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000148}
149
150void Timer::sum(const Timer &T) {
151 Elapsed += T.Elapsed;
152 UserTime += T.UserTime;
153 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000154 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000155 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000156}
157
Chris Lattner8f0d8242002-11-18 21:47:09 +0000158/// addPeakMemoryMeasurement - This method should be called whenever memory
159/// usage needs to be checked. It adds a peak memory measurement to the
160/// currently active timers, which will be printed when the timer group prints
161///
162void Timer::addPeakMemoryMeasurement() {
163 long MemUsed = getMemUsage();
164
165 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
166 E = ActiveTimers.end(); I != E; ++I)
167 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
168}
169
170
Chris Lattner6c38a792002-10-01 19:36:54 +0000171//===----------------------------------------------------------------------===//
172// TimerGroup Implementation
173//===----------------------------------------------------------------------===//
174
Chris Lattnerf205fec2003-05-09 20:05:44 +0000175// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
176// TotalWidth size, and B is the AfterDec size.
177//
178static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
179 std::ostream &OS) {
180 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
181 OS.width(TotalWidth-AfterDec-1);
182 char OldFill = OS.fill();
183 OS.fill(' ');
184 OS << (int)Val; // Integer part;
185 OS << ".";
186 OS.width(AfterDec);
187 OS.fill('0');
188 unsigned ResultFieldSize = 1;
189 while (AfterDec--) ResultFieldSize *= 10;
190 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
191 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000192}
193
Chris Lattnerf205fec2003-05-09 20:05:44 +0000194static void printVal(double Val, double Total, std::ostream &OS) {
195 if (Total < 1e-7) // Avoid dividing by zero...
196 OS << " ----- ";
197 else {
198 OS << " ";
199 printAlignedFP(Val, 4, 7, OS);
200 OS << " (";
201 printAlignedFP(Val*100/Total, 1, 5, OS);
202 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000203 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000204}
205
206void Timer::print(const Timer &Total, std::ostream &OS) {
207 if (Total.UserTime)
208 printVal(UserTime, Total.UserTime, OS);
209 if (Total.SystemTime)
210 printVal(SystemTime, Total.SystemTime, OS);
211 if (Total.getProcessTime())
212 printVal(getProcessTime(), Total.getProcessTime(), OS);
213 printVal(Elapsed, Total.Elapsed, OS);
214
215 OS << " ";
216
217 if (Total.MemUsed) {
218 OS.width(9);
219 OS << MemUsed << " ";
220 }
221 if (Total.PeakMem) {
222 if (PeakMem) {
223 OS.width(9);
224 OS << PeakMem << " ";
225 } else
226 OS << " ";
227 }
228 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000229
230 Started = false; // Once printed, don't print again
231}
232
Chris Lattnerf205fec2003-05-09 20:05:44 +0000233// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
234std::ostream *GetLibSupportInfoOutputFile() {
235 if (LibSupportInfoOutputFilename.empty())
236 return &std::cerr;
237 if (LibSupportInfoOutputFilename == "-")
238 return &std::cout;
239
240 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
Chris Lattner903c2d12003-06-06 22:13:01 +0000241 std::ios::app);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000242 if (!Result->good()) {
243 std::cerr << "Error opening info-output-file '"
244 << LibSupportInfoOutputFilename << " for appending!\n";
245 delete Result;
246 return &std::cerr;
247 }
248 return Result;
249}
250
Chris Lattner6c38a792002-10-01 19:36:54 +0000251
252void TimerGroup::removeTimer() {
253 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
254 // Sort the timers in descending order by amount of time taken...
255 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
256 std::greater<Timer>());
257
258 // Figure out how many spaces to indent TimerGroup name...
259 unsigned Padding = (80-Name.length())/2;
260 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
261
Chris Lattnerf205fec2003-05-09 20:05:44 +0000262 std::ostream *OutStream = GetLibSupportInfoOutputFile();
263
Chris Lattner6c38a792002-10-01 19:36:54 +0000264 ++NumTimers;
265 { // Scope to contain Total timer... don't allow total timer to drop us to
266 // zero timers...
267 Timer Total("TOTAL");
268
269 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
270 Total.sum(TimersToPrint[i]);
271
272 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000273 *OutStream << "===" << std::string(73, '-') << "===\n"
274 << std::string(Padding, ' ') << Name << "\n"
275 << "===" << std::string(73, '-')
276 << "===\n Total Execution Time: ";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000277
Chris Lattnerf205fec2003-05-09 20:05:44 +0000278 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
279 *OutStream << " seconds (";
280 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
281 *OutStream << " wall clock)\n\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000282
283 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000284 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000285 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000286 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000287 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000288 *OutStream << " --User+System--";
289 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000290 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000291 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000292 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000293 *OutStream << " -PeakMem-";
294 *OutStream << " --- Name ---\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000295
296 // Loop through all of the timing data, printing it out...
297 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000298 TimersToPrint[i].print(Total, *OutStream);
Chris Lattner6c38a792002-10-01 19:36:54 +0000299
Chris Lattnerf205fec2003-05-09 20:05:44 +0000300 Total.print(Total, *OutStream);
301 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000302 }
303 --NumTimers;
304
305 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000306
307 if (OutStream != &std::cerr && OutStream != &std::cout)
308 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000309 }
310
311 // Delete default timer group!
312 if (NumTimers == 0 && this == DefaultTimerGroup) {
313 delete DefaultTimerGroup;
314 DefaultTimerGroup = 0;
315 }
316}