Skip to content

Commit 7ce0e87

Browse files
committed
[Statistic] Refactor to make getInstructionsExecuted publically available
1 parent b0bade6 commit 7ce0e87

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

include/swift/Basic/Statistic.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ class Stmt;
7070
class TypeRepr;
7171
struct FingerprintAndMembers;
7272

73+
/// Get the number of instructions executed since this process was launched.
74+
/// Returns 0 if the number of instructions executed could not be determined.
75+
uint64_t getInstructionsExecuted();
76+
7377
// There are a handful of cases where the swift compiler can introduce
7478
// counter-measurement noise via nondeterminism, especially via
7579
// parallelism; inhibiting all such cases reliably using existing avenues

lib/Basic/Statistic.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "clang/AST/Decl.h"
13+
#include "swift/Basic/Statistic.h"
14+
#include "swift/Config.h"
1415
#include "clang/Basic/SourceLocation.h"
1516
#include "clang/Basic/SourceManager.h"
16-
#include "swift/Basic/Statistic.h"
17-
#include "swift/AST/Decl.h"
18-
#include "swift/AST/Expr.h"
1917
#include "llvm/ADT/DenseMap.h"
2018
#include "llvm/Config/config.h"
2119
#include "llvm/Support/FileSystem.h"
2220
#include "llvm/Support/Path.h"
2321
#include "llvm/Support/Process.h"
24-
#include "llvm/Support/raw_ostream.h"
2522
#include "llvm/Support/SaveAndRestore.h"
23+
#include "llvm/Support/raw_ostream.h"
2624
#include <chrono>
2725
#include <limits>
2826

@@ -59,6 +57,16 @@ bool environmentVariableRequestedMaximumDeterminism() {
5957
return false;
6058
}
6159

60+
uint64_t getInstructionsExecuted() {
61+
#if defined(HAVE_PROC_PID_RUSAGE) && defined(RUSAGE_INFO_V4)
62+
struct rusage_info_v4 ru;
63+
if (proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru) == 0) {
64+
return ru.ri_instructions;
65+
}
66+
#endif
67+
return 0;
68+
}
69+
6270
static std::string
6371
makeFileName(StringRef Prefix,
6472
StringRef ProgramName,
@@ -510,12 +518,9 @@ FrontendStatsTracer::~FrontendStatsTracer()
510518
// associated fields in the provided AlwaysOnFrontendCounters.
511519
void updateProcessWideFrontendCounters(
512520
UnifiedStatsReporter::AlwaysOnFrontendCounters &C) {
513-
#if defined(HAVE_PROC_PID_RUSAGE) && defined(RUSAGE_INFO_V4)
514-
struct rusage_info_v4 ru;
515-
if (0 == proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru)) {
516-
C.NumInstructionsExecuted = ru.ri_instructions;
521+
if (auto instrExecuted = getInstructionsExecuted()) {
522+
C.NumInstructionsExecuted = instrExecuted;
517523
}
518-
#endif
519524

520525
#if defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H)
521526
// On Darwin we have a lifetime max that's maintained by malloc we can

0 commit comments

Comments
 (0)