From 344a3780b2e33f6ca763666c380202b18aab72a3 Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Thu, 29 Jul 2021 22:15:26 +0200 Subject: Vendor import of llvm-project main 88e66fa60ae5, the last commit before the upstream release/13.x branch was created. --- llvm/lib/Support/Timer.cpp | 87 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 71 insertions(+), 16 deletions(-) (limited to 'llvm/lib/Support/Timer.cpp') diff --git a/llvm/lib/Support/Timer.cpp b/llvm/lib/Support/Timer.cpp index f5a512f9a22d..f025ecd3d45c 100644 --- a/llvm/lib/Support/Timer.cpp +++ b/llvm/lib/Support/Timer.cpp @@ -11,8 +11,12 @@ //===----------------------------------------------------------------------===// #include "llvm/Support/Timer.h" + +#include "DebugOptions.h" + #include "llvm/ADT/Statistic.h" #include "llvm/ADT/StringMap.h" +#include "llvm/Config/config.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Format.h" @@ -24,6 +28,14 @@ #include "llvm/Support/raw_ostream.h" #include +#if HAVE_UNISTD_H +#include +#endif + +#ifdef HAVE_PROC_PID_RUSAGE +#include +#endif + using namespace llvm; // This ugly hack is brought to you courtesy of constructor/destructor ordering @@ -44,20 +56,41 @@ static ManagedStatic > TimerLock; static ManagedStatic Signposts; namespace { - static cl::opt - TrackSpace("track-memory", cl::desc("Enable -time-passes memory " +struct CreateTrackSpace { + static void *call() { + return new cl::opt("track-memory", + cl::desc("Enable -time-passes memory " "tracking (this may be slow)"), - cl::Hidden); - - static cl::opt - InfoOutputFilename("info-output-file", cl::value_desc("filename"), - cl::desc("File to append -stats and -timer output to"), - cl::Hidden, cl::location(getLibSupportInfoOutputFilename())); + cl::Hidden); + } +}; +static ManagedStatic, CreateTrackSpace> TrackSpace; +struct CreateInfoOutputFilename { + static void *call() { + return new cl::opt( + "info-output-file", cl::value_desc("filename"), + cl::desc("File to append -stats and -timer output to"), cl::Hidden, + cl::location(getLibSupportInfoOutputFilename())); + } +}; +static ManagedStatic, CreateInfoOutputFilename> + InfoOutputFilename; +struct CreateSortTimers { + static void *call() { + return new cl::opt( + "sort-timers", + cl::desc("In the report, sort the timers in each group " + "in wall clock time order"), + cl::init(true), cl::Hidden); + } +}; +ManagedStatic, CreateSortTimers> SortTimers; +} // namespace - static cl::opt - SortTimers("sort-timers", cl::desc("In the report, sort the timers in each group " - "in wall clock time order"), - cl::init(true), cl::Hidden); +void llvm::initTimerOptions() { + *TrackSpace; + *InfoOutputFilename; + *SortTimers; } std::unique_ptr llvm::CreateInfoOutputFile() { @@ -73,7 +106,7 @@ std::unique_ptr llvm::CreateInfoOutputFile() { // info output file before running commands which write to it. std::error_code EC; auto Result = std::make_unique( - OutputFilename, EC, sys::fs::OF_Append | sys::fs::OF_Text); + OutputFilename, EC, sys::fs::OF_Append | sys::fs::OF_TextWithCRLF); if (!EC) return Result; @@ -116,10 +149,22 @@ Timer::~Timer() { } static inline size_t getMemUsage() { - if (!TrackSpace) return 0; + if (!*TrackSpace) + return 0; return sys::Process::GetMallocUsage(); } +static uint64_t getCurInstructionsExecuted() { +#if defined(HAVE_UNISTD_H) && defined(HAVE_PROC_PID_RUSAGE) && \ + defined(RUSAGE_INFO_V4) + struct rusage_info_v4 ru; + if (proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru) == 0) { + return ru.ri_instructions; + } +#endif + return 0; +} + TimeRecord TimeRecord::getCurrentTime(bool Start) { using Seconds = std::chrono::duration>; TimeRecord Result; @@ -128,9 +173,11 @@ TimeRecord TimeRecord::getCurrentTime(bool Start) { if (Start) { Result.MemUsed = getMemUsage(); + Result.InstructionsExecuted = getCurInstructionsExecuted(); sys::Process::GetTimeUsage(now, user, sys); } else { sys::Process::GetTimeUsage(now, user, sys); + Result.InstructionsExecuted = getCurInstructionsExecuted(); Result.MemUsed = getMemUsage(); } @@ -152,7 +199,7 @@ void Timer::stopTimer() { Running = false; Time += TimeRecord::getCurrentTime(false); Time -= StartTime; - Signposts->endInterval(this, getName()); + Signposts->endInterval(this); } void Timer::clear() { @@ -180,6 +227,8 @@ void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const { if (Total.getMemUsed()) OS << format("%9" PRId64 " ", (int64_t)getMemUsed()); + if (Total.getInstructionsExecuted()) + OS << format("%9" PRId64 " ", (int64_t)getInstructionsExecuted()); } @@ -307,7 +356,7 @@ void TimerGroup::addTimer(Timer &T) { void TimerGroup::PrintQueuedTimers(raw_ostream &OS) { // Perhaps sort the timers in descending order by amount of time taken. - if (SortTimers) + if (*SortTimers) llvm::sort(TimersToPrint); TimeRecord Total; @@ -339,6 +388,8 @@ void TimerGroup::PrintQueuedTimers(raw_ostream &OS) { OS << " ---Wall Time---"; if (Total.getMemUsed()) OS << " ---Mem---"; + if (Total.getInstructionsExecuted()) + OS << " ---Instr---"; OS << " --- Name ---\n"; // Loop through all of the timing data, printing it out. @@ -433,6 +484,10 @@ const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) { OS << delim; printJSONValue(OS, R, ".mem", T.getMemUsed()); } + if (T.getInstructionsExecuted()) { + OS << delim; + printJSONValue(OS, R, ".instr", T.getInstructionsExecuted()); + } } TimersToPrint.clear(); return delim; -- cgit v1.2.3