Skip to content

Commit 1349167

Browse files
committed
[Dependency Scanning] Add tracking of the number of dependency queries and emit them as remarks
This change adds collection of three metrics to the scanner: - number of Swift module lookups - number of named Clang module lookups - recorded number of Clang modules which were imported into a Swift module by name It re-uses the prior '-Rdependency-scan-cache', renaming it to '-Rdependency-scan' and adds emission of the above metrics as remarks when this flag is enabled. Followup changes will add further remarks about dependency scanner progress.
1 parent a859ebe commit 1349167

21 files changed

+208
-98
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,6 +2432,22 @@ WARNING(dependency_scan_module_incompatible, none,
24322432
"module file '%0' is incompatible with this Swift compiler: %1",
24332433
(StringRef, StringRef))
24342434

2435+
REMARK(dependency_scan_number_swift_queries, none,
2436+
"Number of Swift module queries: '%0'",
2437+
(uint32_t))
2438+
REMARK(dependency_scan_number_named_clang_queries, none,
2439+
"Number of named Clang module queries: '%0'",
2440+
(uint32_t))
2441+
REMARK(dependency_scan_number_swift_dependencies, none,
2442+
"Number of recorded Swift module dependencies: '%0'",
2443+
(uint32_t))
2444+
REMARK(dependency_scan_number_named_clang_dependencies, none,
2445+
"Number of recorded Clang module dependencies queried by-name from a Swift client: '%0'",
2446+
(uint32_t))
2447+
REMARK(dependency_scan_number_clang_dependencies, none,
2448+
"Number of recorded Clang module dependencies: '%0'",
2449+
(uint32_t))
2450+
24352451
ERROR(clang_dependency_scan_error, none, "clang dependency scanning failure: %0", (StringRef))
24362452

24372453
ERROR(clang_header_dependency_scan_error, none, "bridging header dependency scanning failure: %0", (StringRef))

