blob: 85114b57f2b27f03ed4a8ebf49387852992e8bd6 [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 Lattnerb8c88292003-06-06 21:09:29 +000014#include <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
20std::string LibSupportInfoOutputFilename;
Chris Lattner6c38a792002-10-01 19:36:54 +000021
Chris Lattner3f398492003-01-30 23:08:50 +000022namespace {
23 cl::opt<bool>
24 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
25 "tracking (this may be slow)"),
26 cl::Hidden);
Chris Lattnerf205fec2003-05-09 20:05:44 +000027
28 cl::opt<std::string, true>
29 InfoOutputFilename("info-output-file",
30 cl::desc("File to append -stats and -timer output to"),
31 cl::Hidden, cl::location(LibSupportInfoOutputFilename));
Chris Lattner3f398492003-01-30 23:08:50 +000032}
33
Chris Lattner6c38a792002-10-01 19:36:54 +000034static TimerGroup *DefaultTimerGroup = 0;
35static TimerGroup *getDefaultTimerGroup() {
36 if (DefaultTimerGroup) return DefaultTimerGroup;
37 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
38}
39
40Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000041 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000042 Started(false), TG(getDefaultTimerGroup()) {
43 TG->addTimer();
44}
45
46Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000047 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000048 Started(false), TG(&tg) {
49 TG->addTimer();
50}
51
52Timer::Timer(const Timer &T) {
53 TG = T.TG;
54 if (TG) TG->addTimer();
55 operator=(T);
56}
57
58
59// Copy ctor, initialize with no TG member.
60Timer::Timer(bool, const Timer &T) {
61 TG = T.TG; // Avoid assertion in operator=
62 operator=(T); // Copy contents
63 TG = 0;
64}
65
66
67Timer::~Timer() {
68 if (TG) {
69 if (Started) {
70 Started = false;
71 TG->addTimerToPrint(*this);
72 }
73 TG->removeTimer();
74 }
75}
76
Chris Lattner8f0d8242002-11-18 21:47:09 +000077static long getMemUsage() {
Chris Lattner3f398492003-01-30 23:08:50 +000078 if (TrackSpace) {
79 struct mallinfo MI = mallinfo();
Chris Lattnere040f972003-02-13 05:07:53 +000080 return MI.uordblks/*+MI.hblkhd*/;
Chris Lattner3f398492003-01-30 23:08:50 +000081 } else {
82 return 0;
83 }
Chris Lattner8f0d8242002-11-18 21:47:09 +000084}
85
Chris Lattner6c38a792002-10-01 19:36:54 +000086struct TimeRecord {
87 double Elapsed, UserTime, SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +000088 long MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +000089};
90
Chris Lattner8f0d8242002-11-18 21:47:09 +000091static TimeRecord getTimeRecord(bool Start) {
Chris Lattner6c38a792002-10-01 19:36:54 +000092 struct rusage RU;
93 struct timeval T;
Chris Lattnerbbe5ac12003-02-05 21:44:28 +000094 long MemUsed = 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +000095 if (Start) {
96 MemUsed = getMemUsage();
97 if (getrusage(RUSAGE_SELF, &RU))
98 perror("getrusage call failed: -time-passes info incorrect!");
99 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000100 gettimeofday(&T, 0);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000101
102 if (!Start) {
103 MemUsed = getMemUsage();
104 if (getrusage(RUSAGE_SELF, &RU))
105 perror("getrusage call failed: -time-passes info incorrect!");
Chris Lattner6c38a792002-10-01 19:36:54 +0000106 }
107
108 TimeRecord Result;
109 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
110 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
111 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000112 Result.MemUsed = MemUsed;
113
Chris Lattner6c38a792002-10-01 19:36:54 +0000114 return Result;
115}
116
Chris Lattner8f0d8242002-11-18 21:47:09 +0000117static std::vector<Timer*> ActiveTimers;
118
Chris Lattner6c38a792002-10-01 19:36:54 +0000119void Timer::startTimer() {
120 Started = true;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000121 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000122 Elapsed -= TR.Elapsed;
123 UserTime -= TR.UserTime;
124 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000125 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000126 PeakMemBase = TR.MemUsed;
127 ActiveTimers.push_back(this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000128}
129
130void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000131 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000132 Elapsed += TR.Elapsed;
133 UserTime += TR.UserTime;
134 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000135 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000136
137 if (ActiveTimers.back() == this) {
138 ActiveTimers.pop_back();
139 } else {
140 std::vector<Timer*>::iterator I =
141 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
142 assert(I != ActiveTimers.end() && "stop but no startTimer?");
143 ActiveTimers.erase(I);
144 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000145}
146
147void Timer::sum(const Timer &T) {
148 Elapsed += T.Elapsed;
149 UserTime += T.UserTime;
150 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000151 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000152 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000153}
154
Chris Lattner8f0d8242002-11-18 21:47:09 +0000155/// addPeakMemoryMeasurement - This method should be called whenever memory
156/// usage needs to be checked. It adds a peak memory measurement to the
157/// currently active timers, which will be printed when the timer group prints
158///
159void Timer::addPeakMemoryMeasurement() {
160 long MemUsed = getMemUsage();
161
162 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
163 E = ActiveTimers.end(); I != E; ++I)
164 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
165}
166
167
Chris Lattner6c38a792002-10-01 19:36:54 +0000168//===----------------------------------------------------------------------===//
169// TimerGroup Implementation
170//===----------------------------------------------------------------------===//
171
Chris Lattnerf205fec2003-05-09 20:05:44 +0000172// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
173// TotalWidth size, and B is the AfterDec size.
174//
175static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
176 std::ostream &OS) {
177 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
178 OS.width(TotalWidth-AfterDec-1);
179 char OldFill = OS.fill();
180 OS.fill(' ');
181 OS << (int)Val; // Integer part;
182 OS << ".";
183 OS.width(AfterDec);
184 OS.fill('0');
185 unsigned ResultFieldSize = 1;
186 while (AfterDec--) ResultFieldSize *= 10;
187 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
188 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000189}
190
Chris Lattnerf205fec2003-05-09 20:05:44 +0000191static void printVal(double Val, double Total, std::ostream &OS) {
192 if (Total < 1e-7) // Avoid dividing by zero...
193 OS << " ----- ";
194 else {
195 OS << " ";
196 printAlignedFP(Val, 4, 7, OS);
197 OS << " (";
198 printAlignedFP(Val*100/Total, 1, 5, OS);
199 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000200 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000201}
202
203void Timer::print(const Timer &Total, std::ostream &OS) {
204 if (Total.UserTime)
205 printVal(UserTime, Total.UserTime, OS);
206 if (Total.SystemTime)
207 printVal(SystemTime, Total.SystemTime, OS);
208 if (Total.getProcessTime())
209 printVal(getProcessTime(), Total.getProcessTime(), OS);
210 printVal(Elapsed, Total.Elapsed, OS);
211
212 OS << " ";
213
214 if (Total.MemUsed) {
215 OS.width(9);
216 OS << MemUsed << " ";
217 }
218 if (Total.PeakMem) {
219 if (PeakMem) {
220 OS.width(9);
221 OS << PeakMem << " ";
222 } else
223 OS << " ";
224 }
225 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000226
227 Started = false; // Once printed, don't print again
228}
229
Chris Lattnerf205fec2003-05-09 20:05:44 +0000230// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
231std::ostream *GetLibSupportInfoOutputFile() {
232 if (LibSupportInfoOutputFilename.empty())
233 return &std::cerr;
234 if (LibSupportInfoOutputFilename == "-")
235 return &std::cout;
236
237 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
238 std::ios_base::app);
239 if (!Result->good()) {
240 std::cerr << "Error opening info-output-file '"
241 << LibSupportInfoOutputFilename << " for appending!\n";
242 delete Result;
243 return &std::cerr;
244 }
245 return Result;
246}
247
Chris Lattner6c38a792002-10-01 19:36:54 +0000248
249void TimerGroup::removeTimer() {
250 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
251 // Sort the timers in descending order by amount of time taken...
252 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
253 std::greater<Timer>());
254
255 // Figure out how many spaces to indent TimerGroup name...
256 unsigned Padding = (80-Name.length())/2;
257 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
258
Chris Lattnerf205fec2003-05-09 20:05:44 +0000259 std::ostream *OutStream = GetLibSupportInfoOutputFile();
260
Chris Lattner6c38a792002-10-01 19:36:54 +0000261 ++NumTimers;
262 { // Scope to contain Total timer... don't allow total timer to drop us to
263 // zero timers...
264 Timer Total("TOTAL");
265
266 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
267 Total.sum(TimersToPrint[i]);
268
269 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000270 *OutStream << "===" << std::string(73, '-') << "===\n"
271 << std::string(Padding, ' ') << Name << "\n"
272 << "===" << std::string(73, '-')
273 << "===\n Total Execution Time: ";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000274
Chris Lattnerf205fec2003-05-09 20:05:44 +0000275 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
276 *OutStream << " seconds (";
277 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
278 *OutStream << " wall clock)\n\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000279
280 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000281 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000282 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000283 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000284 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000285 *OutStream << " --User+System--";
286 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000287 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000288 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000289 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000290 *OutStream << " -PeakMem-";
291 *OutStream << " --- Name ---\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000292
293 // Loop through all of the timing data, printing it out...
294 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000295 TimersToPrint[i].print(Total, *OutStream);
Chris Lattner6c38a792002-10-01 19:36:54 +0000296
Chris Lattnerf205fec2003-05-09 20:05:44 +0000297 Total.print(Total, *OutStream);
298 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000299 }
300 --NumTimers;
301
302 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000303
304 if (OutStream != &std::cerr && OutStream != &std::cout)
305 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000306 }
307
308 // Delete default timer group!
309 if (NumTimers == 0 && this == DefaultTimerGroup) {
310 delete DefaultTimerGroup;
311 DefaultTimerGroup = 0;
312 }
313}