From dcafba18fb1f8d0c01b8066150b2c0cff73b00f3 Mon Sep 17 00:00:00 2001 From: "leishilong (A)" Date: Sat, 8 Mar 2025 16:49:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9C=A8SAMPLING=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E4=B8=8Bmmap=E6=B2=A1=E6=9C=89munmap=E5=BC=95?= =?UTF-8?q?=E5=85=A5=E7=9A=84=E5=86=85=E5=AD=98=E6=B3=84=E6=BC=8F=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pmu/evt_list.cpp | 6 ++++++ pmu/evt_list.h | 6 ++++++ pmu/pmu_list.cpp | 31 +++++++++++++++++++++++++++++++ pmu/pmu_list.h | 5 +++++ pmu/sampler.h | 10 ++++++++++ 5 files changed, 58 insertions(+) diff --git a/pmu/evt_list.cpp b/pmu/evt_list.cpp index 28e90a6..93fa00d 100644 --- a/pmu/evt_list.cpp +++ b/pmu/evt_list.cpp @@ -109,6 +109,12 @@ int KUNPENG_PMU::EvtList::Init(const bool groupEnable, const std::shared_ptrGetFd()); + if (this->pmuEvt->collectType == SAMPLING) { + std::shared_ptr perfSampler = std::dynamic_pointer_cast(perfEvt); + if (perfSampler != nullptr) { + mmapList.insert(perfSampler->GetMmap()); + } + } evtVec.emplace_back(perfEvt); } this->xyCounterArray.emplace_back(evtVec); diff --git a/pmu/evt_list.h b/pmu/evt_list.h index f27eec0..c8ac69f 100644 --- a/pmu/evt_list.h +++ b/pmu/evt_list.h @@ -86,6 +86,11 @@ public: return fdList; } + std::set> GetMmapList() const + { + return mmapList; + } + int GetEvtType() const { return pmuEvt->collectType; @@ -120,6 +125,7 @@ private: unsigned int numCpu = 0; unsigned int numPid = 0; std::set fdList; + std::set> mmapList; int64_t ts = 0; std::unordered_map procMap; SymbolMode symMode = NO_SYMBOL_RESOLVE; diff --git a/pmu/pmu_list.cpp b/pmu/pmu_list.cpp index 34af40a..3fa7030 100644 --- a/pmu/pmu_list.cpp +++ b/pmu/pmu_list.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include "linked_list.h" #include "cpu_map.h" #include "process_map.h" @@ -30,6 +31,7 @@ using namespace std; using namespace pcerr; +static const int PAGE_SIZE = sysconf(_SC_PAGESIZE); namespace KUNPENG_PMU { // Initializing pmu list singleton instance and global lock @@ -118,6 +120,8 @@ namespace KUNPENG_PMU { return err; } + AddToMmapInfo(pd, evtList); + err = AddToEpollFd(pd, evtList); if (err != SUCCESS) { return err; @@ -294,6 +298,7 @@ namespace KUNPENG_PMU { void PmuList::Close(const int pd) { + MunmapInfo(pd); auto evtList = GetEvtList(pd); for (auto item: evtList) { item->Close(); @@ -631,6 +636,32 @@ namespace KUNPENG_PMU { throw runtime_error(""); } + void PmuList::AddToMmapInfo(const unsigned pd, const std::shared_ptr& evtList) + { + if (GetTaskType(pd) == SAMPLING) { + lock_guard lg(pmuListMtx); + auto& mmapInfo = mmapList[pd]; + for (auto& mmapNode : evtList->GetMmapList()) { + mmapInfo.emplace_back(mmapNode); + } + } + } + + void PmuList::MunmapInfo(const unsigned pd) + { + if (GetTaskType(pd) == SAMPLING) { + lock_guard lg(pmuListMtx); + auto findMmap = mmapList.find(pd); + if (findMmap != mmapList.end()) { + for (auto& mmapNode : findMmap->second) { + munmap(mmapNode->base, mmapNode->mask + 1 + PAGE_SIZE); + mmapNode->base = nullptr; + } + mmapList.erase(pd); + } + } + } + int PmuList::AddToEpollFd(const int pd, const std::shared_ptr& evtList) { lock_guard lg(pmuListMtx); diff --git a/pmu/pmu_list.h b/pmu/pmu_list.h index 8ad2b7d..1f84f06 100644 --- a/pmu/pmu_list.h +++ b/pmu/pmu_list.h @@ -115,6 +115,8 @@ private: void FillStackInfo(EventData &eventData); void EraseUserData(PmuData* pmuData); + void AddToMmapInfo(const unsigned pd, const std::shared_ptr& evtList); + void MunmapInfo(const unsigned pd); int AddToEpollFd(const int pd, const std::shared_ptr &evtList); void RemoveEpollFd(const int pd); int GetEpollFd(const int pd); @@ -166,6 +168,9 @@ private: // Key: epoll fd // Value: epoll event list std::unordered_map> epollEvents; + // Key: pd + // Value: mmap list in SAMPLING Mode + std::unordered_map>> mmapList; // Key: pd // Value: spe sampling cpu list. diff --git a/pmu/sampler.h b/pmu/sampler.h index d69a1d0..5e8c99f 100644 --- a/pmu/sampler.h +++ b/pmu/sampler.h @@ -44,6 +44,16 @@ namespace KUNPENG_PMU { int MapPerfAttr(const bool groupEnable, const int groupFd) override; + bool IsMainPid() const override + { + return true; + }; + + std::shared_ptr GetMmap() const + { + return sampleMmap; + } + private: int ReadInit(); int Mmap(); -- Gitee