Skip to content

Commit a178da1

Browse files
feat: add storage budget control for FileBackedMemory
Adds ability to limit FileBackedMemory usage via --storage_budget_gb flag or BB_STORAGE_BUDGET_GB env var. When budget is exceeded, falls back to RAM allocation.
1 parent 2f3f861 commit a178da1

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +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)
2627
bool update_inputs{ false }; // update inputs when check fails
2728

2829
friend std::ostream& operator<<(std::ostream& os, const Flags& flags)
@@ -40,6 +41,7 @@ class API {
4041
<< " write_vk " << flags.write_vk << "\n"
4142
<< " include_gates_per_opcode " << flags.include_gates_per_opcode << "\n"
4243
<< " slow_low_memory " << flags.slow_low_memory << "\n"
44+
<< " storage_budget_gb " << flags.storage_budget_gb << "\n"
4345
<< "]" << std::endl;
4446
return os;
4547
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,13 @@ int parse_and_run_cli_command(int argc, char* argv[])
278278
"--slow_low_memory", flags.slow_low_memory, "Enable low memory mode (can be 2x slower or more).");
279279
};
280280

281+
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).");
286+
};
287+
281288
const auto add_update_inputs_flag = [&](CLI::App* subcommand) {
282289
return subcommand->add_flag("--update_inputs", flags.update_inputs, "Update inputs if vk check fails.");
283290
};
@@ -351,6 +358,7 @@ int parse_and_run_cli_command(int argc, char* argv[])
351358
add_ipa_accumulation_flag(prove);
352359
remove_zk_option(prove);
353360
add_slow_low_memory_flag(prove);
361+
add_storage_budget_option(prove);
354362
add_print_op_counts_flag(prove);
355363
add_op_counts_out_option(prove);
356364

@@ -558,6 +566,9 @@ int parse_and_run_cli_command(int argc, char* argv[])
558566
debug_logging = flags.debug;
559567
verbose_logging = debug_logging || flags.verbose;
560568
slow_low_memory = flags.slow_low_memory;
569+
if (flags.storage_budget_gb > 0) {
570+
storage_budget = flags.storage_budget_gb * 1024ULL * 1024ULL * 1024ULL;
571+
}
561572
#ifndef __wasm__
562573
if (print_op_counts || !op_counts_out.empty()) {
563574
bb::detail::use_op_count_time = true;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
#include "barretenberg/polynomials/backing_memory.hpp"
2+
#include <atomic>
3+
#include <cstdlib>
4+
#include <limits>
25

36
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
47
bool slow_low_memory =
58
std::getenv("BB_SLOW_LOW_MEMORY") == nullptr ? false : std::string(std::getenv("BB_SLOW_LOW_MEMORY")) == "1";
9+
10+
// Parse storage budget from environment variable (in GB)
11+
static size_t parse_storage_budget()
12+
{
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+
}
17+
18+
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
23+
}
24+
}
25+
26+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
27+
size_t storage_budget = parse_storage_budget();
28+
29+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
30+
std::atomic<size_t> current_storage_usage{ 0 };

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
2222
extern bool slow_low_memory;
2323

24+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
25+
extern size_t storage_budget;
26+
27+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
28+
extern std::atomic<size_t> current_storage_usage;
29+
2430
template <typename T> class AlignedMemory;
2531

2632
#ifndef __wasm__
@@ -43,7 +49,22 @@ template <typename Fr> class BackingMemory {
4349
{
4450
#ifndef __wasm__
4551
if (slow_low_memory) {
46-
return std::shared_ptr<BackingMemory<Fr>>(new FileBackedMemory<Fr>(size));
52+
size_t required_bytes = size * sizeof(Fr);
53+
size_t current_usage = current_storage_usage.load();
54+
55+
// Check if we're under the storage budget
56+
if (current_usage + required_bytes <= storage_budget) {
57+
// Try to use FileBackedMemory
58+
try {
59+
auto result = std::shared_ptr<BackingMemory<Fr>>(new FileBackedMemory<Fr>(size));
60+
current_storage_usage.fetch_add(required_bytes);
61+
return result;
62+
} catch (const std::exception& e) {
63+
// FileBackedMemory allocation failed, fall back to AlignedMemory
64+
return std::shared_ptr<BackingMemory<Fr>>(new AlignedMemory<Fr>(size));
65+
}
66+
}
67+
// Over budget, use AlignedMemory
4768
}
4869
#endif
4970
return std::shared_ptr<BackingMemory<Fr>>(new AlignedMemory<Fr>(size));
@@ -87,6 +108,8 @@ template <typename T> class FileBackedMemory : public BackingMemory<T> {
87108
}
88109
if (memory != nullptr && file_size > 0) {
89110
munmap(memory, file_size);
111+
// Decrement storage usage when FileBackedMemory is freed
112+
current_storage_usage.fetch_sub(file_size);
90113
}
91114
if (fd >= 0) {
92115
close(fd);

0 commit comments

Comments
 (0)