Skip to content

Commit de8427a

Browse files
feat: improve storage budget with Docker-style size formats
- Changed BB_STORAGE_BUDGET_GB env var to BB_STORAGE_BUDGET - Changed --storage_budget_gb CLI flag to --storage_budget - Now accepts Docker-style size formats: '500m', '2g', '1024k' - Added proper error handling with clear messages for invalid formats - Created reusable parse_size_string() function - Updated docker_isolate to accept TMPFS_SIZE parameter
1 parent a178da1 commit de8427a

File tree

5 files changed

+67
-19
lines changed

5 files changed

+67
-19
lines changed

barretenberg/cpp/src/barretenberg/api/api.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class API {
2323
bool write_vk{ false }; // should we addditionally write the verification key when writing the proof
2424
bool include_gates_per_opcode{ false }; // should we include gates_per_opcode in the gates command output
2525
bool slow_low_memory{ false }; // use file backed memory for polynomials
26-
size_t storage_budget_gb{ 0 }; // storage budget in GB for file backed memory (0 = no limit)
26+
std::string storage_budget; // storage budget for file backed memory (e.g. "500m", "2g")
2727
bool update_inputs{ false }; // update inputs when check fails
2828

2929
friend std::ostream& operator<<(std::ostream& os, const Flags& flags)
@@ -41,7 +41,7 @@ class API {
4141
<< " write_vk " << flags.write_vk << "\n"
4242
<< " include_gates_per_opcode " << flags.include_gates_per_opcode << "\n"
4343
<< " slow_low_memory " << flags.slow_low_memory << "\n"
44-
<< " storage_budget_gb " << flags.storage_budget_gb << "\n"
44+
<< " storage_budget " << flags.storage_budget << "\n"
4545
<< "]" << std::endl;
4646
return os;
4747
}

barretenberg/cpp/src/barretenberg/bb/cli.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,10 @@ int parse_and_run_cli_command(int argc, char* argv[])
279279
};
280280

281281
const auto add_storage_budget_option = [&](CLI::App* subcommand) {
282-
return subcommand->add_option("--storage_budget_gb",
283-
flags.storage_budget_gb,
284-
"Storage budget in GB for FileBackedMemory. When exceeded, falls back to RAM "
285-
"(requires --slow_low_memory).");
282+
return subcommand->add_option("--storage_budget",
283+
flags.storage_budget,
284+
"Storage budget for FileBackedMemory (e.g. '500m', '2g'). When exceeded, falls "
285+
"back to RAM (requires --slow_low_memory).");
286286
};
287287

