blob: a013b46d12b0ee0b1bb3b5f720fc9fccb94ba293 [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>
Chris Lattner9d4ef122002-11-04 00:32:44 +000013#include <malloc.h>
Chris Lattner6c38a792002-10-01 19:36:54 +000014#include <iostream>
15#include <algorithm>
Chris Lattner9550dc22002-10-27 19:08:03 +000016#include <functional>
Chris Lattnerf205fec2003-05-09 20:05:44 +000017#include <fstream>
18
19std::string LibSupportInfoOutputFilename;
Chris Lattner6c38a792002-10-01 19:36:54 +000020
Chris Lattner3f398492003-01-30 23:08:50 +000021namespace {
22 cl::opt<bool>
23 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
24 "tracking (this may be slow)"),
25 cl::Hidden);
Chris Lattnerf205fec2003-05-09 20:05:44 +000026
27 cl::opt<std::string, true>
28 InfoOutputFilename("info-output-file",
29 cl::desc("File to append -stats and -timer output to"),
30 cl::Hidden, cl::location(LibSupportInfoOutputFilename));
Chris Lattner3f398492003-01-30 23:08:50 +000031}
32
Chris Lattner6c38a792002-10-01 19:36:54 +000033static TimerGroup *DefaultTimerGroup = 0;
34static TimerGroup *getDefaultTimerGroup() {
35 if (DefaultTimerGroup) return DefaultTimerGroup;
36 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
37}
38
39Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000040 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000041 Started(false), TG(getDefaultTimerGroup()) {
42 TG->addTimer();
43}
44
45Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000046 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000047 Started(false), TG(&tg) {
48 TG->addTimer();
49}
50
51Timer::Timer(const Timer &T) {
52 TG = T.TG;
53 if (TG) TG->addTimer();
54 operator=(T);
55}
56
57
58// Copy ctor, initialize with no TG member.
59Timer::Timer(bool, const Timer &T) {
60 TG = T.TG; // Avoid assertion in operator=
61 operator=(T); // Copy contents
62 TG = 0;
63}
64
65
66Timer::~Timer() {
67 if (TG) {
68 if (Started) {
69 Started = false;
70 TG->addTimerToPrint(*this);
71 }
72 TG->removeTimer();
73 }
74}
75
Chris Lattner8f0d8242002-11-18 21:47:09 +000076static long getMemUsage() {
Chris Lattner3f398492003-01-30 23:08:50 +000077 if (TrackSpace) {
78 struct mallinfo MI = mallinfo();
Chris Lattnere040f972003-02-13 05:07:53 +000079 return MI.uordblks/*+MI.hblkhd*/;
Chris Lattner3f398492003-01-30 23:08:50 +000080 } else {
81 return 0;
82 }
Chris Lattner8f0d8242002-11-18 21:47:09 +000083}
84
Chris Lattner6c38a792002-10-01 19:36:54 +000085struct TimeRecord {
86 double Elapsed, UserTime, SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +000087 long MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +000088};
89
Chris Lattner8f0d8242002-11-18 21:47:09 +000090static TimeRecord getTimeRecord(bool Start) {
Chris Lattner6c38a792002-10-01 19:36:54 +000091 struct rusage RU;
92 struct timeval T;
Chris Lattnerbbe5ac12003-02-05 21:44:28 +000093 long MemUsed = 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +000094 if (Start) {
95 MemUsed = getMemUsage();
96 if (getrusage(RUSAGE_SELF, &RU))
97 perror("getrusage call failed: -time-passes info incorrect!");
98 }
Chris Lattner6c38a792002-10-01 19:36:54 +000099 gettimeofday(&T, 0);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000100
101 if (!Start) {
102 MemUsed = getMemUsage();
103 if (getrusage(RUSAGE_SELF, &RU))
104 perror("getrusage call failed: -time-passes info incorrect!");
Chris Lattner6c38a792002-10-01 19:36:54 +0000105 }
106
107 TimeRecord Result;
108 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
109 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
110 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000111 Result.MemUsed = MemUsed;
112
Chris Lattner6c38a792002-10-01 19:36:54 +0000113 return Result;
114}
115
Chris Lattner8f0d8242002-11-18 21:47:09 +0000116static std::vector<Timer*> ActiveTimers;
117
Chris Lattner6c38a792002-10-01 19:36:54 +0000118void Timer::startTimer() {
119 Started = true;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000120 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000121 Elapsed -= TR.Elapsed;
122 UserTime -= TR.UserTime;
123 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000124 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000125 PeakMemBase = TR.MemUsed;
126 ActiveTimers.push_back(this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000127}
128
129void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000130 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000131 Elapsed += TR.Elapsed;
132 UserTime += TR.UserTime;
133 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000134 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000135
136 if (ActiveTimers.back() == this) {
137 ActiveTimers.pop_back();
138 } else {
139 std::vector<Timer*>::iterator I =
140 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
141 assert(I != ActiveTimers.end() && "stop but no startTimer?");
142 ActiveTimers.erase(I);
143 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000144}
145
146void Timer::sum(const Timer &T) {
147 Elapsed += T.Elapsed;
148 UserTime += T.UserTime;
149 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000150 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000151 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000152}
153
Chris Lattner8f0d8242002-11-18 21:47:09 +0000154/// addPeakMemoryMeasurement - This method should be called whenever memory
155/// usage needs to be checked. It adds a peak memory measurement to the
156/// currently active timers, which will be printed when the timer group prints
157///
158void Timer::addPeakMemoryMeasurement() {
159 long MemUsed = getMemUsage();
160
161 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
162 E = ActiveTimers.end(); I != E; ++I)
163 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
164}
165
166
Chris Lattner6c38a792002-10-01 19:36:54 +0000167//===----------------------------------------------------------------------===//
168// TimerGroup Implementation
169//===----------------------------------------------------------------------===//
170
Chris Lattnerf205fec2003-05-09 20:05:44 +0000171// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
172// TotalWidth size, and B is the AfterDec size.
173//
174static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
175 std::ostream &OS) {
176 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
177 OS.width(TotalWidth-AfterDec-1);
178 char OldFill = OS.fill();
179 OS.fill(' ');
180 OS << (int)Val; // Integer part;
181 OS << ".";
182 OS.width(AfterDec);
183 OS.fill('0');
184 unsigned ResultFieldSize = 1;
185 while (AfterDec--) ResultFieldSize *= 10;
186 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
187 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000188}
189
Chris Lattnerf205fec2003-05-09 20:05:44 +0000190static void printVal(double Val, double Total, std::ostream &OS) {
191 if (Total < 1e-7) // Avoid dividing by zero...
192 OS << " ----- ";
193 else {
194 OS << " ";
195 printAlignedFP(Val, 4, 7, OS);
196 OS << " (";
197 printAlignedFP(Val*100/Total, 1, 5, OS);
198 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000199 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000200}
201
202void Timer::print(const Timer &Total, std::ostream &OS) {
203 if (Total.UserTime)
204 printVal(UserTime, Total.UserTime, OS);
205 if (Total.SystemTime)
206 printVal(SystemTime, Total.SystemTime, OS);
207 if (Total.getProcessTime())
208 printVal(getProcessTime(), Total.getProcessTime(), OS);
209 printVal(Elapsed, Total.Elapsed, OS);
210
211 OS << " ";
212
213 if (Total.MemUsed) {
214 OS.width(9);
215 OS << MemUsed << " ";
216 }
217 if (Total.PeakMem) {
218 if (PeakMem) {
219 OS.width(9);
220 OS << PeakMem << " ";
221 } else
222 OS << " ";
223 }
224 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000225
226 Started = false; // Once printed, don't print again
227}
228
Chris Lattnerf205fec2003-05-09 20:05:44 +0000229// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
230std::ostream *GetLibSupportInfoOutputFile() {
231 if (LibSupportInfoOutputFilename.empty())
232 return &std::cerr;
233 if (LibSupportInfoOutputFilename == "-")
234 return &std::cout;
235
236 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
237 std::ios_base::app);
238 if (!Result->good()) {
239 std::cerr << "Error opening info-output-file '"
240 << LibSupportInfoOutputFilename << " for appending!\n";
241 delete Result;
242 return &std::cerr;
243 }
244 return Result;
245}
246
Chris Lattner6c38a792002-10-01 19:36:54 +0000247
248void TimerGroup::removeTimer() {
249 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
250 // Sort the timers in descending order by amount of time taken...
251 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
252 std::greater<Timer>());
253
254 // Figure out how many spaces to indent TimerGroup name...
255 unsigned Padding = (80-Name.length())/2;
256 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
257
Chris Lattnerf205fec2003-05-09 20:05:44 +0000258 std::ostream *OutStream = GetLibSupportInfoOutputFile();
259
Chris Lattner6c38a792002-10-01 19:36:54 +0000260 ++NumTimers;
261 { // Scope to contain Total timer... don't allow total timer to drop us to
262 // zero timers...
263 Timer Total("TOTAL");
264
265 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
266 Total.sum(TimersToPrint[i]);
267
268 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000269 *OutStream << "===" << std::string(73, '-') << "===\n"
270 << std::string(Padding, ' ') << Name << "\n"
271 << "===" << std::string(73, '-')
272 << "===\n Total Execution Time: ";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000273
Chris Lattnerf205fec2003-05-09 20:05:44 +0000274 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
275 *OutStream << " seconds (";
276 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
277 *OutStream << " wall clock)\n\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000278
279 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000280 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000281 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000282 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000283 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000284 *OutStream << " --User+System--";
285 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000286 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000287 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000288 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000289 *OutStream << " -PeakMem-";
290 *OutStream << " --- Name ---\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000291
292 // Loop through all of the timing data, printing it out...
293 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000294 TimersToPrint[i].print(Total, *OutStream);
Chris Lattner6c38a792002-10-01 19:36:54 +0000295
Chris Lattnerf205fec2003-05-09 20:05:44 +0000296 Total.print(Total, *OutStream);
297 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000298 }
299 --NumTimers;
300
301 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000302
303 if (OutStream != &std::cerr && OutStream != &std::cout)
304 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000305 }
306
307 // Delete default timer group!
308 if (NumTimers == 0 && this == DefaultTimerGroup) {
309 delete DefaultTimerGroup;
310 DefaultTimerGroup = 0;
311 }
312}