Skip to content

[SourceKit] Report number of instructions executed since SourceKit was started in statistics request #37450

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

Merged
Merged
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
4 changes: 4 additions & 0 deletions include/swift/Basic/Statistic.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ class Stmt;
class TypeRepr;
struct FingerprintAndMembers;

/// Get the number of instructions executed since this process was launched.
/// Returns 0 if the number of instructions executed could not be determined.
uint64_t getInstructionsExecuted();

// There are a handful of cases where the swift compiler can introduce
// counter-measurement noise via nondeterminism, especially via
// parallelism; inhibiting all such cases reliably using existing avenues
Expand Down
25 changes: 15 additions & 10 deletions lib/Basic/Statistic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@
//
//===----------------------------------------------------------------------===//

#include "clang/AST/Decl.h"
#include "swift/Basic/Statistic.h"
#include "swift/Config.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "swift/Basic/Statistic.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/Config/config.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SaveAndRestore.h"
#include "llvm/Support/raw_ostream.h"
#include <chrono>
#include <limits>

Expand Down Expand Up @@ -59,6 +57,16 @@ bool environmentVariableRequestedMaximumDeterminism() {
return false;
}

uint64_t getInstructionsExecuted() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: It's nice to write swift:: explicitly to ensure it matches the header declaration.

#if defined(HAVE_PROC_PID_RUSAGE) && defined(RUSAGE_INFO_V4)
struct rusage_info_v4 ru;
if (proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru) == 0) {
return ru.ri_instructions;
}
#endif
return 0;
}

static std::string
makeFileName(StringRef Prefix,
StringRef ProgramName,
Expand Down Expand Up @@ -510,12 +518,9 @@ FrontendStatsTracer::~FrontendStatsTracer()
// 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.NumInstructionsExecuted = ru.ri_instructions;
if (auto instrExecuted = getInstructionsExecuted()) {
C.NumInstructionsExecuted = instrExecuted;
}
#endif

#if defined(HAVE_MALLOC_ZONE_STATISTICS) && defined(HAVE_MALLOC_MALLOC_H)
// On Darwin we have a lifetime max that's maintained by malloc we can
Expand Down
1 change: 1 addition & 0 deletions test/SourceKit/Misc/stats.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ func foo() {}

// RUN: %sourcekitd-test -req=syntax-map %s == -req=stats | %FileCheck %s -check-prefix=SYNTAX_1

// SYNTAX_1: {{.*}} source.statistic.instruction-count
// SYNTAX_1: 2 {{.*}} source.statistic.num-requests
// SYNTAX_1: 0 {{.*}} source.statistic.num-semantic-requests
// SYNTAX_1: 0 {{.*}} source.statistic.num-ast-builds
Expand Down
6 changes: 6 additions & 0 deletions tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "swift/Basic/ExponentialGrowthAppendingBinaryByteStream.h"
#include "swift/Basic/Mangler.h"
#include "swift/Basic/Statistic.h"
#include "swift/Basic/Version.h"
#include "swift/Demangling/Demangler.h"
#include "swift/Demangling/ManglingMacros.h"
Expand Down Expand Up @@ -918,6 +919,11 @@ void handleRequestImpl(sourcekitd_object_t ReqObj, ResponseReceiver Rec) {
dict.set(KeyValue, stat->value);
};

Statistic instructionCount(
UIdentFromSKDUID(KindStatInstructionCount),
"# of instructions executed since the SourceKit process was started");
instructionCount.value.store(swift::getInstructionsExecuted());
addStat(&instructionCount);
addStat(&numRequests);
addStat(&numSemaRequests);
std::for_each(stats.begin(), stats.end(), addStat);
Expand Down
3 changes: 2 additions & 1 deletion utils/gyb_sourcekit_support/UIDs.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ def __init__(self, internal_name, external_name):
KEY('ExpressionLength', 'key.expression_length'),
KEY('ExpressionType', 'key.expression_type'),
KEY('CanonicalizeType', 'key.canonicalize_type'),
KEY('InternalDiagnostic', "key.internal_diagnostic"),
KEY('InternalDiagnostic', 'key.internal_diagnostic'),
KEY('VFSName', 'key.vfs.name'),
KEY('VFSOptions', 'key.vfs.options'),
KEY('Files', 'key.files'),
Expand Down Expand Up @@ -453,6 +453,7 @@ def __init__(self, internal_name, external_name):
KIND('Unknown', 'source.syntacticrename.unknown'),
KIND('StatNumRequests', 'source.statistic.num-requests'),
KIND('StatNumSemaRequests', 'source.statistic.num-semantic-requests'),
KIND('StatInstructionCount', 'source.statistic.instruction-count'),
KIND('SyntaxTreeOff', 'source.syntaxtree.transfer.off'),
KIND('SyntaxTreeFull', 'source.syntaxtree.transfer.full'),
KIND('Swift', 'source.lang.swift'),
Expand Down