|
14 | 14 | #include "clang/Basic/SourceLocation.h"
|
15 | 15 | #include "clang/Basic/SourceManager.h"
|
16 | 16 | #include "swift/Basic/Statistic.h"
|
| 17 | +#include "swift/Basic/Timer.h" |
17 | 18 | #include "swift/AST/Decl.h"
|
18 | 19 | #include "swift/AST/Expr.h"
|
19 | 20 | #include "swift/SIL/SILFunction.h"
|
@@ -126,6 +127,37 @@ auxName(StringRef ModuleName,
|
126 | 127 | + "-" + cleanName(OptType));
|
127 | 128 | }
|
128 | 129 |
|
| 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 | + |
129 | 161 | UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
|
130 | 162 | StringRef ModuleName,
|
131 | 163 | StringRef InputName,
|
@@ -162,7 +194,8 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
|
162 | 194 | "Building Target",
|
163 | 195 | ProgramName, "Running Program")),
|
164 | 196 | SourceMgr(SM),
|
165 |
| - ClangSourceMgr(CSM) |
| 197 | + ClangSourceMgr(CSM), |
| 198 | + RecursiveTimers(llvm::make_unique<RecursionSafeTimers>()) |
166 | 199 | {
|
167 | 200 | path::append(StatsFilename, makeStatsFileName(ProgramName, AuxName));
|
168 | 201 | path::append(TraceFilename, makeTraceFileName(ProgramName, AuxName));
|
@@ -329,6 +362,16 @@ UnifiedStatsReporter::saveAnyFrontendStatsEvents(
|
329 | 362 | FrontendStatsTracer const& T,
|
330 | 363 | bool IsEntry)
|
331 | 364 | {
|
| 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. |
332 | 375 | if (!LastTracedFrontendCounters)
|
333 | 376 | return;
|
334 | 377 | auto Now = llvm::TimeRecord::getCurrentTime();
|
|
0 commit comments