1009b1c42SEd Schouten //===-- Timer.cpp - Interval Timing Support -------------------------------===//
2009b1c42SEd Schouten //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6009b1c42SEd Schouten //
7009b1c42SEd Schouten //===----------------------------------------------------------------------===//
8009b1c42SEd Schouten //
9b915e9e0SDimitry Andric /// \file Interval Timing implementation.
10009b1c42SEd Schouten //
11009b1c42SEd Schouten //===----------------------------------------------------------------------===//
12009b1c42SEd Schouten
13009b1c42SEd Schouten #include "llvm/Support/Timer.h"
14344a3780SDimitry Andric
15344a3780SDimitry Andric #include "DebugOptions.h"
16344a3780SDimitry Andric
17dd58ef01SDimitry Andric #include "llvm/ADT/Statistic.h"
18104bd817SRoman Divacky #include "llvm/ADT/StringMap.h"
19344a3780SDimitry Andric #include "llvm/Config/config.h"
204a16efa3SDimitry Andric #include "llvm/Support/CommandLine.h"
215ca98fd9SDimitry Andric #include "llvm/Support/FileSystem.h"
224a16efa3SDimitry Andric #include "llvm/Support/Format.h"
234a16efa3SDimitry Andric #include "llvm/Support/ManagedStatic.h"
244a16efa3SDimitry Andric #include "llvm/Support/Mutex.h"
254a16efa3SDimitry Andric #include "llvm/Support/Process.h"
26e6d15924SDimitry Andric #include "llvm/Support/Signposts.h"
27b915e9e0SDimitry Andric #include "llvm/Support/YAMLTraits.h"
287ab83427SDimitry Andric #include "llvm/Support/raw_ostream.h"
29eb11fae6SDimitry Andric #include <limits>
30eb11fae6SDimitry Andric
31344a3780SDimitry Andric #if HAVE_UNISTD_H
32344a3780SDimitry Andric #include <unistd.h>
33344a3780SDimitry Andric #endif
34344a3780SDimitry Andric
35344a3780SDimitry Andric #ifdef HAVE_PROC_PID_RUSAGE
36344a3780SDimitry Andric #include <libproc.h>
37344a3780SDimitry Andric #endif
38344a3780SDimitry Andric
39009b1c42SEd Schouten using namespace llvm;
40009b1c42SEd Schouten
41b915e9e0SDimitry Andric // This ugly hack is brought to you courtesy of constructor/destructor ordering
42b915e9e0SDimitry Andric // being unspecified by C++. Basically the problem is that a Statistic object
43b915e9e0SDimitry Andric // gets destroyed, which ends up calling 'GetLibSupportInfoOutputFile()'
44b915e9e0SDimitry Andric // (below), which calls this function. LibSupportInfoOutputFilename used to be
45b915e9e0SDimitry Andric // a global variable, but sometimes it would get destroyed before the Statistic,
46b915e9e0SDimitry Andric // causing havoc to ensue. We "fix" this by creating the string the first time
47b915e9e0SDimitry Andric // it is needed and never destroying it.
48009b1c42SEd Schouten static ManagedStatic<std::string> LibSupportInfoOutputFilename;
getLibSupportInfoOutputFilename()49009b1c42SEd Schouten static std::string &getLibSupportInfoOutputFilename() {
50009b1c42SEd Schouten return *LibSupportInfoOutputFilename;
51009b1c42SEd Schouten }
52009b1c42SEd Schouten
53f859468fSEd Schouten static ManagedStatic<sys::SmartMutex<true> > TimerLock;
54f859468fSEd Schouten
55e6d15924SDimitry Andric /// Allows llvm::Timer to emit signposts when supported.
56e6d15924SDimitry Andric static ManagedStatic<SignpostEmitter> Signposts;
57e6d15924SDimitry Andric
58009b1c42SEd Schouten namespace {
59344a3780SDimitry Andric struct CreateTrackSpace {
call__anona17f50720111::CreateTrackSpace60344a3780SDimitry Andric static void *call() {
61344a3780SDimitry Andric return new cl::opt<bool>("track-memory",
62344a3780SDimitry Andric cl::desc("Enable -time-passes memory "
63009b1c42SEd Schouten "tracking (this may be slow)"),
64009b1c42SEd Schouten cl::Hidden);
65344a3780SDimitry Andric }
66344a3780SDimitry Andric };
67344a3780SDimitry Andric static ManagedStatic<cl::opt<bool>, CreateTrackSpace> TrackSpace;
68344a3780SDimitry Andric struct CreateInfoOutputFilename {
call__anona17f50720111::CreateInfoOutputFilename69344a3780SDimitry Andric static void *call() {
70344a3780SDimitry Andric return new cl::opt<std::string, true>(
71344a3780SDimitry Andric "info-output-file", cl::value_desc("filename"),
72344a3780SDimitry Andric cl::desc("File to append -stats and -timer output to"), cl::Hidden,
73344a3780SDimitry Andric cl::location(getLibSupportInfoOutputFilename()));
74344a3780SDimitry Andric }
75344a3780SDimitry Andric };
76344a3780SDimitry Andric static ManagedStatic<cl::opt<std::string, true>, CreateInfoOutputFilename>
77344a3780SDimitry Andric InfoOutputFilename;
78344a3780SDimitry Andric struct CreateSortTimers {
call__anona17f50720111::CreateSortTimers79344a3780SDimitry Andric static void *call() {
80344a3780SDimitry Andric return new cl::opt<bool>(
81344a3780SDimitry Andric "sort-timers",
82344a3780SDimitry Andric cl::desc("In the report, sort the timers in each group "
83b60736ecSDimitry Andric "in wall clock time order"),
84b60736ecSDimitry Andric cl::init(true), cl::Hidden);
851a82d4c0SDimitry Andric }
86344a3780SDimitry Andric };
87344a3780SDimitry Andric ManagedStatic<cl::opt<bool>, CreateSortTimers> SortTimers;
88344a3780SDimitry Andric } // namespace
89344a3780SDimitry Andric
initTimerOptions()90344a3780SDimitry Andric void llvm::initTimerOptions() {
91344a3780SDimitry Andric *TrackSpace;
92344a3780SDimitry Andric *InfoOutputFilename;
93344a3780SDimitry Andric *SortTimers;
94344a3780SDimitry Andric }
95009b1c42SEd Schouten
CreateInfoOutputFile()96dd58ef01SDimitry Andric std::unique_ptr<raw_fd_ostream> llvm::CreateInfoOutputFile() {
97104bd817SRoman Divacky const std::string &OutputFilename = getLibSupportInfoOutputFilename();
98104bd817SRoman Divacky if (OutputFilename.empty())
991d5ae102SDimitry Andric return std::make_unique<raw_fd_ostream>(2, false); // stderr.
100104bd817SRoman Divacky if (OutputFilename == "-")
1011d5ae102SDimitry Andric return std::make_unique<raw_fd_ostream>(1, false); // stdout.
102104bd817SRoman Divacky
103abdf259dSRoman Divacky // Append mode is used because the info output file is opened and closed
104abdf259dSRoman Divacky // each time -stats or -time-passes wants to print output to it. To
105abdf259dSRoman Divacky // compensate for this, the test-suite Makefiles have code to delete the
106abdf259dSRoman Divacky // info output file before running commands which write to it.
10767c32a98SDimitry Andric std::error_code EC;
1081d5ae102SDimitry Andric auto Result = std::make_unique<raw_fd_ostream>(
109344a3780SDimitry Andric OutputFilename, EC, sys::fs::OF_Append | sys::fs::OF_TextWithCRLF);
11067c32a98SDimitry Andric if (!EC)
111104bd817SRoman Divacky return Result;
112104bd817SRoman Divacky
113104bd817SRoman Divacky errs() << "Error opening info-output-file '"
114104bd817SRoman Divacky << OutputFilename << " for appending!\n";
1151d5ae102SDimitry Andric return std::make_unique<raw_fd_ostream>(2, false); // stderr.
116104bd817SRoman Divacky }
117104bd817SRoman Divacky
118ee2f195dSDimitry Andric namespace {
119ee2f195dSDimitry Andric struct CreateDefaultTimerGroup {
call__anona17f50720211::CreateDefaultTimerGroup120ee2f195dSDimitry Andric static void *call() {
121ee2f195dSDimitry Andric return new TimerGroup("misc", "Miscellaneous Ungrouped Timers");
122009b1c42SEd Schouten }
123ee2f195dSDimitry Andric };
124ee2f195dSDimitry Andric } // namespace
125ee2f195dSDimitry Andric static ManagedStatic<TimerGroup, CreateDefaultTimerGroup> DefaultTimerGroup;
getDefaultTimerGroup()126ee2f195dSDimitry Andric static TimerGroup *getDefaultTimerGroup() { return &*DefaultTimerGroup; }
127009b1c42SEd Schouten
128104bd817SRoman Divacky //===----------------------------------------------------------------------===//
129104bd817SRoman Divacky // Timer Implementation
130104bd817SRoman Divacky //===----------------------------------------------------------------------===//
131104bd817SRoman Divacky
init(StringRef TimerName,StringRef TimerDescription)132706b4fc4SDimitry Andric void Timer::init(StringRef TimerName, StringRef TimerDescription) {
133706b4fc4SDimitry Andric init(TimerName, TimerDescription, *getDefaultTimerGroup());
134009b1c42SEd Schouten }
135009b1c42SEd Schouten
init(StringRef TimerName,StringRef TimerDescription,TimerGroup & tg)136706b4fc4SDimitry Andric void Timer::init(StringRef TimerName, StringRef TimerDescription,
137706b4fc4SDimitry Andric TimerGroup &tg) {
1385ca98fd9SDimitry Andric assert(!TG && "Timer already initialized");
139706b4fc4SDimitry Andric Name.assign(TimerName.begin(), TimerName.end());
140706b4fc4SDimitry Andric Description.assign(TimerDescription.begin(), TimerDescription.end());
141dd58ef01SDimitry Andric Running = Triggered = false;
142104bd817SRoman Divacky TG = &tg;
143104bd817SRoman Divacky TG->addTimer(*this);
144009b1c42SEd Schouten }
145009b1c42SEd Schouten
~Timer()146009b1c42SEd Schouten Timer::~Timer() {
147104bd817SRoman Divacky if (!TG) return; // Never initialized, or already cleared.
148104bd817SRoman Divacky TG->removeTimer(*this);
149009b1c42SEd Schouten }
150009b1c42SEd Schouten
getMemUsage()151009b1c42SEd Schouten static inline size_t getMemUsage() {
152344a3780SDimitry Andric if (!*TrackSpace)
153344a3780SDimitry Andric return 0;
154009b1c42SEd Schouten return sys::Process::GetMallocUsage();
155009b1c42SEd Schouten }
156009b1c42SEd Schouten
getCurInstructionsExecuted()157344a3780SDimitry Andric static uint64_t getCurInstructionsExecuted() {
158344a3780SDimitry Andric #if defined(HAVE_UNISTD_H) && defined(HAVE_PROC_PID_RUSAGE) && \
159344a3780SDimitry Andric defined(RUSAGE_INFO_V4)
160344a3780SDimitry Andric struct rusage_info_v4 ru;
161344a3780SDimitry Andric if (proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru) == 0) {
162344a3780SDimitry Andric return ru.ri_instructions;
163344a3780SDimitry Andric }
164344a3780SDimitry Andric #endif
165344a3780SDimitry Andric return 0;
166344a3780SDimitry Andric }
167344a3780SDimitry Andric
getCurrentTime(bool Start)168104bd817SRoman Divacky TimeRecord TimeRecord::getCurrentTime(bool Start) {
169b915e9e0SDimitry Andric using Seconds = std::chrono::duration<double, std::ratio<1>>;
170009b1c42SEd Schouten TimeRecord Result;
171b915e9e0SDimitry Andric sys::TimePoint<> now;
172b915e9e0SDimitry Andric std::chrono::nanoseconds user, sys;
173009b1c42SEd Schouten
174009b1c42SEd Schouten if (Start) {
175104bd817SRoman Divacky Result.MemUsed = getMemUsage();
176344a3780SDimitry Andric Result.InstructionsExecuted = getCurInstructionsExecuted();
177009b1c42SEd Schouten sys::Process::GetTimeUsage(now, user, sys);
178009b1c42SEd Schouten } else {
179009b1c42SEd Schouten sys::Process::GetTimeUsage(now, user, sys);
180344a3780SDimitry Andric Result.InstructionsExecuted = getCurInstructionsExecuted();
181104bd817SRoman Divacky Result.MemUsed = getMemUsage();
182009b1c42SEd Schouten }
183009b1c42SEd Schouten
184b915e9e0SDimitry Andric Result.WallTime = Seconds(now.time_since_epoch()).count();
185b915e9e0SDimitry Andric Result.UserTime = Seconds(user).count();
186b915e9e0SDimitry Andric Result.SystemTime = Seconds(sys).count();
187009b1c42SEd Schouten return Result;
188009b1c42SEd Schouten }
189009b1c42SEd Schouten
startTimer()190009b1c42SEd Schouten void Timer::startTimer() {
191dd58ef01SDimitry Andric assert(!Running && "Cannot start a running timer");
192dd58ef01SDimitry Andric Running = Triggered = true;
193b60736ecSDimitry Andric Signposts->startInterval(this, getName());
194dd58ef01SDimitry Andric StartTime = TimeRecord::getCurrentTime(true);
195009b1c42SEd Schouten }
196009b1c42SEd Schouten
stopTimer()197009b1c42SEd Schouten void Timer::stopTimer() {
198dd58ef01SDimitry Andric assert(Running && "Cannot stop a paused timer");
199dd58ef01SDimitry Andric Running = false;
200104bd817SRoman Divacky Time += TimeRecord::getCurrentTime(false);
201dd58ef01SDimitry Andric Time -= StartTime;
202c0981da4SDimitry Andric Signposts->endInterval(this, getName());
203009b1c42SEd Schouten }
204dd58ef01SDimitry Andric
clear()205dd58ef01SDimitry Andric void Timer::clear() {
206dd58ef01SDimitry Andric Running = Triggered = false;
207dd58ef01SDimitry Andric Time = StartTime = TimeRecord();
208009b1c42SEd Schouten }
209009b1c42SEd Schouten
printVal(double Val,double Total,raw_ostream & OS)21059850d08SRoman Divacky static void printVal(double Val, double Total, raw_ostream &OS) {
211104bd817SRoman Divacky if (Total < 1e-7) // Avoid dividing by zero.
212009b1c42SEd Schouten OS << " ----- ";
21363faed5bSDimitry Andric else
21463faed5bSDimitry Andric OS << format(" %7.4f (%5.1f%%)", Val, Val*100/Total);
215009b1c42SEd Schouten }
216009b1c42SEd Schouten
print(const TimeRecord & Total,raw_ostream & OS) const217104bd817SRoman Divacky void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
218104bd817SRoman Divacky if (Total.getUserTime())
219104bd817SRoman Divacky printVal(getUserTime(), Total.getUserTime(), OS);
220104bd817SRoman Divacky if (Total.getSystemTime())
221104bd817SRoman Divacky printVal(getSystemTime(), Total.getSystemTime(), OS);
222009b1c42SEd Schouten if (Total.getProcessTime())
223f859468fSEd Schouten printVal(getProcessTime(), Total.getProcessTime(), OS);
224104bd817SRoman Divacky printVal(getWallTime(), Total.getWallTime(), OS);
225009b1c42SEd Schouten
226009b1c42SEd Schouten OS << " ";
227009b1c42SEd Schouten
228104bd817SRoman Divacky if (Total.getMemUsed())
22963faed5bSDimitry Andric OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
230344a3780SDimitry Andric if (Total.getInstructionsExecuted())
231344a3780SDimitry Andric OS << format("%9" PRId64 " ", (int64_t)getInstructionsExecuted());
232009b1c42SEd Schouten }
233009b1c42SEd Schouten
234009b1c42SEd Schouten
235104bd817SRoman Divacky //===----------------------------------------------------------------------===//
236104bd817SRoman Divacky // NamedRegionTimer Implementation
237104bd817SRoman Divacky //===----------------------------------------------------------------------===//
238104bd817SRoman Divacky
239d7f7719eSRoman Divacky namespace {
240d7f7719eSRoman Divacky
241104bd817SRoman Divacky typedef StringMap<Timer> Name2TimerMap;
242104bd817SRoman Divacky
243104bd817SRoman Divacky class Name2PairMap {
244104bd817SRoman Divacky StringMap<std::pair<TimerGroup*, Name2TimerMap> > Map;
245104bd817SRoman Divacky public:
~Name2PairMap()246104bd817SRoman Divacky ~Name2PairMap() {
247104bd817SRoman Divacky for (StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
248104bd817SRoman Divacky I = Map.begin(), E = Map.end(); I != E; ++I)
249104bd817SRoman Divacky delete I->second.first;
250104bd817SRoman Divacky }
251104bd817SRoman Divacky
get(StringRef Name,StringRef Description,StringRef GroupName,StringRef GroupDescription)252b915e9e0SDimitry Andric Timer &get(StringRef Name, StringRef Description, StringRef GroupName,
253b915e9e0SDimitry Andric StringRef GroupDescription) {
25459850d08SRoman Divacky sys::SmartScopedLock<true> L(*TimerLock);
255009b1c42SEd Schouten
256104bd817SRoman Divacky std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
257104bd817SRoman Divacky
258104bd817SRoman Divacky if (!GroupEntry.first)
259b915e9e0SDimitry Andric GroupEntry.first = new TimerGroup(GroupName, GroupDescription);
260104bd817SRoman Divacky
261104bd817SRoman Divacky Timer &T = GroupEntry.second[Name];
262104bd817SRoman Divacky if (!T.isInitialized())
263b915e9e0SDimitry Andric T.init(Name, Description, *GroupEntry.first);
264104bd817SRoman Divacky return T;
265104bd817SRoman Divacky }
266104bd817SRoman Divacky };
267104bd817SRoman Divacky
2681a82d4c0SDimitry Andric }
269d7f7719eSRoman Divacky
270104bd817SRoman Divacky static ManagedStatic<Name2PairMap> NamedGroupedTimers;
271104bd817SRoman Divacky
NamedRegionTimer(StringRef Name,StringRef Description,StringRef GroupName,StringRef GroupDescription,bool Enabled)272b915e9e0SDimitry Andric NamedRegionTimer::NamedRegionTimer(StringRef Name, StringRef Description,
273b915e9e0SDimitry Andric StringRef GroupName,
274b915e9e0SDimitry Andric StringRef GroupDescription, bool Enabled)
275b915e9e0SDimitry Andric : TimeRegion(!Enabled ? nullptr
276b915e9e0SDimitry Andric : &NamedGroupedTimers->get(Name, Description, GroupName,
277b915e9e0SDimitry Andric GroupDescription)) {}
278104bd817SRoman Divacky
279104bd817SRoman Divacky //===----------------------------------------------------------------------===//
280104bd817SRoman Divacky // TimerGroup Implementation
281104bd817SRoman Divacky //===----------------------------------------------------------------------===//
282104bd817SRoman Divacky
283b915e9e0SDimitry Andric /// This is the global list of TimerGroups, maintained by the TimerGroup
284b915e9e0SDimitry Andric /// ctor/dtor and is protected by the TimerLock lock.
2855ca98fd9SDimitry Andric static TimerGroup *TimerGroupList = nullptr;
286104bd817SRoman Divacky
TimerGroup(StringRef Name,StringRef Description)287b915e9e0SDimitry Andric TimerGroup::TimerGroup(StringRef Name, StringRef Description)
288b915e9e0SDimitry Andric : Name(Name.begin(), Name.end()),
289b915e9e0SDimitry Andric Description(Description.begin(), Description.end()) {
290104bd817SRoman Divacky // Add the group to TimerGroupList.
291104bd817SRoman Divacky sys::SmartScopedLock<true> L(*TimerLock);
292104bd817SRoman Divacky if (TimerGroupList)
293104bd817SRoman Divacky TimerGroupList->Prev = &Next;
294104bd817SRoman Divacky Next = TimerGroupList;
295104bd817SRoman Divacky Prev = &TimerGroupList;
296104bd817SRoman Divacky TimerGroupList = this;
297104bd817SRoman Divacky }
298104bd817SRoman Divacky
TimerGroup(StringRef Name,StringRef Description,const StringMap<TimeRecord> & Records)299eb11fae6SDimitry Andric TimerGroup::TimerGroup(StringRef Name, StringRef Description,
300eb11fae6SDimitry Andric const StringMap<TimeRecord> &Records)
301eb11fae6SDimitry Andric : TimerGroup(Name, Description) {
302eb11fae6SDimitry Andric TimersToPrint.reserve(Records.size());
303eb11fae6SDimitry Andric for (const auto &P : Records)
304cfca06d7SDimitry Andric TimersToPrint.emplace_back(P.getValue(), std::string(P.getKey()),
305cfca06d7SDimitry Andric std::string(P.getKey()));
306eb11fae6SDimitry Andric assert(TimersToPrint.size() == Records.size() && "Size mismatch");
307eb11fae6SDimitry Andric }
308eb11fae6SDimitry Andric
~TimerGroup()309104bd817SRoman Divacky TimerGroup::~TimerGroup() {
310104bd817SRoman Divacky // If the timer group is destroyed before the timers it owns, accumulate and
311104bd817SRoman Divacky // print the timing data.
3125ca98fd9SDimitry Andric while (FirstTimer)
313104bd817SRoman Divacky removeTimer(*FirstTimer);
314104bd817SRoman Divacky
315104bd817SRoman Divacky // Remove the group from the TimerGroupList.
316104bd817SRoman Divacky sys::SmartScopedLock<true> L(*TimerLock);
317104bd817SRoman Divacky *Prev = Next;
318104bd817SRoman Divacky if (Next)
319104bd817SRoman Divacky Next->Prev = Prev;
320104bd817SRoman Divacky }
321104bd817SRoman Divacky
322104bd817SRoman Divacky
removeTimer(Timer & T)323104bd817SRoman Divacky void TimerGroup::removeTimer(Timer &T) {
324104bd817SRoman Divacky sys::SmartScopedLock<true> L(*TimerLock);
325104bd817SRoman Divacky
326104bd817SRoman Divacky // If the timer was started, move its data to TimersToPrint.
327dd58ef01SDimitry Andric if (T.hasTriggered())
328b915e9e0SDimitry Andric TimersToPrint.emplace_back(T.Time, T.Name, T.Description);
329104bd817SRoman Divacky
3305ca98fd9SDimitry Andric T.TG = nullptr;
331104bd817SRoman Divacky
332104bd817SRoman Divacky // Unlink the timer from our list.
333104bd817SRoman Divacky *T.Prev = T.Next;
334104bd817SRoman Divacky if (T.Next)
335104bd817SRoman Divacky T.Next->Prev = T.Prev;
336104bd817SRoman Divacky
337104bd817SRoman Divacky // Print the report when all timers in this group are destroyed if some of
338104bd817SRoman Divacky // them were started.
3395ca98fd9SDimitry Andric if (FirstTimer || TimersToPrint.empty())
340104bd817SRoman Divacky return;
341104bd817SRoman Divacky
342dd58ef01SDimitry Andric std::unique_ptr<raw_ostream> OutStream = CreateInfoOutputFile();
343104bd817SRoman Divacky PrintQueuedTimers(*OutStream);
344104bd817SRoman Divacky }
345104bd817SRoman Divacky
addTimer(Timer & T)346104bd817SRoman Divacky void TimerGroup::addTimer(Timer &T) {
347104bd817SRoman Divacky sys::SmartScopedLock<true> L(*TimerLock);
348104bd817SRoman Divacky
349104bd817SRoman Divacky // Add the timer to our list.
350104bd817SRoman Divacky if (FirstTimer)
351104bd817SRoman Divacky FirstTimer->Prev = &T.Next;
352104bd817SRoman Divacky T.Next = FirstTimer;
353104bd817SRoman Divacky T.Prev = &FirstTimer;
354104bd817SRoman Divacky FirstTimer = &T;
355104bd817SRoman Divacky }
356104bd817SRoman Divacky
PrintQueuedTimers(raw_ostream & OS)357104bd817SRoman Divacky void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
358b60736ecSDimitry Andric // Perhaps sort the timers in descending order by amount of time taken.
359344a3780SDimitry Andric if (*SortTimers)
360d8e91e46SDimitry Andric llvm::sort(TimersToPrint);
361104bd817SRoman Divacky
362104bd817SRoman Divacky TimeRecord Total;
363b915e9e0SDimitry Andric for (const PrintRecord &Record : TimersToPrint)
364b915e9e0SDimitry Andric Total += Record.Time;
365104bd817SRoman Divacky
366104bd817SRoman Divacky // Print out timing header.
367104bd817SRoman Divacky OS << "===" << std::string(73, '-') << "===\n";
368104bd817SRoman Divacky // Figure out how many spaces to indent TimerGroup name.
369b915e9e0SDimitry Andric unsigned Padding = (80-Description.length())/2;
370009b1c42SEd Schouten if (Padding > 80) Padding = 0; // Don't allow "negative" numbers
371b915e9e0SDimitry Andric OS.indent(Padding) << Description << '\n';
372104bd817SRoman Divacky OS << "===" << std::string(73, '-') << "===\n";
373009b1c42SEd Schouten
374009b1c42SEd Schouten // If this is not an collection of ungrouped times, print the total time.
375009b1c42SEd Schouten // Ungrouped timers don't really make sense to add up. We still print the
376009b1c42SEd Schouten // TOTAL line to make the percentages make sense.
37771d5a254SDimitry Andric if (this != getDefaultTimerGroup())
37863faed5bSDimitry Andric OS << format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
37963faed5bSDimitry Andric Total.getProcessTime(), Total.getWallTime());
380104bd817SRoman Divacky OS << '\n';
381009b1c42SEd Schouten
382104bd817SRoman Divacky if (Total.getUserTime())
383104bd817SRoman Divacky OS << " ---User Time---";
384104bd817SRoman Divacky if (Total.getSystemTime())
385104bd817SRoman Divacky OS << " --System Time--";
386f859468fSEd Schouten if (Total.getProcessTime())
387104bd817SRoman Divacky OS << " --User+System--";
388104bd817SRoman Divacky OS << " ---Wall Time---";
389f859468fSEd Schouten if (Total.getMemUsed())
390104bd817SRoman Divacky OS << " ---Mem---";
391344a3780SDimitry Andric if (Total.getInstructionsExecuted())
392344a3780SDimitry Andric OS << " ---Instr---";
393104bd817SRoman Divacky OS << " --- Name ---\n";
394009b1c42SEd Schouten
395104bd817SRoman Divacky // Loop through all of the timing data, printing it out.
396c0981da4SDimitry Andric for (const PrintRecord &Record : llvm::reverse(TimersToPrint)) {
397b915e9e0SDimitry Andric Record.Time.print(Total, OS);
398b915e9e0SDimitry Andric OS << Record.Description << '\n';
399009b1c42SEd Schouten }
400104bd817SRoman Divacky
401104bd817SRoman Divacky Total.print(Total, OS);
402104bd817SRoman Divacky OS << "Total\n\n";
403104bd817SRoman Divacky OS.flush();
404009b1c42SEd Schouten
405009b1c42SEd Schouten TimersToPrint.clear();
406009b1c42SEd Schouten }
407009b1c42SEd Schouten
prepareToPrintList(bool ResetTime)408e6d15924SDimitry Andric void TimerGroup::prepareToPrintList(bool ResetTime) {
409d8e91e46SDimitry Andric // See if any of our timers were started, if so add them to TimersToPrint.
410104bd817SRoman Divacky for (Timer *T = FirstTimer; T; T = T->Next) {
411dd58ef01SDimitry Andric if (!T->hasTriggered()) continue;
412eb11fae6SDimitry Andric bool WasRunning = T->isRunning();
413eb11fae6SDimitry Andric if (WasRunning)
414eb11fae6SDimitry Andric T->stopTimer();
415eb11fae6SDimitry Andric
416b915e9e0SDimitry Andric TimersToPrint.emplace_back(T->Time, T->Name, T->Description);
417104bd817SRoman Divacky
418e6d15924SDimitry Andric if (ResetTime)
419e6d15924SDimitry Andric T->clear();
420e6d15924SDimitry Andric
421eb11fae6SDimitry Andric if (WasRunning)
422eb11fae6SDimitry Andric T->startTimer();
423f859468fSEd Schouten }
424b915e9e0SDimitry Andric }
425b915e9e0SDimitry Andric
print(raw_ostream & OS,bool ResetAfterPrint)426e6d15924SDimitry Andric void TimerGroup::print(raw_ostream &OS, bool ResetAfterPrint) {
427e6d15924SDimitry Andric {
428e6d15924SDimitry Andric // After preparing the timers we can free the lock
429b915e9e0SDimitry Andric sys::SmartScopedLock<true> L(*TimerLock);
430e6d15924SDimitry Andric prepareToPrintList(ResetAfterPrint);
431e6d15924SDimitry Andric }
432f859468fSEd Schouten
433104bd817SRoman Divacky // If any timers were started, print the group.
434104bd817SRoman Divacky if (!TimersToPrint.empty())
435104bd817SRoman Divacky PrintQueuedTimers(OS);
436104bd817SRoman Divacky }
437104bd817SRoman Divacky
clear()438d8e91e46SDimitry Andric void TimerGroup::clear() {
439d8e91e46SDimitry Andric sys::SmartScopedLock<true> L(*TimerLock);
440d8e91e46SDimitry Andric for (Timer *T = FirstTimer; T; T = T->Next)
441d8e91e46SDimitry Andric T->clear();
442d8e91e46SDimitry Andric }
443d8e91e46SDimitry Andric
printAll(raw_ostream & OS)444104bd817SRoman Divacky void TimerGroup::printAll(raw_ostream &OS) {
44559850d08SRoman Divacky sys::SmartScopedLock<true> L(*TimerLock);
446f859468fSEd Schouten
447104bd817SRoman Divacky for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
448104bd817SRoman Divacky TG->print(OS);
449104bd817SRoman Divacky }
450b915e9e0SDimitry Andric
clearAll()451d8e91e46SDimitry Andric void TimerGroup::clearAll() {
452d8e91e46SDimitry Andric sys::SmartScopedLock<true> L(*TimerLock);
453d8e91e46SDimitry Andric for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
454d8e91e46SDimitry Andric TG->clear();
455d8e91e46SDimitry Andric }
456d8e91e46SDimitry Andric
printJSONValue(raw_ostream & OS,const PrintRecord & R,const char * suffix,double Value)457b915e9e0SDimitry Andric void TimerGroup::printJSONValue(raw_ostream &OS, const PrintRecord &R,
458b915e9e0SDimitry Andric const char *suffix, double Value) {
459044eb2f6SDimitry Andric assert(yaml::needsQuotes(Name) == yaml::QuotingType::None &&
460eb11fae6SDimitry Andric "TimerGroup name should not need quotes");
461044eb2f6SDimitry Andric assert(yaml::needsQuotes(R.Name) == yaml::QuotingType::None &&
462eb11fae6SDimitry Andric "Timer name should not need quotes");
463eb11fae6SDimitry Andric constexpr auto max_digits10 = std::numeric_limits<double>::max_digits10;
464eb11fae6SDimitry Andric OS << "\t\"time." << Name << '.' << R.Name << suffix
465eb11fae6SDimitry Andric << "\": " << format("%.*e", max_digits10 - 1, Value);
466b915e9e0SDimitry Andric }
467b915e9e0SDimitry Andric
printJSONValues(raw_ostream & OS,const char * delim)468b915e9e0SDimitry Andric const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) {
469eb11fae6SDimitry Andric sys::SmartScopedLock<true> L(*TimerLock);
470eb11fae6SDimitry Andric
471e6d15924SDimitry Andric prepareToPrintList(false);
472b915e9e0SDimitry Andric for (const PrintRecord &R : TimersToPrint) {
473b915e9e0SDimitry Andric OS << delim;
474b915e9e0SDimitry Andric delim = ",\n";
475b915e9e0SDimitry Andric
476b915e9e0SDimitry Andric const TimeRecord &T = R.Time;
477b915e9e0SDimitry Andric printJSONValue(OS, R, ".wall", T.getWallTime());
478b915e9e0SDimitry Andric OS << delim;
479b915e9e0SDimitry Andric printJSONValue(OS, R, ".user", T.getUserTime());
480b915e9e0SDimitry Andric OS << delim;
481b915e9e0SDimitry Andric printJSONValue(OS, R, ".sys", T.getSystemTime());
482eb11fae6SDimitry Andric if (T.getMemUsed()) {
483eb11fae6SDimitry Andric OS << delim;
484eb11fae6SDimitry Andric printJSONValue(OS, R, ".mem", T.getMemUsed());
485eb11fae6SDimitry Andric }
486344a3780SDimitry Andric if (T.getInstructionsExecuted()) {
487344a3780SDimitry Andric OS << delim;
488344a3780SDimitry Andric printJSONValue(OS, R, ".instr", T.getInstructionsExecuted());
489344a3780SDimitry Andric }
490b915e9e0SDimitry Andric }
491b915e9e0SDimitry Andric TimersToPrint.clear();
492b915e9e0SDimitry Andric return delim;
493b915e9e0SDimitry Andric }
494b915e9e0SDimitry Andric
printAllJSONValues(raw_ostream & OS,const char * delim)495b915e9e0SDimitry Andric const char *TimerGroup::printAllJSONValues(raw_ostream &OS, const char *delim) {
496b915e9e0SDimitry Andric sys::SmartScopedLock<true> L(*TimerLock);
497b915e9e0SDimitry Andric for (TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
498b915e9e0SDimitry Andric delim = TG->printJSONValues(OS, delim);
499b915e9e0SDimitry Andric return delim;
500b915e9e0SDimitry Andric }
501b915e9e0SDimitry Andric
constructForStatistics()502e3b55780SDimitry Andric void TimerGroup::constructForStatistics() {
503e3b55780SDimitry Andric (void)getLibSupportInfoOutputFilename();
504b915e9e0SDimitry Andric (void)*NamedGroupedTimers;
505b915e9e0SDimitry Andric }
506cfca06d7SDimitry Andric
aquireDefaultGroup()507cfca06d7SDimitry Andric std::unique_ptr<TimerGroup> TimerGroup::aquireDefaultGroup() {
508cfca06d7SDimitry Andric return std::unique_ptr<TimerGroup>(DefaultTimerGroup.claim());
509cfca06d7SDimitry Andric }
510