Skip to content

Commit 137b4f8

Browse files
committed
add mem info utils
1 parent da526c4 commit 137b4f8

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

cpp/include/rapidsmpf/system_info.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66

77
namespace rapidsmpf {
88

9+
/**
10+
* @brief Get the total amount of host (main) memory.
11+
*
12+
* @return Total host memory in bytes.
13+
*
14+
* @note On WSL and in containerized environments, the returned value
15+
* reflects the memory visible to the Linux kernel instance, which may
16+
* differ from the host system's physical memory.
17+
*/
18+
std::uint64_t get_total_host_memory() noexcept;
19+
920
/**
1021
* @brief Get the NUMA node ID associated with the calling CPU thread.
1122
*
@@ -27,4 +38,21 @@ namespace rapidsmpf {
2738
*/
2839
int get_current_numa_node_id() noexcept;
2940

41+
/**
42+
* @brief Get the total amount of host memory for a NUMA node.
43+
*
44+
* @param numa_id
45+
* NUMA node for which to query the total host memory. Defaults to the
46+
* current NUMA node as returned by `get_current_numa_node_id()`.
47+
*
48+
* @note If NUMA support is not available or the node size cannot be
49+
* determined, this function falls back to returning the total host memory.
50+
*
51+
* @return Total host memory of the NUMA node in bytes.
52+
*/
53+
std::uint64_t get_numa_node_host_memory(
54+
int numa_id = get_current_numa_node_id()
55+
) noexcept;
56+
57+
3058
} // namespace rapidsmpf

cpp/src/system_info.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,44 @@
1212

1313
namespace rapidsmpf {
1414

15+
std::uint64_t get_total_host_memory() noexcept {
16+
static const uint64_t ret = [] {
17+
auto const page_size = ::sysconf(_SC_PAGE_SIZE);
18+
auto const phys_pages = ::sysconf(_SC_PHYS_PAGES);
19+
20+
// Because `get_total_host_memory()` is marked `noexcept`, any failure
21+
// here results in process termination, which is intentional.
22+
RAPIDSMPF_EXPECTS(
23+
page_size > 0 && phys_pages > 0,
24+
"sysconf(_SC_PAGE_SIZE/_SC_PHYS_PAGES) failed",
25+
std::runtime_error
26+
);
27+
return static_cast<std::uint64_t>(page_size)
28+
* static_cast<std::uint64_t>(phys_pages);
29+
}();
30+
return ret;
31+
}
32+
1533
int get_current_numa_node_id() noexcept {
16-
static const int numa_node_id = [] {
34+
static const int ret = [] {
1735
if (!numa_available()) {
1836
return 0;
1937
}
2038
return numa_node_of_cpu(sched_getcpu());
2139
}();
22-
return numa_node_id;
40+
return ret;
41+
}
42+
43+
std::uint64_t get_numa_node_host_memory(int numa_id) noexcept {
44+
if (numa_available() < 0) {
45+
return get_total_host_memory();
46+
}
47+
long long free_ll = 0; // ignored.
48+
long long total_ll = numa_node_size64(numa_id, &free_ll);
49+
if (total_ll < 0) {
50+
return get_total_host_memory();
51+
}
52+
return static_cast<std::uint64_t>(total_ll);
2353
}
2454

2555
} // namespace rapidsmpf

0 commit comments

Comments
 (0)