Skip to content

Commit bfc4f95

Browse files
authored
Merge pull request #76662 from slavapestov/fine-grained-timers-flag
Add -fine-grained-timers flag
2 parents 5925df7 + 0814c50 commit bfc4f95

File tree

17 files changed

+262
-241
lines changed

17 files changed

+262
-241
lines changed

include/swift/Basic/Statistic.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ class UnifiedStatsReporter {
167167
std::unique_ptr<StatsProfilers> EventProfilers;
168168
std::unique_ptr<StatsProfilers> EntityProfilers;
169169

170+
/// Whether fine-grained timers are enabled. In practice, this means request
171+
/// evaluator requests. This will have a runtime performance impact.
172+
bool FineGrainedTimers;
173+
170174
/// Whether we are currently flushing statistics and should not therefore
171175
/// record any additional stats until we've finished.
172176
bool IsFlushingTracesAndProfiles;
@@ -179,6 +183,7 @@ class UnifiedStatsReporter {
179183
StringRef Directory,
180184
SourceManager *SM,
181185
clang::SourceManager *CSM,
186+
bool FineGrainedTimers,
182187
bool TraceEvents,
183188
bool ProfileEvents,
184189
bool ProfileEntities);
@@ -190,13 +195,16 @@ class UnifiedStatsReporter {
190195
StringRef OutputType,
191196
StringRef OptType,
192197
StringRef Directory,
193-
SourceManager *SM=nullptr,
194-
clang::SourceManager *CSM=nullptr,
195-
bool TraceEvents=false,
196-
bool ProfileEvents=false,
197-
bool ProfileEntities=false);
198+
SourceManager *SM,
199+
clang::SourceManager *CSM,
200+
bool FineGrainedTimers,
201+
bool TraceEvents,
202+
bool ProfileEvents,
203+
bool ProfileEntities);
198204
~UnifiedStatsReporter();
199205

206+
bool fineGrainedTimers() const { return FineGrainedTimers; }
207+
200208
AlwaysOnDriverCounters &getDriverCounters();
201209
AlwaysOnFrontendCounters &getFrontendCounters();
202210
void flushTracesAndProfiles();