include/swift/AST/ModuleDependencies.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,11 @@ class ModuleDependenciesCache {
11111111
bool hasDependency(StringRef moduleName) const;
11121112
/// Whether we have cached dependency information for the given Swift module.
11131113
bool hasSwiftDependency(StringRef moduleName) const;
1114+
/// Report the number of recorded Clang dependencies
1115+
int numberOfClangDependencies() const;
1116+
/// Report the number of recorded Swift dependencies
1117+
/// (Textual + Binary)
1118+
int numberOfSwiftDependencies() const;
11141119

11151120
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &
11161121
getAlreadySeenClangModules() const {

include/swift/DependencyScan/ModuleDependencyScanner.h

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ using ImportStatementInfoMap =
3737
std::unordered_map<ModuleDependencyID,
3838
std::vector<ScannerImportStatementInfo>>;
3939

40+
struct ScannerMetrics {
41+
/// Number of performed queries for a Swift dependency with a given name
42+
std::atomic<uint32_t> SwiftModuleQueries;
43+
/// Number of performed queries for a Clang dependency with a given name
44+
std::atomic<uint32_t> NamedClangModuleQueries;
45+
/// Number of discovered Clang module dependencies which are directly
46+
/// imported from a Swift module by-name
47+
std::atomic<uint32_t> RecordedNamedClangModuleDependencies;
48+
};
49+
4050
/// A dependency scanning worker which performs filesystem lookup
4151
/// of a named module dependency.
4252
class ModuleDependencyScanningWorker {
@@ -48,6 +58,7 @@ class ModuleDependencyScanningWorker {
4858
DependencyTracker &DependencyTracker,
4959
std::shared_ptr<llvm::cas::ObjectStore> CAS,
5060
std::shared_ptr<llvm::cas::ActionCache> ActionCache,
61+
std::shared_ptr<ScannerMetrics> ScanMetrics,
5162
llvm::PrefixMapper *mapper, DiagnosticEngine &diags);
5263

5364
private:
@@ -128,6 +139,9 @@ class ModuleDependencyScanningWorker {
128139
std::shared_ptr<llvm::cas::ObjectStore> CAS;
129140
std::shared_ptr<llvm::cas::ActionCache> ActionCache;
130141

142+
// Metrics about actions performed by the scanner
143+
std::shared_ptr<ScannerMetrics> scanMetrics;
144+
131145
// Base command line invocation for clang scanner queries (both module and header)
132146
std::vector<std::string> clangScanningBaseCommandLineArgs;
133147
// Command line invocation for clang by-name module lookups
@@ -173,10 +187,11 @@ class SwiftDependencyTracker {
173187
std::map<std::string, FileEntry> TrackedFiles;
174188
};
175189

176-
class ModuleDependencyIssueReporter {
190+
class DependencyScannerDiagnosticReporter {
177191
private:
178-
ModuleDependencyIssueReporter(DiagnosticEngine &Diagnostics)
179-
: Diagnostics(Diagnostics) {}
192+
DependencyScannerDiagnosticReporter(DiagnosticEngine &Diagnostics,
193+
bool EmitScanRemarks)
194+
: Diagnostics(Diagnostics), EmitScanRemarks(EmitScanRemarks) {}
180195

181196
/// Diagnose scanner failure and attempt to reconstruct the dependency
182197
/// path from the main module to the missing dependency
@@ -207,7 +222,12 @@ class ModuleDependencyIssueReporter {
207222
const std::vector<SwiftModuleScannerQueryResult::IncompatibleCandidate>
208223
&candidates);
209224

225+
/// Emit various metrics about the current scannig action
226+
void emitScanMetrics(std::shared_ptr<ScannerMetrics> scanMetrics,
227+
const ModuleDependenciesCache &cache) const;
228+
210229
DiagnosticEngine &Diagnostics;
230+
bool EmitScanRemarks;
211231
std::unordered_set<std::string> ReportedMissing;
212232
// Restrict access to the parent scanner class.
213233
friend class ModuleDependencyScanner;
@@ -223,7 +243,8 @@ class ModuleDependencyScanner {
223243
DependencyTracker &DependencyTracker,
224244
std::shared_ptr<llvm::cas::ObjectStore> CAS,
225245
std::shared_ptr<llvm::cas::ActionCache> ActionCache,
226-
DiagnosticEngine &Diagnostics, bool ParallelScan);
246+
DiagnosticEngine &Diagnostics, bool ParallelScan,
247+
bool EmitScanRemarks);
227248

228249
/// Identify the scanner invocation's main module's dependencies
229250
llvm::ErrorOr<ModuleDependencyInfo>
@@ -347,7 +368,7 @@ class ModuleDependencyScanner {
347368
/// For the provided collection of unresolved imports
348369
/// belonging to identified Swift dependnecies, execute a parallel
349370
/// query to the Clang dependency scanner for each import's module identifier.
350-
void performParallelClangModuleLookup(
371+
void performClangModuleLookup(
351372
const ImportStatementInfoMap &unresolvedImportsMap,
352373
const ImportStatementInfoMap &unresolvedOptionalImportsMap,
353374
BatchClangModuleLookupResult &result);
@@ -396,7 +417,7 @@ class ModuleDependencyScanner {
396417
private:
397418
const CompilerInvocation &ScanCompilerInvocation;
398419
ASTContext &ScanASTContext;
399-
ModuleDependencyIssueReporter IssueReporter;
420+
DependencyScannerDiagnosticReporter ScanDiagnosticReporter;
400421

401422
/// The location of where the explicitly-built modules will be output to
402423
std::string ModuleOutputPath;
@@ -406,6 +427,9 @@ class ModuleDependencyScanner {
406427
/// Reference to a module dependency cache
407428
ModuleDependenciesCache &DependencyCache;
408429

430+
/// When requested, collect scanning-related statistics
431+
std::shared_ptr<ScannerMetrics> ScanMetrics;
432+
409433
/// The available pool of workers for filesystem module search
410434
unsigned NumThreads;
411435
std::list<std::unique_ptr<ModuleDependencyScanningWorker>> Workers;

include/swift/Frontend/FrontendOptions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,8 @@ class FrontendOptions {
399399
/// The path at which to either serialize or deserialize the dependency scanner cache.
400400
std::string SerializedDependencyScannerCachePath;
401401

402-
/// Emit remarks indicating use of the serialized module dependency scanning cache.
403-
bool EmitDependencyScannerCacheRemarks = false;
402+
/// Emit dependency scanning related remarks.
403+
bool EmitDependencyScannerRemarks = false;
404404

405405
/// The path at which the dependency scanner can write generated files.
406406
std::string ScannerOutputDir;

include/swift/Option/FrontendOptions.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ def validate_prior_dependency_scan_cache : Flag<["-"], "validate-prior-dependenc
292292
def dependency_scan_cache_path : Separate<["-"], "dependency-scan-cache-path">,
293293
HelpText<"The path to output the dependency scanner's internal state.">;
294294

295-
def dependency_scan_cache_remarks : Flag<["-"], "Rdependency-scan-cache">,
296-
HelpText<"Emit remarks indicating use of the serialized module dependency scanning cache.">;
295+
def dependency_scan_remarks : Flag<["-"], "Rdependency-scan">,
296+
HelpText<"Emit remarks for various steps taken by the dependency scanner.">;
297297

298298
def parallel_scan : Flag<["-"], "parallel-scan">,
299299
HelpText<"Perform dependency scanning in-parallel.">;

lib/AST/ModuleDependencies.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,14 @@ bool ModuleDependenciesCache::hasSwiftDependency(StringRef moduleName) const {
772772
return findSwiftDependency(moduleName).has_value();
773773
}
774774

775+
int ModuleDependenciesCache::numberOfClangDependencies() const {
776+
return ModuleDependenciesMap.at(ModuleDependencyKind::Clang).size();
777+
}
778+
int ModuleDependenciesCache::numberOfSwiftDependencies() const {
779+
return ModuleDependenciesMap.at(ModuleDependencyKind::SwiftInterface).size() +
780+
ModuleDependenciesMap.at(ModuleDependencyKind::SwiftBinary).size();
781+
}
782+
775783
void ModuleDependenciesCache::recordDependency(
776784
StringRef moduleName, ModuleDependencyInfo dependency) {
777785
auto dependenciesKind = dependency.getKind();
@@ -908,7 +916,6 @@ void ModuleDependenciesCache::setSwiftOverlayDependencies(
908916
ModuleDependencyID moduleID,
909917
const ArrayRef<ModuleDependencyID> dependencyIDs) {
910918
auto dependencyInfo = findKnownDependency(moduleID);
911-
assert(dependencyInfo.getSwiftOverlayDependencies().empty());
912919
#ifndef NDEBUG
913920
for (const auto &depID : dependencyIDs)
914921
assert(depID.Kind != ModuleDependencyKind::Clang);
@@ -923,7 +930,6 @@ void ModuleDependenciesCache::setCrossImportOverlayDependencies(
923930
ModuleDependencyID moduleID,
924931
const ModuleDependencyIDCollectionView dependencyIDs) {
925932
auto dependencyInfo = findKnownDependency(moduleID);
926-
assert(dependencyInfo.getCrossImportOverlayDependencies().empty());
927933
// Copy the existing info to a mutable one we can then replace it with,
928934
// after setting its overlay dependencies.
929935
auto updatedDependencyInfo = dependencyInfo;

0 commit comments

Comments
 (0)