blob: bfe19be1f6dbf2905e36ed1f6f9f90b2295253f6 [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"
Matthias Braundb39fd62016-11-18 19:43:24 +000023#include "llvm/Support/YAMLTraits.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000024#include "llvm/Support/raw_ostream.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
Benjamin Kramer2a441a52017-05-29 14:28:04 +000075namespace {
Benjamin Kramer74de0802017-05-29 20:56:27 +000076struct CreateDefaultTimerGroup {
77 static void *call() {
78 return new TimerGroup("misc", "Miscellaneous Ungrouped Timers");
79 }
80};
Benjamin Kramer2a441a52017-05-29 14:28:04 +000081} // namespace
Benjamin Kramer351779e2017-05-29 14:05:29 +000082static ManagedStatic<TimerGroup, CreateDefaultTimerGroup> DefaultTimerGroup;
83static TimerGroup *getDefaultTimerGroup() { return &*DefaultTimerGroup; }
Chris Lattner6dad11f2002-10-01 19:36:54 +000084
Chris Lattnerfafa57a2010-03-29 20:35:01 +000085//===----------------------------------------------------------------------===//
86// Timer Implementation
87//===----------------------------------------------------------------------===//
88
Matthias Braun9f15a792016-11-18 19:43:18 +000089void Timer::init(StringRef Name, StringRef Description) {
90 init(Name, Description, *getDefaultTimerGroup());
Chris Lattner6dad11f2002-10-01 19:36:54 +000091}
92
Matthias Braun9f15a792016-11-18 19:43:18 +000093void Timer::init(StringRef Name, StringRef Description, TimerGroup &tg) {
Craig Topper2617dcc2014-04-15 06:32:26 +000094 assert(!TG && "Timer already initialized");
Matthias Braun9f15a792016-11-18 19:43:18 +000095 this->Name.assign(Name.begin(), Name.end());
96 this->Description.assign(Description.begin(), Description.end());
Vedant Kumard1675862015-12-22 17:36:17 +000097 Running = Triggered = false;
Chris Lattner707431c2010-03-30 04:03:22 +000098 TG = &tg;
Chris Lattner9a608d22010-03-30 04:40:01 +000099 TG->addTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000100}
101
Chris Lattner6dad11f2002-10-01 19:36:54 +0000102Timer::~Timer() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000103 if (!TG) return; // Never initialized, or already cleared.
Chris Lattner9a608d22010-03-30 04:40:01 +0000104 TG->removeTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000105}
106
Jeff Cohen1a26d152005-01-08 20:15:57 +0000107static inline size_t getMemUsage() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000108 if (!TrackSpace) return 0;
109 return sys::Process::GetMallocUsage();
Reid Spencerad7bdf72004-12-27 08:03:04 +0000110}
111
Chris Lattner707431c2010-03-30 04:03:22 +0000112TimeRecord TimeRecord::getCurrentTime(bool Start) {
Pavel Labath757ca882016-10-24 10:59:17 +0000113 using Seconds = std::chrono::duration<double, std::ratio<1>>;
Chris Lattner60683452004-06-07 19:34:51 +0000114 TimeRecord Result;
Pavel Labath757ca882016-10-24 10:59:17 +0000115 sys::TimePoint<> now;
116 std::chrono::nanoseconds user, sys;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000117
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000118 if (Start) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000119 Result.MemUsed = getMemUsage();
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000120 sys::Process::GetTimeUsage(now, user, sys);
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000121 } else {
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000122 sys::Process::GetTimeUsage(now, user, sys);
Chris Lattner4cd65712010-03-30 05:20:02 +0000123 Result.MemUsed = getMemUsage();
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000124 }
Reid Spencer27088812004-12-20 00:59:04 +0000125
Pavel Labath757ca882016-10-24 10:59:17 +0000126 Result.WallTime = Seconds(now.time_since_epoch()).count();
127 Result.UserTime = Seconds(user).count();
128 Result.SystemTime = Seconds(sys).count();
Chris Lattner6dad11f2002-10-01 19:36:54 +0000129 return Result;
130}
131
Chris Lattner6dad11f2002-10-01 19:36:54 +0000132void Timer::startTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000133 assert(!Running && "Cannot start a running timer");
134 Running = Triggered = true;
135 StartTime = TimeRecord::getCurrentTime(true);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000136}
137
138void Timer::stopTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000139 assert(Running && "Cannot stop a paused timer");
140 Running = false;
Chris Lattner707431c2010-03-30 04:03:22 +0000141 Time += TimeRecord::getCurrentTime(false);
Vedant Kumard1675862015-12-22 17:36:17 +0000142 Time -= StartTime;
143}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000144
Vedant Kumard1675862015-12-22 17:36:17 +0000145void Timer::clear() {
146 Running = Triggered = false;
147 Time = StartTime = TimeRecord();
Chris Lattner6dad11f2002-10-01 19:36:54 +0000148}
149
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000150static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner5092a6d2010-03-29 20:40:19 +0000151 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000152 OS << " ----- ";
Benjamin Kramercc863b22011-10-16 16:30:34 +0000153 else
154 OS << format(" %7.4f (%5.1f%%)", Val, Val*100/Total);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000155}
156
Chris Lattner707431c2010-03-30 04:03:22 +0000157void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
158 if (Total.getUserTime())
159 printVal(getUserTime(), Total.getUserTime(), OS);
160 if (Total.getSystemTime())
161 printVal(getSystemTime(), Total.getSystemTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000162 if (Total.getProcessTime())
163 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattner707431c2010-03-30 04:03:22 +0000164 printVal(getWallTime(), Total.getWallTime(), OS);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000165
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000166 OS << " ";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000167
Chris Lattner707431c2010-03-30 04:03:22 +0000168 if (Total.getMemUsed())
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000169 OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000170}
171
172
Chris Lattner8bfda652003-10-06 15:02:31 +0000173//===----------------------------------------------------------------------===//
174// NamedRegionTimer Implementation
175//===----------------------------------------------------------------------===//
176
Dan Gohmanb29cda92010-04-15 17:08:50 +0000177namespace {
178
Chris Lattner707431c2010-03-30 04:03:22 +0000179typedef StringMap<Timer> Name2TimerMap;
Chris Lattner4cd65712010-03-30 05:20:02 +0000180
181class Name2PairMap {
182 StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
183public:
184 ~Name2PairMap() {
185 for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
186 I = Map.begin(), E = Map.end(); I != E; ++I)
187 delete I->second.first;
188 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000189
Matthias Braun9f15a792016-11-18 19:43:18 +0000190 Timer &get(StringRef Name, StringRef Description, StringRef GroupName,
191 StringRef GroupDescription) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000192 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000193
Chris Lattner4cd65712010-03-30 05:20:02 +0000194 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000195
Chris Lattner4cd65712010-03-30 05:20:02 +0000196 if (!GroupEntry.first)
Matthias Braun9f15a792016-11-18 19:43:18 +0000197 GroupEntry.first = new TimerGroup(GroupName, GroupDescription);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000198
Chris Lattner4cd65712010-03-30 05:20:02 +0000199 Timer &T = GroupEntry.second[Name];
200 if (!T.isInitialized())
Matthias Braun9f15a792016-11-18 19:43:18 +0000201 T.init(Name, Description, *GroupEntry.first);
Chris Lattner4cd65712010-03-30 05:20:02 +0000202 return T;
203 }
204};
Dan Gohmanadec96f2008-07-14 18:19:29 +0000205
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000206}
Dan Gohmanb29cda92010-04-15 17:08:50 +0000207
Chris Lattner707431c2010-03-30 04:03:22 +0000208static ManagedStatic<Name2PairMap> NamedGroupedTimers;
Chris Lattner8bfda652003-10-06 15:02:31 +0000209
Matthias Braun9f15a792016-11-18 19:43:18 +0000210NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef Description,
211 StringRef GroupName,
212 StringRef GroupDescription, bool Enabled)
213 : TimeRegion(!Enabled ? nullptr
214 : &NamedGroupedTimers->get(Name, Description, GroupName,
215 GroupDescription)) {}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000216
Chris Lattner6dad11f2002-10-01 19:36:54 +0000217//===----------------------------------------------------------------------===//
218// TimerGroup Implementation
219//===----------------------------------------------------------------------===//
220
Matthias Braunac287032016-10-14 00:17:19 +0000221/// This is the global list of TimerGroups, maintained by the TimerGroup
222/// ctor/dtor and is protected by the TimerLock lock.
Craig Topperc10719f2014-04-07 04:17:22 +0000223static TimerGroup *TimerGroupList = nullptr;
Chris Lattner90fe73d2010-03-30 05:27:58 +0000224
Matthias Braun9f15a792016-11-18 19:43:18 +0000225TimerGroup::TimerGroup(StringRef Name, StringRef Description)
226 : Name(Name.begin(), Name.end()),
227 Description(Description.begin(), Description.end()) {
Chris Lattner90fe73d2010-03-30 05:27:58 +0000228 // Add the group to TimerGroupList.
229 sys::SmartScopedLock<true> L(*TimerLock);
230 if (TimerGroupList)
231 TimerGroupList->Prev = &Next;
232 Next = TimerGroupList;
233 Prev = &TimerGroupList;
234 TimerGroupList = this;
235}
236
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000237TimerGroup::~TimerGroup() {
238 // If the timer group is destroyed before the timers it owns, accumulate and
239 // print the timing data.
Craig Topper8d399f82014-04-09 04:20:00 +0000240 while (FirstTimer)
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000241 removeTimer(*FirstTimer);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000242
Chris Lattner90fe73d2010-03-30 05:27:58 +0000243 // Remove the group from the TimerGroupList.
244 sys::SmartScopedLock<true> L(*TimerLock);
245 *Prev = Next;
246 if (Next)
247 Next->Prev = Prev;
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000248}
249
250
Chris Lattner9a608d22010-03-30 04:40:01 +0000251void TimerGroup::removeTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000252 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000253
Chris Lattner9a608d22010-03-30 04:40:01 +0000254 // If the timer was started, move its data to TimersToPrint.
Vedant Kumard1675862015-12-22 17:36:17 +0000255 if (T.hasTriggered())
Matthias Braundb39fd62016-11-18 19:43:24 +0000256 TimersToPrint.emplace_back(T.Time, T.Name, T.Description);
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000257
Craig Topperc10719f2014-04-07 04:17:22 +0000258 T.TG = nullptr;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000259
Chris Lattner9a608d22010-03-30 04:40:01 +0000260 // Unlink the timer from our list.
261 *T.Prev = T.Next;
262 if (T.Next)
263 T.Next->Prev = T.Prev;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000264
Chris Lattner9a608d22010-03-30 04:40:01 +0000265 // Print the report when all timers in this group are destroyed if some of
266 // them were started.
Craig Topper8d399f82014-04-09 04:20:00 +0000267 if (FirstTimer || TimersToPrint.empty())
Chris Lattner9a608d22010-03-30 04:40:01 +0000268 return;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000269
270 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
Chris Lattner9a608d22010-03-30 04:40:01 +0000271 PrintQueuedTimers(*OutStream);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000272}
Brian Gaeke960707c2003-11-11 22:41:34 +0000273
Chris Lattner9a608d22010-03-30 04:40:01 +0000274void TimerGroup::addTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000275 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000276
Chris Lattner9a608d22010-03-30 04:40:01 +0000277 // Add the timer to our list.
278 if (FirstTimer)
279 FirstTimer->Prev = &T.Next;
280 T.Next = FirstTimer;
281 T.Prev = &FirstTimer;
282 FirstTimer = &T;
Owen Andersone9b1beb2009-06-23 20:52:29 +0000283}
284
Chris Lattner9a608d22010-03-30 04:40:01 +0000285void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
286 // Sort the timers in descending order by amount of time taken.
Benjamin Kramera7d0ccf2010-08-07 13:27:41 +0000287 std::sort(TimersToPrint.begin(), TimersToPrint.end());
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000288
Chris Lattner9a608d22010-03-30 04:40:01 +0000289 TimeRecord Total;
Matthias Braundb39fd62016-11-18 19:43:24 +0000290 for (const PrintRecord &Record : TimersToPrint)
291 Total += Record.Time;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000292
Chris Lattner9a608d22010-03-30 04:40:01 +0000293 // Print out timing header.
294 OS << "===" << std::string(73, '-') << "===\n";
295 // Figure out how many spaces to indent TimerGroup name.
Matthias Braun9f15a792016-11-18 19:43:18 +0000296 unsigned Padding = (80-Description.length())/2;
Chris Lattner9a608d22010-03-30 04:40:01 +0000297 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
Matthias Braun9f15a792016-11-18 19:43:18 +0000298 OS.indent(Padding) << Description << '\n';
Chris Lattner9a608d22010-03-30 04:40:01 +0000299 OS << "===" << std::string(73, '-') << "===\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000300
Chris Lattner9a608d22010-03-30 04:40:01 +0000301 // If this is not an collection of ungrouped times, print the total time.
302 // Ungrouped timers don't really make sense to add up. We still print the
303 // TOTAL line to make the percentages make sense.
Erich Keanec4c31e22017-02-16 20:19:49 +0000304 if (this != getDefaultTimerGroup())
Benjamin Kramercc863b22011-10-16 16:30:34 +0000305 OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
306 Total.getProcessTime(), Total.getWallTime());
Chris Lattner9a608d22010-03-30 04:40:01 +0000307 OS << '\n';
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000308
Chris Lattner9a608d22010-03-30 04:40:01 +0000309 if (Total.getUserTime())
310 OS << " ---User Time---";
311 if (Total.getSystemTime())
312 OS << " --System Time--";
313 if (Total.getProcessTime())
314 OS << " --User+System--";
315 OS << " ---Wall Time---";
316 if (Total.getMemUsed())
317 OS << " ---Mem---";
318 OS << " --- Name ---\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000319
Chris Lattner9a608d22010-03-30 04:40:01 +0000320 // Loop through all of the timing data, printing it out.
Matthias Braundb39fd62016-11-18 19:43:24 +0000321 for (const PrintRecord &Record : make_range(TimersToPrint.rbegin(),
322 TimersToPrint.rend())) {
323 Record.Time.print(Total, OS);
324 OS << Record.Description << '\n';
Chris Lattner9a608d22010-03-30 04:40:01 +0000325 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000326
Chris Lattner9a608d22010-03-30 04:40:01 +0000327 Total.print(Total, OS);
328 OS << "Total\n\n";
329 OS.flush();
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000330
Chris Lattner9a608d22010-03-30 04:40:01 +0000331 TimersToPrint.clear();
Owen Andersone9b1beb2009-06-23 20:52:29 +0000332}
333
Matthias Braundb39fd62016-11-18 19:43:24 +0000334void TimerGroup::prepareToPrintList() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000335 // See if any of our timers were started, if so add them to TimersToPrint and
336 // reset them.
337 for (Timer *T = FirstTimer; T; T = T->Next) {
Vedant Kumard1675862015-12-22 17:36:17 +0000338 if (!T->hasTriggered()) continue;
George Karpenkov560b2442018-02-10 00:38:21 +0000339 bool WasRunning = T->isRunning();
340 if (WasRunning)
341 T->stopTimer();
342
Matthias Braundb39fd62016-11-18 19:43:24 +0000343 TimersToPrint.emplace_back(T->Time, T->Name, T->Description);
Matthias Braunac287032016-10-14 00:17:19 +0000344
George Karpenkov560b2442018-02-10 00:38:21 +0000345 if (WasRunning)
346 T->startTimer();
Chris Lattner4cd65712010-03-30 05:20:02 +0000347 }
Matthias Braundb39fd62016-11-18 19:43:24 +0000348}
349
350void TimerGroup::print(raw_ostream &OS) {
351 sys::SmartScopedLock<true> L(*TimerLock);
352
353 prepareToPrintList();
Chris Lattner4cd65712010-03-30 05:20:02 +0000354
355 // If any timers were started, print the group.
356 if (!TimersToPrint.empty())
357 PrintQueuedTimers(OS);
358}
Chris Lattner90fe73d2010-03-30 05:27:58 +0000359
Chris Lattner90fe73d2010-03-30 05:27:58 +0000360void TimerGroup::printAll(raw_ostream &OS) {
361 sys::SmartScopedLock<true> L(*TimerLock);
362
363 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
364 TG->print(OS);
365}
Matthias Braundb39fd62016-11-18 19:43:24 +0000366
367void TimerGroup::printJSONValue(raw_ostream &OS, const PrintRecord &R,
368 const char *suffix, double Value) {
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000369 assert(yaml::needsQuotes(Name) == yaml::QuotingType::None &&
370 "TimerGroup name needs no quotes");
371 assert(yaml::needsQuotes(R.Name) == yaml::QuotingType::None &&
372 "Timer name needs no quotes");
Matthias Braundb39fd62016-11-18 19:43:24 +0000373 OS << "\t\"time." << Name << '.' << R.Name << suffix << "\": " << Value;
374}
375
376const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) {
377 prepareToPrintList();
378 for (const PrintRecord &R : TimersToPrint) {
379 OS << delim;
380 delim = ",\n";
381
382 const TimeRecord &T = R.Time;
383 printJSONValue(OS, R, ".wall", T.getWallTime());
384 OS << delim;
385 printJSONValue(OS, R, ".user", T.getUserTime());
386 OS << delim;
387 printJSONValue(OS, R, ".sys", T.getSystemTime());
George Karpenkov560b2442018-02-10 00:38:21 +0000388 if (T.getMemUsed()) {
389 OS << delim;
390 printJSONValue(OS, R, ".sys", T.getMemUsed());
391 }
Matthias Braundb39fd62016-11-18 19:43:24 +0000392 }
393 TimersToPrint.clear();
394 return delim;
395}
396
397const char *TimerGroup::printAllJSONValues(raw_ostream &OS, const char *delim) {
398 sys::SmartScopedLock<true> L(*TimerLock);
399 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
400 delim = TG->printJSONValues(OS, delim);
401 return delim;
402}
403
404void TimerGroup::ConstructTimerLists() {
405 (void)*NamedGroupedTimers;
406}