Skip to content

Commit 668fe3e

Browse files
committed
making helper function, adding tests
1 parent 9f00dc3 commit 668fe3e

6 files changed

Lines changed: 53 additions & 51 deletions

File tree

examples/ipc_shared_memory_release.cpp

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,8 @@
77

88
#include <cstddef>
99
#include <cstdint>
10-
#include <cctype>
11-
#include <cstdio>
12-
#include <cstring>
13-
#include <fstream>
1410
#include <iostream>
1511
#include <string>
16-
#include <vector>
1712

1813
#include <unistd.h>
1914

@@ -57,44 +52,6 @@ void touch_one_byte_per_page(std::uint8_t* buffer, std::size_t bytes)
5752
}
5853
}
5954

60-
std::size_t get_smaps_rss_bytes_for_mapping(const std::string& mapping_name_substr)
61-
{
62-
#if defined(__linux__)
63-
std::ifstream smaps("/proc/self/smaps");
64-
if (!smaps) {
65-
return 0;
66-
}
67-
68-
std::size_t rss_kb{0};
69-
bool in_target{false};
70-
71-
std::string line;
72-
while (std::getline(smaps, line)) {
73-
const bool is_header = (!line.empty() && std::isxdigit(static_cast<unsigned char>(line[0])) && line.find('-') != std::string::npos);
74-
75-
if (is_header) {
76-
in_target = (line.find(mapping_name_substr) != std::string::npos);
77-
continue;
78-
}
79-
80-
if (!in_target) {
81-
continue;
82-
}
83-
84-
constexpr const char* rss_prefix = "Rss:";
85-
if (line.rfind(rss_prefix, 0) == 0) {
86-
std::size_t value_kb{0};
87-
std::sscanf(line.c_str(), "Rss: %zu kB", &value_kb);
88-
rss_kb += value_kb;
89-
}
90-
}
91-
92-
return rss_kb * 1024;
93-
#else
94-
(void)mapping_name_substr;
95-
return 0;
96-
#endif
97-
}
9855
} // namespace
9956

10057
int main(int, char**)
@@ -112,7 +69,7 @@ int main(int, char**)
11269
const std::size_t rss_before = umpire::get_process_memory_usage();
11370
std::cout << "RSS before: " << format_bytes(rss_before) << "\n";
11471

115-
const std::size_t shm_rss_before = get_smaps_rss_bytes_for_mapping(allocator_name);
72+
const std::size_t shm_rss_before = umpire::get_mapping_memory_usage(allocator_name);
11673
if (shm_rss_before > 0) {
11774
std::cout << "Shared segment RSS before: " << format_bytes(shm_rss_before) << "\n";
11875
}
@@ -123,7 +80,7 @@ int main(int, char**)
12380
const std::size_t rss_after_touch = umpire::get_process_memory_usage();
12481
std::cout << "RSS after touching allocation: " << format_bytes(rss_after_touch) << "\n";
12582

126-
const std::size_t shm_rss_after_touch = get_smaps_rss_bytes_for_mapping(allocator_name);
83+
const std::size_t shm_rss_after_touch = umpire::get_mapping_memory_usage(allocator_name);
12784
if (shm_rss_after_touch > 0) {
12885
std::cout << "Shared segment RSS after touch: " << format_bytes(shm_rss_after_touch) << "\n";
12986
}
@@ -133,7 +90,7 @@ int main(int, char**)
13390
const std::size_t rss_after_free = umpire::get_process_memory_usage();
13491
std::cout << "RSS after deallocate (before release): " << format_bytes(rss_after_free) << "\n";
13592

136-
const std::size_t shm_rss_after_free = get_smaps_rss_bytes_for_mapping(allocator_name);
93+
const std::size_t shm_rss_after_free = umpire::get_mapping_memory_usage(allocator_name);
13794
if (shm_rss_after_free > 0) {
13895
std::cout << "Shared segment RSS after deallocate: " << format_bytes(shm_rss_after_free) << "\n";
13996
}
@@ -143,7 +100,7 @@ int main(int, char**)
143100
const std::size_t rss_after_release = umpire::get_process_memory_usage();
144101
std::cout << "RSS after allocator.release(): " << format_bytes(rss_after_release) << "\n";
145102

