45 TrackSpace(
"track-memory",
cl::desc(
"Enable -time-passes memory " 46 "tracking (this may be slow)"),
51 cl::desc(
"File to append -stats and -timer output to"),
57 if (OutputFilename.empty())
58 return llvm::make_unique<raw_fd_ostream>(2,
false);
59 if (OutputFilename ==
"-")
60 return llvm::make_unique<raw_fd_ostream>(1,
false);
67 auto Result = llvm::make_unique<raw_fd_ostream>(
72 errs() <<
"Error opening info-output-file '" 73 << OutputFilename <<
" for appending!\n";
74 return llvm::make_unique<raw_fd_ostream>(2,
false);
78 struct CreateDefaultTimerGroup {
80 return new TimerGroup(
"misc",
"Miscellaneous Ungrouped Timers");
96 assert(!TG &&
"Timer already initialized");
97 this->Name.assign(Name.
begin(), Name.
end());
98 this->Description.assign(Description.
begin(), Description.
end());
99 Running = Triggered =
false;
106 TG->removeTimer(*
this);
110 if (!TrackSpace)
return 0;
115 using Seconds = std::chrono::duration<double, std::ratio<1>>;
118 std::chrono::nanoseconds user, sys;
128 Result.WallTime = Seconds(now.time_since_epoch()).
count();
129 Result.UserTime = Seconds(user).count();
130 Result.SystemTime = Seconds(sys).count();
135 assert(!Running &&
"Cannot start a running timer");
136 Running = Triggered =
true;
141 assert(Running &&
"Cannot stop a paused timer");
148 Running = Triggered =
false;
156 OS <<
format(
" %7.4f (%5.1f%%)", Val, Val*100/Total);
171 OS <<
format(
"%9" PRId64
" ", (int64_t)getMemUsed());
187 for (
StringMap<std::pair<TimerGroup*, Name2TimerMap> >::iterator
189 delete I->second.first;
196 std::pair<TimerGroup*, Name2TimerMap> &GroupEntry = Map[GroupName];
198 if (!GroupEntry.first)
199 GroupEntry.first =
new TimerGroup(GroupName, GroupDescription);
203 T.
init(Name, Description, *GroupEntry.first);
216 : &NamedGroupedTimers->get(Name, Description, GroupName,
217 GroupDescription)) {}
229 Description(Description.
begin(), Description.
end()) {
233 TimerGroupList->Prev = &Next;
236 TimerGroupList =
this;
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");
252 removeTimer(*FirstTimer);
262 void TimerGroup::removeTimer(
Timer &
T) {
267 TimersToPrint.emplace_back(T.Time, T.Name, T.Description);
274 T.Next->Prev = T.Prev;
278 if (FirstTimer || TimersToPrint.empty())
282 PrintQueuedTimers(*OutStream);
285 void TimerGroup::addTimer(
Timer &T) {
290 FirstTimer->Prev = &T.Next;
292 T.Prev = &FirstTimer;
296 void TimerGroup::PrintQueuedTimers(
raw_ostream &OS) {
301 for (
const PrintRecord &
Record : TimersToPrint)
305 OS <<
"===" << std::string(73,
'-') <<
"===\n";
307 unsigned Padding = (80-Description.length())/2;
308 if (Padding > 80) Padding = 0;
309 OS.
indent(Padding) << Description <<
'\n';
310 OS <<
"===" << std::string(73,
'-') <<
"===\n";
316 OS <<
format(
" Total Execution Time: %5.4f seconds (%5.4f wall clock)\n",
321 OS <<
" ---User Time---";
323 OS <<
" --System Time--";
325 OS <<
" --User+System--";
326 OS <<
" ---Wall Time---";
329 OS <<
" --- Name ---\n";
333 TimersToPrint.rend())) {
335 OS <<
Record.Description <<
'\n';
338 Total.
print(Total, OS);
342 TimersToPrint.clear();
345 void TimerGroup::prepareToPrintList() {
347 for (
Timer *T = FirstTimer;
T; T = T->Next) {
353 TimersToPrint.emplace_back(T->Time, T->Name, T->Description);
363 prepareToPrintList();
366 if (!TimersToPrint.empty())
367 PrintQueuedTimers(OS);
372 for (
Timer *T = FirstTimer;
T; T = T->Next)
379 for (
TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
385 for (
TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
389 void TimerGroup::printJSONValue(
raw_ostream &OS,
const PrintRecord &R,
390 const char *suffix,
double Value) {
392 "TimerGroup name should not need quotes");
394 "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);
403 prepareToPrintList();
404 for (
const PrintRecord &R : TimersToPrint) {
416 printJSONValue(OS, R,
".mem", T.
getMemUsed());
419 TimersToPrint.clear();
425 for (
TimerGroup *TG = TimerGroupList; TG; TG = TG->Next)
426 delim = TG->printJSONValues(OS, delim);
431 (void)*NamedGroupedTimers;
const_iterator end(StringRef path)
Get end iterator over path.
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.
const char * printJSONValues(raw_ostream &OS, const char *delim)
void init(StringRef Name, StringRef Description)
This class represents lattice values for constants.
void print(raw_ostream &OS) const
void stopTimer()
Stop the timer.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
bool isRunning() const
Check if the timer is currently running.
The TimeRegion class is used as a helper class to call the startTimer() and stopTimer() methods of th...
amdgpu Simplify well known AMD library false Value Value const Twine & Name
double getUserTime() 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...
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...
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.
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
static TimerGroup * TimerGroupList
This is the global list of TimerGroups, maintained by the TimerGroup ctor/dtor and is protected by th...
initializer< Ty > init(const Ty &Val)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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.
void sort(IteratorTy Start, IteratorTy End)
ssize_t getMemUsed() const
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
double getProcessTime() const
static TimerGroup * getDefaultTimerGroup()
double getSystemTime() const
bool isInitialized() const
void startTimer()
Start the timer running.
static void clearAll()
Clear out all timers.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
static std::string & getLibSupportInfoOutputFilename()
void clear()
Clear all timers in this group.
static ManagedStatic< TimerGroup, CreateDefaultTimerGroup > DefaultTimerGroup
static const char * printAllJSONValues(raw_ostream &OS, const char *delim)
Prints all timers as JSON key/value pairs.
Provides a library for accessing information about this process and other processes on the operating ...
The TimerGroup class is used to group together related timers into a single report that is printed wh...
double getWallTime() const
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.
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...
LocationClass< Ty > location(Ty &L)
static ManagedStatic< sys::SmartMutex< true > > TimerLock
bool hasTriggered() const
Check if startTimer() has ever been called on this timer.
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.