Skip to content

Add -fine-grained-timers flag #76662

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 7 commits into from
Sep 25, 2024
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
18 changes: 13 additions & 5 deletions include/swift/Basic/Statistic.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ class UnifiedStatsReporter {
std::unique_ptr<StatsProfilers> EventProfilers;
std::unique_ptr<StatsProfilers> EntityProfilers;

/// Whether fine-grained timers are enabled. In practice, this means request
/// evaluator requests. This will have a runtime performance impact.
bool FineGrainedTimers;

/// Whether we are currently flushing statistics and should not therefore
/// record any additional stats until we've finished.
bool IsFlushingTracesAndProfiles;
Expand All @@ -179,6 +183,7 @@ class UnifiedStatsReporter {
StringRef Directory,
SourceManager *SM,
clang::SourceManager *CSM,
bool FineGrainedTimers,
bool TraceEvents,
bool ProfileEvents,
bool ProfileEntities);
Expand All @@ -190,13 +195,16 @@ class UnifiedStatsReporter {
StringRef OutputType,
StringRef OptType,
StringRef Directory,
SourceManager *SM=nullptr,
clang::SourceManager *CSM=nullptr,
bool TraceEvents=false,
bool ProfileEvents=false,
bool ProfileEntities=false);
SourceManager *SM,
clang::SourceManager *CSM,
bool FineGrainedTimers,
bool TraceEvents,
bool ProfileEvents,
bool ProfileEntities);
~UnifiedStatsReporter();

bool fineGrainedTimers() const { return FineGrainedTimers; }

