blob: 30a7fbb3bd17bcf2f0dd3be77f06a1ff28987938 [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#include "Config/sys/resource.h"
10#include "Config/sys/time.h"
11#include "Config/unistd.h"
12#include "Config/malloc.h"
Chris Lattner6c38a792002-10-01 19:36:54 +000013#include <iostream>
14#include <algorithm>
Chris Lattner9550dc22002-10-27 19:08:03 +000015#include <functional>
Chris Lattnerf205fec2003-05-09 20:05:44 +000016#include <fstream>
Chris Lattnerd5a310e2003-10-06 15:02:31 +000017#include <map>
Chris Lattnerf205fec2003-05-09 20:05:44 +000018
Chris Lattner71336a92003-07-31 19:38:34 +000019// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
20// of constructor/destructor ordering being unspecified by C++. Basically the
21// problem is that a Statistic<> object gets destroyed, which ends up calling
22// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
23// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
24// would get destroyed before the Statistic, causing havoc to ensue. We "fix"
25// this by creating the string the first time it is needed and never destroying
26// it.
27static std::string &getLibSupportInfoOutputFilename() {
28 static std::string *LibSupportInfoOutputFilename = new std::string();
29 return *LibSupportInfoOutputFilename;
30}
Chris Lattner6c38a792002-10-01 19:36:54 +000031
Chris Lattner3f398492003-01-30 23:08:50 +000032namespace {
John Criswell7a73b802003-06-30 21:59:07 +000033#ifdef HAVE_MALLINFO
Chris Lattner3f398492003-01-30 23:08:50 +000034 cl::opt<bool>
35 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
36 "tracking (this may be slow)"),
37 cl::Hidden);
John Criswell7a73b802003-06-30 21:59:07 +000038#endif
Chris Lattnerf205fec2003-05-09 20:05:44 +000039
40 cl::opt<std::string, true>
Chris Lattner96a54db2003-08-01 22:15:15 +000041 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerf205fec2003-05-09 20:05:44 +000042 cl::desc("File to append -stats and -timer output to"),
Chris Lattner71336a92003-07-31 19:38:34 +000043 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Chris Lattner3f398492003-01-30 23:08:50 +000044}
45
Chris Lattner6c38a792002-10-01 19:36:54 +000046static TimerGroup *DefaultTimerGroup = 0;
47static TimerGroup *getDefaultTimerGroup() {
48 if (DefaultTimerGroup) return DefaultTimerGroup;
49 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
50}
51
52Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000053 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000054 Started(false), TG(getDefaultTimerGroup()) {
55 TG->addTimer();
56}
57
58Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000059 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000060 Started(false), TG(&tg) {
61 TG->addTimer();
62}
63
64Timer::Timer(const Timer &T) {
65 TG = T.TG;
66 if (TG) TG->addTimer();
67 operator=(T);
68}
69
70
71// Copy ctor, initialize with no TG member.
72Timer::Timer(bool, const Timer &T) {
73 TG = T.TG; // Avoid assertion in operator=
74 operator=(T); // Copy contents
75 TG = 0;
76}
77
78
79Timer::~Timer() {
80 if (TG) {
81 if (Started) {
82 Started = false;
83 TG->addTimerToPrint(*this);
84 }
85 TG->removeTimer();
86 }
87}
88
Chris Lattner8f0d8242002-11-18 21:47:09 +000089static long getMemUsage() {
John Criswell7a73b802003-06-30 21:59:07 +000090#ifdef HAVE_MALLINFO
Chris Lattner3f398492003-01-30 23:08:50 +000091 if (TrackSpace) {
92 struct mallinfo MI = mallinfo();
Chris Lattnere040f972003-02-13 05:07:53 +000093 return MI.uordblks/*+MI.hblkhd*/;
Chris Lattner3f398492003-01-30 23:08:50 +000094 }
John Criswell7a73b802003-06-30 21:59:07 +000095#endif
Brian Gaeke8c638832003-06-17 19:54:00 +000096 return 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +000097}
98
Chris Lattner6c38a792002-10-01 19:36:54 +000099struct TimeRecord {
100 double Elapsed, UserTime, SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000101 long MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000102};
103
Chris Lattner8f0d8242002-11-18 21:47:09 +0000104static TimeRecord getTimeRecord(bool Start) {
Chris Lattner6c38a792002-10-01 19:36:54 +0000105 struct rusage RU;
106 struct timeval T;
Chris Lattnerbbe5ac12003-02-05 21:44:28 +0000107 long MemUsed = 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000108 if (Start) {
109 MemUsed = getMemUsage();
110 if (getrusage(RUSAGE_SELF, &RU))
111 perror("getrusage call failed: -time-passes info incorrect!");
112 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000113 gettimeofday(&T, 0);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000114
115 if (!Start) {
116 MemUsed = getMemUsage();
117 if (getrusage(RUSAGE_SELF, &RU))
118 perror("getrusage call failed: -time-passes info incorrect!");
Chris Lattner6c38a792002-10-01 19:36:54 +0000119 }
120
121 TimeRecord Result;
122 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
123 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
124 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000125 Result.MemUsed = MemUsed;
126
Chris Lattner6c38a792002-10-01 19:36:54 +0000127 return Result;
128}
129
Chris Lattner8f0d8242002-11-18 21:47:09 +0000130static std::vector<Timer*> ActiveTimers;
131
Chris Lattner6c38a792002-10-01 19:36:54 +0000132void Timer::startTimer() {
133 Started = true;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000134 TimeRecord TR = getTimeRecord(true);
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 PeakMemBase = TR.MemUsed;
140 ActiveTimers.push_back(this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000141}
142
143void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000144 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000145 Elapsed += TR.Elapsed;
146 UserTime += TR.UserTime;
147 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000148 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000149
150 if (ActiveTimers.back() == this) {
151 ActiveTimers.pop_back();
152 } else {
153 std::vector<Timer*>::iterator I =
154 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
155 assert(I != ActiveTimers.end() && "stop but no startTimer?");
156 ActiveTimers.erase(I);
157 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000158}
159
160void Timer::sum(const Timer &T) {
161 Elapsed += T.Elapsed;
162 UserTime += T.UserTime;
163 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000164 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000165 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000166}
167
Chris Lattner8f0d8242002-11-18 21:47:09 +0000168/// addPeakMemoryMeasurement - This method should be called whenever memory
169/// usage needs to be checked. It adds a peak memory measurement to the
170/// currently active timers, which will be printed when the timer group prints
171///
172void Timer::addPeakMemoryMeasurement() {
173 long MemUsed = getMemUsage();
174
175 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
176 E = ActiveTimers.end(); I != E; ++I)
177 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
178}
179
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000180//===----------------------------------------------------------------------===//
181// NamedRegionTimer Implementation
182//===----------------------------------------------------------------------===//
183
184static Timer &getNamedRegionTimer(const std::string &Name) {
185 static std::map<std::string, Timer> NamedTimers;
186
187 std::map<std::string, Timer>::iterator I = NamedTimers.lower_bound(Name);
188 if (I != NamedTimers.end() && I->first == Name)
189 return I->second;
190
191 return NamedTimers.insert(I, std::make_pair(Name, Timer(Name)))->second;
192}
193
194NamedRegionTimer::NamedRegionTimer(const std::string &Name)
195 : TimeRegion(getNamedRegionTimer(Name)) {}
196
Chris Lattner8f0d8242002-11-18 21:47:09 +0000197
Chris Lattner6c38a792002-10-01 19:36:54 +0000198//===----------------------------------------------------------------------===//
199// TimerGroup Implementation
200//===----------------------------------------------------------------------===//
201
Chris Lattnerf205fec2003-05-09 20:05:44 +0000202// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
203// TotalWidth size, and B is the AfterDec size.
204//
205static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
206 std::ostream &OS) {
207 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
208 OS.width(TotalWidth-AfterDec-1);
209 char OldFill = OS.fill();
210 OS.fill(' ');
211 OS << (int)Val; // Integer part;
212 OS << ".";
213 OS.width(AfterDec);
214 OS.fill('0');
215 unsigned ResultFieldSize = 1;
216 while (AfterDec--) ResultFieldSize *= 10;
217 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
218 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000219}
220
Chris Lattnerf205fec2003-05-09 20:05:44 +0000221static void printVal(double Val, double Total, std::ostream &OS) {
222 if (Total < 1e-7) // Avoid dividing by zero...
223 OS << " ----- ";
224 else {
225 OS << " ";
226 printAlignedFP(Val, 4, 7, OS);
227 OS << " (";
228 printAlignedFP(Val*100/Total, 1, 5, OS);
229 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000230 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000231}
232
233void Timer::print(const Timer &Total, std::ostream &OS) {
234 if (Total.UserTime)
235 printVal(UserTime, Total.UserTime, OS);
236 if (Total.SystemTime)
237 printVal(SystemTime, Total.SystemTime, OS);
238 if (Total.getProcessTime())
239 printVal(getProcessTime(), Total.getProcessTime(), OS);
240 printVal(Elapsed, Total.Elapsed, OS);
241
242 OS << " ";
243
244 if (Total.MemUsed) {
245 OS.width(9);
246 OS << MemUsed << " ";
247 }
248 if (Total.PeakMem) {
249 if (PeakMem) {
250 OS.width(9);
251 OS << PeakMem << " ";
252 } else
253 OS << " ";
254 }
255 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000256
257 Started = false; // Once printed, don't print again
258}
259
Chris Lattnerf205fec2003-05-09 20:05:44 +0000260// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
261std::ostream *GetLibSupportInfoOutputFile() {
Chris Lattner71336a92003-07-31 19:38:34 +0000262 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000263 if (LibSupportInfoOutputFilename.empty())
264 return &std::cerr;
265 if (LibSupportInfoOutputFilename == "-")
266 return &std::cout;
267
268 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
Chris Lattner903c2d12003-06-06 22:13:01 +0000269 std::ios::app);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000270 if (!Result->good()) {
271 std::cerr << "Error opening info-output-file '"
272 << LibSupportInfoOutputFilename << " for appending!\n";
273 delete Result;
274 return &std::cerr;
275 }
276 return Result;
277}
278
Chris Lattner6c38a792002-10-01 19:36:54 +0000279
280void TimerGroup::removeTimer() {
281 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
282 // Sort the timers in descending order by amount of time taken...
283 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
284 std::greater<Timer>());
285
286 // Figure out how many spaces to indent TimerGroup name...
287 unsigned Padding = (80-Name.length())/2;
288 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
289
Chris Lattnerf205fec2003-05-09 20:05:44 +0000290 std::ostream *OutStream = GetLibSupportInfoOutputFile();
291
Chris Lattner6c38a792002-10-01 19:36:54 +0000292 ++NumTimers;
293 { // Scope to contain Total timer... don't allow total timer to drop us to
294 // zero timers...
295 Timer Total("TOTAL");
296
297 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
298 Total.sum(TimersToPrint[i]);
299
300 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000301 *OutStream << "===" << std::string(73, '-') << "===\n"
302 << std::string(Padding, ' ') << Name << "\n"
303 << "===" << std::string(73, '-')
304 << "===\n Total Execution Time: ";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000305
Chris Lattnerf205fec2003-05-09 20:05:44 +0000306 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
307 *OutStream << " seconds (";
308 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
309 *OutStream << " wall clock)\n\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000310
311 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000312 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000313 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000314 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000315 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000316 *OutStream << " --User+System--";
317 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000318 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000319 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000320 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000321 *OutStream << " -PeakMem-";
322 *OutStream << " --- Name ---\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000323
324 // Loop through all of the timing data, printing it out...
325 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000326 TimersToPrint[i].print(Total, *OutStream);
Chris Lattner6c38a792002-10-01 19:36:54 +0000327
Chris Lattnerf205fec2003-05-09 20:05:44 +0000328 Total.print(Total, *OutStream);
329 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000330 }
331 --NumTimers;
332
333 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000334
335 if (OutStream != &std::cerr && OutStream != &std::cout)
336 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000337 }
338
339 // Delete default timer group!
340 if (NumTimers == 0 && this == DefaultTimerGroup) {
341 delete DefaultTimerGroup;
342 DefaultTimerGroup = 0;
343 }
344}