288288
const auto add_update_inputs_flag = [&](CLI::App* subcommand) {
@@ -566,8 +566,8 @@ int parse_and_run_cli_command(int argc, char* argv[])
566566
debug_logging = flags.debug;
567567
verbose_logging = debug_logging || flags.verbose;
568568
slow_low_memory = flags.slow_low_memory;
569-
if (flags.storage_budget_gb > 0) {
570-
storage_budget = flags.storage_budget_gb * 1024ULL * 1024ULL * 1024ULL;
569+
if (!flags.storage_budget.empty()) {
570+
storage_budget = parse_size_string(flags.storage_budget);
571571
}
572572
#ifndef __wasm__
573573
if (print_op_counts || !op_counts_out.empty()) {
Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,73 @@
11
#include "barretenberg/polynomials/backing_memory.hpp"
2+
#include "barretenberg/common/throw_or_abort.hpp"
23
#include <atomic>
4+
#include <cctype>
35
#include <cstdlib>
46
#include <limits>
7+
#include <string>
58

69
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
710
bool slow_low_memory =
811
std::getenv("BB_SLOW_LOW_MEMORY") == nullptr ? false : std::string(std::getenv("BB_SLOW_LOW_MEMORY")) == "1";
912

10-
// Parse storage budget from environment variable (in GB)
11-
static size_t parse_storage_budget()
13+
// Parse storage size string (e.g., "500m", "2g", "1024k")
14+
size_t parse_size_string(const std::string& size_str)
1215
{
13-
const char* env_val = std::getenv("BB_STORAGE_BUDGET_GB");
14-
if (env_val == nullptr) {
15-
return std::numeric_limits<size_t>::max(); // No limit by default
16+
if (size_str.empty()) {
17+
return std::numeric_limits<size_t>::max();
1618
}
1719

1820
try {
19-
size_t gb = std::stoull(env_val);
20-
return gb * 1024ULL * 1024ULL * 1024ULL; // Convert GB to bytes
21-
} catch (...) {
22-
return std::numeric_limits<size_t>::max(); // Invalid value, no limit
21+
std::string str = size_str;
22+
23+
// Convert to lowercase for case-insensitive comparison
24+
char suffix = static_cast<char>(std::tolower(static_cast<unsigned char>(str.back())));
25+
size_t multiplier = 1;
26+
27+
// Check for unit suffix
28+
if (suffix == 'k') {
29+
multiplier = 1024ULL;
30+
str.pop_back();
31+
} else if (suffix == 'm') {
32+
multiplier = 1024ULL * 1024ULL;
33+
str.pop_back();
34+
} else if (suffix == 'g') {
35+
multiplier = 1024ULL * 1024ULL * 1024ULL;
36+
str.pop_back();
37+
} else if (std::isdigit(static_cast<unsigned char>(suffix)) == 0) {
38+
// Invalid suffix
39+
throw_or_abort("Invalid storage size format: '" + size_str + "'. Use format like '500m', '2g', or '1024k'");
40+
}
41+
42+
// Check if remaining string is a valid number
43+
if (str.empty()) {
44+
throw_or_abort("Invalid storage size format: '" + size_str + "'. No numeric value provided");
45+
}
46+
47+
size_t value = std::stoull(str);
48+
return value * multiplier;
49+
} catch (const std::invalid_argument&) {
50+
throw_or_abort("Invalid storage size format: '" + size_str + "'. Not a valid number");
51+
} catch (const std::out_of_range&) {
52+
throw_or_abort("Invalid storage size format: '" + size_str + "'. Value out of range");
2353
}
2454
}
2555

56+
namespace {
57+
// Parse storage budget from environment variable (supports k/m/g suffixes like Docker)
58+
size_t parse_storage_budget()
59+
{
60+
const char* env_val = std::getenv("BB_STORAGE_BUDGET");
61+
if (env_val == nullptr) {
62+
return std::numeric_limits<size_t>::max(); // No limit by default
63+
}
64+
65+
return parse_size_string(std::string(env_val));
66+
}
67+
} // namespace
68+
2669
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
2770
size_t storage_budget = parse_storage_budget();
2871

2972
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
30-
std::atomic<size_t> current_storage_usage{ 0 };
73+
std::atomic<size_t> current_storage_usage{ 0 };

barretenberg/cpp/src/barretenberg/polynomials/backing_memory.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ extern size_t storage_budget;
2727
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
2828
extern std::atomic<size_t> current_storage_usage;
2929

30+
// Parse storage size string (e.g., "500m", "2g", "1024k")
31+
size_t parse_size_string(const std::string& size_str);
32+
3033
template <typename T> class AlignedMemory;
3134

3235
#ifndef __wasm__

ci3/docker_isolate

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ NO_CD=1 source $(git rev-parse --show-toplevel)/ci3/source
66
cmd=$1
77
export CPUS=${CPUS:-2}
88
export MEM=${MEM:-$((CPUS * 4))g}
9+
export TMPFS_SIZE=${TMPFS_SIZE:-1g}
910

1011
function cleanup {
1112
if [ -n "${cid:-}" ]; then
@@ -54,14 +55,15 @@ cid=$(docker run -d \
5455
--pid=host \
5556
--user $(id -u):$(id -g) \
5657
-v$HOME:$HOME \
57-
--mount type=tmpfs,target=/tmp,tmpfs-size=1g \
58+
--mount type=tmpfs,target=/tmp,tmpfs-size=$TMPFS_SIZE \
5859
--workdir $PWD \
5960
-e HOME \
6061
-e VERBOSE \
6162
-e GIT_CONFIG_GLOBAL=$root/build-images/src/home/.gitconfig \
6263
-e FORCE_COLOR=true \
6364
-e CPUS \
6465
-e MEM \
66+
-e TMPFS_SIZE \
6567
"${arg_env_vars[@]}" \
6668
aztecprotocol/build:3.0 \
6769
/bin/bash -c "$cmd")

0 commit comments

Comments
 (0)