Skip to content

Commit 69a695e

Browse files
committed
feat: comptime configurable thread count
We can set threadcount with -Dthread-count=<NUMBER>, or leave it up to the default of @max(std.Thread.getCpuCount() - 1, 1).
1 parent e35b93c commit 69a695e

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

bindings/napi/root.zig

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const napi = @import("zapi:zapi");
33
const pool = @import("./pool.zig");
44
const pubkeys = @import("./pubkeys.zig");
55
const config = @import("./config.zig");
6+
const options = @import("bls_options");
67
const shuffle = @import("./shuffle.zig");
78
const metrics = @import("./metrics.zig");
89
const BeaconStateView = @import("./BeaconStateView.zig");
@@ -36,7 +37,13 @@ var env_cleanup: EnvCleanup = .{};
3637
fn register(env: napi.Env, exports: napi.Value) !void {
3738
if (env_refcount.fetchAdd(1, .monotonic) == 0) {
3839
// First environment — initialize shared state.
39-
const cpu_count = std.Thread.getCpuCount() catch 1;
40+
// in your threadpool init
41+
var cpu_count: usize = options.thread_count;
42+
if (options.thread_count == 0) {
43+
std.debug.print("Note: no -Dthread-count set, will use runtime CPU count minus 1: {}\n", .{cpu_count});
44+
cpu_count = @max((try std.Thread.getCpuCount()) - 1, 1);
45+
}
46+
4047
const n_workers = @min(cpu_count, @import("bls").ThreadPool.MAX_WORKERS);
4148
try blst.initThreadPool(@intCast(n_workers));
4249
try pool.state.init();

build.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ pub fn build(b: *std.Build) void {
1313
options_build_options.addOption([]const u8, "preset", option_preset);
1414
const options_module_build_options = options_build_options.createModule();
1515

16+
const options_bls_options = b.addOptions();
17+
const option_thread_count = b.option(u8, "thread_count", "") orelse 0;
18+
options_bls_options.addOption(u8, "thread_count", option_thread_count);
19+
const options_module_bls_options = options_bls_options.createModule();
20+
1621
const options_download_era_options = b.addOptions();
1722
const option_era_base_url = b.option([]const u8, "era_base_url", "") orelse "https://mainnet.era.nimbus.team";
1823
options_download_era_options.addOption([]const u8, "era_base_url", option_era_base_url);
@@ -1153,6 +1158,7 @@ pub fn build(b: *std.Build) void {
11531158
module_bench_process_epoch.addImport("era", module_era);
11541159

11551160
module_bindings.addImport("bls", module_bls);
1161+
module_bindings.addImport("bls_options", options_module_bls_options);
11561162
module_bindings.addImport("persistent_merkle_tree", module_persistent_merkle_tree);
11571163
module_bindings.addImport("ssz", module_ssz);
11581164
module_bindings.addImport("consensus_types", module_consensus_types);

zbuild.zon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
.type = "string",
4545
},
4646
},
47+
.bls_options = .{
48+
.thread_count = .{
49+
.default = 0,
50+
.type = "u8",
51+
},
52+
},
4753
.download_era_options = .{
4854
.era_base_url = .{
4955
.default = "https://mainnet.era.nimbus.team",
@@ -245,6 +251,7 @@
245251
.root_source_file = "bindings/napi/root.zig",
246252
.imports = .{
247253
.bls,
254+
.bls_options,
248255
.persistent_merkle_tree,
249256
.ssz,
250257
.consensus_types,

0 commit comments

Comments
 (0)