Daniel Carvalho has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/38703 )

Change subject: arch-arm: Fix memory leak of PMU events
......................................................................

arch-arm: Fix memory leak of PMU events

Memory of PMU events was never being released.

Jira: https://gem5.atlassian.net/browse/GEM5-857

Change-Id: I3cd9583103333008799f0873af3a490f847a21b5
Signed-off-by: Daniel R. Carvalho <[email protected]>
---
M src/arch/arm/pmu.cc
M src/arch/arm/pmu.hh
2 files changed, 17 insertions(+), 19 deletions(-)



diff --git a/src/arch/arm/pmu.cc b/src/arch/arm/pmu.cc
index c7489f6..a3f8437 100644
--- a/src/arch/arm/pmu.cc
+++ b/src/arch/arm/pmu.cc
@@ -39,6 +39,7 @@

 #include "arch/arm/isa.hh"
 #include "arch/arm/utility.hh"
+#include "base/compiler.hh"
 #include "base/trace.hh"
 #include "cpu/base.hh"
 #include "debug/Checkpoint.hh"
@@ -101,7 +102,7 @@
 void
 PMU::addSoftwareIncrementEvent(unsigned int id)
 {
-    auto old_event = eventMap.find(id);
+    M5_VAR_USED auto old_event = eventMap.find(id);
DPRINTF(PMUVerbose, "PMU: Adding SW increment event with id '0x%x'\n", id);

     if (swIncrementEvent) {
@@ -115,8 +116,9 @@
     fatal_if(old_event != eventMap.end(), "An event with id %d has "
              "been previously defined\n", id);

-    swIncrementEvent = new SWIncrementEvent();
-    eventMap[id] = swIncrementEvent;
+    swIncrementEvent = std::make_shared<SWIncrementEvent>();
+    eventMap[id] =
+        std::static_pointer_cast<SWIncrementEvent>(swIncrementEvent);
     registerEvent(id);
 }

@@ -130,15 +132,11 @@
     RegularEvent *event = nullptr;
     auto event_entry = eventMap.find(id);
     if (event_entry == eventMap.end()) {
-
-        event = new RegularEvent();
-        eventMap[id] = event;
-
+        eventMap[id] = std::make_shared<RegularEvent>();
+        event = static_cast<RegularEvent*>(eventMap[id].get());
     } else {
-        event = dynamic_cast<RegularEvent*>(event_entry->second);
-        if (!event) {
-            fatal("Event with id %d is not probe driven\n", id);
-        }
+        event = dynamic_cast<RegularEvent*>(event_entry->second.get());
+        fatal_if(!event, "Event with id %d is not probe driven\n", id);
     }
     event->addMicroarchitectureProbe(obj, probe_name);

@@ -179,7 +177,7 @@
         counters.emplace_back(*this, index);
     }

-    PMUEvent *event = getEvent(cycleCounterEventId);
+    std::shared_ptr<PMUEvent> event = getEvent(cycleCounterEventId);
     panic_if(!event, "core cycle event is not present\n");
     cycleCounter.enabled = true;
     cycleCounter.attach(event);
@@ -523,7 +521,7 @@
 }

 void
-PMU::CounterState::attach(PMUEvent* event)
+PMU::CounterState::attach(const std::shared_ptr<PMUEvent> &event)
 {
     if (!resetValue) {
       value = 0;
@@ -726,7 +724,7 @@
     cycleCounter.unserializeSection(cp, "cycleCounter");
 }

-PMU::PMUEvent*
+std::shared_ptr<PMU::PMUEvent>
 PMU::getEvent(uint64_t eventId)
 {
     auto entry = eventMap.find(eventId);
diff --git a/src/arch/arm/pmu.hh b/src/arch/arm/pmu.hh
index 64d3b3d..92c3097 100644
--- a/src/arch/arm/pmu.hh
+++ b/src/arch/arm/pmu.hh
@@ -388,7 +388,7 @@
      * @param the id of the event to obtain
      * @return a pointer to the event with id eventId
      */
-    PMUEvent* getEvent(uint64_t eventId);
+    std::shared_ptr<PMUEvent> getEvent(uint64_t eventId);

     /** State of a counter within the PMU. **/
     struct CounterState : public Serializable {
@@ -421,7 +421,7 @@
          *
          * @param the event to attach the counter to
          */
-        void attach(PMUEvent* event);
+        void attach(const std::shared_ptr<PMUEvent> &event);

         /**
          * Obtain the counter id
@@ -461,7 +461,7 @@

       protected: /* Configuration */
         /** PmuEvent currently in use (if any) **/
-        PMUEvent *sourceEvent;
+        std::shared_ptr<PMUEvent> sourceEvent;

         /** id of the counter instance **/
         uint64_t counterId;
@@ -591,7 +591,7 @@
     const uint64_t cycleCounterEventId;

     /** The event that implements the software increment **/
-    SWIncrementEvent *swIncrementEvent;
+    std::shared_ptr<SWIncrementEvent> swIncrementEvent;

   protected: /* Configuration and constants */
     /** Constant (configuration-dependent) part of the PMCR */
@@ -606,7 +606,7 @@
     /**
      * List of event types supported by this PMU.
      */
-    std::map<EventTypeId, PMUEvent*> eventMap;
+    std::map<EventTypeId, std::shared_ptr<PMUEvent>> eventMap;
 };

 } // namespace ArmISA

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/38703
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I3cd9583103333008799f0873af3a490f847a21b5
Gerrit-Change-Number: 38703
Gerrit-PatchSet: 1
Gerrit-Owner: Daniel Carvalho <[email protected]>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to