-
Notifications
You must be signed in to change notification settings - Fork 64
IPC Shared Memory Release #1061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kab163
wants to merge
6
commits into
develop
Choose a base branch
from
feature/shared-mem-reset
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9f00dc3
initial implementation for releasing shared memory
kab163 668fe3e
making helper function, adding tests
kab163 ceea167
codex attempt to fix
kab163 41165c6
adding checks and docs
kab163 134ab47
windows build guard
kab163 33f4b22
Merge branch 'develop' of github.com:llnl/Umpire into feature/shared-…
kab163 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| ////////////////////////////////////////////////////////////////////////////// | ||
| // Copyright (c) 2016-26, Lawrence Livermore National Security, LLC and Umpire | ||
| // project contributors. See the COPYRIGHT file for details. | ||
| // | ||
| // SPDX-License-Identifier: (MIT) | ||
| ////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <iostream> | ||
| #include <string> | ||
|
|
||
| #include <unistd.h> | ||
|
|
||
| #include "umpire/Allocator.hpp" | ||
| #include "umpire/ResourceManager.hpp" | ||
| #include "umpire/Umpire.hpp" | ||
|
|
||
| namespace { | ||
| std::size_t page_size() | ||
| { | ||
| long ps = ::sysconf(_SC_PAGESIZE); | ||
| return (ps > 0) ? static_cast<std::size_t>(ps) : 4096; | ||
| } | ||
|
|
||
| std::string format_bytes(std::size_t bytes) | ||
| { | ||
| constexpr double KiB = 1024.0; | ||
| constexpr double MiB = 1024.0 * KiB; | ||
| constexpr double GiB = 1024.0 * MiB; | ||
|
|
||
| const double b = static_cast<double>(bytes); | ||
| if (b >= GiB) { | ||
| return std::to_string(b / GiB) + " GiB"; | ||
| } else if (b >= MiB) { | ||
| return std::to_string(b / MiB) + " MiB"; | ||
| } else if (b >= KiB) { | ||
| return std::to_string(b / KiB) + " KiB"; | ||
| } else { | ||
| return std::to_string(bytes) + " B"; | ||
| } | ||
| } | ||
|
|
||
| void touch_one_byte_per_page(std::uint8_t* buffer, std::size_t bytes) | ||
| { | ||
| const std::size_t ps = page_size(); | ||
| for (std::size_t i = 0; i < bytes; i += ps) { | ||
| buffer[i] = static_cast<std::uint8_t>(buffer[i] + 1); | ||
| } | ||
| if (bytes > 0) { | ||
| buffer[bytes - 1] = static_cast<std::uint8_t>(buffer[bytes - 1] + 1); | ||
| } | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| int main(int, char**) | ||
| { | ||
| constexpr std::size_t segment_size = 512ULL * 1024ULL * 1024ULL; | ||
| constexpr std::size_t alloc_size = 256ULL * 1024ULL * 1024ULL; | ||
|
|
||
| auto& rm = umpire::ResourceManager::getInstance(); | ||
| auto traits = umpire::get_default_resource_traits("SHARED::POSIX"); | ||
| traits.size = segment_size; | ||
|
|
||
| const std::string allocator_name = "SHARED::POSIX::release_example"; | ||
| umpire::Allocator allocator = rm.makeResource(allocator_name, traits); | ||
|
|
||
| const std::size_t rss_before = umpire::get_process_memory_usage(); | ||
| std::cout << "RSS before: " << format_bytes(rss_before) << "\n"; | ||
|
|
||
| const std::size_t shm_rss_before = umpire::get_mapping_memory_usage(allocator_name); | ||
| if (shm_rss_before > 0) { | ||
| std::cout << "Shared segment RSS before: " << format_bytes(shm_rss_before) << "\n"; | ||
| } | ||
|
|
||
| void* ptr = nullptr; | ||
| try { | ||
| ptr = allocator.allocate("buffer", alloc_size); | ||
| } catch (const std::exception& e) { | ||
| std::cerr << "Failed to allocate " << format_bytes(alloc_size) << ": " << e.what() << "\n"; | ||
| return 1; | ||
| } | ||
|
|
||
| touch_one_byte_per_page(static_cast<std::uint8_t*>(ptr), alloc_size); | ||
|
|
||
| const std::size_t rss_after_touch = umpire::get_process_memory_usage(); | ||
| std::cout << "RSS after touching allocation: " << format_bytes(rss_after_touch) << "\n"; | ||
|
|
||
| const std::size_t shm_rss_after_touch = umpire::get_mapping_memory_usage(allocator_name); | ||
| if (shm_rss_after_touch > 0) { | ||
| std::cout << "Shared segment RSS after touch: " << format_bytes(shm_rss_after_touch) << "\n"; | ||
| } | ||
|
|
||
| allocator.deallocate(ptr); | ||
|
|
||
| const std::size_t rss_after_free = umpire::get_process_memory_usage(); | ||
| std::cout << "RSS after deallocate (before release): " << format_bytes(rss_after_free) << "\n"; | ||
|
|
||
| const std::size_t shm_rss_after_free = umpire::get_mapping_memory_usage(allocator_name); | ||
| if (shm_rss_after_free > 0) { | ||
| std::cout << "Shared segment RSS after deallocate: " << format_bytes(shm_rss_after_free) << "\n"; | ||
| } | ||
|
|
||
| allocator.release(); | ||
|
|
||
| const std::size_t rss_after_release = umpire::get_process_memory_usage(); | ||
| std::cout << "RSS after allocator.release(): " << format_bytes(rss_after_release) << "\n"; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "allocator.release()" should probably be changed to "release" to match the other output. |
||
|
|
||
| const std::size_t shm_rss_after_release = umpire::get_mapping_memory_usage(allocator_name); | ||
| if (shm_rss_after_release > 0) { | ||
| std::cout << "Shared segment RSS after release: " << format_bytes(shm_rss_after_release) << "\n"; | ||
| } else { | ||
| std::cout << "NOTE: shared segment RSS reporting requires Linux (/proc/self/smaps)\n"; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ | |
| #include "umpire/Umpire.hpp" | ||
|
|
||
| #include <algorithm> | ||
| #include <cctype> | ||
| #include <cstdio> | ||
| #include <iostream> | ||
| #include <iterator> | ||
| #include <limits> | ||
|
|
@@ -17,7 +19,9 @@ | |
|
|
||
| #include "umpire/ResourceManager.hpp" | ||
| #include "umpire/config.hpp" | ||
| #if defined(UMPIRE_ENABLE_IPC_SHARED_MEMORY) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it a different file included for MPI shared memory? |
||
| #include "umpire/resource/HostSharedMemoryResource.hpp" | ||
| #endif | ||
| #include "umpire/resource/MemoryResource.hpp" | ||
| #if defined(UMPIRE_ENABLE_MPI) && defined(UMPIRE_ENABLE_IPC_SHARED_MEMORY) | ||
| #if defined(UMPIRE_ENABLE_DEVICE) | ||
|
|
@@ -172,6 +176,45 @@ std::size_t get_process_memory_usage() | |
| #endif | ||
| } | ||
|
|
||
| std::size_t get_mapping_memory_usage(const std::string& mapping_name) | ||
| { | ||
| #if defined(__linux__) | ||
| std::ifstream smaps{"/proc/self/smaps"}; | ||
| if (!smaps) { | ||
| return 0; | ||
| } | ||
|
|
||
| std::size_t rss_kb{0}; | ||
| bool in_target_mapping{false}; | ||
| std::string line; | ||
|
|
||
| while (std::getline(smaps, line)) { | ||
| // Memory mapping header lines follow the format: "address-address perms ..." | ||
| // Check if line starts with hex digits and contains a hyphen in the first field | ||
| const bool is_header = (!line.empty() && std::isxdigit(static_cast<unsigned char>(line[0])) && | ||
| line.find('-') != std::string::npos && line.find('-') < 20); | ||
|
|
||
| if (is_header) { | ||
| in_target_mapping = (line.find(mapping_name) != std::string::npos); | ||
| continue; | ||
| } | ||
|
|
||
| if (!in_target_mapping || line.rfind("Rss:", 0) != 0) { | ||
| continue; | ||
| } | ||
|
|
||
| std::size_t value_kb{0}; | ||
| std::sscanf(line.c_str(), "Rss: %zu kB", &value_kb); | ||
| rss_kb += value_kb; | ||
| } | ||
|
|
||
| return rss_kb * 1024; | ||
| #else | ||
| UMPIRE_USE_VAR(mapping_name); | ||
| return 0; | ||
| #endif | ||
| } | ||
|
|
||
| std::size_t get_internal_memory_usage() | ||
| { | ||
| return umpire::ResourceManager::getInstance().getInternalMemoryUsage(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not simplify to
++buffer[i];?