Skip to content

Commit 34e9ada

Browse files
PeterChou1yuxuanchen1997
authored andcommitted
[clang-doc] add ftime profiling (#97644)
Summary: this patches adds ftime-tracing option for clang-doc which was what I used to profile the performance problems we had with clang-doc generating the llvm docs. - ftime-trace produces a time profiler json file called clang-doc-tracing.json Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251381
1 parent 8daca29 commit 34e9ada

File tree

5 files changed

+69
-8
lines changed

5 files changed

+69
-8
lines changed

clang-tools-extra/clang-doc/BitcodeReader.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "BitcodeReader.h"
1010
#include "llvm/ADT/IndexedMap.h"
1111
#include "llvm/Support/Error.h"
12+
#include "llvm/Support/TimeProfiler.h"
1213
#include "llvm/Support/raw_ostream.h"
1314
#include <optional>
1415

@@ -670,6 +671,7 @@ llvm::Error ClangDocBitcodeReader::readRecord(unsigned ID, T I) {
670671

671672
template <>
672673
llvm::Error ClangDocBitcodeReader::readRecord(unsigned ID, Reference *I) {
674+
llvm::TimeTraceScope("Reducing infos", "readRecord");
673675
Record R;
674676
llvm::StringRef Blob;
675677
llvm::Expected<unsigned> MaybeRecID = Stream.readRecord(ID, R, &Blob);
@@ -681,6 +683,7 @@ llvm::Error ClangDocBitcodeReader::readRecord(unsigned ID, Reference *I) {
681683
// Read a block of records into a single info.
682684
template <typename T>
683685
llvm::Error ClangDocBitcodeReader::readBlock(unsigned ID, T I) {
686+
llvm::TimeTraceScope("Reducing infos", "readBlock");
684687
if (llvm::Error Err = Stream.EnterSubBlock(ID))
685688
return Err;
686689

@@ -711,6 +714,7 @@ llvm::Error ClangDocBitcodeReader::readBlock(unsigned ID, T I) {
711714

712715
template <typename T>
713716
llvm::Error ClangDocBitcodeReader::readSubBlock(unsigned ID, T I) {
717+
llvm::TimeTraceScope("Reducing infos", "readSubBlock");
714718
switch (ID) {
715719
// Blocks can only have certain types of sub blocks.
716720
case BI_COMMENT_BLOCK_ID: {
@@ -817,6 +821,7 @@ llvm::Error ClangDocBitcodeReader::readSubBlock(unsigned ID, T I) {
817821

818822
ClangDocBitcodeReader::Cursor
819823
ClangDocBitcodeReader::skipUntilRecordOrBlock(unsigned &BlockOrRecordID) {
824+
llvm::TimeTraceScope("Reducing infos", "skipUntilRecordOrBlock");
820825
BlockOrRecordID = 0;
821826

822827
while (!Stream.AtEndOfStream()) {
@@ -878,6 +883,7 @@ llvm::Error ClangDocBitcodeReader::validateStream() {
878883
}
879884

880885
llvm::Error ClangDocBitcodeReader::readBlockInfoBlock() {
886+
llvm::TimeTraceScope("Reducing infos", "readBlockInfoBlock");
881887
Expected<std::optional<llvm::BitstreamBlockInfo>> MaybeBlockInfo =
882888
Stream.ReadBlockInfoBlock();
883889
if (!MaybeBlockInfo)
@@ -894,6 +900,7 @@ llvm::Error ClangDocBitcodeReader::readBlockInfoBlock() {
894900
template <typename T>
895901
llvm::Expected<std::unique_ptr<Info>>
896902
ClangDocBitcodeReader::createInfo(unsigned ID) {
903+
llvm::TimeTraceScope("Reducing infos", "createInfo");
897904
std::unique_ptr<Info> I = std::make_unique<T>();
898905
if (auto Err = readBlock(ID, static_cast<T *>(I.get())))
899906
return std::move(Err);
@@ -902,6 +909,7 @@ ClangDocBitcodeReader::createInfo(unsigned ID) {
902909

903910
llvm::Expected<std::unique_ptr<Info>>
904911
ClangDocBitcodeReader::readBlockToInfo(unsigned ID) {
912+
llvm::TimeTraceScope("Reducing infos", "readBlockToInfo");
905913
switch (ID) {
906914
case BI_NAMESPACE_BLOCK_ID:
907915
return createInfo<NamespaceInfo>(ID);

clang-tools-extra/clang-doc/Mapper.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@
1313
#include "clang/Index/USRGeneration.h"
1414
#include "llvm/ADT/StringExtras.h"
1515
#include "llvm/Support/Error.h"
16+
#include "llvm/Support/TimeProfiler.h"
1617

1718
namespace clang {
1819
namespace doc {
1920

2021
void MapASTVisitor::HandleTranslationUnit(ASTContext &Context) {
22+
if (CDCtx.FTimeTrace)
23+
llvm::timeTraceProfilerInitialize(200, "clang-doc");
2124
TraverseDecl(Context.getTranslationUnitDecl());
25+
if (CDCtx.FTimeTrace)
26+
llvm::timeTraceProfilerFinishThread();
2227
}
2328

2429
template <typename T> bool MapASTVisitor::mapDecl(const T *D) {
@@ -30,6 +35,7 @@ template <typename T> bool MapASTVisitor::mapDecl(const T *D) {
3035
if (D->getParentFunctionOrMethod())
3136
return true;
3237

38+
llvm::timeTraceProfilerBegin("Mapping declaration", "emit info from astnode");
3339
llvm::SmallString<128> USR;
3440
// If there is an error generating a USR for the decl, skip this decl.
3541
if (index::generateUSRForDecl(D, USR))
@@ -40,7 +46,10 @@ template <typename T> bool MapASTVisitor::mapDecl(const T *D) {
4046
auto I = serialize::emitInfo(D, getComment(D, D->getASTContext()),
4147
getLine(D, D->getASTContext()), File,
4248
IsFileInRootDir, CDCtx.PublicOnly);
49+
llvm::timeTraceProfilerEnd();
4350

51+
llvm::timeTraceProfilerBegin("Mapping declaration",
52+
"serialized info into bitcode");
4453
// A null in place of I indicates that the serializer is skipping this decl
4554
// for some reason (e.g. we're only reporting public decls).
4655
if (I.first)
@@ -49,6 +58,7 @@ template <typename T> bool MapASTVisitor::mapDecl(const T *D) {
4958
if (I.second)
5059
CDCtx.ECtx->reportResult(llvm::toHex(llvm::toStringRef(I.second->USR)),
5160
serialize::serialize(I.second));
61+
llvm::timeTraceProfilerEnd();
5262
return true;
5363
}
5464

clang-tools-extra/clang-doc/Representation.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,11 @@ ClangDocContext::ClangDocContext(tooling::ExecutionContext *ECtx,
368368
StringRef ProjectName, bool PublicOnly,
369369
StringRef OutDirectory, StringRef SourceRoot,
370370
StringRef RepositoryUrl,
371-
std::vector<std::string> UserStylesheets)
371+
std::vector<std::string> UserStylesheets,
372+
bool FTimeTrace)
372373
: ECtx(ECtx), ProjectName(ProjectName), PublicOnly(PublicOnly),
373-
OutDirectory(OutDirectory), UserStylesheets(UserStylesheets) {
374+
OutDirectory(OutDirectory), UserStylesheets(UserStylesheets),
375+
FTimeTrace(FTimeTrace) {
374376
llvm::SmallString<128> SourceRootDir(SourceRoot);
375377
if (SourceRoot.empty())
376378
// If no SourceRoot was provided the current path is used as the default

clang-tools-extra/clang-doc/Representation.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,10 +482,13 @@ struct ClangDocContext {
482482
ClangDocContext(tooling::ExecutionContext *ECtx, StringRef ProjectName,
483483
bool PublicOnly, StringRef OutDirectory, StringRef SourceRoot,
484484
StringRef RepositoryUrl,
485-
std::vector<std::string> UserStylesheets);
485+
std::vector<std::string> UserStylesheets,
486+
bool FTimeTrace = false);
486487
tooling::ExecutionContext *ECtx;
487488
std::string ProjectName; // Name of project clang-doc is documenting.
488489
bool PublicOnly; // Indicates if only public declarations are documented.
490+
bool FTimeTrace; // Indicates if ftime trace is turned on
491+
int Granularity; // Granularity of ftime trace
489492
std::string OutDirectory; // Directory for outputting generated files.
490493
std::string SourceRoot; // Directory where processed files are stored. Links
491494
// to definition locations will only be generated if

clang-tools-extra/clang-doc/tool/ClangDocMain.cpp

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "llvm/Support/Process.h"
4242
#include "llvm/Support/Signals.h"
4343
#include "llvm/Support/ThreadPool.h"
44+
#include "llvm/Support/TimeProfiler.h"
4445
#include "llvm/Support/raw_ostream.h"
4546
#include <atomic>
4647
#include <mutex>
@@ -99,6 +100,11 @@ URL of repository that hosts code.
99100
Used for links to definition locations.)"),
100101
llvm::cl::cat(ClangDocCategory));
101102

103+
static llvm::cl::opt<bool> FTimeTrace("ftime-trace", llvm::cl::desc(R"(
104+
Turn on time profiler. Generates clang-doc-tracing.json)"),
105+
llvm::cl::init(false),
106+
llvm::cl::cat(ClangDocCategory));
107+
102108
enum OutputFormatTy {
103109
md,
104110
yaml,
@@ -229,6 +235,12 @@ Example usage for a project using a compile commands database:
229235
return 1;
230236
}
231237

238+
// turns on ftime trace profiling
239+
if (FTimeTrace)
240+
llvm::timeTraceProfilerInitialize(200, "clang-doc");
241+
242+
llvm::TimeTraceScope("main");
243+
232244
// Fail early if an invalid format was provided.
233245
std::string Format = getFormatString();
234246
llvm::outs() << "Emiting docs in " << Format << " format.\n";
@@ -252,8 +264,8 @@ Example usage for a project using a compile commands database:
252264
OutDirectory,
253265
SourceRoot,
254266
RepositoryUrl,
255-
{UserStylesheets.begin(), UserStylesheets.end()}
256-
};
267+
{UserStylesheets.begin(), UserStylesheets.end()},
268+
FTimeTrace};
257269

258270
if (Format == "html") {
259271
if (auto Err = getHtmlAssetFiles(argv[0], CDCtx)) {
@@ -262,6 +274,7 @@ Example usage for a project using a compile commands database:
262274
}
263275
}
264276

277+
llvm::timeTraceProfilerBegin("Mapping declaration", "total runtime");
265278
// Mapping phase
266279
llvm::outs() << "Mapping decls...\n";
267280
auto Err =
@@ -276,24 +289,28 @@ Example usage for a project using a compile commands database:
276289
return 1;
277290
}
278291
}
292+
llvm::timeTraceProfilerEnd();
279293

280294
// Collect values into output by key.
281295
// In ToolResults, the Key is the hashed USR and the value is the
282296
// bitcode-encoded representation of the Info object.
297+
llvm::timeTraceProfilerBegin("Collect Info", "total runtime");
283298
llvm::outs() << "Collecting infos...\n";
284299
llvm::StringMap<std::vector<StringRef>> USRToBitcode;
285300
Executor->get()->getToolResults()->forEachResult(
286301
[&](StringRef Key, StringRef Value) {
287302
auto R = USRToBitcode.try_emplace(Key, std::vector<StringRef>());
288303
R.first->second.emplace_back(Value);
289304
});
305+
llvm::timeTraceProfilerEnd();
290306

291307
// Collects all Infos according to their unique USR value. This map is added
292308
// to from the thread pool below and is protected by the USRToInfoMutex.
293309
llvm::sys::Mutex USRToInfoMutex;
294310
llvm::StringMap<std::unique_ptr<doc::Info>> USRToInfo;
295311

296312
// First reducing phase (reduce all decls into one info per decl).
313+
llvm::timeTraceProfilerBegin("Reducing infos", "total runtime");
297314
llvm::outs() << "Reducing " << USRToBitcode.size() << " infos...\n";
298315
std::atomic<bool> Error;
299316
Error = false;
@@ -302,8 +319,11 @@ Example usage for a project using a compile commands database:
302319
llvm::DefaultThreadPool Pool(llvm::hardware_concurrency(ExecutorConcurrency));
303320
for (auto &Group : USRToBitcode) {
304321
Pool.async([&]() {
305-
std::vector<std::unique_ptr<doc::Info>> Infos;
322+
if (FTimeTrace)
323+
llvm::timeTraceProfilerInitialize(200, "clang-doc");
306324

325+
llvm::timeTraceProfilerBegin("Reducing infos", "decoding bitcode");
326+
std::vector<std::unique_ptr<doc::Info>> Infos;
307327
for (auto &Bitcode : Group.getValue()) {
308328
llvm::BitstreamCursor Stream(Bitcode);
309329
doc::ClangDocBitcodeReader Reader(Stream);
@@ -316,32 +336,40 @@ Example usage for a project using a compile commands database:
316336
std::move(ReadInfos->begin(), ReadInfos->end(),
317337
std::back_inserter(Infos));
318338
}
339+
llvm::timeTraceProfilerEnd();
319340

341+
llvm::timeTraceProfilerBegin("Reducing infos", "merging bitcode");
320342
auto Reduced = doc::mergeInfos(Infos);
321343
if (!Reduced) {
322344
llvm::errs() << llvm::toString(Reduced.takeError());
323345
return;
324346
}
347+
llvm::timeTraceProfilerEnd();
325348

326349
// Add a reference to this Info in the Index
327350
{
328351
std::lock_guard<llvm::sys::Mutex> Guard(IndexMutex);
329352
clang::doc::Generator::addInfoToIndex(CDCtx.Idx, Reduced.get().get());
330353
}
331-
332354
// Save in the result map (needs a lock due to threaded access).
355+
333356
{
334357
std::lock_guard<llvm::sys::Mutex> Guard(USRToInfoMutex);
335358
USRToInfo[Group.getKey()] = std::move(Reduced.get());
336359
}
360+
361+
if (CDCtx.FTimeTrace)
362+
llvm::timeTraceProfilerFinishThread();
337363
});
338364
}
365+
llvm::timeTraceProfilerEnd();
339366

340367
Pool.wait();
341368

342369
if (Error)
343370
return 1;
344371

372+
llvm::timeTraceProfilerBegin("Writing output", "total runtime");
345373
// Ensure the root output directory exists.
346374
if (std::error_code Err = llvm::sys::fs::create_directories(OutDirectory);
347375
Err != std::error_code()) {
@@ -362,6 +390,16 @@ Example usage for a project using a compile commands database:
362390
if (Err) {
363391
llvm::outs() << "warning: " << toString(std::move(Err)) << "\n";
364392
}
365-
393+
llvm::timeTraceProfilerEnd();
394+
395+
if (FTimeTrace) {
396+
std::error_code EC;
397+
llvm::raw_fd_ostream OS("clang-doc-tracing.json", EC,
398+
llvm::sys::fs::OF_Text);
399+
if (!EC)
400+
llvm::timeTraceProfilerWrite(OS);
401+
else
402+
return 1;
403+
}
366404
return 0;
367405
}

0 commit comments

Comments
 (0)