Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
backing_memory.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Planned, auditors: [], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
9#include <atomic>
10#include <cctype>
11#include <cstdlib>
12#include <limits>
13#include <string>
14
15// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
17 std::getenv("BB_SLOW_LOW_MEMORY") == nullptr ? false : std::string(std::getenv("BB_SLOW_LOW_MEMORY")) == "1";
18
19// Storage budget is disabled for WASM builds unless ENABLE_WASM_BENCH is defined
20#if !defined(__wasm__) || defined(ENABLE_WASM_BENCH)
21
22#if defined(__wasm__) && defined(BB_NO_EXCEPTIONS)
23// Simplified version for WASM builds without exception support
24// For WASM benchmarking, we don't enforce storage budgets
25size_t parse_size_string(const std::string& /* size_str */)
26{
27 // Return unlimited budget for WASM builds
28 return std::numeric_limits<size_t>::max();
29}
30#else
31// Full version with exception handling for native builds
32// Parse storage size string (e.g., "500m", "2g", "1024k")
33size_t parse_size_string(const std::string& size_str)
34{
35 if (size_str.empty()) {
36 return std::numeric_limits<size_t>::max();
37 }
38
39 try {
40 std::string str = size_str;
41
42 // Convert to lowercase for case-insensitive comparison
43 char suffix = static_cast<char>(std::tolower(static_cast<unsigned char>(str.back())));
44 size_t multiplier = 1;
45
46 // Check for unit suffix
47 if (suffix == 'k') {
48 multiplier = 1024ULL;
49 str.pop_back();
50 } else if (suffix == 'm') {
51 multiplier = 1024ULL * 1024ULL;
52 str.pop_back();
53 } else if (suffix == 'g') {
54 multiplier = 1024ULL * 1024ULL * 1024ULL;
55 str.pop_back();
56 } else if (std::isdigit(static_cast<unsigned char>(suffix)) == 0) {
57 // Invalid suffix
58 throw_or_abort("Invalid storage size format: '" + size_str + "'. Use format like '500m', '2g', or '1024k'");
59 }
60
61 // Check if remaining string is a valid number
62 if (str.empty()) {
63 throw_or_abort("Invalid storage size format: '" + size_str + "'. No numeric value provided");
64 }
65
66 size_t value = std::stoull(str);
67 return value * multiplier;
68 } catch (const std::invalid_argument&) {
69 throw_or_abort("Invalid storage size format: '" + size_str + "'. Not a valid number");
70 } catch (const std::out_of_range&) {
71 throw_or_abort("Invalid storage size format: '" + size_str + "'. Value out of range");
72 }
73}
74#endif
75
76namespace {
77// Parse storage budget from environment variable (supports k/m/g suffixes like Docker)
78size_t parse_storage_budget()
79{
80 const char* env_val = std::getenv("BB_STORAGE_BUDGET");
81 if (env_val == nullptr) {
82 return std::numeric_limits<size_t>::max(); // No limit by default
83 }
84
85 return parse_size_string(std::string(env_val));
86}
87} // namespace
88
89// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
90size_t storage_budget = parse_storage_budget();
91
92// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
93std::atomic<size_t> current_storage_usage{ 0 };
94
95#endif // !defined(__wasm__) || defined(ENABLE_WASM_BENCH)
size_t parse_size_string(const std::string &size_str)
std::atomic< size_t > current_storage_usage
bool slow_low_memory
size_t storage_budget
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
void throw_or_abort(std::string const &err)