From 33186946d22d700a0a8eea3296ccc17c7c667d15 Mon Sep 17 00:00:00 2001 From: doublefree Date: Thu, 7 Aug 2025 16:45:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=81=9C=E6=AD=A2=E9=87=87=E9=9B=86=E6=97=B6?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E8=AF=BB=E5=8F=96=E5=AF=84=E5=AD=98=E5=99=A8?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pmu/perf_counter.cpp | 21 +++++++++++++++++---- pmu/perf_counter.h | 1 + 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pmu/perf_counter.cpp b/pmu/perf_counter.cpp index cd00fc5..e3640ff 100644 --- a/pmu/perf_counter.cpp +++ b/pmu/perf_counter.cpp @@ -145,6 +145,12 @@ int KUNPENG_PMU::PerfCounter::ReadSingleEvent(std::vector &data) { ReadFormat perfCountValue; if (this->evt->enableUserAccess) { + if (!this->isCollect) { + // To keep consistency with read(fd) while the counting is disabled, + // we should return the value we last read as we can't access the register now. + CountValueToData(this->accumCount[0], this->enabled, this->running, this->accumCount[0], data); + return SUCCESS; + } int err = PerfMmapReadSelf(this->countMmap, perfCountValue); if (err != SUCCESS) { return err; @@ -354,6 +360,7 @@ int KUNPENG_PMU::PerfCounter::MapPerfAttrUserAccess() attr.type = this->evt->type; attr.config = this->evt->config; attr.config1 = this->evt->config1; + attr.disabled = 1; this->fd = PerfEventOpen(&attr, this->pid, this->cpu, -1, 0); DBG_PRINT("type: %d cpu: %d config: %llx config1: %llx myfd: %d \n", attr.type, @@ -364,6 +371,7 @@ int KUNPENG_PMU::PerfCounter::MapPerfAttrUserAccess() if (__glibc_unlikely(this->fd < 0)) { return MapErrno(errno); } + this->groupFd = -1; return SUCCESS; } @@ -380,9 +388,6 @@ int KUNPENG_PMU::PerfCounter::Mmap() return LIBPERF_ERR_FAIL_MMAP; } this->countMmap->base = static_cast(currentMap); - if (this->countMmap->base->index == 0) { - return LIBPERF_ERR_COUNTER_INDEX_IS_ZERO; - } this->countMmap->fd = this->fd; return SUCCESS; } @@ -401,6 +406,10 @@ int KUNPENG_PMU::PerfCounter::Enable() if (err != SUCCESS) { return err; } + if (this->evt->enableUserAccess && this->countMmap->base->index == 0) { + return LIBPERF_ERR_COUNTER_INDEX_IS_ZERO; + } + this->isCollect = true; this->accumCount.clear(); this->enabled = 0; this->running = 0; @@ -412,7 +421,11 @@ int KUNPENG_PMU::PerfCounter::Disable() if (groupFd != -1) { return SUCCESS; } - return PerfEvt::Disable(); + int err = PerfEvt::Disable(); + if (err == SUCCESS) { + this->isCollect = false; + } + return err; } int KUNPENG_PMU::PerfCounter::Reset() diff --git a/pmu/perf_counter.h b/pmu/perf_counter.h index 0fe1e3d..cdb6ddd 100644 --- a/pmu/perf_counter.h +++ b/pmu/perf_counter.h @@ -71,6 +71,7 @@ namespace KUNPENG_PMU { GroupStatus groupStatus = GroupStatus::NO_GROUP; // reg index is stored in countMmap->base std::shared_ptr countMmap = nullptr; + bool isCollect{false}; }; } // namespace KUNPENG_PMU #endif -- Gitee