Skip to content

Commit

Permalink
Add Frontend.NumInstructions statistic, populated on macOS by rusage_…
Browse files Browse the repository at this point in the history
…info_v4.
  • Loading branch information
graydon committed Aug 12, 2018
1 parent e68e087 commit bdb00fc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,11 @@ cmake_pop_check_state()

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

check_symbol_exists(proc_pid_rusage "libproc.h" HAVE_PROC_PID_RUSAGE)
if(HAVE_PROC_PID_RUSAGE)
list(APPEND CMAKE_REQUIRED_LIBRARIES proc)
endif()

if (LLVM_ENABLE_DOXYGEN)
message(STATUS "Doxygen: enabled")
endif()
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Basic/Statistics.def
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ DRIVER_STATISTIC(ChildrenMaxRSS)
/// EXIT_SUCCESS.
FRONTEND_STATISTIC(Frontend, NumProcessFailures)

/// Total instruction count in each frontend process.
FRONTEND_STATISTIC(Frontend, NumInstructions)

/// Number of source buffers visible in the source manager.
FRONTEND_STATISTIC(AST, NumSourceBuffers)

Expand Down
2 changes: 2 additions & 0 deletions include/swift/Config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#cmakedefine HAVE_WAIT4 1

#cmakedefine HAVE_PROC_PID_RUSAGE 1

#cmakedefine01 SWIFT_DARWIN_ENABLE_STABLE_ABI_BIT

#endif // SWIFT_CONFIG_H
19 changes: 19 additions & 0 deletions lib/Basic/Statistic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@
#include "llvm/Support/raw_ostream.h"
#include <chrono>
#include <limits>
#include <unistd.h>

#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#ifdef HAVE_PROC_PID_RUSAGE
#include <libproc.h>
#endif

namespace swift {
using namespace llvm;
Expand Down Expand Up @@ -481,6 +485,18 @@ FrontendStatsTracer::~FrontendStatsTracer()
Reporter->saveAnyFrontendStatsEvents(*this, false);
}

// Copy any interesting process-wide resource accounting stats to
// associated fields in the provided AlwaysOnFrontendCounters.
void updateProcessWideFrontendCounters(
UnifiedStatsReporter::AlwaysOnFrontendCounters &C) {
#if defined(HAVE_PROC_PID_RUSAGE) && defined(RUSAGE_INFO_V4)
struct rusage_info_v4 ru;
if (0 == proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru)) {
C.NumInstructions = ru.ri_instructions;
}
#endif
}

static inline void
saveEvent(StringRef StatName,
int64_t Curr, int64_t Last,
Expand Down Expand Up @@ -516,6 +532,7 @@ UnifiedStatsReporter::saveAnyFrontendStatsEvents(
auto Now = llvm::TimeRecord::getCurrentTime();
auto &Curr = getFrontendCounters();
auto &Last = *LastTracedFrontendCounters;
updateProcessWideFrontendCounters(Curr);
if (EventProfilers) {
auto TimeDelta = Now;
TimeDelta -= EventProfilers->LastUpdated;
Expand Down Expand Up @@ -592,6 +609,8 @@ UnifiedStatsReporter::~UnifiedStatsReporter()
}
}

updateProcessWideFrontendCounters(getFrontendCounters());

// NB: Timer needs to be Optional<> because it needs to be destructed early;
// LLVM will complain about double-stopping a timer if you tear down a
// NamedRegionTimer after printing all timers. The printing routines were
Expand Down
10 changes: 10 additions & 0 deletions test/Misc/stats_dir_instructions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// REQUIRES: OS=macosx
// RUN: %empty-directory(%t)
// RUN: %target-swiftc_driver -o %t/main -module-name main -stats-output-dir %t %s
// RUN: %{python} %utils/process-stats-dir.py --set-csv-baseline %t/frontend.csv %t
// RUN: %FileCheck -input-file %t/frontend.csv %s
// CHECK: {{"Frontend.NumInstructions" [1-9][0-9]*$}}

public func foo() {
print("hello")
}

0 comments on commit bdb00fc

Please sign in to comment.