blob: 01ab03dd2bf7f8e53d0783bfd8159ec8c53eebc7 [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"
Chris Lattnerb4db5f32004-06-07 19:34:51 +000017#include <fstream>
Reid Spencerdf52c9a2004-12-20 00:59:04 +000018#include <iostream>
Chris Lattnerb4db5f32004-06-07 19:34:51 +000019#include <map>
Reid Spencerdf52c9a2004-12-20 00:59:04 +000020
Chris Lattnerb6d465f2003-12-14 21:27:33 +000021using namespace llvm;
Chris Lattnerf205fec2003-05-09 20:05:44 +000022
Chris Lattnerb4db5f32004-06-07 19:34:51 +000023// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattnerb6d465f2003-12-14 21:27:33 +000024namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
Brian Gaeked0fde302003-11-11 22:41:34 +000025
Chris Lattner71336a92003-07-31 19:38:34 +000026// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
27// of constructor/destructor ordering being unspecified by C++. Basically the
28// problem is that a Statistic<> object gets destroyed, which ends up calling
29// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
30// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
Reid Spencerf6e5a252004-12-14 03:55:21 +000031// would get destroyed before the Statistic, causing havoc to ensue. We "fix"
32// this by creating the string the first time it is needed and never destroying
33// it.
Chris Lattner71336a92003-07-31 19:38:34 +000034static std::string &getLibSupportInfoOutputFilename() {
Reid Spencerf6e5a252004-12-14 03:55:21 +000035 static std::string *LibSupportInfoOutputFilename = new std::string();
36 return *LibSupportInfoOutputFilename;
Chris Lattner71336a92003-07-31 19:38:34 +000037}
Chris Lattner6c38a792002-10-01 19:36:54 +000038
Chris Lattner3f398492003-01-30 23:08:50 +000039namespace {
40 cl::opt<bool>
41 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
42 "tracking (this may be slow)"),
43 cl::Hidden);
Chris Lattnerf205fec2003-05-09 20:05:44 +000044
45 cl::opt<std::string, true>
Chris Lattner96a54db2003-08-01 22:15:15 +000046 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerf205fec2003-05-09 20:05:44 +000047 cl::desc("File to append -stats and -timer output to"),
Chris Lattner71336a92003-07-31 19:38:34 +000048 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Chris Lattner3f398492003-01-30 23:08:50 +000049}
50
Chris Lattner6c38a792002-10-01 19:36:54 +000051static TimerGroup *DefaultTimerGroup = 0;
52static TimerGroup *getDefaultTimerGroup() {
53 if (DefaultTimerGroup) return DefaultTimerGroup;
54 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
55}
56
57Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000058 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000059 Started(false), TG(getDefaultTimerGroup()) {
60 TG->addTimer();
61}
62
63Timer::Timer(const std::string &N, TimerGroup &tg)
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(&tg) {
66 TG->addTimer();
67}
68
69Timer::Timer(const Timer &T) {
70 TG = T.TG;
71 if (TG) TG->addTimer();
72 operator=(T);
73}
74
75
76// Copy ctor, initialize with no TG member.
77Timer::Timer(bool, const Timer &T) {
78 TG = T.TG; // Avoid assertion in operator=
79 operator=(T); // Copy contents
80 TG = 0;
81}
82
83
84Timer::~Timer() {
85 if (TG) {
86 if (Started) {
87 Started = false;
88 TG->addTimerToPrint(*this);
89 }
90 TG->removeTimer();
91 }
92}
93
94struct TimeRecord {
95 double Elapsed, UserTime, SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +000096 long MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +000097};
98
Chris Lattner8f0d8242002-11-18 21:47:09 +000099static TimeRecord getTimeRecord(bool Start) {
Chris Lattnerb4db5f32004-06-07 19:34:51 +0000100 TimeRecord Result;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000101
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000102 sys::TimeValue now(0,0);
103 sys::TimeValue user(0,0);
104 sys::TimeValue sys(0,0);
Chris Lattner6c38a792002-10-01 19:36:54 +0000105
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000106 sys::Process::GetTimeUsage(now,user,sys);
107
108 Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0;
109 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
110 Result.UserTime = sys.seconds() + sys.microseconds() / 1000000.0;
111 Result.MemUsed = sys::Process::GetMallocUsage();
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() {
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000159 long MemUsed = sys::Process::GetMallocUsage();
Chris Lattner8f0d8242002-11-18 21:47:09 +0000160
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
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000166//===----------------------------------------------------------------------===//
167// NamedRegionTimer Implementation
168//===----------------------------------------------------------------------===//
169
170static Timer &getNamedRegionTimer(const std::string &Name) {
171 static std::map<std::string, Timer> NamedTimers;
172
173 std::map<std::string, Timer>::iterator I = NamedTimers.lower_bound(Name);
174 if (I != NamedTimers.end() && I->first == Name)
175 return I->second;
176
177 return NamedTimers.insert(I, std::make_pair(Name, Timer(Name)))->second;
178}
179
180NamedRegionTimer::NamedRegionTimer(const std::string &Name)
181 : TimeRegion(getNamedRegionTimer(Name)) {}
182
Chris Lattner8f0d8242002-11-18 21:47:09 +0000183
Chris Lattner6c38a792002-10-01 19:36:54 +0000184//===----------------------------------------------------------------------===//
185// TimerGroup Implementation
186//===----------------------------------------------------------------------===//
187
Chris Lattnerf205fec2003-05-09 20:05:44 +0000188// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
189// TotalWidth size, and B is the AfterDec size.
190//
191static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
192 std::ostream &OS) {
193 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
194 OS.width(TotalWidth-AfterDec-1);
195 char OldFill = OS.fill();
196 OS.fill(' ');
197 OS << (int)Val; // Integer part;
198 OS << ".";
199 OS.width(AfterDec);
200 OS.fill('0');
201 unsigned ResultFieldSize = 1;
202 while (AfterDec--) ResultFieldSize *= 10;
203 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
204 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000205}
206
Chris Lattnerf205fec2003-05-09 20:05:44 +0000207static void printVal(double Val, double Total, std::ostream &OS) {
208 if (Total < 1e-7) // Avoid dividing by zero...
209 OS << " ----- ";
210 else {
211 OS << " ";
212 printAlignedFP(Val, 4, 7, OS);
213 OS << " (";
214 printAlignedFP(Val*100/Total, 1, 5, OS);
215 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000216 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000217}
218
219void Timer::print(const Timer &Total, std::ostream &OS) {
220 if (Total.UserTime)
221 printVal(UserTime, Total.UserTime, OS);
222 if (Total.SystemTime)
223 printVal(SystemTime, Total.SystemTime, OS);
224 if (Total.getProcessTime())
225 printVal(getProcessTime(), Total.getProcessTime(), OS);
226 printVal(Elapsed, Total.Elapsed, OS);
227
228 OS << " ";
229
230 if (Total.MemUsed) {
231 OS.width(9);
232 OS << MemUsed << " ";
233 }
234 if (Total.PeakMem) {
235 if (PeakMem) {
236 OS.width(9);
237 OS << PeakMem << " ";
238 } else
239 OS << " ";
240 }
241 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000242
243 Started = false; // Once printed, don't print again
244}
245
Chris Lattnerf205fec2003-05-09 20:05:44 +0000246// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
Brian Gaeked0fde302003-11-11 22:41:34 +0000247std::ostream *
Chris Lattnerb6d465f2003-12-14 21:27:33 +0000248llvm::GetLibSupportInfoOutputFile() {
Chris Lattner71336a92003-07-31 19:38:34 +0000249 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000250 if (LibSupportInfoOutputFilename.empty())
251 return &std::cerr;
252 if (LibSupportInfoOutputFilename == "-")
253 return &std::cout;
254
255 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
Chris Lattner903c2d12003-06-06 22:13:01 +0000256 std::ios::app);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000257 if (!Result->good()) {
258 std::cerr << "Error opening info-output-file '"
259 << LibSupportInfoOutputFilename << " for appending!\n";
260 delete Result;
261 return &std::cerr;
262 }
263 return Result;
264}
265
Chris Lattner6c38a792002-10-01 19:36:54 +0000266
267void TimerGroup::removeTimer() {
268 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
269 // Sort the timers in descending order by amount of time taken...
270 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
271 std::greater<Timer>());
272
273 // Figure out how many spaces to indent TimerGroup name...
274 unsigned Padding = (80-Name.length())/2;
275 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
276
Chris Lattnerf205fec2003-05-09 20:05:44 +0000277 std::ostream *OutStream = GetLibSupportInfoOutputFile();
278
Chris Lattner6c38a792002-10-01 19:36:54 +0000279 ++NumTimers;
280 { // Scope to contain Total timer... don't allow total timer to drop us to
281 // zero timers...
282 Timer Total("TOTAL");
283
284 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
285 Total.sum(TimersToPrint[i]);
286
287 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000288 *OutStream << "===" << std::string(73, '-') << "===\n"
289 << std::string(Padding, ' ') << Name << "\n"
290 << "===" << std::string(73, '-')
291 << "===\n Total Execution Time: ";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000292
Chris Lattnerf205fec2003-05-09 20:05:44 +0000293 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
294 *OutStream << " seconds (";
295 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
296 *OutStream << " wall clock)\n\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000297
298 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000299 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000300 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000301 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000302 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000303 *OutStream << " --User+System--";
304 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000305 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000306 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000307 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000308 *OutStream << " -PeakMem-";
309 *OutStream << " --- Name ---\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000310
311 // Loop through all of the timing data, printing it out...
312 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000313 TimersToPrint[i].print(Total, *OutStream);
Chris Lattner6c38a792002-10-01 19:36:54 +0000314
Chris Lattnerf205fec2003-05-09 20:05:44 +0000315 Total.print(Total, *OutStream);
316 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000317 }
318 --NumTimers;
319
320 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000321
322 if (OutStream != &std::cerr && OutStream != &std::cout)
323 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000324 }
325
326 // Delete default timer group!
327 if (NumTimers == 0 && this == DefaultTimerGroup) {
328 delete DefaultTimerGroup;
329 DefaultTimerGroup = 0;
330 }
331}
Brian Gaeked0fde302003-11-11 22:41:34 +0000332