blob: 1b2f91d03b430a6fd984017e83a3f928ee7e806f [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
Reid Spencer551ccae2004-09-01 22:55:40 +000014#include "llvm/Support/Timer.h"
15#include "llvm/Support/CommandLine.h"
Reid Spencerdf52c9a2004-12-20 00:59:04 +000016#include "llvm/System/Process.h"
Reid Spencer0255abb2004-12-20 03:59:23 +000017#include <algorithm>
Chris Lattnerb4db5f32004-06-07 19:34:51 +000018#include <fstream>
Reid Spencer0255abb2004-12-20 03:59:23 +000019#include <functional>
Reid Spencerdf52c9a2004-12-20 00:59:04 +000020#include <iostream>
Chris Lattnerb4db5f32004-06-07 19:34:51 +000021#include <map>
Reid Spencerdf52c9a2004-12-20 00:59:04 +000022
Chris Lattnerb6d465f2003-12-14 21:27:33 +000023using namespace llvm;
Chris Lattnerf205fec2003-05-09 20:05:44 +000024
Chris Lattnerb4db5f32004-06-07 19:34:51 +000025// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattnerb6d465f2003-12-14 21:27:33 +000026namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
Brian Gaeked0fde302003-11-11 22:41:34 +000027
Chris Lattner71336a92003-07-31 19:38:34 +000028// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
29// of constructor/destructor ordering being unspecified by C++. Basically the
30// problem is that a Statistic<> object gets destroyed, which ends up calling
31// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
32// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
Reid Spencerf6e5a252004-12-14 03:55:21 +000033// would get destroyed before the Statistic, causing havoc to ensue. We "fix"
34// this by creating the string the first time it is needed and never destroying
35// it.
Chris Lattner71336a92003-07-31 19:38:34 +000036static std::string &getLibSupportInfoOutputFilename() {
Reid Spencerf6e5a252004-12-14 03:55:21 +000037 static std::string *LibSupportInfoOutputFilename = new std::string();
38 return *LibSupportInfoOutputFilename;
Chris Lattner71336a92003-07-31 19:38:34 +000039}
Chris Lattner6c38a792002-10-01 19:36:54 +000040
Chris Lattner3f398492003-01-30 23:08:50 +000041namespace {
42 cl::opt<bool>
43 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
44 "tracking (this may be slow)"),
45 cl::Hidden);
Chris Lattnerf205fec2003-05-09 20:05:44 +000046
47 cl::opt<std::string, true>
Chris Lattner96a54db2003-08-01 22:15:15 +000048 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerf205fec2003-05-09 20:05:44 +000049 cl::desc("File to append -stats and -timer output to"),
Chris Lattner71336a92003-07-31 19:38:34 +000050 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Chris Lattner3f398492003-01-30 23:08:50 +000051}
52
Chris Lattner6c38a792002-10-01 19:36:54 +000053static TimerGroup *DefaultTimerGroup = 0;
54static TimerGroup *getDefaultTimerGroup() {
55 if (DefaultTimerGroup) return DefaultTimerGroup;
56 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
57}
58
59Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000060 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000061 Started(false), TG(getDefaultTimerGroup()) {
62 TG->addTimer();
63}
64
65Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000066 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000067 Started(false), TG(&tg) {
68 TG->addTimer();
69}
70
71Timer::Timer(const Timer &T) {
72 TG = T.TG;
73 if (TG) TG->addTimer();
74 operator=(T);
75}
76
77
78// Copy ctor, initialize with no TG member.
79Timer::Timer(bool, const Timer &T) {
80 TG = T.TG; // Avoid assertion in operator=
81 operator=(T); // Copy contents
82 TG = 0;
83}
84
85
86Timer::~Timer() {
87 if (TG) {
88 if (Started) {
89 Started = false;
90 TG->addTimerToPrint(*this);
91 }
92 TG->removeTimer();
93 }
94}
95
96struct TimeRecord {
97 double Elapsed, UserTime, SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +000098 long MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +000099};
100
Chris Lattner8f0d8242002-11-18 21:47:09 +0000101static TimeRecord getTimeRecord(bool Start) {
Chris Lattnerb4db5f32004-06-07 19:34:51 +0000102 TimeRecord Result;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000103
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000104 sys::TimeValue now(0,0);
105 sys::TimeValue user(0,0);
106 sys::TimeValue sys(0,0);
Chris Lattner6c38a792002-10-01 19:36:54 +0000107
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000108 sys::Process::GetTimeUsage(now,user,sys);
109
110 Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0;
111 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
112 Result.UserTime = sys.seconds() + sys.microseconds() / 1000000.0;
113 Result.MemUsed = sys::Process::GetMallocUsage();
114
Chris Lattner6c38a792002-10-01 19:36:54 +0000115 return Result;
116}
117
Chris Lattner8f0d8242002-11-18 21:47:09 +0000118static std::vector<Timer*> ActiveTimers;
119
Chris Lattner6c38a792002-10-01 19:36:54 +0000120void Timer::startTimer() {
121 Started = true;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000122 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000123 Elapsed -= TR.Elapsed;
124 UserTime -= TR.UserTime;
125 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000126 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000127 PeakMemBase = TR.MemUsed;
128 ActiveTimers.push_back(this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000129}
130
131void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000132 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000133 Elapsed += TR.Elapsed;
134 UserTime += TR.UserTime;
135 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000136 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000137
138 if (ActiveTimers.back() == this) {
139 ActiveTimers.pop_back();
140 } else {
141 std::vector<Timer*>::iterator I =
142 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
143 assert(I != ActiveTimers.end() && "stop but no startTimer?");
144 ActiveTimers.erase(I);
145 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000146}
147
148void Timer::sum(const Timer &T) {
149 Elapsed += T.Elapsed;
150 UserTime += T.UserTime;
151 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000152 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000153 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000154}
155
Chris Lattner8f0d8242002-11-18 21:47:09 +0000156/// addPeakMemoryMeasurement - This method should be called whenever memory
157/// usage needs to be checked. It adds a peak memory measurement to the
158/// currently active timers, which will be printed when the timer group prints
159///
160void Timer::addPeakMemoryMeasurement() {
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000161 long MemUsed = sys::Process::GetMallocUsage();
Chris Lattner8f0d8242002-11-18 21:47:09 +0000162
163 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
164 E = ActiveTimers.end(); I != E; ++I)
165 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
166}
167
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000168//===----------------------------------------------------------------------===//
169// NamedRegionTimer Implementation
170//===----------------------------------------------------------------------===//
171
172static Timer &getNamedRegionTimer(const std::string &Name) {
173 static std::map<std::string, Timer> NamedTimers;
174
175 std::map<std::string, Timer>::iterator I = NamedTimers.lower_bound(Name);
176 if (I != NamedTimers.end() && I->first == Name)
177 return I->second;
178
179 return NamedTimers.insert(I, std::make_pair(Name, Timer(Name)))->second;
180}
181
182NamedRegionTimer::NamedRegionTimer(const std::string &Name)
183 : TimeRegion(getNamedRegionTimer(Name)) {}
184
Chris Lattner8f0d8242002-11-18 21:47:09 +0000185
Chris Lattner6c38a792002-10-01 19:36:54 +0000186//===----------------------------------------------------------------------===//
187// TimerGroup Implementation
188//===----------------------------------------------------------------------===//
189
Chris Lattnerf205fec2003-05-09 20:05:44 +0000190// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
191// TotalWidth size, and B is the AfterDec size.
192//
193static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
194 std::ostream &OS) {
195 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
196 OS.width(TotalWidth-AfterDec-1);
197 char OldFill = OS.fill();
198 OS.fill(' ');
199 OS << (int)Val; // Integer part;
200 OS << ".";
201 OS.width(AfterDec);
202 OS.fill('0');
203 unsigned ResultFieldSize = 1;
204 while (AfterDec--) ResultFieldSize *= 10;
205 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
206 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000207}
208
Chris Lattnerf205fec2003-05-09 20:05:44 +0000209static void printVal(double Val, double Total, std::ostream &OS) {
210 if (Total < 1e-7) // Avoid dividing by zero...
211 OS << " ----- ";
212 else {
213 OS << " ";
214 printAlignedFP(Val, 4, 7, OS);
215 OS << " (";
216 printAlignedFP(Val*100/Total, 1, 5, OS);
217 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000218 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000219}
220
221void Timer::print(const Timer &Total, std::ostream &OS) {
222 if (Total.UserTime)
223 printVal(UserTime, Total.UserTime, OS);
224 if (Total.SystemTime)
225 printVal(SystemTime, Total.SystemTime, OS);
226 if (Total.getProcessTime())
227 printVal(getProcessTime(), Total.getProcessTime(), OS);
228 printVal(Elapsed, Total.Elapsed, OS);
229
230 OS << " ";
231
232 if (Total.MemUsed) {
233 OS.width(9);
234 OS << MemUsed << " ";
235 }
236 if (Total.PeakMem) {
237 if (PeakMem) {
238 OS.width(9);
239 OS << PeakMem << " ";
240 } else
241 OS << " ";
242 }
243 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000244
245 Started = false; // Once printed, don't print again
246}
247
Chris Lattnerf205fec2003-05-09 20:05:44 +0000248// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
Brian Gaeked0fde302003-11-11 22:41:34 +0000249std::ostream *
Chris Lattnerb6d465f2003-12-14 21:27:33 +0000250llvm::GetLibSupportInfoOutputFile() {
Chris Lattner71336a92003-07-31 19:38:34 +0000251 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000252 if (LibSupportInfoOutputFilename.empty())
253 return &std::cerr;
254 if (LibSupportInfoOutputFilename == "-")
255 return &std::cout;
256
257 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
Chris Lattner903c2d12003-06-06 22:13:01 +0000258 std::ios::app);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000259 if (!Result->good()) {
260 std::cerr << "Error opening info-output-file '"
261 << LibSupportInfoOutputFilename << " for appending!\n";
262 delete Result;
263 return &std::cerr;
264 }
265 return Result;
266}
267
Chris Lattner6c38a792002-10-01 19:36:54 +0000268
269void TimerGroup::removeTimer() {
270 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
271 // Sort the timers in descending order by amount of time taken...
272 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
273 std::greater<Timer>());
274
275 // Figure out how many spaces to indent TimerGroup name...
276 unsigned Padding = (80-Name.length())/2;
277 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
278
Chris Lattnerf205fec2003-05-09 20:05:44 +0000279 std::ostream *OutStream = GetLibSupportInfoOutputFile();
280
Chris Lattner6c38a792002-10-01 19:36:54 +0000281 ++NumTimers;
282 { // Scope to contain Total timer... don't allow total timer to drop us to
283 // zero timers...
284 Timer Total("TOTAL");
285
286 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
287 Total.sum(TimersToPrint[i]);
288
289 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000290 *OutStream << "===" << std::string(73, '-') << "===\n"
291 << std::string(Padding, ' ') << Name << "\n"
292 << "===" << std::string(73, '-')
293 << "===\n Total Execution Time: ";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000294
Chris Lattnerf205fec2003-05-09 20:05:44 +0000295 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
296 *OutStream << " seconds (";
297 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
298 *OutStream << " wall clock)\n\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000299
300 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000301 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000302 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000303 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000304 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000305 *OutStream << " --User+System--";
306 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000307 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000308 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000309 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000310 *OutStream << " -PeakMem-";
311 *OutStream << " --- Name ---\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000312
313 // Loop through all of the timing data, printing it out...
314 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000315 TimersToPrint[i].print(Total, *OutStream);
Chris Lattner6c38a792002-10-01 19:36:54 +0000316
Chris Lattnerf205fec2003-05-09 20:05:44 +0000317 Total.print(Total, *OutStream);
318 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000319 }
320 --NumTimers;
321
322 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000323
324 if (OutStream != &std::cerr && OutStream != &std::cout)
325 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000326 }
327
328 // Delete default timer group!
329 if (NumTimers == 0 && this == DefaultTimerGroup) {
330 delete DefaultTimerGroup;
331 DefaultTimerGroup = 0;
332 }
333}
Brian Gaeked0fde302003-11-11 22:41:34 +0000334