Skip to content

[Timers] Add a flag to set a minimum timer value for printing #139306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions llvm/lib/Support/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Name2PairMap;
static std::string &libSupportInfoOutputFilename();
static bool trackSpace();
static bool sortTimers();
cl::opt<unsigned> &minPrintTime();
[[maybe_unused]]
static SignpostEmitter &signposts();
static sys::SmartMutex<true> &timerLock();
Expand Down Expand Up @@ -380,8 +381,13 @@ 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);
Expand Down Expand Up @@ -452,23 +458,20 @@ 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;
printJSONValue(OS, R, ".wall", T.getWallTime());
OS << delim;
printJSONValue(OS, R, ".user", T.getUserTime());
OS << delim;
printJSONValue(OS, R, ".sys", T.getSystemTime());
if (T.getMemUsed()) {
OS << delim;
printJSONValue(OS, R, ".mem", T.getMemUsed());
}
if (T.getInstructionsExecuted()) {
OS << delim;
printJSONValue(OS, R, ".instr", T.getInstructionsExecuted());
}
auto MaybePrintJSON = [&](double Value, const char *suffix,
double Threshold) {
if (Value > Threshold) {
OS << delim;
printJSONValue(OS, R, suffix, Value);
delim = ",\n";
}
};
MaybePrintJSON(T.getWallTime(), ".wall", minPrintTime());
MaybePrintJSON(T.getUserTime(), ".user", minPrintTime());
MaybePrintJSON(T.getSystemTime(), ".sys", minPrintTime());
MaybePrintJSON(T.getMemUsed(), ".mem", 0);
MaybePrintJSON(T.getInstructionsExecuted(), ".instr", 0);
}
TimersToPrint.clear();
return delim;
Expand Down Expand Up @@ -515,6 +518,10 @@ class llvm::TimerGlobals {
cl::desc("In the report, sort the timers in each group in wall clock"
" time order"),
cl::init(true), cl::Hidden};
cl::opt<unsigned> MinPrintTime{
"timer-min-print-time",
cl::desc("Minimum time in seconds for a timer to be printed"),
cl::init(0)};

sys::SmartMutex<true> TimerLock;
TimerGroup DefaultTimerGroup{"misc", "Miscellaneous Ungrouped Timers",
Expand All @@ -541,6 +548,7 @@ static std::string &libSupportInfoOutputFilename() {
}
static bool trackSpace() { return ManagedTimerGlobals->TrackSpace; }
static bool sortTimers() { return ManagedTimerGlobals->SortTimers; }
cl::opt<unsigned> &minPrintTime() { return ManagedTimerGlobals->MinPrintTime; }
static SignpostEmitter &signposts() { return ManagedTimerGlobals->Signposts; }
static sys::SmartMutex<true> &timerLock() {
return ManagedTimerGlobals->TimerLock;
Expand Down
44 changes: 39 additions & 5 deletions llvm/unittests/Support/TimerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//===----------------------------------------------------------------------===//

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

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

using namespace llvm;

cl::opt<unsigned> &minPrintTime();

namespace {

// FIXME: Put this somewhere in Support, it's also used in LockFileManager.
void SleepMS() {
void SleepMS(int ms = 1) {
#if _WIN32
Sleep(1);
Sleep(ms);
#else
struct timespec Interval;
Interval.tv_sec = 0;
Interval.tv_nsec = 1000000;
Interval.tv_sec = ms / 1000;
Interval.tv_nsec = 1000000 * (ms % 1000);
#if defined(__MVS__)
long Microseconds = (Interval.tv_nsec + 999) / 1000;
long Microseconds = (Interval.tv_nsec + Interval.tv_sec * 1000 + 999) / 1000;
usleep(Microseconds);
#else
nanosleep(&Interval, nullptr);
Expand Down Expand Up @@ -82,4 +86,34 @@ TEST(Timer, TimerGroupTimerDestructed) {
EXPECT_FALSE(testing::internal::GetCapturedStderr().empty());
}

TEST(Timer, MinTimerFlag) {
testing::internal::CaptureStderr();

Timer T1("T1", "T1");
Timer T2("T2", "T2");

minPrintTime().setValue(1);

T1.startTimer();
T2.startTimer();

SleepMS(500);
T1.stopTimer();

SleepMS(600);
T2.stopTimer();

TimerGroup::printAll(llvm::errs());
std::string stderr = testing::internal::GetCapturedStderr();
EXPECT_THAT(stderr, testing::HasSubstr("T2"));
EXPECT_THAT(stderr, testing::Not(testing::HasSubstr("T1")));

testing::internal::CaptureStderr();

TimerGroup::printAllJSONValues(llvm::errs(), "");
stderr = testing::internal::GetCapturedStderr();
EXPECT_THAT(stderr, testing::HasSubstr("T2.wall"));
EXPECT_THAT(stderr, testing::Not(testing::HasSubstr("T1.wall")));
}

} // namespace
Loading