Skip to content

Commit 7e3b9ab

Browse files
ahoppenTeemperor
authored andcommitted
[Timer] On macOS count number of executed instructions
In addition to wall time etc. this should allow us to get less noisy values for time measurements. Reviewed By: JDevlieghere Differential Revision: https://reviews.llvm.org/D96049
1 parent d96bb48 commit 7e3b9ab

File tree

5 files changed

+59
-14
lines changed

5 files changed

+59
-14
lines changed

clang-tools-extra/test/clang-tidy/infrastructure/clang-tidy-store-check-profile-one-tu.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
// CHECK-FILE-NEXT:"profile": {
1818
// CHECK-FILE-NEXT: "time.clang-tidy.readability-function-size.wall": {{.*}}{{[0-9]}}.{{[0-9]+}}e{{[-+]}}{{[0-9]}}{{[0-9]}},
1919
// CHECK-FILE-NEXT: "time.clang-tidy.readability-function-size.user": {{.*}}{{[0-9]}}.{{[0-9]+}}e{{[-+]}}{{[0-9]}}{{[0-9]}},
20-
// CHECK-FILE-NEXT: "time.clang-tidy.readability-function-size.sys": {{.*}}{{[0-9]}}.{{[0-9]+}}e{{[-+]}}{{[0-9]}}{{[0-9]}}
21-
// CHECK-FILE-NEXT: }
20+
// CHECK-FILE-NEXT: "time.clang-tidy.readability-function-size.sys": {{.*}}{{[0-9]}}.{{[0-9]+}}e{{[-+]}}{{[0-9]}}{{[0-9]}}{{,?}}
21+
// If available on the platform, we also have a "time.clang-tidy.readability-function-size.instr" entry
22+
// CHECK-FILE: }
2223
// CHECK-FILE-NEXT: }
2324

2425
// CHECK-FILE-NOT: {
@@ -27,7 +28,7 @@
2728
// CHECK-FILE-NOT: "profile": {
2829
// CHECK-FILE-NOT: "time.clang-tidy.readability-function-size.wall": {{.*}}{{[0-9]}}.{{[0-9]+}}e{{[-+]}}{{[0-9]}}{{[0-9]}},
2930
// CHECK-FILE-NOT: "time.clang-tidy.readability-function-size.user": {{.*}}{{[0-9]}}.{{[0-9]+}}e{{[-+]}}{{[0-9]}}{{[0-9]}},
30-
// CHECK-FILE-NOT: "time.clang-tidy.readability-function-size.sys": {{.*}}{{[0-9]}}.{{[0-9]+}}e{{[-+]}}{{[0-9]}}{{[0-9]}}
31+
// CHECK-FILE-NOT: "time.clang-tidy.readability-function-size.sys": {{.*}}{{[0-9]}}.{{[0-9]+}}e{{[-+]}}{{[0-9]}}{{[0-9]}}{{,?}}
3132
// CHECK-FILE-NOT: }
3233
// CHECK-FILE-NOT: }
3334

llvm/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,12 @@ if (LLVM_BUILD_STATIC)
649649
endforeach()
650650
endif()
651651

652+
include(CheckSymbolExists)
653+
check_symbol_exists(proc_pid_rusage "libproc.h" HAVE_PROC_PID_RUSAGE)
654+
if(HAVE_PROC_PID_RUSAGE)
655+
list(APPEND CMAKE_REQUIRED_LIBRARIES proc)
656+
endif()
657+
652658
# Use libtool instead of ar if you are both on an Apple host, and targeting Apple.
653659
if(CMAKE_HOST_APPLE AND APPLE)
654660
include(UseLibtool)

llvm/include/llvm/Config/config.h.cmake

+2
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,6 @@
353353
/* Whether Timers signpost passes in Xcode Instruments */
354354
#cmakedefine01 LLVM_SUPPORT_XCODE_SIGNPOSTS
355355

356+
#cmakedefine HAVE_PROC_PID_RUSAGE 1
357+
356358
#endif

llvm/include/llvm/Support/Timer.h

+17-11
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ class TimerGroup;
2424
class raw_ostream;
2525

