10#include <kernel/util/string.hpp>
12#include <kernel/util/os_windows.hpp>
13#include <kernel/util/dist.hpp>
18#include <sys/resource.h>
45 std::size_t _current_physical;
46 std::size_t _peak_physical;
47 std::size_t _current_virtual;
48 std::size_t _peak_virtual;
49 std::size_t _current_swap;
67 std::ifstream status_file(
"/proc/self/status");
68 if (!status_file.is_open())
69 XABORTM(
"could not open /proc/self/status!");
97 while (std::getline(status_file, line))
102 XASSERTM(v.back() ==
"kB",
"get_memory_usage: unit mismatch!");
103 v.at(v.size() - 2).parse(_current_physical);
104 _current_physical *= 1024;
111 XASSERTM(v.back() ==
"kB",
"get_memory_usage: unit mismatch!");
112 v.at(v.size() - 2).parse(_peak_physical);
113 _peak_physical *= 1024;
120 XASSERTM(v.back() ==
"kB",
"get_memory_usage: unit mismatch!");
121 v.at(v.size() - 2).parse(_current_virtual);
122 _current_virtual *= 1024;
129 XASSERTM(v.back() ==
"kB",
"get_memory_usage: unit mismatch!");
130 v.at(v.size() - 2).parse(_peak_virtual);
131 _peak_virtual *= 1024;
138 XASSERTM(v.back() ==
"kB",
"get_memory_usage: unit mismatch!");
139 v.at(v.size() - 2).parse(_current_swap);
140 _current_swap *= 1024;
148 unsigned long long work_set_size(0ull), work_set_size_peak(0ull), page_file_usage(0ull), page_file_usage_peak(0ull);
152 _current_physical = std::size_t(work_set_size);
153 _current_virtual = std::size_t(page_file_usage);
154 _peak_physical = std::size_t(work_set_size_peak);
155 _peak_virtual = std::size_t(page_file_usage_peak);
156 _current_swap = std::size_t(0);
158#elif defined(__unix__)
162 struct rusage r_usage;
163 if (0 != getrusage(RUSAGE_SELF, &r_usage))
164 XABORTM(
"Error in getrusage call!");
165 _peak_physical = (std::size_t)r_usage.ru_maxrss;
166 _peak_virtual = (std::size_t)r_usage.ru_maxrss;
167 _current_physical = (std::size_t)r_usage.ru_maxrss;
168 _current_virtual = (std::size_t)r_usage.ru_maxrss;
169 _current_swap = std::size_t(0);
171 _peak_physical *= 1024u;
172 _peak_virtual *= 1024u;
173 _current_physical *= 1024u;
174 _current_virtual *= 1024u;
181 return _current_physical;
187 return _peak_physical;
193 return _current_virtual;
199 return _peak_virtual;
205 return _current_swap;
230 std::uint64_t min_p, max_p, sum_p;
#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.