blob: dd58d1f68b4dee2f7e280aa4fbcb6537af637e14 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// 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"
Chris Lattner90aa8392006-10-04 21:52:35 +000016#include "llvm/Support/ManagedStatic.h"
Chris Lattnerd9ea85a2009-08-23 08:43:55 +000017#include "llvm/Support/raw_ostream.h"
18#include "llvm/Support/Format.h"
Reid Spencerdf52c9a2004-12-20 00:59:04 +000019#include "llvm/System/Process.h"
Reid Spencer0255abb2004-12-20 03:59:23 +000020#include <algorithm>
Reid Spencer0255abb2004-12-20 03:59:23 +000021#include <functional>
Chris Lattnerb4db5f32004-06-07 19:34:51 +000022#include <map>
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 Lattnerd9ea85a2009-08-23 08:43:55 +000026namespace llvm { extern raw_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
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000030// problem is that a Statistic object gets destroyed, which ends up calling
Chris Lattner71336a92003-07-31 19:38:34 +000031// '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 Lattner90aa8392006-10-04 21:52:35 +000036static ManagedStatic<std::string> LibSupportInfoOutputFilename;
Chris Lattner71336a92003-07-31 19:38:34 +000037static std::string &getLibSupportInfoOutputFilename() {
Reid Spencerf6e5a252004-12-14 03:55:21 +000038 return *LibSupportInfoOutputFilename;
Chris Lattner71336a92003-07-31 19:38:34 +000039}
Chris Lattner6c38a792002-10-01 19:36:54 +000040
Owen Anderson46d9a642009-06-23 20:52:29 +000041static ManagedStatic<sys::SmartMutex<true> > TimerLock;
42
Chris Lattner3f398492003-01-30 23:08:50 +000043namespace {
Dan Gohman3c02aca2008-04-23 23:15:23 +000044 static cl::opt<bool>
Chris Lattner3f398492003-01-30 23:08:50 +000045 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
46 "tracking (this may be slow)"),
47 cl::Hidden);
Chris Lattnerf205fec2003-05-09 20:05:44 +000048
Dan Gohman3c02aca2008-04-23 23:15:23 +000049 static cl::opt<std::string, true>
Chris Lattner96a54db2003-08-01 22:15:15 +000050 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerf205fec2003-05-09 20:05:44 +000051 cl::desc("File to append -stats and -timer output to"),
Chris Lattner71336a92003-07-31 19:38:34 +000052 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Chris Lattner3f398492003-01-30 23:08:50 +000053}
54
Owen Anderson200aa6d2009-06-23 16:36:10 +000055static TimerGroup *DefaultTimerGroup = 0;
Chris Lattner6c38a792002-10-01 19:36:54 +000056static TimerGroup *getDefaultTimerGroup() {
Owen Anderson3b8d1352009-06-23 17:33:37 +000057 TimerGroup* tmp = DefaultTimerGroup;
58 sys::MemoryFence();
59 if (!tmp) {
60 llvm_acquire_global_lock();
61 tmp = DefaultTimerGroup;
62 if (!tmp) {
63 tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
64 sys::MemoryFence();
65 DefaultTimerGroup = tmp;
66 }
67 llvm_release_global_lock();
68 }
69
70 return tmp;
Chris Lattner6c38a792002-10-01 19:36:54 +000071}
72
73Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000074 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000075 Started(false), TG(getDefaultTimerGroup()) {
76 TG->addTimer();
77}
78
79Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000080 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000081 Started(false), TG(&tg) {
82 TG->addTimer();
83}
84
85Timer::Timer(const Timer &T) {
86 TG = T.TG;
87 if (TG) TG->addTimer();
88 operator=(T);
89}
90
91
92// Copy ctor, initialize with no TG member.
93Timer::Timer(bool, const Timer &T) {
94 TG = T.TG; // Avoid assertion in operator=
95 operator=(T); // Copy contents
96 TG = 0;
97}
98
99
100Timer::~Timer() {
101 if (TG) {
102 if (Started) {
103 Started = false;
104 TG->addTimerToPrint(*this);
105 }
106 TG->removeTimer();
107 }
108}
109
Jeff Cohene269a1a2005-01-08 20:15:57 +0000110static inline size_t getMemUsage() {
Reid Spenceraeb47b82004-12-27 08:03:04 +0000111 if (TrackSpace)
Jeff Cohene269a1a2005-01-08 20:15:57 +0000112 return sys::Process::GetMallocUsage();
Reid Spenceraeb47b82004-12-27 08:03:04 +0000113 return 0;
114}
115
Chris Lattner6c38a792002-10-01 19:36:54 +0000116struct TimeRecord {
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000117 double Elapsed, UserTime, SystemTime;
118 ssize_t MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000119};
120
Chris Lattner8f0d8242002-11-18 21:47:09 +0000121static TimeRecord getTimeRecord(bool Start) {
Chris Lattnerb4db5f32004-06-07 19:34:51 +0000122 TimeRecord Result;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000123
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000124 sys::TimeValue now(0,0);
125 sys::TimeValue user(0,0);
126 sys::TimeValue sys(0,0);
Chris Lattner6c38a792002-10-01 19:36:54 +0000127
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000128 ssize_t MemUsed = 0;
Reid Spencer7d055632004-12-20 21:44:27 +0000129 if (Start) {
Reid Spenceraeb47b82004-12-27 08:03:04 +0000130 MemUsed = getMemUsage();
Chris Lattnerfed1b272005-03-22 03:20:38 +0000131 sys::Process::GetTimeUsage(now,user,sys);
Reid Spencer7d055632004-12-20 21:44:27 +0000132 } else {
Reid Spencer7d055632004-12-20 21:44:27 +0000133 sys::Process::GetTimeUsage(now,user,sys);
Chris Lattnerfed1b272005-03-22 03:20:38 +0000134 MemUsed = getMemUsage();
Reid Spencer7d055632004-12-20 21:44:27 +0000135 }
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000136
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000137 Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0;
138 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
139 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
Reid Spencer7d055632004-12-20 21:44:27 +0000140 Result.MemUsed = MemUsed;
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000141
Chris Lattner6c38a792002-10-01 19:36:54 +0000142 return Result;
143}
144
Chris Lattner90aa8392006-10-04 21:52:35 +0000145static ManagedStatic<std::vector<Timer*> > ActiveTimers;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000146
Chris Lattner6c38a792002-10-01 19:36:54 +0000147void Timer::startTimer() {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000148 sys::SmartScopedLock<true> L(Lock);
Chris Lattner6c38a792002-10-01 19:36:54 +0000149 Started = true;
Dan Gohman153d28a2008-06-24 22:07:07 +0000150 ActiveTimers->push_back(this);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000151 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000152 Elapsed -= TR.Elapsed;
153 UserTime -= TR.UserTime;
154 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000155 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000156 PeakMemBase = TR.MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000157}
158
159void Timer::stopTimer() {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000160 sys::SmartScopedLock<true> L(Lock);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000161 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000162 Elapsed += TR.Elapsed;
163 UserTime += TR.UserTime;
164 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000165 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000166
Chris Lattner90aa8392006-10-04 21:52:35 +0000167 if (ActiveTimers->back() == this) {
168 ActiveTimers->pop_back();
Chris Lattner8f0d8242002-11-18 21:47:09 +0000169 } else {
170 std::vector<Timer*>::iterator I =
Chris Lattner90aa8392006-10-04 21:52:35 +0000171 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
172 assert(I != ActiveTimers->end() && "stop but no startTimer?");
173 ActiveTimers->erase(I);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000174 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000175}
176
177void Timer::sum(const Timer &T) {
Owen Anderson46d9a642009-06-23 20:52:29 +0000178 if (&T < this) {
179 T.Lock.acquire();
180 Lock.acquire();
181 } else {
182 Lock.acquire();
183 T.Lock.acquire();
184 }
185
Chris Lattner6c38a792002-10-01 19:36:54 +0000186 Elapsed += T.Elapsed;
187 UserTime += T.UserTime;
188 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000189 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000190 PeakMem += T.PeakMem;
Owen Anderson46d9a642009-06-23 20:52:29 +0000191
192 if (&T < this) {
193 T.Lock.release();
194 Lock.release();
195 } else {
196 Lock.release();
197 T.Lock.release();
198 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000199}
200
Chris Lattner8f0d8242002-11-18 21:47:09 +0000201/// addPeakMemoryMeasurement - This method should be called whenever memory
202/// usage needs to be checked. It adds a peak memory measurement to the
203/// currently active timers, which will be printed when the timer group prints
204///
205void Timer::addPeakMemoryMeasurement() {
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000206 size_t MemUsed = getMemUsage();
Chris Lattner8f0d8242002-11-18 21:47:09 +0000207
Chris Lattner90aa8392006-10-04 21:52:35 +0000208 for (std::vector<Timer*>::iterator I = ActiveTimers->begin(),
Owen Anderson46d9a642009-06-23 20:52:29 +0000209 E = ActiveTimers->end(); I != E; ++I) {
210 (*I)->Lock.acquire();
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000211 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
Owen Anderson46d9a642009-06-23 20:52:29 +0000212 (*I)->Lock.release();
213 }
Chris Lattner8f0d8242002-11-18 21:47:09 +0000214}
215
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000216//===----------------------------------------------------------------------===//
217// NamedRegionTimer Implementation
218//===----------------------------------------------------------------------===//
219
Dan Gohman5e843682008-07-14 18:19:29 +0000220namespace {
221
222typedef std::map<std::string, Timer> Name2Timer;
223typedef std::map<std::string, std::pair<TimerGroup, Name2Timer> > Name2Pair;
224
225}
226
227static ManagedStatic<Name2Timer> NamedTimers;
228
229static ManagedStatic<Name2Pair> NamedGroupedTimers;
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000230
Chris Lattner90aa8392006-10-04 21:52:35 +0000231static Timer &getNamedRegionTimer(const std::string &Name) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000232 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman5e843682008-07-14 18:19:29 +0000233 Name2Timer::iterator I = NamedTimers->find(Name);
Dan Gohmanc418bf32008-07-11 20:58:19 +0000234 if (I != NamedTimers->end())
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000235 return I->second;
236
Chris Lattner90aa8392006-10-04 21:52:35 +0000237 return NamedTimers->insert(I, std::make_pair(Name, Timer(Name)))->second;
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000238}
239
Dan Gohman5e843682008-07-14 18:19:29 +0000240static Timer &getNamedRegionTimer(const std::string &Name,
241 const std::string &GroupName) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000242 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman5e843682008-07-14 18:19:29 +0000243
244 Name2Pair::iterator I = NamedGroupedTimers->find(GroupName);
245 if (I == NamedGroupedTimers->end()) {
246 TimerGroup TG(GroupName);
247 std::pair<TimerGroup, Name2Timer> Pair(TG, Name2Timer());
248 I = NamedGroupedTimers->insert(I, std::make_pair(GroupName, Pair));
249 }
250
251 Name2Timer::iterator J = I->second.second.find(Name);
252 if (J == I->second.second.end())
253 J = I->second.second.insert(J,
254 std::make_pair(Name,
255 Timer(Name,
256 I->second.first)));
257
258 return J->second;
259}
260
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000261NamedRegionTimer::NamedRegionTimer(const std::string &Name)
262 : TimeRegion(getNamedRegionTimer(Name)) {}
263
Dan Gohman5e843682008-07-14 18:19:29 +0000264NamedRegionTimer::NamedRegionTimer(const std::string &Name,
265 const std::string &GroupName)
266 : TimeRegion(getNamedRegionTimer(Name, GroupName)) {}
Chris Lattner8f0d8242002-11-18 21:47:09 +0000267
Chris Lattner6c38a792002-10-01 19:36:54 +0000268//===----------------------------------------------------------------------===//
269// TimerGroup Implementation
270//===----------------------------------------------------------------------===//
271
Chris Lattner6c38a792002-10-01 19:36:54 +0000272
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000273static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattnerf205fec2003-05-09 20:05:44 +0000274 if (Total < 1e-7) // Avoid dividing by zero...
275 OS << " ----- ";
276 else {
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000277 OS << " " << format("%7.4f", Val) << " (";
278 OS << format("%5.1f", Val*100/Total) << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000279 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000280}
281
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000282void Timer::print(const Timer &Total, raw_ostream &OS) {
Owen Anderson46d9a642009-06-23 20:52:29 +0000283 if (&Total < this) {
284 Total.Lock.acquire();
285 Lock.acquire();
286 } else {
287 Lock.acquire();
288 Total.Lock.acquire();
289 }
290
Chris Lattnerf205fec2003-05-09 20:05:44 +0000291 if (Total.UserTime)
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000292 printVal(UserTime, Total.UserTime, OS);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000293 if (Total.SystemTime)
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000294 printVal(SystemTime, Total.SystemTime, OS);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000295 if (Total.getProcessTime())
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000296 printVal(getProcessTime(), Total.getProcessTime(), OS);
297 printVal(Elapsed, Total.Elapsed, OS);
Misha Brukmanf976c852005-04-21 22:55:34 +0000298
Chris Lattnerf205fec2003-05-09 20:05:44 +0000299 OS << " ";
300
301 if (Total.MemUsed) {
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000302 OS << format("%9lld", (long long)MemUsed) << " ";
Chris Lattnerf205fec2003-05-09 20:05:44 +0000303 }
304 if (Total.PeakMem) {
305 if (PeakMem) {
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000306 OS << format("%9lld", (long long)PeakMem) << " ";
Chris Lattnerf205fec2003-05-09 20:05:44 +0000307 } else
308 OS << " ";
309 }
310 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000311
312 Started = false; // Once printed, don't print again
Owen Anderson46d9a642009-06-23 20:52:29 +0000313
314 if (&Total < this) {
315 Total.Lock.release();
316 Lock.release();
317 } else {
318 Lock.release();
319 Total.Lock.release();
320 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000321}
322
Chris Lattnerf205fec2003-05-09 20:05:44 +0000323// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000324raw_ostream *
Chris Lattnerb6d465f2003-12-14 21:27:33 +0000325llvm::GetLibSupportInfoOutputFile() {
Chris Lattner71336a92003-07-31 19:38:34 +0000326 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000327 if (LibSupportInfoOutputFilename.empty())
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000328 return &errs();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000329 if (LibSupportInfoOutputFilename == "-")
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000330 return &outs();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000331
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000332
333 std::string Error;
334 raw_ostream *Result = new raw_fd_ostream(LibSupportInfoOutputFilename.c_str(),
335 Error, raw_fd_ostream::F_Append);
336 if (Error.empty())
337 return Result;
338
339 errs() << "Error opening info-output-file '"
Bill Wendlingbcd24982006-12-07 20:28:15 +0000340 << LibSupportInfoOutputFilename << " for appending!\n";
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000341 delete Result;
342 return &errs();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000343}
344
Chris Lattner6c38a792002-10-01 19:36:54 +0000345
346void TimerGroup::removeTimer() {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000347 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner6c38a792002-10-01 19:36:54 +0000348 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
349 // Sort the timers in descending order by amount of time taken...
350 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
351 std::greater<Timer>());
352
353 // Figure out how many spaces to indent TimerGroup name...
354 unsigned Padding = (80-Name.length())/2;
355 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
356
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000357 raw_ostream *OutStream = GetLibSupportInfoOutputFile();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000358
Chris Lattner6c38a792002-10-01 19:36:54 +0000359 ++NumTimers;
360 { // Scope to contain Total timer... don't allow total timer to drop us to
361 // zero timers...
362 Timer Total("TOTAL");
Misha Brukmanf976c852005-04-21 22:55:34 +0000363
Chris Lattner6c38a792002-10-01 19:36:54 +0000364 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
365 Total.sum(TimersToPrint[i]);
Misha Brukmanf976c852005-04-21 22:55:34 +0000366
Chris Lattner6c38a792002-10-01 19:36:54 +0000367 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000368 *OutStream << "===" << std::string(73, '-') << "===\n"
369 << std::string(Padding, ' ') << Name << "\n"
370 << "===" << std::string(73, '-')
Chris Lattner3ac96052005-02-09 18:41:32 +0000371 << "===\n";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000372
Chris Lattner3ac96052005-02-09 18:41:32 +0000373 // If this is not an collection of ungrouped times, print the total time.
374 // Ungrouped timers don't really make sense to add up. We still print the
375 // TOTAL line to make the percentages make sense.
Owen Anderson200aa6d2009-06-23 16:36:10 +0000376 if (this != DefaultTimerGroup) {
Chris Lattner3ac96052005-02-09 18:41:32 +0000377 *OutStream << " Total Execution Time: ";
378
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000379 *OutStream << format("%5.4f", Total.getProcessTime()) << " seconds (";
380 *OutStream << format("%5.4f", Total.getWallTime()) << " wall clock)\n";
Chris Lattner3ac96052005-02-09 18:41:32 +0000381 }
382 *OutStream << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000383
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000384 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000385 *OutStream << " ---User Time---";
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000386 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000387 *OutStream << " --System Time--";
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000388 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000389 *OutStream << " --User+System--";
390 *OutStream << " ---Wall Time---";
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000391 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000392 *OutStream << " ---Mem---";
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000393 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000394 *OutStream << " -PeakMem-";
395 *OutStream << " --- Name ---\n";
Misha Brukmanf976c852005-04-21 22:55:34 +0000396
Chris Lattner6c38a792002-10-01 19:36:54 +0000397 // Loop through all of the timing data, printing it out...
398 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000399 TimersToPrint[i].print(Total, *OutStream);
Misha Brukmanf976c852005-04-21 22:55:34 +0000400
Chris Lattnerf205fec2003-05-09 20:05:44 +0000401 Total.print(Total, *OutStream);
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000402 *OutStream << '\n';
403 OutStream->flush();
Chris Lattner6c38a792002-10-01 19:36:54 +0000404 }
405 --NumTimers;
406
407 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000408
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000409 if (OutStream != &errs() && OutStream != &outs())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000410 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000411 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000412}
Brian Gaeked0fde302003-11-11 22:41:34 +0000413
Owen Anderson46d9a642009-06-23 20:52:29 +0000414void TimerGroup::addTimer() {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000415 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson46d9a642009-06-23 20:52:29 +0000416 ++NumTimers;
417}
418
419void TimerGroup::addTimerToPrint(const Timer &T) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000420 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson46d9a642009-06-23 20:52:29 +0000421 TimersToPrint.push_back(Timer(true, T));
422}
423