43 TrackSpace(
"track-memory",
cl::desc(
"Enable -time-passes memory "
44 "tracking (this may be slow)"),
49 cl::desc(
"File to append -stats and -timer output to"),
55 if (OutputFilename.empty())
56 return llvm::make_unique<raw_fd_ostream>(2,
false);
57 if (OutputFilename ==
"-")
58 return llvm::make_unique<raw_fd_ostream>(1,
false);
65 auto Result = llvm::make_unique<raw_fd_ostream>(
70 errs() <<
"Error opening info-output-file '"
71 << OutputFilename <<
" for appending!\n";
72 return llvm::make_unique<raw_fd_ostream>(2,
false);
76 struct CreateDefaultTimerGroup {
78 return new TimerGroup(
"misc",
"Miscellaneous Ungrouped Timers");
94 assert(!TG &&
"Timer already initialized");
95 this->Name.assign(Name.
begin(), Name.
end());
96 this->Description.assign(Description.
begin(), Description.
end());
97 Running = Triggered =
false;
104 TG->removeTimer(*
this);
108 if (!TrackSpace)
return 0;
113 using Seconds = std::chrono::duration<double, std::ratio<1>>;
116 std::chrono::nanoseconds user, sys;
126 Result.WallTime = Seconds(now.time_since_epoch()).
count();
127 Result.UserTime = Seconds(user).count();
128 Result.SystemTime = Seconds(sys).count();
133 assert(!Running &&
"Cannot start a running timer");
134 Running = Triggered =
true;
139 assert(Running &&
"Cannot stop a paused timer");
146 Running = Triggered =
false;
154 OS <<
format(
" %7.4f (%5.1f%%)", Val, Val*100/Total);
185 for (
StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
186 I = Map.begin(),
E = Map.end();
I !=
E; ++
I)
187 delete I->second.first;
194 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
196 if (!GroupEntry.first)
197 GroupEntry.first =
new TimerGroup(GroupName, GroupDescription);
201 T.
init(
Name, Description, *GroupEntry.first);
215 GroupDescription)) {}
227 Description(Description.
begin(), Description.
end()) {
241 removeTimer(*FirstTimer);
251 void TimerGroup::removeTimer(
Timer &
T) {
256 TimersToPrint.emplace_back(T.Time, T.Name, T.Description);
263 T.Next->Prev = T.Prev;
267 if (FirstTimer || TimersToPrint.empty())
271 PrintQueuedTimers(*OutStream);
274 void TimerGroup::addTimer(
Timer &T) {
279 FirstTimer->Prev = &T.Next;
281 T.Prev = &FirstTimer;
285 void TimerGroup::PrintQueuedTimers(
raw_ostream &OS) {
287 std::sort(TimersToPrint.begin(), TimersToPrint.end());
290 for (
const PrintRecord &
Record : TimersToPrint)
294 OS <<
"===" << std::string(73,
'-') <<
"===\n";
296 unsigned Padding = (80-Description.length())/2;
297 if (Padding > 80) Padding = 0;
298 OS.
indent(Padding) << Description <<
'\n';
299 OS <<
"===" << std::string(73,
'-') <<
"===\n";
305 OS <<
format(
" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
306 Total.getProcessTime(), Total.getWallTime());
309 if (Total.getUserTime())
310 OS <<
" ---User Time---";
311 if (Total.getSystemTime())
312 OS <<
" --System Time--";
313 if (Total.getProcessTime())
314 OS <<
" --User+System--";
315 OS <<
" ---Wall Time---";
316 if (Total.getMemUsed())
318 OS <<
" --- Name ---\n";
322 TimersToPrint.rend())) {
324 OS <<
Record.Description <<
'\n';
327 Total.
print(Total, OS);
331 TimersToPrint.clear();
334 void TimerGroup::prepareToPrintList() {
337 for (
Timer *T = FirstTimer;
T; T = T->Next) {
339 TimersToPrint.emplace_back(T->Time, T->Name, T->Description);
349 prepareToPrintList();
352 if (!TimersToPrint.empty())
353 PrintQueuedTimers(OS);
363 void TimerGroup::printJSONValue(
raw_ostream &OS,
const PrintRecord &R,
364 const char *suffix,
double Value) {
367 OS <<
"\t\"time." << Name <<
'.' << R.Name << suffix <<
"\": " << Value;
370 const char *TimerGroup::printJSONValues(
raw_ostream &OS,
const char *delim) {
371 prepareToPrintList();
372 for (
const PrintRecord &R : TimersToPrint) {
383 TimersToPrint.clear();
390 delim = TG->printJSONValues(OS, delim);
bool isInitialized() const
const_iterator end(StringRef path)
Get end iterator over path.
F_Append - When opening a file, if it already exists append to the existing file instead of returning...
void print(const TimeRecord &Total, raw_ostream &OS) const
Print the current time record to OS, with a breakdown showing contributions to the Total time record...
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
static TimeRecord getCurrentTime(bool Start=true)
Get the current time and memory usage.
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
void init(StringRef Name, StringRef Description)
void stopTimer()
Stop the timer.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
bool needsQuotes(StringRef S)
The TimeRegion class is used as a helper class to call the startTimer() and stopTimer() methods of th...
double getProcessTime() const
double getWallTime() const
static cl::opt< std::string > OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), cl::init("-"))
This class is used to track the amount of time spent between invocations of its startTimer()/stopTime...
double getUserTime() const
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(std::begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
static void printVal(double Val, double Total, raw_ostream &OS)
std::unique_ptr< raw_fd_ostream > CreateInfoOutputFile()
Return a file stream to print our output on.
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
static TimerGroup * TimerGroupList
This is the global list of TimerGroups, maintained by the TimerGroup ctor/dtor and is protected by th...
ssize_t getMemUsed() const
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
void clear()
Clear the timer state.
static ManagedStatic< std::string > LibSupportInfoOutputFilename
static ManagedStatic< Name2PairMap > NamedGroupedTimers
static size_t GetMallocUsage()
Return process memory usage.
void print(raw_ostream &OS)
Print any started timers in this group and zero them.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
static TimerGroup * getDefaultTimerGroup()
double getSystemTime() const
void startTimer()
Start the timer running.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
static std::string & getLibSupportInfoOutputFilename()
The file should be opened in text mode on platforms that make this distinction.
static ManagedStatic< TimerGroup, CreateDefaultTimerGroup > DefaultTimerGroup
bool hasTriggered() const
Check if startTimer() has ever been called on this timer.
static const char * printAllJSONValues(raw_ostream &OS, const char *delim)
Prints all timers as JSON key/value pairs, and clears them all out.
Provides a library for accessing information about this process and other processes on the operating ...
void print(raw_ostream &OS) const
The TimerGroup class is used to group together related timers into a single report that is printed wh...
static void GetTimeUsage(TimePoint<> &elapsed, std::chrono::nanoseconds &user_time, std::chrono::nanoseconds &sys_time)
This static function will set user_time to the amount of CPU time spent in user (non-kernel) mode and...
NamedRegionTimer(StringRef Name, StringRef Description, StringRef GroupName, StringRef GroupDescription, bool Enabled=true)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void ConstructTimerLists()
Ensure global timer group lists are initialized.
LLVM Value Representation.
static void printAll(raw_ostream &OS)
This static method prints all timers and clears them all out.
This class implements an extremely fast bulk output stream that can only output to a stream...
static size_t getMemUsage()
StringRef - Represent a constant reference to a string, i.e.
ManagedStatic - This transparently changes the behavior of global statics to be lazily constructed on...
void sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
LocationClass< Ty > location(Ty &L)
static ManagedStatic< sys::SmartMutex< true > > TimerLock
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.