blob: c3a385f471f9d12953d383648d3049c98e99479c [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"
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000015#include "llvm/ADT/Statistic.h"
Chris Lattner707431c2010-03-30 04:03:22 +000016#include "llvm/ADT/StringMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000018#include "llvm/Support/FileSystem.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/Support/Format.h"
20#include "llvm/Support/ManagedStatic.h"
Zachary Turnerccbf3d02014-06-16 22:49:41 +000021#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Support/Process.h"
23#include "llvm/Support/raw_ostream.h"
Chris Lattnerdd978ce2003-12-14 21:27:33 +000024using namespace llvm;
Chris Lattnerb0e59582003-05-09 20:05:44 +000025
Chris Lattnerc4bbc712003-07-31 19:38:34 +000026// getLibSupportInfoOutputFilename - This ugly hack is brought to you courtesy
27// of constructor/destructor ordering being unspecified by C++. Basically the
Chris Lattner700b8732006-12-06 17:46:33 +000028// problem is that a Statistic object gets destroyed, which ends up calling
Chris Lattnerc4bbc712003-07-31 19:38:34 +000029// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
30// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
Reid Spencer87ad6662004-12-14 03:55:21 +000031// would get destroyed before the Statistic, causing havoc to ensue. We "fix"
32// this by creating the string the first time it is needed and never destroying
33// it.
Chris Lattner8111c592006-10-04 21:52:35 +000034static ManagedStatic<std::string> LibSupportInfoOutputFilename;
Chris Lattnerc4bbc712003-07-31 19:38:34 +000035static std::string &getLibSupportInfoOutputFilename() {
Reid Spencer87ad6662004-12-14 03:55:21 +000036 return *LibSupportInfoOutputFilename;
Chris Lattnerc4bbc712003-07-31 19:38:34 +000037}
Chris Lattner6dad11f2002-10-01 19:36:54 +000038
Owen Andersone9b1beb2009-06-23 20:52:29 +000039static ManagedStatic<sys::SmartMutex<true> > TimerLock;
40
Chris Lattner2f752042003-01-30 23:08:50 +000041namespace {
Dan Gohmanc107d002008-04-23 23:15:23 +000042 static cl::opt<bool>
Chris Lattner2f752042003-01-30 23:08:50 +000043 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
44 "tracking (this may be slow)"),
45 cl::Hidden);
Chris Lattnerb0e59582003-05-09 20:05:44 +000046
Dan Gohmanc107d002008-04-23 23:15:23 +000047 static cl::opt<std::string, true>
Chris Lattnerf1afe32352003-08-01 22:15:15 +000048 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerb0e59582003-05-09 20:05:44 +000049 cl::desc("File to append -stats and -timer output to"),
Chris Lattnerc4bbc712003-07-31 19:38:34 +000050 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Alexander Kornienkof00654e2015-06-23 09:49:53 +000051}
Chris Lattner2f752042003-01-30 23:08:50 +000052
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000053// Return a file stream to print our output on.
54std::unique_ptr<raw_fd_ostream> llvm::CreateInfoOutputFile() {
Chris Lattner39e56a02010-03-30 05:34:02 +000055 const std::string &OutputFilename = getLibSupportInfoOutputFilename();
56 if (OutputFilename.empty())
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000057 return llvm::make_unique<raw_fd_ostream>(2, false); // stderr.
Chris Lattner39e56a02010-03-30 05:34:02 +000058 if (OutputFilename == "-")
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000059 return llvm::make_unique<raw_fd_ostream>(1, false); // stdout.
60
Dan Gohman744c96d2010-05-19 01:21:34 +000061 // Append mode is used because the info output file is opened and closed
62 // each time -stats or -time-passes wants to print output to it. To
63 // compensate for this, the test-suite Makefiles have code to delete the
64 // info output file before running commands which write to it.
Rafael Espindola3fd1e992014-08-25 18:16:47 +000065 std::error_code EC;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000066 auto Result = llvm::make_unique<raw_fd_ostream>(
67 OutputFilename, EC, sys::fs::F_Append | sys::fs::F_Text);
Rafael Espindola3fd1e992014-08-25 18:16:47 +000068 if (!EC)
Chris Lattnerdcd7f922010-03-29 21:34:06 +000069 return Result;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000070
Chris Lattnerdcd7f922010-03-29 21:34:06 +000071 errs() << "Error opening info-output-file '"
Chris Lattner39e56a02010-03-30 05:34:02 +000072 << OutputFilename << " for appending!\n";
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000073 return llvm::make_unique<raw_fd_ostream>(2, false); // stderr.
Chris Lattnerdcd7f922010-03-29 21:34:06 +000074}
75
76
Craig Topperc10719f2014-04-07 04:17:22 +000077static TimerGroup *DefaultTimerGroup = nullptr;
Chris Lattner6dad11f2002-10-01 19:36:54 +000078static TimerGroup *getDefaultTimerGroup() {
Chris Lattnerfafa57a2010-03-29 20:35:01 +000079 TimerGroup *tmp = DefaultTimerGroup;
Benjamin Kramer17388a62014-03-03 18:02:34 +000080 sys::MemoryFence();
Chris Lattnerfafa57a2010-03-29 20:35:01 +000081 if (tmp) return tmp;
82
Zachary Turner6ad24442014-06-19 16:17:42 +000083 sys::SmartScopedLock<true> Lock(*TimerLock);
Chris Lattnerfafa57a2010-03-29 20:35:01 +000084 tmp = DefaultTimerGroup;
Owen Anderson4ed41c82009-06-23 17:33:37 +000085 if (!tmp) {
Chris Lattnerfafa57a2010-03-29 20:35:01 +000086 tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
Benjamin Kramer17388a62014-03-03 18:02:34 +000087 sys::MemoryFence();
Chris Lattnerfafa57a2010-03-29 20:35:01 +000088 DefaultTimerGroup = tmp;
Owen Anderson4ed41c82009-06-23 17:33:37 +000089 }
Mikhail Glushenkov358607d2009-11-07 06:33:12 +000090
Owen Anderson4ed41c82009-06-23 17:33:37 +000091 return tmp;
Chris Lattner6dad11f2002-10-01 19:36:54 +000092}
93
Chris Lattnerfafa57a2010-03-29 20:35:01 +000094//===----------------------------------------------------------------------===//
95// Timer Implementation
96//===----------------------------------------------------------------------===//
97
Chris Lattner39e56a02010-03-30 05:34:02 +000098void Timer::init(StringRef N) {
Vedant Kumar3f79e322015-12-21 23:27:44 +000099 init(N, *getDefaultTimerGroup());
Chris Lattner6dad11f2002-10-01 19:36:54 +0000100}
101
Chris Lattner39e56a02010-03-30 05:34:02 +0000102void Timer::init(StringRef N, TimerGroup &tg) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000103 assert(!TG && "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 = &tg;
Chris Lattner9a608d22010-03-30 04:40:01 +0000107 TG->addTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000108}
109
Chris Lattner6dad11f2002-10-01 19:36:54 +0000110Timer::~Timer() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000111 if (!TG) return; // Never initialized, or already cleared.
Chris Lattner9a608d22010-03-30 04:40:01 +0000112 TG->removeTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000113}
114
Jeff Cohen1a26d152005-01-08 20:15:57 +0000115static inline size_t getMemUsage() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000116 if (!TrackSpace) return 0;
117 return sys::Process::GetMallocUsage();
Reid Spencerad7bdf72004-12-27 08:03:04 +0000118}
119
Chris Lattner707431c2010-03-30 04:03:22 +0000120TimeRecord TimeRecord::getCurrentTime(bool Start) {
Chris Lattner60683452004-06-07 19:34:51 +0000121 TimeRecord Result;
Chris Lattner4cd65712010-03-30 05:20:02 +0000122 sys::TimeValue now(0,0), user(0,0), sys(0,0);
123
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000124 if (Start) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000125 Result.MemUsed = getMemUsage();
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000126 sys::Process::GetTimeUsage(now, user, sys);
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000127 } else {
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000128 sys::Process::GetTimeUsage(now, user, sys);
Chris Lattner4cd65712010-03-30 05:20:02 +0000129 Result.MemUsed = getMemUsage();
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000130 }
Reid Spencer27088812004-12-20 00:59:04 +0000131
Chris Lattner707431c2010-03-30 04:03:22 +0000132 Result.WallTime = now.seconds() + now.microseconds() / 1000000.0;
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000133 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
Chris Lattner707431c2010-03-30 04:03:22 +0000134 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
Chris Lattner6dad11f2002-10-01 19:36:54 +0000135 return Result;
136}
137
Chris Lattner8111c592006-10-04 21:52:35 +0000138static ManagedStatic<std::vector<Timer*> > ActiveTimers;
Chris Lattnerf96a2182002-11-18 21:47:09 +0000139
Chris Lattner6dad11f2002-10-01 19:36:54 +0000140void Timer::startTimer() {
141 Started = true;
Dan Gohman5ceb8b62008-06-24 22:07:07 +0000142 ActiveTimers->push_back(this);
Chris Lattner707431c2010-03-30 04:03:22 +0000143 Time -= TimeRecord::getCurrentTime(true);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000144}
145
146void Timer::stopTimer() {
Chris Lattner707431c2010-03-30 04:03:22 +0000147 Time += TimeRecord::getCurrentTime(false);
Chris Lattnerf96a2182002-11-18 21:47:09 +0000148
Chris Lattner8111c592006-10-04 21:52:35 +0000149 if (ActiveTimers->back() == this) {
150 ActiveTimers->pop_back();
Chris Lattnerf96a2182002-11-18 21:47:09 +0000151 } else {
152 std::vector<Timer*>::iterator I =
Chris Lattner8111c592006-10-04 21:52:35 +0000153 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
154 assert(I != ActiveTimers->end() && "stop but no startTimer?");
155 ActiveTimers->erase(I);
Chris Lattnerf96a2182002-11-18 21:47:09 +0000156 }
Chris Lattner6dad11f2002-10-01 19:36:54 +0000157}
158
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000159static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner5092a6d2010-03-29 20:40:19 +0000160 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000161 OS << " ----- ";
Benjamin Kramercc863b22011-10-16 16:30:34 +0000162 else
163 OS << format(" %7.4f (%5.1f%%)", Val, Val*100/Total);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000164}
165
Chris Lattner707431c2010-03-30 04:03:22 +0000166void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
167 if (Total.getUserTime())
168 printVal(getUserTime(), Total.getUserTime(), OS);
169 if (Total.getSystemTime())
170 printVal(getSystemTime(), Total.getSystemTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000171 if (Total.getProcessTime())
172 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattner707431c2010-03-30 04:03:22 +0000173 printVal(getWallTime(), Total.getWallTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000174
175 OS << " ";
176
Chris Lattner707431c2010-03-30 04:03:22 +0000177 if (Total.getMemUsed())
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000178 OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000179}
180
181
Chris Lattner8bfda652003-10-06 15:02:31 +0000182//===----------------------------------------------------------------------===//
183// NamedRegionTimer Implementation
184//===----------------------------------------------------------------------===//
185
Dan Gohmanb29cda92010-04-15 17:08:50 +0000186namespace {
187
Chris Lattner707431c2010-03-30 04:03:22 +0000188typedef StringMap<Timer> Name2TimerMap;
Chris Lattner4cd65712010-03-30 05:20:02 +0000189
190class Name2PairMap {
191 StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
192public:
193 ~Name2PairMap() {
194 for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
195 I = Map.begin(), E = Map.end(); I != E; ++I)
196 delete I->second.first;
197 }
198
Chris Lattner39e56a02010-03-30 05:34:02 +0000199 Timer &get(StringRef Name, StringRef GroupName) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000200 sys::SmartScopedLock<true> L(*TimerLock);
201
202 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
203
204 if (!GroupEntry.first)
205 GroupEntry.first = new TimerGroup(GroupName);
206
207 Timer &T = GroupEntry.second[Name];
208 if (!T.isInitialized())
209 T.init(Name, *GroupEntry.first);
210 return T;
211 }
212};
Dan Gohmanadec96f2008-07-14 18:19:29 +0000213
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000214}
Dan Gohmanb29cda92010-04-15 17:08:50 +0000215
Chris Lattner707431c2010-03-30 04:03:22 +0000216static ManagedStatic<Name2TimerMap> NamedTimers;
217static ManagedStatic<Name2PairMap> NamedGroupedTimers;
Chris Lattner8bfda652003-10-06 15:02:31 +0000218
Chris Lattner39e56a02010-03-30 05:34:02 +0000219static Timer &getNamedRegionTimer(StringRef Name) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000220 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner707431c2010-03-30 04:03:22 +0000221
222 Timer &T = (*NamedTimers)[Name];
223 if (!T.isInitialized())
224 T.init(Name);
225 return T;
Chris Lattner8bfda652003-10-06 15:02:31 +0000226}
227
Dan Gohman6e681a52010-06-18 15:56:31 +0000228NamedRegionTimer::NamedRegionTimer(StringRef Name,
229 bool Enabled)
Craig Topperc10719f2014-04-07 04:17:22 +0000230 : TimeRegion(!Enabled ? nullptr : &getNamedRegionTimer(Name)) {}
Chris Lattner8bfda652003-10-06 15:02:31 +0000231
Dan Gohman6e681a52010-06-18 15:56:31 +0000232NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef GroupName,
233 bool Enabled)
Craig Topperc10719f2014-04-07 04:17:22 +0000234 : TimeRegion(!Enabled ? nullptr : &NamedGroupedTimers->get(Name, GroupName)){}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000235
Chris Lattner6dad11f2002-10-01 19:36:54 +0000236//===----------------------------------------------------------------------===//
237// TimerGroup Implementation
238//===----------------------------------------------------------------------===//
239
Chris Lattner90fe73d2010-03-30 05:27:58 +0000240/// TimerGroupList - This is the global list of TimerGroups, maintained by the
241/// TimerGroup ctor/dtor and is protected by the TimerLock lock.
Craig Topperc10719f2014-04-07 04:17:22 +0000242static TimerGroup *TimerGroupList = nullptr;
Chris Lattner90fe73d2010-03-30 05:27:58 +0000243
Chris Lattner39e56a02010-03-30 05:34:02 +0000244TimerGroup::TimerGroup(StringRef name)
Craig Topperc10719f2014-04-07 04:17:22 +0000245 : Name(name.begin(), name.end()), FirstTimer(nullptr) {
Chris Lattner90fe73d2010-03-30 05:27:58 +0000246
247 // Add the group to TimerGroupList.
248 sys::SmartScopedLock<true> L(*TimerLock);
249 if (TimerGroupList)
250 TimerGroupList->Prev = &Next;
251 Next = TimerGroupList;
252 Prev = &TimerGroupList;
253 TimerGroupList = this;
254}
255
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000256TimerGroup::~TimerGroup() {
257 // If the timer group is destroyed before the timers it owns, accumulate and
258 // print the timing data.
Craig Topper8d399f82014-04-09 04:20:00 +0000259 while (FirstTimer)
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000260 removeTimer(*FirstTimer);
Chris Lattner90fe73d2010-03-30 05:27:58 +0000261
262 // Remove the group from the TimerGroupList.
263 sys::SmartScopedLock<true> L(*TimerLock);
264 *Prev = Next;
265 if (Next)
266 Next->Prev = Prev;
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000267}
268
269
Chris Lattner9a608d22010-03-30 04:40:01 +0000270void TimerGroup::removeTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000271 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000272
Chris Lattner9a608d22010-03-30 04:40:01 +0000273 // If the timer was started, move its data to TimersToPrint.
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000274 if (T.Started)
Chris Lattner9a608d22010-03-30 04:40:01 +0000275 TimersToPrint.push_back(std::make_pair(T.Time, T.Name));
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000276
Craig Topperc10719f2014-04-07 04:17:22 +0000277 T.TG = nullptr;
Chris Lattner9a608d22010-03-30 04:40:01 +0000278
279 // Unlink the timer from our list.
280 *T.Prev = T.Next;
281 if (T.Next)
282 T.Next->Prev = T.Prev;
283
284 // Print the report when all timers in this group are destroyed if some of
285 // them were started.
Craig Topper8d399f82014-04-09 04:20:00 +0000286 if (FirstTimer || TimersToPrint.empty())
Chris Lattner9a608d22010-03-30 04:40:01 +0000287 return;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000288
289 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
Chris Lattner9a608d22010-03-30 04:40:01 +0000290 PrintQueuedTimers(*OutStream);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000291}
Brian Gaeke960707c2003-11-11 22:41:34 +0000292
Chris Lattner9a608d22010-03-30 04:40:01 +0000293void TimerGroup::addTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000294 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner9a608d22010-03-30 04:40:01 +0000295
296 // Add the timer to our list.
297 if (FirstTimer)
298 FirstTimer->Prev = &T.Next;
299 T.Next = FirstTimer;
300 T.Prev = &FirstTimer;
301 FirstTimer = &T;
Owen Andersone9b1beb2009-06-23 20:52:29 +0000302}
303
Chris Lattner9a608d22010-03-30 04:40:01 +0000304void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
305 // Sort the timers in descending order by amount of time taken.
Benjamin Kramera7d0ccf2010-08-07 13:27:41 +0000306 std::sort(TimersToPrint.begin(), TimersToPrint.end());
307
Chris Lattner9a608d22010-03-30 04:40:01 +0000308 TimeRecord Total;
309 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
310 Total += TimersToPrint[i].first;
311
312 // Print out timing header.
313 OS << "===" << std::string(73, '-') << "===\n";
314 // Figure out how many spaces to indent TimerGroup name.
315 unsigned Padding = (80-Name.length())/2;
316 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
317 OS.indent(Padding) << Name << '\n';
318 OS << "===" << std::string(73, '-') << "===\n";
319
320 // If this is not an collection of ungrouped times, print the total time.
321 // Ungrouped timers don't really make sense to add up. We still print the
322 // TOTAL line to make the percentages make sense.
Benjamin Kramercc863b22011-10-16 16:30:34 +0000323 if (this != DefaultTimerGroup)
324 OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
325 Total.getProcessTime(), Total.getWallTime());
Chris Lattner9a608d22010-03-30 04:40:01 +0000326 OS << '\n';
327
328 if (Total.getUserTime())
329 OS << " ---User Time---";
330 if (Total.getSystemTime())
331 OS << " --System Time--";
332 if (Total.getProcessTime())
333 OS << " --User+System--";
334 OS << " ---Wall Time---";
335 if (Total.getMemUsed())
336 OS << " ---Mem---";
337 OS << " --- Name ---\n";
338
339 // Loop through all of the timing data, printing it out.
340 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) {
341 const std::pair<TimeRecord, std::string> &Entry = TimersToPrint[e-i-1];
342 Entry.first.print(Total, OS);
343 OS << Entry.second << '\n';
344 }
345
346 Total.print(Total, OS);
347 OS << "Total\n\n";
348 OS.flush();
349
350 TimersToPrint.clear();
Owen Andersone9b1beb2009-06-23 20:52:29 +0000351}
352
Chris Lattner4cd65712010-03-30 05:20:02 +0000353/// print - Print any started timers in this group and zero them.
354void TimerGroup::print(raw_ostream &OS) {
355 sys::SmartScopedLock<true> L(*TimerLock);
356
357 // See if any of our timers were started, if so add them to TimersToPrint and
358 // reset them.
359 for (Timer *T = FirstTimer; T; T = T->Next) {
360 if (!T->Started) continue;
361 TimersToPrint.push_back(std::make_pair(T->Time, T->Name));
362
363 // Clear out the time.
364 T->Started = 0;
365 T->Time = TimeRecord();
366 }
367
368 // If any timers were started, print the group.
369 if (!TimersToPrint.empty())
370 PrintQueuedTimers(OS);
371}
Chris Lattner90fe73d2010-03-30 05:27:58 +0000372
373/// printAll - This static method prints all timers and clears them all out.
374void TimerGroup::printAll(raw_ostream &OS) {
375 sys::SmartScopedLock<true> L(*TimerLock);
376
377 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
378 TG->print(OS);
379}