Skip to content

Commit cd48479

Browse files
authored
Merge pull request #9110 from graydon/grab-bag-of-metrics
Add an assortment of new "always-on" metrics.
2 parents 473b373 + b5292f0 commit cd48479

File tree

13 files changed

+246
-0
lines changed

13 files changed

+246
-0
lines changed

include/swift/AST/ASTContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ namespace swift {
103103
class SILBoxType;
104104
class TypeAliasDecl;
105105
class VarDecl;
106+
class UnifiedStatsReporter;
106107

107108
enum class KnownProtocolKind : uint8_t;
108109

@@ -241,6 +242,9 @@ class ASTContext {
241242
/// Cache of remapped types (useful for diagnostics).
242243
llvm::StringMap<Type> RemappedTypes;
243244

245+
/// Optional table of counters to report, nullptr when not collecting.
246+
UnifiedStatsReporter *Stats = nullptr;
247+
244248
private:
245249
/// \brief The current generation number, which reflects the number of
246250
/// times that external modules have been loaded.

include/swift/Basic/Statistic.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,38 @@ class UnifiedStatsReporter {
7171

7272
struct AlwaysOnFrontendCounters
7373
{
74+
size_t NumSourceBuffers;
75+
size_t NumLinkLibraries;
76+
size_t NumLoadedModules;
77+
size_t NumImportedExternalDefinitions;
78+
size_t NumTotalClangImportedEntities;
79+
size_t NumASTBytesAllocated;
80+
size_t NumDependencies;
81+
size_t NumReferencedTopLevelNames;
82+
size_t NumReferencedDynamicNames;
83+
size_t NumReferencedMemberNames;
84+
size_t NumDecls;
85+
size_t NumLocalTypeDecls;
86+
size_t NumObjCMethods;
87+
size_t NumInfixOperators;
88+
size_t NumPostfixOperators;
89+
size_t NumPrefixOperators;
90+
size_t NumPrecedenceGroups;
91+
size_t NumUsedConformances;
92+
93+
size_t NumConformancesDeserialized;
94+
size_t NumConstraintScopes;
95+
size_t NumDeclsDeserialized;
96+
size_t NumDeclsValidated;
97+
size_t NumFunctionsTypechecked;
98+
size_t NumGenericSignatureBuilders;
99+
size_t NumLazyGenericEnvironments;
100+
size_t NumLazyGenericEnvironmentsLoaded;
101+
size_t NumLazyIterableDeclContexts;
102+
size_t NumTypesDeserialized;
103+
size_t NumTypesValidated;
104+
size_t NumUnloadedLazyIterableDeclContexts;
105+
74106
size_t NumSILGenFunctions;
75107
size_t NumSILGenVtables;
76108
size_t NumSILGenWitnessTables;
@@ -82,6 +114,16 @@ class UnifiedStatsReporter {
82114
size_t NumSILOptWitnessTables;
83115
size_t NumSILOptDefaultWitnessTables;
84116
size_t NumSILOptGlobalVariables;
117+
118+
size_t NumIRGlobals;
119+
size_t NumIRFunctions;
120+
size_t NumIRAliases;
121+
size_t NumIRIFuncs;
122+
size_t NumIRNamedMetaData;
123+
size_t NumIRValueSymbols;
124+
size_t NumIRComdatSymbols;
125+
size_t NumIRBasicBlocks;
126+
size_t NumIRInsts;
85127
};
86128

87129
private:

lib/AST/Decl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include "llvm/Support/raw_ostream.h"
4444
#include "swift/Basic/Range.h"
4545
#include "swift/Basic/StringExtras.h"
46+
#include "swift/Basic/Statistic.h"
4647

4748
#include "clang/Basic/CharInfo.h"
4849
#include "clang/AST/Attr.h"
@@ -672,6 +673,9 @@ GenericContext::getLazyGenericEnvironmentSlow() const {
672673

673674
const_cast<GenericContext *>(this)->setGenericEnvironment(genericEnv);
674675
++NumLazyGenericEnvironmentsLoaded;
676+
// FIXME: (transitional) increment the redundant "always-on" counter.
677+
if (getASTContext().Stats)
678+
getASTContext().Stats->getFrontendCounters().NumLazyGenericEnvironmentsLoaded++;
675679
return genericEnv;
676680
}
677681

@@ -686,6 +690,10 @@ void GenericContext::setLazyGenericEnvironment(LazyMemberLoader *lazyLoader,
686690
contextData->genericEnvData = genericEnvData;
687691

688692
++NumLazyGenericEnvironments;
693+
// FIXME: (transitional) increment the redundant "always-on" counter.
694+
if (getASTContext().Stats)
695+
getASTContext().Stats->getFrontendCounters().NumLazyGenericEnvironments++;
696+
689697
}
690698

691699
ImportDecl *ImportDecl::create(ASTContext &Ctx, DeclContext *DC,

lib/AST/DeclContext.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/Module.h"
2121
#include "swift/AST/Types.h"
2222
#include "swift/Basic/SourceManager.h"
23+
#include "swift/Basic/Statistic.h"
2324
#include "llvm/ADT/DenseMap.h"
2425
#include "llvm/ADT/Statistic.h"
2526
#include "llvm/Support/raw_ostream.h"
@@ -888,6 +889,11 @@ void IterableDeclContext::setMemberLoader(LazyMemberLoader *loader,
888889

889890
++NumLazyIterableDeclContexts;
890891
++NumUnloadedLazyIterableDeclContexts;
892+
// FIXME: (transitional) increment the redundant "always-on" counter.
893+
if (ctx.Stats) {
894+
++ctx.Stats->getFrontendCounters().NumLazyIterableDeclContexts;
895+
++ctx.Stats->getFrontendCounters().NumUnloadedLazyIterableDeclContexts;
896+
}
891897
}
892898

893899
void IterableDeclContext::loadAllMembers() const {
@@ -915,6 +921,9 @@ void IterableDeclContext::loadAllMembers() const {
915921
contextInfo->memberData);
916922

917923
--NumUnloadedLazyIterableDeclContexts;
924+
// FIXME: (transitional) decrement the redundant "always-on" counter.
925+
if (ctx.Stats)
926+
ctx.Stats->getFrontendCounters().NumUnloadedLazyIterableDeclContexts--;
918927
}
919928

920929
bool IterableDeclContext::classof(const Decl *D) {

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "swift/AST/TypeRepr.h"
3030
#include "swift/AST/TypeWalker.h"
3131
#include "swift/Basic/Defer.h"
32+
#include "swift/Basic/Statistic.h"
3233
#include "llvm/ADT/SetVector.h"
3334
#include "llvm/ADT/SmallPtrSet.h"
3435
#include "llvm/ADT/SmallString.h"
@@ -2237,6 +2238,8 @@ GenericSignatureBuilder::GenericSignatureBuilder(
22372238
std::function<GenericFunction> lookupConformance)
22382239
: Context(ctx), Diags(Context.Diags), Impl(new Implementation) {
22392240
Impl->LookupConformance = std::move(lookupConformance);
2241+
if (Context.Stats)
2242+
Context.Stats->getFrontendCounters().NumGenericSignatureBuilders++;
22402243
}
22412244

22422245
GenericSignatureBuilder::GenericSignatureBuilder(GenericSignatureBuilder &&) = default;

lib/Basic/Statistic.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,39 @@ void
7474
UnifiedStatsReporter::publishAlwaysOnStatsToLLVM() {
7575
if (FrontendCounters) {
7676
auto &C = getFrontendCounters();
77+
78+
PUBLISH_STAT(C, "AST", NumSourceBuffers);
79+
PUBLISH_STAT(C, "AST", NumLinkLibraries);
80+
PUBLISH_STAT(C, "AST", NumLoadedModules);
81+
PUBLISH_STAT(C, "AST", NumImportedExternalDefinitions);
82+
PUBLISH_STAT(C, "AST", NumTotalClangImportedEntities);
83+
PUBLISH_STAT(C, "AST", NumASTBytesAllocated);
84+
PUBLISH_STAT(C, "AST", NumDependencies);
85+
PUBLISH_STAT(C, "AST", NumReferencedTopLevelNames);
86+
PUBLISH_STAT(C, "AST", NumReferencedDynamicNames);
87+
PUBLISH_STAT(C, "AST", NumReferencedMemberNames);
88+
PUBLISH_STAT(C, "AST", NumDecls);
89+
PUBLISH_STAT(C, "AST", NumLocalTypeDecls);
90+
PUBLISH_STAT(C, "AST", NumObjCMethods);
91+
PUBLISH_STAT(C, "AST", NumInfixOperators);
92+
PUBLISH_STAT(C, "AST", NumPostfixOperators);
93+
PUBLISH_STAT(C, "AST", NumPrefixOperators);
94+
PUBLISH_STAT(C, "AST", NumPrecedenceGroups);
95+
PUBLISH_STAT(C, "AST", NumUsedConformances);
96+
97+
PUBLISH_STAT(C, "Sema", NumConformancesDeserialized);
98+
PUBLISH_STAT(C, "Sema", NumConstraintScopes);
99+
PUBLISH_STAT(C, "Sema", NumDeclsDeserialized);
100+
PUBLISH_STAT(C, "Sema", NumDeclsValidated);
101+
PUBLISH_STAT(C, "Sema", NumFunctionsTypechecked);
102+
PUBLISH_STAT(C, "Sema", NumGenericSignatureBuilders);
103+
PUBLISH_STAT(C, "Sema", NumLazyGenericEnvironments);
104+
PUBLISH_STAT(C, "Sema", NumLazyGenericEnvironmentsLoaded);
105+
PUBLISH_STAT(C, "Sema", NumLazyIterableDeclContexts);
106+
PUBLISH_STAT(C, "Sema", NumTypesDeserialized);
107+
PUBLISH_STAT(C, "Sema", NumTypesValidated);
108+
PUBLISH_STAT(C, "Sema", NumUnloadedLazyIterableDeclContexts);
109+
77110
PUBLISH_STAT(C, "SILModule", NumSILGenFunctions);
78111
PUBLISH_STAT(C, "SILModule", NumSILGenVtables);
79112
PUBLISH_STAT(C, "SILModule", NumSILGenWitnessTables);
@@ -85,6 +118,16 @@ UnifiedStatsReporter::publishAlwaysOnStatsToLLVM() {
85118
PUBLISH_STAT(C, "SILModule", NumSILOptWitnessTables);
86119
PUBLISH_STAT(C, "SILModule", NumSILOptDefaultWitnessTables);
87120
PUBLISH_STAT(C, "SILModule", NumSILOptGlobalVariables);
121+
122+
PUBLISH_STAT(C, "IRModule", NumIRGlobals);
123+
PUBLISH_STAT(C, "IRModule", NumIRFunctions);
124+
PUBLISH_STAT(C, "IRModule", NumIRAliases);
125+
PUBLISH_STAT(C, "IRModule", NumIRIFuncs);
126+
PUBLISH_STAT(C, "IRModule", NumIRNamedMetaData);
127+
PUBLISH_STAT(C, "IRModule", NumIRValueSymbols);
128+
PUBLISH_STAT(C, "IRModule", NumIRComdatSymbols);
129+
PUBLISH_STAT(C, "IRModule", NumIRBasicBlocks);
130+
PUBLISH_STAT(C, "IRModule", NumIRInsts);
88131
}
89132
if (DriverCounters) {
90133
auto &C = getDriverCounters();
@@ -118,6 +161,39 @@ UnifiedStatsReporter::printAlwaysOnStatsAndTimers(raw_ostream &OS) {
118161
const char *delim = "";
119162
if (FrontendCounters) {
120163
auto &C = getFrontendCounters();
164+
165+
PRINT_STAT(OS, delim, C, "AST", NumSourceBuffers);
166+
PRINT_STAT(OS, delim, C, "AST", NumLinkLibraries);
167+
PRINT_STAT(OS, delim, C, "AST", NumLoadedModules);
168+
PRINT_STAT(OS, delim, C, "AST", NumImportedExternalDefinitions);
169+
PRINT_STAT(OS, delim, C, "AST", NumTotalClangImportedEntities);
170+
PRINT_STAT(OS, delim, C, "AST", NumASTBytesAllocated);
171+
PRINT_STAT(OS, delim, C, "AST", NumDependencies);
172+
PRINT_STAT(OS, delim, C, "AST", NumReferencedTopLevelNames);
173+
PRINT_STAT(OS, delim, C, "AST", NumReferencedDynamicNames);
174+
PRINT_STAT(OS, delim, C, "AST", NumReferencedMemberNames);
175+
PRINT_STAT(OS, delim, C, "AST", NumDecls);
176+
PRINT_STAT(OS, delim, C, "AST", NumLocalTypeDecls);
177+
PRINT_STAT(OS, delim, C, "AST", NumObjCMethods);
178+
PRINT_STAT(OS, delim, C, "AST", NumInfixOperators);
179+
PRINT_STAT(OS, delim, C, "AST", NumPostfixOperators);
180+
PRINT_STAT(OS, delim, C, "AST", NumPrefixOperators);
181+
PRINT_STAT(OS, delim, C, "AST", NumPrecedenceGroups);
182+
PRINT_STAT(OS, delim, C, "AST", NumUsedConformances);
183+
184+
PRINT_STAT(OS, delim, C, "Sema", NumConformancesDeserialized);
185+
PRINT_STAT(OS, delim, C, "Sema", NumConstraintScopes);
186+
PRINT_STAT(OS, delim, C, "Sema", NumDeclsDeserialized);
187+
PRINT_STAT(OS, delim, C, "Sema", NumDeclsValidated);
188+
PRINT_STAT(OS, delim, C, "Sema", NumFunctionsTypechecked);
189+
PRINT_STAT(OS, delim, C, "Sema", NumGenericSignatureBuilders);
190+
PRINT_STAT(OS, delim, C, "Sema", NumLazyGenericEnvironments);
191+
PRINT_STAT(OS, delim, C, "Sema", NumLazyGenericEnvironmentsLoaded);
192+
PRINT_STAT(OS, delim, C, "Sema", NumLazyIterableDeclContexts);
193+
PRINT_STAT(OS, delim, C, "Sema", NumTypesDeserialized);
194+
PRINT_STAT(OS, delim, C, "Sema", NumTypesValidated);
195+
PRINT_STAT(OS, delim, C, "Sema", NumUnloadedLazyIterableDeclContexts);
196+
121197
PRINT_STAT(OS, delim, C, "SILModule", NumSILGenFunctions);
122198
PRINT_STAT(OS, delim, C, "SILModule", NumSILGenVtables);
123199
PRINT_STAT(OS, delim, C, "SILModule", NumSILGenWitnessTables);
@@ -129,6 +205,16 @@ UnifiedStatsReporter::printAlwaysOnStatsAndTimers(raw_ostream &OS) {
129205
PRINT_STAT(OS, delim, C, "SILModule", NumSILOptWitnessTables);
130206
PRINT_STAT(OS, delim, C, "SILModule", NumSILOptDefaultWitnessTables);
131207
PRINT_STAT(OS, delim, C, "SILModule", NumSILOptGlobalVariables);
208+
209+
PRINT_STAT(OS, delim, C, "IRModule", NumIRGlobals);
210+
PRINT_STAT(OS, delim, C, "IRModule", NumIRFunctions);
211+
PRINT_STAT(OS, delim, C, "IRModule", NumIRAliases);
212+
PRINT_STAT(OS, delim, C, "IRModule", NumIRIFuncs);
213+
PRINT_STAT(OS, delim, C, "IRModule", NumIRNamedMetaData);
214+
PRINT_STAT(OS, delim, C, "IRModule", NumIRValueSymbols);
215+
PRINT_STAT(OS, delim, C, "IRModule", NumIRComdatSymbols);
216+
PRINT_STAT(OS, delim, C, "IRModule", NumIRBasicBlocks);
217+
PRINT_STAT(OS, delim, C, "IRModule", NumIRInsts);
132218
}
133219
if (DriverCounters) {
134220
auto &C = getDriverCounters();

lib/ClangImporter/ImportDecl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "clang/AST/ASTContext.h"
4343
#include "clang/AST/Attr.h"
4444
#include "clang/Basic/CharInfo.h"
45+
#include "swift/Basic/Statistic.h"
4546
#include "clang/Lex/Preprocessor.h"
4647
#include "clang/Sema/Lookup.h"
4748

@@ -7051,6 +7052,9 @@ ClangImporter::Implementation::importDeclImpl(const clang::NamedDecl *ClangDecl,
70517052
void ClangImporter::Implementation::startedImportingEntity() {
70527053
++NumCurrentImportingEntities;
70537054
++NumTotalImportedEntities;
7055+
// FIXME: (transitional) increment the redundant "always-on" counter.
7056+
if (SwiftContext.Stats)
7057+
SwiftContext.Stats->getFrontendCounters().NumTotalClangImportedEntities++;
70547058
}
70557059

70567060
void ClangImporter::Implementation::finishedImportingEntity() {

lib/FrontendTool/FrontendTool.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include "llvm/ADT/Statistic.h"
6464
#include "llvm/IR/LLVMContext.h"
6565
#include "llvm/IR/Module.h"
66+
#include "llvm/IR/ValueSymbolTable.h"
6667
#include "llvm/IRReader/IRReader.h"
6768
#include "llvm/Option/Option.h"
6869
#include "llvm/Option/OptTable.h"
@@ -347,6 +348,58 @@ static void debugFailWithCrash() {
347348
LLVM_BUILTIN_TRAP;
348349
}
349350

351+
static void countStatsPostSema(UnifiedStatsReporter &Stats,
352+
CompilerInstance& Instance) {
353+
auto &C = Stats.getFrontendCounters();
354+
C.NumSourceBuffers = Instance.getSourceMgr().getLLVMSourceMgr().getNumBuffers();
355+
C.NumLinkLibraries = Instance.getLinkLibraries().size();
356+
357+
auto const &AST = Instance.getASTContext();
358+
C.NumLoadedModules = AST.LoadedModules.size();
359+
C.NumImportedExternalDefinitions = AST.ExternalDefinitions.size();
360+
C.NumASTBytesAllocated = AST.getAllocator().getBytesAllocated();
361+
362+
if (auto *D = Instance.getDependencyTracker()) {
363+
C.NumDependencies = D->getDependencies().size();
364+
}
365+
366+
if (auto *R = Instance.getReferencedNameTracker()) {
367+
C.NumReferencedTopLevelNames = R->getTopLevelNames().size();
368+
C.NumReferencedDynamicNames = R->getDynamicLookupNames().size();
369+
C.NumReferencedMemberNames = R->getUsedMembers().size();
370+
}
371+
372+
if (auto *SF = Instance.getPrimarySourceFile()) {
373+
C.NumDecls = SF->Decls.size();
374+
C.NumLocalTypeDecls = SF->LocalTypeDecls.size();
375+
C.NumObjCMethods = SF->ObjCMethods.size();
376+
C.NumInfixOperators = SF->InfixOperators.size();
377+
C.NumPostfixOperators = SF->PostfixOperators.size();
378+
C.NumPrefixOperators = SF->PrefixOperators.size();
379+
C.NumPrecedenceGroups = SF->PrecedenceGroups.size();
380+
C.NumUsedConformances = SF->getUsedConformances().size();
381+
}
382+
}
383+
384+
static void countStatsPostIRGen(UnifiedStatsReporter &Stats,
385+
const llvm::Module& Module) {
386+
auto &C = Stats.getFrontendCounters();
387+
// FIXME: calculate these in constant time if possible.
388+
C.NumIRGlobals = Module.getGlobalList().size();
389+
C.NumIRFunctions = Module.getFunctionList().size();
390+
C.NumIRAliases = Module.getAliasList().size();
391+
C.NumIRIFuncs = Module.getIFuncList().size();
392+
C.NumIRNamedMetaData = Module.getNamedMDList().size();
393+
C.NumIRValueSymbols = Module.getValueSymbolTable().size();
394+
C.NumIRComdatSymbols = Module.getComdatSymbolTable().size();
395+
for (auto const &Func : Module) {
396+
for (auto const &BB : Func) {
397+
C.NumIRBasicBlocks++;
398+
C.NumIRInsts += BB.size();
399+
}
400+
}
401+
}
402+
350403
static void countStatsPostSILGen(UnifiedStatsReporter &Stats,
351404
const SILModule& Module) {
352405
auto &C = Stats.getFrontendCounters();
@@ -452,6 +505,10 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
452505
observer->performedSemanticAnalysis(*Instance);
453506
}
454507

508+
if (Stats) {
509+
countStatsPostSema(*Stats, *Instance);
510+
}
511+
455512
FrontendOptions::DebugCrashMode CrashMode = opts.CrashMode;
456513
if (CrashMode == FrontendOptions::DebugCrashMode::AssertAfterParse)
457514
debugFailWithAssertion();
@@ -829,6 +886,10 @@ static bool performCompile(std::unique_ptr<CompilerInstance> &Instance,
829886
return HadError;
830887
}
831888

889+
if (Stats) {
890+
countStatsPostIRGen(*Stats, *IRModule);
891+
}
892+
832893
if (opts.ValidateTBDAgainstIR) {
833894
auto hasMultipleIRGenThreads = Invocation.getSILOptions().NumThreads > 1;
834895
bool error;
@@ -1115,6 +1176,13 @@ int swift::performFrontend(ArrayRef<const char *> Args,
11151176
return 1;
11161177
}
11171178

1179+
if (StatsReporter) {
1180+
// Install stats-reporter somewhere visible for subsystems that
1181+
// need to bump counters as they work, rather than measure
1182+
// accumulated work on completion (mostly: TypeChecker).
1183+
Instance->getASTContext().Stats = StatsReporter.get();
1184+
}
1185+
11181186
// The compiler instance has been configured; notify our observer.
11191187
if (observer) {
11201188
observer->configuredCompiler(*Instance);

lib/Sema/ConstraintSystem.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ ConstraintSystem::~ConstraintSystem() {
4343
void ConstraintSystem::incrementScopeCounter() {
4444
SWIFT_FUNC_STAT;
4545
CountScopes++;
46+
// FIXME: (transitional) increment the redundant "always-on" counter.
47+
if (TC.Context.Stats)
48+
TC.Context.Stats->getFrontendCounters().NumConstraintScopes++;
4649
}
4750

4851
bool ConstraintSystem::hasFreeTypeVariables() {

0 commit comments

Comments
 (0)