include/swift/Frontend/FrontendOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ class FrontendOptions {
228228
/// The path to which we should output statistics files.
229229
std::string StatsOutputDir;
230230

231+
/// Whether to enable timers tracking individual requests. This adds some
232+
/// runtime overhead.
233+
bool FineGrainedTimers = false;
234+
231235
/// Trace changes to stats to files in StatsOutputDir.
232236
bool TraceStats = false;
233237

include/swift/Option/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ def driver_time_compilation : Flag<["-"], "driver-time-compilation">,
361361
def stats_output_dir: Separate<["-"], "stats-output-dir">,
362362
Flags<[FrontendOption, HelpHidden, ArgumentIsPath]>,
363363
HelpText<"Directory to write unified compilation-statistics files to">;
364+
def fine_grained_timers: Flag<["-"], "fine-grained-timers">,
365+
Flags<[FrontendOption, HelpHidden]>,
366+
HelpText<"Enable per-request timers">;
364367
def trace_stats_events: Flag<["-"], "trace-stats-events">,
365368
Flags<[FrontendOption, HelpHidden]>,
366369
HelpText<"Trace changes to stats in -stats-output-dir">;

lib/AST/ASTContext.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -928,12 +928,16 @@ swift::detail::ImportPathBuilder_getIdentifierImpl(ASTContext &ctx,
928928

929929
/// Set a new stats reporter.
930930
void ASTContext::setStatsReporter(UnifiedStatsReporter *stats) {
931-
if (stats) {
932-
stats->getFrontendCounters().NumASTBytesAllocated =
933-
getAllocator().getBytesAllocated();
934-
}
935-
evaluator.setStatsReporter(stats);
931+
if (!stats)
932+
return;
933+
936934
Stats = stats;
935+
936+
stats->getFrontendCounters().NumASTBytesAllocated =
937+
getAllocator().getBytesAllocated();
938+
939+
if (stats->fineGrainedTimers())
940+
evaluator.setStatsReporter(stats);
937941
}
938942

939943
/// getIdentifier - Return the uniqued and AST-Context-owned version of the

lib/AST/UnqualifiedLookup.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,6 @@ void UnqualifiedLookupFactory::performUnqualifiedLookup() {
240240
auto localCounter = lookupCounter;
241241
(void)localCounter; // for debugging
242242
#endif
243-
FrontendStatsTracer StatsTracer(Ctx.Stats,
244-
"performUnqualifiedLookup",
245-
DC->getParentSourceFile());
246243

247244
if (options.contains(UnqualifiedLookupFlags::ModuleLookup)) {
248245
lookForAModuleWithTheGivenName(DC->getModuleScopeContext());

lib/Basic/Statistic.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
319319
StringRef Directory,
320320
SourceManager *SM,
321321
clang::SourceManager *CSM,
322+
bool FineGrainedTimers,
322323
bool TraceEvents,
323324
bool ProfileEvents,
324325
bool ProfileEntities)
@@ -329,7 +330,7 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
329330
OutputType,
330331
OptType),
331332
Directory,
332-
SM, CSM,
333+
SM, CSM, FineGrainedTimers,
333334
TraceEvents, ProfileEvents, ProfileEntities)
334335
{
335336
}
@@ -339,6 +340,7 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
339340
StringRef Directory,
340341
SourceManager *SM,
341342
clang::SourceManager *CSM,
343+
bool FineGrainedTimers,
342344
bool TraceEvents,
343345
bool ProfileEvents,
344346
bool ProfileEntities)
@@ -355,7 +357,8 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
355357
SourceMgr(SM),
356358
ClangSourceMgr(CSM),
357359
RecursiveTimers(std::make_unique<RecursionSafeTimers>()),
358-
IsFlushingTracesAndProfiles(false)
360+
IsFlushingTracesAndProfiles(false),
361+
FineGrainedTimers(FineGrainedTimers)
359362
{
360363
path::append(StatsFilename, makeStatsFileName(ProgramName, AuxName));
361364
path::append(TraceFilename, makeTraceFileName(ProgramName, AuxName));
@@ -453,20 +456,22 @@ UnifiedStatsReporter::printAlwaysOnStatsAndTimers(raw_ostream &OS) {
453456
const char *delim = "";
454457
if (FrontendCounters) {
455458
auto &C = getFrontendCounters();
456-
#define FRONTEND_STATISTIC(TY, NAME) \
457-
do { \
458-
OS << delim << "\t\"" #TY "." #NAME "\": " << C.NAME; \
459-
delim = ",\n"; \
459+
#define FRONTEND_STATISTIC(TY, NAME) \
460+
do { \
461+
if (C.NAME) \
462+
OS << delim << "\t\"" #TY "." #NAME "\": " << C.NAME; \
463+
delim = ",\n"; \
460464
} while (0);
461465
#include "swift/Basic/Statistics.def"
462466
#undef FRONTEND_STATISTIC
463467
}
464468
if (DriverCounters) {
465469
auto &C = getDriverCounters();
466-
#define DRIVER_STATISTIC(NAME) \
467-
do { \
468-
OS << delim << "\t\"Driver." #NAME "\": " << C.NAME; \
469-
delim = ",\n"; \
470+
#define DRIVER_STATISTIC(NAME) \
471+
do { \
472+
if (C.NAME) \
473+
OS << delim << "\t\"Driver." #NAME "\": " << C.NAME; \
474+
delim = ",\n"; \
470475
} while (0);
471476
#include "swift/Basic/Statistics.def"
472477
#undef DRIVER_STATISTIC

lib/Driver/Driver.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,13 @@ createStatsReporter(const llvm::opt::InputArgList *ArgList,
516516
DefaultTargetTriple,
517517
OutputType,
518518
OptType,
519-
A->getValue());
519+
A->getValue(),
520+
nullptr,
521+
nullptr,
522+
false,
523+
false,
524+
false,
525+
false);
520526
}
521527

522528
static bool

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,9 @@ void ArgsToFrontendOptionsConverter::computeDebugTimeOptions() {
416416
using namespace options;
417417
if (const Arg *A = Args.getLastArg(OPT_stats_output_dir)) {
418418
Opts.StatsOutputDir = A->getValue();
419+
if (Args.getLastArg(OPT_fine_grained_timers)) {
420+
Opts.FineGrainedTimers = true;
421+
}
419422
if (Args.getLastArg(OPT_trace_stats_events)) {
420423
Opts.TraceStats = true;
421424
}

lib/Frontend/Frontend.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ void CompilerInstance::setupStatsReporter() {
381381
StatsOutputDir,
382382
&getSourceMgr(),
383383
getClangSourceManager(getASTContext()),
384+
Invoke.getFrontendOptions().FineGrainedTimers,
384385
Invoke.getFrontendOptions().TraceStats,
385386
Invoke.getFrontendOptions().ProfileEvents,
386387
Invoke.getFrontendOptions().ProfileEntities);

0 commit comments

Comments
 (0)