Skip to content

Commit 80d3ded

Browse files
authored
Merge pull request swiftlang#37450 from ahoppen/pr/count-instructions-for-completion-request
[SourceKit] Report number of instructions executed since SourceKit was started in statistics request
2 parents f1df707 + e8b5bca commit 80d3ded

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
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

test/SourceKit/Misc/stats.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ func foo() {}
22

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

5+
// SYNTAX_1: {{.*}} source.statistic.instruction-count
56
// SYNTAX_1: 2 {{.*}} source.statistic.num-requests
67
// SYNTAX_1: 0 {{.*}} source.statistic.num-semantic-requests
78
// SYNTAX_1: 0 {{.*}} source.statistic.num-ast-builds

tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "swift/Basic/ExponentialGrowthAppendingBinaryByteStream.h"
3131
#include "swift/Basic/Mangler.h"
32+
#include "swift/Basic/Statistic.h"
3233
#include "swift/Basic/Version.h"
3334
#include "swift/Demangling/Demangler.h"
3435
#include "swift/Demangling/ManglingMacros.h"
@@ -918,6 +919,11 @@ void handleRequestImpl(sourcekitd_object_t ReqObj, ResponseReceiver Rec) {
918919
dict.set(KeyValue, stat->value);
919920
};
920921

922+
Statistic instructionCount(
923+
UIdentFromSKDUID(KindStatInstructionCount),
924+
"# of instructions executed since the SourceKit process was started");
925+
instructionCount.value.store(swift::getInstructionsExecuted());
926+
addStat(&instructionCount);
921927
addStat(&numRequests);
922928
addStat(&numSemaRequests);
923929
std::for_each(stats.begin(), stats.end(), addStat);

utils/gyb_sourcekit_support/UIDs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def __init__(self, internal_name, external_name):
178178
KEY('ExpressionLength', 'key.expression_length'),
179179
KEY('ExpressionType', 'key.expression_type'),
180180
KEY('CanonicalizeType', 'key.canonicalize_type'),
181-
KEY('InternalDiagnostic', "key.internal_diagnostic"),
181+
KEY('InternalDiagnostic', 'key.internal_diagnostic'),
182182
KEY('VFSName', 'key.vfs.name'),
183183
KEY('VFSOptions', 'key.vfs.options'),
184184
KEY('Files', 'key.files'),
@@ -457,6 +457,7 @@ def __init__(self, internal_name, external_name):
457457
KIND('Unknown', 'source.syntacticrename.unknown'),
458458
KIND('StatNumRequests', 'source.statistic.num-requests'),
459459
KIND('StatNumSemaRequests', 'source.statistic.num-semantic-requests'),
460+
KIND('StatInstructionCount', 'source.statistic.instruction-count'),
460461
KIND('SyntaxTreeOff', 'source.syntaxtree.transfer.off'),
461462
KIND('SyntaxTreeFull', 'source.syntaxtree.transfer.full'),
462463
KIND('Swift', 'source.lang.swift'),

0 commit comments

Comments
 (0)