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// clang-format off
21#include <windows.h>
22#include <psapi.h>
23// clang-format on
24#endif
25
26//---------------------------------------------------------------------
27// peak_rss_bytes()
28//---------------------------------------------------------------------
29// Returns the *peak* RSS in **bytes** for the current process,
30// or 0 on failure / unsupported platform.
31//---------------------------------------------------------------------
33{
34#if defined(_WIN32)
35 PROCESS_MEMORY_COUNTERS pmc{};
36 if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
37 return static_cast<std::size_t>(pmc.PeakWorkingSetSize);
38
39#elif defined(__APPLE__) || defined(__FreeBSD__)
40 struct rusage usage{};
41 if (getrusage(RUSAGE_SELF, &usage) == 0)
42 // ru_maxrss is already bytes on macOS / BSD
43 return static_cast<std::size_t>(usage.ru_maxrss);
44
45#elif defined(__linux__)
46 struct rusage usage{};
47 if (getrusage(RUSAGE_SELF, &usage) == 0)
48 // ru_maxrss is kilobytes on Linux → convert to bytes
49 return static_cast<std::size_t>(usage.ru_maxrss) * 1024ULL;
50#endif
51
52 return 0; // fallback on error / unknown OS
53}
54
55//---------------------------------------------------------------------
56// C-linkage wrapper: log_with_mem_usage()
57//---------------------------------------------------------------------
58// Prints "<msg> (mem: <value> MiB)" with two-digit precision.
59//
60// • Safe to call from C, C++, or dlopen’d plugins.
61// • Thread-safe w.r.t. internal state; output lines may still
62// interleave if multiple threads call concurrently (as with any
63// stderr logging).
64//---------------------------------------------------------------------
65// WASM_EXPORT ensures this symbol stays visible when compiling with -fvisibility=hidden.
67WASM_EXPORT void logstr(char const* msg)
68{
69#ifndef NO_MULTITHREADING
70 static std::mutex log_mutex;
71 std::lock_guard<std::mutex> lock(log_mutex);
72#endif
73
74 static bool disable_mem_usage = std::getenv("BB_DISABLE_MEM_USAGE") != nullptr;
75 if (disable_mem_usage) {
76 std::cerr << msg << '\n';
77 return;
78 }
79
80 const std::size_t bytes = peak_rss_bytes();
81 std::cerr << msg;
82
83 if (bytes != 0) {
84 const double mib = static_cast<double>(bytes) / (1024.0 * 1024.0);
85 std::cerr << " (mem: " << std::fixed << std::setprecision(2) << mib << " MiB)";
86 } else {
87 std::cerr << " (mem: N/A)";
88 }
89 std::cerr << '\n';
90}
WASM_EXPORT void logstr(char const *msg)
Definition logstr.cpp:67
std::size_t peak_rss_bytes()
Definition logstr.cpp:32
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
#define WASM_EXPORT