Skip to content

Commit bdb00fc

Browse files
committed
Add Frontend.NumInstructions statistic, populated on macOS by rusage_info_v4.
1 parent e68e087 commit bdb00fc

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,11 @@ cmake_pop_check_state()
898898

899899
check_symbol_exists(wait4 "sys/wait.h" HAVE_WAIT4)
900900

901+
check_symbol_exists(proc_pid_rusage "libproc.h" HAVE_PROC_PID_RUSAGE)
902+
if(HAVE_PROC_PID_RUSAGE)
903+
list(APPEND CMAKE_REQUIRED_LIBRARIES proc)
904+
endif()
905+
901906
if (LLVM_ENABLE_DOXYGEN)
902907
message(STATUS "Doxygen: enabled")
903908
endif()

include/swift/Basic/Statistics.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ DRIVER_STATISTIC(ChildrenMaxRSS)
8080
/// EXIT_SUCCESS.
8181
FRONTEND_STATISTIC(Frontend, NumProcessFailures)
8282

83+
/// Total instruction count in each frontend process.
84+
FRONTEND_STATISTIC(Frontend, NumInstructions)
85+
8386
/// Number of source buffers visible in the source manager.
8487
FRONTEND_STATISTIC(AST, NumSourceBuffers)
8588

include/swift/Config.h.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#cmakedefine HAVE_WAIT4 1
1212

13+
#cmakedefine HAVE_PROC_PID_RUSAGE 1
14+
1315
#cmakedefine01 SWIFT_DARWIN_ENABLE_STABLE_ABI_BIT
1416

1517
#endif // SWIFT_CONFIG_H

lib/Basic/Statistic.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@
2727
#include "llvm/Support/raw_ostream.h"
2828
#include <chrono>
2929
#include <limits>
30+
#include <unistd.h>
3031

3132
#ifdef HAVE_SYS_TIME_H
3233
#include <sys/time.h>
3334
#endif
3435
#ifdef HAVE_SYS_RESOURCE_H
3536
#include <sys/resource.h>
3637
#endif
38+
#ifdef HAVE_PROC_PID_RUSAGE
39+
#include <libproc.h>
40+
#endif
3741

3842
namespace swift {
3943
using namespace llvm;
@@ -481,6 +485,18 @@ FrontendStatsTracer::~FrontendStatsTracer()
481485
Reporter->saveAnyFrontendStatsEvents(*this, false);
482486
}
483487

488+
// Copy any interesting process-wide resource accounting stats to
489+
// associated fields in the provided AlwaysOnFrontendCounters.
490+
void updateProcessWideFrontendCounters(
491+
UnifiedStatsReporter::AlwaysOnFrontendCounters &C) {
492+
#if defined(HAVE_PROC_PID_RUSAGE) && defined(RUSAGE_INFO_V4)
493+
struct rusage_info_v4 ru;
494+
if (0 == proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru)) {
495+
C.NumInstructions = ru.ri_instructions;
496+
}
497+
#endif
498+
}
499+
484500
static inline void
485501
saveEvent(StringRef StatName,
486502
int64_t Curr, int64_t Last,
@@ -516,6 +532,7 @@ UnifiedStatsReporter::saveAnyFrontendStatsEvents(
516532
auto Now = llvm::TimeRecord::getCurrentTime();
517533
auto &Curr = getFrontendCounters();
518534
auto &Last = *LastTracedFrontendCounters;
535+
updateProcessWideFrontendCounters(Curr);
519536
if (EventProfilers) {
520537
auto TimeDelta = Now;
521538
TimeDelta -= EventProfilers->LastUpdated;
@@ -592,6 +609,8 @@ UnifiedStatsReporter::~UnifiedStatsReporter()
592609
}
593610
}
594611

612+
updateProcessWideFrontendCounters(getFrontendCounters());
613+
595614
// NB: Timer needs to be Optional<> because it needs to be destructed early;
596615
// LLVM will complain about double-stopping a timer if you tear down a
597616
// NamedRegionTimer after printing all timers. The printing routines were
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// REQUIRES: OS=macosx
2+
// RUN: %empty-directory(%t)
3+
// RUN: %target-swiftc_driver -o %t/main -module-name main -stats-output-dir %t %s
4+
// RUN: %{python} %utils/process-stats-dir.py --set-csv-baseline %t/frontend.csv %t
5+
// RUN: %FileCheck -input-file %t/frontend.csv %s
6+
// CHECK: {{"Frontend.NumInstructions" [1-9][0-9]*$}}
7+
8+
public func foo() {
9+
print("hello")
10+
}

0 commit comments

Comments
 (0)