Skip to content

Commit c4bacc3

Browse files
committed
[Modules] Add stats to measure performance of building and loading modules.
Measure amount of high-level or fixed-cost operations performed during building/loading modules and during header search. High-level operations like building a module or processing a .pcm file are motivated by previous issues where clang was re-building modules or re-reading .pcm files unnecessarily. Fixed-cost operations like `stat` calls are tracked because clang cannot change how long each operation takes but it can perform fewer of such operations to improve the compile time. Also tracking such stats over time can help us detect compile-time regressions. Added stats are more stable than the actual measured compilation time, so expect the detected regressions to be less noisy. rdar://problem/55715134 Reviewed By: aprantl, bruno Differential Revision: https://reviews.llvm.org/D86895
1 parent e75afc9 commit c4bacc3

File tree

6 files changed

+41
-2
lines changed

6 files changed

+41
-2
lines changed

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555

5656
using namespace clang;
5757

58+
#define DEBUG_TYPE "modules"
59+
60+
ALWAYS_ENABLED_STATISTIC(NumCompiledModules, "Number of compiled modules.");
61+
5862
CompilerInstance::CompilerInstance(
5963
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
6064
InMemoryModuleCache *SharedModuleCache)
@@ -1063,6 +1067,7 @@ compileModuleImpl(CompilerInstance &ImportingInstance, SourceLocation ImportLoc,
10631067
llvm::function_ref<void(CompilerInstance &)> PostBuildStep =
10641068
[](CompilerInstance &) {}) {
10651069
llvm::TimeTraceScope TimeScope("Module Compile", ModuleName);
1070+
++NumCompiledModules;
10661071

10671072
// Construct a compiler invocation for creating this module.
10681073
auto Invocation =

clang/lib/Serialization/ASTReader.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
#include "llvm/ADT/SmallPtrSet.h"
101101
#include "llvm/ADT/SmallString.h"
102102
#include "llvm/ADT/SmallVector.h"
103+
#include "llvm/ADT/Statistic.h"
103104
#include "llvm/ADT/StringExtras.h"
104105
#include "llvm/ADT/StringMap.h"
105106
#include "llvm/ADT/StringRef.h"
@@ -142,6 +143,15 @@ using namespace clang::serialization::reader;
142143
using llvm::BitstreamCursor;
143144
using llvm::RoundingMode;
144145

146+
#define DEBUG_TYPE "modules"
147+
148+
ALWAYS_ENABLED_STATISTIC(NumTryLoadModule, "Number of times tried to load a "
149+
"module. Includes failed attempts "
150+
"and using cached results.");
151+
ALWAYS_ENABLED_STATISTIC(NumReadASTCore,
152+
"Number of ReadASTCore() invocations. Includes only "
153+
"actual AST core parsing and ignores cached results.");
154+
145155
//===----------------------------------------------------------------------===//
146156
// ChainedASTReaderListener implementation
147157
//===----------------------------------------------------------------------===//
@@ -4203,6 +4213,7 @@ ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName,
42034213
SourceLocation ImportLoc,
42044214
unsigned ClientLoadCapabilities,
42054215
SmallVectorImpl<ImportedSubmodule> *Imported) {
4216+
++NumTryLoadModule;
42064217
llvm::SaveAndRestore<SourceLocation>
42074218
SetCurImportLocRAII(CurrentImportLoc, ImportLoc);
42084219

@@ -4552,6 +4563,7 @@ ASTReader::ReadASTCore(StringRef FileName,
45524563
return Failure;
45534564
}
45544565

4566+
++NumReadASTCore;
45554567
// This is used for compatibility with older PCH formats.
45564568
bool HaveReadControlBlock = false;
45574569
while (true) {

llvm/lib/Support/MemoryBuffer.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "llvm/Support/MemoryBuffer.h"
1414
#include "llvm/ADT/SmallString.h"
15+
#include "llvm/ADT/Statistic.h"
1516
#include "llvm/Config/config.h"
1617
#include "llvm/Support/Errc.h"
1718
#include "llvm/Support/Errno.h"
@@ -34,6 +35,12 @@
3435
#endif
3536
using namespace llvm;
3637

38+
#define DEBUG_TYPE "memory-buffer"
39+
40+
ALWAYS_ENABLED_STATISTIC(NumMmapFile, "Number of mmap-ed files.");
41+
ALWAYS_ENABLED_STATISTIC(NumAllocFile,
42+
"Number of files read into allocated memory buffer.");
43+
3744
//===----------------------------------------------------------------------===//
3845
// MemoryBuffer implementation itself.
3946
//===----------------------------------------------------------------------===//
@@ -449,8 +456,10 @@ getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
449456
// buffer by copying off the stream.
450457
sys::fs::file_type Type = Status.type();
451458
if (Type != sys::fs::file_type::regular_file &&
452-
Type != sys::fs::file_type::block_file)
459+
Type != sys::fs::file_type::block_file) {
460+
++NumAllocFile;
453461
return getMemoryBufferForStream(FD, Filename);
462+
}
454463

455464
FileSize = Status.getSize();
456465
}
@@ -463,8 +472,10 @@ getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
463472
std::unique_ptr<MB> Result(
464473
new (NamedBufferAlloc(Filename)) MemoryBufferMMapFile<MB>(
465474
RequiresNullTerminator, FD, MapSize, Offset, EC));
466-
if (!EC)
475+
if (!EC) {
476+
++NumMmapFile;
467477
return std::move(Result);
478+
}
468479
}
469480

