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
10057int 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 {
0 commit comments