Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
logstr.cpp
Go to the documentation of this file.
1// logstr()
2// --------------------
3// Logs a message to stderr and appends the *peak* resident-set size (RSS)
4// of the current process in MiB.
5//
6// Windows note: link with Psapi.lib
7
8#include <cstddef>
9#include <iomanip>
10#include <iostream>
11#ifndef NO_MULTITHREADING
12#include <mutex>
13#endif
14
15#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
16#include <sys/resource.h>
17#elif defined(_WIN32)
18#define NOMINMAX
19#define PSAPI_VERSION 1
20#include <psapi.h>
21#include <windows.h>
22#endif
23
24namespace {
25//---------------------------------------------------------------------
26// peak_rss_bytes()
27//---------------------------------------------------------------------
28// Returns the *peak* RSS in **bytes** for the current process,
29// or 0 on failure / unsupported platform.
30//---------------------------------------------------------------------
31std::size_t peak_rss_bytes()
32{
33#if defined(_WIN32)
34 PROCESS_MEMORY_COUNTERS pmc{};
35 if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
36 return static_cast<std::size_t>(pmc.PeakWorkingSetSize);
37
38#elif defined(__APPLE__) || defined(__FreeBSD__)
39 struct rusage usage{};
40 if (getrusage(RUSAGE_SELF, &usage) == 0)
41 // ru_maxrss is already bytes on macOS / BSD
42 return static_cast<std::size_t>(usage.ru_maxrss);
43
44#elif defined(__linux__)
45 struct rusage usage{};
46 if (getrusage(RUSAGE_SELF, &usage) == 0)
47 // ru_maxrss is kilobytes on Linux → convert to bytes
48 return static_cast<std::size_t>(usage.ru_maxrss) * 1024ULL;
49#endif
50
51 return 0; // fallback on error / unknown OS
52}
53
54} // namespace
55
56//---------------------------------------------------------------------
57// C-linkage wrapper: log_with_mem_usage()
58//---------------------------------------------------------------------
59// Prints "<msg> (mem: <value> MiB)" with two-digit precision.
60//
61// • Safe to call from C, C++, or dlopen’d plugins.
62// • Thread-safe w.r.t. internal state; output lines may still
63// interleave if multiple threads call concurrently (as with any
64// stderr logging).
65//---------------------------------------------------------------------
66extern "C" void logstr(char const* msg)
67{
68#ifndef NO_MULTITHREADING
69 static std::mutex log_mutex;
70 std::lock_guard<std::mutex> lock(log_mutex);
71#endif
72
73 static bool disable_mem_usage = std::getenv("BB_DISABLE_MEM_USAGE") != nullptr;
74 if (disable_mem_usage) {
75 std::cerr << msg << '\n';
76 return;
77 }
78
79 const std::size_t bytes = peak_rss_bytes();
80 std::cerr << msg;
81
82 if (bytes != 0) {
83 const double mib = static_cast<double>(bytes) / (1024.0 * 1024.0);
84 std::cerr << " (mem: " << std::fixed << std::setprecision(2) << mib << " MiB)";
85 } else {
86 std::cerr << " (mem: N/A)";
87 }
88 std::cerr << '\n';
89}
void logstr(char const *msg)
Definition logstr.cpp:66
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13