[Timers] Add a flag to set a minimum timer value for printing by alanzhao1 · Pull Request #139306 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-llvm-support

Author: Alan Zhao (alanzhao1)

Changes

The new LLVM flag -min-print-time is the number of seconds a timer must have in order for its value to be printed in either JSON or human readable formatting. This may be used, for example, with Clang's -ftime-report to reduce its output size by not printing insignificant values.

The default value of this flag is 0 which retains the current behavior of printing all timer values.


Full diff: https://github.com/llvm/llvm-project/pull/139306.diff

2 Files Affected:

diff --git a/llvm/lib/Support/Timer.cpp b/llvm/lib/Support/Timer.cpp index 22811d7b4af0a..9cc22fbb8bc38 100644 --- a/llvm/lib/Support/Timer.cpp +++ b/llvm/lib/Support/Timer.cpp @@ -53,6 +53,7 @@ class Name2PairMap; static std::string &libSupportInfoOutputFilename(); static bool trackSpace(); static bool sortTimers(); +cl::opt &minPrintTime(); [[maybe_unused]] static SignpostEmitter &signposts(); static sys::SmartMutex &timerLock(); @@ -380,8 +381,12 @@ void TimerGroup::PrintQueuedTimers(raw_ostream &OS) { // Loop through all of the timing data, printing it out. for (const PrintRecord &Record : llvm::reverse(TimersToPrint)) { - Record.Time.print(Total, OS); - OS << Record.Description << '\n'; + if (const TimeRecord &TR = Record.Time; TR.getUserTime() >= minPrintTime() || + TR.getSystemTime() >= minPrintTime() || + TR.getWallTime() >= minPrintTime()) { + Record.Time.print(Total, OS); + OS << Record.Description << '\n'; + } } Total.print(Total, OS); @@ -452,22 +457,31 @@ const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) { prepareToPrintList(false); for (const PrintRecord &R : TimersToPrint) { - OS << delim; - delim = ",\n";

 const TimeRecord &T = R.Time;

@@ -541,6 +558,7 @@ static std::string &libSupportInfoOutputFilename() { } static bool trackSpace() { return ManagedTimerGlobals->TrackSpace; } static bool sortTimers() { return ManagedTimerGlobals->SortTimers; } +cl::opt &minPrintTime() { return ManagedTimerGlobals->MinPrintTime; } static SignpostEmitter &signposts() { return ManagedTimerGlobals->Signposts; } static sys::SmartMutex &timerLock() { return ManagedTimerGlobals->TimerLock; diff --git a/llvm/unittests/Support/TimerTest.cpp b/llvm/unittests/Support/TimerTest.cpp index 612fd7231da70..0eb81f07bed5a 100644 --- a/llvm/unittests/Support/TimerTest.cpp +++ b/llvm/unittests/Support/TimerTest.cpp @@ -7,6 +7,8 @@ //===----------------------------------------------------------------------===//

#include "llvm/Support/Timer.h" +#include "llvm/Support/CommandLine.h" +#include "gmock/gmock.h" #include "gtest/gtest.h"

#if _WIN32 @@ -17,18 +19,20 @@

using namespace llvm;

+cl::opt &minPrintTime(); + namespace {

// FIXME: Put this somewhere in Support, it's also used in LockFileManager. -void SleepMS() { +void SleepMS(int ms = 1) { #if _WIN32

#else struct timespec Interval;

#if defined(MVS)

#else nanosleep(&Interval, nullptr); @@ -82,4 +86,34 @@ TEST(Timer, TimerGroupTimerDestructed) { EXPECT_FALSE(testing::internal::GetCapturedStderr().empty()); }

+TEST(Timer, MinTimerFlag) {