AlwaysOnDriverCounters &getDriverCounters();
AlwaysOnFrontendCounters &getFrontendCounters();
void flushTracesAndProfiles();
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ class FrontendOptions {
/// The path to which we should output statistics files.
std::string StatsOutputDir;

/// Whether to enable timers tracking individual requests. This adds some
/// runtime overhead.
bool FineGrainedTimers = false;

/// Trace changes to stats to files in StatsOutputDir.
bool TraceStats = false;

Expand Down
3 changes: 3 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,9 @@ def driver_time_compilation : Flag<["-"], "driver-time-compilation">,
def stats_output_dir: Separate<["-"], "stats-output-dir">,
Flags<[FrontendOption, HelpHidden, ArgumentIsPath]>,
HelpText<"Directory to write unified compilation-statistics files to">;
def fine_grained_timers: Flag<["-"], "fine-grained-timers">,
Flags<[FrontendOption, HelpHidden]>,
HelpText<"Enable per-request timers">;
def trace_stats_events: Flag<["-"], "trace-stats-events">,
Flags<[FrontendOption, HelpHidden]>,
HelpText<"Trace changes to stats in -stats-output-dir">;
Expand Down
14 changes: 9 additions & 5 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,12 +928,16 @@ swift::detail::ImportPathBuilder_getIdentifierImpl(ASTContext &ctx,

/// Set a new stats reporter.
void ASTContext::setStatsReporter(UnifiedStatsReporter *stats) {
if (stats) {
stats->getFrontendCounters().NumASTBytesAllocated =
getAllocator().getBytesAllocated();
}
evaluator.setStatsReporter(stats);
if (!stats)
return;

Stats = stats;

stats->getFrontendCounters().NumASTBytesAllocated =
getAllocator().getBytesAllocated();

if (stats->fineGrainedTimers())
evaluator.setStatsReporter(stats);
}

/// getIdentifier - Return the uniqued and AST-Context-owned version of the
Expand Down
3 changes: 0 additions & 3 deletions lib/AST/UnqualifiedLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,6 @@ void UnqualifiedLookupFactory::performUnqualifiedLookup() {
auto localCounter = lookupCounter;
(void)localCounter; // for debugging
#endif
FrontendStatsTracer StatsTracer(Ctx.Stats,
"performUnqualifiedLookup",
DC->getParentSourceFile());

if (options.contains(UnqualifiedLookupFlags::ModuleLookup)) {
lookForAModuleWithTheGivenName(DC->getModuleScopeContext());
Expand Down
25 changes: 15 additions & 10 deletions lib/Basic/Statistic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
StringRef Directory,
SourceManager *SM,
clang::SourceManager *CSM,
bool FineGrainedTimers,
bool TraceEvents,
bool ProfileEvents,
bool ProfileEntities)
Expand All @@ -329,7 +330,7 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
OutputType,
OptType),
Directory,
SM, CSM,
SM, CSM, FineGrainedTimers,
TraceEvents, ProfileEvents, ProfileEntities)
{
}
Expand All @@ -339,6 +340,7 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
StringRef Directory,
SourceManager *SM,
clang::SourceManager *CSM,
bool FineGrainedTimers,
bool TraceEvents,
bool ProfileEvents,
bool ProfileEntities)
Expand All @@ -355,7 +357,8 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
SourceMgr(SM),
ClangSourceMgr(CSM),
RecursiveTimers(std::make_unique<RecursionSafeTimers>()),
IsFlushingTracesAndProfiles(false)
IsFlushingTracesAndProfiles(false),
FineGrainedTimers(FineGrainedTimers)
{
path::append(StatsFilename, makeStatsFileName(ProgramName, AuxName));
path::append(TraceFilename, makeTraceFileName(ProgramName, AuxName));
Expand Down Expand Up @@ -453,20 +456,22 @@ UnifiedStatsReporter::printAlwaysOnStatsAndTimers(raw_ostream &OS) {
const char *delim = "";
if (FrontendCounters) {
auto &C = getFrontendCounters();
#define FRONTEND_STATISTIC(TY, NAME) \
do { \
OS << delim << "\t\"" #TY "." #NAME "\": " << C.NAME; \
delim = ",\n"; \
#define FRONTEND_STATISTIC(TY, NAME) \
do { \
if (C.NAME) \
OS << delim << "\t\"" #TY "." #NAME "\": " << C.NAME; \
delim = ",\n"; \
} while (0);
#include "swift/Basic/Statistics.def"
#undef FRONTEND_STATISTIC
}
if (DriverCounters) {
auto &C = getDriverCounters();
#define DRIVER_STATISTIC(NAME) \
do { \
OS << delim << "\t\"Driver." #NAME "\": " << C.NAME; \
delim = ",\n"; \
#define DRIVER_STATISTIC(NAME) \
do { \
if (C.NAME) \
OS << delim << "\t\"Driver." #NAME "\": " << C.NAME; \
delim = ",\n"; \
} while (0);
#include "swift/Basic/Statistics.def"
#undef DRIVER_STATISTIC
Expand Down
8 changes: 7 additions & 1 deletion lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,13 @@ createStatsReporter(const llvm::opt::InputArgList *ArgList,
DefaultTargetTriple,
OutputType,
OptType,
A->getValue());
A->getValue(),
nullptr,
nullptr,
false,
false,
false,
false);
}

static bool
Expand Down
3 changes: 3 additions & 0 deletions lib/Frontend/ArgsToFrontendOptionsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,9 @@ void ArgsToFrontendOptionsConverter::computeDebugTimeOptions() {
using namespace options;
if (const Arg *A = Args.getLastArg(OPT_stats_output_dir)) {
Opts.StatsOutputDir = A->getValue();
if (Args.getLastArg(OPT_fine_grained_timers)) {
Opts.FineGrainedTimers = true;
}
if (Args.getLastArg(OPT_trace_stats_events)) {
Opts.TraceStats = true;
}
Expand Down
1 change: 1 addition & 0 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ void CompilerInstance::setupStatsReporter() {
StatsOutputDir,
&getSourceMgr(),
getClangSourceManager(getASTContext()),
Invoke.getFrontendOptions().FineGrainedTimers,
Invoke.getFrontendOptions().TraceStats,
Invoke.getFrontendOptions().ProfileEvents,
Invoke.getFrontendOptions().ProfileEntities);
Expand Down
Loading