Jui-min Lee has uploaded this change for review. ( 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
---
M src/mem/physical.cc
M src/mem/physical.hh
M src/sim/System.py
M src/sim/system.cc
4 files changed, 30 insertions(+), 3 deletions(-)



diff --git a/src/mem/physical.cc b/src/mem/physical.cc
index a7e261f..4e0cfbc 100644
--- a/src/mem/physical.cc
+++ b/src/mem/physical.cc
@@ -77,9 +77,12 @@
 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)
+    sharedBackstore(shared_backstore),
+    autoUnlinkSharedBackstore(auto_unlink_shared_backstore),
+    sharedBackstoreSize(0)
 {
     if (mmap_using_noreserve)
warn("Not reserving swap space. May cause SIGSEGV on actual usage\n");
@@ -255,6 +258,8 @@
     // unmap the backing store
     for (auto& s : backingStore)
         munmap((char*)s.pmem, s.range.size());
+    if (autoUnlinkSharedBackstore && !sharedBackstore.empty())
+        shm_unlink(sharedBackstore.c_str());
 }

 bool
diff --git a/src/mem/physical.hh b/src/mem/physical.hh
index bb90664..3e7d031 100644
--- a/src/mem/physical.hh
+++ b/src/mem/physical.hh
@@ -140,6 +140,7 @@
     const bool mmapUsingNoReserve;

     const std::string sharedBackstore;
+    const bool autoUnlinkSharedBackstore;
     uint64_t sharedBackstoreSize;

     // The physical memory used to provide the memory in the simulated
diff --git a/src/sim/System.py b/src/sim/System.py
index 499cf9b..a1bff12 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(True, "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: 1
Gerrit-Owner: Jui-min Lee <f...@google.com>
Gerrit-MessageType: newchange
_______________________________________________
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