blob: 2a7ff1eaaf6385148d1ad6dd7a34093d8f4593ea [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"
Daniel Sanderse1414d12019-02-19 18:18:31 +000022#include "llvm/Support/Signposts.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
Daniel Sanderse1414d12019-02-19 18:18:31 +000043/// Allows llvm::Timer to emit signposts when supported.
44static ManagedStatic<SignpostEmitter> Signposts;
45
Chris Lattner2f752042003-01-30 23:08:50 +000046namespace {
Dan Gohmanc107d002008-04-23 23:15:23 +000047 static cl::opt<bool>
Chris Lattner2f752042003-01-30 23:08:50 +000048 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
49 "tracking (this may be slow)"),
50 cl::Hidden);
Chris Lattnerb0e59582003-05-09 20:05:44 +000051
Dan Gohmanc107d002008-04-23 23:15:23 +000052 static cl::opt<std::string, true>
Chris Lattnerf1afe32352003-08-01 22:15:15 +000053 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
Chris Lattnerb0e59582003-05-09 20:05:44 +000054 cl::desc("File to append -stats and -timer output to"),
Chris Lattnerc4bbc712003-07-31 19:38:34 +000055 cl::Hidden, cl::location(getLibSupportInfoOutputFilename()));
Alexander Kornienkof00654e2015-06-23 09:49:53 +000056}
Chris Lattner2f752042003-01-30 23:08:50 +000057
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000058std::unique_ptr<raw_fd_ostream> llvm::CreateInfoOutputFile() {
Chris Lattner39e56a02010-03-30 05:34:02 +000059 const std::string &OutputFilename = getLibSupportInfoOutputFilename();
60 if (OutputFilename.empty())
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000061 return llvm::make_unique<raw_fd_ostream>(2, false); // stderr.
Chris Lattner39e56a02010-03-30 05:34:02 +000062 if (OutputFilename == "-")
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000063 return llvm::make_unique<raw_fd_ostream>(1, false); // stdout.
64
Dan Gohman744c96d2010-05-19 01:21:34 +000065 // Append mode is used because the info output file is opened and closed
66 // each time -stats or -time-passes wants to print output to it. To
67 // compensate for this, the test-suite Makefiles have code to delete the
68 // info output file before running commands which write to it.
Rafael Espindola3fd1e992014-08-25 18:16:47 +000069 std::error_code EC;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000070 auto Result = llvm::make_unique<raw_fd_ostream>(
71 OutputFilename, EC, sys::fs::F_Append | sys::fs::F_Text);
Rafael Espindola3fd1e992014-08-25 18:16:47 +000072 if (!EC)
Chris Lattnerdcd7f922010-03-29 21:34:06 +000073 return Result;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000074
Chris Lattnerdcd7f922010-03-29 21:34:06 +000075 errs() << "Error opening info-output-file '"
Chris Lattner39e56a02010-03-30 05:34:02 +000076 << OutputFilename << " for appending!\n";
Rafael Espindolab94ab5f2015-12-16 22:28:34 +000077 return llvm::make_unique<raw_fd_ostream>(2, false); // stderr.
Chris Lattnerdcd7f922010-03-29 21:34:06 +000078}
79
Benjamin Kramer2a441a52017-05-29 14:28:04 +000080namespace {
Benjamin Kramer74de0802017-05-29 20:56:27 +000081struct CreateDefaultTimerGroup {
82 static void *call() {
83 return new TimerGroup("misc", "Miscellaneous Ungrouped Timers");
84 }
85};
Benjamin Kramer2a441a52017-05-29 14:28:04 +000086} // namespace
Benjamin Kramer351779e2017-05-29 14:05:29 +000087static ManagedStatic<TimerGroup, CreateDefaultTimerGroup> DefaultTimerGroup;
88static TimerGroup *getDefaultTimerGroup() { return &*DefaultTimerGroup; }
Chris Lattner6dad11f2002-10-01 19:36:54 +000089
Chris Lattnerfafa57a2010-03-29 20:35:01 +000090//===----------------------------------------------------------------------===//
91// Timer Implementation
92//===----------------------------------------------------------------------===//
93
Matthias Braun9f15a792016-11-18 19:43:18 +000094void Timer::init(StringRef Name, StringRef Description) {
95 init(Name, Description, *getDefaultTimerGroup());
Chris Lattner6dad11f2002-10-01 19:36:54 +000096}
97
Matthias Braun9f15a792016-11-18 19:43:18 +000098void Timer::init(StringRef Name, StringRef Description, TimerGroup &tg) {
Craig Topper2617dcc2014-04-15 06:32:26 +000099 assert(!TG && "Timer already initialized");
Matthias Braun9f15a792016-11-18 19:43:18 +0000100 this->Name.assign(Name.begin(), Name.end());
101 this->Description.assign(Description.begin(), Description.end());
Vedant Kumard1675862015-12-22 17:36:17 +0000102 Running = Triggered = false;
Chris Lattner707431c2010-03-30 04:03:22 +0000103 TG = &tg;
Chris Lattner9a608d22010-03-30 04:40:01 +0000104 TG->addTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000105}
106
Chris Lattner6dad11f2002-10-01 19:36:54 +0000107Timer::~Timer() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000108 if (!TG) return; // Never initialized, or already cleared.
Chris Lattner9a608d22010-03-30 04:40:01 +0000109 TG->removeTimer(*this);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000110}
111
Jeff Cohen1a26d152005-01-08 20:15:57 +0000112static inline size_t getMemUsage() {
Chris Lattner4cd65712010-03-30 05:20:02 +0000113 if (!TrackSpace) return 0;
114 return sys::Process::GetMallocUsage();
Reid Spencerad7bdf72004-12-27 08:03:04 +0000115}
116
Chris Lattner707431c2010-03-30 04:03:22 +0000117TimeRecord TimeRecord::getCurrentTime(bool Start) {
Pavel Labath757ca882016-10-24 10:59:17 +0000118 using Seconds = std::chrono::duration<double, std::ratio<1>>;
Chris Lattner60683452004-06-07 19:34:51 +0000119 TimeRecord Result;
Pavel Labath757ca882016-10-24 10:59:17 +0000120 sys::TimePoint<> now;
121 std::chrono::nanoseconds user, sys;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000122
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000123 if (Start) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000124 Result.MemUsed = getMemUsage();
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000125 sys::Process::GetTimeUsage(now, user, sys);
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000126 } else {
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000127 sys::Process::GetTimeUsage(now, user, sys);
Chris Lattner4cd65712010-03-30 05:20:02 +0000128 Result.MemUsed = getMemUsage();
Reid Spencer92e8a5a2004-12-20 21:44:27 +0000129 }
Reid Spencer27088812004-12-20 00:59:04 +0000130
Pavel Labath757ca882016-10-24 10:59:17 +0000131 Result.WallTime = Seconds(now.time_since_epoch()).count();
132 Result.UserTime = Seconds(user).count();
133 Result.SystemTime = Seconds(sys).count();
Chris Lattner6dad11f2002-10-01 19:36:54 +0000134 return Result;
135}
136
Chris Lattner6dad11f2002-10-01 19:36:54 +0000137void Timer::startTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000138 assert(!Running && "Cannot start a running timer");
139 Running = Triggered = true;
Daniel Sanderse1414d12019-02-19 18:18:31 +0000140 Signposts->startTimerInterval(this);
Vedant Kumard1675862015-12-22 17:36:17 +0000141 StartTime = TimeRecord::getCurrentTime(true);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000142}
143
144void Timer::stopTimer() {
Vedant Kumard1675862015-12-22 17:36:17 +0000145 assert(Running && "Cannot stop a paused timer");
146 Running = false;
Chris Lattner707431c2010-03-30 04:03:22 +0000147 Time += TimeRecord::getCurrentTime(false);
Vedant Kumard1675862015-12-22 17:36:17 +0000148 Time -= StartTime;
Daniel Sanderse1414d12019-02-19 18:18:31 +0000149 Signposts->endTimerInterval(this);
Vedant Kumard1675862015-12-22 17:36:17 +0000150}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000151
Vedant Kumard1675862015-12-22 17:36:17 +0000152void Timer::clear() {
153 Running = Triggered = false;
154 Time = StartTime = TimeRecord();
Chris Lattner6dad11f2002-10-01 19:36:54 +0000155}
156
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000157static void printVal(double Val, double Total, raw_ostream &OS) {
Chris Lattner5092a6d2010-03-29 20:40:19 +0000158 if (Total < 1e-7) // Avoid dividing by zero.
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000159 OS << " ----- ";
Benjamin Kramercc863b22011-10-16 16:30:34 +0000160 else
161 OS << format(" %7.4f (%5.1f%%)", Val, Val*100/Total);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000162}
163
Chris Lattner707431c2010-03-30 04:03:22 +0000164void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
165 if (Total.getUserTime())
166 printVal(getUserTime(), Total.getUserTime(), OS);
167 if (Total.getSystemTime())
168 printVal(getSystemTime(), Total.getSystemTime(), OS);
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000169 if (Total.getProcessTime())
170 printVal(getProcessTime(), Total.getProcessTime(), OS);
Chris Lattner707431c2010-03-30 04:03:22 +0000171 printVal(getWallTime(), Total.getWallTime(), OS);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000172
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000173 OS << " ";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000174
Chris Lattner707431c2010-03-30 04:03:22 +0000175 if (Total.getMemUsed())
Benjamin Kramerf3da5292011-11-05 08:57:40 +0000176 OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
Chris Lattnerfafa57a2010-03-29 20:35:01 +0000177}
178
179
Chris Lattner8bfda652003-10-06 15:02:31 +0000180//===----------------------------------------------------------------------===//
181// NamedRegionTimer Implementation
182//===----------------------------------------------------------------------===//
183
Dan Gohmanb29cda92010-04-15 17:08:50 +0000184namespace {
185
Chris Lattner707431c2010-03-30 04:03:22 +0000186typedef StringMap<Timer> Name2TimerMap;
Chris Lattner4cd65712010-03-30 05:20:02 +0000187
188class Name2PairMap {
189 StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
190public:
191 ~Name2PairMap() {
192 for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
193 I = Map.begin(), E = Map.end(); I != E; ++I)
194 delete I->second.first;
195 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000196
Matthias Braun9f15a792016-11-18 19:43:18 +0000197 Timer &get(StringRef Name, StringRef Description, StringRef GroupName,
198 StringRef GroupDescription) {
Chris Lattner4cd65712010-03-30 05:20:02 +0000199 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000200
Chris Lattner4cd65712010-03-30 05:20:02 +0000201 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000202
Chris Lattner4cd65712010-03-30 05:20:02 +0000203 if (!GroupEntry.first)
Matthias Braun9f15a792016-11-18 19:43:18 +0000204 GroupEntry.first = new TimerGroup(GroupName, GroupDescription);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000205
Chris Lattner4cd65712010-03-30 05:20:02 +0000206 Timer &T = GroupEntry.second[Name];
207 if (!T.isInitialized())
Matthias Braun9f15a792016-11-18 19:43:18 +0000208 T.init(Name, Description, *GroupEntry.first);
Chris Lattner4cd65712010-03-30 05:20:02 +0000209 return T;
210 }
211};
Dan Gohmanadec96f2008-07-14 18:19:29 +0000212
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000213}
Dan Gohmanb29cda92010-04-15 17:08:50 +0000214
Chris Lattner707431c2010-03-30 04:03:22 +0000215static ManagedStatic<Name2PairMap> NamedGroupedTimers;
Chris Lattner8bfda652003-10-06 15:02:31 +0000216
Matthias Braun9f15a792016-11-18 19:43:18 +0000217NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef Description,
218 StringRef GroupName,
219 StringRef GroupDescription, bool Enabled)
220 : TimeRegion(!Enabled ? nullptr
221 : &NamedGroupedTimers->get(Name, Description, GroupName,
222 GroupDescription)) {}
Chris Lattnerf96a2182002-11-18 21:47:09 +0000223
Chris Lattner6dad11f2002-10-01 19:36:54 +0000224//===----------------------------------------------------------------------===//
225// TimerGroup Implementation
226//===----------------------------------------------------------------------===//
227
Matthias Braunac287032016-10-14 00:17:19 +0000228/// This is the global list of TimerGroups, maintained by the TimerGroup
229/// ctor/dtor and is protected by the TimerLock lock.
Craig Topperc10719f2014-04-07 04:17:22 +0000230static TimerGroup *TimerGroupList = nullptr;
Chris Lattner90fe73d2010-03-30 05:27:58 +0000231
Matthias Braun9f15a792016-11-18 19:43:18 +0000232TimerGroup::TimerGroup(StringRef Name, StringRef Description)
233 : Name(Name.begin(), Name.end()),
234 Description(Description.begin(), Description.end()) {
Chris Lattner90fe73d2010-03-30 05:27:58 +0000235 // Add the group to TimerGroupList.
236 sys::SmartScopedLock<true> L(*TimerLock);
237 if (TimerGroupList)
238 TimerGroupList->Prev = &Next;
239 Next = TimerGroupList;
240 Prev = &TimerGroupList;
241 TimerGroupList = this;
242}
243
Roman Lebedeve5921042018-05-16 18:16:01 +0000244TimerGroup::TimerGroup(StringRef Name, StringRef Description,
245 const StringMap<TimeRecord> &Records)
246 : TimerGroup(Name, Description) {
247 TimersToPrint.reserve(Records.size());
248 for (const auto &P : Records)
249 TimersToPrint.emplace_back(P.getValue(), P.getKey(), P.getKey());
250 assert(TimersToPrint.size() == Records.size() && "Size mismatch");
251}
252
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000253TimerGroup::~TimerGroup() {
254 // If the timer group is destroyed before the timers it owns, accumulate and
255 // print the timing data.
Craig Topper8d399f82014-04-09 04:20:00 +0000256 while (FirstTimer)
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000257 removeTimer(*FirstTimer);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000258
Chris Lattner90fe73d2010-03-30 05:27:58 +0000259 // Remove the group from the TimerGroupList.
260 sys::SmartScopedLock<true> L(*TimerLock);
261 *Prev = Next;
262 if (Next)
263 Next->Prev = Prev;
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000264}
265
266
Chris Lattner9a608d22010-03-30 04:40:01 +0000267void TimerGroup::removeTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000268 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000269
Chris Lattner9a608d22010-03-30 04:40:01 +0000270 // If the timer was started, move its data to TimersToPrint.
Vedant Kumard1675862015-12-22 17:36:17 +0000271 if (T.hasTriggered())
Matthias Braundb39fd62016-11-18 19:43:24 +0000272 TimersToPrint.emplace_back(T.Time, T.Name, T.Description);
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000273
Craig Topperc10719f2014-04-07 04:17:22 +0000274 T.TG = nullptr;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000275
Chris Lattner9a608d22010-03-30 04:40:01 +0000276 // Unlink the timer from our list.
277 *T.Prev = T.Next;
278 if (T.Next)
279 T.Next->Prev = T.Prev;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000280
Chris Lattner9a608d22010-03-30 04:40:01 +0000281 // Print the report when all timers in this group are destroyed if some of
282 // them were started.
Craig Topper8d399f82014-04-09 04:20:00 +0000283 if (FirstTimer || TimersToPrint.empty())
Chris Lattner9a608d22010-03-30 04:40:01 +0000284 return;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000285
286 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
Chris Lattner9a608d22010-03-30 04:40:01 +0000287 PrintQueuedTimers(*OutStream);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000288}
Brian Gaeke960707c2003-11-11 22:41:34 +0000289
Chris Lattner9a608d22010-03-30 04:40:01 +0000290void TimerGroup::addTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000291 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000292
Chris Lattner9a608d22010-03-30 04:40:01 +0000293 // Add the timer to our list.
294 if (FirstTimer)
295 FirstTimer->Prev = &T.Next;
296 T.Next = FirstTimer;
297 T.Prev = &FirstTimer;
298 FirstTimer = &T;
Owen Andersone9b1beb2009-06-23 20:52:29 +0000299}
300
Chris Lattner9a608d22010-03-30 04:40:01 +0000301void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
302 // Sort the timers in descending order by amount of time taken.
Fangrui Song0cac7262018-09-27 02:13:45 +0000303 llvm::sort(TimersToPrint);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000304
Chris Lattner9a608d22010-03-30 04:40:01 +0000305 TimeRecord Total;
Matthias Braundb39fd62016-11-18 19:43:24 +0000306 for (const PrintRecord &Record : TimersToPrint)
307 Total += Record.Time;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000308
Chris Lattner9a608d22010-03-30 04:40:01 +0000309 // Print out timing header.
310 OS << "===" << std::string(73, '-') << "===\n";
311 // Figure out how many spaces to indent TimerGroup name.
Matthias Braun9f15a792016-11-18 19:43:18 +0000312 unsigned Padding = (80-Description.length())/2;
Chris Lattner9a608d22010-03-30 04:40:01 +0000313 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
Matthias Braun9f15a792016-11-18 19:43:18 +0000314 OS.indent(Padding) << Description << '\n';
Chris Lattner9a608d22010-03-30 04:40:01 +0000315 OS << "===" << std::string(73, '-') << "===\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000316
Chris Lattner9a608d22010-03-30 04:40:01 +0000317 // If this is not an collection of ungrouped times, print the total time.
318 // Ungrouped timers don't really make sense to add up. We still print the
319 // TOTAL line to make the percentages make sense.
Erich Keanec4c31e22017-02-16 20:19:49 +0000320 if (this != getDefaultTimerGroup())
Benjamin Kramercc863b22011-10-16 16:30:34 +0000321 OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
322 Total.getProcessTime(), Total.getWallTime());
Chris Lattner9a608d22010-03-30 04:40:01 +0000323 OS << '\n';
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000324
Chris Lattner9a608d22010-03-30 04:40:01 +0000325 if (Total.getUserTime())
326 OS << " ---User Time---";
327 if (Total.getSystemTime())
328 OS << " --System Time--";
329 if (Total.getProcessTime())
330 OS << " --User+System--";
331 OS << " ---Wall Time---";
332 if (Total.getMemUsed())
333 OS << " ---Mem---";
334 OS << " --- Name ---\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000335
Chris Lattner9a608d22010-03-30 04:40:01 +0000336 // Loop through all of the timing data, printing it out.
Matthias Braundb39fd62016-11-18 19:43:24 +0000337 for (const PrintRecord &Record : make_range(TimersToPrint.rbegin(),
338 TimersToPrint.rend())) {
339 Record.Time.print(Total, OS);
340 OS << Record.Description << '\n';
Chris Lattner9a608d22010-03-30 04:40:01 +0000341 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000342
Chris Lattner9a608d22010-03-30 04:40:01 +0000343 Total.print(Total, OS);
344 OS << "Total\n\n";
345 OS.flush();
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000346
Chris Lattner9a608d22010-03-30 04:40:01 +0000347 TimersToPrint.clear();
Owen Andersone9b1beb2009-06-23 20:52:29 +0000348}
349
Fedor Sergeevec743782019-03-22 23:11:08 +0000350void TimerGroup::prepareToPrintList(bool ResetTime) {
Graydon Hoareeac6e872018-08-17 04:13:19 +0000351 // See if any of our timers were started, if so add them to TimersToPrint.
Chris Lattner4cd65712010-03-30 05:20:02 +0000352 for (Timer *T = FirstTimer; T; T = T->Next) {
Vedant Kumard1675862015-12-22 17:36:17 +0000353 if (!T->hasTriggered()) continue;
George Karpenkov560b2442018-02-10 00:38:21 +0000354 bool WasRunning = T->isRunning();
355 if (WasRunning)
356 T->stopTimer();
357
Matthias Braundb39fd62016-11-18 19:43:24 +0000358 TimersToPrint.emplace_back(T->Time, T->Name, T->Description);
Matthias Braunac287032016-10-14 00:17:19 +0000359
Fedor Sergeevec743782019-03-22 23:11:08 +0000360 if (ResetTime)
361 T->clear();
362
George Karpenkov560b2442018-02-10 00:38:21 +0000363 if (WasRunning)
364 T->startTimer();
Chris Lattner4cd65712010-03-30 05:20:02 +0000365 }
Matthias Braundb39fd62016-11-18 19:43:24 +0000366}
367
Fedor Sergeevec743782019-03-22 23:11:08 +0000368void TimerGroup::print(raw_ostream &OS, bool ResetAfterPrint) {
369 {
370 // After preparing the timers we can free the lock
371 sys::SmartScopedLock<true> L(*TimerLock);
372 prepareToPrintList(ResetAfterPrint);
373 }
Chris Lattner4cd65712010-03-30 05:20:02 +0000374
375 // If any timers were started, print the group.
376 if (!TimersToPrint.empty())
377 PrintQueuedTimers(OS);
378}
Chris Lattner90fe73d2010-03-30 05:27:58 +0000379
Graydon Hoareeac6e872018-08-17 04:13:19 +0000380void TimerGroup::clear() {
381 sys::SmartScopedLock<true> L(*TimerLock);
382 for (Timer *T = FirstTimer; T; T = T->Next)
383 T->clear();
384}
385
Chris Lattner90fe73d2010-03-30 05:27:58 +0000386void TimerGroup::printAll(raw_ostream &OS) {
387 sys::SmartScopedLock<true> L(*TimerLock);
388
389 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
390 TG->print(OS);
391}
Matthias Braundb39fd62016-11-18 19:43:24 +0000392
Graydon Hoareeac6e872018-08-17 04:13:19 +0000393void TimerGroup::clearAll() {
394 sys::SmartScopedLock<true> L(*TimerLock);
395 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
396 TG->clear();
397}
398
Matthias Braundb39fd62016-11-18 19:43:24 +0000399void TimerGroup::printJSONValue(raw_ostream &OS, const PrintRecord &R,
400 const char *suffix, double Value) {
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000401 assert(yaml::needsQuotes(Name) == yaml::QuotingType::None &&
Roman Lebedevddfefc32018-05-16 18:15:51 +0000402 "TimerGroup name should not need quotes");
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000403 assert(yaml::needsQuotes(R.Name) == yaml::QuotingType::None &&
Roman Lebedevddfefc32018-05-16 18:15:51 +0000404 "Timer name should not need quotes");
405 constexpr auto max_digits10 = std::numeric_limits<double>::max_digits10;
406 OS << "\t\"time." << Name << '.' << R.Name << suffix
407 << "\": " << format("%.*e", max_digits10 - 1, Value);
Matthias Braundb39fd62016-11-18 19:43:24 +0000408}
409
410const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) {
Roman Lebedevd9ade382018-05-16 18:15:56 +0000411 sys::SmartScopedLock<true> L(*TimerLock);
412
Fedor Sergeevec743782019-03-22 23:11:08 +0000413 prepareToPrintList(false);
Matthias Braundb39fd62016-11-18 19:43:24 +0000414 for (const PrintRecord &R : TimersToPrint) {
415 OS << delim;
416 delim = ",\n";
417
418 const TimeRecord &T = R.Time;
419 printJSONValue(OS, R, ".wall", T.getWallTime());
420 OS << delim;
421 printJSONValue(OS, R, ".user", T.getUserTime());
422 OS << delim;
423 printJSONValue(OS, R, ".sys", T.getSystemTime());
George Karpenkov560b2442018-02-10 00:38:21 +0000424 if (T.getMemUsed()) {
425 OS << delim;
Roman Lebedevc39ad982018-05-16 18:15:47 +0000426 printJSONValue(OS, R, ".mem", T.getMemUsed());
George Karpenkov560b2442018-02-10 00:38:21 +0000427 }
Matthias Braundb39fd62016-11-18 19:43:24 +0000428 }
429 TimersToPrint.clear();
430 return delim;
431}
432
433const char *TimerGroup::printAllJSONValues(raw_ostream &OS, const char *delim) {
434 sys::SmartScopedLock<true> L(*TimerLock);
435 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
436 delim = TG->printJSONValues(OS, delim);
437 return delim;
438}
439
440void TimerGroup::ConstructTimerLists() {
441 (void)*NamedGroupedTimers;
442}