blob: fbd73d0b6b3b640224ba64897eb061d526670b1d [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"
Matthias Braundb39fd62016-11-18 19:43:24 +000024#include "llvm/Support/YAMLTraits.h"
Chris Lattnerdd978ce2003-12-14 21:27:33 +000025using namespace llvm;
Chris Lattnerb0e59582003-05-09 20:05:44 +000026
Matthias Braunac287032016-10-14 00:17:19 +000027// This ugly hack is brought to you courtesy of constructor/destructor ordering
28// being unspecified by C++. Basically the problem is that a Statistic object
29// gets destroyed, which ends up calling 'GetLibSupportInfoOutputFile()'
30// (below), which calls this function. LibSupportInfoOutputFilename used to be
31// a global variable, but sometimes it would get destroyed before the Statistic,
32// causing havoc to ensue. We "fix" this by creating the string the first time
33// it is needed and never destroying 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 +000053std::unique_ptr<raw_fd_ostream> llvm::CreateInfoOutputFile() {
Chris Lattner39e56a02010-03-30 05:34:02 +000054 const std::string &OutputFilename = getLibSupportInfoOutputFilename();
55 if (OutputFilename.empty())
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000056 return llvm::make_unique<raw_fd_ostream>(2, false); // stderr.
Chris Lattner39e56a02010-03-30 05:34:02 +000057 if (OutputFilename == "-")
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000058 return llvm::make_unique<raw_fd_ostream>(1, false); // stdout.
59
Dan Gohman744c96d2010-05-19 01:21:34 +000060 // Append mode is used because the info output file is opened and closed
61 // each time -stats or -time-passes wants to print output to it. To
62 // compensate for this, the test-suite Makefiles have code to delete the
63 // info output file before running commands which write to it.
Rafael Espindola3fd1e992014-08-25 18:16:47 +000064 std::error_code EC;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000065 auto Result = llvm::make_unique<raw_fd_ostream>(
66 OutputFilename, EC, sys::fs::F_Append | sys::fs::F_Text);
Rafael Espindola3fd1e992014-08-25 18:16:47 +000067 if (!EC)
Chris Lattnerdcd7f922010-03-29 21:34:06 +000068 return Result;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000069
Chris Lattnerdcd7f922010-03-29 21:34:06 +000070 errs() << "Error opening info-output-file '"
Chris Lattner39e56a02010-03-30 05:34:02 +000071 << OutputFilename << " for appending!\n";
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000072 return llvm::make_unique<raw_fd_ostream>(2, false); // stderr.
Chris Lattnerdcd7f922010-03-29 21:34:06 +000073}
74
75
Craig Topperc10719f2014-04-07 04:17:22 +000076static TimerGroup *DefaultTimerGroup = nullptr;
Chris Lattner6dad11f2002-10-01 19:36:54 +000077static TimerGroup *getDefaultTimerGroup() {
Chris Lattnerfafa57a2010-03-29 20:35:01 +000078 TimerGroup *tmp = DefaultTimerGroup;
Benjamin Kramer17388a62014-03-03 18:02:34 +000079 sys::MemoryFence();
Chris Lattnerfafa57a2010-03-29 20:35:01 +000080 if (tmp) return tmp;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +000081
Zachary Turner6ad24442014-06-19 16:17:42 +000082 sys::SmartScopedLock<true> Lock(*TimerLock);
Chris Lattnerfafa57a2010-03-29 20:35:01 +000083 tmp = DefaultTimerGroup;
Owen Anderson4ed41c82009-06-23 17:33:37 +000084 if (!tmp) {
Matthias Braun9f15a792016-11-18 19:43:18 +000085 tmp = new TimerGroup("misc", "Miscellaneous Ungrouped Timers");
Benjamin Kramer17388a62014-03-03 18:02:34 +000086 sys::MemoryFence();
Chris Lattnerfafa57a2010-03-29 20:35:01 +000087 DefaultTimerGroup = tmp;
Owen Anderson4ed41c82009-06-23 17:33:37 +000088 }
Mikhail Glushenkov358607d2009-11-07 06:33:12 +000089
Owen Anderson4ed41c82009-06-23 17:33:37 +000090 return tmp;
Chris Lattner6dad11f2002-10-01 19:36:54 +000091}
92
Chris Lattnerfafa57a2010-03-29 20:35:01 +000093//===----------------------------------------------------------------------===//
94// Timer Implementation
95//===----------------------------------------------------------------------===//
96
Matthias Braun9f15a792016-11-18 19:43:18 +000097void Timer::init(StringRef Name, StringRef Description) {
98 init(Name, Description, *getDefaultTimerGroup());
Chris Lattner6dad11f2002-10-01 19:36:54 +000099}
100
Matthias Braun9f15a792016-11-18 19:43:18 +0000101void Timer::init(StringRef Name, StringRef Description, TimerGroup &tg) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000102 assert(!TG && "Timer already initialized");
Matthias Braun9f15a792016-11-18 19:43:18 +0000103 this->Name.assign(Name.begin(), Name.end());
104 this->Description.assign(Description.begin(), Description.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) {
Pavel Labath757ca882016-10-24 10:59:17 +0000121 using Seconds = std::chrono::duration<double, std::ratio<1>>;
Chris Lattner60683452004-06-07 19:34:51 +0000122 TimeRecord Result;
Pavel Labath757ca882016-10-24 10:59:17 +0000123 sys::TimePoint<> now;
124 std::chrono::nanoseconds user, sys;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000125
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000126 if (Start) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000127 Result.MemUsed = getMemUsage();
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000128 sys::Process::GetTimeUsage(now, user, sys);
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000129 } else {
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000130 sys::Process::GetTimeUsage(now, user, sys);
Chris Lattner4cd65712010-03-30 05:20:02 +0000131 Result.MemUsed = getMemUsage();
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000132 }
Reid Spencer27088812004-12-20 00:59:04 +0000133
Pavel Labath757ca882016-10-24 10:59:17 +0000134 Result.WallTime = Seconds(now.time_since_epoch()).count();
135 Result.UserTime = Seconds(user).count();
136 Result.SystemTime = Seconds(sys).count();
Chris Lattner6dad11f2002-10-01 19:36:54 +0000137 return Result;
138}
139
Chris Lattner6dad11f2002-10-01 19:36:54 +0000140void Timer::startTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000141 assert(!Running && "Cannot start a running timer");
142 Running = Triggered = true;
143 StartTime = TimeRecord::getCurrentTime(true);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000144}
145
146void Timer::stopTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000147 assert(Running && "Cannot stop a paused timer");
148 Running = false;
Chris Lattner707431c2010-03-30 04:03:22 +0000149 Time += TimeRecord::getCurrentTime(false);
Vedant Kumard1675862015-12-22 17:36:17 +0000150 Time -= StartTime;
151}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000152
Vedant Kumard1675862015-12-22 17:36:17 +0000153void Timer::clear() {
154 Running = Triggered = false;
155 Time = StartTime = TimeRecord();
Chris Lattner6dad11f2002-10-01 19:36:54 +0000156}
157
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000158static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner5092a6d2010-03-29 20:40:19 +0000159 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000160 OS << " ----- ";
Benjamin Kramercc863b22011-10-16 16:30:34 +0000161 else
162 OS << format(" %7.4f (%5.1f%%)", Val, Val*100/Total);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000163}
164
Chris Lattner707431c2010-03-30 04:03:22 +0000165void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
166 if (Total.getUserTime())
167 printVal(getUserTime(), Total.getUserTime(), OS);
168 if (Total.getSystemTime())
169 printVal(getSystemTime(), Total.getSystemTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000170 if (Total.getProcessTime())
171 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattner707431c2010-03-30 04:03:22 +0000172 printVal(getWallTime(), Total.getWallTime(), OS);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000173
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000174 OS << " ";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000175
Chris Lattner707431c2010-03-30 04:03:22 +0000176 if (Total.getMemUsed())
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000177 OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000178}
179
180
Chris Lattner8bfda652003-10-06 15:02:31 +0000181//===----------------------------------------------------------------------===//
182// NamedRegionTimer Implementation
183//===----------------------------------------------------------------------===//
184
Dan Gohmanb29cda92010-04-15 17:08:50 +0000185namespace {
186
Chris Lattner707431c2010-03-30 04:03:22 +0000187typedef StringMap<Timer> Name2TimerMap;
Chris Lattner4cd65712010-03-30 05:20:02 +0000188
189class Name2PairMap {
190 StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
191public:
192 ~Name2PairMap() {
193 for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
194 I = Map.begin(), E = Map.end(); I != E; ++I)
195 delete I->second.first;
196 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000197
Matthias Braun9f15a792016-11-18 19:43:18 +0000198 Timer &get(StringRef Name, StringRef Description, StringRef GroupName,
199 StringRef GroupDescription) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000200 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000201
Chris Lattner4cd65712010-03-30 05:20:02 +0000202 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000203
Chris Lattner4cd65712010-03-30 05:20:02 +0000204 if (!GroupEntry.first)
Matthias Braun9f15a792016-11-18 19:43:18 +0000205 GroupEntry.first = new TimerGroup(GroupName, GroupDescription);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000206
Chris Lattner4cd65712010-03-30 05:20:02 +0000207 Timer &T = GroupEntry.second[Name];
208 if (!T.isInitialized())
Matthias Braun9f15a792016-11-18 19:43:18 +0000209 T.init(Name, Description, *GroupEntry.first);
Chris Lattner4cd65712010-03-30 05:20:02 +0000210 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<Name2PairMap> NamedGroupedTimers;
Chris Lattner8bfda652003-10-06 15:02:31 +0000217
Matthias Braun9f15a792016-11-18 19:43:18 +0000218NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef Description,
219 StringRef GroupName,
220 StringRef GroupDescription, bool Enabled)
221 : TimeRegion(!Enabled ? nullptr
222 : &NamedGroupedTimers->get(Name, Description, GroupName,
223 GroupDescription)) {}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000224
Chris Lattner6dad11f2002-10-01 19:36:54 +0000225//===----------------------------------------------------------------------===//
226// TimerGroup Implementation
227//===----------------------------------------------------------------------===//
228
Matthias Braunac287032016-10-14 00:17:19 +0000229/// This is the global list of TimerGroups, maintained by the TimerGroup
230/// ctor/dtor and is protected by the TimerLock lock.
Craig Topperc10719f2014-04-07 04:17:22 +0000231static TimerGroup *TimerGroupList = nullptr;
Chris Lattner90fe73d2010-03-30 05:27:58 +0000232
Matthias Braun9f15a792016-11-18 19:43:18 +0000233TimerGroup::TimerGroup(StringRef Name, StringRef Description)
234 : Name(Name.begin(), Name.end()),
235 Description(Description.begin(), Description.end()) {
Chris Lattner90fe73d2010-03-30 05:27:58 +0000236 // Add the group to TimerGroupList.
237 sys::SmartScopedLock<true> L(*TimerLock);
238 if (TimerGroupList)
239 TimerGroupList->Prev = &Next;
240 Next = TimerGroupList;
241 Prev = &TimerGroupList;
242 TimerGroupList = this;
243}
244
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000245TimerGroup::~TimerGroup() {
246 // If the timer group is destroyed before the timers it owns, accumulate and
247 // print the timing data.
Craig Topper8d399f82014-04-09 04:20:00 +0000248 while (FirstTimer)
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000249 removeTimer(*FirstTimer);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000250
Chris Lattner90fe73d2010-03-30 05:27:58 +0000251 // Remove the group from the TimerGroupList.
252 sys::SmartScopedLock<true> L(*TimerLock);
253 *Prev = Next;
254 if (Next)
255 Next->Prev = Prev;
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000256}
257
258
Chris Lattner9a608d22010-03-30 04:40:01 +0000259void TimerGroup::removeTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000260 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000261
Chris Lattner9a608d22010-03-30 04:40:01 +0000262 // If the timer was started, move its data to TimersToPrint.
Vedant Kumard1675862015-12-22 17:36:17 +0000263 if (T.hasTriggered())
Matthias Braundb39fd62016-11-18 19:43:24 +0000264 TimersToPrint.emplace_back(T.Time, T.Name, T.Description);
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000265
Craig Topperc10719f2014-04-07 04:17:22 +0000266 T.TG = nullptr;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000267
Chris Lattner9a608d22010-03-30 04:40:01 +0000268 // Unlink the timer from our list.
269 *T.Prev = T.Next;
270 if (T.Next)
271 T.Next->Prev = T.Prev;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000272
Chris Lattner9a608d22010-03-30 04:40:01 +0000273 // Print the report when all timers in this group are destroyed if some of
274 // them were started.
Craig Topper8d399f82014-04-09 04:20:00 +0000275 if (FirstTimer || TimersToPrint.empty())
Chris Lattner9a608d22010-03-30 04:40:01 +0000276 return;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000277
278 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
Chris Lattner9a608d22010-03-30 04:40:01 +0000279 PrintQueuedTimers(*OutStream);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000280}
Brian Gaeke960707c2003-11-11 22:41:34 +0000281
Chris Lattner9a608d22010-03-30 04:40:01 +0000282void TimerGroup::addTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000283 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000284
Chris Lattner9a608d22010-03-30 04:40:01 +0000285 // Add the timer to our list.
286 if (FirstTimer)
287 FirstTimer->Prev = &T.Next;
288 T.Next = FirstTimer;
289 T.Prev = &FirstTimer;
290 FirstTimer = &T;
Owen Andersone9b1beb2009-06-23 20:52:29 +0000291}
292
Chris Lattner9a608d22010-03-30 04:40:01 +0000293void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
294 // Sort the timers in descending order by amount of time taken.
Benjamin Kramera7d0ccf2010-08-07 13:27:41 +0000295 std::sort(TimersToPrint.begin(), TimersToPrint.end());
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000296
Chris Lattner9a608d22010-03-30 04:40:01 +0000297 TimeRecord Total;
Matthias Braundb39fd62016-11-18 19:43:24 +0000298 for (const PrintRecord &Record : TimersToPrint)
299 Total += Record.Time;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000300
Chris Lattner9a608d22010-03-30 04:40:01 +0000301 // Print out timing header.
302 OS << "===" << std::string(73, '-') << "===\n";
303 // Figure out how many spaces to indent TimerGroup name.
Matthias Braun9f15a792016-11-18 19:43:18 +0000304 unsigned Padding = (80-Description.length())/2;
Chris Lattner9a608d22010-03-30 04:40:01 +0000305 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
Matthias Braun9f15a792016-11-18 19:43:18 +0000306 OS.indent(Padding) << Description << '\n';
Chris Lattner9a608d22010-03-30 04:40:01 +0000307 OS << "===" << std::string(73, '-') << "===\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000308
Chris Lattner9a608d22010-03-30 04:40:01 +0000309 // If this is not an collection of ungrouped times, print the total time.
310 // Ungrouped timers don't really make sense to add up. We still print the
311 // TOTAL line to make the percentages make sense.
Benjamin Kramercc863b22011-10-16 16:30:34 +0000312 if (this != DefaultTimerGroup)
313 OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
314 Total.getProcessTime(), Total.getWallTime());
Chris Lattner9a608d22010-03-30 04:40:01 +0000315 OS << '\n';
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000316
Chris Lattner9a608d22010-03-30 04:40:01 +0000317 if (Total.getUserTime())
318 OS << " ---User Time---";
319 if (Total.getSystemTime())
320 OS << " --System Time--";
321 if (Total.getProcessTime())
322 OS << " --User+System--";
323 OS << " ---Wall Time---";
324 if (Total.getMemUsed())
325 OS << " ---Mem---";
326 OS << " --- Name ---\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000327
Chris Lattner9a608d22010-03-30 04:40:01 +0000328 // Loop through all of the timing data, printing it out.
Matthias Braundb39fd62016-11-18 19:43:24 +0000329 for (const PrintRecord &Record : make_range(TimersToPrint.rbegin(),
330 TimersToPrint.rend())) {
331 Record.Time.print(Total, OS);
332 OS << Record.Description << '\n';
Chris Lattner9a608d22010-03-30 04:40:01 +0000333 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000334
Chris Lattner9a608d22010-03-30 04:40:01 +0000335 Total.print(Total, OS);
336 OS << "Total\n\n";
337 OS.flush();
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000338
Chris Lattner9a608d22010-03-30 04:40:01 +0000339 TimersToPrint.clear();
Owen Andersone9b1beb2009-06-23 20:52:29 +0000340}
341
Matthias Braundb39fd62016-11-18 19:43:24 +0000342void TimerGroup::prepareToPrintList() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000343 // See if any of our timers were started, if so add them to TimersToPrint and
344 // reset them.
345 for (Timer *T = FirstTimer; T; T = T->Next) {
Vedant Kumard1675862015-12-22 17:36:17 +0000346 if (!T->hasTriggered()) continue;
Matthias Braundb39fd62016-11-18 19:43:24 +0000347 TimersToPrint.emplace_back(T->Time, T->Name, T->Description);
Matthias Braunac287032016-10-14 00:17:19 +0000348
Chris Lattner4cd65712010-03-30 05:20:02 +0000349 // Clear out the time.
Vedant Kumard1675862015-12-22 17:36:17 +0000350 T->clear();
Chris Lattner4cd65712010-03-30 05:20:02 +0000351 }
Matthias Braundb39fd62016-11-18 19:43:24 +0000352}
353
354void TimerGroup::print(raw_ostream &OS) {
355 sys::SmartScopedLock<true> L(*TimerLock);
356
357 prepareToPrintList();
Chris Lattner4cd65712010-03-30 05:20:02 +0000358
359 // If any timers were started, print the group.
360 if (!TimersToPrint.empty())
361 PrintQueuedTimers(OS);
362}
Chris Lattner90fe73d2010-03-30 05:27:58 +0000363
Chris Lattner90fe73d2010-03-30 05:27:58 +0000364void TimerGroup::printAll(raw_ostream &OS) {
365 sys::SmartScopedLock<true> L(*TimerLock);
366
367 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
368 TG->print(OS);
369}
Matthias Braundb39fd62016-11-18 19:43:24 +0000370
371void TimerGroup::printJSONValue(raw_ostream &OS, const PrintRecord &R,
372 const char *suffix, double Value) {
373 assert(!yaml::needsQuotes(Name) && "TimerGroup name needs no quotes");
374 assert(!yaml::needsQuotes(R.Name) && "Timer name needs no quotes");
375 OS << "\t\"time." << Name << '.' << R.Name << suffix << "\": " << Value;
376}
377
378const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) {
379 prepareToPrintList();
380 for (const PrintRecord &R : TimersToPrint) {
381 OS << delim;
382 delim = ",\n";
383
384 const TimeRecord &T = R.Time;
385 printJSONValue(OS, R, ".wall", T.getWallTime());
386 OS << delim;
387 printJSONValue(OS, R, ".user", T.getUserTime());
388 OS << delim;
389 printJSONValue(OS, R, ".sys", T.getSystemTime());
390 }
391 TimersToPrint.clear();
392 return delim;
393}
394
395const char *TimerGroup::printAllJSONValues(raw_ostream &OS, const char *delim) {
396 sys::SmartScopedLock<true> L(*TimerLock);
397 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
398 delim = TG->printJSONValues(OS, delim);
399 return delim;
400}
401
402void TimerGroup::ConstructTimerLists() {
403 (void)*NamedGroupedTimers;
404}