470481
auto Buf = WritableMemoryBuffer::getNewUninitMemBuffer(MapSize, Filename);
@@ -475,6 +486,7 @@ getOpenFileImpl(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
475486
}
476487

477488
// Read until EOF, zero-initialize the rest.
489+
++NumAllocFile;
478490
MutableArrayRef<char> ToRead = Buf->getBuffer();
479491
while (!ToRead.empty()) {
480492
Expected<size_t> ReadBytes =

llvm/lib/Support/Path.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "llvm/Support/Path.h"
1414
#include "llvm/ADT/ArrayRef.h"
15+
#include "llvm/ADT/Statistic.h"
1516
#include "llvm/Config/llvm-config.h"
1617
#include "llvm/Support/Endian.h"
1718
#include "llvm/Support/Errc.h"
@@ -31,6 +32,10 @@
3132
using namespace llvm;
3233
using namespace llvm::support::endian;
3334

35+
#define DEBUG_TYPE "file-system"
36+
37+
ALWAYS_ENABLED_STATISTIC(NumStatusCalls, "Number of `status` calls.");
38+
3439
namespace {
3540
using llvm::StringRef;
3641
using llvm::sys::path::is_separator;

llvm/lib/Support/Unix/Path.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ static std::error_code fillStatus(int StatRet, const struct stat &Status,
736736
}
737737

738738
std::error_code status(const Twine &Path, file_status &Result, bool Follow) {
739+
++NumStatusCalls;
739740
SmallString<128> PathStorage;
740741
StringRef P = Path.toNullTerminatedStringRef(PathStorage);
741742

@@ -745,6 +746,7 @@ std::error_code status(const Twine &Path, file_status &Result, bool Follow) {
745746
}
746747

747748
std::error_code status(int FD, file_status &Result) {
749+
++NumStatusCalls;
748750
struct stat Status;
749751
int StatRet = ::fstat(FD, &Status);
750752
return fillStatus(StatRet, Status, Result);

llvm/lib/Support/Windows/Path.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ handle_status_error:
710710
}
711711

712712
std::error_code status(const Twine &path, file_status &result, bool Follow) {
713+
++NumStatusCalls;
713714
SmallString<128> path_storage;
714715
SmallVector<wchar_t, 128> path_utf16;
715716

@@ -742,11 +743,13 @@ std::error_code status(const Twine &path, file_status &result, bool Follow) {
742743
}
743744

744745
std::error_code status(int FD, file_status &Result) {
746+
++NumStatusCalls;
745747
HANDLE FileHandle = reinterpret_cast<HANDLE>(_get_osfhandle(FD));
746748
return getStatus(FileHandle, Result);
747749
}
748750

749751
std::error_code status(file_t FileHandle, file_status &Result) {
752+
++NumStatusCalls;
750753
return getStatus(FileHandle, Result);
751754
}
752755

0 commit comments

Comments
 (0)