2626
class TimeRecord {
27-
double WallTime; ///< Wall clock time elapsed in seconds.
28-
double UserTime; ///< User time elapsed.
29-
double SystemTime; ///< System time elapsed.
30-
ssize_t MemUsed; ///< Memory allocated (in bytes).
27+
double WallTime; ///< Wall clock time elapsed in seconds.
28+
double UserTime; ///< User time elapsed.
29+
double SystemTime; ///< System time elapsed.
30+
ssize_t MemUsed; ///< Memory allocated (in bytes).
31+
uint64_t InstructionsExecuted; ///< Number of instructions executed
3132
public:
32-
TimeRecord() : WallTime(0), UserTime(0), SystemTime(0), MemUsed(0) {}
33+
TimeRecord()
34+
: WallTime(0), UserTime(0), SystemTime(0), MemUsed(0),
35+
InstructionsExecuted(0) {}
3336

3437
/// Get the current time and memory usage. If Start is true we get the memory
3538
/// usage before the time, otherwise we get time before memory usage. This
@@ -42,23 +45,26 @@ class TimeRecord {
4245
double getSystemTime() const { return SystemTime; }
4346
double getWallTime() const { return WallTime; }
4447
ssize_t getMemUsed() const { return MemUsed; }
48+
uint64_t getInstructionsExecuted() const { return InstructionsExecuted; }
4549

4650
bool operator<(const TimeRecord &T) const {
4751
// Sort by Wall Time elapsed, as it is the only thing really accurate
4852
return WallTime < T.WallTime;
4953
}
5054

5155
void operator+=(const TimeRecord &RHS) {
52-
WallTime += RHS.WallTime;
53-
UserTime += RHS.UserTime;
56+
WallTime += RHS.WallTime;
57+
UserTime += RHS.UserTime;
5458
SystemTime += RHS.SystemTime;
55-
MemUsed += RHS.MemUsed;
59+
MemUsed += RHS.MemUsed;
60+
InstructionsExecuted += RHS.InstructionsExecuted;
5661
}
5762
void operator-=(const TimeRecord &RHS) {
58-
WallTime -= RHS.WallTime;
59-
UserTime -= RHS.UserTime;
63+
WallTime -= RHS.WallTime;
64+
UserTime -= RHS.UserTime;
6065
SystemTime -= RHS.SystemTime;
61-
MemUsed -= RHS.MemUsed;
66+
MemUsed -= RHS.MemUsed;
67+
InstructionsExecuted -= RHS.InstructionsExecuted;
6268
}
6369

6470
/// Print the current time record to \p OS, with a breakdown showing

llvm/lib/Support/Timer.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "llvm/Support/Timer.h"
1414
#include "llvm/ADT/Statistic.h"
1515
#include "llvm/ADT/StringMap.h"
16+
#include "llvm/Config/config.h"
1617
#include "llvm/Support/CommandLine.h"
1718
#include "llvm/Support/FileSystem.h"
1819
#include "llvm/Support/Format.h"
@@ -24,6 +25,14 @@
2425
#include "llvm/Support/raw_ostream.h"
2526
#include <limits>
2627

28+
#if HAVE_UNISTD_H
29+
#include <unistd.h>
30+
#endif
31+
32+
#ifdef HAVE_PROC_PID_RUSAGE
33+
#include <libproc.h>
34+
#endif
35+
2736
using namespace llvm;
2837

2938
// This ugly hack is brought to you courtesy of constructor/destructor ordering
@@ -120,6 +129,17 @@ static inline size_t getMemUsage() {
120129
return sys::Process::GetMallocUsage();
121130
}
122131

132+
static uint64_t getCurInstructionsExecuted() {
133+
#if defined(HAVE_UNISTD_H) && defined(HAVE_PROC_PID_RUSAGE) && \
134+
defined(RUSAGE_INFO_V4)
135+
struct rusage_info_v4 ru;
136+
if (proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru) == 0) {
137+
return ru.ri_instructions;
138+
}
139+
#endif
140+
return 0;
141+
}
142+
123143
TimeRecord TimeRecord::getCurrentTime(bool Start) {
124144
using Seconds = std::chrono::duration<double, std::ratio<1>>;
125145
TimeRecord Result;
@@ -128,9 +148,11 @@ TimeRecord TimeRecord::getCurrentTime(bool Start) {
128148

129149
if (Start) {
130150
Result.MemUsed = getMemUsage();
151+
Result.InstructionsExecuted = getCurInstructionsExecuted();
131152
sys::Process::GetTimeUsage(now, user, sys);
132153
} else {
133154
sys::Process::GetTimeUsage(now, user, sys);
155+
Result.InstructionsExecuted = getCurInstructionsExecuted();
134156
Result.MemUsed = getMemUsage();
135157
}
136158

@@ -180,6 +202,8 @@ void TimeRecord::print(const TimeRecord &Total, raw_ostream &OS) const {
180202

181203
if (Total.getMemUsed())
182204
OS << format("%9" PRId64 " ", (int64_t)getMemUsed());
205+
if (Total.getInstructionsExecuted())
206+
OS << format("%9" PRId64 " ", (int64_t)getInstructionsExecuted());
183207
}
184208

185209

@@ -339,6 +363,8 @@ void TimerGroup::PrintQueuedTimers(raw_ostream &OS) {
339363
OS << " ---Wall Time---";
340364
if (Total.getMemUsed())
341365
OS << " ---Mem---";
366+
if (Total.getInstructionsExecuted())
367+
OS << " ---Instr---";
342368
OS << " --- Name ---\n";
343369

344370
// Loop through all of the timing data, printing it out.
@@ -433,6 +459,10 @@ const char *TimerGroup::printJSONValues(raw_ostream &OS, const char *delim) {
433459
OS << delim;
434460
printJSONValue(OS, R, ".mem", T.getMemUsed());
435461
}
462+
if (T.getInstructionsExecuted()) {
463+
OS << delim;
464+
printJSONValue(OS, R, ".instr", T.getInstructionsExecuted());
465+
}
436466
}
437467
TimersToPrint.clear();
438468
return delim;

0 commit comments

Comments
 (0)