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