Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
sysinfo.cpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
16
18
19// Mac OSX implementation:
20#if defined(__MACH__)
21 #include <mach/mach.h>
22 #include <sys/sysctl.h>
23 #include <sys/types.h>
24
25std::optional<std::size_t> sham::getHostPhysicalMemory() {
26
27 int mib[] = {CTL_HW, HW_MEMSIZE};
28 int64_t value = 0;
29 size_t length = sizeof(value);
30
31 if (-1 == sysctl(mib, 2, &value, &length, NULL, 0)) {
32 return std::nullopt;
33 }
34 return value;
35}
36
37std::optional<std::size_t> sham::getHostAvailableMemory() {
38
39 vm_size_t page_size = 0;
40 if (KERN_SUCCESS != host_page_size(mach_host_self(), &page_size)) {
41 return std::nullopt;
42 }
43
44 vm_statistics64_data_t vm_stats;
45 mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
46 if (KERN_SUCCESS
47 != host_statistics64(
48 mach_host_self(), HOST_VM_INFO64, reinterpret_cast<host_info64_t>(&vm_stats), &count)) {
49 return std::nullopt;
50 }
51
52 // Free memory plus reclaimable (inactive) pages, as reported as "available" by most tools.
53 std::size_t available_pages = static_cast<std::size_t>(vm_stats.free_count)
54 + static_cast<std::size_t>(vm_stats.inactive_count);
55
56 return available_pages * static_cast<std::size_t>(page_size);
57}
58
59// Linux/BSD implementation:
60#elif (defined(linux) || defined(__linux__) || defined(__linux)) \
61 || (defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) \
62 || defined(__OpenBSD__))
63
64 #include <sys/sysinfo.h>
65 #include <fstream>
66 #include <string>
67
68std::optional<std::size_t> sham::getHostPhysicalMemory() {
69 struct sysinfo info;
70 sysinfo(&info);
71 return info.totalram;
72}
73
74std::optional<std::size_t> sham::getHostAvailableMemory() {
75 // Prefer /proc/meminfo's MemAvailable, which accounts for reclaimable caches/buffers
76 // (unlike sysinfo's freeram field).
77 std::ifstream meminfo("/proc/meminfo");
78 if (meminfo) {
79 std::string label;
80 std::size_t value_kb;
81 std::string unit;
82 while (meminfo >> label >> value_kb >> unit) {
83 if (label == "MemAvailable:") {
84 return value_kb * 1024;
85 }
86 }
87 }
88
89 struct sysinfo info;
90 if (-1 == sysinfo(&info)) {
91 return std::nullopt;
92 }
93 return static_cast<std::size_t>(info.freeram) * static_cast<std::size_t>(info.mem_unit);
94}
95
96#else
97
98std::optional<std::size_t> sham::getHostPhysicalMemory() { return std::nullopt; }
99std::optional<std::size_t> sham::getHostAvailableMemory() { return std::nullopt; }
100
101#endif
std::optional< std::size_t > getHostPhysicalMemory()
Get the amount of physical memory (RAM) available on the system, in bytes.
Definition sysinfo.cpp:98
std::optional< std::size_t > getHostAvailableMemory()
Get the amount of physical memory (RAM) currently available on the system, in bytes.
Definition sysinfo.cpp:99
void info(std::string module_name, Types... var2)
Prints a log message with multiple arguments.
Definition logs.hpp:132