blob: 0e634b392b5cfcfa112493b6a2f0105a3054c500 [file] [log] [blame]
Chris Lattner6dad11f2002-10-01 19:36:54 +00001//===-- Timer.cpp - Interval Timing Support -------------------------------===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukman10468d82005-04-21 22:55:34 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Chris Lattner6dad11f2002-10-01 19:36:54 +00008//
Matthias Braunac287032016-10-14 00:17:19 +00009/// \file Interval Timing implementation.
Chris Lattner6dad11f2002-10-01 19:36:54 +000010//
11//===----------------------------------------------------------------------===//
12
Reid Spencer7c16caa2004-09-01 22:55:40 +000013#include "llvm/Support/Timer.h"
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000014#include "llvm/ADT/Statistic.h"
Chris Lattner707431c2010-03-30 04:03:22 +000015#include "llvm/ADT/StringMap.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Support/CommandLine.h"
Benjamin Kramerd59664f2014-04-29 23:26:49 +000017#include "llvm/Support/FileSystem.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/Support/Format.h"
19#include "llvm/Support/ManagedStatic.h"
Zachary Turnerccbf3d02014-06-16 22:49:41 +000020#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/Support/Process.h"
Matthias Braundb39fd62016-11-18 19:43:24 +000022#include "llvm/Support/YAMLTraits.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000023#include "llvm/Support/raw_ostream.h"
Roman Lebedevddfefc32018-05-16 18:15:51 +000024#include <limits>
25
Chris Lattnerdd978ce2003-12-14 21:27:33 +000026using namespace llvm;
Chris Lattnerb0e59582003-05-09 20:05:44 +000027
Matthias Braunac287032016-10-14 00:17:19 +000028// This ugly hack is brought to you courtesy of constructor/destructor ordering
29// being unspecified by C++. Basically the problem is that a Statistic object
30// gets destroyed, which ends up calling 'GetLibSupportInfoOutputFile()'
31// (below), which calls this function. LibSupportInfoOutputFilename used to be
32// a global variable, but sometimes it would get destroyed before the Statistic,
33// causing havoc to ensue. We "fix" this by creating the string the first time
34// it is needed and never destroying it.
Chris Lattner8111c592006-10-04 21:52:35 +000035static ManagedStatic<std::string> LibSupportInfoOutputFilename;
Chris Lattnerc4bbc712003-07-31 19:38:34 +000036static std::string &getLibSupportInfoOutputFilename() {
Reid Spencer87ad6662004-12-14 03:55:21 +000037 return *LibSupportInfoOutputFilename;
Chris Lattnerc4bbc712003-07-31 19:38:34 +000038}
Chris Lattner6dad11f2002-10-01 19:36:54 +000039
Owen Andersone9b1beb2009-06-23 20:52:29 +000040static ManagedStatic<sys::SmartMutex<true> > TimerLock;
41
Chris Lattner2f752042003-01-30 23:08:50 +000042namespace {
Dan Gohmanc107d002008-04-23 23:15:23 +000043 static cl::opt<bool>
Chris Lattner2f752042003-01-30 23:08:50 +000044 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
45 "tracking (this may be slow)"),
46 cl::Hidden);
Chris Lattnerb0e59582003-05-09 20:05:44 +000047
Dan Gohmanc107d002008-04-23 23:15:23 +000048 static cl::opt<std::string, true>
Chris Lattnerf1afe32352003-08-01 22:15:15 +000049 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerb0e59582003-05-09 20:05:44 +000050 cl::desc("File to append -stats and -timer output to"),
Chris Lattnerc4bbc712003-07-31 19:38:34 +000051 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Alexander Kornienkof00654e2015-06-23 09:49:53 +000052}
Chris Lattner2f752042003-01-30 23:08:50 +000053
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000054std::unique_ptr<raw_fd_ostream> llvm::CreateInfoOutputFile() {
Chris Lattner39e56a02010-03-30 05:34:02 +000055 const std::string &OutputFilename = getLibSupportInfoOutputFilename();
56 if (OutputFilename.empty())
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000057 return llvm::make_unique<raw_fd_ostream>(2, false); // stderr.
Chris Lattner39e56a02010-03-30 05:34:02 +000058 if (OutputFilename == "-")
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000059 return llvm::make_unique<raw_fd_ostream>(1, false); // stdout.
60
Dan Gohman744c96d2010-05-19 01:21:34 +000061 // Append mode is used because the info output file is opened and closed
62 // each time -stats or -time-passes wants to print output to it. To
63 // compensate for this, the test-suite Makefiles have code to delete the
64 // info output file before running commands which write to it.
Rafael Espindola3fd1e992014-08-25 18:16:47 +000065 std::error_code EC;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000066 auto Result = llvm::make_unique<raw_fd_ostream>(
67 OutputFilename, EC, sys::fs::F_Append | sys::fs::F_Text);
Rafael Espindola3fd1e992014-08-25 18:16:47 +000068 if (!EC)
Chris Lattnerdcd7f922010-03-29 21:34:06 +000069 return Result;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000070
Chris Lattnerdcd7f922010-03-29 21:34:06 +000071 errs() << "Error opening info-output-file '"
Chris Lattner39e56a02010-03-30 05:34:02 +000072 << OutputFilename << " for appending!\n";
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000073 return llvm::make_unique<raw_fd_ostream>(2, false); // stderr.
Chris Lattnerdcd7f922010-03-29 21:34:06 +000074}
75
Benjamin Kramer2a441a52017-05-29 14:28:04 +000076namespace {
Benjamin Kramer74de0802017-05-29 20:56:27 +000077struct CreateDefaultTimerGroup {
78 static void *call() {
79 return new TimerGroup("misc", "Miscellaneous Ungrouped Timers");
80 }
81};
Benjamin Kramer2a441a52017-05-29 14:28:04 +000082} // namespace
Benjamin Kramer351779e2017-05-29 14:05:29 +000083static ManagedStatic<TimerGroup, CreateDefaultTimerGroup> DefaultTimerGroup;
84static TimerGroup *getDefaultTimerGroup() { return &*DefaultTimerGroup; }
Chris Lattner6dad11f2002-10-01 19:36:54 +000085
Chris Lattnerfafa57a2010-03-29 20:35:01 +000086//===----------------------------------------------------------------------===//
87// Timer Implementation
88//===----------------------------------------------------------------------===//
89
Matthias Braun9f15a792016-11-18 19:43:18 +000090void Timer::init(StringRef Name, StringRef Description) {
91 init(Name, Description, *getDefaultTimerGroup());
Chris Lattner6dad11f2002-10-01 19:36:54 +000092}
93
Matthias Braun9f15a792016-11-18 19:43:18 +000094void Timer::init(StringRef Name, StringRef Description, TimerGroup &tg) {
Craig Topper2617dcc2014-04-15 06:32:26 +000095 assert(!TG && "Timer already initialized");
Matthias Braun9f15a792016-11-18 19:43:18 +000096 this->Name.assign(Name.begin(), Name.end());
97 this->Description.assign(Description.begin(), Description.end());
Vedant Kumard1675862015-12-22 17:36:17 +000098 Running = Triggered = false;
Chris Lattner707431c2010-03-30 04:03:22 +000099 TG = &tg;
Chris Lattner9a608d22010-03-30 04:40:01 +0000100 TG->addTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000101}
102
Chris Lattner6dad11f2002-10-01 19:36:54 +0000103Timer::~Timer() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000104 if (!TG) return; // Never initialized, or already cleared.
Chris Lattner9a608d22010-03-30 04:40:01 +0000105 TG->removeTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000106}
107
Jeff Cohen1a26d152005-01-08 20:15:57 +0000108static inline size_t getMemUsage() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000109 if (!TrackSpace) return 0;
110 return sys::Process::GetMallocUsage();
Reid Spencerad7bdf72004-12-27 08:03:04 +0000111}
112
Chris Lattner707431c2010-03-30 04:03:22 +0000113TimeRecord TimeRecord::getCurrentTime(bool Start) {
Pavel Labath757ca882016-10-24 10:59:17 +0000114 using Seconds = std::chrono::duration<double, std::ratio<1>>;
Chris Lattner60683452004-06-07 19:34:51 +0000115 TimeRecord Result;
Pavel Labath757ca882016-10-24 10:59:17 +0000116 sys::TimePoint<> now;
117 std::chrono::nanoseconds user, sys;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000118
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000119 if (Start) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000120 Result.MemUsed = getMemUsage();
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000121 sys::Process::GetTimeUsage(now, user, sys);
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000122 } else {
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000123 sys::Process::GetTimeUsage(now, user, sys);
Chris Lattner4cd65712010-03-30 05:20:02 +0000124 Result.MemUsed = getMemUsage();
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000125 }
Reid Spencer27088812004-12-20 00:59:04 +0000126
Pavel Labath757ca882016-10-24 10:59:17 +0000127 Result.WallTime = Seconds(now.time_since_epoch()).count();
128 Result.UserTime = Seconds(user).count();
129 Result.SystemTime = Seconds(sys).count();
Chris Lattner6dad11f2002-10-01 19:36:54 +0000130 return Result;
131}
132
Chris Lattner6dad11f2002-10-01 19:36:54 +0000133void Timer::startTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000134 assert(!Running && "Cannot start a running timer");
135 Running = Triggered = true;
136 StartTime = TimeRecord::getCurrentTime(true);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000137}
138
139void Timer::stopTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000140 assert(Running && "Cannot stop a paused timer");
141 Running = false;
Chris Lattner707431c2010-03-30 04:03:22 +0000142 Time += TimeRecord::getCurrentTime(false);
Vedant Kumard1675862015-12-22 17:36:17 +0000143 Time -= StartTime;
144}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000145
Vedant Kumard1675862015-12-22 17:36:17 +0000146void Timer::clear() {
147 Running = Triggered = false;
148 Time = StartTime = TimeRecord();
Chris Lattner6dad11f2002-10-01 19:36:54 +0000149}
150
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000151static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner5092a6d2010-03-29 20:40:19 +0000152 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000153 OS << " ----- ";
Benjamin Kramercc863b22011-10-16 16:30:34 +0000154 else
155 OS << format(" %7.4f (%5.1f%%)", Val, Val*100/Total);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000156}
157
Chris Lattner707431c2010-03-30 04:03:22 +0000158void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
159 if (Total.getUserTime())
160 printVal(getUserTime(), Total.getUserTime(), OS);
161 if (Total.getSystemTime())
162 printVal(getSystemTime(), Total.getSystemTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000163 if (Total.getProcessTime())
164 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattner707431c2010-03-30 04:03:22 +0000165 printVal(getWallTime(), Total.getWallTime(), OS);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000166
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000167 OS << " ";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000168
Chris Lattner707431c2010-03-30 04:03:22 +0000169 if (Total.getMemUsed())
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000170 OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000171}
172
173
Chris Lattner8bfda652003-10-06 15:02:31 +0000174//===----------------------------------------------------------------------===//
175// NamedRegionTimer Implementation
176//===----------------------------------------------------------------------===//
177
Dan Gohmanb29cda92010-04-15 17:08:50 +0000178namespace {
179
Chris Lattner707431c2010-03-30 04:03:22 +0000180typedef StringMap<Timer> Name2TimerMap;
Chris Lattner4cd65712010-03-30 05:20:02 +0000181
182class Name2PairMap {
183 StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
184public:
185 ~Name2PairMap() {
186 for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
187 I = Map.begin(), E = Map.end(); I != E; ++I)
188 delete I->second.first;
189 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000190
Matthias Braun9f15a792016-11-18 19:43:18 +0000191 Timer &get(StringRef Name, StringRef Description, StringRef GroupName,
192 StringRef GroupDescription) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000193 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000194
Chris Lattner4cd65712010-03-30 05:20:02 +0000195 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000196
Chris Lattner4cd65712010-03-30 05:20:02 +0000197 if (!GroupEntry.first)
Matthias Braun9f15a792016-11-18 19:43:18 +0000198 GroupEntry.first = new TimerGroup(GroupName, GroupDescription);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000199
Chris Lattner4cd65712010-03-30 05:20:02 +0000200 Timer &T = GroupEntry.second[Name];
201 if (!T.isInitialized())
Matthias Braun9f15a792016-11-18 19:43:18 +0000202 T.init(Name, Description, *GroupEntry.first);
Chris Lattner4cd65712010-03-30 05:20:02 +0000203 return T;
204 }
205};
Dan Gohmanadec96f2008-07-14 18:19:29 +0000206
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000207}
Dan Gohmanb29cda92010-04-15 17:08:50 +0000208
Chris Lattner707431c2010-03-30 04:03:22 +0000209static ManagedStatic<Name2PairMap> NamedGroupedTimers;
Chris Lattner8bfda652003-10-06 15:02:31 +0000210
Matthias Braun9f15a792016-11-18 19:43:18 +0000211NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef Description,
212 StringRef GroupName,
213 StringRef GroupDescription, bool Enabled)
214 : TimeRegion(!Enabled ? nullptr
215 : &NamedGroupedTimers->get(Name, Description, GroupName,
216 GroupDescription)) {}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000217
Chris Lattner6dad11f2002-10-01 19:36:54 +0000218//===----------------------------------------------------------------------===//
219// TimerGroup Implementation
220//===----------------------------------------------------------------------===//
221
Matthias Braunac287032016-10-14 00:17:19 +0000222/// This is the global list of TimerGroups, maintained by the TimerGroup
223/// ctor/dtor and is protected by the TimerLock lock.
Craig Topperc10719f2014-04-07 04:17:22 +0000224static TimerGroup *TimerGroupList = nullptr;
Chris Lattner90fe73d2010-03-30 05:27:58 +0000225
Matthias Braun9f15a792016-11-18 19:43:18 +0000226TimerGroup::TimerGroup(StringRef Name, StringRef Description)
227 : Name(Name.begin(), Name.end()),
228 Description(Description.begin(), Description.end()) {
Chris Lattner90fe73d2010-03-30 05:27:58 +0000229 // Add the group to TimerGroupList.
230 sys::SmartScopedLock<true> L(*TimerLock);
231 if (TimerGroupList)
232 TimerGroupList->Prev = &Next;
233 Next = TimerGroupList;
234 Prev = &TimerGroupList;
235 TimerGroupList = this;
236}
237
Roman Lebedeve5921042018-05-16 18:16:01 +0000238TimerGroup::TimerGroup(StringRef Name, StringRef Description,
239 const StringMap<TimeRecord> &Records)
240 : TimerGroup(Name, Description) {
241 TimersToPrint.reserve(Records.size());
242 for (const auto &P : Records)
243 TimersToPrint.emplace_back(P.getValue(), P.getKey(), P.getKey());
244 assert(TimersToPrint.size() == Records.size() && "Size mismatch");
245}
246
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000247TimerGroup::~TimerGroup() {
248 // If the timer group is destroyed before the timers it owns, accumulate and
249 // print the timing data.
Craig Topper8d399f82014-04-09 04:20:00 +0000250 while (FirstTimer)
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000251 removeTimer(*FirstTimer);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000252
Chris Lattner90fe73d2010-03-30 05:27:58 +0000253 // Remove the group from the TimerGroupList.
254 sys::SmartScopedLock<true> L(*TimerLock);
255 *Prev = Next;
256 if (Next)
257 Next->Prev = Prev;
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000258}
259
260
Chris Lattner9a608d22010-03-30 04:40:01 +0000261void TimerGroup::removeTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000262 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000263
Chris Lattner9a608d22010-03-30 04:40:01 +0000264 // If the timer was started, move its data to TimersToPrint.
Vedant Kumard1675862015-12-22 17:36:17 +0000265 if (T.hasTriggered())
Matthias Braundb39fd62016-11-18 19:43:24 +0000266 TimersToPrint.emplace_back(T.Time, T.Name, T.Description);
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000267
Craig Topperc10719f2014-04-07 04:17:22 +0000268 T.TG = nullptr;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000269
Chris Lattner9a608d22010-03-30 04:40:01 +0000270 // Unlink the timer from our list.
271 *T.Prev = T.Next;
272 if (T.Next)
273 T.Next->Prev = T.Prev;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000274
Chris Lattner9a608d22010-03-30 04:40:01 +0000275 // Print the report when all timers in this group are destroyed if some of
276 // them were started.
Craig Topper8d399f82014-04-09 04:20:00 +0000277 if (FirstTimer || TimersToPrint.empty())
Chris Lattner9a608d22010-03-30 04:40:01 +0000278 return;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000279
280 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
Chris Lattner9a608d22010-03-30 04:40:01 +0000281 PrintQueuedTimers(*OutStream);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000282}
Brian Gaeke960707c2003-11-11 22:41:34 +0000283
Chris Lattner9a608d22010-03-30 04:40:01 +0000284void TimerGroup::addTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000285 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000286
Chris Lattner9a608d22010-03-30 04:40:01 +0000287 // Add the timer to our list.
288 if (FirstTimer)
289 FirstTimer->Prev = &T.Next;
290 T.Next = FirstTimer;
291 T.Prev = &FirstTimer;
292 FirstTimer = &T;
Owen Andersone9b1beb2009-06-23 20:52:29 +0000293}
294
Chris Lattner9a608d22010-03-30 04:40:01 +0000295void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
296 // Sort the timers in descending order by amount of time taken.
Fangrui Song0cac7262018-09-27 02:13:45 +0000297 llvm::sort(TimersToPrint);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000298
Chris Lattner9a608d22010-03-30 04:40:01 +0000299 TimeRecord Total;
Matthias Braundb39fd62016-11-18 19:43:24 +0000300 for (const PrintRecord &Record : TimersToPrint)
301 Total += Record.Time;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000302
Chris Lattner9a608d22010-03-30 04:40:01 +0000303 // Print out timing header.
304 OS << "===" << std::string(73, '-') << "===\n";
305 // Figure out how many spaces to indent TimerGroup name.
Matthias Braun9f15a792016-11-18 19:43:18 +0000306 unsigned Padding = (80-Description.length())/2;
Chris Lattner9a608d22010-03-30 04:40:01 +0000307 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
Matthias Braun9f15a792016-11-18 19:43:18 +0000308 OS.indent(Padding) << Description << '\n';
Chris Lattner9a608d22010-03-30 04:40:01 +0000309 OS << "===" << std::string(73, '-') << "===\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000310
Chris Lattner9a608d22010-03-30 04:40:01 +0000311 // If this is not an collection of ungrouped times, print the total time.
312 // Ungrouped timers don't really make sense to add up. We still print the
313 // TOTAL line to make the percentages make sense.
Erich Keanec4c31e22017-02-16 20:19:49 +0000314 if (this != getDefaultTimerGroup())
Benjamin Kramercc863b22011-10-16 16:30:34 +0000315 OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
316 Total.getProcessTime(), Total.getWallTime());
Chris Lattner9a608d22010-03-30 04:40:01 +0000317 OS << '\n';
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000318
Chris Lattner9a608d22010-03-30 04:40:01 +0000319 if (Total.getUserTime())
320 OS << " ---User Time---";
321 if (Total.getSystemTime())
322 OS << " --System Time--";
323 if (Total.getProcessTime())
324 OS << " --User+System--";
325 OS << " ---Wall Time---";
326 if (Total.getMemUsed())
327 OS << " ---Mem---";
328 OS << " --- Name ---\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000329
Chris Lattner9a608d22010-03-30 04:40:01 +0000330 // Loop through all of the timing data, printing it out.
Matthias Braundb39fd62016-11-18 19:43:24 +0000331 for (const PrintRecord &Record : make_range(TimersToPrint.rbegin(),
332 TimersToPrint.rend())) {
333 Record.Time.print(Total, OS);
334 OS << Record.Description << '\n';
Chris Lattner9a608d22010-03-30 04:40:01 +0000335 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000336
Chris Lattner9a608d22010-03-30 04:40:01 +0000337 Total.print(Total, OS);
338 OS << "Total\n\n";
339 OS.flush();
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000340
Chris Lattner9a608d22010-03-30 04:40:01 +0000341 TimersToPrint.clear();
Owen Andersone9b1beb2009-06-23 20:52:29 +0000342}
343
Matthias Braundb39fd62016-11-18 19:43:24 +0000344void TimerGroup::prepareToPrintList() {
Graydon Hoareeac6e872018-08-17 04:13:19 +0000345 // See if any of our timers were started, if so add them to TimersToPrint.
Chris Lattner4cd65712010-03-30 05:20:02 +0000346 for (Timer *T = FirstTimer; T; T = T->Next) {
Vedant Kumard1675862015-12-22 17:36:17 +0000347 if (!T->hasTriggered()) continue;
George Karpenkov560b2442018-02-10 00:38:21 +0000348 bool WasRunning = T->isRunning();
349 if (WasRunning)
350 T->stopTimer();
351
Matthias Braundb39fd62016-11-18 19:43:24 +0000352 TimersToPrint.emplace_back(T->Time, T->Name, T->Description);
Matthias Braunac287032016-10-14 00:17:19 +0000353
George Karpenkov560b2442018-02-10 00:38:21 +0000354 if (WasRunning)
355 T->startTimer();
Chris Lattner4cd65712010-03-30 05:20:02 +0000356 }
Matthias Braundb39fd62016-11-18 19:43:24 +0000357}
358
359void TimerGroup::print(raw_ostream &OS) {
360 sys::SmartScopedLock<true> L(*TimerLock);
361
362 prepareToPrintList();
Chris Lattner4cd65712010-03-30 05:20:02 +0000363
364 // If any timers were started, print the group.
365 if (!TimersToPrint.empty())
366 PrintQueuedTimers(OS);
367}
Chris Lattner90fe73d2010-03-30 05:27:58 +0000368
Graydon Hoareeac6e872018-08-17 04:13:19 +0000369void TimerGroup::clear() {
370 sys::SmartScopedLock<true> L(*TimerLock);
371 for (Timer *T = FirstTimer; T; T = T->Next)
372 T->clear();
373}
374
Chris Lattner90fe73d2010-03-30 05:27:58 +0000375void TimerGroup::printAll(raw_ostream &OS) {
376 sys::SmartScopedLock<true> L(*TimerLock);
377
378 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
379 TG->print(OS);
380}
Matthias Braundb39fd62016-11-18 19:43:24 +0000381
Graydon Hoareeac6e872018-08-17 04:13:19 +0000382void TimerGroup::clearAll() {
383 sys::SmartScopedLock<true> L(*TimerLock);
384 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
385 TG->clear();
386}
387
Matthias Braundb39fd62016-11-18 19:43:24 +0000388void TimerGroup::printJSONValue(raw_ostream &OS, const PrintRecord &R,
389 const char *suffix, double Value) {
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000390 assert(yaml::needsQuotes(Name) == yaml::QuotingType::None &&
Roman Lebedevddfefc32018-05-16 18:15:51 +0000391 "TimerGroup name should not need quotes");
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000392 assert(yaml::needsQuotes(R.Name) == yaml::QuotingType::None &&
Roman Lebedevddfefc32018-05-16 18:15:51 +0000393 "Timer name should not need quotes");
394 constexpr auto max_digits10 = std::numeric_limits<double>::max_digits10;
395 OS << "\t\"time." << Name << '.' << R.Name << suffix
396 << "\": " << format("%.*e", max_digits10 - 1, Value);
Matthias Braundb39fd62016-11-18 19:43:24 +0000397}
398
399const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) {
Roman Lebedevd9ade382018-05-16 18:15:56 +0000400 sys::SmartScopedLock<true> L(*TimerLock);
401
Matthias Braundb39fd62016-11-18 19:43:24 +0000402 prepareToPrintList();
403 for (const PrintRecord &R : TimersToPrint) {
404 OS << delim;
405 delim = ",\n";
406
407 const TimeRecord &T = R.Time;
408 printJSONValue(OS, R, ".wall", T.getWallTime());
409 OS << delim;
410 printJSONValue(OS, R, ".user", T.getUserTime());
411 OS << delim;
412 printJSONValue(OS, R, ".sys", T.getSystemTime());
George Karpenkov560b2442018-02-10 00:38:21 +0000413 if (T.getMemUsed()) {
414 OS << delim;
Roman Lebedevc39ad982018-05-16 18:15:47 +0000415 printJSONValue(OS, R, ".mem", T.getMemUsed());
George Karpenkov560b2442018-02-10 00:38:21 +0000416 }
Matthias Braundb39fd62016-11-18 19:43:24 +0000417 }
418 TimersToPrint.clear();
419 return delim;
420}
421
422const char *TimerGroup::printAllJSONValues(raw_ostream &OS, const char *delim) {
423 sys::SmartScopedLock<true> L(*TimerLock);
424 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
425 delim = TG->printJSONValues(OS, delim);
426 return delim;
427}
428
429void TimerGroup::ConstructTimerLists() {
430 (void)*NamedGroupedTimers;
431}