Skip to content

Commit

Permalink
Merge pull request #125 from monadicus/feat-stock-runtime
Browse files Browse the repository at this point in the history
feat(aot): configure tokio runtime by env on node
  • Loading branch information
Meshiest authored Apr 26, 2024
2 parents 82053ba + 736d1b1 commit 4bc2ace
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ lasso = { version = "0.7", features = ["multi-threaded"] }
local-ip-address = "0.6"
metrics-exporter-prometheus = "0.14"
nix = { version = "0.28", features = ["process"] }
num_cpus = "1.16"
rand = { version = "0.8", default-features = false }
rand_chacha = { version = "0.3", default-features = false }
rayon = "1"
Expand Down
4 changes: 4 additions & 0 deletions crates/aot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ node = [
"crossterm",
"snarkos-node/metrics",
"metrics-exporter-prometheus",
"num_cpus",
"rayon",
]

[dependencies]
Expand All @@ -29,6 +31,8 @@ indicatif.workspace = true
lazy_static.workspace = true
metrics-exporter-prometheus = { workspace = true, optional = true }
nix.workspace = true
num_cpus = { optional = true, workspace = true }
rayon = { workspace = true, optional = true }
rand.workspace = true
rand_chacha.workspace = true
reqwest = { workspace = true, features = ["blocking", "json"] }
Expand Down
43 changes: 42 additions & 1 deletion crates/aot/src/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,20 @@ pub struct Runner {
}

impl Runner {
pub fn parse(self) -> Result<()> {
if std::env::var("DEFAULT_RUNTIME").ok().is_some() {
self.start_without_runtime()
} else {
Self::runtime().block_on(async move { self.start().await })
}
}

#[tokio::main]
pub async fn parse(self) -> Result<()> {
pub async fn start_without_runtime(self) -> Result<()> {
self.start().await
}

pub async fn start(self) -> Result<()> {
let bind_addr = self.bind_addr;
let node_ip = SocketAddr::new(bind_addr, self.node);
let rest_ip = SocketAddr::new(bind_addr, self.rest);
Expand Down Expand Up @@ -212,4 +224,33 @@ impl Runner {

Ok(())
}

/// Returns a runtime for the node.
pub fn runtime() -> tokio::runtime::Runtime {
// Retrieve the number of cores.
let num_cores = num_cpus::get();

// Initialize the number of tokio worker threads, max tokio blocking threads,
// and rayon cores. Note: We intentionally set the number of tokio
// worker threads and number of rayon cores to be more than the number
// of physical cores, because the node is expected to be I/O-bound.
let (num_tokio_worker_threads, max_tokio_blocking_threads, num_rayon_cores_global) =
(2 * num_cores, 512, num_cores);

// Initialize the parallelization parameters.
rayon::ThreadPoolBuilder::new()
.stack_size(8 * 1024 * 1024)
.num_threads(num_rayon_cores_global)
.build_global()
.unwrap();

// Initialize the runtime configuration.
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.thread_stack_size(8 * 1024 * 1024)
.worker_threads(num_tokio_worker_threads)
.max_blocking_threads(max_tokio_blocking_threads)
.build()
.expect("Failed to initialize a runtime for the router")
}
}

0 comments on commit 4bc2ace

Please sign in to comment.