Skip to content

[memprof][NFC] Free symbolizer memory eagerly #75849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it makes a difference to the peak memory usage if we move L343-L347 after the readRawRawProfile call. That would mean that we initialize the Symbolizer after we free the raw profile data buffer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's a good idea. In fact, we can move it after setupForSymbolization(), although I don't think there is a memory savings from doing that.


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