blob: b0012ceb51819a0285aedf02c592864ea729470d [file] [log] [blame]
Chris Lattner6c38a792002-10-01 19:36:54 +00001//===-- Timer.cpp - Interval Timing Support -------------------------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
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>
Chris Lattnerb6d465f2003-12-14 21:27:33 +000022using namespace llvm;
Chris Lattnerf205fec2003-05-09 20:05:44 +000023
Chris Lattnerb4db5f32004-06-07 19:34:51 +000024// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattnerb6d465f2003-12-14 21:27:33 +000025namespace llvm { extern std::ostream *GetLibSupportInfoOutputFile(); }
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattner71336a92003-07-31 19:38:34 +000027// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
28// of constructor/destructor ordering being unspecified by C++. Basically the
29// problem is that a Statistic<> object gets destroyed, which ends up calling
30// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
31// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
Reid Spencerf6e5a252004-12-14 03:55:21 +000032// would get destroyed before the Statistic, causing havoc to ensue. We "fix"
33// this by creating the string the first time it is needed and never destroying
34// it.
Chris Lattner71336a92003-07-31 19:38:34 +000035static std::string &getLibSupportInfoOutputFilename() {
Reid Spencerf6e5a252004-12-14 03:55:21 +000036 static std::string *LibSupportInfoOutputFilename = new std::string();
37 return *LibSupportInfoOutputFilename;
Chris Lattner71336a92003-07-31 19:38:34 +000038}
Chris Lattner6c38a792002-10-01 19:36:54 +000039
Chris Lattner3f398492003-01-30 23:08:50 +000040namespace {
41 cl::opt<bool>
42 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
43 "tracking (this may be slow)"),
44 cl::Hidden);
Chris Lattnerf205fec2003-05-09 20:05:44 +000045
46 cl::opt<std::string, true>
Chris Lattner96a54db2003-08-01 22:15:15 +000047 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerf205fec2003-05-09 20:05:44 +000048 cl::desc("File to append -stats and -timer output to"),
Chris Lattner71336a92003-07-31 19:38:34 +000049 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Chris Lattner3f398492003-01-30 23:08:50 +000050}
51
Chris Lattner6c38a792002-10-01 19:36:54 +000052static TimerGroup *DefaultTimerGroup = 0;
53static TimerGroup *getDefaultTimerGroup() {
54 if (DefaultTimerGroup) return DefaultTimerGroup;
55 return DefaultTimerGroup = new TimerGroup("Miscellaneous Ungrouped Timers");
56}
57
58Timer::Timer(const std::string &N)
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(getDefaultTimerGroup()) {
61 TG->addTimer();
62}
63
64Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000065 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000066 Started(false), TG(&tg) {
67 TG->addTimer();
68}
69
70Timer::Timer(const Timer &T) {
71 TG = T.TG;
72 if (TG) TG->addTimer();
73 operator=(T);
74}
75
76
77// Copy ctor, initialize with no TG member.
78Timer::Timer(bool, const Timer &T) {
79 TG = T.TG; // Avoid assertion in operator=
80 operator=(T); // Copy contents
81 TG = 0;
82}
83
84
85Timer::~Timer() {
86 if (TG) {
87 if (Started) {
88 Started = false;
89 TG->addTimerToPrint(*this);
90 }
91 TG->removeTimer();
92 }
93}
94
Jeff Cohene269a1a2005-01-08 20:15:57 +000095static inline size_t getMemUsage() {
Reid Spenceraeb47b82004-12-27 08:03:04 +000096 if (TrackSpace)
Jeff Cohene269a1a2005-01-08 20:15:57 +000097 return sys::Process::GetMallocUsage();
Reid Spenceraeb47b82004-12-27 08:03:04 +000098 return 0;
99}
100
Chris Lattner6c38a792002-10-01 19:36:54 +0000101struct TimeRecord {
102 double Elapsed, UserTime, SystemTime;
Chris Lattner6cfbd622005-01-29 05:21:16 +0000103 ssize_t MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000104};
105
Chris Lattner8f0d8242002-11-18 21:47:09 +0000106static TimeRecord getTimeRecord(bool Start) {
Chris Lattnerb4db5f32004-06-07 19:34:51 +0000107 TimeRecord Result;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000108
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000109 sys::TimeValue now(0,0);
110 sys::TimeValue user(0,0);
111 sys::TimeValue sys(0,0);
Chris Lattner6c38a792002-10-01 19:36:54 +0000112
Chris Lattner6cfbd622005-01-29 05:21:16 +0000113 ssize_t MemUsed = 0;
Reid Spencer7d055632004-12-20 21:44:27 +0000114 if (Start) {
Reid Spenceraeb47b82004-12-27 08:03:04 +0000115 MemUsed = getMemUsage();
Chris Lattnerfed1b272005-03-22 03:20:38 +0000116 sys::Process::GetTimeUsage(now,user,sys);
Reid Spencer7d055632004-12-20 21:44:27 +0000117 } else {
Reid Spencer7d055632004-12-20 21:44:27 +0000118 sys::Process::GetTimeUsage(now,user,sys);
Chris Lattnerfed1b272005-03-22 03:20:38 +0000119 MemUsed = getMemUsage();
Reid Spencer7d055632004-12-20 21:44:27 +0000120 }
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000121
Reid Spencer7d055632004-12-20 21:44:27 +0000122 Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0;
123 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
124 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
125 Result.MemUsed = MemUsed;
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000126
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() {
Jeff Cohene269a1a2005-01-08 20:15:57 +0000173 size_t MemUsed = getMemUsage();
Chris Lattner8f0d8242002-11-18 21:47:09 +0000174
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);
Misha Brukmanf976c852005-04-21 22:55:34 +0000241
Chris Lattnerf205fec2003-05-09 20:05:44 +0000242 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...
Brian Gaeked0fde302003-11-11 22:41:34 +0000261std::ostream *
Chris Lattnerb6d465f2003-12-14 21:27:33 +0000262llvm::GetLibSupportInfoOutputFile() {
Chris Lattner71336a92003-07-31 19:38:34 +0000263 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000264 if (LibSupportInfoOutputFilename.empty())
265 return &std::cerr;
266 if (LibSupportInfoOutputFilename == "-")
267 return &std::cout;
268
269 std::ostream *Result = new std::ofstream(LibSupportInfoOutputFilename.c_str(),
Chris Lattner903c2d12003-06-06 22:13:01 +0000270 std::ios::app);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000271 if (!Result->good()) {
272 std::cerr << "Error opening info-output-file '"
273 << LibSupportInfoOutputFilename << " for appending!\n";
274 delete Result;
275 return &std::cerr;
276 }
277 return Result;
278}
279
Chris Lattner6c38a792002-10-01 19:36:54 +0000280
281void TimerGroup::removeTimer() {
282 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
283 // Sort the timers in descending order by amount of time taken...
284 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
285 std::greater<Timer>());
286
287 // Figure out how many spaces to indent TimerGroup name...
288 unsigned Padding = (80-Name.length())/2;
289 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
290
Chris Lattnerf205fec2003-05-09 20:05:44 +0000291 std::ostream *OutStream = GetLibSupportInfoOutputFile();
292
Chris Lattner6c38a792002-10-01 19:36:54 +0000293 ++NumTimers;
294 { // Scope to contain Total timer... don't allow total timer to drop us to
295 // zero timers...
296 Timer Total("TOTAL");
Misha Brukmanf976c852005-04-21 22:55:34 +0000297
Chris Lattner6c38a792002-10-01 19:36:54 +0000298 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
299 Total.sum(TimersToPrint[i]);
Misha Brukmanf976c852005-04-21 22:55:34 +0000300
Chris Lattner6c38a792002-10-01 19:36:54 +0000301 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000302 *OutStream << "===" << std::string(73, '-') << "===\n"
303 << std::string(Padding, ' ') << Name << "\n"
304 << "===" << std::string(73, '-')
Chris Lattner3ac96052005-02-09 18:41:32 +0000305 << "===\n";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000306
Chris Lattner3ac96052005-02-09 18:41:32 +0000307 // If this is not an collection of ungrouped times, print the total time.
308 // Ungrouped timers don't really make sense to add up. We still print the
309 // TOTAL line to make the percentages make sense.
310 if (this != DefaultTimerGroup) {
311 *OutStream << " Total Execution Time: ";
312
313 printAlignedFP(Total.getProcessTime(), 4, 5, *OutStream);
314 *OutStream << " seconds (";
315 printAlignedFP(Total.getWallTime(), 4, 5, *OutStream);
316 *OutStream << " wall clock)\n";
317 }
318 *OutStream << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000319
320 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000321 *OutStream << " ---User Time---";
Chris Lattner6c38a792002-10-01 19:36:54 +0000322 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000323 *OutStream << " --System Time--";
Chris Lattner6c38a792002-10-01 19:36:54 +0000324 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000325 *OutStream << " --User+System--";
326 *OutStream << " ---Wall Time---";
Chris Lattner18eba912002-11-04 19:19:36 +0000327 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000328 *OutStream << " ---Mem---";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000329 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000330 *OutStream << " -PeakMem-";
331 *OutStream << " --- Name ---\n";
Misha Brukmanf976c852005-04-21 22:55:34 +0000332
Chris Lattner6c38a792002-10-01 19:36:54 +0000333 // Loop through all of the timing data, printing it out...
334 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000335 TimersToPrint[i].print(Total, *OutStream);
Misha Brukmanf976c852005-04-21 22:55:34 +0000336
Chris Lattnerf205fec2003-05-09 20:05:44 +0000337 Total.print(Total, *OutStream);
338 *OutStream << std::endl; // Flush output
Chris Lattner6c38a792002-10-01 19:36:54 +0000339 }
340 --NumTimers;
341
342 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000343
344 if (OutStream != &std::cerr && OutStream != &std::cout)
345 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000346 }
347
348 // Delete default timer group!
349 if (NumTimers == 0 && this == DefaultTimerGroup) {
350 delete DefaultTimerGroup;
351 DefaultTimerGroup = 0;
352 }
353}
Brian Gaeked0fde302003-11-11 22:41:34 +0000354