blob: 5d493b8a15e2e329b073362fea01235e384468b3 [file] [log] [blame]
Chris Lattner6c38a792002-10-01 19:36:54 +00001//===-- Timer.cpp - Interval Timing Support -------------------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner6c38a792002-10-01 19:36:54 +00009//
10// Interval Timing implementation.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Support/Timer.h"
Chris Lattner3f398492003-01-30 23:08:50 +000015#include "Support/CommandLine.h"
John Criswell7a73b802003-06-30 21:59:07 +000016#include "Config/sys/resource.h"
17#include "Config/sys/time.h"
18#include "Config/unistd.h"
19#include "Config/malloc.h"
Chris Lattner6c38a792002-10-01 19:36:54 +000020#include <iostream>
21#include <algorithm>
Chris Lattner9550dc22002-10-27 19:08:03 +000022#include <functional>
Chris Lattnerf205fec2003-05-09 20:05:44 +000023#include <fstream>
Chris Lattnerd5a310e2003-10-06 15:02:31 +000024#include <map>
Chris Lattnerb6d465f2003-12-14 21:27:33 +000025using namespace llvm;
Chris Lattnerf205fec2003-05-09 20:05:44 +000026
Chris Lattnerb6d465f2003-12-14 21:27:33 +000027// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
28namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner71336a92003-07-31 19:38:34 +000030// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
31// of constructor/destructor ordering being unspecified by C++. Basically the
32// problem is that a Statistic<> object gets destroyed, which ends up calling
33// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
34// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
35// would get destroyed before the Statistic, causing havoc to ensue. We "fix"
36// this by creating the string the first time it is needed and never destroying
37// it.
38static std::string &getLibSupportInfoOutputFilename() {
39 static std::string *LibSupportInfoOutputFilename = new std::string();
40 return *LibSupportInfoOutputFilename;
41}
Chris Lattner6c38a792002-10-01 19:36:54 +000042
Chris Lattner3f398492003-01-30 23:08:50 +000043namespace {
John Criswell7a73b802003-06-30 21:59:07 +000044#ifdef HAVE_MALLINFO
Chris Lattner3f398492003-01-30 23:08:50 +000045 cl::opt<bool>
46 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
47 "tracking (this may be slow)"),
48 cl::Hidden);
John Criswell7a73b802003-06-30 21:59:07 +000049#endif
Chris Lattnerf205fec2003-05-09 20:05:44 +000050
51 cl::opt<std::string, true>
Chris Lattner96a54db2003-08-01 22:15:15 +000052 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerf205fec2003-05-09 20:05:44 +000053 cl::desc("File to append -stats and -timer output to"),
Chris Lattner71336a92003-07-31 19:38:34 +000054 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Chris Lattner3f398492003-01-30 23:08:50 +000055}
56
Chris Lattner6c38a792002-10-01 19:36:54 +000057static TimerGroup *DefaultTimerGroup = 0;
58static TimerGroup *getDefaultTimerGroup() {
59 if (DefaultTimerGroup) return DefaultTimerGroup;
60 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
61}
62
63Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000064 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000065 Started(false), TG(getDefaultTimerGroup()) {
66 TG->addTimer();
67}
68
69Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000070 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000071 Started(false), TG(&tg) {
72 TG->addTimer();
73}
74
75Timer::Timer(const Timer &T) {
76 TG = T.TG;
77 if (TG) TG->addTimer();
78 operator=(T);
79}
80
81
82// Copy ctor, initialize with no TG member.
83Timer::Timer(bool, const Timer &T) {
84 TG = T.TG; // Avoid assertion in operator=
85 operator=(T); // Copy contents
86 TG = 0;
87}
88
89
90Timer::~Timer() {
91 if (TG) {
92 if (Started) {
93 Started = false;
94 TG->addTimerToPrint(*this);
95 }
96 TG->removeTimer();
97 }
98}
99
Chris Lattner8f0d8242002-11-18 21:47:09 +0000100static long getMemUsage() {
John Criswell7a73b802003-06-30 21:59:07 +0000101#ifdef HAVE_MALLINFO
Chris Lattner3f398492003-01-30 23:08:50 +0000102 if (TrackSpace) {
103 struct mallinfo MI = mallinfo();
Chris Lattnere040f972003-02-13 05:07:53 +0000104 return MI.uordblks/*+MI.hblkhd*/;
Chris Lattner3f398492003-01-30 23:08:50 +0000105 }
John Criswell7a73b802003-06-30 21:59:07 +0000106#endif
Brian Gaeke8c638832003-06-17 19:54:00 +0000107 return 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000108}
109
Chris Lattner6c38a792002-10-01 19:36:54 +0000110struct TimeRecord {
111 double Elapsed, UserTime, SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000112 long MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000113};
114
Chris Lattner8f0d8242002-11-18 21:47:09 +0000115static TimeRecord getTimeRecord(bool Start) {
Chris Lattner6c38a792002-10-01 19:36:54 +0000116 struct rusage RU;
117 struct timeval T;
Chris Lattnerbbe5ac12003-02-05 21:44:28 +0000118 long MemUsed = 0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000119 if (Start) {
120 MemUsed = getMemUsage();
121 if (getrusage(RUSAGE_SELF, &RU))
122 perror("getrusage call failed: -time-passes info incorrect!");
123 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000124 gettimeofday(&T, 0);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000125
126 if (!Start) {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000127 if (getrusage(RUSAGE_SELF, &RU))
128 perror("getrusage call failed: -time-passes info incorrect!");
Chris Lattnerb6d465f2003-12-14 21:27:33 +0000129 MemUsed = getMemUsage();
Chris Lattner6c38a792002-10-01 19:36:54 +0000130 }
131
132 TimeRecord Result;
133 Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0;
134 Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0;
135 Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000136 Result.MemUsed = MemUsed;
137
Chris Lattner6c38a792002-10-01 19:36:54 +0000138 return Result;
139}
140
Chris Lattner8f0d8242002-11-18 21:47:09 +0000141static std::vector<Timer*> ActiveTimers;
142
Chris Lattner6c38a792002-10-01 19:36:54 +0000143void Timer::startTimer() {
144 Started = true;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000145 TimeRecord TR = getTimeRecord(true);
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 PeakMemBase = TR.MemUsed;
151 ActiveTimers.push_back(this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000152}
153
154void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000155 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000156 Elapsed += TR.Elapsed;
157 UserTime += TR.UserTime;
158 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000159 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000160
161 if (ActiveTimers.back() == this) {
162 ActiveTimers.pop_back();
163 } else {
164 std::vector<Timer*>::iterator I =
165 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
166 assert(I != ActiveTimers.end() && "stop but no startTimer?");
167 ActiveTimers.erase(I);
168 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000169}
170
171void Timer::sum(const Timer &T) {
172 Elapsed += T.Elapsed;
173 UserTime += T.UserTime;
174 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000175 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000176 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000177}
178
Chris Lattner8f0d8242002-11-18 21:47:09 +0000179/// addPeakMemoryMeasurement - This method should be called whenever memory
180/// usage needs to be checked. It adds a peak memory measurement to the
181/// currently active timers, which will be printed when the timer group prints
182///
183void Timer::addPeakMemoryMeasurement() {
184 long MemUsed = getMemUsage();
185
186 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
187 E = ActiveTimers.end(); I != E; ++I)
188 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
189}
190
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000191//===----------------------------------------------------------------------===//
192// NamedRegionTimer Implementation
193//===----------------------------------------------------------------------===//
194
195static Timer &getNamedRegionTimer(const std::string &Name) {
196 static std::map<std::string, Timer> NamedTimers;
197
198 std::map<std::string, Timer>::iterator I = NamedTimers.lower_bound(Name);
199 if (I != NamedTimers.end() && I->first == Name)
200 return I->second;
201
202 return NamedTimers.insert(I, std::make_pair(Name, Timer(Name)))->second;
203}
204
205NamedRegionTimer::NamedRegionTimer(const std::string &Name)
206 : TimeRegion(getNamedRegionTimer(Name)) {}
207
Chris Lattner8f0d8242002-11-18 21:47:09 +0000208
Chris Lattner6c38a792002-10-01 19:36:54 +0000209//===----------------------------------------------------------------------===//
210// TimerGroup Implementation
211//===----------------------------------------------------------------------===//
212
Chris Lattnerf205fec2003-05-09 20:05:44 +0000213// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
214// TotalWidth size, and B is the AfterDec size.
215//
216static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
217 std::ostream &OS) {
218 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
219 OS.width(TotalWidth-AfterDec-1);
220 char OldFill = OS.fill();
221 OS.fill(' ');
222 OS << (int)Val; // Integer part;
223 OS << ".";
224 OS.width(AfterDec);
225 OS.fill('0');
226 unsigned ResultFieldSize = 1;
227 while (AfterDec--) ResultFieldSize *= 10;
228 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
229 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000230}
231
Chris Lattnerf205fec2003-05-09 20:05:44 +0000232static void printVal(double Val, double Total, std::ostream &OS) {
233 if (Total < 1e-7) // Avoid dividing by zero...
234 OS << " ----- ";
235 else {
236 OS << " ";
237 printAlignedFP(Val, 4, 7, OS);
238 OS << " (";
239 printAlignedFP(Val*100/Total, 1, 5, OS);
240 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000241 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000242}
243
244void Timer::print(const Timer &Total, std::ostream &OS) {
245 if (Total.UserTime)
246 printVal(UserTime, Total.UserTime, OS);
247 if (Total.SystemTime)
248 printVal(SystemTime, Total.SystemTime, OS);
249 if (Total.getProcessTime())
250 printVal(getProcessTime(), Total.getProcessTime(), OS);
251 printVal(Elapsed, Total.Elapsed, OS);
252
253 OS << " ";
254
255 if (Total.MemUsed) {
256 OS.width(9);
257 OS << MemUsed << " ";
258 }
259 if (Total.PeakMem) {
260 if (PeakMem) {
261 OS.width(9);
262 OS << PeakMem << " ";
263 } else
264 OS << " ";
265 }
266 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000267
268 Started = false; // Once printed, don't print again
269}
270
Chris Lattnerf205fec2003-05-09 20:05:44 +0000271// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
Brian Gaeked0fde302003-11-11 22:41:34 +0000272std::ostream *
Chris Lattnerb6d465f2003-12-14 21:27:33 +0000273llvm::GetLibSupportInfoOutputFile() {
Chris Lattner71336a92003-07-31 19:38:34 +0000274 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000275 if (LibSupportInfoOutputFilename.empty())
276 return &std::cerr;
277 if (LibSupportInfoOutputFilename == "-")
278 return &std::cout;
279
280 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
Chris Lattner903c2d12003-06-06 22:13:01 +0000281 std::ios::app);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000282 if (!Result->good()) {
283 std::cerr << "Error opening info-output-file '"
284 << LibSupportInfoOutputFilename << " for appending!\n";
285 delete Result;
286 return &std::cerr;
287 }
288 return Result;
289}
290
Chris Lattner6c38a792002-10-01 19:36:54 +0000291
292void TimerGroup::removeTimer() {
293 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
294 // Sort the timers in descending order by amount of time taken...
295 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
296 std::greater<Timer>());
297
298 // Figure out how many spaces to indent TimerGroup name...
299 unsigned Padding = (80-Name.length())/2;
300 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
301
Chris Lattnerf205fec2003-05-09 20:05:44 +0000302 std::ostream *OutStream = GetLibSupportInfoOutputFile();
303
Chris Lattner6c38a792002-10-01 19:36:54 +0000304 ++NumTimers;
305 { // Scope to contain Total timer... don't allow total timer to drop us to
306 // zero timers...
307 Timer Total("TOTAL");
308
309 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
310 Total.sum(TimersToPrint[i]);
311
312 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000313 *OutStream << "===" << std::string(73, '-') << "===\n"
314 << std::string(Padding, ' ') << Name << "\n"
315 << "===" << std::string(73, '-')
316 << "===\n Total Execution Time: ";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000317
Chris Lattnerf205fec2003-05-09 20:05:44 +0000318 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
319 *OutStream << " seconds (";
320 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
321 *OutStream << " wall clock)\n\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000322
323 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000324 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000325 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000326 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000327 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000328 *OutStream << " --User+System--";
329 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000330 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000331 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000332 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000333 *OutStream << " -PeakMem-";
334 *OutStream << " --- Name ---\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000335
336 // Loop through all of the timing data, printing it out...
337 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000338 TimersToPrint[i].print(Total, *OutStream);
Chris Lattner6c38a792002-10-01 19:36:54 +0000339
Chris Lattnerf205fec2003-05-09 20:05:44 +0000340 Total.print(Total, *OutStream);
341 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000342 }
343 --NumTimers;
344
345 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000346
347 if (OutStream != &std::cerr && OutStream != &std::cout)
348 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000349 }
350
351 // Delete default timer group!
352 if (NumTimers == 0 && this == DefaultTimerGroup) {
353 delete DefaultTimerGroup;
354 DefaultTimerGroup = 0;
355 }
356}
Brian Gaeked0fde302003-11-11 22:41:34 +0000357