Jui-min Lee has submitted this change. ( https://gem5-review.googlesource.com/c/public/gem5/+/57469 )

Change subject: mem: Add option to remove shared memory at the end
......................................................................

mem: Add option to remove shared memory at the end

Add a new option `auto_unlink_shared_backstore` to System so it will
remove the shared backstore used in physical memories when the System is
getting destructed. This will prevent unintended memory leak.

If the shared memory is designed to live through multiple round of
simulations, you may set the option to false to prevent the removal.

Test: Run a simulation with shared_backstore set, and see whether there
    is anything left in /dev/shm/ after simulation ends.
Change-Id: I0267b643bd24e62cb7571674fe98f831c13a586d
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/57469
Reviewed-by: Daniel Carvalho <oda...@yahoo.com.br>
Maintainer: Daniel Carvalho <oda...@yahoo.com.br>
Tested-by: kokoro <noreply+kok...@google.com>
---
M src/mem/physical.cc
M src/mem/physical.hh
M src/sim/System.py
M src/sim/system.cc
4 files changed, 36 insertions(+), 3 deletions(-)

Approvals:
  Daniel Carvalho: Looks good to me, approved; Looks good to me, approved
  kokoro: Regressions pass




diff --git a/src/mem/physical.cc b/src/mem/physical.cc
index a7e261f..08707eb 100644
--- a/src/mem/physical.cc
+++ b/src/mem/physical.cc
@@ -55,6 +55,7 @@
 #include "debug/Checkpoint.hh"
 #include "mem/abstract_mem.hh"
 #include "sim/serialize.hh"
+#include "sim/sim_exit.hh"

 /**
  * On Linux, MAP_NORESERVE allow us to simulate a very large memory
@@ -77,10 +78,16 @@
 PhysicalMemory::PhysicalMemory(const std::string& _name,
const std::vector<AbstractMemory*>& _memories,
                                bool mmap_using_noreserve,
-                               const std::string& shared_backstore) :
+                               const std::string& shared_backstore,
+                               bool auto_unlink_shared_backstore) :
     _name(_name), size(0), mmapUsingNoReserve(mmap_using_noreserve),
     sharedBackstore(shared_backstore), sharedBackstoreSize(0)
 {
+    // Register cleanup callback if requested.
+    if (auto_unlink_shared_backstore && !sharedBackstore.empty()) {
+ registerExitCallback([=]() { shm_unlink(shared_backstore.c_str()); });
+    }
+
     if (mmap_using_noreserve)
warn("Not reserving swap space. May cause SIGSEGV on actual usage\n");

diff --git a/src/mem/physical.hh b/src/mem/physical.hh
index bb90664..ff0dc61 100644
--- a/src/mem/physical.hh
+++ b/src/mem/physical.hh
@@ -174,7 +174,8 @@
     PhysicalMemory(const std::string& _name,
                    const std::vector<AbstractMemory*>& _memories,
                    bool mmap_using_noreserve,
-                   const std::string& shared_backstore);
+                   const std::string& shared_backstore,
+                   bool auto_unlink_shared_backstore);

     /**
      * Unmap all the backing store we have used.
diff --git a/src/sim/System.py b/src/sim/System.py
index 499cf9b..b5bd5df 100644
--- a/src/sim/System.py
+++ b/src/sim/System.py
@@ -87,6 +87,9 @@
shared_backstore = Param.String("", "backstore's shmem segment filename, " "use to directly address the backstore from another host-OS process. "
         "Leave this empty to unset the MAP_SHARED flag.")
+ auto_unlink_shared_backstore = Param.Bool(False, "Automatically remove the "
+        "shmem segment file upon destruction. This is used only if "
+        "shared_backstore is non-empty.")

     cache_line_size = Param.Unsigned(64, "Cache line size in bytes")

diff --git a/src/sim/system.cc b/src/sim/system.cc
index d5e5e3f..b7fba8a 100644
--- a/src/sim/system.cc
+++ b/src/sim/system.cc
@@ -182,7 +182,7 @@
       physProxy(_systemPort, p.cache_line_size),
       workload(p.workload),
       physmem(name() + ".physmem", p.memories, p.mmap_using_noreserve,
-              p.shared_backstore),
+              p.shared_backstore, p.auto_unlink_shared_backstore),
       ShadowRomRanges(p.shadow_rom_ranges.begin(),
                       p.shadow_rom_ranges.end()),
       memoryMode(p.mem_mode),

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/57469
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: I0267b643bd24e62cb7571674fe98f831c13a586d
Gerrit-Change-Number: 57469
Gerrit-PatchSet: 8
Gerrit-Owner: Jui-min Lee <f...@google.com>
Gerrit-Reviewer: Daniel Carvalho <oda...@yahoo.com.br>
Gerrit-Reviewer: Gabe Black <gabe.bl...@gmail.com>
Gerrit-Reviewer: Jui-min Lee <f...@google.com>
Gerrit-Reviewer: Nikos Nikoleris <nikos.nikole...@arm.com>
Gerrit-Reviewer: kokoro <noreply+kok...@google.com>
Gerrit-MessageType: merged
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to