blob: 4dbc8ed034d654c0b05301973d3b69ed28b5e529 [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"
Roman Lebedevddfefc32018-05-16 18:15:51 +000025#include <limits>
26
Chris Lattnerdd978ce2003-12-14 21:27:33 +000027using namespace llvm;
Chris Lattnerb0e59582003-05-09 20:05:44 +000028
Matthias Braunac287032016-10-14 00:17:19 +000029// This ugly hack is brought to you courtesy of constructor/destructor ordering
30// being unspecified by C++. Basically the problem is that a Statistic object
31// gets destroyed, which ends up calling 'GetLibSupportInfoOutputFile()'
32// (below), which calls this function. LibSupportInfoOutputFilename used to be
33// a global variable, but sometimes it would get destroyed before the Statistic,
34// causing havoc to ensue. We "fix" this by creating the string the first time
35// it is needed and never destroying it.
Chris Lattner8111c592006-10-04 21:52:35 +000036static ManagedStatic<std::string> LibSupportInfoOutputFilename;
Chris Lattnerc4bbc712003-07-31 19:38:34 +000037static std::string &getLibSupportInfoOutputFilename() {
Reid Spencer87ad6662004-12-14 03:55:21 +000038 return *LibSupportInfoOutputFilename;
Chris Lattnerc4bbc712003-07-31 19:38:34 +000039}
Chris Lattner6dad11f2002-10-01 19:36:54 +000040
Owen Andersone9b1beb2009-06-23 20:52:29 +000041static ManagedStatic<sys::SmartMutex<true> > TimerLock;
42
Chris Lattner2f752042003-01-30 23:08:50 +000043namespace {
Dan Gohmanc107d002008-04-23 23:15:23 +000044 static cl::opt<bool>
Chris Lattner2f752042003-01-30 23:08:50 +000045 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
46 "tracking (this may be slow)"),
47 cl::Hidden);
Chris Lattnerb0e59582003-05-09 20:05:44 +000048
Dan Gohmanc107d002008-04-23 23:15:23 +000049 static cl::opt<std::string, true>
Chris Lattnerf1afe32352003-08-01 22:15:15 +000050 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerb0e59582003-05-09 20:05:44 +000051 cl::desc("File to append -stats and -timer output to"),
Chris Lattnerc4bbc712003-07-31 19:38:34 +000052 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Alexander Kornienkof00654e2015-06-23 09:49:53 +000053}
Chris Lattner2f752042003-01-30 23:08:50 +000054
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000055std::unique_ptr<raw_fd_ostream> llvm::CreateInfoOutputFile() {
Chris Lattner39e56a02010-03-30 05:34:02 +000056 const std::string &OutputFilename = getLibSupportInfoOutputFilename();
57 if (OutputFilename.empty())
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000058 return llvm::make_unique<raw_fd_ostream>(2, false); // stderr.
Chris Lattner39e56a02010-03-30 05:34:02 +000059 if (OutputFilename == "-")
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000060 return llvm::make_unique<raw_fd_ostream>(1, false); // stdout.
61
Dan Gohman744c96d2010-05-19 01:21:34 +000062 // Append mode is used because the info output file is opened and closed
63 // each time -stats or -time-passes wants to print output to it. To
64 // compensate for this, the test-suite Makefiles have code to delete the
65 // info output file before running commands which write to it.
Rafael Espindola3fd1e992014-08-25 18:16:47 +000066 std::error_code EC;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000067 auto Result = llvm::make_unique<raw_fd_ostream>(
68 OutputFilename, EC, sys::fs::F_Append | sys::fs::F_Text);
Rafael Espindola3fd1e992014-08-25 18:16:47 +000069 if (!EC)
Chris Lattnerdcd7f922010-03-29 21:34:06 +000070 return Result;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000071
Chris Lattnerdcd7f922010-03-29 21:34:06 +000072 errs() << "Error opening info-output-file '"
Chris Lattner39e56a02010-03-30 05:34:02 +000073 << OutputFilename << " for appending!\n";
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000074 return llvm::make_unique<raw_fd_ostream>(2, false); // stderr.
Chris Lattnerdcd7f922010-03-29 21:34:06 +000075}
76
Benjamin Kramer2a441a52017-05-29 14:28:04 +000077namespace {
Benjamin Kramer74de0802017-05-29 20:56:27 +000078struct CreateDefaultTimerGroup {
79 static void *call() {
80 return new TimerGroup("misc", "Miscellaneous Ungrouped Timers");
81 }
82};
Benjamin Kramer2a441a52017-05-29 14:28:04 +000083} // namespace
Benjamin Kramer351779e2017-05-29 14:05:29 +000084static ManagedStatic<TimerGroup, CreateDefaultTimerGroup> DefaultTimerGroup;
85static TimerGroup *getDefaultTimerGroup() { return &*DefaultTimerGroup; }
Chris Lattner6dad11f2002-10-01 19:36:54 +000086
Chris Lattnerfafa57a2010-03-29 20:35:01 +000087//===----------------------------------------------------------------------===//
88// Timer Implementation
89//===----------------------------------------------------------------------===//
90
Matthias Braun9f15a792016-11-18 19:43:18 +000091void Timer::init(StringRef Name, StringRef Description) {
92 init(Name, Description, *getDefaultTimerGroup());
Chris Lattner6dad11f2002-10-01 19:36:54 +000093}
94
Matthias Braun9f15a792016-11-18 19:43:18 +000095void Timer::init(StringRef Name, StringRef Description, TimerGroup &tg) {
Craig Topper2617dcc2014-04-15 06:32:26 +000096 assert(!TG && "Timer already initialized");
Matthias Braun9f15a792016-11-18 19:43:18 +000097 this->Name.assign(Name.begin(), Name.end());
98 this->Description.assign(Description.begin(), Description.end());
Vedant Kumard1675862015-12-22 17:36:17 +000099 Running = Triggered = false;
Chris Lattner707431c2010-03-30 04:03:22 +0000100 TG = &tg;
Chris Lattner9a608d22010-03-30 04:40:01 +0000101 TG->addTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000102}
103
Chris Lattner6dad11f2002-10-01 19:36:54 +0000104Timer::~Timer() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000105 if (!TG) return; // Never initialized, or already cleared.
Chris Lattner9a608d22010-03-30 04:40:01 +0000106 TG->removeTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000107}
108
Jeff Cohen1a26d152005-01-08 20:15:57 +0000109static inline size_t getMemUsage() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000110 if (!TrackSpace) return 0;
111 return sys::Process::GetMallocUsage();
Reid Spencerad7bdf72004-12-27 08:03:04 +0000112}
113
Chris Lattner707431c2010-03-30 04:03:22 +0000114TimeRecord TimeRecord::getCurrentTime(bool Start) {
Pavel Labath757ca882016-10-24 10:59:17 +0000115 using Seconds = std::chrono::duration<double, std::ratio<1>>;
Chris Lattner60683452004-06-07 19:34:51 +0000116 TimeRecord Result;
Pavel Labath757ca882016-10-24 10:59:17 +0000117 sys::TimePoint<> now;
118 std::chrono::nanoseconds user, sys;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000119
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000120 if (Start) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000121 Result.MemUsed = getMemUsage();
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000122 sys::Process::GetTimeUsage(now, user, sys);
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000123 } else {
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000124 sys::Process::GetTimeUsage(now, user, sys);
Chris Lattner4cd65712010-03-30 05:20:02 +0000125 Result.MemUsed = getMemUsage();
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000126 }
Reid Spencer27088812004-12-20 00:59:04 +0000127
Pavel Labath757ca882016-10-24 10:59:17 +0000128 Result.WallTime = Seconds(now.time_since_epoch()).count();
129 Result.UserTime = Seconds(user).count();
130 Result.SystemTime = Seconds(sys).count();
Chris Lattner6dad11f2002-10-01 19:36:54 +0000131 return Result;
132}
133
Chris Lattner6dad11f2002-10-01 19:36:54 +0000134void Timer::startTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000135 assert(!Running && "Cannot start a running timer");
136 Running = Triggered = true;
137 StartTime = TimeRecord::getCurrentTime(true);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000138}
139
140void Timer::stopTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000141 assert(Running && "Cannot stop a paused timer");
142 Running = false;
Chris Lattner707431c2010-03-30 04:03:22 +0000143 Time += TimeRecord::getCurrentTime(false);
Vedant Kumard1675862015-12-22 17:36:17 +0000144 Time -= StartTime;
145}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000146
Vedant Kumard1675862015-12-22 17:36:17 +0000147void Timer::clear() {
148 Running = Triggered = false;
149 Time = StartTime = TimeRecord();
Chris Lattner6dad11f2002-10-01 19:36:54 +0000150}
151
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000152static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner5092a6d2010-03-29 20:40:19 +0000153 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000154 OS << " ----- ";
Benjamin Kramercc863b22011-10-16 16:30:34 +0000155 else
156 OS << format(" %7.4f (%5.1f%%)", Val, Val*100/Total);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000157}
158
Chris Lattner707431c2010-03-30 04:03:22 +0000159void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
160 if (Total.getUserTime())
161 printVal(getUserTime(), Total.getUserTime(), OS);
162 if (Total.getSystemTime())
163 printVal(getSystemTime(), Total.getSystemTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000164 if (Total.getProcessTime())
165 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattner707431c2010-03-30 04:03:22 +0000166 printVal(getWallTime(), Total.getWallTime(), OS);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000167
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000168 OS << " ";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000169
Chris Lattner707431c2010-03-30 04:03:22 +0000170 if (Total.getMemUsed())
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000171 OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000172}
173
174
Chris Lattner8bfda652003-10-06 15:02:31 +0000175//===----------------------------------------------------------------------===//
176// NamedRegionTimer Implementation
177//===----------------------------------------------------------------------===//
178
Dan Gohmanb29cda92010-04-15 17:08:50 +0000179namespace {
180
Chris Lattner707431c2010-03-30 04:03:22 +0000181typedef StringMap<Timer> Name2TimerMap;
Chris Lattner4cd65712010-03-30 05:20:02 +0000182
183class Name2PairMap {
184 StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
185public:
186 ~Name2PairMap() {
187 for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
188 I = Map.begin(), E = Map.end(); I != E; ++I)
189 delete I->second.first;
190 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000191
Matthias Braun9f15a792016-11-18 19:43:18 +0000192 Timer &get(StringRef Name, StringRef Description, StringRef GroupName,
193 StringRef GroupDescription) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000194 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000195
Chris Lattner4cd65712010-03-30 05:20:02 +0000196 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000197
Chris Lattner4cd65712010-03-30 05:20:02 +0000198 if (!GroupEntry.first)
Matthias Braun9f15a792016-11-18 19:43:18 +0000199 GroupEntry.first = new TimerGroup(GroupName, GroupDescription);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000200
Chris Lattner4cd65712010-03-30 05:20:02 +0000201 Timer &T = GroupEntry.second[Name];
202 if (!T.isInitialized())
Matthias Braun9f15a792016-11-18 19:43:18 +0000203 T.init(Name, Description, *GroupEntry.first);
Chris Lattner4cd65712010-03-30 05:20:02 +0000204 return T;
205 }
206};
Dan Gohmanadec96f2008-07-14 18:19:29 +0000207
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000208}
Dan Gohmanb29cda92010-04-15 17:08:50 +0000209
Chris Lattner707431c2010-03-30 04:03:22 +0000210static ManagedStatic<Name2PairMap> NamedGroupedTimers;
Chris Lattner8bfda652003-10-06 15:02:31 +0000211
Matthias Braun9f15a792016-11-18 19:43:18 +0000212NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef Description,
213 StringRef GroupName,
214 StringRef GroupDescription, bool Enabled)
215 : TimeRegion(!Enabled ? nullptr
216 : &NamedGroupedTimers->get(Name, Description, GroupName,
217 GroupDescription)) {}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000218
Chris Lattner6dad11f2002-10-01 19:36:54 +0000219//===----------------------------------------------------------------------===//
220// TimerGroup Implementation
221//===----------------------------------------------------------------------===//
222
Matthias Braunac287032016-10-14 00:17:19 +0000223/// This is the global list of TimerGroups, maintained by the TimerGroup
224/// ctor/dtor and is protected by the TimerLock lock.
Craig Topperc10719f2014-04-07 04:17:22 +0000225static TimerGroup *TimerGroupList = nullptr;
Chris Lattner90fe73d2010-03-30 05:27:58 +0000226
Matthias Braun9f15a792016-11-18 19:43:18 +0000227TimerGroup::TimerGroup(StringRef Name, StringRef Description)
228 : Name(Name.begin(), Name.end()),
229 Description(Description.begin(), Description.end()) {
Chris Lattner90fe73d2010-03-30 05:27:58 +0000230 // Add the group to TimerGroupList.
231 sys::SmartScopedLock<true> L(*TimerLock);
232 if (TimerGroupList)
233 TimerGroupList->Prev = &Next;
234 Next = TimerGroupList;
235 Prev = &TimerGroupList;
236 TimerGroupList = this;
237}
238
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000239TimerGroup::~TimerGroup() {
240 // If the timer group is destroyed before the timers it owns, accumulate and
241 // print the timing data.
Craig Topper8d399f82014-04-09 04:20:00 +0000242 while (FirstTimer)
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000243 removeTimer(*FirstTimer);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000244
Chris Lattner90fe73d2010-03-30 05:27:58 +0000245 // Remove the group from the TimerGroupList.
246 sys::SmartScopedLock<true> L(*TimerLock);
247 *Prev = Next;
248 if (Next)
249 Next->Prev = Prev;
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000250}
251
252
Chris Lattner9a608d22010-03-30 04:40:01 +0000253void TimerGroup::removeTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000254 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000255
Chris Lattner9a608d22010-03-30 04:40:01 +0000256 // If the timer was started, move its data to TimersToPrint.
Vedant Kumard1675862015-12-22 17:36:17 +0000257 if (T.hasTriggered())
Matthias Braundb39fd62016-11-18 19:43:24 +0000258 TimersToPrint.emplace_back(T.Time, T.Name, T.Description);
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000259
Craig Topperc10719f2014-04-07 04:17:22 +0000260 T.TG = nullptr;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000261
Chris Lattner9a608d22010-03-30 04:40:01 +0000262 // Unlink the timer from our list.
263 *T.Prev = T.Next;
264 if (T.Next)
265 T.Next->Prev = T.Prev;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000266
Chris Lattner9a608d22010-03-30 04:40:01 +0000267 // Print the report when all timers in this group are destroyed if some of
268 // them were started.
Craig Topper8d399f82014-04-09 04:20:00 +0000269 if (FirstTimer || TimersToPrint.empty())
Chris Lattner9a608d22010-03-30 04:40:01 +0000270 return;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000271
272 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
Chris Lattner9a608d22010-03-30 04:40:01 +0000273 PrintQueuedTimers(*OutStream);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000274}
Brian Gaeke960707c2003-11-11 22:41:34 +0000275
Chris Lattner9a608d22010-03-30 04:40:01 +0000276void TimerGroup::addTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000277 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000278
Chris Lattner9a608d22010-03-30 04:40:01 +0000279 // Add the timer to our list.
280 if (FirstTimer)
281 FirstTimer->Prev = &T.Next;
282 T.Next = FirstTimer;
283 T.Prev = &FirstTimer;
284 FirstTimer = &T;
Owen Andersone9b1beb2009-06-23 20:52:29 +0000285}
286
Chris Lattner9a608d22010-03-30 04:40:01 +0000287void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
288 // Sort the timers in descending order by amount of time taken.
Mandeep Singh Grang68ab4012018-04-08 16:46:22 +0000289 llvm::sort(TimersToPrint.begin(), TimersToPrint.end());
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000290
Chris Lattner9a608d22010-03-30 04:40:01 +0000291 TimeRecord Total;
Matthias Braundb39fd62016-11-18 19:43:24 +0000292 for (const PrintRecord &Record : TimersToPrint)
293 Total += Record.Time;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000294
Chris Lattner9a608d22010-03-30 04:40:01 +0000295 // Print out timing header.
296 OS << "===" << std::string(73, '-') << "===\n";
297 // Figure out how many spaces to indent TimerGroup name.
Matthias Braun9f15a792016-11-18 19:43:18 +0000298 unsigned Padding = (80-Description.length())/2;
Chris Lattner9a608d22010-03-30 04:40:01 +0000299 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
Matthias Braun9f15a792016-11-18 19:43:18 +0000300 OS.indent(Padding) << Description << '\n';
Chris Lattner9a608d22010-03-30 04:40:01 +0000301 OS << "===" << std::string(73, '-') << "===\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000302
Chris Lattner9a608d22010-03-30 04:40:01 +0000303 // If this is not an collection of ungrouped times, print the total time.
304 // Ungrouped timers don't really make sense to add up. We still print the
305 // TOTAL line to make the percentages make sense.
Erich Keanec4c31e22017-02-16 20:19:49 +0000306 if (this != getDefaultTimerGroup())
Benjamin Kramercc863b22011-10-16 16:30:34 +0000307 OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
308 Total.getProcessTime(), Total.getWallTime());
Chris Lattner9a608d22010-03-30 04:40:01 +0000309 OS << '\n';
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000310
Chris Lattner9a608d22010-03-30 04:40:01 +0000311 if (Total.getUserTime())
312 OS << " ---User Time---";
313 if (Total.getSystemTime())
314 OS << " --System Time--";
315 if (Total.getProcessTime())
316 OS << " --User+System--";
317 OS << " ---Wall Time---";
318 if (Total.getMemUsed())
319 OS << " ---Mem---";
320 OS << " --- Name ---\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000321
Chris Lattner9a608d22010-03-30 04:40:01 +0000322 // Loop through all of the timing data, printing it out.
Matthias Braundb39fd62016-11-18 19:43:24 +0000323 for (const PrintRecord &Record : make_range(TimersToPrint.rbegin(),
324 TimersToPrint.rend())) {
325 Record.Time.print(Total, OS);
326 OS << Record.Description << '\n';
Chris Lattner9a608d22010-03-30 04:40:01 +0000327 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000328
Chris Lattner9a608d22010-03-30 04:40:01 +0000329 Total.print(Total, OS);
330 OS << "Total\n\n";
331 OS.flush();
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000332
Chris Lattner9a608d22010-03-30 04:40:01 +0000333 TimersToPrint.clear();
Owen Andersone9b1beb2009-06-23 20:52:29 +0000334}
335
Matthias Braundb39fd62016-11-18 19:43:24 +0000336void TimerGroup::prepareToPrintList() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000337 // See if any of our timers were started, if so add them to TimersToPrint and
338 // reset them.
339 for (Timer *T = FirstTimer; T; T = T->Next) {
Vedant Kumard1675862015-12-22 17:36:17 +0000340 if (!T->hasTriggered()) continue;
George Karpenkov560b2442018-02-10 00:38:21 +0000341 bool WasRunning = T->isRunning();
342 if (WasRunning)
343 T->stopTimer();
344
Matthias Braundb39fd62016-11-18 19:43:24 +0000345 TimersToPrint.emplace_back(T->Time, T->Name, T->Description);
Matthias Braunac287032016-10-14 00:17:19 +0000346
George Karpenkov560b2442018-02-10 00:38:21 +0000347 if (WasRunning)
348 T->startTimer();
Chris Lattner4cd65712010-03-30 05:20:02 +0000349 }
Matthias Braundb39fd62016-11-18 19:43:24 +0000350}
351
352void TimerGroup::print(raw_ostream &OS) {
353 sys::SmartScopedLock<true> L(*TimerLock);
354
355 prepareToPrintList();
Chris Lattner4cd65712010-03-30 05:20:02 +0000356
357 // If any timers were started, print the group.
358 if (!TimersToPrint.empty())
359 PrintQueuedTimers(OS);
360}
Chris Lattner90fe73d2010-03-30 05:27:58 +0000361
Chris Lattner90fe73d2010-03-30 05:27:58 +0000362void TimerGroup::printAll(raw_ostream &OS) {
363 sys::SmartScopedLock<true> L(*TimerLock);
364
365 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
366 TG->print(OS);
367}
Matthias Braundb39fd62016-11-18 19:43:24 +0000368
369void TimerGroup::printJSONValue(raw_ostream &OS, const PrintRecord &R,
370 const char *suffix, double Value) {
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000371 assert(yaml::needsQuotes(Name) == yaml::QuotingType::None &&
Roman Lebedevddfefc32018-05-16 18:15:51 +0000372 "TimerGroup name should not need quotes");
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000373 assert(yaml::needsQuotes(R.Name) == yaml::QuotingType::None &&
Roman Lebedevddfefc32018-05-16 18:15:51 +0000374 "Timer name should not need quotes");
375 constexpr auto max_digits10 = std::numeric_limits<double>::max_digits10;
376 OS << "\t\"time." << Name << '.' << R.Name << suffix
377 << "\": " << format("%.*e", max_digits10 - 1, Value);
Matthias Braundb39fd62016-11-18 19:43:24 +0000378}
379
380const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) {
381 prepareToPrintList();
382 for (const PrintRecord &R : TimersToPrint) {
383 OS << delim;
384 delim = ",\n";
385
386 const TimeRecord &T = R.Time;
387 printJSONValue(OS, R, ".wall", T.getWallTime());
388 OS << delim;
389 printJSONValue(OS, R, ".user", T.getUserTime());
390 OS << delim;
391 printJSONValue(OS, R, ".sys", T.getSystemTime());
George Karpenkov560b2442018-02-10 00:38:21 +0000392 if (T.getMemUsed()) {
393 OS << delim;
Roman Lebedevc39ad982018-05-16 18:15:47 +0000394 printJSONValue(OS, R, ".mem", T.getMemUsed());
George Karpenkov560b2442018-02-10 00:38:21 +0000395 }
Matthias Braundb39fd62016-11-18 19:43:24 +0000396 }
397 TimersToPrint.clear();
398 return delim;
399}
400
401const char *TimerGroup::printAllJSONValues(raw_ostream &OS, const char *delim) {
402 sys::SmartScopedLock<true> L(*TimerLock);
403 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
404 delim = TG->printJSONValues(OS, delim);
405 return delim;
406}
407
408void TimerGroup::ConstructTimerLists() {
409 (void)*NamedGroupedTimers;
410}