Skip to content

Commit

Permalink
[memprof][NFC] Free symbolizer memory eagerly (llvm#75849)
Browse files Browse the repository at this point in the history
Move the ownership of the symbolizer into symbolizeAndFilterStackFrames
so that it is freed on exit, when we are done with it, to reduce peak
memory in the reader. This reduces about 9G from the peak for one large
profile.
  • Loading branch information
teresajohnson authored Dec 19, 2023
1 parent 6ce23ea commit 6a7bbf7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
11 changes: 5 additions & 6 deletions llvm/include/llvm/ProfileData/RawMemProfReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ class RawMemProfReader final : public MemProfReader {
llvm::SmallVectorImpl<SegmentEntry> &Seg,
llvm::MapVector<uint64_t, MemInfoBlock> &Prof,
CallStackMap &SM, bool KeepName = false)
: Symbolizer(std::move(Sym)), SegmentInfo(Seg.begin(), Seg.end()),
CallstackProfileData(Prof), StackMap(SM), KeepSymbolName(KeepName) {
: SegmentInfo(Seg.begin(), Seg.end()), CallstackProfileData(Prof),
StackMap(SM), KeepSymbolName(KeepName) {
// We don't call initialize here since there is no raw profile to read. The
// test should pass in the raw profile as structured data.

// If there is an error here then the mock symbolizer has not been
// initialized properly.
if (Error E = symbolizeAndFilterStackFrames())
if (Error E = symbolizeAndFilterStackFrames(std::move(Sym)))
report_fatal_error(std::move(E));
if (Error E = mapRawProfileToRecords())
report_fatal_error(std::move(E));
Expand All @@ -173,7 +173,8 @@ class RawMemProfReader final : public MemProfReader {
// callstacks from the raw profile. Also prune callstack frames which we can't
// symbolize or those that belong to the runtime. For profile entries where
// the entire callstack is pruned, we drop the entry from the profile.
Error symbolizeAndFilterStackFrames();
Error symbolizeAndFilterStackFrames(
std::unique_ptr<llvm::symbolize::SymbolizableModule> Symbolizer);
// Construct memprof records for each function and store it in the
// `FunctionProfileData` map. A function may have allocation profile data or
// callsite data or both.
Expand All @@ -183,8 +184,6 @@ class RawMemProfReader final : public MemProfReader {

// The profiled binary.
object::OwningBinary<object::Binary> Binary;
// A symbolizer to translate virtual addresses to code locations.
std::unique_ptr<llvm::symbolize::SymbolizableModule> Symbolizer;
// The preferred load address of the executable segment.
uint64_t PreferredTextSegmentAddress = 0;
// The base address of the text segment in the process during profiling.
Expand Down
25 changes: 15 additions & 10 deletions llvm/lib/ProfileData/RawMemProfReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,13 @@ Error RawMemProfReader::initialize(std::unique_ptr<MemoryBuffer> DataBuffer) {
inconvertibleErrorCode()),
FileName);

// Process the raw profile.
if (Error E = readRawProfile(std::move(DataBuffer)))
return E;

if (Error E = setupForSymbolization())
return E;

auto *Object = cast<object::ObjectFile>(Binary.getBinary());
std::unique_ptr<DIContext> Context = DWARFContext::create(
*Object, DWARFContext::ProcessDebugRelocations::Process);
Expand All @@ -344,16 +351,13 @@ Error RawMemProfReader::initialize(std::unique_ptr<MemoryBuffer> DataBuffer) {
Object, std::move(Context), /*UntagAddresses=*/false);
if (!SOFOr)
return report(SOFOr.takeError(), FileName);
Symbolizer = std::move(SOFOr.get());

// Process the raw profile.
if (Error E = readRawProfile(std::move(DataBuffer)))
return E;

if (Error E = setupForSymbolization())
return E;
auto Symbolizer = std::move(SOFOr.get());

if (Error E = symbolizeAndFilterStackFrames())
// The symbolizer ownership is moved into symbolizeAndFilterStackFrames so
// that it is freed automatically at the end, when it is no longer used. This
// reduces peak memory since it won't be live while also mapping the raw
// profile into records afterwards.
if (Error E = symbolizeAndFilterStackFrames(std::move(Symbolizer)))
return E;

return mapRawProfileToRecords();
Expand Down Expand Up @@ -469,7 +473,8 @@ Error RawMemProfReader::mapRawProfileToRecords() {
return Error::success();
}

Error RawMemProfReader::symbolizeAndFilterStackFrames() {
Error RawMemProfReader::symbolizeAndFilterStackFrames(
std::unique_ptr<llvm::symbolize::SymbolizableModule> Symbolizer) {
// The specifier to use when symbolization is requested.
const DILineInfoSpecifier Specifier(
DILineInfoSpecifier::FileLineInfoKind::RawValue,
Expand Down

0 comments on commit 6a7bbf7

Please sign in to comment.