blob: 61465ae5e8be861a33d12058c38fa9c11cf50b9d [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"
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 Lattner6cbd8d12010-03-30 05:01:08 +000026// CreateInfoOutputFile - Return a file stream to print our output on.
27namespace llvm { extern raw_ostream *CreateInfoOutputFile(); }
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattnerc4bbc712003-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 Lattner700b8732006-12-06 17:46:33 +000031// problem is that a Statistic object gets destroyed, which ends up calling
Chris Lattnerc4bbc712003-07-31 19:38:34 +000032// 'GetLibSupportInfoOutputFile()' (below), which calls this function.
33// LibSupportInfoOutputFilename used to be a global variable, but sometimes it
Reid Spencer87ad6662004-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 Lattner8111c592006-10-04 21:52:35 +000037static ManagedStatic<std::string> LibSupportInfoOutputFilename;
Chris Lattnerc4bbc712003-07-31 19:38:34 +000038static std::string &getLibSupportInfoOutputFilename() {
Reid Spencer87ad6662004-12-14 03:55:21 +000039 return *LibSupportInfoOutputFilename;
Chris Lattnerc4bbc712003-07-31 19:38:34 +000040}
Chris Lattner6dad11f2002-10-01 19:36:54 +000041
Owen Andersone9b1beb2009-06-23 20:52:29 +000042static ManagedStatic<sys::SmartMutex<true> > TimerLock;
43
Chris Lattner2f752042003-01-30 23:08:50 +000044namespace {
Dan Gohmanc107d002008-04-23 23:15:23 +000045 static cl::opt<bool>
Chris Lattner2f752042003-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 Lattnerb0e59582003-05-09 20:05:44 +000049
Dan Gohmanc107d002008-04-23 23:15:23 +000050 static cl::opt<std::string, true>
Chris Lattnerf1afe32352003-08-01 22:15:15 +000051 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerb0e59582003-05-09 20:05:44 +000052 cl::desc("File to append -stats and -timer output to"),
Chris Lattnerc4bbc712003-07-31 19:38:34 +000053 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Chris Lattner2f752042003-01-30 23:08:50 +000054}
55
Chris Lattner6cbd8d12010-03-30 05:01:08 +000056// CreateInfoOutputFile - Return a file stream to print our output on.
57raw_ostream *llvm::CreateInfoOutputFile() {
Chris Lattner39e56a02010-03-30 05:34:02 +000058 const std::string &OutputFilename = getLibSupportInfoOutputFilename();
59 if (OutputFilename.empty())
Chris Lattner6cbd8d12010-03-30 05:01:08 +000060 return new raw_fd_ostream(2, false); // stderr.
Chris Lattner39e56a02010-03-30 05:34:02 +000061 if (OutputFilename == "-")
Chris Lattner6cbd8d12010-03-30 05:01:08 +000062 return new raw_fd_ostream(1, false); // stdout.
Chris Lattnerdcd7f922010-03-29 21:34:06 +000063
Dan Gohman744c96d2010-05-19 01:21:34 +000064 // Append mode is used because the info output file is opened and closed
65 // each time -stats or -time-passes wants to print output to it. To
66 // compensate for this, the test-suite Makefiles have code to delete the
67 // info output file before running commands which write to it.
Chris Lattnerdcd7f922010-03-29 21:34:06 +000068 std::string Error;
Rafael Espindola90c7f1c2014-02-24 18:20:12 +000069 raw_ostream *Result = new raw_fd_ostream(
70 OutputFilename.c_str(), Error, sys::fs::F_Append | sys::fs::F_Text);
Chris Lattnerdcd7f922010-03-29 21:34:06 +000071 if (Error.empty())
72 return Result;
73
74 errs() << "Error opening info-output-file '"
Chris Lattner39e56a02010-03-30 05:34:02 +000075 << OutputFilename << " for appending!\n";
Chris Lattnerdcd7f922010-03-29 21:34:06 +000076 delete Result;
Chris Lattner6cbd8d12010-03-30 05:01:08 +000077 return new raw_fd_ostream(2, false); // stderr.
Chris Lattnerdcd7f922010-03-29 21:34:06 +000078}
79
80
Craig Topperc10719f2014-04-07 04:17:22 +000081static TimerGroup *DefaultTimerGroup = nullptr;
Chris Lattner6dad11f2002-10-01 19:36:54 +000082static TimerGroup *getDefaultTimerGroup() {
Chris Lattnerfafa57a2010-03-29 20:35:01 +000083 TimerGroup *tmp = DefaultTimerGroup;
Benjamin Kramer17388a62014-03-03 18:02:34 +000084 sys::MemoryFence();
Chris Lattnerfafa57a2010-03-29 20:35:01 +000085 if (tmp) return tmp;
86
Zachary Turnerccbf3d02014-06-16 22:49:41 +000087 llvm_acquire_global_lock();
Chris Lattnerfafa57a2010-03-29 20:35:01 +000088 tmp = DefaultTimerGroup;
Owen Anderson4ed41c82009-06-23 17:33:37 +000089 if (!tmp) {
Chris Lattnerfafa57a2010-03-29 20:35:01 +000090 tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
Benjamin Kramer17388a62014-03-03 18:02:34 +000091 sys::MemoryFence();
Chris Lattnerfafa57a2010-03-29 20:35:01 +000092 DefaultTimerGroup = tmp;
Owen Anderson4ed41c82009-06-23 17:33:37 +000093 }
Zachary Turnerccbf3d02014-06-16 22:49:41 +000094 llvm_release_global_lock();
Mikhail Glushenkov358607d2009-11-07 06:33:12 +000095
Owen Anderson4ed41c82009-06-23 17:33:37 +000096 return tmp;
Chris Lattner6dad11f2002-10-01 19:36:54 +000097}
98
Chris Lattnerfafa57a2010-03-29 20:35:01 +000099//===----------------------------------------------------------------------===//
100// Timer Implementation
101//===----------------------------------------------------------------------===//
102
Chris Lattner39e56a02010-03-30 05:34:02 +0000103void Timer::init(StringRef N) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000104 assert(!TG && "Timer already initialized");
Chris Lattner39e56a02010-03-30 05:34:02 +0000105 Name.assign(N.begin(), N.end());
Chris Lattner707431c2010-03-30 04:03:22 +0000106 Started = false;
107 TG = getDefaultTimerGroup();
Chris Lattner9a608d22010-03-30 04:40:01 +0000108 TG->addTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000109}
110
Chris Lattner39e56a02010-03-30 05:34:02 +0000111void Timer::init(StringRef N, TimerGroup &tg) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000112 assert(!TG && "Timer already initialized");
Chris Lattner39e56a02010-03-30 05:34:02 +0000113 Name.assign(N.begin(), N.end());
Chris Lattner707431c2010-03-30 04:03:22 +0000114 Started = false;
115 TG = &tg;
Chris Lattner9a608d22010-03-30 04:40:01 +0000116 TG->addTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000117}
118
Chris Lattner6dad11f2002-10-01 19:36:54 +0000119Timer::~Timer() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000120 if (!TG) return; // Never initialized, or already cleared.
Chris Lattner9a608d22010-03-30 04:40:01 +0000121 TG->removeTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000122}
123
Jeff Cohen1a26d152005-01-08 20:15:57 +0000124static inline size_t getMemUsage() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000125 if (!TrackSpace) return 0;
126 return sys::Process::GetMallocUsage();
Reid Spencerad7bdf72004-12-27 08:03:04 +0000127}
128
Chris Lattner707431c2010-03-30 04:03:22 +0000129TimeRecord TimeRecord::getCurrentTime(bool Start) {
Chris Lattner60683452004-06-07 19:34:51 +0000130 TimeRecord Result;
Chris Lattner4cd65712010-03-30 05:20:02 +0000131 sys::TimeValue now(0,0), user(0,0), sys(0,0);
132
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000133 if (Start) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000134 Result.MemUsed = getMemUsage();
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000135 sys::Process::GetTimeUsage(now, user, sys);
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000136 } else {
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000137 sys::Process::GetTimeUsage(now, user, sys);
Chris Lattner4cd65712010-03-30 05:20:02 +0000138 Result.MemUsed = getMemUsage();
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000139 }
Reid Spencer27088812004-12-20 00:59:04 +0000140
Chris Lattner707431c2010-03-30 04:03:22 +0000141 Result.WallTime = now.seconds() + now.microseconds() / 1000000.0;
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000142 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
Chris Lattner707431c2010-03-30 04:03:22 +0000143 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
Chris Lattner6dad11f2002-10-01 19:36:54 +0000144 return Result;
145}
146
Chris Lattner8111c592006-10-04 21:52:35 +0000147static ManagedStatic<std::vector<Timer*> > ActiveTimers;
Chris Lattnerf96a2182002-11-18 21:47:09 +0000148
Chris Lattner6dad11f2002-10-01 19:36:54 +0000149void Timer::startTimer() {
150 Started = true;
Dan Gohman5ceb8b62008-06-24 22:07:07 +0000151 ActiveTimers->push_back(this);
Chris Lattner707431c2010-03-30 04:03:22 +0000152 Time -= TimeRecord::getCurrentTime(true);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000153}
154
155void Timer::stopTimer() {
Chris Lattner707431c2010-03-30 04:03:22 +0000156 Time += TimeRecord::getCurrentTime(false);
Chris Lattnerf96a2182002-11-18 21:47:09 +0000157
Chris Lattner8111c592006-10-04 21:52:35 +0000158 if (ActiveTimers->back() == this) {
159 ActiveTimers->pop_back();
Chris Lattnerf96a2182002-11-18 21:47:09 +0000160 } else {
161 std::vector<Timer*>::iterator I =
Chris Lattner8111c592006-10-04 21:52:35 +0000162 std::find(ActiveTimers->begin(), ActiveTimers->end(), this);
163 assert(I != ActiveTimers->end() && "stop but no startTimer?");
164 ActiveTimers->erase(I);
Chris Lattnerf96a2182002-11-18 21:47:09 +0000165 }
Chris Lattner6dad11f2002-10-01 19:36:54 +0000166}
167
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000168static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner5092a6d2010-03-29 20:40:19 +0000169 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000170 OS << " ----- ";
Benjamin Kramercc863b22011-10-16 16:30:34 +0000171 else
172 OS << format(" %7.4f (%5.1f%%)", Val, Val*100/Total);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000173}
174
Chris Lattner707431c2010-03-30 04:03:22 +0000175void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
176 if (Total.getUserTime())
177 printVal(getUserTime(), Total.getUserTime(), OS);
178 if (Total.getSystemTime())
179 printVal(getSystemTime(), Total.getSystemTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000180 if (Total.getProcessTime())
181 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattner707431c2010-03-30 04:03:22 +0000182 printVal(getWallTime(), Total.getWallTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000183
184 OS << " ";
185
Chris Lattner707431c2010-03-30 04:03:22 +0000186 if (Total.getMemUsed())
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000187 OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000188}
189
190
Chris Lattner8bfda652003-10-06 15:02:31 +0000191//===----------------------------------------------------------------------===//
192// NamedRegionTimer Implementation
193//===----------------------------------------------------------------------===//
194
Dan Gohmanb29cda92010-04-15 17:08:50 +0000195namespace {
196
Chris Lattner707431c2010-03-30 04:03:22 +0000197typedef StringMap<Timer> Name2TimerMap;
Chris Lattner4cd65712010-03-30 05:20:02 +0000198
199class Name2PairMap {
200 StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
201public:
202 ~Name2PairMap() {
203 for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
204 I = Map.begin(), E = Map.end(); I != E; ++I)
205 delete I->second.first;
206 }
207
Chris Lattner39e56a02010-03-30 05:34:02 +0000208 Timer &get(StringRef Name, StringRef GroupName) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000209 sys::SmartScopedLock<true> L(*TimerLock);
210
211 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
212
213 if (!GroupEntry.first)
214 GroupEntry.first = new TimerGroup(GroupName);
215
216 Timer &T = GroupEntry.second[Name];
217 if (!T.isInitialized())
218 T.init(Name, *GroupEntry.first);
219 return T;
220 }
221};
Dan Gohmanadec96f2008-07-14 18:19:29 +0000222
Dan Gohmanb29cda92010-04-15 17:08:50 +0000223}
224
Chris Lattner707431c2010-03-30 04:03:22 +0000225static ManagedStatic<Name2TimerMap> NamedTimers;
226static ManagedStatic<Name2PairMap> NamedGroupedTimers;
Chris Lattner8bfda652003-10-06 15:02:31 +0000227
Chris Lattner39e56a02010-03-30 05:34:02 +0000228static Timer &getNamedRegionTimer(StringRef Name) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000229 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner707431c2010-03-30 04:03:22 +0000230
231 Timer &T = (*NamedTimers)[Name];
232 if (!T.isInitialized())
233 T.init(Name);
234 return T;
Chris Lattner8bfda652003-10-06 15:02:31 +0000235}
236
Dan Gohman6e681a52010-06-18 15:56:31 +0000237NamedRegionTimer::NamedRegionTimer(StringRef Name,
238 bool Enabled)
Craig Topperc10719f2014-04-07 04:17:22 +0000239 : TimeRegion(!Enabled ? nullptr : &getNamedRegionTimer(Name)) {}
Chris Lattner8bfda652003-10-06 15:02:31 +0000240
Dan Gohman6e681a52010-06-18 15:56:31 +0000241NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef GroupName,
242 bool Enabled)
Craig Topperc10719f2014-04-07 04:17:22 +0000243 : TimeRegion(!Enabled ? nullptr : &NamedGroupedTimers->get(Name, GroupName)){}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000244
Chris Lattner6dad11f2002-10-01 19:36:54 +0000245//===----------------------------------------------------------------------===//
246// TimerGroup Implementation
247//===----------------------------------------------------------------------===//
248
Chris Lattner90fe73d2010-03-30 05:27:58 +0000249/// TimerGroupList - This is the global list of TimerGroups, maintained by the
250/// TimerGroup ctor/dtor and is protected by the TimerLock lock.
Craig Topperc10719f2014-04-07 04:17:22 +0000251static TimerGroup *TimerGroupList = nullptr;
Chris Lattner90fe73d2010-03-30 05:27:58 +0000252
Chris Lattner39e56a02010-03-30 05:34:02 +0000253TimerGroup::TimerGroup(StringRef name)
Craig Topperc10719f2014-04-07 04:17:22 +0000254 : Name(name.begin(), name.end()), FirstTimer(nullptr) {
Chris Lattner90fe73d2010-03-30 05:27:58 +0000255
256 // Add the group to TimerGroupList.
257 sys::SmartScopedLock<true> L(*TimerLock);
258 if (TimerGroupList)
259 TimerGroupList->Prev = &Next;
260 Next = TimerGroupList;
261 Prev = &TimerGroupList;
262 TimerGroupList = this;
263}
264
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000265TimerGroup::~TimerGroup() {
266 // If the timer group is destroyed before the timers it owns, accumulate and
267 // print the timing data.
Craig Topper8d399f82014-04-09 04:20:00 +0000268 while (FirstTimer)
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000269 removeTimer(*FirstTimer);
Chris Lattner90fe73d2010-03-30 05:27:58 +0000270
271 // Remove the group from the TimerGroupList.
272 sys::SmartScopedLock<true> L(*TimerLock);
273 *Prev = Next;
274 if (Next)
275 Next->Prev = Prev;
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000276}
277
278
Chris Lattner9a608d22010-03-30 04:40:01 +0000279void TimerGroup::removeTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000280 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000281
Chris Lattner9a608d22010-03-30 04:40:01 +0000282 // If the timer was started, move its data to TimersToPrint.
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000283 if (T.Started)
Chris Lattner9a608d22010-03-30 04:40:01 +0000284 TimersToPrint.push_back(std::make_pair(T.Time, T.Name));
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000285
Craig Topperc10719f2014-04-07 04:17:22 +0000286 T.TG = nullptr;
Chris Lattner9a608d22010-03-30 04:40:01 +0000287
288 // Unlink the timer from our list.
289 *T.Prev = T.Next;
290 if (T.Next)
291 T.Next->Prev = T.Prev;
292
293 // Print the report when all timers in this group are destroyed if some of
294 // them were started.
Craig Topper8d399f82014-04-09 04:20:00 +0000295 if (FirstTimer || TimersToPrint.empty())
Chris Lattner9a608d22010-03-30 04:40:01 +0000296 return;
297
Chris Lattner6cbd8d12010-03-30 05:01:08 +0000298 raw_ostream *OutStream = CreateInfoOutputFile();
Chris Lattner9a608d22010-03-30 04:40:01 +0000299 PrintQueuedTimers(*OutStream);
Chris Lattner6cbd8d12010-03-30 05:01:08 +0000300 delete OutStream; // Close the file.
Chris Lattner6dad11f2002-10-01 19:36:54 +0000301}
Brian Gaeke960707c2003-11-11 22:41:34 +0000302
Chris Lattner9a608d22010-03-30 04:40:01 +0000303void TimerGroup::addTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000304 sys::SmartScopedLock<true> L(*TimerLock);
Chris Lattner9a608d22010-03-30 04:40:01 +0000305
306 // Add the timer to our list.
307 if (FirstTimer)
308 FirstTimer->Prev = &T.Next;
309 T.Next = FirstTimer;
310 T.Prev = &FirstTimer;
311 FirstTimer = &T;
Owen Andersone9b1beb2009-06-23 20:52:29 +0000312}
313
Chris Lattner9a608d22010-03-30 04:40:01 +0000314void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
315 // Sort the timers in descending order by amount of time taken.
Benjamin Kramera7d0ccf2010-08-07 13:27:41 +0000316 std::sort(TimersToPrint.begin(), TimersToPrint.end());
317
Chris Lattner9a608d22010-03-30 04:40:01 +0000318 TimeRecord Total;
319 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i)
320 Total += TimersToPrint[i].first;
321
322 // Print out timing header.
323 OS << "===" << std::string(73, '-') << "===\n";
324 // Figure out how many spaces to indent TimerGroup name.
325 unsigned Padding = (80-Name.length())/2;
326 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
327 OS.indent(Padding) << Name << '\n';
328 OS << "===" << std::string(73, '-') << "===\n";
329
330 // If this is not an collection of ungrouped times, print the total time.
331 // Ungrouped timers don't really make sense to add up. We still print the
332 // TOTAL line to make the percentages make sense.
Benjamin Kramercc863b22011-10-16 16:30:34 +0000333 if (this != DefaultTimerGroup)
334 OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
335 Total.getProcessTime(), Total.getWallTime());
Chris Lattner9a608d22010-03-30 04:40:01 +0000336 OS << '\n';
337
338 if (Total.getUserTime())
339 OS << " ---User Time---";
340 if (Total.getSystemTime())
341 OS << " --System Time--";
342 if (Total.getProcessTime())
343 OS << " --User+System--";
344 OS << " ---Wall Time---";
345 if (Total.getMemUsed())
346 OS << " ---Mem---";
347 OS << " --- Name ---\n";
348
349 // Loop through all of the timing data, printing it out.
350 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) {
351 const std::pair<TimeRecord, std::string> &Entry = TimersToPrint[e-i-1];
352 Entry.first.print(Total, OS);
353 OS << Entry.second << '\n';
354 }
355
356 Total.print(Total, OS);
357 OS << "Total\n\n";
358 OS.flush();
359
360 TimersToPrint.clear();
Owen Andersone9b1beb2009-06-23 20:52:29 +0000361}
362
Chris Lattner4cd65712010-03-30 05:20:02 +0000363/// print - Print any started timers in this group and zero them.
364void TimerGroup::print(raw_ostream &OS) {
365 sys::SmartScopedLock<true> L(*TimerLock);
366
367 // See if any of our timers were started, if so add them to TimersToPrint and
368 // reset them.
369 for (Timer *T = FirstTimer; T; T = T->Next) {
370 if (!T->Started) continue;
371 TimersToPrint.push_back(std::make_pair(T->Time, T->Name));
372
373 // Clear out the time.
374 T->Started = 0;
375 T->Time = TimeRecord();
376 }
377
378 // If any timers were started, print the group.
379 if (!TimersToPrint.empty())
380 PrintQueuedTimers(OS);
381}
Chris Lattner90fe73d2010-03-30 05:27:58 +0000382
383/// printAll - This static method prints all timers and clears them all out.
384void TimerGroup::printAll(raw_ostream &OS) {
385 sys::SmartScopedLock<true> L(*TimerLock);
386
387 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
388 TG->print(OS);
389}