Skip to content

Commit fd5597f

Browse files
committed
[Stats] Add a recursive-timers registry to UnifiedStatsReporter.
1 parent a8bb856 commit fd5597f

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

include/swift/Basic/Statistic.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ class UnifiedStatsReporter {
113113
const TraceFormatter *Formatter;
114114
};
115115

116+
// We only write fine-grained trace entries when the user passed
117+
// -trace-stats-events, but we recycle the same FrontendStatsTracers to give
118+
// us some free recursion-save phase timings whenever -trace-stats-dir is
119+
// active at all. Reduces redundant machinery.
120+
class RecursionSafeTimers;
121+
116122
private:
117123
bool currentProcessExitStatusSet;
118124
int currentProcessExitStatus;
@@ -126,6 +132,7 @@ class UnifiedStatsReporter {
126132
std::unique_ptr<AlwaysOnFrontendCounters> FrontendCounters;
127133
std::unique_ptr<AlwaysOnFrontendCounters> LastTracedFrontendCounters;
128134
std::vector<FrontendStatsEvent> FrontendStatsEvents;
135+
std::unique_ptr<RecursionSafeTimers> RecursiveTimers;
129136
std::unique_ptr<AlwaysOnFrontendRecursiveSharedTimers>
130137
FrontendRecursiveSharedTimers;
131138

lib/Basic/Statistic.cpp

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "clang/Basic/SourceLocation.h"
1515
#include "clang/Basic/SourceManager.h"
1616
#include "swift/Basic/Statistic.h"
17+
#include "swift/Basic/Timer.h"
1718
#include "swift/AST/Decl.h"
1819
#include "swift/AST/Expr.h"
1920
#include "swift/SIL/SILFunction.h"
@@ -126,6 +127,37 @@ auxName(StringRef ModuleName,
126127
+ "-" + cleanName(OptType));
127128
}
128129

130+
class UnifiedStatsReporter::RecursionSafeTimers {
131+
132+
struct RecursionSafeTimer {
133+
llvm::Optional<SharedTimer> Timer;
134+
size_t RecursionDepth;
135+
};
136+
137+
StringMap<RecursionSafeTimer> Timers;
138+
139+
public:
140+
141+
void BeginTimer(StringRef Name) {
142+
RecursionSafeTimer &T = Timers[Name];
143+
if (T.RecursionDepth == 0) {
144+
T.Timer.emplace(Name);
145+
}
146+
T.RecursionDepth++;
147+
}
148+
149+
void EndTimer(StringRef Name) {
150+
auto I = Timers.find(Name);
151+
assert(I != Timers.end());
152+
RecursionSafeTimer &T = I->getValue();
153+
assert(T.RecursionDepth != 0);
154+
T.RecursionDepth--;
155+
if (T.RecursionDepth == 0) {
156+
T.Timer.reset();
157+
}
158+
}
159+
};
160+
129161
UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
130162
StringRef ModuleName,
131163
StringRef InputName,
@@ -162,7 +194,8 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
162194
"Building Target",
163195
ProgramName, "Running Program")),
164196
SourceMgr(SM),
165-
ClangSourceMgr(CSM)
197+
ClangSourceMgr(CSM),
198+
RecursiveTimers(llvm::make_unique<RecursionSafeTimers>())
166199
{
167200
path::append(StatsFilename, makeStatsFileName(ProgramName, AuxName));
168201
path::append(TraceFilename, makeTraceFileName(ProgramName, AuxName));
@@ -329,6 +362,16 @@ UnifiedStatsReporter::saveAnyFrontendStatsEvents(
329362
FrontendStatsTracer const& T,
330363
bool IsEntry)
331364
{
365+
// First make a note in the recursion-safe timers; these
366+
// are active anytime UnifiedStatsReporter is active.
367+
if (IsEntry) {
368+
RecursiveTimers->BeginTimer(T.EventName);
369+
} else {
370+
RecursiveTimers->EndTimer(T.EventName);
371+
}
372+
373+
// If we don't have a saved entry to form deltas against in
374+
// the trace buffer, we're not tracing: return early.
332375
if (!LastTracedFrontendCounters)
333376
return;
334377
auto Now = llvm::TimeRecord::getCurrentTime();

0 commit comments

Comments
 (0)