Skip to content

Commit ff104e4

Browse files
authored
Merge pull request #14059 from graydon/count-stats-stacks
[Stats] Expand FrontendStatsTracer to trace multiple entity-types.
2 parents 4b727e5 + 5f130a8 commit ff104e4

File tree

7 files changed

+185
-50
lines changed

7 files changed

+185
-50
lines changed

include/swift/Basic/Statistic.h

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
#ifndef SWIFT_BASIC_STATISTIC_H
1414
#define SWIFT_BASIC_STATISTIC_H
1515

16+
#include "llvm/ADT/PointerUnion.h"
1617
#include "llvm/ADT/SmallString.h"
1718
#include "llvm/ADT/Statistic.h"
19+
#include "swift/AST/Identifier.h"
1820
#include "swift/Basic/SourceLoc.h"
1921
#include "swift/Basic/Timer.h"
2022

@@ -47,8 +49,17 @@
4749
// Generally we make one of these per-process: either early in the life of the
4850
// driver, or early in the life of the frontend.
4951

52+
namespace clang {
53+
class Decl;
54+
class SourceManager;
55+
}
56+
5057
namespace swift {
5158

59+
class Decl;
60+
class Expr;
61+
class SILFunction;
62+
5263
class UnifiedStatsReporter {
5364

5465
public:
@@ -75,14 +86,19 @@ class UnifiedStatsReporter {
7586
int dummyInstanceVariableToGetConstructorToParse;
7687
};
7788

89+
typedef llvm::PointerUnion4<const Decl*,
90+
const clang::Decl*,
91+
const Expr*,
92+
const SILFunction*> TraceEntity;
93+
7894
struct FrontendStatsTracer
7995
{
8096
UnifiedStatsReporter *Reporter;
8197
llvm::TimeRecord SavedTime;
82-
StringRef Name;
83-
SourceRange Range;
84-
FrontendStatsTracer(StringRef Name,
85-
SourceRange const &Range,
98+
StringRef EventName;
99+
TraceEntity Entity;
100+
FrontendStatsTracer(StringRef EventName,
101+
TraceEntity Entity,
86102
UnifiedStatsReporter *Reporter);
87103
FrontendStatsTracer();
88104
FrontendStatsTracer(FrontendStatsTracer&& other);
@@ -101,7 +117,7 @@ class UnifiedStatsReporter {
101117
StringRef CounterName;
102118
size_t CounterDelta;
103119
size_t CounterValue;
104-
SourceRange SourceRange;
120+
TraceEntity Entity;
105121
};
106122

107123
private:
@@ -112,6 +128,7 @@ class UnifiedStatsReporter {
112128
llvm::TimeRecord StartedTime;
113129
std::unique_ptr<llvm::NamedRegionTimer> Timer;
114130
SourceManager *SourceMgr;
131+
clang::SourceManager *ClangSourceMgr;
115132
std::unique_ptr<AlwaysOnDriverCounters> DriverCounters;
116133
std::unique_ptr<AlwaysOnFrontendCounters> FrontendCounters;
117134
std::unique_ptr<AlwaysOnFrontendCounters> LastTracedFrontendCounters;
@@ -126,6 +143,7 @@ class UnifiedStatsReporter {
126143
StringRef AuxName,
127144
StringRef Directory,
128145
SourceManager *SM,
146+
clang::SourceManager *CSM,
129147
bool TraceEvents);
130148
public:
131149
UnifiedStatsReporter(StringRef ProgramName,
@@ -136,17 +154,22 @@ class UnifiedStatsReporter {
136154
StringRef OptType,
137155
StringRef Directory,
138156
SourceManager *SM=nullptr,
157+
clang::SourceManager *CSM=nullptr,
139158
bool TraceEvents=false);
140159
~UnifiedStatsReporter();
141160

142161
AlwaysOnDriverCounters &getDriverCounters();
143162
AlwaysOnFrontendCounters &getFrontendCounters();
144163
AlwaysOnFrontendRecursiveSharedTimers &getFrontendRecursiveSharedTimers();
145164
void noteCurrentProcessExitStatus(int);
146-
FrontendStatsTracer getStatsTracer(StringRef N,
147-
SourceRange const &R);
148-
void saveAnyFrontendStatsEvents(FrontendStatsTracer const& T,
149-
bool IsEntry);
165+
// We provide 4 explicit overloads here, rather than a single function that
166+
// takes a TraceEntity, to save all of our clients from having to include all
167+
// 4 headers that define these 4 forward-declared types.
168+
FrontendStatsTracer getStatsTracer(StringRef EventName, const Decl *D);
169+
FrontendStatsTracer getStatsTracer(StringRef EventName, const clang::Decl*D);
170+
FrontendStatsTracer getStatsTracer(StringRef EventName, const Expr *E);
171+
FrontendStatsTracer getStatsTracer(StringRef EventName, const SILFunction *F);
172+
void saveAnyFrontendStatsEvents(FrontendStatsTracer const &T, bool IsEntry);
150173
};
151174

152175
}

lib/Basic/Statistic.cpp

Lines changed: 110 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "clang/AST/Decl.h"
14+
#include "clang/Basic/SourceLocation.h"
15+
#include "clang/Basic/SourceManager.h"
1316
#include "swift/Basic/Statistic.h"
17+
#include "swift/AST/Decl.h"
18+
#include "swift/AST/Expr.h"
19+
#include "swift/SIL/SILFunction.h"
1420
#include "swift/Driver/DependencyGraph.h"
1521
#include "llvm/Config/config.h"
1622
#include "llvm/Support/FileSystem.h"
@@ -128,6 +134,7 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
128134
StringRef OptType,
129135
StringRef Directory,
130136
SourceManager *SM,
137+
clang::SourceManager *CSM,
131138
bool TraceEvents)
132139
: UnifiedStatsReporter(ProgramName,
133140
auxName(ModuleName,
@@ -136,14 +143,15 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
136143
OutputType,
137144
OptType),
138145
Directory,
139-
SM, TraceEvents)
146+
SM, CSM, TraceEvents)
140147
{
141148
}
142149

143150
UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
144151
StringRef AuxName,
145152
StringRef Directory,
146153
SourceManager *SM,
154+
clang::SourceManager *CSM,
147155
bool TraceEvents)
148156
: currentProcessExitStatusSet(false),
149157
currentProcessExitStatus(EXIT_FAILURE),
@@ -153,7 +161,8 @@ UnifiedStatsReporter::UnifiedStatsReporter(StringRef ProgramName,
153161
Timer(make_unique<NamedRegionTimer>(AuxName,
154162
"Building Target",
155163
ProgramName, "Running Program")),
156-
SourceMgr(SM)
164+
SourceMgr(SM),
165+
ClangSourceMgr(CSM)
157166
{
158167
path::append(StatsFilename, makeStatsFileName(ProgramName, AuxName));
159168
path::append(TraceFilename, makeTraceFileName(ProgramName, AuxName));
@@ -250,13 +259,13 @@ UnifiedStatsReporter::printAlwaysOnStatsAndTimers(raw_ostream &OS) {
250259
}
251260

252261
UnifiedStatsReporter::FrontendStatsTracer::FrontendStatsTracer(
253-
StringRef Name,
254-
SourceRange const &Range,
262+
StringRef EventName,
263+
TraceEntity Entity,
255264
UnifiedStatsReporter *Reporter)
256265
: Reporter(Reporter),
257266
SavedTime(llvm::TimeRecord::getCurrentTime()),
258-
Name(Name),
259-
Range(Range)
267+
EventName(EventName),
268+
Entity(Entity)
260269
{
261270
if (Reporter)
262271
Reporter->saveAnyFrontendStatsEvents(*this, true);
@@ -273,8 +282,8 @@ UnifiedStatsReporter::FrontendStatsTracer::operator=(
273282
{
274283
Reporter = other.Reporter;
275284
SavedTime = other.SavedTime;
276-
Name = other.Name;
277-
Range = other.Range;
285+
EventName = other.EventName;
286+
Entity = other.Entity;
278287
other.Reporter = nullptr;
279288
return *this;
280289
}
@@ -283,8 +292,8 @@ UnifiedStatsReporter::FrontendStatsTracer::FrontendStatsTracer(
283292
FrontendStatsTracer&& other)
284293
: Reporter(other.Reporter),
285294
SavedTime(other.SavedTime),
286-
Name(other.Name),
287-
Range(other.Range)
295+
EventName(other.EventName),
296+
Entity(other.Entity)
288297
{
289298
other.Reporter = nullptr;
290299
}
@@ -296,17 +305,46 @@ UnifiedStatsReporter::FrontendStatsTracer::~FrontendStatsTracer()
296305
}
297306

298307
UnifiedStatsReporter::FrontendStatsTracer
299-
UnifiedStatsReporter::getStatsTracer(StringRef N,
300-
SourceRange const &R)
308+
UnifiedStatsReporter::getStatsTracer(StringRef EventName,
309+
const Decl *D)
301310
{
302311
if (LastTracedFrontendCounters)
303312
// Return live tracer object.
304-
return FrontendStatsTracer(N, R, this);
313+
return FrontendStatsTracer(EventName, D, this);
305314
else
306315
// Return inert tracer object.
307316
return FrontendStatsTracer();
308317
}
309318

319+
UnifiedStatsReporter::FrontendStatsTracer
320+
UnifiedStatsReporter::getStatsTracer(StringRef EventName,
321+
const Expr *E) {
322+
if (LastTracedFrontendCounters)
323+
return FrontendStatsTracer(EventName, E, this);
324+
else
325+
return FrontendStatsTracer();
326+
}
327+
328+
UnifiedStatsReporter::FrontendStatsTracer
329+
UnifiedStatsReporter::getStatsTracer(StringRef EventName,
330+
const clang::Decl *D)
331+
{
332+
if (LastTracedFrontendCounters)
333+
return FrontendStatsTracer(EventName, D, this);
334+
else
335+
return FrontendStatsTracer();
336+
}
337+
338+
UnifiedStatsReporter::FrontendStatsTracer
339+
UnifiedStatsReporter::getStatsTracer(StringRef EventName,
340+
const SILFunction *F)
341+
{
342+
if (LastTracedFrontendCounters)
343+
return FrontendStatsTracer(EventName, F, this);
344+
else
345+
return FrontendStatsTracer();
346+
}
347+
310348
void
311349
UnifiedStatsReporter::saveAnyFrontendStatsEvents(
312350
FrontendStatsTracer const& T,
@@ -321,13 +359,14 @@ UnifiedStatsReporter::saveAnyFrontendStatsEvents(
321359
auto &C = getFrontendCounters();
322360
#define FRONTEND_STATISTIC(TY, NAME) \
323361
do { \
362+
auto total = C.NAME; \
324363
auto delta = C.NAME - LastTracedFrontendCounters->NAME; \
325364
static char const *name = #TY "." #NAME; \
326365
if (delta != 0) { \
327366
LastTracedFrontendCounters->NAME = C.NAME; \
328367
FrontendStatsEvents.emplace_back(FrontendStatsEvent { \
329-
NowUS, LiveUS, IsEntry, T.Name, name, \
330-
delta, C.NAME, T.Range}); \
368+
NowUS, LiveUS, IsEntry, T.EventName, name, \
369+
delta, total, T.Entity}); \
331370
} \
332371
} while (0);
333372
#include "swift/Basic/Statistics.def"
@@ -343,6 +382,57 @@ UnifiedStatsReporter::AlwaysOnFrontendRecursiveSharedTimers::
343382
dummyInstanceVariableToGetConstructorToParse(0) {
344383
}
345384

385+
static inline void
386+
printTraceEntityName(raw_ostream &OS,
387+
UnifiedStatsReporter::TraceEntity E) {
388+
if (auto const *CD = E.dyn_cast<const clang::Decl*>()) {
389+
if (auto const *ND = dyn_cast<const clang::NamedDecl>(CD)) {
390+
ND->printName(OS);
391+
}
392+
} else if (auto const *D = E.dyn_cast<const Decl*>()) {
393+
if (auto const *VD = dyn_cast<const ValueDecl>(D)) {
394+
VD->getFullName().print(OS, false);
395+
}
396+
} else if (auto const *X = E.dyn_cast<const Expr*>()) {
397+
// Exprs don't have names
398+
} else if (auto const *F = E.dyn_cast<const SILFunction*>()) {
399+
OS << F->getName();
400+
}
401+
}
402+
403+
static inline void
404+
printClangShortLoc(raw_ostream &OS,
405+
clang::SourceManager *CSM,
406+
clang::SourceLocation L) {
407+
if (!L.isValid() || !L.isFileID())
408+
return;
409+
auto PLoc = CSM->getPresumedLoc(L);
410+
OS << llvm::sys::path::filename(PLoc.getFilename())
411+
<< ':' << PLoc.getLine()
412+
<< ':' << PLoc.getColumn();
413+
}
414+
415+
static inline void
416+
printTraceEntityLoc(raw_ostream &OS,
417+
SourceManager *SM,
418+
clang::SourceManager *CSM,
419+
UnifiedStatsReporter::TraceEntity E) {
420+
if (auto const *CD = E.dyn_cast<const clang::Decl*>()) {
421+
if (CSM) {
422+
auto Range = CD->getSourceRange();
423+
printClangShortLoc(OS, CSM, Range.getBegin());
424+
OS << '-';
425+
printClangShortLoc(OS, CSM, Range.getEnd());
426+
}
427+
} else if (auto const *D = E.dyn_cast<const Decl*>()) {
428+
D->getSourceRange().print(OS, *SM, false);
429+
} else if (auto const *X = E.dyn_cast<const Expr*>()) {
430+
X->getSourceRange().print(OS, *SM, false);
431+
} else if (auto const *F = E.dyn_cast<const SILFunction*>()) {
432+
F->getLocation().getSourceRange().print(OS, *SM, false);
433+
}
434+
}
435+
346436
UnifiedStatsReporter::~UnifiedStatsReporter()
347437
{
348438
// If nobody's marked this process as successful yet,
@@ -419,7 +509,7 @@ UnifiedStatsReporter::~UnifiedStatsReporter()
419509
return;
420510
}
421511
tstream << "Time,Live,IsEntry,EventName,CounterName,"
422-
<< "CounterDelta,CounterValue,SourceRange\n";
512+
<< "CounterDelta,CounterValue,EntityName,EntityRange\n";
423513
for (auto const &E : FrontendStatsEvents) {
424514
tstream << E.TimeUSec << ','
425515
<< E.LiveUSec << ','
@@ -429,7 +519,10 @@ UnifiedStatsReporter::~UnifiedStatsReporter()
429519
<< E.CounterDelta << ','
430520
<< E.CounterValue << ',';
431521
tstream << '"';
432-
E.SourceRange.print(tstream, *SourceMgr, false);
522+
printTraceEntityName(tstream, E.Entity);
523+
tstream << '"' << ',';
524+
tstream << '"';
525+
printTraceEntityLoc(tstream, SourceMgr, ClangSourceMgr, E.Entity);
433526
tstream << '"' << '\n';
434527
}
435528
}

lib/ClangImporter/ImportDecl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7870,6 +7870,11 @@ Decl *ClangImporter::Implementation::importDeclAndCacheImpl(
78707870
if (!ClangDecl)
78717871
return nullptr;
78727872

7873+
UnifiedStatsReporter::FrontendStatsTracer Tracer;
7874+
if (SwiftContext.Stats)
7875+
Tracer = SwiftContext.Stats->getStatsTracer("import-clang-decl",
7876+
ClangDecl);
7877+
78737878
clang::PrettyStackTraceDecl trace(ClangDecl, clang::SourceLocation(),
78747879
Instance->getSourceManager(), "importing");
78757880

0 commit comments

Comments
 (0)