From 73a83c7a99962f3005f766825252d4b81338ed5b Mon Sep 17 00:00:00 2001 From: glx Date: Wed, 26 Jun 2024 22:13:25 +0800 Subject: [PATCH 1/2] Fix memleak of SPE --- pmu/evt.h | 2 +- pmu/evt_list.cpp | 4 ++-- pmu/evt_list.h | 2 +- pmu/perf_counter.cpp | 2 +- pmu/perf_counter.h | 2 +- pmu/pmu_list.cpp | 7 +++++-- pmu/pmu_list.h | 1 + pmu/sampler.cpp | 2 +- pmu/sampler.h | 2 +- pmu/spe_sampler.cpp | 14 +++++--------- pmu/spe_sampler.h | 6 ++---- 11 files changed, 21 insertions(+), 23 deletions(-) diff --git a/pmu/evt.h b/pmu/evt.h index a9e2598..9868308 100644 --- a/pmu/evt.h +++ b/pmu/evt.h @@ -44,7 +44,7 @@ public: virtual int Init() = 0; - virtual int Read(std::vector &data, std::vector &sampleIps) = 0; + virtual int Read(std::vector &data, std::vector &sampleIps, std::vector &extPool) = 0; virtual int MapPerfAttr() = 0; diff --git a/pmu/evt_list.cpp b/pmu/evt_list.cpp index 5323fb7..4563787 100644 --- a/pmu/evt_list.cpp +++ b/pmu/evt_list.cpp @@ -152,7 +152,7 @@ void KUNPENG_PMU::EvtList::FillFields( } } -int KUNPENG_PMU::EvtList::Read(vector &data, std::vector &sampleIps) +int KUNPENG_PMU::EvtList::Read(vector &data, std::vector &sampleIps, std::vector &extPool) { for (unsigned int row = 0; row < numCpu; row++) { for (unsigned int col = 0; col < numPid; col++) { @@ -168,7 +168,7 @@ int KUNPENG_PMU::EvtList::Read(vector &data, std::vector auto cpuTopo = this->cpuList[row].get(); for (unsigned int col = 0; col < numPid; col++) { auto cnt = data.size(); - int err = this->xyCounterArray[row][col]->Read(data, sampleIps); + int err = this->xyCounterArray[row][col]->Read(data, sampleIps, extPool); if (err != SUCCESS) { return err; } diff --git a/pmu/evt_list.h b/pmu/evt_list.h index 1a54b7d..b507bf0 100644 --- a/pmu/evt_list.h +++ b/pmu/evt_list.h @@ -45,7 +45,7 @@ public: int Enable(); int Stop(); int Reset(); - int Read(std::vector &pmuData, std::vector &sampleIps); + int Read(std::vector &pmuData, std::vector &sampleIps, std::vector &extPool); void SetTimeStamp(const int64_t ×tamp) { diff --git a/pmu/perf_counter.cpp b/pmu/perf_counter.cpp index 6262003..1b5e319 100644 --- a/pmu/perf_counter.cpp +++ b/pmu/perf_counter.cpp @@ -36,7 +36,7 @@ static constexpr int MAX_ATTR_SIZE = 120; * Right now we do not implement grouping logic, thus we ignore the * PERF_FORMAT_ID section for now */ -int KUNPENG_PMU::PerfCounter::Read(vector &data, std::vector &sampleIps) +int KUNPENG_PMU::PerfCounter::Read(vector &data, std::vector &sampleIps, std::vector &extPool) { struct ReadFormat perfCountValue; diff --git a/pmu/perf_counter.h b/pmu/perf_counter.h index 19628fc..289a7c3 100644 --- a/pmu/perf_counter.h +++ b/pmu/perf_counter.h @@ -36,7 +36,7 @@ namespace KUNPENG_PMU { ~PerfCounter() {} int Init() override; - int Read(std::vector &data, std::vector &sampleIps) override; + int Read(std::vector &data, std::vector &sampleIps, std::vector &extPool) override; int MapPerfAttr() override; }; } // namespace KUNPENG_PMU diff --git a/pmu/pmu_list.cpp b/pmu/pmu_list.cpp index 9117b75..e85c52b 100644 --- a/pmu/pmu_list.cpp +++ b/pmu/pmu_list.cpp @@ -159,7 +159,7 @@ namespace KUNPENG_PMU { auto eventList = GetEvtList(pd); for (auto item : eventList) { item->SetTimeStamp(ts); - auto err = item->Read(evtData.data, evtData.sampleIps); + auto err = item->Read(evtData.data, evtData.sampleIps, evtData.extPool); if (err != SUCCESS) { return err; } @@ -403,6 +403,9 @@ namespace KUNPENG_PMU { if (findData == userDataList.end()) { return; } + for (auto &extMem : findData->second.extPool) { + delete[] extMem; + } userDataList.erase(pmuData); } @@ -587,4 +590,4 @@ namespace KUNPENG_PMU { lock_guard lg(dataListMtx); return symModeList[pd]; } -} \ No newline at end of file +} diff --git a/pmu/pmu_list.h b/pmu/pmu_list.h index 91994d8..9af4906 100644 --- a/pmu/pmu_list.h +++ b/pmu/pmu_list.h @@ -82,6 +82,7 @@ private: PmuTaskType collectType; std::vector data; std::vector sampleIps; + std::vector extPool; }; void InsertEvtList(const unsigned pd, std::shared_ptr evtList); diff --git a/pmu/sampler.cpp b/pmu/sampler.cpp index f8ad789..6cad67b 100644 --- a/pmu/sampler.cpp +++ b/pmu/sampler.cpp @@ -192,7 +192,7 @@ void KUNPENG_PMU::PerfSampler::FillComm(const size_t &start, const size_t &end, } } -int KUNPENG_PMU::PerfSampler::Read(vector &data, std::vector &sampleIps) +int KUNPENG_PMU::PerfSampler::Read(vector &data, std::vector &sampleIps, std::vector &extPool) { auto err = this->ReadInit(); if (__glibc_unlikely(err != SUCCESS)) { diff --git a/pmu/sampler.h b/pmu/sampler.h index e9ab432..3dba2d8 100644 --- a/pmu/sampler.h +++ b/pmu/sampler.h @@ -40,7 +40,7 @@ namespace KUNPENG_PMU { {} int Init() override; - int Read(std::vector &data, std::vector &sampleIps) override; + int Read(std::vector &data, std::vector &sampleIps, std::vector &extPool) override; int MapPerfAttr() override; diff --git a/pmu/spe_sampler.cpp b/pmu/spe_sampler.cpp index 9cca5e4..1421ec3 100644 --- a/pmu/spe_sampler.cpp +++ b/pmu/spe_sampler.cpp @@ -65,7 +65,7 @@ namespace KUNPENG_PMU { return SUCCESS; } - int PerfSpe::Read(vector &data, std::vector &sampleIps) + int PerfSpe::Read(vector &data, std::vector &sampleIps, std::vector &extPool) { auto findSpe = speSet.find(this->cpu); if (findSpe == speSet.end()) { @@ -90,14 +90,14 @@ namespace KUNPENG_PMU { continue; } // Insert each spe record for each tid. - InsertSpeRecords(records.first, records.second, data, sampleIps); + InsertSpeRecords(records.first, records.second, data, sampleIps, extPool); } } else { // Loop over all tids. for (auto &proc : procMap) { // Get all spe records for tid. const auto &records = findSpe->second.GetPidRecords(proc.first); - InsertSpeRecords(proc.second->tid, records, data, sampleIps); + InsertSpeRecords(proc.second->tid, records, data, sampleIps, extPool); } } @@ -105,7 +105,7 @@ namespace KUNPENG_PMU { } void PerfSpe::InsertSpeRecords( - const int &tid, const std::vector &speRecords, vector &data, vector &sampleIps) + const int &tid, const std::vector &speRecords, vector &data, vector &sampleIps, std::vector &extPool) { ProcTopology *procTopo = nullptr; auto findProc = procMap.find(tid); @@ -187,12 +187,8 @@ namespace KUNPENG_PMU { findSpe->second.Close(); speSet.erase(this->cpu); - for (auto extPtr : extPool) { - delete[] extPtr; - } - extPool.clear(); return SUCCESS; } -} // namespace KUNPENG_PMU \ No newline at end of file +} // namespace KUNPENG_PMU diff --git a/pmu/spe_sampler.h b/pmu/spe_sampler.h index 2f503ce..b6c342c 100644 --- a/pmu/spe_sampler.h +++ b/pmu/spe_sampler.h @@ -37,7 +37,7 @@ namespace KUNPENG_PMU { {} int Init() override; - int Read(std::vector &data, std::vector &sampleIps) override; + int Read(std::vector &data, std::vector &sampleIps, std::vector &extPool) override; int MapPerfAttr() override; bool Mmap(); @@ -50,10 +50,8 @@ namespace KUNPENG_PMU { private: bool SpeExist(int cpu) const; void InsertSpeRecords(const int &tid, const std::vector &speRecords, std::vector &data, - std::vector &sampleIps); + std::vector &sampleIps, std::vector &extPool); void UpdatePidList(const Spe &spe); - - std::vector extPool; }; } // namespace KUNPENG_PMU #endif -- Gitee From 46677fab3673ba035da5542ccd54b239df9a6411 Mon Sep 17 00:00:00 2001 From: glx Date: Wed, 26 Jun 2024 22:34:38 +0800 Subject: [PATCH 2/2] Fix bug in SymbolMode --- pmu/sampler.cpp | 8 ++++++-- pmu/spe.cpp | 2 +- pmu/spe.h | 5 +++-- pmu/spe_sampler.cpp | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/pmu/sampler.cpp b/pmu/sampler.cpp index 6cad67b..73fcf41 100644 --- a/pmu/sampler.cpp +++ b/pmu/sampler.cpp @@ -160,11 +160,15 @@ void KUNPENG_PMU::PerfSampler::ReadRingBuffer(vector &data, vectormmap.tid, event->mmap.filename, event->mmap.addr); + if (symMode != NO_SYMBOL_RESOLVE) { + SymResolverUpdateModule(event->mmap.tid, event->mmap.filename, event->mmap.addr); + } break; } case PERF_RECORD_MMAP2: { - SymResolverUpdateModule(event->mmap2.tid, event->mmap2.filename, event->mmap2.addr); + if (symMode != NO_SYMBOL_RESOLVE) { + SymResolverUpdateModule(event->mmap2.tid, event->mmap2.filename, event->mmap2.addr); + } break; } case PERF_RECORD_FORK: { diff --git a/pmu/spe.cpp b/pmu/spe.cpp index 8470edd..81b3de6 100644 --- a/pmu/spe.cpp +++ b/pmu/spe.cpp @@ -358,7 +358,7 @@ void Spe::CoreDummyData(struct SpeCoreContext *context, struct ContextSwitchData uint64_t off = dataTail % mpage->data_size; struct perf_event_header *header = (struct perf_event_header *)(ringBuf + off); - if (header->type == PERF_RECORD_MMAP) { + if (header->type == PERF_RECORD_MMAP && symbolMode != NO_SYMBOL_RESOLVE) { struct PerfRecordMmap *sample = (struct PerfRecordMmap *)header; SymResolverUpdateModule(sample->tid, sample->filename, sample->addr); dataTail += header->size; diff --git a/pmu/spe.h b/pmu/spe.h index 7e5697f..a954913 100644 --- a/pmu/spe.h +++ b/pmu/spe.h @@ -128,8 +128,8 @@ struct SampleId { */ class Spe { public: - explicit Spe(int cpu, std::unordered_map> &procMap) - : cpu(cpu), procMap(procMap) + explicit Spe(int cpu, std::unordered_map> &procMap, SymbolMode symMode) + : cpu(cpu), procMap(procMap), symbolMode(symMode) {} ~Spe() @@ -209,6 +209,7 @@ private: const unsigned short READ = 1 << 3; int cpu = 0; + SymbolMode symbolMode = NO_SYMBOL_RESOLVE; SpeContext *ctx = nullptr; unsigned short status = NONE; int dummyFd = 0; diff --git a/pmu/spe_sampler.cpp b/pmu/spe_sampler.cpp index 1421ec3..c1af303 100644 --- a/pmu/spe_sampler.cpp +++ b/pmu/spe_sampler.cpp @@ -55,7 +55,7 @@ namespace KUNPENG_PMU { return SUCCESS; } - findSpe = speSet.emplace(this->cpu, Spe(this->cpu, procMap)).first; + findSpe = speSet.emplace(this->cpu, Spe(this->cpu, procMap, symMode)).first; auto err = findSpe->second.Open(evt); if (err != SUCCESS) { speSet.erase(this->cpu); -- Gitee