blob: ee2bdb0533262ca52a5c85f1f6c398991454fedc [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//
Matthias Braunac287032016-10-14 00:17:19 +000010/// \file Interval Timing implementation.
Chris Lattner6dad11f2002-10-01 19:36:54 +000011//
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
Matthias Braunac287032016-10-14 00:17:19 +000026// This ugly hack is brought to you courtesy of constructor/destructor ordering
27// being unspecified by C++. Basically the problem is that a Statistic object
28// gets destroyed, which ends up calling 'GetLibSupportInfoOutputFile()'
29// (below), which calls this function. LibSupportInfoOutputFilename used to be
30// a global variable, but sometimes it would get destroyed before the Statistic,
31// causing havoc to ensue. We "fix" this by creating the string the first time
32// it is needed and never destroying it.
Chris Lattner8111c592006-10-04 21:52:35 +000033static ManagedStatic<std::string> LibSupportInfoOutputFilename;
Chris Lattnerc4bbc712003-07-31 19:38:34 +000034static std::string &getLibSupportInfoOutputFilename() {
Reid Spencer87ad6662004-12-14 03:55:21 +000035 return *LibSupportInfoOutputFilename;
Chris Lattnerc4bbc712003-07-31 19:38:34 +000036}
Chris Lattner6dad11f2002-10-01 19:36:54 +000037
Owen Andersone9b1beb2009-06-23 20:52:29 +000038static ManagedStatic<sys::SmartMutex<true> > TimerLock;
39
Chris Lattner2f752042003-01-30 23:08:50 +000040namespace {
Dan Gohmanc107d002008-04-23 23:15:23 +000041 static cl::opt<bool>
Chris Lattner2f752042003-01-30 23:08:50 +000042 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
43 "tracking (this may be slow)"),
44 cl::Hidden);
Chris Lattnerb0e59582003-05-09 20:05:44 +000045
Dan Gohmanc107d002008-04-23 23:15:23 +000046 static cl::opt<std::string, true>
Chris Lattnerf1afe32352003-08-01 22:15:15 +000047 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerb0e59582003-05-09 20:05:44 +000048 cl::desc("File to append -stats and -timer output to"),
Chris Lattnerc4bbc712003-07-31 19:38:34 +000049 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Alexander Kornienkof00654e2015-06-23 09:49:53 +000050}
Chris Lattner2f752042003-01-30 23:08:50 +000051
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000052std::unique_ptr<raw_fd_ostream> llvm::CreateInfoOutputFile() {
Chris Lattner39e56a02010-03-30 05:34:02 +000053 const std::string &OutputFilename = getLibSupportInfoOutputFilename();
54 if (OutputFilename.empty())
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000055 return llvm::make_unique<raw_fd_ostream>(2, false); // stderr.
Chris Lattner39e56a02010-03-30 05:34:02 +000056 if (OutputFilename == "-")
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000057 return llvm::make_unique<raw_fd_ostream>(1, false); // stdout.
58
Dan Gohman744c96d2010-05-19 01:21:34 +000059 // Append mode is used because the info output file is opened and closed
60 // each time -stats or -time-passes wants to print output to it. To
61 // compensate for this, the test-suite Makefiles have code to delete the
62 // info output file before running commands which write to it.
Rafael Espindola3fd1e992014-08-25 18:16:47 +000063 std::error_code EC;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000064 auto Result = llvm::make_unique<raw_fd_ostream>(
65 OutputFilename, EC, sys::fs::F_Append | sys::fs::F_Text);
Rafael Espindola3fd1e992014-08-25 18:16:47 +000066 if (!EC)
Chris Lattnerdcd7f922010-03-29 21:34:06 +000067 return Result;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000068
Chris Lattnerdcd7f922010-03-29 21:34:06 +000069 errs() << "Error opening info-output-file '"
Chris Lattner39e56a02010-03-30 05:34:02 +000070 << OutputFilename << " for appending!\n";
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000071 return llvm::make_unique<raw_fd_ostream>(2, false); // stderr.
Chris Lattnerdcd7f922010-03-29 21:34:06 +000072}
73
74
Craig Topperc10719f2014-04-07 04:17:22 +000075static TimerGroup *DefaultTimerGroup = nullptr;
Chris Lattner6dad11f2002-10-01 19:36:54 +000076static TimerGroup *getDefaultTimerGroup() {
Chris Lattnerfafa57a2010-03-29 20:35:01 +000077 TimerGroup *tmp = DefaultTimerGroup;
Benjamin Kramer17388a62014-03-03 18:02:34 +000078 sys::MemoryFence();
Chris Lattnerfafa57a2010-03-29 20:35:01 +000079 if (tmp) return tmp;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +000080
Zachary Turner6ad24442014-06-19 16:17:42 +000081 sys::SmartScopedLock<true> Lock(*TimerLock);
Chris Lattnerfafa57a2010-03-29 20:35:01 +000082 tmp = DefaultTimerGroup;
Owen Anderson4ed41c82009-06-23 17:33:37 +000083 if (!tmp) {
Chris Lattnerfafa57a2010-03-29 20:35:01 +000084 tmp = new TimerGroup("Miscellaneous Ungrouped Timers");
Benjamin Kramer17388a62014-03-03 18:02:34 +000085 sys::MemoryFence();
Chris Lattnerfafa57a2010-03-29 20:35:01 +000086 DefaultTimerGroup = tmp;
Owen Anderson4ed41c82009-06-23 17:33:37 +000087 }
Mikhail Glushenkov358607d2009-11-07 06:33:12 +000088
Owen Anderson4ed41c82009-06-23 17:33:37 +000089 return tmp;
Chris Lattner6dad11f2002-10-01 19:36:54 +000090}
91
Chris Lattnerfafa57a2010-03-29 20:35:01 +000092//===----------------------------------------------------------------------===//
93// Timer Implementation
94//===----------------------------------------------------------------------===//
95
Chris Lattner39e56a02010-03-30 05:34:02 +000096void Timer::init(StringRef N) {
Vedant Kumar3f79e322015-12-21 23:27:44 +000097 init(N, *getDefaultTimerGroup());
Chris Lattner6dad11f2002-10-01 19:36:54 +000098}
99
Chris Lattner39e56a02010-03-30 05:34:02 +0000100void Timer::init(StringRef N, TimerGroup &tg) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000101 assert(!TG && "Timer already initialized");
Chris Lattner39e56a02010-03-30 05:34:02 +0000102 Name.assign(N.begin(), N.end());
Vedant Kumard1675862015-12-22 17:36:17 +0000103 Running = Triggered = false;
Chris Lattner707431c2010-03-30 04:03:22 +0000104 TG = &tg;
Chris Lattner9a608d22010-03-30 04:40:01 +0000105 TG->addTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000106}
107
Chris Lattner6dad11f2002-10-01 19:36:54 +0000108Timer::~Timer() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000109 if (!TG) return; // Never initialized, or already cleared.
Chris Lattner9a608d22010-03-30 04:40:01 +0000110 TG->removeTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000111}
112
Jeff Cohen1a26d152005-01-08 20:15:57 +0000113static inline size_t getMemUsage() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000114 if (!TrackSpace) return 0;
115 return sys::Process::GetMallocUsage();
Reid Spencerad7bdf72004-12-27 08:03:04 +0000116}
117
Chris Lattner707431c2010-03-30 04:03:22 +0000118TimeRecord TimeRecord::getCurrentTime(bool Start) {
Chris Lattner60683452004-06-07 19:34:51 +0000119 TimeRecord Result;
Chris Lattner4cd65712010-03-30 05:20:02 +0000120 sys::TimeValue now(0,0), user(0,0), sys(0,0);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000121
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000122 if (Start) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000123 Result.MemUsed = getMemUsage();
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000124 sys::Process::GetTimeUsage(now, user, sys);
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000125 } else {
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000126 sys::Process::GetTimeUsage(now, user, sys);
Chris Lattner4cd65712010-03-30 05:20:02 +0000127 Result.MemUsed = getMemUsage();
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000128 }
Reid Spencer27088812004-12-20 00:59:04 +0000129
Chris Lattner707431c2010-03-30 04:03:22 +0000130 Result.WallTime = now.seconds() + now.microseconds() / 1000000.0;
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000131 Result.UserTime = user.seconds() + user.microseconds() / 1000000.0;
Chris Lattner707431c2010-03-30 04:03:22 +0000132 Result.SystemTime = sys.seconds() + sys.microseconds() / 1000000.0;
Chris Lattner6dad11f2002-10-01 19:36:54 +0000133 return Result;
134}
135
Chris Lattner6dad11f2002-10-01 19:36:54 +0000136void Timer::startTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000137 assert(!Running && "Cannot start a running timer");
138 Running = Triggered = true;
139 StartTime = TimeRecord::getCurrentTime(true);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000140}
141
142void Timer::stopTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000143 assert(Running && "Cannot stop a paused timer");
144 Running = false;
Chris Lattner707431c2010-03-30 04:03:22 +0000145 Time += TimeRecord::getCurrentTime(false);
Vedant Kumard1675862015-12-22 17:36:17 +0000146 Time -= StartTime;
147}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000148
Vedant Kumard1675862015-12-22 17:36:17 +0000149void Timer::clear() {
150 Running = Triggered = false;
151 Time = StartTime = TimeRecord();
Chris Lattner6dad11f2002-10-01 19:36:54 +0000152}
153
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000154static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner5092a6d2010-03-29 20:40:19 +0000155 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000156 OS << " ----- ";
Benjamin Kramercc863b22011-10-16 16:30:34 +0000157 else
158 OS << format(" %7.4f (%5.1f%%)", Val, Val*100/Total);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000159}
160
Chris Lattner707431c2010-03-30 04:03:22 +0000161void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
162 if (Total.getUserTime())
163 printVal(getUserTime(), Total.getUserTime(), OS);
164 if (Total.getSystemTime())
165 printVal(getSystemTime(), Total.getSystemTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000166 if (Total.getProcessTime())
167 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattner707431c2010-03-30 04:03:22 +0000168 printVal(getWallTime(), Total.getWallTime(), OS);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000169
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000170 OS << " ";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000171
Chris Lattner707431c2010-03-30 04:03:22 +0000172 if (Total.getMemUsed())
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000173 OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000174}
175
176
Chris Lattner8bfda652003-10-06 15:02:31 +0000177//===----------------------------------------------------------------------===//
178// NamedRegionTimer Implementation
179//===----------------------------------------------------------------------===//
180
Dan Gohmanb29cda92010-04-15 17:08:50 +0000181namespace {
182
Chris Lattner707431c2010-03-30 04:03:22 +0000183typedef StringMap<Timer> Name2TimerMap;
Chris Lattner4cd65712010-03-30 05:20:02 +0000184
185class Name2PairMap {
186 StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
187public:
188 ~Name2PairMap() {
189 for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
190 I = Map.begin(), E = Map.end(); I != E; ++I)
191 delete I->second.first;
192 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000193
Chris Lattner39e56a02010-03-30 05:34:02 +0000194 Timer &get(StringRef Name, StringRef GroupName) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000195 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000196
Chris Lattner4cd65712010-03-30 05:20:02 +0000197 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000198
Chris Lattner4cd65712010-03-30 05:20:02 +0000199 if (!GroupEntry.first)
200 GroupEntry.first = new TimerGroup(GroupName);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000201
Chris Lattner4cd65712010-03-30 05:20:02 +0000202 Timer &T = GroupEntry.second[Name];
203 if (!T.isInitialized())
204 T.init(Name, *GroupEntry.first);
205 return T;
206 }
207};
Dan Gohmanadec96f2008-07-14 18:19:29 +0000208
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000209}
Dan Gohmanb29cda92010-04-15 17:08:50 +0000210
Chris Lattner707431c2010-03-30 04:03:22 +0000211static ManagedStatic<Name2TimerMap> NamedTimers;
212static ManagedStatic<Name2PairMap> NamedGroupedTimers;
Chris Lattner8bfda652003-10-06 15:02:31 +0000213
Chris Lattner39e56a02010-03-30 05:34:02 +0000214static Timer &getNamedRegionTimer(StringRef Name) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000215 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000216
Chris Lattner707431c2010-03-30 04:03:22 +0000217 Timer &T = (*NamedTimers)[Name];
218 if (!T.isInitialized())
219 T.init(Name);
220 return T;
Chris Lattner8bfda652003-10-06 15:02:31 +0000221}
222
Dan Gohman6e681a52010-06-18 15:56:31 +0000223NamedRegionTimer::NamedRegionTimer(StringRef Name,
224 bool Enabled)
Craig Topperc10719f2014-04-07 04:17:22 +0000225 : TimeRegion(!Enabled ? nullptr : &getNamedRegionTimer(Name)) {}
Chris Lattner8bfda652003-10-06 15:02:31 +0000226
Dan Gohman6e681a52010-06-18 15:56:31 +0000227NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef GroupName,
228 bool Enabled)
Craig Topperc10719f2014-04-07 04:17:22 +0000229 : TimeRegion(!Enabled ? nullptr : &NamedGroupedTimers->get(Name, GroupName)){}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000230
Chris Lattner6dad11f2002-10-01 19:36:54 +0000231//===----------------------------------------------------------------------===//
232// TimerGroup Implementation
233//===----------------------------------------------------------------------===//
234
Matthias Braunac287032016-10-14 00:17:19 +0000235/// This is the global list of TimerGroups, maintained by the TimerGroup
236/// ctor/dtor and is protected by the TimerLock lock.
Craig Topperc10719f2014-04-07 04:17:22 +0000237static TimerGroup *TimerGroupList = nullptr;
Chris Lattner90fe73d2010-03-30 05:27:58 +0000238
Chris Lattner39e56a02010-03-30 05:34:02 +0000239TimerGroup::TimerGroup(StringRef name)
Matthias Braunac287032016-10-14 00:17:19 +0000240 : Name(name.begin(), name.end()) {
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000241
Chris Lattner90fe73d2010-03-30 05:27:58 +0000242 // Add the group to TimerGroupList.
243 sys::SmartScopedLock<true> L(*TimerLock);
244 if (TimerGroupList)
245 TimerGroupList->Prev = &Next;
246 Next = TimerGroupList;
247 Prev = &TimerGroupList;
248 TimerGroupList = this;
249}
250
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000251TimerGroup::~TimerGroup() {
252 // If the timer group is destroyed before the timers it owns, accumulate and
253 // print the timing data.
Craig Topper8d399f82014-04-09 04:20:00 +0000254 while (FirstTimer)
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000255 removeTimer(*FirstTimer);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000256
Chris Lattner90fe73d2010-03-30 05:27:58 +0000257 // Remove the group from the TimerGroupList.
258 sys::SmartScopedLock<true> L(*TimerLock);
259 *Prev = Next;
260 if (Next)
261 Next->Prev = Prev;
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000262}
263
264
Chris Lattner9a608d22010-03-30 04:40:01 +0000265void TimerGroup::removeTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000266 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000267
Chris Lattner9a608d22010-03-30 04:40:01 +0000268 // If the timer was started, move its data to TimersToPrint.
Vedant Kumard1675862015-12-22 17:36:17 +0000269 if (T.hasTriggered())
Vedant Kumar11dc6dc2015-12-21 23:41:38 +0000270 TimersToPrint.emplace_back(T.Time, T.Name);
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000271
Craig Topperc10719f2014-04-07 04:17:22 +0000272 T.TG = nullptr;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000273
Chris Lattner9a608d22010-03-30 04:40:01 +0000274 // Unlink the timer from our list.
275 *T.Prev = T.Next;
276 if (T.Next)
277 T.Next->Prev = T.Prev;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000278
Chris Lattner9a608d22010-03-30 04:40:01 +0000279 // Print the report when all timers in this group are destroyed if some of
280 // them were started.
Craig Topper8d399f82014-04-09 04:20:00 +0000281 if (FirstTimer || TimersToPrint.empty())
Chris Lattner9a608d22010-03-30 04:40:01 +0000282 return;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000283
284 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
Chris Lattner9a608d22010-03-30 04:40:01 +0000285 PrintQueuedTimers(*OutStream);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000286}
Brian Gaeke960707c2003-11-11 22:41:34 +0000287
Chris Lattner9a608d22010-03-30 04:40:01 +0000288void TimerGroup::addTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000289 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000290
Chris Lattner9a608d22010-03-30 04:40:01 +0000291 // Add the timer to our list.
292 if (FirstTimer)
293 FirstTimer->Prev = &T.Next;
294 T.Next = FirstTimer;
295 T.Prev = &FirstTimer;
296 FirstTimer = &T;
Owen Andersone9b1beb2009-06-23 20:52:29 +0000297}
298
Chris Lattner9a608d22010-03-30 04:40:01 +0000299void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
300 // Sort the timers in descending order by amount of time taken.
Benjamin Kramera7d0ccf2010-08-07 13:27:41 +0000301 std::sort(TimersToPrint.begin(), TimersToPrint.end());
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000302
Chris Lattner9a608d22010-03-30 04:40:01 +0000303 TimeRecord Total;
Vedant Kumar11dc6dc2015-12-21 23:41:38 +0000304 for (auto &RecordNamePair : TimersToPrint)
305 Total += RecordNamePair.first;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000306
Chris Lattner9a608d22010-03-30 04:40:01 +0000307 // Print out timing header.
308 OS << "===" << std::string(73, '-') << "===\n";
309 // Figure out how many spaces to indent TimerGroup name.
310 unsigned Padding = (80-Name.length())/2;
311 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
312 OS.indent(Padding) << Name << '\n';
313 OS << "===" << std::string(73, '-') << "===\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000314
Chris Lattner9a608d22010-03-30 04:40:01 +0000315 // If this is not an collection of ungrouped times, print the total time.
316 // Ungrouped timers don't really make sense to add up. We still print the
317 // TOTAL line to make the percentages make sense.
Benjamin Kramercc863b22011-10-16 16:30:34 +0000318 if (this != DefaultTimerGroup)
319 OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
320 Total.getProcessTime(), Total.getWallTime());
Chris Lattner9a608d22010-03-30 04:40:01 +0000321 OS << '\n';
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000322
Chris Lattner9a608d22010-03-30 04:40:01 +0000323 if (Total.getUserTime())
324 OS << " ---User Time---";
325 if (Total.getSystemTime())
326 OS << " --System Time--";
327 if (Total.getProcessTime())
328 OS << " --User+System--";
329 OS << " ---Wall Time---";
330 if (Total.getMemUsed())
331 OS << " ---Mem---";
332 OS << " --- Name ---\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000333
Chris Lattner9a608d22010-03-30 04:40:01 +0000334 // Loop through all of the timing data, printing it out.
335 for (unsigned i = 0, e = TimersToPrint.size(); i != e; ++i) {
336 const std::pair<TimeRecord, std::string> &Entry = TimersToPrint[e-i-1];
337 Entry.first.print(Total, OS);
338 OS << Entry.second << '\n';
339 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000340
Chris Lattner9a608d22010-03-30 04:40:01 +0000341 Total.print(Total, OS);
342 OS << "Total\n\n";
343 OS.flush();
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000344
Chris Lattner9a608d22010-03-30 04:40:01 +0000345 TimersToPrint.clear();
Owen Andersone9b1beb2009-06-23 20:52:29 +0000346}
347
Chris Lattner4cd65712010-03-30 05:20:02 +0000348void TimerGroup::print(raw_ostream &OS) {
349 sys::SmartScopedLock<true> L(*TimerLock);
350
351 // See if any of our timers were started, if so add them to TimersToPrint and
352 // reset them.
353 for (Timer *T = FirstTimer; T; T = T->Next) {
Vedant Kumard1675862015-12-22 17:36:17 +0000354 if (!T->hasTriggered()) continue;
Vedant Kumar11dc6dc2015-12-21 23:41:38 +0000355 TimersToPrint.emplace_back(T->Time, T->Name);
Matthias Braunac287032016-10-14 00:17:19 +0000356
Chris Lattner4cd65712010-03-30 05:20:02 +0000357 // Clear out the time.
Vedant Kumard1675862015-12-22 17:36:17 +0000358 T->clear();
Chris Lattner4cd65712010-03-30 05:20:02 +0000359 }
360
361 // If any timers were started, print the group.
362 if (!TimersToPrint.empty())
363 PrintQueuedTimers(OS);
364}
Chris Lattner90fe73d2010-03-30 05:27:58 +0000365
Chris Lattner90fe73d2010-03-30 05:27:58 +0000366void TimerGroup::printAll(raw_ostream &OS) {
367 sys::SmartScopedLock<true> L(*TimerLock);
368
369 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
370 TG->print(OS);
371}