blob: c2ce90c85880db28360bc382d3b5548aa1247bd0 [file] [log] [blame]
Chris Lattner6dad11f2002-10-01 19:36:54 +00001//===-- Timer.cpp - Interval Timing Support -------------------------------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukman10468d82005-04-21 22:55:34 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner6dad11f2002-10-01 19:36:54 +00009//
10// Interval Timing implementation.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer7c16caa2004-09-01 22:55:40 +000014#include "llvm/Support/Timer.h"
Chris Lattner707431c2010-03-30 04:03:22 +000015#include "llvm/ADT/StringMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Support/CommandLine.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/Format.h"
19#include "llvm/Support/ManagedStatic.h"
20#include "llvm/Support/Mutex.h"
21#include "llvm/Support/Process.h"
22#include "llvm/Support/raw_ostream.h"
Chris Lattnerdd978ce2003-12-14 21:27:33 +000023using namespace llvm;
Chris Lattnerb0e59582003-05-09 20:05:44 +000024
Chris Lattner6cbd8d12010-03-30 05:01:08 +000025// CreateInfoOutputFile - Return a file stream to print our output on.
26namespace llvm { extern raw_ostream *CreateInfoOutputFile(); }
Brian Gaeke960707c2003-11-11 22:41:34 +000027
Chris Lattnerc4bbc712003-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 Lattner700b8732006-12-06 17:46:33 +000030// problem is that a Statistic object gets destroyed, which ends up calling
Chris Lattnerc4bbc712003-07-31 19:38:34 +000031// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
32// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
Reid Spencer87ad6662004-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 Lattner8111c592006-10-04 21:52:35 +000036static ManagedStatic<std::string> LibSupportInfoOutputFilename;
Chris Lattnerc4bbc712003-07-31 19:38:34 +000037static std::string &getLibSupportInfoOutputFilename() {
Reid Spencer87ad6662004-12-14 03:55:21 +000038 return *LibSupportInfoOutputFilename;
Chris Lattnerc4bbc712003-07-31 19:38:34 +000039}
Chris Lattner6dad11f2002-10-01 19:36:54 +000040
Owen Andersone9b1beb2009-06-23 20:52:29 +000041static ManagedStatic<sys::SmartMutex<true> > TimerLock;
42
Chris Lattner2f752042003-01-30 23:08:50 +000043namespace {
Dan Gohmanc107d002008-04-23 23:15:23 +000044 static cl::opt<bool>
Chris Lattner2f752042003-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 Lattnerb0e59582003-05-09 20:05:44 +000048
Dan Gohmanc107d002008-04-23 23:15:23 +000049 static cl::opt<std::string, true>
Chris Lattnerf1afe32352003-08-01 22:15:15 +000050 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerb0e59582003-05-09 20:05:44 +000051 cl::desc("File to append -stats and -timer output to"),
Chris Lattnerc4bbc712003-07-31 19:38:34 +000052 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Chris Lattner2f752042003-01-30 23:08:50 +000053}
54
Chris Lattner6cbd8d12010-03-30 05:01:08 +000055// CreateInfoOutputFile - Return a file stream to print our output on.
56raw_ostream *llvm::CreateInfoOutputFile() {
Chris Lattner39e56a02010-03-30 05:34:02 +000057 const std::string &OutputFilename = getLibSupportInfoOutputFilename();
58 if (OutputFilename.empty())
Chris Lattner6cbd8d12010-03-30 05:01:08 +000059 return new raw_fd_ostream(2, false); // stderr.
Chris Lattner39e56a02010-03-30 05:34:02 +000060 if (OutputFilename == "-")
Chris Lattner6cbd8d12010-03-30 05:01:08 +000061 return new raw_fd_ostream(1, false); // stdout.
Chris Lattnerdcd7f922010-03-29 21:34:06 +000062
Dan Gohman744c96d2010-05-19 01:21:34 +000063 // Append mode is used because the info output file is opened and closed
64 // each time -stats or -time-passes wants to print output to it. To
65 // compensate for this, the test-suite Makefiles have code to delete the
66 // info output file before running commands which write to it.
Chris Lattnerdcd7f922010-03-29 21:34:06 +000067 std::string Error;
Rafael Espindola90c7f1c2014-02-24 18:20:12 +000068 raw_ostream *Result = new raw_fd_ostream(
69 OutputFilename.c_str(), Error, sys::fs::F_Append | sys::fs::F_Text);
Chris Lattnerdcd7f922010-03-29 21:34:06 +000070 if (Error.empty())
71 return Result;
72
73 errs() << "Error opening info-output-file '"
Chris Lattner39e56a02010-03-30 05:34:02 +000074 << OutputFilename << " for appending!\n";
Chris Lattnerdcd7f922010-03-29 21:34:06 +000075 delete Result;
Chris Lattner6cbd8d12010-03-30 05:01:08 +000076 return new raw_fd_ostream(2, false); // stderr.
Chris Lattnerdcd7f922010-03-29 21:34:06 +000077}
78
79
Craig Topperc10719f2014-04-07 04:17:22 +000080static TimerGroup *DefaultTimerGroup = nullptr;
Chris Lattner6dad11f2002-10-01 19:36:54 +000081static TimerGroup *getDefaultTimerGroup() {
Chris Lattnerfafa57a2010-03-29 20:35:01 +000082 TimerGroup *tmp = DefaultTimerGroup;
Benjamin Kramer17388a62014-03-03 18:02:34 +000083 sys::MemoryFence();
Chris Lattnerfafa57a2010-03-29 20:35:01 +000084 if (tmp) return tmp;
85
86 llvm_acquire_global_lock();
87 tmp = DefaultTimerGroup;
Owen Anderson4ed41c82009-06-23 17:33:37 +000088 if (!tmp) {
Chris Lattnerfafa57a2010-03-29 20:35:01 +000089 tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
Benjamin Kramer17388a62014-03-03 18:02:34 +000090 sys::MemoryFence();
Chris Lattnerfafa57a2010-03-29 20:35:01 +000091 DefaultTimerGroup = tmp;
Owen Anderson4ed41c82009-06-23 17:33:37 +000092 }
Chris Lattnerfafa57a2010-03-29 20:35:01 +000093 llvm_release_global_lock();
Mikhail Glushenkov358607d2009-11-07 06:33:12 +000094
Owen Anderson4ed41c82009-06-23 17:33:37 +000095 return tmp;
Chris Lattner6dad11f2002-10-01 19:36:54 +000096}
97
Chris Lattnerfafa57a2010-03-29 20:35:01 +000098//===----------------------------------------------------------------------===//
99// Timer Implementation
100//===----------------------------------------------------------------------===//
101
Chris Lattner39e56a02010-03-30 05:34:02 +0000102void Timer::init(StringRef N) {
Chris Lattner707431c2010-03-30 04:03:22 +0000103 assert(TG == 0 && "Timer already initialized");
Chris Lattner39e56a02010-03-30 05:34:02 +0000104 Name.assign(N.begin(), N.end());
Chris Lattner707431c2010-03-30 04:03:22 +0000105 Started = false;
106 TG = getDefaultTimerGroup();
Chris Lattner9a608d22010-03-30 04:40:01 +0000107 TG->addTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000108}
109
Chris Lattner39e56a02010-03-30 05:34:02 +0000110void Timer::init(StringRef N, TimerGroup &tg) {
Chris Lattner707431c2010-03-30 04:03:22 +0000111 assert(TG == 0 && "Timer already initialized");
Chris Lattner39e56a02010-03-30 05:34:02 +0000112 Name.assign(N.begin(), N.end());
Chris Lattner707431c2010-03-30 04:03:22 +0000113 Started = false;
114 TG = &tg;
Chris Lattner9a608d22010-03-30 04:40:01 +0000115 TG->addTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000116}
117
Chris Lattner6dad11f2002-10-01 19:36:54 +0000118Timer::~Timer() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000119 if (!TG) return; // Never initialized, or already cleared.
Chris Lattner9a608d22010-03-30 04:40:01 +0000120 TG->removeTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000121}
122
Jeff Cohen1a26d152005-01-08 20:15:57 +0000123static inline size_t getMemUsage() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000124 if (!TrackSpace) return 0;
125 return sys::Process::GetMallocUsage();
Reid Spencerad7bdf72004-12-27 08:03:04 +0000126}
127
Chris Lattner707431c2010-03-30 04:03:22 +0000128TimeRecord TimeRecord::getCurrentTime(bool Start) {
Chris Lattner60683452004-06-07 19:34:51 +0000129 TimeRecord Result;
Chris Lattner4cd65712010-03-30 05:20:02 +0000130 sys::TimeValue now(0,0), user(0,0), sys(0,0);
131
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000132 if (Start) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000133 Result.MemUsed = getMemUsage();
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000134 sys::Process::GetTimeUsage(now, user, sys);
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000135 } else {
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000136 sys::Process::GetTimeUsage(now, user, sys);
Chris Lattner4cd65712010-03-30 05:20:02 +0000137 Result.MemUsed = getMemUsage();
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000138 }
Reid Spencer27088812004-12-20 00:59:04 +0000139
Chris Lattner707431c2010-03-30 04:03:22 +0000140 Result.WallTime = now.seconds() + now.microseconds() / 1000000.0;
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000141 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
Chris Lattner707431c2010-03-30 04:03:22 +0000142 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
Chris Lattner6dad11f2002-10-01 19:36:54 +0000143 return Result;
144}
145
Chris Lattner8111c592006-10-04 21:52:35 +0000146static ManagedStatic<std::vector<Timer*> > ActiveTimers;
Chris Lattnerf96a2182002-11-18 21:47:09 +0000147
Chris Lattner6dad11f2002-10-01 19:36:54 +0000148void Timer::startTimer() {
149 Started = true;
Dan Gohman5ceb8b62008-06-24 22:07:07 +0000150 ActiveTimers->push_back(this);
Chris Lattner707431c2010-03-30 04:03:22 +0000151 Time -= TimeRecord::getCurrentTime(true);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000152}
153
154void Timer::stopTimer() {
Chris Lattner707431c2010-03-30 04:03:22 +0000155 Time += TimeRecord::getCurrentTime(false);
Chris Lattnerf96a2182002-11-18 21:47:09 +0000156
Chris Lattner8111c592006-10-04 21:52:35 +0000157 if (ActiveTimers->back() == this) {
158 ActiveTimers->pop_back();
Chris Lattnerf96a2182002-11-18 21:47:09 +0000159 } else {
160 std::vector<Timer*>::iterator I =
Chris Lattner8111c592006-10-04 21:52:35 +0000161 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
162 assert(I != ActiveTimers->end() && "stop but no startTimer?");
163 ActiveTimers->erase(I);
Chris Lattnerf96a2182002-11-18 21:47:09 +0000164 }
Chris Lattner6dad11f2002-10-01 19:36:54 +0000165}
166
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000167static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner5092a6d2010-03-29 20:40:19 +0000168 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000169 OS << " ----- ";
Benjamin Kramercc863b22011-10-16 16:30:34 +0000170 else
171 OS << format(" %7.4f (%5.1f%%)", Val, Val*100/Total);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000172}
173
Chris Lattner707431c2010-03-30 04:03:22 +0000174void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
175 if (Total.getUserTime())
176 printVal(getUserTime(), Total.getUserTime(), OS);
177 if (Total.getSystemTime())
178 printVal(getSystemTime(), Total.getSystemTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000179 if (Total.getProcessTime())
180 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattner707431c2010-03-30 04:03:22 +0000181 printVal(getWallTime(), Total.getWallTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000182
183 OS << " ";
184
Chris Lattner707431c2010-03-30 04:03:22 +0000185 if (Total.getMemUsed())
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000186 OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000187}
188
189
Chris Lattner8bfda652003-10-06 15:02:31 +0000190//===----------------------------------------------------------------------===//
191// NamedRegionTimer Implementation
192//===----------------------------------------------------------------------===//
193
Dan Gohmanb29cda92010-04-15 17:08:50 +0000194namespace {
195
Chris Lattner707431c2010-03-30 04:03:22 +0000196typedef StringMap<Timer> Name2TimerMap;
Chris Lattner4cd65712010-03-30 05:20:02 +0000197
198class Name2PairMap {
199 StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
200public:
201 ~Name2PairMap() {
202 for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
203 I = Map.begin(), E = Map.end(); I != E; ++I)
204 delete I->second.first;
205 }
206
Chris Lattner39e56a02010-03-30 05:34:02 +0000207 Timer &get(StringRef Name, StringRef GroupName) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000208 sys::SmartScopedLock<true> L(*TimerLock);
209
210 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
211
212 if (!GroupEntry.first)
213 GroupEntry.first = new TimerGroup(GroupName);
214
215 Timer &T = GroupEntry.second[Name];
216 if (!T.isInitialized())
217 T.init(Name, *GroupEntry.first);
218 return T;
219 }
220};
Dan Gohmanadec96f2008-07-14 18:19:29 +0000221
Dan Gohmanb29cda92010-04-15 17:08:50 +0000222}
223
Chris Lattner707431c2010-03-30 04:03:22 +0000224static ManagedStatic<Name2TimerMap> NamedTimers;
225static ManagedStatic<Name2PairMap> NamedGroupedTimers;
Chris Lattner8bfda652003-10-06 15:02:31 +0000226
Chris Lattner39e56a02010-03-30 05:34:02 +0000227static Timer &getNamedRegionTimer(StringRef Name) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000228 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner707431c2010-03-30 04:03:22 +0000229
230 Timer &T = (*NamedTimers)[Name];
231 if (!T.isInitialized())
232 T.init(Name);
233 return T;
Chris Lattner8bfda652003-10-06 15:02:31 +0000234}
235
Dan Gohman6e681a52010-06-18 15:56:31 +0000236NamedRegionTimer::NamedRegionTimer(StringRef Name,
237 bool Enabled)
Craig Topperc10719f2014-04-07 04:17:22 +0000238 : TimeRegion(!Enabled ? nullptr : &getNamedRegionTimer(Name)) {}
Chris Lattner8bfda652003-10-06 15:02:31 +0000239
Dan Gohman6e681a52010-06-18 15:56:31 +0000240NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef GroupName,
241 bool Enabled)
Craig Topperc10719f2014-04-07 04:17:22 +0000242 : TimeRegion(!Enabled ? nullptr : &NamedGroupedTimers->get(Name, GroupName)){}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000243
Chris Lattner6dad11f2002-10-01 19:36:54 +0000244//===----------------------------------------------------------------------===//
245// TimerGroup Implementation
246//===----------------------------------------------------------------------===//
247
Chris Lattner90fe73d2010-03-30 05:27:58 +0000248/// TimerGroupList - This is the global list of TimerGroups, maintained by the
249/// TimerGroup ctor/dtor and is protected by the TimerLock lock.
Craig Topperc10719f2014-04-07 04:17:22 +0000250static TimerGroup *TimerGroupList = nullptr;
Chris Lattner90fe73d2010-03-30 05:27:58 +0000251
Chris Lattner39e56a02010-03-30 05:34:02 +0000252TimerGroup::TimerGroup(StringRef name)
Craig Topperc10719f2014-04-07 04:17:22 +0000253 : Name(name.begin(), name.end()), FirstTimer(nullptr) {
Chris Lattner90fe73d2010-03-30 05:27:58 +0000254
255 // Add the group to TimerGroupList.
256 sys::SmartScopedLock<true> L(*TimerLock);
257 if (TimerGroupList)
258 TimerGroupList->Prev = &Next;
259 Next = TimerGroupList;
260 Prev = &TimerGroupList;
261 TimerGroupList = this;
262}
263
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000264TimerGroup::~TimerGroup() {
265 // If the timer group is destroyed before the timers it owns, accumulate and
266 // print the timing data.
Craig Topperc10719f2014-04-07 04:17:22 +0000267 while (FirstTimer != nullptr)
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000268 removeTimer(*FirstTimer);
Chris Lattner90fe73d2010-03-30 05:27:58 +0000269
270 // Remove the group from the TimerGroupList.
271 sys::SmartScopedLock<true> L(*TimerLock);
272 *Prev = Next;
273 if (Next)
274 Next->Prev = Prev;
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000275}
276
277
Chris Lattner9a608d22010-03-30 04:40:01 +0000278void TimerGroup::removeTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000279 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000280
Chris Lattner9a608d22010-03-30 04:40:01 +0000281 // If the timer was started, move its data to TimersToPrint.
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000282 if (T.Started)
Chris Lattner9a608d22010-03-30 04:40:01 +0000283 TimersToPrint.push_back(std::make_pair(T.Time, T.Name));
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000284
Craig Topperc10719f2014-04-07 04:17:22 +0000285 T.TG = nullptr;
Chris Lattner9a608d22010-03-30 04:40:01 +0000286
287 // Unlink the timer from our list.
288 *T.Prev = T.Next;
289 if (T.Next)
290 T.Next->Prev = T.Prev;
291
292 // Print the report when all timers in this group are destroyed if some of
293 // them were started.
Craig Topperc10719f2014-04-07 04:17:22 +0000294 if (FirstTimer != nullptr || TimersToPrint.empty())
Chris Lattner9a608d22010-03-30 04:40:01 +0000295 return;
296
Chris Lattner6cbd8d12010-03-30 05:01:08 +0000297 raw_ostream *OutStream = CreateInfoOutputFile();
Chris Lattner9a608d22010-03-30 04:40:01 +0000298 PrintQueuedTimers(*OutStream);
Chris Lattner6cbd8d12010-03-30 05:01:08 +0000299 delete OutStream; // Close the file.
Chris Lattner6dad11f2002-10-01 19:36:54 +0000300}
Brian Gaeke960707c2003-11-11 22:41:34 +0000301
Chris Lattner9a608d22010-03-30 04:40:01 +0000302void TimerGroup::addTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000303 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner9a608d22010-03-30 04:40:01 +0000304
305 // Add the timer to our list.
306 if (FirstTimer)
307 FirstTimer->Prev = &T.Next;
308 T.Next = FirstTimer;
309 T.Prev = &FirstTimer;
310 FirstTimer = &T;
Owen Andersone9b1beb2009-06-23 20:52:29 +0000311}
312
Chris Lattner9a608d22010-03-30 04:40:01 +0000313void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
314 // Sort the timers in descending order by amount of time taken.
Benjamin Kramera7d0ccf2010-08-07 13:27:41 +0000315 std::sort(TimersToPrint.begin(), TimersToPrint.end());
316
Chris Lattner9a608d22010-03-30 04:40:01 +0000317 TimeRecord Total;
318 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
319 Total += TimersToPrint[i].first;
320
321 // Print out timing header.
322 OS << "===" << std::string(73, '-') << "===\n";
323 // Figure out how many spaces to indent TimerGroup name.
324 unsigned Padding = (80-Name.length())/2;
325 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
326 OS.indent(Padding) << Name << '\n';
327 OS << "===" << std::string(73, '-') << "===\n";
328
329 // If this is not an collection of ungrouped times, print the total time.
330 // Ungrouped timers don't really make sense to add up. We still print the
331 // TOTAL line to make the percentages make sense.
Benjamin Kramercc863b22011-10-16 16:30:34 +0000332 if (this != DefaultTimerGroup)
333 OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
334 Total.getProcessTime(), Total.getWallTime());
Chris Lattner9a608d22010-03-30 04:40:01 +0000335 OS << '\n';
336
337 if (Total.getUserTime())
338 OS << " ---User Time---";
339 if (Total.getSystemTime())
340 OS << " --System Time--";
341 if (Total.getProcessTime())
342 OS << " --User+System--";
343 OS << " ---Wall Time---";
344 if (Total.getMemUsed())
345 OS << " ---Mem---";
346 OS << " --- Name ---\n";
347
348 // Loop through all of the timing data, printing it out.
349 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) {
350 const std::pair<TimeRecord, std::string> &Entry = TimersToPrint[e-i-1];
351 Entry.first.print(Total, OS);
352 OS << Entry.second << '\n';
353 }
354
355 Total.print(Total, OS);
356 OS << "Total\n\n";
357 OS.flush();
358
359 TimersToPrint.clear();
Owen Andersone9b1beb2009-06-23 20:52:29 +0000360}
361
Chris Lattner4cd65712010-03-30 05:20:02 +0000362/// print - Print any started timers in this group and zero them.
363void TimerGroup::print(raw_ostream &OS) {
364 sys::SmartScopedLock<true> L(*TimerLock);
365
366 // See if any of our timers were started, if so add them to TimersToPrint and
367 // reset them.
368 for (Timer *T = FirstTimer; T; T = T->Next) {
369 if (!T->Started) continue;
370 TimersToPrint.push_back(std::make_pair(T->Time, T->Name));
371
372 // Clear out the time.
373 T->Started = 0;
374 T->Time = TimeRecord();
375 }
376
377 // If any timers were started, print the group.
378 if (!TimersToPrint.empty())
379 PrintQueuedTimers(OS);
380}
Chris Lattner90fe73d2010-03-30 05:27:58 +0000381
382/// printAll - This static method prints all timers and clears them all out.
383void TimerGroup::printAll(raw_ostream &OS) {
384 sys::SmartScopedLock<true> L(*TimerLock);
385
386 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
387 TG->print(OS);
388}