blob: f032ee5d30d04ef06f8b510574eee17daeddc4cc [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) {
Craig Topper2617dcc2014-04-15 06:32:26 +000099 assert(!TG && "Timer already initialized");
Chris Lattner39e56a02010-03-30 05:34:02 +0000100 Name.assign(N.begin(), N.end());
Chris Lattner707431c2010-03-30 04:03:22 +0000101 Started = false;
102 TG = getDefaultTimerGroup();
Chris Lattner9a608d22010-03-30 04:40:01 +0000103 TG->addTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000104}
105
Chris Lattner39e56a02010-03-30 05:34:02 +0000106void Timer::init(StringRef N, TimerGroup &tg) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000107 assert(!TG && "Timer already initialized");
Chris Lattner39e56a02010-03-30 05:34:02 +0000108 Name.assign(N.begin(), N.end());
Chris Lattner707431c2010-03-30 04:03:22 +0000109 Started = false;
110 TG = &tg;
Chris Lattner9a608d22010-03-30 04:40:01 +0000111 TG->addTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000112}
113
Chris Lattner6dad11f2002-10-01 19:36:54 +0000114Timer::~Timer() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000115 if (!TG) return; // Never initialized, or already cleared.
Chris Lattner9a608d22010-03-30 04:40:01 +0000116 TG->removeTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000117}
118
Jeff Cohen1a26d152005-01-08 20:15:57 +0000119static inline size_t getMemUsage() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000120 if (!TrackSpace) return 0;
121 return sys::Process::GetMallocUsage();
Reid Spencerad7bdf72004-12-27 08:03:04 +0000122}
123
Chris Lattner707431c2010-03-30 04:03:22 +0000124TimeRecord TimeRecord::getCurrentTime(bool Start) {
Chris Lattner60683452004-06-07 19:34:51 +0000125 TimeRecord Result;
Chris Lattner4cd65712010-03-30 05:20:02 +0000126 sys::TimeValue now(0,0), user(0,0), sys(0,0);
127
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000128 if (Start) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000129 Result.MemUsed = getMemUsage();
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000130 sys::Process::GetTimeUsage(now, user, sys);
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000131 } else {
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000132 sys::Process::GetTimeUsage(now, user, sys);
Chris Lattner4cd65712010-03-30 05:20:02 +0000133 Result.MemUsed = getMemUsage();
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000134 }
Reid Spencer27088812004-12-20 00:59:04 +0000135
Chris Lattner707431c2010-03-30 04:03:22 +0000136 Result.WallTime = now.seconds() + now.microseconds() / 1000000.0;
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000137 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
Chris Lattner707431c2010-03-30 04:03:22 +0000138 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
Chris Lattner6dad11f2002-10-01 19:36:54 +0000139 return Result;
140}
141
Chris Lattner8111c592006-10-04 21:52:35 +0000142static ManagedStatic<std::vector<Timer*> > ActiveTimers;
Chris Lattnerf96a2182002-11-18 21:47:09 +0000143
Chris Lattner6dad11f2002-10-01 19:36:54 +0000144void Timer::startTimer() {
145 Started = true;
Dan Gohman5ceb8b62008-06-24 22:07:07 +0000146 ActiveTimers->push_back(this);
Chris Lattner707431c2010-03-30 04:03:22 +0000147 Time -= TimeRecord::getCurrentTime(true);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000148}
149
150void Timer::stopTimer() {
Chris Lattner707431c2010-03-30 04:03:22 +0000151 Time += TimeRecord::getCurrentTime(false);
Chris Lattnerf96a2182002-11-18 21:47:09 +0000152
Chris Lattner8111c592006-10-04 21:52:35 +0000153 if (ActiveTimers->back() == this) {
154 ActiveTimers->pop_back();
Chris Lattnerf96a2182002-11-18 21:47:09 +0000155 } else {
156 std::vector<Timer*>::iterator I =
Chris Lattner8111c592006-10-04 21:52:35 +0000157 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
158 assert(I != ActiveTimers->end() && "stop but no startTimer?");
159 ActiveTimers->erase(I);
Chris Lattnerf96a2182002-11-18 21:47:09 +0000160 }
Chris Lattner6dad11f2002-10-01 19:36:54 +0000161}
162
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000163static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner5092a6d2010-03-29 20:40:19 +0000164 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000165 OS << " ----- ";
Benjamin Kramercc863b22011-10-16 16:30:34 +0000166 else
167 OS << format(" %7.4f (%5.1f%%)", Val, Val*100/Total);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000168}
169
Chris Lattner707431c2010-03-30 04:03:22 +0000170void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
171 if (Total.getUserTime())
172 printVal(getUserTime(), Total.getUserTime(), OS);
173 if (Total.getSystemTime())
174 printVal(getSystemTime(), Total.getSystemTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000175 if (Total.getProcessTime())
176 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattner707431c2010-03-30 04:03:22 +0000177 printVal(getWallTime(), Total.getWallTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000178
179 OS << " ";
180
Chris Lattner707431c2010-03-30 04:03:22 +0000181 if (Total.getMemUsed())
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000182 OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000183}
184
185
Chris Lattner8bfda652003-10-06 15:02:31 +0000186//===----------------------------------------------------------------------===//
187// NamedRegionTimer Implementation
188//===----------------------------------------------------------------------===//
189
Dan Gohmanb29cda92010-04-15 17:08:50 +0000190namespace {
191
Chris Lattner707431c2010-03-30 04:03:22 +0000192typedef StringMap<Timer> Name2TimerMap;
Chris Lattner4cd65712010-03-30 05:20:02 +0000193
194class Name2PairMap {
195 StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
196public:
197 ~Name2PairMap() {
198 for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
199 I = Map.begin(), E = Map.end(); I != E; ++I)
200 delete I->second.first;
201 }
202
Chris Lattner39e56a02010-03-30 05:34:02 +0000203 Timer &get(StringRef Name, StringRef GroupName) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000204 sys::SmartScopedLock<true> L(*TimerLock);
205
206 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
207
208 if (!GroupEntry.first)
209 GroupEntry.first = new TimerGroup(GroupName);
210
211 Timer &T = GroupEntry.second[Name];
212 if (!T.isInitialized())
213 T.init(Name, *GroupEntry.first);
214 return T;
215 }
216};
Dan Gohmanadec96f2008-07-14 18:19:29 +0000217
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000218}
Dan Gohmanb29cda92010-04-15 17:08:50 +0000219
Chris Lattner707431c2010-03-30 04:03:22 +0000220static ManagedStatic<Name2TimerMap> NamedTimers;
221static ManagedStatic<Name2PairMap> NamedGroupedTimers;
Chris Lattner8bfda652003-10-06 15:02:31 +0000222
Chris Lattner39e56a02010-03-30 05:34:02 +0000223static Timer &getNamedRegionTimer(StringRef Name) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000224 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner707431c2010-03-30 04:03:22 +0000225
226 Timer &T = (*NamedTimers)[Name];
227 if (!T.isInitialized())
228 T.init(Name);
229 return T;
Chris Lattner8bfda652003-10-06 15:02:31 +0000230}
231
Dan Gohman6e681a52010-06-18 15:56:31 +0000232NamedRegionTimer::NamedRegionTimer(StringRef Name,
233 bool Enabled)
Craig Topperc10719f2014-04-07 04:17:22 +0000234 : TimeRegion(!Enabled ? nullptr : &getNamedRegionTimer(Name)) {}
Chris Lattner8bfda652003-10-06 15:02:31 +0000235
Dan Gohman6e681a52010-06-18 15:56:31 +0000236NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef GroupName,
237 bool Enabled)
Craig Topperc10719f2014-04-07 04:17:22 +0000238 : TimeRegion(!Enabled ? nullptr : &NamedGroupedTimers->get(Name, GroupName)){}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000239
Chris Lattner6dad11f2002-10-01 19:36:54 +0000240//===----------------------------------------------------------------------===//
241// TimerGroup Implementation
242//===----------------------------------------------------------------------===//
243
Chris Lattner90fe73d2010-03-30 05:27:58 +0000244/// TimerGroupList - This is the global list of TimerGroups, maintained by the
245/// TimerGroup ctor/dtor and is protected by the TimerLock lock.
Craig Topperc10719f2014-04-07 04:17:22 +0000246static TimerGroup *TimerGroupList = nullptr;
Chris Lattner90fe73d2010-03-30 05:27:58 +0000247
Chris Lattner39e56a02010-03-30 05:34:02 +0000248TimerGroup::TimerGroup(StringRef name)
Craig Topperc10719f2014-04-07 04:17:22 +0000249 : Name(name.begin(), name.end()), FirstTimer(nullptr) {
Chris Lattner90fe73d2010-03-30 05:27:58 +0000250
251 // Add the group to TimerGroupList.
252 sys::SmartScopedLock<true> L(*TimerLock);
253 if (TimerGroupList)
254 TimerGroupList->Prev = &Next;
255 Next = TimerGroupList;
256 Prev = &TimerGroupList;
257 TimerGroupList = this;
258}
259
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000260TimerGroup::~TimerGroup() {
261 // If the timer group is destroyed before the timers it owns, accumulate and
262 // print the timing data.
Craig Topper8d399f82014-04-09 04:20:00 +0000263 while (FirstTimer)
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000264 removeTimer(*FirstTimer);
Chris Lattner90fe73d2010-03-30 05:27:58 +0000265
266 // Remove the group from the TimerGroupList.
267 sys::SmartScopedLock<true> L(*TimerLock);
268 *Prev = Next;
269 if (Next)
270 Next->Prev = Prev;
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000271}
272
273
Chris Lattner9a608d22010-03-30 04:40:01 +0000274void TimerGroup::removeTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000275 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000276
Chris Lattner9a608d22010-03-30 04:40:01 +0000277 // If the timer was started, move its data to TimersToPrint.
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000278 if (T.Started)
Chris Lattner9a608d22010-03-30 04:40:01 +0000279 TimersToPrint.push_back(std::make_pair(T.Time, T.Name));
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000280
Craig Topperc10719f2014-04-07 04:17:22 +0000281 T.TG = nullptr;
Chris Lattner9a608d22010-03-30 04:40:01 +0000282
283 // Unlink the timer from our list.
284 *T.Prev = T.Next;
285 if (T.Next)
286 T.Next->Prev = T.Prev;
287
288 // Print the report when all timers in this group are destroyed if some of
289 // them were started.
Craig Topper8d399f82014-04-09 04:20:00 +0000290 if (FirstTimer || TimersToPrint.empty())
Chris Lattner9a608d22010-03-30 04:40:01 +0000291 return;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000292
293 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
Chris Lattner9a608d22010-03-30 04:40:01 +0000294 PrintQueuedTimers(*OutStream);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000295}
Brian Gaeke960707c2003-11-11 22:41:34 +0000296
Chris Lattner9a608d22010-03-30 04:40:01 +0000297void TimerGroup::addTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000298 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner9a608d22010-03-30 04:40:01 +0000299
300 // Add the timer to our list.
301 if (FirstTimer)
302 FirstTimer->Prev = &T.Next;
303 T.Next = FirstTimer;
304 T.Prev = &FirstTimer;
305 FirstTimer = &T;
Owen Andersone9b1beb2009-06-23 20:52:29 +0000306}
307
Chris Lattner9a608d22010-03-30 04:40:01 +0000308void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
309 // Sort the timers in descending order by amount of time taken.
Benjamin Kramera7d0ccf2010-08-07 13:27:41 +0000310 std::sort(TimersToPrint.begin(), TimersToPrint.end());
311
Chris Lattner9a608d22010-03-30 04:40:01 +0000312 TimeRecord Total;
313 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
314 Total += TimersToPrint[i].first;
315
316 // Print out timing header.
317 OS << "===" << std::string(73, '-') << "===\n";
318 // Figure out how many spaces to indent TimerGroup name.
319 unsigned Padding = (80-Name.length())/2;
320 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
321 OS.indent(Padding) << Name << '\n';
322 OS << "===" << std::string(73, '-') << "===\n";
323
324 // If this is not an collection of ungrouped times, print the total time.
325 // Ungrouped timers don't really make sense to add up. We still print the
326 // TOTAL line to make the percentages make sense.
Benjamin Kramercc863b22011-10-16 16:30:34 +0000327 if (this != DefaultTimerGroup)
328 OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
329 Total.getProcessTime(), Total.getWallTime());
Chris Lattner9a608d22010-03-30 04:40:01 +0000330 OS << '\n';
331
332 if (Total.getUserTime())
333 OS << " ---User Time---";
334 if (Total.getSystemTime())
335 OS << " --System Time--";
336 if (Total.getProcessTime())
337 OS << " --User+System--";
338 OS << " ---Wall Time---";
339 if (Total.getMemUsed())
340 OS << " ---Mem---";
341 OS << " --- Name ---\n";
342
343 // Loop through all of the timing data, printing it out.
344 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) {
345 const std::pair<TimeRecord, std::string> &Entry = TimersToPrint[e-i-1];
346 Entry.first.print(Total, OS);
347 OS << Entry.second << '\n';
348 }
349
350 Total.print(Total, OS);
351 OS << "Total\n\n";
352 OS.flush();
353
354 TimersToPrint.clear();
Owen Andersone9b1beb2009-06-23 20:52:29 +0000355}
356
Chris Lattner4cd65712010-03-30 05:20:02 +0000357/// print - Print any started timers in this group and zero them.
358void TimerGroup::print(raw_ostream &OS) {
359 sys::SmartScopedLock<true> L(*TimerLock);
360
361 // See if any of our timers were started, if so add them to TimersToPrint and
362 // reset them.
363 for (Timer *T = FirstTimer; T; T = T->Next) {
364 if (!T->Started) continue;
365 TimersToPrint.push_back(std::make_pair(T->Time, T->Name));
366
367 // Clear out the time.
368 T->Started = 0;
369 T->Time = TimeRecord();
370 }
371
372 // If any timers were started, print the group.
373 if (!TimersToPrint.empty())
374 PrintQueuedTimers(OS);
375}
Chris Lattner90fe73d2010-03-30 05:27:58 +0000376
377/// printAll - This static method prints all timers and clears them all out.
378void TimerGroup::printAll(raw_ostream &OS) {
379 sys::SmartScopedLock<true> L(*TimerLock);
380
381 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
382 TG->print(OS);
383}