10#include <kernel/util/string.hpp>
12#include <kernel/util/os_windows.hpp>
13#include <kernel/util/dist.hpp>
18#include <sys/resource.h>
46 enum memory_type : std::uint8_t
56 static const char* format(memory_type type)
60 return "Peak Physical";
62 return "Current Physical";
64 return "Peak Virtual";
66 return "Current Virtual";
68 return "Current Swap";
75 std::array<std::size_t, memory_type::size> _memory;
89 std::ifstream status_file(
"/proc/self/status");
90 if (!status_file.is_open())
91 XABORTM(
"could not open /proc/self/status!");
119 while (std::getline(status_file, line))
123 std::size_t& _current_physical = _memory[memory_type::cur_physical];
125 XASSERTM(v.back() ==
"kB",
"get_memory_usage: unit mismatch!");
126 v.at(v.size() - 2).parse(_current_physical);
127 _current_physical *= 1024;
133 std::size_t& _peak_physical = _memory[memory_type::peak_physical];
135 XASSERTM(v.back() ==
"kB",
"get_memory_usage: unit mismatch!");
136 v.at(v.size() - 2).parse(_peak_physical);
137 _peak_physical *= 1024;
143 std::size_t& _current_virtual = _memory[memory_type::cur_virtual];
145 XASSERTM(v.back() ==
"kB",
"get_memory_usage: unit mismatch!");
146 v.at(v.size() - 2).parse(_current_virtual);
147 _current_virtual *= 1024;
153 std::size_t& _peak_virtual = _memory[memory_type::peak_virtual];
155 XASSERTM(v.back() ==
"kB",
"get_memory_usage: unit mismatch!");
156 v.at(v.size() - 2).parse(_peak_virtual);
157 _peak_virtual *= 1024;
163 std::size_t& _current_swap = _memory[memory_type::cur_swap];
165 XASSERTM(v.back() ==
"kB",
"get_memory_usage: unit mismatch!");
166 v.at(v.size() - 2).parse(_current_swap);
167 _current_swap *= 1024;
175 unsigned long long work_set_size(0ull), work_set_size_peak(0ull), page_file_usage(0ull), page_file_usage_peak(0ull);
179 _memory[memory_type::cur_physical] = std::size_t(work_set_size);
180 _memory[memory_type::cur_virtual] = std::size_t(page_file_usage);
181 _memory[memory_type::peak_physical] = std::size_t(work_set_size_peak);
182 _memory[memory_type::peak_virtual] = std::size_t(page_file_usage_peak);
183 _memory[memory_type::cur_swap] = std::size_t(0);
185#elif defined(__unix__)
189 struct rusage r_usage;
190 if (0 != getrusage(RUSAGE_SELF, &r_usage))
191 XABORTM(
"Error in getrusage call!");
193 _memory[memory_type::cur_physical] = std::size_t(r_usage.ru_maxrss) * 1024u;
194 _memory[memory_type::cur_virtual] = std::size_t(r_usage.ru_maxrss) * 1024u;
195 _memory[memory_type::peak_physical] = std::size_t(r_usage.ru_maxrss) * 1024u;
196 _memory[memory_type::peak_virtual] = std::size_t(r_usage.ru_maxrss) * 1024u;
197 _memory[memory_type::cur_swap] = std::size_t(0);
204 return _memory[memory_type::cur_physical];
210 return _memory[memory_type::peak_physical];
216 return _memory[memory_type::cur_virtual];
222 return _memory[memory_type::peak_virtual];
228 return _memory[memory_type::cur_swap];
235 for(std::size_t k = 0u; k < memory_type::size; ++k)
237 memory_type type = memory_type(k);
238 r += (
String(format(type)) +
String(
":")).pad_back(20) +
stringify(_memory[type] / 1024 / 1024) +
" MByte\n";
246 std::uint64_t min_p, max_p, sum_p;
247 min_p = max_p = sum_p = _memory[mem_type];
#define XABORTM(msg)
Abortion macro definition with custom message.
#define XASSERTM(expr, msg)
Assertion macro definition with custom message.
void allreduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking All-Reduce.
Memory usage info object.
std::size_t get_peak_virtual() const
Returns peak virtual memory.
std::size_t get_current_swap() const
Returns current (last stamp call) swap memory.
std::size_t get_current_physical() const
Returns current (last stamp call) physical memory.
std::size_t get_peak_physical() const
Returns peak physical memory.
void stamp()
update the memory usage statistics with current data
static String format_peak_physical_usage(const Dist::Comm &comm)
Returns the formatted peak physical memory usage over an entire communicator.
std::size_t get_current_virtual() const
Returns current (last stamp call) virtual memory.
String get_formatted_memory_usage() const
Retrieve formatted memory usage string.
String class implementation.
std::deque< String > split_by_whitespaces() const
Splits the string by white-spaces.
String pad_back(size_type len, char c=' ') const
Pads the back of the string up to a desired length.
bool starts_with(const String &head) const
Checks whether this string starts with another string.
const Operation op_min(MPI_MIN)
Operation wrapper for MPI_MIN.
const Operation op_max(MPI_MAX)
Operation wrapper for MPI_MAX.
const Operation op_sum(MPI_SUM)
Operation wrapper for MPI_SUM.
void query_memory_usage(unsigned long long &work_set_size, unsigned long long &work_set_size_peak, unsigned long long &page_file_usage, unsigned long long &page_file_usage_peak)
Queries memory usage information.
String stringify(const T_ &item)
Converts an item into a String.
String stringify_bytes(std::uint64_t bytes, int precision=3, int width=7)
Prints a byte size to a string using the common units Bytes, KiB, MiB, Gib, TiB or PiB.