blob: 3821f487a8fb6f3b8c6ad2470062cead93c96d97 [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
Roman Lebedeve5921042018-05-16 18:16:01 +0000239TimerGroup::TimerGroup(StringRef Name, StringRef Description,
240 const StringMap<TimeRecord> &Records)
241 : TimerGroup(Name, Description) {
242 TimersToPrint.reserve(Records.size());
243 for (const auto &P : Records)
244 TimersToPrint.emplace_back(P.getValue(), P.getKey(), P.getKey());
245 assert(TimersToPrint.size() == Records.size() && "Size mismatch");
246}
247
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000248TimerGroup::~TimerGroup() {
249 // If the timer group is destroyed before the timers it owns, accumulate and
250 // print the timing data.
Craig Topper8d399f82014-04-09 04:20:00 +0000251 while (FirstTimer)
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000252 removeTimer(*FirstTimer);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000253
Chris Lattner90fe73d2010-03-30 05:27:58 +0000254 // Remove the group from the TimerGroupList.
255 sys::SmartScopedLock<true> L(*TimerLock);
256 *Prev = Next;
257 if (Next)
258 Next->Prev = Prev;
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000259}
260
261
Chris Lattner9a608d22010-03-30 04:40:01 +0000262void TimerGroup::removeTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000263 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000264
Chris Lattner9a608d22010-03-30 04:40:01 +0000265 // If the timer was started, move its data to TimersToPrint.
Vedant Kumard1675862015-12-22 17:36:17 +0000266 if (T.hasTriggered())
Matthias Braundb39fd62016-11-18 19:43:24 +0000267 TimersToPrint.emplace_back(T.Time, T.Name, T.Description);
Chris Lattnerdcd68b72010-03-30 04:58:26 +0000268
Craig Topperc10719f2014-04-07 04:17:22 +0000269 T.TG = nullptr;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000270
Chris Lattner9a608d22010-03-30 04:40:01 +0000271 // Unlink the timer from our list.
272 *T.Prev = T.Next;
273 if (T.Next)
274 T.Next->Prev = T.Prev;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000275
Chris Lattner9a608d22010-03-30 04:40:01 +0000276 // Print the report when all timers in this group are destroyed if some of
277 // them were started.
Craig Topper8d399f82014-04-09 04:20:00 +0000278 if (FirstTimer || TimersToPrint.empty())
Chris Lattner9a608d22010-03-30 04:40:01 +0000279 return;
Rafael Espindolab94ab5f2015-12-16 22:28:34 +0000280
281 std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
Chris Lattner9a608d22010-03-30 04:40:01 +0000282 PrintQueuedTimers(*OutStream);
Chris Lattner6dad11f2002-10-01 19:36:54 +0000283}
Brian Gaeke960707c2003-11-11 22:41:34 +0000284
Chris Lattner9a608d22010-03-30 04:40:01 +0000285void TimerGroup::addTimer(Timer &T) {
Owen Anderson5c96ef72009-07-07 18:33:04 +0000286 sys::SmartScopedLock<true> L(*TimerLock);
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000287
Chris Lattner9a608d22010-03-30 04:40:01 +0000288 // Add the timer to our list.
289 if (FirstTimer)
290 FirstTimer->Prev = &T.Next;
291 T.Next = FirstTimer;
292 T.Prev = &FirstTimer;
293 FirstTimer = &T;
Owen Andersone9b1beb2009-06-23 20:52:29 +0000294}
295
Chris Lattner9a608d22010-03-30 04:40:01 +0000296void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
297 // Sort the timers in descending order by amount of time taken.
Mandeep Singh Grang68ab4012018-04-08 16:46:22 +0000298 llvm::sort(TimersToPrint.begin(), TimersToPrint.end());
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000299
Chris Lattner9a608d22010-03-30 04:40:01 +0000300 TimeRecord Total;
Matthias Braundb39fd62016-11-18 19:43:24 +0000301 for (const PrintRecord &Record : TimersToPrint)
302 Total += Record.Time;
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000303
Chris Lattner9a608d22010-03-30 04:40:01 +0000304 // Print out timing header.
305 OS << "===" << std::string(73, '-') << "===\n";
306 // Figure out how many spaces to indent TimerGroup name.
Matthias Braun9f15a792016-11-18 19:43:18 +0000307 unsigned Padding = (80-Description.length())/2;
Chris Lattner9a608d22010-03-30 04:40:01 +0000308 if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
Matthias Braun9f15a792016-11-18 19:43:18 +0000309 OS.indent(Padding) << Description << '\n';
Chris Lattner9a608d22010-03-30 04:40:01 +0000310 OS << "===" << std::string(73, '-') << "===\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000311
Chris Lattner9a608d22010-03-30 04:40:01 +0000312 // If this is not an collection of ungrouped times, print the total time.
313 // Ungrouped timers don't really make sense to add up. We still print the
314 // TOTAL line to make the percentages make sense.
Erich Keanec4c31e22017-02-16 20:19:49 +0000315 if (this != getDefaultTimerGroup())
Benjamin Kramercc863b22011-10-16 16:30:34 +0000316 OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
317 Total.getProcessTime(), Total.getWallTime());
Chris Lattner9a608d22010-03-30 04:40:01 +0000318 OS << '\n';
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000319
Chris Lattner9a608d22010-03-30 04:40:01 +0000320 if (Total.getUserTime())
321 OS << " ---User Time---";
322 if (Total.getSystemTime())
323 OS << " --System Time--";
324 if (Total.getProcessTime())
325 OS << " --User+System--";
326 OS << " ---Wall Time---";
327 if (Total.getMemUsed())
328 OS << " ---Mem---";
329 OS << " --- Name ---\n";
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000330
Chris Lattner9a608d22010-03-30 04:40:01 +0000331 // Loop through all of the timing data, printing it out.
Matthias Braundb39fd62016-11-18 19:43:24 +0000332 for (const PrintRecord &Record : make_range(TimersToPrint.rbegin(),
333 TimersToPrint.rend())) {
334 Record.Time.print(Total, OS);
335 OS << Record.Description << '\n';
Chris Lattner9a608d22010-03-30 04:40:01 +0000336 }
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000337
Chris Lattner9a608d22010-03-30 04:40:01 +0000338 Total.print(Total, OS);
339 OS << "Total\n\n";
340 OS.flush();
Alina Sbirlea8c9ad102016-06-03 19:20:37 +0000341
Chris Lattner9a608d22010-03-30 04:40:01 +0000342 TimersToPrint.clear();
Owen Andersone9b1beb2009-06-23 20:52:29 +0000343}
344
Matthias Braundb39fd62016-11-18 19:43:24 +0000345void TimerGroup::prepareToPrintList() {
Graydon Hoareeac6e872018-08-17 04:13:19 +0000346 // See if any of our timers were started, if so add them to TimersToPrint.
Chris Lattner4cd65712010-03-30 05:20:02 +0000347 for (Timer *T = FirstTimer; T; T = T->Next) {
Vedant Kumard1675862015-12-22 17:36:17 +0000348 if (!T->hasTriggered()) continue;
George Karpenkov560b2442018-02-10 00:38:21 +0000349 bool WasRunning = T->isRunning();
350 if (WasRunning)
351 T->stopTimer();
352
Matthias Braundb39fd62016-11-18 19:43:24 +0000353 TimersToPrint.emplace_back(T->Time, T->Name, T->Description);
Matthias Braunac287032016-10-14 00:17:19 +0000354
George Karpenkov560b2442018-02-10 00:38:21 +0000355 if (WasRunning)
356 T->startTimer();
Chris Lattner4cd65712010-03-30 05:20:02 +0000357 }
Matthias Braundb39fd62016-11-18 19:43:24 +0000358}
359
360void TimerGroup::print(raw_ostream &OS) {
361 sys::SmartScopedLock<true> L(*TimerLock);
362
363 prepareToPrintList();
Chris Lattner4cd65712010-03-30 05:20:02 +0000364
365 // If any timers were started, print the group.
366 if (!TimersToPrint.empty())
367 PrintQueuedTimers(OS);
368}
Chris Lattner90fe73d2010-03-30 05:27:58 +0000369
Graydon Hoareeac6e872018-08-17 04:13:19 +0000370void TimerGroup::clear() {
371 sys::SmartScopedLock<true> L(*TimerLock);
372 for (Timer *T = FirstTimer; T; T = T->Next)
373 T->clear();
374}
375
Chris Lattner90fe73d2010-03-30 05:27:58 +0000376void TimerGroup::printAll(raw_ostream &OS) {
377 sys::SmartScopedLock<true> L(*TimerLock);
378
379 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
380 TG->print(OS);
381}
Matthias Braundb39fd62016-11-18 19:43:24 +0000382
Graydon Hoareeac6e872018-08-17 04:13:19 +0000383void TimerGroup::clearAll() {
384 sys::SmartScopedLock<true> L(*TimerLock);
385 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
386 TG->clear();
387}
388
Matthias Braundb39fd62016-11-18 19:43:24 +0000389void TimerGroup::printJSONValue(raw_ostream &OS, const PrintRecord &R,
390 const char *suffix, double Value) {
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000391 assert(yaml::needsQuotes(Name) == yaml::QuotingType::None &&
Roman Lebedevddfefc32018-05-16 18:15:51 +0000392 "TimerGroup name should not need quotes");
Francis Visoiu Mistrihb213b272017-12-18 17:38:03 +0000393 assert(yaml::needsQuotes(R.Name) == yaml::QuotingType::None &&
Roman Lebedevddfefc32018-05-16 18:15:51 +0000394 "Timer name should not need quotes");
395 constexpr auto max_digits10 = std::numeric_limits<double>::max_digits10;
396 OS << "\t\"time." << Name << '.' << R.Name << suffix
397 << "\": " << format("%.*e", max_digits10 - 1, Value);
Matthias Braundb39fd62016-11-18 19:43:24 +0000398}
399
400const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) {
Roman Lebedevd9ade382018-05-16 18:15:56 +0000401 sys::SmartScopedLock<true> L(*TimerLock);
402
Matthias Braundb39fd62016-11-18 19:43:24 +0000403 prepareToPrintList();
404 for (const PrintRecord &R : TimersToPrint) {
405 OS << delim;
406 delim = ",\n";
407
408 const TimeRecord &T = R.Time;
409 printJSONValue(OS, R, ".wall", T.getWallTime());
410 OS << delim;
411 printJSONValue(OS, R, ".user", T.getUserTime());
412 OS << delim;
413 printJSONValue(OS, R, ".sys", T.getSystemTime());
George Karpenkov560b2442018-02-10 00:38:21 +0000414 if (T.getMemUsed()) {
415 OS << delim;
Roman Lebedevc39ad982018-05-16 18:15:47 +0000416 printJSONValue(OS, R, ".mem", T.getMemUsed());
George Karpenkov560b2442018-02-10 00:38:21 +0000417 }
Matthias Braundb39fd62016-11-18 19:43:24 +0000418 }
419 TimersToPrint.clear();
420 return delim;
421}
422
423const char *TimerGroup::printAllJSONValues(raw_ostream &OS, const char *delim) {
424 sys::SmartScopedLock<true> L(*TimerLock);
425 for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
426 delim = TG->printJSONValues(OS, delim);
427 return delim;
428}
429
430void TimerGroup::ConstructTimerLists() {
431 (void)*NamedGroupedTimers;
432}