Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aot): configure tokio runtime by env on node #125

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ultimate nit: define these on separate lines for readability

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy/pasted from snarkos source. not gonna change any of this 😨

(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")
}
}