blob: 4bdfac298cc0c74eaab2c96f4575bc450a17bef3 [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
David Greeneda80ff62010-01-05 01:28:58 +000014#include "llvm/Support/Debug.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000015#include "llvm/Support/Timer.h"
16#include "llvm/Support/CommandLine.h"
Chris Lattner90aa8392006-10-04 21:52:35 +000017#include "llvm/Support/ManagedStatic.h"
Chris Lattnerd9ea85a2009-08-23 08:43:55 +000018#include "llvm/Support/raw_ostream.h"
19#include "llvm/Support/Format.h"
Reid Spencerdf52c9a2004-12-20 00:59:04 +000020#include "llvm/System/Process.h"
Reid Spencer0255abb2004-12-20 03:59:23 +000021#include <algorithm>
Reid Spencer0255abb2004-12-20 03:59:23 +000022#include <functional>
Chris Lattnerb4db5f32004-06-07 19:34:51 +000023#include <map>
Chris Lattnerb6d465f2003-12-14 21:27:33 +000024using namespace llvm;
Chris Lattnerf205fec2003-05-09 20:05:44 +000025
Chris Lattnerb4db5f32004-06-07 19:34:51 +000026// GetLibSupportInfoOutputFile - Return a file stream to print our output on.
Chris Lattnerd9ea85a2009-08-23 08:43:55 +000027namespace llvm { extern raw_ostream *GetLibSupportInfoOutputFile(); }
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner71336a92003-07-31 19:38:34 +000029// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
30// of constructor/destructor ordering being unspecified by C++. Basically the
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000031// problem is that a Statistic object gets destroyed, which ends up calling
Chris Lattner71336a92003-07-31 19:38:34 +000032// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
33// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
Reid Spencerf6e5a252004-12-14 03:55:21 +000034// would get destroyed before the Statistic, causing havoc to ensue. We "fix"
35// this by creating the string the first time it is needed and never destroying
36// it.
Chris Lattner90aa8392006-10-04 21:52:35 +000037static ManagedStatic<std::string> LibSupportInfoOutputFilename;
Chris Lattner71336a92003-07-31 19:38:34 +000038static std::string &getLibSupportInfoOutputFilename() {
Reid Spencerf6e5a252004-12-14 03:55:21 +000039 return *LibSupportInfoOutputFilename;
Chris Lattner71336a92003-07-31 19:38:34 +000040}
Chris Lattner6c38a792002-10-01 19:36:54 +000041
Owen Anderson46d9a642009-06-23 20:52:29 +000042static ManagedStatic<sys::SmartMutex<true> > TimerLock;
43
Chris Lattner3f398492003-01-30 23:08:50 +000044namespace {
Dan Gohman3c02aca2008-04-23 23:15:23 +000045 static cl::opt<bool>
Chris Lattner3f398492003-01-30 23:08:50 +000046 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
47 "tracking (this may be slow)"),
48 cl::Hidden);
Chris Lattnerf205fec2003-05-09 20:05:44 +000049
Dan Gohman3c02aca2008-04-23 23:15:23 +000050 static cl::opt<std::string, true>
Chris Lattner96a54db2003-08-01 22:15:15 +000051 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerf205fec2003-05-09 20:05:44 +000052 cl::desc("File to append -stats and -timer output to"),
Chris Lattner71336a92003-07-31 19:38:34 +000053 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Chris Lattner3f398492003-01-30 23:08:50 +000054}
55
Owen Anderson200aa6d2009-06-23 16:36:10 +000056static TimerGroup *DefaultTimerGroup = 0;
Chris Lattner6c38a792002-10-01 19:36:54 +000057static TimerGroup *getDefaultTimerGroup() {
Owen Anderson3b8d1352009-06-23 17:33:37 +000058 TimerGroup* tmp = DefaultTimerGroup;
59 sys::MemoryFence();
60 if (!tmp) {
61 llvm_acquire_global_lock();
62 tmp = DefaultTimerGroup;
63 if (!tmp) {
64 tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
65 sys::MemoryFence();
66 DefaultTimerGroup = tmp;
67 }
68 llvm_release_global_lock();
69 }
Mikhail Glushenkovc11e84d2009-11-07 06:33:12 +000070
Owen Anderson3b8d1352009-06-23 17:33:37 +000071 return tmp;
Chris Lattner6c38a792002-10-01 19:36:54 +000072}
73
74Timer::Timer(const std::string &N)
Chris Lattner8f0d8242002-11-18 21:47:09 +000075 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000076 Started(false), TG(getDefaultTimerGroup()) {
77 TG->addTimer();
78}
79
80Timer::Timer(const std::string &N, TimerGroup &tg)
Chris Lattner8f0d8242002-11-18 21:47:09 +000081 : Elapsed(0), UserTime(0), SystemTime(0), MemUsed(0), PeakMem(0), Name(N),
Chris Lattner6c38a792002-10-01 19:36:54 +000082 Started(false), TG(&tg) {
83 TG->addTimer();
84}
85
86Timer::Timer(const Timer &T) {
87 TG = T.TG;
88 if (TG) TG->addTimer();
89 operator=(T);
90}
91
92
93// Copy ctor, initialize with no TG member.
94Timer::Timer(bool, const Timer &T) {
95 TG = T.TG; // Avoid assertion in operator=
96 operator=(T); // Copy contents
97 TG = 0;
98}
99
100
101Timer::~Timer() {
102 if (TG) {
103 if (Started) {
104 Started = false;
105 TG->addTimerToPrint(*this);
106 }
107 TG->removeTimer();
108 }
109}
110
Jeff Cohene269a1a2005-01-08 20:15:57 +0000111static inline size_t getMemUsage() {
Reid Spenceraeb47b82004-12-27 08:03:04 +0000112 if (TrackSpace)
Jeff Cohene269a1a2005-01-08 20:15:57 +0000113 return sys::Process::GetMallocUsage();
Reid Spenceraeb47b82004-12-27 08:03:04 +0000114 return 0;
115}
116
Chris Lattner6c38a792002-10-01 19:36:54 +0000117struct TimeRecord {
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000118 double Elapsed, UserTime, SystemTime;
119 ssize_t MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000120};
121
Chris Lattner8f0d8242002-11-18 21:47:09 +0000122static TimeRecord getTimeRecord(bool Start) {
Chris Lattnerb4db5f32004-06-07 19:34:51 +0000123 TimeRecord Result;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000124
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000125 sys::TimeValue now(0,0);
126 sys::TimeValue user(0,0);
127 sys::TimeValue sys(0,0);
Chris Lattner6c38a792002-10-01 19:36:54 +0000128
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000129 ssize_t MemUsed = 0;
Reid Spencer7d055632004-12-20 21:44:27 +0000130 if (Start) {
Reid Spenceraeb47b82004-12-27 08:03:04 +0000131 MemUsed = getMemUsage();
Chris Lattnerfed1b272005-03-22 03:20:38 +0000132 sys::Process::GetTimeUsage(now,user,sys);
Reid Spencer7d055632004-12-20 21:44:27 +0000133 } else {
Reid Spencer7d055632004-12-20 21:44:27 +0000134 sys::Process::GetTimeUsage(now,user,sys);
Chris Lattnerfed1b272005-03-22 03:20:38 +0000135 MemUsed = getMemUsage();
Reid Spencer7d055632004-12-20 21:44:27 +0000136 }
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000137
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000138 Result.Elapsed = now.seconds() + now.microseconds() / 1000000.0;
139 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
140 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
Reid Spencer7d055632004-12-20 21:44:27 +0000141 Result.MemUsed = MemUsed;
Reid Spencerdf52c9a2004-12-20 00:59:04 +0000142
Chris Lattner6c38a792002-10-01 19:36:54 +0000143 return Result;
144}
145
Chris Lattner90aa8392006-10-04 21:52:35 +0000146static ManagedStatic<std::vector<Timer*> > ActiveTimers;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000147
Chris Lattner6c38a792002-10-01 19:36:54 +0000148void Timer::startTimer() {
Owen Anderson252e5742009-11-17 07:06:10 +0000149 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner6c38a792002-10-01 19:36:54 +0000150 Started = true;
Dan Gohman153d28a2008-06-24 22:07:07 +0000151 ActiveTimers->push_back(this);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000152 TimeRecord TR = getTimeRecord(true);
Chris Lattner6c38a792002-10-01 19:36:54 +0000153 Elapsed -= TR.Elapsed;
154 UserTime -= TR.UserTime;
155 SystemTime -= TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000156 MemUsed -= TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000157 PeakMemBase = TR.MemUsed;
Chris Lattner6c38a792002-10-01 19:36:54 +0000158}
159
160void Timer::stopTimer() {
Owen Anderson252e5742009-11-17 07:06:10 +0000161 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000162 TimeRecord TR = getTimeRecord(false);
Chris Lattner6c38a792002-10-01 19:36:54 +0000163 Elapsed += TR.Elapsed;
164 UserTime += TR.UserTime;
165 SystemTime += TR.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000166 MemUsed += TR.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000167
Chris Lattner90aa8392006-10-04 21:52:35 +0000168 if (ActiveTimers->back() == this) {
169 ActiveTimers->pop_back();
Chris Lattner8f0d8242002-11-18 21:47:09 +0000170 } else {
171 std::vector<Timer*>::iterator I =
Chris Lattner90aa8392006-10-04 21:52:35 +0000172 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
173 assert(I != ActiveTimers->end() && "stop but no startTimer?");
174 ActiveTimers->erase(I);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000175 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000176}
177
178void Timer::sum(const Timer &T) {
179 Elapsed += T.Elapsed;
180 UserTime += T.UserTime;
181 SystemTime += T.SystemTime;
Chris Lattner18eba912002-11-04 19:19:36 +0000182 MemUsed += T.MemUsed;
Chris Lattner8f0d8242002-11-18 21:47:09 +0000183 PeakMem += T.PeakMem;
Chris Lattner6c38a792002-10-01 19:36:54 +0000184}
185
Chris Lattner8f0d8242002-11-18 21:47:09 +0000186/// addPeakMemoryMeasurement - This method should be called whenever memory
187/// usage needs to be checked. It adds a peak memory measurement to the
188/// currently active timers, which will be printed when the timer group prints
189///
190void Timer::addPeakMemoryMeasurement() {
Owen Anderson252e5742009-11-17 07:06:10 +0000191 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000192 size_t MemUsed = getMemUsage();
Chris Lattner8f0d8242002-11-18 21:47:09 +0000193
Chris Lattner90aa8392006-10-04 21:52:35 +0000194 for (std::vector<Timer*>::iterator I = ActiveTimers->begin(),
Owen Anderson252e5742009-11-17 07:06:10 +0000195 E = ActiveTimers->end(); I != E; ++I)
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000196 (*I)->PeakMem = std::max((*I)->PeakMem, MemUsed-(*I)->PeakMemBase);
Chris Lattner8f0d8242002-11-18 21:47:09 +0000197}
198
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000199//===----------------------------------------------------------------------===//
200// NamedRegionTimer Implementation
201//===----------------------------------------------------------------------===//
202
Dan Gohman5e843682008-07-14 18:19:29 +0000203namespace {
204
205typedef std::map<std::string, Timer> Name2Timer;
206typedef std::map<std::string, std::pair<TimerGroup, Name2Timer> > Name2Pair;
207
208}
209
210static ManagedStatic<Name2Timer> NamedTimers;
211
212static ManagedStatic<Name2Pair> NamedGroupedTimers;
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000213
Chris Lattner90aa8392006-10-04 21:52:35 +0000214static Timer &getNamedRegionTimer(const std::string &Name) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000215 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman5e843682008-07-14 18:19:29 +0000216 Name2Timer::iterator I = NamedTimers->find(Name);
Dan Gohmanc418bf32008-07-11 20:58:19 +0000217 if (I != NamedTimers->end())
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000218 return I->second;
219
Chris Lattner90aa8392006-10-04 21:52:35 +0000220 return NamedTimers->insert(I, std::make_pair(Name, Timer(Name)))->second;
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000221}
222
Dan Gohman5e843682008-07-14 18:19:29 +0000223static Timer &getNamedRegionTimer(const std::string &Name,
224 const std::string &GroupName) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000225 sys::SmartScopedLock<true> L(*TimerLock);
Dan Gohman5e843682008-07-14 18:19:29 +0000226
227 Name2Pair::iterator I = NamedGroupedTimers->find(GroupName);
228 if (I == NamedGroupedTimers->end()) {
229 TimerGroup TG(GroupName);
230 std::pair<TimerGroup, Name2Timer> Pair(TG, Name2Timer());
231 I = NamedGroupedTimers->insert(I, std::make_pair(GroupName, Pair));
232 }
233
234 Name2Timer::iterator J = I->second.second.find(Name);
235 if (J == I->second.second.end())
236 J = I->second.second.insert(J,
237 std::make_pair(Name,
238 Timer(Name,
239 I->second.first)));
240
241 return J->second;
242}
243
Chris Lattnerd5a310e2003-10-06 15:02:31 +0000244NamedRegionTimer::NamedRegionTimer(const std::string &Name)
245 : TimeRegion(getNamedRegionTimer(Name)) {}
246
Dan Gohman5e843682008-07-14 18:19:29 +0000247NamedRegionTimer::NamedRegionTimer(const std::string &Name,
248 const std::string &GroupName)
249 : TimeRegion(getNamedRegionTimer(Name, GroupName)) {}
Chris Lattner8f0d8242002-11-18 21:47:09 +0000250
Chris Lattner6c38a792002-10-01 19:36:54 +0000251//===----------------------------------------------------------------------===//
252// TimerGroup Implementation
253//===----------------------------------------------------------------------===//
254
Chris Lattner6c38a792002-10-01 19:36:54 +0000255
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000256static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattnerf205fec2003-05-09 20:05:44 +0000257 if (Total < 1e-7) // Avoid dividing by zero...
258 OS << " ----- ";
259 else {
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000260 OS << " " << format("%7.4f", Val) << " (";
261 OS << format("%5.1f", Val*100/Total) << "%)";
Chris Lattner8f0d8242002-11-18 21:47:09 +0000262 }
Chris Lattnerf205fec2003-05-09 20:05:44 +0000263}
264
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000265void Timer::print(const Timer &Total, raw_ostream &OS) {
Owen Anderson252e5742009-11-17 07:06:10 +0000266 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000267 if (Total.UserTime)
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000268 printVal(UserTime, Total.UserTime, OS);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000269 if (Total.SystemTime)
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000270 printVal(SystemTime, Total.SystemTime, OS);
Chris Lattnerf205fec2003-05-09 20:05:44 +0000271 if (Total.getProcessTime())
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000272 printVal(getProcessTime(), Total.getProcessTime(), OS);
273 printVal(Elapsed, Total.Elapsed, OS);
Misha Brukmanf976c852005-04-21 22:55:34 +0000274
Chris Lattnerf205fec2003-05-09 20:05:44 +0000275 OS << " ";
276
277 if (Total.MemUsed) {
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000278 OS << format("%9lld", (long long)MemUsed) << " ";
Chris Lattnerf205fec2003-05-09 20:05:44 +0000279 }
280 if (Total.PeakMem) {
281 if (PeakMem) {
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000282 OS << format("%9lld", (long long)PeakMem) << " ";
Chris Lattnerf205fec2003-05-09 20:05:44 +0000283 } else
284 OS << " ";
285 }
286 OS << Name << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000287
288 Started = false; // Once printed, don't print again
289}
290
Chris Lattnerf205fec2003-05-09 20:05:44 +0000291// GetLibSupportInfoOutputFile - Return a file stream to print our output on...
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000292raw_ostream *
Chris Lattnerb6d465f2003-12-14 21:27:33 +0000293llvm::GetLibSupportInfoOutputFile() {
Chris Lattner71336a92003-07-31 19:38:34 +0000294 std::string &LibSupportInfoOutputFilename = getLibSupportInfoOutputFilename();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000295 if (LibSupportInfoOutputFilename.empty())
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000296 return &errs();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000297 if (LibSupportInfoOutputFilename == "-")
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000298 return &outs();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000299
Mikhail Glushenkovc11e84d2009-11-07 06:33:12 +0000300
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000301 std::string Error;
302 raw_ostream *Result = new raw_fd_ostream(LibSupportInfoOutputFilename.c_str(),
303 Error, raw_fd_ostream::F_Append);
304 if (Error.empty())
305 return Result;
Mikhail Glushenkovc11e84d2009-11-07 06:33:12 +0000306
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000307 errs() << "Error opening info-output-file '"
Bill Wendlingbcd24982006-12-07 20:28:15 +0000308 << LibSupportInfoOutputFilename << " for appending!\n";
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000309 delete Result;
310 return &errs();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000311}
312
Chris Lattner6c38a792002-10-01 19:36:54 +0000313
314void TimerGroup::removeTimer() {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000315 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner6c38a792002-10-01 19:36:54 +0000316 if (--NumTimers == 0 && !TimersToPrint.empty()) { // Print timing report...
317 // Sort the timers in descending order by amount of time taken...
318 std::sort(TimersToPrint.begin(), TimersToPrint.end(),
319 std::greater<Timer>());
320
321 // Figure out how many spaces to indent TimerGroup name...
322 unsigned Padding = (80-Name.length())/2;
323 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
324
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000325 raw_ostream *OutStream = GetLibSupportInfoOutputFile();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000326
Chris Lattner6c38a792002-10-01 19:36:54 +0000327 ++NumTimers;
328 { // Scope to contain Total timer... don't allow total timer to drop us to
329 // zero timers...
330 Timer Total("TOTAL");
Misha Brukmanf976c852005-04-21 22:55:34 +0000331
Chris Lattner6c38a792002-10-01 19:36:54 +0000332 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
333 Total.sum(TimersToPrint[i]);
Misha Brukmanf976c852005-04-21 22:55:34 +0000334
Chris Lattner6c38a792002-10-01 19:36:54 +0000335 // Print out timing header...
Chris Lattnerf205fec2003-05-09 20:05:44 +0000336 *OutStream << "===" << std::string(73, '-') << "===\n"
337 << std::string(Padding, ' ') << Name << "\n"
338 << "===" << std::string(73, '-')
Chris Lattner3ac96052005-02-09 18:41:32 +0000339 << "===\n";
Chris Lattner8166b7c2003-02-13 16:25:28 +0000340
Chris Lattner3ac96052005-02-09 18:41:32 +0000341 // If this is not an collection of ungrouped times, print the total time.
342 // Ungrouped timers don't really make sense to add up. We still print the
343 // TOTAL line to make the percentages make sense.
Owen Anderson200aa6d2009-06-23 16:36:10 +0000344 if (this != DefaultTimerGroup) {
Chris Lattner3ac96052005-02-09 18:41:32 +0000345 *OutStream << " Total Execution Time: ";
346
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000347 *OutStream << format("%5.4f", Total.getProcessTime()) << " seconds (";
348 *OutStream << format("%5.4f", Total.getWallTime()) << " wall clock)\n";
Chris Lattner3ac96052005-02-09 18:41:32 +0000349 }
350 *OutStream << "\n";
Chris Lattner6c38a792002-10-01 19:36:54 +0000351
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000352 if (Total.UserTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000353 *OutStream << " ---User Time---";
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000354 if (Total.SystemTime)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000355 *OutStream << " --System Time--";
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000356 if (Total.getProcessTime())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000357 *OutStream << " --User+System--";
358 *OutStream << " ---Wall Time---";
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000359 if (Total.getMemUsed())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000360 *OutStream << " ---Mem---";
Owen Anderson6f2c64d2009-06-23 20:17:22 +0000361 if (Total.getPeakMem())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000362 *OutStream << " -PeakMem-";
363 *OutStream << " --- Name ---\n";
Misha Brukmanf976c852005-04-21 22:55:34 +0000364
Chris Lattner6c38a792002-10-01 19:36:54 +0000365 // Loop through all of the timing data, printing it out...
366 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
Chris Lattnerf205fec2003-05-09 20:05:44 +0000367 TimersToPrint[i].print(Total, *OutStream);
Misha Brukmanf976c852005-04-21 22:55:34 +0000368
Chris Lattnerf205fec2003-05-09 20:05:44 +0000369 Total.print(Total, *OutStream);
Chris Lattnerd9ea85a2009-08-23 08:43:55 +0000370 *OutStream << '\n';
371 OutStream->flush();
Chris Lattner6c38a792002-10-01 19:36:54 +0000372 }
373 --NumTimers;
374
375 TimersToPrint.clear();
Chris Lattnerf205fec2003-05-09 20:05:44 +0000376
David Greeneda80ff62010-01-05 01:28:58 +0000377 if (OutStream != &errs() && OutStream != &outs() && OutStream != &dbgs())
Chris Lattnerf205fec2003-05-09 20:05:44 +0000378 delete OutStream; // Close the file...
Chris Lattner6c38a792002-10-01 19:36:54 +0000379 }
Chris Lattner6c38a792002-10-01 19:36:54 +0000380}
Brian Gaeked0fde302003-11-11 22:41:34 +0000381
Owen Anderson46d9a642009-06-23 20:52:29 +0000382void TimerGroup::addTimer() {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000383 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson46d9a642009-06-23 20:52:29 +0000384 ++NumTimers;
385}
386
387void TimerGroup::addTimerToPrint(const Timer &T) {
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000388 sys::SmartScopedLock<true> L(*TimerLock);
Owen Anderson46d9a642009-06-23 20:52:29 +0000389 TimersToPrint.push_back(Timer(true, T));
390}
391