From da4e5fc86158c669ddcb335d151fffd7c6e2ed4b Mon Sep 17 00:00:00 2001 From: Wenlei He Date: Thu, 7 Oct 2021 21:27:17 -0700 Subject: [PATCH] [llvm-profgen] Deduplicate PID when processing perf input When parsing mmap to retrieve PID, deduplicate them before passing PID list to perf script. Perf script would error out when there's duplicated PID in the input, however raw perf data may main duplicated PID for large binary where more than one mmap is needed to load executable segment. Differential Revision: https://reviews.llvm.org/D111384 --- llvm/tools/llvm-profgen/PerfReader.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/llvm/tools/llvm-profgen/PerfReader.cpp b/llvm/tools/llvm-profgen/PerfReader.cpp index 019ca5ab4651..2aa66832a051 100644 --- a/llvm/tools/llvm-profgen/PerfReader.cpp +++ b/llvm/tools/llvm-profgen/PerfReader.cpp @@ -322,18 +322,26 @@ std::string PerfReaderBase::convertPerfDataToTrace(ProfiledBinary *Binary, // Collect the PIDs TraceStream TraceIt(PerfTraceFile); std::string PIDs; + std::unordered_set PIDSet; while (!TraceIt.isAtEoF()) { MMapEvent MMap; if (isMMap2Event(TraceIt.getCurrentLine()) && extractMMap2EventForBinary(Binary, TraceIt.getCurrentLine(), MMap)) { - if (!PIDs.empty()) { - PIDs.append(","); + auto It = PIDSet.emplace(MMap.PID); + if (It.second) { + if (!PIDs.empty()) { + PIDs.append(","); + } + PIDs.append(utostr(MMap.PID)); } - PIDs.append(utostr(MMap.PID)); } TraceIt.advance(); } + if (PIDs.empty()) { + exitWithError("No relevant mmap event is found in perf data."); + } + // Run perf script again to retrieve events for PIDs collected above StringRef ScriptSampleArgs[] = {PerfPath, "script", "--show-mmap-events", "-F", "ip,brstack", "--pid",