1 //===-- Timer.cpp - Interval Timing Support -------------------------------===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 /// \file Interval Timing implementation.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/Timer.h"
14 #include "llvm/ADT/Statistic.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/Mutex.h"
21 #include "llvm/Support/Process.h"
22 #include "llvm/Support/Signposts.h"
23 #include "llvm/Support/YAMLTraits.h"
24 #include "llvm/Support/raw_ostream.h"
29 // 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.
36 static ManagedStatic
<std::string
> LibSupportInfoOutputFilename
;
37 static std::string
&getLibSupportInfoOutputFilename() {
38 return *LibSupportInfoOutputFilename
;
41 static ManagedStatic
<sys::SmartMutex
<true> > TimerLock
;
43 /// Allows llvm::Timer to emit signposts when supported.
44 static ManagedStatic
<SignpostEmitter
> Signposts
;
48 TrackSpace("track-memory", cl::desc("Enable -time-passes memory "
49 "tracking (this may be slow)"),
52 static cl::opt
<std::string
, true>
53 InfoOutputFilename("info-output-file", cl::value_desc("filename"),
54 cl::desc("File to append -stats and -timer output to"),
55 cl::Hidden
, cl::location(getLibSupportInfoOutputFilename()));
58 std::unique_ptr
<raw_fd_ostream
> llvm::CreateInfoOutputFile() {
59 const std::string
&OutputFilename
= getLibSupportInfoOutputFilename();
60 if (OutputFilename
.empty())
61 return std::make_unique
<raw_fd_ostream
>(2, false); // stderr.
62 if (OutputFilename
== "-")
63 return std::make_unique
<raw_fd_ostream
>(1, false); // stdout.
65 // 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.
70 auto Result
= std::make_unique
<raw_fd_ostream
>(
71 OutputFilename
, EC
, sys::fs::OF_Append
| sys::fs::OF_Text
);
75 errs() << "Error opening info-output-file '"
76 << OutputFilename
<< " for appending!\n";
77 return std::make_unique
<raw_fd_ostream
>(2, false); // stderr.
81 struct CreateDefaultTimerGroup
{
83 return new TimerGroup("misc", "Miscellaneous Ungrouped Timers");
87 static ManagedStatic
<TimerGroup
, CreateDefaultTimerGroup
> DefaultTimerGroup
;
88 static TimerGroup
*getDefaultTimerGroup() { return &*DefaultTimerGroup
; }
90 //===----------------------------------------------------------------------===//
91 // Timer Implementation
92 //===----------------------------------------------------------------------===//
94 void Timer::init(StringRef Name
, StringRef Description
) {
95 init(Name
, Description
, *getDefaultTimerGroup());
98 void Timer::init(StringRef Name
, StringRef Description
, TimerGroup
&tg
) {
99 assert(!TG
&& "Timer already initialized");
100 this->Name
.assign(Name
.begin(), Name
.end());
101 this->Description
.assign(Description
.begin(), Description
.end());
102 Running
= Triggered
= false;
108 if (!TG
) return; // Never initialized, or already cleared.
109 TG
->removeTimer(*this);
112 static inline size_t getMemUsage() {
113 if (!TrackSpace
) return 0;
114 return sys::Process::GetMallocUsage();
117 TimeRecord
TimeRecord::getCurrentTime(bool Start
) {
118 using Seconds
= std::chrono::duration
<double, std::ratio
<1>>;
120 sys::TimePoint
<> now
;
121 std::chrono::nanoseconds user
, sys
;
124 Result
.MemUsed
= getMemUsage();
125 sys::Process::GetTimeUsage(now
, user
, sys
);
127 sys::Process::GetTimeUsage(now
, user
, sys
);
128 Result
.MemUsed
= getMemUsage();
131 Result
.WallTime
= Seconds(now
.time_since_epoch()).count();
132 Result
.UserTime
= Seconds(user
).count();
133 Result
.SystemTime
= Seconds(sys
).count();
137 void Timer::startTimer() {
138 assert(!Running
&& "Cannot start a running timer");
139 Running
= Triggered
= true;
140 Signposts
->startTimerInterval(this);
141 StartTime
= TimeRecord::getCurrentTime(true);
144 void Timer::stopTimer() {
145 assert(Running
&& "Cannot stop a paused timer");
147 Time
+= TimeRecord::getCurrentTime(false);
149 Signposts
->endTimerInterval(this);
152 void Timer::clear() {
153 Running
= Triggered
= false;
154 Time
= StartTime
= TimeRecord();
157 static void printVal(double Val
, double Total
, raw_ostream
&OS
) {
158 if (Total
< 1e-7) // Avoid dividing by zero.
161 OS
<< format(" %7.4f (%5.1f%%)", Val
, Val
*100/Total
);
164 void 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
);
169 if (Total
.getProcessTime())
170 printVal(getProcessTime(), Total
.getProcessTime(), OS
);
171 printVal(getWallTime(), Total
.getWallTime(), OS
);
175 if (Total
.getMemUsed())
176 OS
<< format("%9" PRId64
" ", (int64_t)getMemUsed());
180 //===----------------------------------------------------------------------===//
181 // NamedRegionTimer Implementation
182 //===----------------------------------------------------------------------===//
186 typedef StringMap
<Timer
> Name2TimerMap
;
189 StringMap
<std::pair
<TimerGroup
*, Name2TimerMap
> > Map
;
192 for (StringMap
<std::pair
<TimerGroup
*, Name2TimerMap
> >::iterator
193 I
= Map
.begin(), E
= Map
.end(); I
!= E
; ++I
)
194 delete I
->second
.first
;
197 Timer
&get(StringRef Name
, StringRef Description
, StringRef GroupName
,
198 StringRef GroupDescription
) {
199 sys::SmartScopedLock
<true> L(*TimerLock
);
201 std::pair
<TimerGroup
*, Name2TimerMap
> &GroupEntry
= Map
[GroupName
];
203 if (!GroupEntry
.first
)
204 GroupEntry
.first
= new TimerGroup(GroupName
, GroupDescription
);
206 Timer
&T
= GroupEntry
.second
[Name
];
207 if (!T
.isInitialized())
208 T
.init(Name
, Description
, *GroupEntry
.first
);
215 static ManagedStatic
<Name2PairMap
> NamedGroupedTimers
;
217 NamedRegionTimer::NamedRegionTimer(StringRef Name
, StringRef Description
,
219 StringRef GroupDescription
, bool Enabled
)
220 : TimeRegion(!Enabled
? nullptr
221 : &NamedGroupedTimers
->get(Name
, Description
, GroupName
,
222 GroupDescription
)) {}
224 //===----------------------------------------------------------------------===//
225 // TimerGroup Implementation
226 //===----------------------------------------------------------------------===//
228 /// This is the global list of TimerGroups, maintained by the TimerGroup
229 /// ctor/dtor and is protected by the TimerLock lock.
230 static TimerGroup
*TimerGroupList
= nullptr;
232 TimerGroup::TimerGroup(StringRef Name
, StringRef Description
)
233 : Name(Name
.begin(), Name
.end()),
234 Description(Description
.begin(), Description
.end()) {
235 // Add the group to TimerGroupList.
236 sys::SmartScopedLock
<true> L(*TimerLock
);
238 TimerGroupList
->Prev
= &Next
;
239 Next
= TimerGroupList
;
240 Prev
= &TimerGroupList
;
241 TimerGroupList
= this;
244 TimerGroup::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");
253 TimerGroup::~TimerGroup() {
254 // If the timer group is destroyed before the timers it owns, accumulate and
255 // print the timing data.
257 removeTimer(*FirstTimer
);
259 // Remove the group from the TimerGroupList.
260 sys::SmartScopedLock
<true> L(*TimerLock
);
267 void TimerGroup::removeTimer(Timer
&T
) {
268 sys::SmartScopedLock
<true> L(*TimerLock
);
270 // If the timer was started, move its data to TimersToPrint.
271 if (T
.hasTriggered())
272 TimersToPrint
.emplace_back(T
.Time
, T
.Name
, T
.Description
);
276 // Unlink the timer from our list.
279 T
.Next
->Prev
= T
.Prev
;
281 // Print the report when all timers in this group are destroyed if some of
282 // them were started.
283 if (FirstTimer
|| TimersToPrint
.empty())
286 std::unique_ptr
<raw_ostream
> OutStream
= CreateInfoOutputFile();
287 PrintQueuedTimers(*OutStream
);
290 void TimerGroup::addTimer(Timer
&T
) {
291 sys::SmartScopedLock
<true> L(*TimerLock
);
293 // Add the timer to our list.
295 FirstTimer
->Prev
= &T
.Next
;
297 T
.Prev
= &FirstTimer
;
301 void TimerGroup::PrintQueuedTimers(raw_ostream
&OS
) {
302 // Sort the timers in descending order by amount of time taken.
303 llvm::sort(TimersToPrint
);
306 for (const PrintRecord
&Record
: TimersToPrint
)
307 Total
+= Record
.Time
;
309 // Print out timing header.
310 OS
<< "===" << std::string(73, '-') << "===\n";
311 // Figure out how many spaces to indent TimerGroup name.
312 unsigned Padding
= (80-Description
.length())/2;
313 if (Padding
> 80) Padding
= 0; // Don't allow "negative" numbers
314 OS
.indent(Padding
) << Description
<< '\n';
315 OS
<< "===" << std::string(73, '-') << "===\n";
317 // 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.
320 if (this != getDefaultTimerGroup())
321 OS
<< format(" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
322 Total
.getProcessTime(), Total
.getWallTime());
325 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())
334 OS
<< " --- Name ---\n";
336 // Loop through all of the timing data, printing it out.
337 for (const PrintRecord
&Record
: make_range(TimersToPrint
.rbegin(),
338 TimersToPrint
.rend())) {
339 Record
.Time
.print(Total
, OS
);
340 OS
<< Record
.Description
<< '\n';
343 Total
.print(Total
, OS
);
347 TimersToPrint
.clear();
350 void TimerGroup::prepareToPrintList(bool ResetTime
) {
351 // See if any of our timers were started, if so add them to TimersToPrint.
352 for (Timer
*T
= FirstTimer
; T
; T
= T
->Next
) {
353 if (!T
->hasTriggered()) continue;
354 bool WasRunning
= T
->isRunning();
358 TimersToPrint
.emplace_back(T
->Time
, T
->Name
, T
->Description
);
368 void TimerGroup::print(raw_ostream
&OS
, bool ResetAfterPrint
) {
370 // After preparing the timers we can free the lock
371 sys::SmartScopedLock
<true> L(*TimerLock
);
372 prepareToPrintList(ResetAfterPrint
);
375 // If any timers were started, print the group.
376 if (!TimersToPrint
.empty())
377 PrintQueuedTimers(OS
);
380 void TimerGroup::clear() {
381 sys::SmartScopedLock
<true> L(*TimerLock
);
382 for (Timer
*T
= FirstTimer
; T
; T
= T
->Next
)
386 void TimerGroup::printAll(raw_ostream
&OS
) {
387 sys::SmartScopedLock
<true> L(*TimerLock
);
389 for (TimerGroup
*TG
= TimerGroupList
; TG
; TG
= TG
->Next
)
393 void TimerGroup::clearAll() {
394 sys::SmartScopedLock
<true> L(*TimerLock
);
395 for (TimerGroup
*TG
= TimerGroupList
; TG
; TG
= TG
->Next
)
399 void TimerGroup::printJSONValue(raw_ostream
&OS
, const PrintRecord
&R
,
400 const char *suffix
, double Value
) {
401 assert(yaml::needsQuotes(Name
) == yaml::QuotingType::None
&&
402 "TimerGroup name should not need quotes");
403 assert(yaml::needsQuotes(R
.Name
) == yaml::QuotingType::None
&&
404 "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
);
410 const char *TimerGroup::printJSONValues(raw_ostream
&OS
, const char *delim
) {
411 sys::SmartScopedLock
<true> L(*TimerLock
);
413 prepareToPrintList(false);
414 for (const PrintRecord
&R
: TimersToPrint
) {
418 const TimeRecord
&T
= R
.Time
;
419 printJSONValue(OS
, R
, ".wall", T
.getWallTime());
421 printJSONValue(OS
, R
, ".user", T
.getUserTime());
423 printJSONValue(OS
, R
, ".sys", T
.getSystemTime());
424 if (T
.getMemUsed()) {
426 printJSONValue(OS
, R
, ".mem", T
.getMemUsed());
429 TimersToPrint
.clear();
433 const 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
);
440 void TimerGroup::ConstructTimerLists() {
441 (void)*NamedGroupedTimers
;