[llvm-exegesis] Fix X86LbrCounter destructor to correctly unmap memory and not double-close fd (PR50620)

As was reported on PR50620, the X86LbrCounter destructor was double-closing the filedescriptor and not unmapping the buffer.

Differential Revision: https://reviews.llvm.org/D104201
This commit is contained in:
Simon Pilgrim 2021-06-15 14:16:28 +01:00
parent f112bd61eb
commit 941188e965
1 changed files with 10 additions and 5 deletions

View File

@ -41,6 +41,10 @@ static constexpr int kLbrEntries = 16;
static constexpr size_t kBufferPages = 8;
static const size_t kDataBufferSize = kBufferPages * getpagesize();
// First page is reserved for perf_event_mmap_page. Data buffer starts on
// the next page, so we allocate one more page.
static const size_t kMappedBufferSize = (kBufferPages + 1) * getpagesize();
// Waits for the LBR perf events.
static int pollLbrPerfEvent(const int FileDescriptor) {
struct pollfd PollFd;
@ -137,15 +141,16 @@ X86LbrPerfEvent::X86LbrPerfEvent(unsigned SamplingPeriod) {
X86LbrCounter::X86LbrCounter(pfm::PerfEvent &&NewEvent)
: Counter(std::move(NewEvent)) {
// First page is reserved for perf_event_mmap_page. Data buffer starts on
// the next page, so we allocate one more page.
MMappedBuffer = mmap(nullptr, (kBufferPages + 1) * getpagesize(),
PROT_READ | PROT_WRITE, MAP_SHARED, FileDescriptor, 0);
MMappedBuffer = mmap(nullptr, kMappedBufferSize, PROT_READ | PROT_WRITE,
MAP_SHARED, FileDescriptor, 0);
if (MMappedBuffer == MAP_FAILED)
llvm::errs() << "Failed to mmap buffer.";
}
X86LbrCounter::~X86LbrCounter() { close(FileDescriptor); }
X86LbrCounter::~X86LbrCounter() {
if (0 != munmap(MMappedBuffer, kMappedBufferSize))
llvm::errs() << "Failed to munmap buffer.";
}
void X86LbrCounter::start() {
ioctl(FileDescriptor, PERF_EVENT_IOC_REFRESH, 1024 /* kMaxPollsPerFd */);