blob: 8d68c6ae9682a6f1cb3e167add2cbb9027127475 [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
Chris Lattner6dad11f2002-10-01 19:36:54 +000075static TimerGroup *getDefaultTimerGroup() {
Erich Keanec4c31e22017-02-16 20:19:49 +000076 static TimerGroup DefaultTimerGroup("misc", "Miscellaneous Ungrouped Timers");
77 return &DefaultTimerGroup;
Chris Lattner6dad11f2002-10-01 19:36:54 +000078}
79
Chris Lattnerfafa57a2010-03-29 20:35:01 +000080//===----------------------------------------------------------------------===//
81// Timer Implementation
82//===----------------------------------------------------------------------===//
83
Matthias Braun9f15a792016-11-18 19:43:18 +000084void Timer::init(StringRef Name, StringRef Description) {
85 init(Name, Description, *getDefaultTimerGroup());
Chris Lattner6dad11f2002-10-01 19:36:54 +000086}
87
Matthias Braun9f15a792016-11-18 19:43:18 +000088void Timer::init(StringRef Name, StringRef Description, TimerGroup &tg) {
Craig Topper2617dcc2014-04-15 06:32:26 +000089 assert(!TG && "Timer already initialized");
Matthias Braun9f15a792016-11-18 19:43:18 +000090 this->Name.assign(Name.begin(), Name.end());
91 this->Description.assign(Description.begin(), Description.end());
Vedant Kumard1675862015-12-22 17:36:17 +000092 Running = Triggered = false;
Chris Lattner707431c2010-03-30 04:03:22 +000093 TG = &tg;
Chris Lattner9a608d22010-03-30 04:40:01 +000094 TG->addTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +000095}
96
Chris Lattner6dad11f2002-10-01 19:36:54 +000097Timer::~Timer() {
Chris Lattner4cd65712010-03-30 05:20:02 +000098 if (!TG) return; // Never initialized, or already cleared.
Chris Lattner9a608d22010-03-30 04:40:01 +000099 TG->removeTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000100}
101
Jeff Cohen1a26d152005-01-08 20:15:57 +0000102static inline size_t getMemUsage() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000103 if (!TrackSpace) return 0;
104 return sys::Process::GetMallocUsage();
Reid Spencerad7bdf72004-12-27 08:03:04 +0000105}
106
Chris Lattner707431c2010-03-30 04:03:22 +0000107TimeRecord TimeRecord::getCurrentTime(bool Start) {
Pavel Labath757ca882016-10-24 10:59:17 +0000108 using Seconds = std::chrono::duration<double, std::ratio<1>>;
Chris Lattner60683452004-06-07 19:34:51 +0000109 TimeRecord Result;
Pavel Labath757ca882016-10-24 10:59:17 +0000110 sys::TimePoint<> now;
111 std::chrono::nanoseconds user, sys;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000112
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000113 if (Start) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000114 Result.MemUsed = getMemUsage();
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000115 sys::Process::GetTimeUsage(now, user, sys);
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000116 } else {
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000117 sys::Process::GetTimeUsage(now, user, sys);
Chris Lattner4cd65712010-03-30 05:20:02 +0000118 Result.MemUsed = getMemUsage();
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000119 }
Reid Spencer27088812004-12-20 00:59:04 +0000120
Pavel Labath757ca882016-10-24 10:59:17 +0000121 Result.WallTime = Seconds(now.time_since_epoch()).count();
122 Result.UserTime = Seconds(user).count();
123 Result.SystemTime = Seconds(sys).count();
Chris Lattner6dad11f2002-10-01 19:36:54 +0000124 return Result;
125}
126
Chris Lattner6dad11f2002-10-01 19:36:54 +0000127void Timer::startTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000128 assert(!Running && "Cannot start a running timer");
129 Running = Triggered = true;
130 StartTime = TimeRecord::getCurrentTime(true);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000131}
132
133void Timer::stopTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000134 assert(Running && "Cannot stop a paused timer");
135 Running = false;
Chris Lattner707431c2010-03-30 04:03:22 +0000136 Time += TimeRecord::getCurrentTime(false);
Vedant Kumard1675862015-12-22 17:36:17 +0000137 Time -= StartTime;
138}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000139
Vedant Kumard1675862015-12-22 17:36:17 +0000140void Timer::clear() {
141 Running = Triggered = false;
142 Time = StartTime = TimeRecord();
Chris Lattner6dad11f2002-10-01 19:36:54 +0000143}
144
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000145static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner5092a6d2010-03-29 20:40:19 +0000146 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000147 OS << " ----- ";
Benjamin Kramercc863b22011-10-16 16:30:34 +0000148 else
149 OS << format(" %7.4f (%5.1f%%)", Val, Val*100/Total);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000150}
151
Chris Lattner707431c2010-03-30 04:03:22 +0000152void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
153 if (Total.getUserTime())
154 printVal(getUserTime(), Total.getUserTime(), OS);
155 if (Total.getSystemTime())
156 printVal(getSystemTime(), Total.getSystemTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000157 if (Total.getProcessTime())
158 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattner707431c2010-03-30 04:03:22 +0000159 printVal(getWallTime(), Total.getWallTime(), OS);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000160
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000161 OS << " ";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000162
Chris Lattner707431c2010-03-30 04:03:22 +0000163 if (Total.getMemUsed())
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000164 OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000165}
166
167
Chris Lattner8bfda652003-10-06 15:02:31 +0000168//===----------------------------------------------------------------------===//
169// NamedRegionTimer Implementation
170//===----------------------------------------------------------------------===//
171
Dan Gohmanb29cda92010-04-15 17:08:50 +0000172namespace {
173
Chris Lattner707431c2010-03-30 04:03:22 +0000174typedef StringMap<Timer> Name2TimerMap;
Chris Lattner4cd65712010-03-30 05:20:02 +0000175
176class Name2PairMap {
177 StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
178public:
179 ~Name2PairMap() {
180 for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
181 I = Map.begin(), E = Map.end(); I != E; ++I)
182 delete I->second.first;
183 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000184
Matthias Braun9f15a792016-11-18 19:43:18 +0000185 Timer &get(StringRef Name, StringRef Description, StringRef GroupName,
186 StringRef GroupDescription) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000187 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000188
Chris Lattner4cd65712010-03-30 05:20:02 +0000189 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000190
Chris Lattner4cd65712010-03-30 05:20:02 +0000191 if (!GroupEntry.first)
Matthias Braun9f15a792016-11-18 19:43:18 +0000192 GroupEntry.first = new TimerGroup(GroupName, GroupDescription);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000193
Chris Lattner4cd65712010-03-30 05:20:02 +0000194 Timer &T = GroupEntry.second[Name];
195 if (!T.isInitialized())
Matthias Braun9f15a792016-11-18 19:43:18 +0000196 T.init(Name, Description, *GroupEntry.first);
Chris Lattner4cd65712010-03-30 05:20:02 +0000197 return T;
198 }
199};
Dan Gohmanadec96f2008-07-14 18:19:29 +0000200
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000201}
Dan Gohmanb29cda92010-04-15 17:08:50 +0000202
Chris Lattner707431c2010-03-30 04:03:22 +0000203static ManagedStatic<Name2PairMap> NamedGroupedTimers;
Chris Lattner8bfda652003-10-06 15:02:31 +0000204
Matthias Braun9f15a792016-11-18 19:43:18 +0000205NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef Description,
206 StringRef GroupName,
207 StringRef GroupDescription, bool Enabled)
208 : TimeRegion(!Enabled ? nullptr
209 : &NamedGroupedTimers->get(Name, Description, GroupName,
210 GroupDescription)) {}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000211
Chris Lattner6dad11f2002-10-01 19:36:54 +0000212//===----------------------------------------------------------------------===//
213// TimerGroup Implementation
214//===----------------------------------------------------------------------===//
215
Matthias Braunac287032016-10-14 00:17:19 +0000216/// This is the global list of TimerGroups, maintained by the TimerGroup
217/// ctor/dtor and is protected by the TimerLock lock.
Craig Topperc10719f2014-04-07 04:17:22 +0000218static TimerGroup *TimerGroupList = nullptr;
Chris Lattner90fe73d2010-03-30 05:27:58 +0000219
Matthias Braun9f15a792016-11-18 19:43:18 +0000220TimerGroup::TimerGroup(StringRef Name, StringRef Description)
221 : Name(Name.begin(), Name.end()),
222 Description(Description.begin(), Description.end()) {
Chris Lattner90fe73d2010-03-30 05:27:58 +0000223 // Add the group to TimerGroupList.
224 sys::SmartScopedLock<true> L(*TimerLock);
225 if (TimerGroupList)
226 TimerGroupList->Prev = &Next;
227 Next = TimerGroupList;
228 Prev = &TimerGroupList;
229 TimerGroupList = this;
230}
231
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000232TimerGroup::~TimerGroup() {
233 // If the timer group is destroyed before the timers it owns, accumulate and
234 // print the timing data.
Craig Topper8d399f82014-04-09 04:20:00 +0000235 while (FirstTimer)
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000236 removeTimer(*FirstTimer);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000237
Chris Lattner90fe73d2010-03-30 05:27:58 +0000238 // Remove the group from the TimerGroupList.
239 sys::SmartScopedLock<true> L(*TimerLock);
240 *Prev = Next;
241 if (Next)
242 Next->Prev = Prev;
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000243}
244
245
Chris Lattner9a608d22010-03-30 04:40:01 +0000246void TimerGroup::removeTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000247 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000248
Chris Lattner9a608d22010-03-30 04:40:01 +0000249 // If the timer was started, move its data to TimersToPrint.
Vedant Kumard1675862015-12-22 17:36:17 +0000250 if (T.hasTriggered())
Matthias Braundb39fd62016-11-18 19:43:24 +0000251 TimersToPrint.emplace_back(T.Time, T.Name, T.Description);
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000252
Craig Topperc10719f2014-04-07 04:17:22 +0000253 T.TG = nullptr;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000254
Chris Lattner9a608d22010-03-30 04:40:01 +0000255 // Unlink the timer from our list.
256 *T.Prev = T.Next;
257 if (T.Next)
258 T.Next->Prev = T.Prev;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000259
Chris Lattner9a608d22010-03-30 04:40:01 +0000260 // Print the report when all timers in this group are destroyed if some of
261 // them were started.
Craig Topper8d399f82014-04-09 04:20:00 +0000262 if (FirstTimer || TimersToPrint.empty())
Chris Lattner9a608d22010-03-30 04:40:01 +0000263 return;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000264
265 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
Chris Lattner9a608d22010-03-30 04:40:01 +0000266 PrintQueuedTimers(*OutStream);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000267}
Brian Gaeke960707c2003-11-11 22:41:34 +0000268
Chris Lattner9a608d22010-03-30 04:40:01 +0000269void TimerGroup::addTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000270 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000271
Chris Lattner9a608d22010-03-30 04:40:01 +0000272 // Add the timer to our list.
273 if (FirstTimer)
274 FirstTimer->Prev = &T.Next;
275 T.Next = FirstTimer;
276 T.Prev = &FirstTimer;
277 FirstTimer = &T;
Owen Andersone9b1beb2009-06-23 20:52:29 +0000278}
279
Chris Lattner9a608d22010-03-30 04:40:01 +0000280void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
281 // Sort the timers in descending order by amount of time taken.
Benjamin Kramera7d0ccf2010-08-07 13:27:41 +0000282 std::sort(TimersToPrint.begin(), TimersToPrint.end());
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000283
Chris Lattner9a608d22010-03-30 04:40:01 +0000284 TimeRecord Total;
Matthias Braundb39fd62016-11-18 19:43:24 +0000285 for (const PrintRecord &Record : TimersToPrint)
286 Total += Record.Time;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000287
Chris Lattner9a608d22010-03-30 04:40:01 +0000288 // Print out timing header.
289 OS << "===" << std::string(73, '-') << "===\n";
290 // Figure out how many spaces to indent TimerGroup name.
Matthias Braun9f15a792016-11-18 19:43:18 +0000291 unsigned Padding = (80-Description.length())/2;
Chris Lattner9a608d22010-03-30 04:40:01 +0000292 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
Matthias Braun9f15a792016-11-18 19:43:18 +0000293 OS.indent(Padding) << Description << '\n';
Chris Lattner9a608d22010-03-30 04:40:01 +0000294 OS << "===" << std::string(73, '-') << "===\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000295
Chris Lattner9a608d22010-03-30 04:40:01 +0000296 // If this is not an collection of ungrouped times, print the total time.
297 // Ungrouped timers don't really make sense to add up. We still print the
298 // TOTAL line to make the percentages make sense.
Erich Keanec4c31e22017-02-16 20:19:49 +0000299 if (this != getDefaultTimerGroup())
Benjamin Kramercc863b22011-10-16 16:30:34 +0000300 OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
301 Total.getProcessTime(), Total.getWallTime());
Chris Lattner9a608d22010-03-30 04:40:01 +0000302 OS << '\n';
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000303
Chris Lattner9a608d22010-03-30 04:40:01 +0000304 if (Total.getUserTime())
305 OS << " ---User Time---";
306 if (Total.getSystemTime())
307 OS << " --System Time--";
308 if (Total.getProcessTime())
309 OS << " --User+System--";
310 OS << " ---Wall Time---";
311 if (Total.getMemUsed())
312 OS << " ---Mem---";
313 OS << " --- Name ---\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000314
Chris Lattner9a608d22010-03-30 04:40:01 +0000315 // Loop through all of the timing data, printing it out.
Matthias Braundb39fd62016-11-18 19:43:24 +0000316 for (const PrintRecord &Record : make_range(TimersToPrint.rbegin(),
317 TimersToPrint.rend())) {
318 Record.Time.print(Total, OS);
319 OS << Record.Description << '\n';
Chris Lattner9a608d22010-03-30 04:40:01 +0000320 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000321
Chris Lattner9a608d22010-03-30 04:40:01 +0000322 Total.print(Total, OS);
323 OS << "Total\n\n";
324 OS.flush();
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000325
Chris Lattner9a608d22010-03-30 04:40:01 +0000326 TimersToPrint.clear();
Owen Andersone9b1beb2009-06-23 20:52:29 +0000327}
328
Matthias Braundb39fd62016-11-18 19:43:24 +0000329void TimerGroup::prepareToPrintList() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000330 // See if any of our timers were started, if so add them to TimersToPrint and
331 // reset them.
332 for (Timer *T = FirstTimer; T; T = T->Next) {
Vedant Kumard1675862015-12-22 17:36:17 +0000333 if (!T->hasTriggered()) continue;
Matthias Braundb39fd62016-11-18 19:43:24 +0000334 TimersToPrint.emplace_back(T->Time, T->Name, T->Description);
Matthias Braunac287032016-10-14 00:17:19 +0000335
Chris Lattner4cd65712010-03-30 05:20:02 +0000336 // Clear out the time.
Vedant Kumard1675862015-12-22 17:36:17 +0000337 T->clear();
Chris Lattner4cd65712010-03-30 05:20:02 +0000338 }
Matthias Braundb39fd62016-11-18 19:43:24 +0000339}
340
341void TimerGroup::print(raw_ostream &OS) {
342 sys::SmartScopedLock<true> L(*TimerLock);
343
344 prepareToPrintList();
Chris Lattner4cd65712010-03-30 05:20:02 +0000345
346 // If any timers were started, print the group.
347 if (!TimersToPrint.empty())
348 PrintQueuedTimers(OS);
349}
Chris Lattner90fe73d2010-03-30 05:27:58 +0000350
Chris Lattner90fe73d2010-03-30 05:27:58 +0000351void TimerGroup::printAll(raw_ostream &OS) {
352 sys::SmartScopedLock<true> L(*TimerLock);
353
354 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
355 TG->print(OS);
356}
Matthias Braundb39fd62016-11-18 19:43:24 +0000357
358void TimerGroup::printJSONValue(raw_ostream &OS, const PrintRecord &R,
359 const char *suffix, double Value) {
360 assert(!yaml::needsQuotes(Name) && "TimerGroup name needs no quotes");
361 assert(!yaml::needsQuotes(R.Name) && "Timer name needs no quotes");
362 OS << "\t\"time." << Name << '.' << R.Name << suffix << "\": " << Value;
363}
364
365const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) {
366 prepareToPrintList();
367 for (const PrintRecord &R : TimersToPrint) {
368 OS << delim;
369 delim = ",\n";
370
371 const TimeRecord &T = R.Time;
372 printJSONValue(OS, R, ".wall", T.getWallTime());
373 OS << delim;
374 printJSONValue(OS, R, ".user", T.getUserTime());
375 OS << delim;
376 printJSONValue(OS, R, ".sys", T.getSystemTime());
377 }
378 TimersToPrint.clear();
379 return delim;
380}
381
382const char *TimerGroup::printAllJSONValues(raw_ostream &OS, const char *delim) {
383 sys::SmartScopedLock<true> L(*TimerLock);
384 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
385 delim = TG->printJSONValues(OS, delim);
386 return delim;
387}
388
389void TimerGroup::ConstructTimerLists() {
390 (void)*NamedGroupedTimers;
391}