blob: 49bd39e14e288a2cede33ff509350bbfa0026527 [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;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +000082
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());
Vedant Kumard1675862015-12-22 17:36:17 +0000105 Running = Triggered = false;
Chris Lattner707431c2010-03-30 04:03:22 +0000106 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);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000123
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 Lattner6dad11f2002-10-01 19:36:54 +0000138void Timer::startTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000139 assert(!Running && "Cannot start a running timer");
140 Running = Triggered = true;
141 StartTime = TimeRecord::getCurrentTime(true);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000142}
143
144void Timer::stopTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000145 assert(Running && "Cannot stop a paused timer");
146 Running = false;
Chris Lattner707431c2010-03-30 04:03:22 +0000147 Time += TimeRecord::getCurrentTime(false);
Vedant Kumard1675862015-12-22 17:36:17 +0000148 Time -= StartTime;
149}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000150
Vedant Kumard1675862015-12-22 17:36:17 +0000151void Timer::clear() {
152 Running = Triggered = false;
153 Time = StartTime = TimeRecord();
Chris Lattner6dad11f2002-10-01 19:36:54 +0000154}
155
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000156static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner5092a6d2010-03-29 20:40:19 +0000157 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000158 OS << " ----- ";
Benjamin Kramercc863b22011-10-16 16:30:34 +0000159 else
160 OS << format(" %7.4f (%5.1f%%)", Val, Val*100/Total);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000161}
162
Chris Lattner707431c2010-03-30 04:03:22 +0000163void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
164 if (Total.getUserTime())
165 printVal(getUserTime(), Total.getUserTime(), OS);
166 if (Total.getSystemTime())
167 printVal(getSystemTime(), Total.getSystemTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000168 if (Total.getProcessTime())
169 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattner707431c2010-03-30 04:03:22 +0000170 printVal(getWallTime(), Total.getWallTime(), OS);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000171
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000172 OS << " ";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000173
Chris Lattner707431c2010-03-30 04:03:22 +0000174 if (Total.getMemUsed())
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000175 OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000176}
177
178
Chris Lattner8bfda652003-10-06 15:02:31 +0000179//===----------------------------------------------------------------------===//
180// NamedRegionTimer Implementation
181//===----------------------------------------------------------------------===//
182
Dan Gohmanb29cda92010-04-15 17:08:50 +0000183namespace {
184
Chris Lattner707431c2010-03-30 04:03:22 +0000185typedef StringMap<Timer> Name2TimerMap;
Chris Lattner4cd65712010-03-30 05:20:02 +0000186
187class Name2PairMap {
188 StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
189public:
190 ~Name2PairMap() {
191 for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
192 I = Map.begin(), E = Map.end(); I != E; ++I)
193 delete I->second.first;
194 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000195
Chris Lattner39e56a02010-03-30 05:34:02 +0000196 Timer &get(StringRef Name, StringRef GroupName) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000197 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000198
Chris Lattner4cd65712010-03-30 05:20:02 +0000199 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000200
Chris Lattner4cd65712010-03-30 05:20:02 +0000201 if (!GroupEntry.first)
202 GroupEntry.first = new TimerGroup(GroupName);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000203
Chris Lattner4cd65712010-03-30 05:20:02 +0000204 Timer &T = GroupEntry.second[Name];
205 if (!T.isInitialized())
206 T.init(Name, *GroupEntry.first);
207 return T;
208 }
209};
Dan Gohmanadec96f2008-07-14 18:19:29 +0000210
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000211}
Dan Gohmanb29cda92010-04-15 17:08:50 +0000212
Chris Lattner707431c2010-03-30 04:03:22 +0000213static ManagedStatic<Name2TimerMap> NamedTimers;
214static ManagedStatic<Name2PairMap> NamedGroupedTimers;
Chris Lattner8bfda652003-10-06 15:02:31 +0000215
Chris Lattner39e56a02010-03-30 05:34:02 +0000216static Timer &getNamedRegionTimer(StringRef Name) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000217 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000218
Chris Lattner707431c2010-03-30 04:03:22 +0000219 Timer &T = (*NamedTimers)[Name];
220 if (!T.isInitialized())
221 T.init(Name);
222 return T;
Chris Lattner8bfda652003-10-06 15:02:31 +0000223}
224
Dan Gohman6e681a52010-06-18 15:56:31 +0000225NamedRegionTimer::NamedRegionTimer(StringRef Name,
226 bool Enabled)
Craig Topperc10719f2014-04-07 04:17:22 +0000227 : TimeRegion(!Enabled ? nullptr : &getNamedRegionTimer(Name)) {}
Chris Lattner8bfda652003-10-06 15:02:31 +0000228
Dan Gohman6e681a52010-06-18 15:56:31 +0000229NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef GroupName,
230 bool Enabled)
Craig Topperc10719f2014-04-07 04:17:22 +0000231 : TimeRegion(!Enabled ? nullptr : &NamedGroupedTimers->get(Name, GroupName)){}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000232
Chris Lattner6dad11f2002-10-01 19:36:54 +0000233//===----------------------------------------------------------------------===//
234// TimerGroup Implementation
235//===----------------------------------------------------------------------===//
236
Chris Lattner90fe73d2010-03-30 05:27:58 +0000237/// TimerGroupList - This is the global list of TimerGroups, maintained by the
238/// TimerGroup ctor/dtor and is protected by the TimerLock lock.
Craig Topperc10719f2014-04-07 04:17:22 +0000239static TimerGroup *TimerGroupList = nullptr;
Chris Lattner90fe73d2010-03-30 05:27:58 +0000240
Chris Lattner39e56a02010-03-30 05:34:02 +0000241TimerGroup::TimerGroup(StringRef name)
Craig Topperc10719f2014-04-07 04:17:22 +0000242 : Name(name.begin(), name.end()), FirstTimer(nullptr) {
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000243
Chris Lattner90fe73d2010-03-30 05:27:58 +0000244 // Add the group to TimerGroupList.
245 sys::SmartScopedLock<true> L(*TimerLock);
246 if (TimerGroupList)
247 TimerGroupList->Prev = &Next;
248 Next = TimerGroupList;
249 Prev = &TimerGroupList;
250 TimerGroupList = this;
251}
252
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000253TimerGroup::~TimerGroup() {
254 // If the timer group is destroyed before the timers it owns, accumulate and
255 // print the timing data.
Craig Topper8d399f82014-04-09 04:20:00 +0000256 while (FirstTimer)
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000257 removeTimer(*FirstTimer);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000258
Chris Lattner90fe73d2010-03-30 05:27:58 +0000259 // Remove the group from the TimerGroupList.
260 sys::SmartScopedLock<true> L(*TimerLock);
261 *Prev = Next;
262 if (Next)
263 Next->Prev = Prev;
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000264}
265
266
Chris Lattner9a608d22010-03-30 04:40:01 +0000267void TimerGroup::removeTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000268 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000269
Chris Lattner9a608d22010-03-30 04:40:01 +0000270 // If the timer was started, move its data to TimersToPrint.
Vedant Kumard1675862015-12-22 17:36:17 +0000271 if (T.hasTriggered())
Vedant Kumar11dc6dc2015-12-21 23:41:38 +0000272 TimersToPrint.emplace_back(T.Time, T.Name);
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000273
Craig Topperc10719f2014-04-07 04:17:22 +0000274 T.TG = nullptr;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000275
Chris Lattner9a608d22010-03-30 04:40:01 +0000276 // Unlink the timer from our list.
277 *T.Prev = T.Next;
278 if (T.Next)
279 T.Next->Prev = T.Prev;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000280
Chris Lattner9a608d22010-03-30 04:40:01 +0000281 // Print the report when all timers in this group are destroyed if some of
282 // them were started.
Craig Topper8d399f82014-04-09 04:20:00 +0000283 if (FirstTimer || TimersToPrint.empty())
Chris Lattner9a608d22010-03-30 04:40:01 +0000284 return;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000285
286 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
Chris Lattner9a608d22010-03-30 04:40:01 +0000287 PrintQueuedTimers(*OutStream);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000288}
Brian Gaeke960707c2003-11-11 22:41:34 +0000289
Chris Lattner9a608d22010-03-30 04:40:01 +0000290void TimerGroup::addTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000291 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000292
Chris Lattner9a608d22010-03-30 04:40:01 +0000293 // Add the timer to our list.
294 if (FirstTimer)
295 FirstTimer->Prev = &T.Next;
296 T.Next = FirstTimer;
297 T.Prev = &FirstTimer;
298 FirstTimer = &T;
Owen Andersone9b1beb2009-06-23 20:52:29 +0000299}
300
Chris Lattner9a608d22010-03-30 04:40:01 +0000301void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
302 // Sort the timers in descending order by amount of time taken.
Benjamin Kramera7d0ccf2010-08-07 13:27:41 +0000303 std::sort(TimersToPrint.begin(), TimersToPrint.end());
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000304
Chris Lattner9a608d22010-03-30 04:40:01 +0000305 TimeRecord Total;
Vedant Kumar11dc6dc2015-12-21 23:41:38 +0000306 for (auto &RecordNamePair : TimersToPrint)
307 Total += RecordNamePair.first;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000308
Chris Lattner9a608d22010-03-30 04:40:01 +0000309 // Print out timing header.
310 OS << "===" << std::string(73, '-') << "===\n";
311 // Figure out how many spaces to indent TimerGroup name.
312 unsigned Padding = (80-Name.length())/2;
313 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
314 OS.indent(Padding) << Name << '\n';
315 OS << "===" << std::string(73, '-') << "===\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000316
Chris Lattner9a608d22010-03-30 04:40:01 +0000317 // If this is not an collection of ungrouped times, print the total time.
318 // Ungrouped timers don't really make sense to add up. We still print the
319 // TOTAL line to make the percentages make sense.
Benjamin Kramercc863b22011-10-16 16:30:34 +0000320 if (this != DefaultTimerGroup)
321 OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
322 Total.getProcessTime(), Total.getWallTime());
Chris Lattner9a608d22010-03-30 04:40:01 +0000323 OS << '\n';
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000324
Chris Lattner9a608d22010-03-30 04:40:01 +0000325 if (Total.getUserTime())
326 OS << " ---User Time---";
327 if (Total.getSystemTime())
328 OS << " --System Time--";
329 if (Total.getProcessTime())
330 OS << " --User+System--";
331 OS << " ---Wall Time---";
332 if (Total.getMemUsed())
333 OS << " ---Mem---";
334 OS << " --- Name ---\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000335
Chris Lattner9a608d22010-03-30 04:40:01 +0000336 // Loop through all of the timing data, printing it out.
337 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) {
338 const std::pair<TimeRecord, std::string> &Entry = TimersToPrint[e-i-1];
339 Entry.first.print(Total, OS);
340 OS << Entry.second << '\n';
341 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000342
Chris Lattner9a608d22010-03-30 04:40:01 +0000343 Total.print(Total, OS);
344 OS << "Total\n\n";
345 OS.flush();
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000346
Chris Lattner9a608d22010-03-30 04:40:01 +0000347 TimersToPrint.clear();
Owen Andersone9b1beb2009-06-23 20:52:29 +0000348}
349
Chris Lattner4cd65712010-03-30 05:20:02 +0000350/// print - Print any started timers in this group and zero them.
351void TimerGroup::print(raw_ostream &OS) {
352 sys::SmartScopedLock<true> L(*TimerLock);
353
354 // See if any of our timers were started, if so add them to TimersToPrint and
355 // reset them.
356 for (Timer *T = FirstTimer; T; T = T->Next) {
Vedant Kumard1675862015-12-22 17:36:17 +0000357 if (!T->hasTriggered()) continue;
Vedant Kumar11dc6dc2015-12-21 23:41:38 +0000358 TimersToPrint.emplace_back(T->Time, T->Name);
Chris Lattner4cd65712010-03-30 05:20:02 +0000359
360 // Clear out the time.
Vedant Kumard1675862015-12-22 17:36:17 +0000361 T->clear();
Chris Lattner4cd65712010-03-30 05:20:02 +0000362 }
363
364 // If any timers were started, print the group.
365 if (!TimersToPrint.empty())
366 PrintQueuedTimers(OS);
367}
Chris Lattner90fe73d2010-03-30 05:27:58 +0000368
369/// printAll - This static method prints all timers and clears them all out.
370void TimerGroup::printAll(raw_ostream &OS) {
371 sys::SmartScopedLock<true> L(*TimerLock);
372
373 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
374 TG->print(OS);
375}