blob: 47d696804a77b65f2c79b3cc22f16737f9210a17 [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 Spencer7d055632004-12-20 21:44:27 +0000108 long MemUsed = 0;
109 if (Start) {
110 sys::Process::GetTimeUsage(now,user,sys);
111 MemUsed = sys::Process::GetMallocUsage();
112 } else {
113 MemUsed = sys::Process::GetMallocUsage();
114 sys::Process::GetTimeUsage(now,user,sys);
115 }
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000116
Reid Spencer7d055632004-12-20 21:44:27 +0000117 Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0;
118 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
119 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
120 Result.MemUsed = MemUsed;
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000121
Chris Lattner6c38a792002-10-01 19:36:54 +0000122 return Result;
123}
124
Chris Lattner8f0d8242002-11-18 21:47:09 +0000125static std::vector<Timer*> ActiveTimers;
126
Chris Lattner6c38a792002-10-01 19:36:54 +0000127void Timer::startTimer() {
128 Started = true;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000129 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000130 Elapsed -= TR.Elapsed;
131 UserTime -= TR.UserTime;
132 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000133 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000134 PeakMemBase = TR.MemUsed;
135 ActiveTimers.push_back(this);
Chris Lattner6c38a792002-10-01 19:36:54 +0000136}
137
138void Timer::stopTimer() {
Chris Lattner8f0d8242002-11-18 21:47:09 +0000139 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000140 Elapsed += TR.Elapsed;
141 UserTime += TR.UserTime;
142 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000143 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000144
145 if (ActiveTimers.back() == this) {
146 ActiveTimers.pop_back();
147 } else {
148 std::vector<Timer*>::iterator I =
149 std::find(ActiveTimers.begin(), ActiveTimers.end(), this);
150 assert(I != ActiveTimers.end() && "stop but no startTimer?");
151 ActiveTimers.erase(I);
152 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000153}
154
155void Timer::sum(const Timer &T) {
156 Elapsed += T.Elapsed;
157 UserTime += T.UserTime;
158 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000159 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000160 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000161}
162
Chris Lattner8f0d8242002-11-18 21:47:09 +0000163/// addPeakMemoryMeasurement - This method should be called whenever memory
164/// usage needs to be checked. It adds a peak memory measurement to the
165/// currently active timers, which will be printed when the timer group prints
166///
167void Timer::addPeakMemoryMeasurement() {
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000168 long MemUsed = sys::Process::GetMallocUsage();
Chris Lattner8f0d8242002-11-18 21:47:09 +0000169
170 for (std::vector<Timer*>::iterator I = ActiveTimers.begin(),
171 E = ActiveTimers.end(); I != E; ++I)
172 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
173}
174
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000175//===----------------------------------------------------------------------===//
176// NamedRegionTimer Implementation
177//===----------------------------------------------------------------------===//
178
179static Timer &getNamedRegionTimer(const std::string &Name) {
180 static std::map<std::string, Timer> NamedTimers;
181
182 std::map<std::string, Timer>::iterator I = NamedTimers.lower_bound(Name);
183 if (I != NamedTimers.end() && I->first == Name)
184 return I->second;
185
186 return NamedTimers.insert(I, std::make_pair(Name, Timer(Name)))->second;
187}
188
189NamedRegionTimer::NamedRegionTimer(const std::string &Name)
190 : TimeRegion(getNamedRegionTimer(Name)) {}
191
Chris Lattner8f0d8242002-11-18 21:47:09 +0000192
Chris Lattner6c38a792002-10-01 19:36:54 +0000193//===----------------------------------------------------------------------===//
194// TimerGroup Implementation
195//===----------------------------------------------------------------------===//
196
Chris Lattnerf205fec2003-05-09 20:05:44 +0000197// printAlignedFP - Simulate the printf "%A.Bf" format, where A is the
198// TotalWidth size, and B is the AfterDec size.
199//
200static void printAlignedFP(double Val, unsigned AfterDec, unsigned TotalWidth,
201 std::ostream &OS) {
202 assert(TotalWidth >= AfterDec+1 && "Bad FP Format!");
203 OS.width(TotalWidth-AfterDec-1);
204 char OldFill = OS.fill();
205 OS.fill(' ');
206 OS << (int)Val; // Integer part;
207 OS << ".";
208 OS.width(AfterDec);
209 OS.fill('0');
210 unsigned ResultFieldSize = 1;
211 while (AfterDec--) ResultFieldSize *= 10;
212 OS << (int)(Val*ResultFieldSize) % ResultFieldSize;
213 OS.fill(OldFill);
Chris Lattner6c38a792002-10-01 19:36:54 +0000214}
215
Chris Lattnerf205fec2003-05-09 20:05:44 +0000216static void printVal(double Val, double Total, std::ostream &OS) {
217 if (Total < 1e-7) // Avoid dividing by zero...
218 OS << " ----- ";
219 else {
220 OS << " ";
221 printAlignedFP(Val, 4, 7, OS);
222 OS << " (";
223 printAlignedFP(Val*100/Total, 1, 5, OS);
224 OS << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000225 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000226}
227
228void Timer::print(const Timer &Total, std::ostream &OS) {
229 if (Total.UserTime)
230 printVal(UserTime, Total.UserTime, OS);
231 if (Total.SystemTime)
232 printVal(SystemTime, Total.SystemTime, OS);
233 if (Total.getProcessTime())
234 printVal(getProcessTime(), Total.getProcessTime(), OS);
235 printVal(Elapsed, Total.Elapsed, OS);
236
237 OS << " ";
238
239 if (Total.MemUsed) {
240 OS.width(9);
241 OS << MemUsed << " ";
242 }
243 if (Total.PeakMem) {
244 if (PeakMem) {
245 OS.width(9);
246 OS << PeakMem << " ";
247 } else
248 OS << " ";
249 }
250 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000251
252 Started = false; // Once printed, don't print again
253}
254
Chris Lattnerf205fec2003-05-09 20:05:44 +0000255// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
Brian Gaeked0fde302003-11-11 22:41:34 +0000256std::ostream *
Chris Lattnerb6d465f2003-12-14 21:27:33 +0000257llvm::GetLibSupportInfoOutputFile() {
Chris Lattner71336a92003-07-31 19:38:34 +0000258 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000259 if (LibSupportInfoOutputFilename.empty())
260 return &std::cerr;
261 if (LibSupportInfoOutputFilename == "-")
262 return &std::cout;
263
264 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
Chris Lattner903c2d12003-06-06 22:13:01 +0000265 std::ios::app);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000266 if (!Result->good()) {
267 std::cerr << "Error opening info-output-file '"
268 << LibSupportInfoOutputFilename << " for appending!\n";
269 delete Result;
270 return &std::cerr;
271 }
272 return Result;
273}
274
Chris Lattner6c38a792002-10-01 19:36:54 +0000275
276void TimerGroup::removeTimer() {
277 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
278 // Sort the timers in descending order by amount of time taken...
279 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
280 std::greater<Timer>());
281
282 // Figure out how many spaces to indent TimerGroup name...
283 unsigned Padding = (80-Name.length())/2;
284 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
285
Chris Lattnerf205fec2003-05-09 20:05:44 +0000286 std::ostream *OutStream = GetLibSupportInfoOutputFile();
287
Chris Lattner6c38a792002-10-01 19:36:54 +0000288 ++NumTimers;
289 { // Scope to contain Total timer... don't allow total timer to drop us to
290 // zero timers...
291 Timer Total("TOTAL");
292
293 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
294 Total.sum(TimersToPrint[i]);
295
296 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000297 *OutStream << "===" << std::string(73, '-') << "===\n"
298 << std::string(Padding, ' ') << Name << "\n"
299 << "===" << std::string(73, '-')
300 << "===\n Total Execution Time: ";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000301
Chris Lattnerf205fec2003-05-09 20:05:44 +0000302 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
303 *OutStream << " seconds (";
304 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
305 *OutStream << " wall clock)\n\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000306
307 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000308 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000309 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000310 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000311 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000312 *OutStream << " --User+System--";
313 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000314 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000315 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000316 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000317 *OutStream << " -PeakMem-";
318 *OutStream << " --- Name ---\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000319
320 // Loop through all of the timing data, printing it out...
321 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000322 TimersToPrint[i].print(Total, *OutStream);
Chris Lattner6c38a792002-10-01 19:36:54 +0000323
Chris Lattnerf205fec2003-05-09 20:05:44 +0000324 Total.print(Total, *OutStream);
325 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000326 }
327 --NumTimers;
328
329 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000330
331 if (OutStream != &std::cerr && OutStream != &std::cout)
332 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000333 }
334
335 // Delete default timer group!
336 if (NumTimers == 0 && this == DefaultTimerGroup) {
337 delete DefaultTimerGroup;
338 DefaultTimerGroup = 0;
339 }
340}
Brian Gaeked0fde302003-11-11 22:41:34 +0000341