146-
const std::size_t shm_rss_after_release = get_smaps_rss_bytes_for_mapping(allocator_name);
103+
const std::size_t shm_rss_after_release = umpire::get_mapping_memory_usage(allocator_name);
147104
if (shm_rss_after_release > 0) {
148105
std::cout << "Shared segment RSS after release: " << format_bytes(shm_rss_after_release) << "\n";
149106
} else {

src/umpire/Umpire.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "umpire/Umpire.hpp"
99

1010
#include <algorithm>
11+
#include <cctype>
12+
#include <cstdio>
1113
#include <iostream>
1214
#include <iterator>
1315
#include <limits>
@@ -172,6 +174,43 @@ std::size_t get_process_memory_usage()
172174
#endif
173175
}
174176

177+
std::size_t get_mapping_memory_usage(const std::string& mapping_name)
178+
{
179+
#if defined(__linux__)
180+
std::ifstream smaps{"/proc/self/smaps"};
181+
if (!smaps) {
182+
return 0;
183+
}
184+
185+
std::size_t rss_kb{0};
186+
bool in_target_mapping{false};
187+
std::string line;
188+
189+
while (std::getline(smaps, line)) {
190+
const bool is_header = (!line.empty() && std::isxdigit(static_cast<unsigned char>(line[0])) &&
191+
line.find('-') != std::string::npos);
192+
193+
if (is_header) {
194+
in_target_mapping = (line.find(mapping_name) != std::string::npos);
195+
continue;
196+
}
197+
198+
if (!in_target_mapping || line.rfind("Rss:", 0) != 0) {
199+
continue;
200+
}
201+
202+
std::size_t value_kb{0};
203+
std::sscanf(line.c_str(), "Rss: %zu kB", &value_kb);
204+
rss_kb += value_kb;
205+
}
206+
207+
return rss_kb * 1024;
208+
#else
209+
UMPIRE_USE_VAR(mapping_name);
210+
return 0;
211+
#endif
212+
}
213+
175214
std::size_t get_internal_memory_usage()
176215
{
177216
return umpire::ResourceManager::getInstance().getInternalMemoryUsage();

src/umpire/Umpire.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ std::string get_backtrace(void* ptr);
151151
*/
152152
std::size_t get_process_memory_usage();
153153

154+
/*!
155+
* \brief Get resident memory usage for memory mappings whose name contains
156+
* the provided substring.
157+
*
158+
* On Linux, this uses /proc/self/smaps and sums the RSS values for matching
159+
* mappings. Returns 0 on unsupported platforms.
160+
*/
161+
std::size_t get_mapping_memory_usage(const std::string& mapping_name);
162+
154163
/*!
155164
* \brief Get high watermark memory usage of the current process (uses underlying
156165
* system-dependent calls)

src/umpire/resource/HostSharedMemoryResourceImpl.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,6 @@ class HostSharedMemoryResource::impl {
320320
offset_to_pointer(m_segment->free_blocks_off, block_ptr);
321321

322322
while (block_ptr != nullptr) {
323-
#if defined(__linux__)
324323
char* const block_begin = reinterpret_cast<char*>(block_ptr);
325324
char* const block_end = block_begin + block_ptr->block_size;
326325

@@ -344,7 +343,6 @@ class HostSharedMemoryResource::impl {
344343
<< m_segment_name << ": " << strerror(madvise_err));
345344
}
346345
}
347-
#endif
348346

349347
offset_to_pointer(block_ptr->next_block_off, block_ptr);
350348
}

tests/unit/resource/ipc_shared_memory_resource_tests.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,6 @@ TEST_F(SharedMemoryTest, UnitTests)
232232
allocator.deallocate(ptr);
233233
}
234234
MPI_Barrier(MPI_COMM_WORLD);
235-
236-
MPI_Barrier(MPI_COMM_WORLD);
237235
}
238236
}
239237
} // namespace

tests/unit/umpire_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
TEST(Umpire, ProcessorMemoryStatistics)
1414
{
1515
ASSERT_GE(umpire::get_process_memory_usage(), 0);
16+
ASSERT_GE(umpire::get_mapping_memory_usage(""), 0);
1617
ASSERT_GE(umpire::get_device_memory_usage(0), 0);
1718
}
1819

0 commit comments

Comments
 (0)