Skip to content
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
4 changes: 2 additions & 2 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion yellowstone-grpc-geyser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ crate-type = ["cdylib", "rlib"]
name = "config-check"

[dependencies]
affinity = { workspace = true }
anyhow = { workspace = true }
base64 = { workspace = true }
bincode = { workspace = true }
Expand Down Expand Up @@ -79,6 +78,10 @@ prost_011 = { workspace = true, optional = true }
# Yellowstone
yellowstone-grpc-proto = { workspace = true, features = ["tonic", "account-data-as-bytes"] }

[target.'cfg(any(target_os = "linux", target_os = "windows"))'.dependencies]
# wrapped in mod cpu_core_affinity
affinity = { workspace = true }

[[bench]]
name = "encode"
harness = false
Expand Down
2 changes: 1 addition & 1 deletion yellowstone-grpc-geyser/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn parse_taskset(taskset: &str) -> Result<Vec<usize>, String> {
vec.sort();

if let Some(set_max_index) = vec.last().copied() {
let max_index = affinity::get_thread_affinity()
let max_index = crate::util::cpu_core_affinity::get_thread_affinity()
.map_err(|_err| "failed to get affinity".to_owned())?
.into_iter()
.max()
Expand Down
3 changes: 2 additions & 1 deletion yellowstone-grpc-geyser/src/plugin/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ impl GeyserPlugin for Plugin {
}
if let Some(tokio_cpus) = config.tokio.affinity.clone() {
builder.on_thread_start(move || {
affinity::set_thread_affinity(&tokio_cpus).expect("failed to set affinity")
crate::util::cpu_core_affinity::set_thread_affinity(&tokio_cpus)
.expect("failed to set affinity")
});
}
let plugin_cancellation_token = CancellationToken::new();
Expand Down
30 changes: 30 additions & 0 deletions yellowstone-grpc-geyser/src/util/cpu_core_affinity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#![allow(clippy::missing_const_for_fn)]

#[cfg(any(target_os = "linux", target_os = "windows"))]
pub fn set_thread_affinity(cpus: &[usize]) -> Result<(), String> {
affinity::set_thread_affinity(cpus).map_err(|e| e.to_string())
}

#[cfg(not(any(target_os = "linux", target_os = "windows")))]
pub fn set_thread_affinity(_cpus: &[usize]) -> Result<(), String> {
Ok(())
}

#[cfg(any(target_os = "linux", target_os = "windows"))]
pub fn get_thread_affinity() -> Result<Vec<usize>, String> {
affinity::get_thread_affinity().map_err(|e| e.to_string())
}

/// Return sensible values though these IDs are not really useful as the set_thread_affinity() method is a no-op.
///
/// Returns all CPU indices as available cores. Uses `std::thread::available_parallelism`
/// which accounts for cgroup/sched_affinity restrictions on Linux but on macOS simply
/// returns the number of logical CPUs (hw.logicalcpu) without actual affinity support.
/// See https://doc.rust-lang.org/std/thread/fn.available_parallelism.html
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
pub fn get_thread_affinity() -> Result<Vec<usize>, String> {
let num_cpus = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1);
Ok((0..num_cpus).collect())
}
1 change: 1 addition & 0 deletions yellowstone-grpc-geyser/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod cpu_core_affinity;
pub mod stream;
#[cfg(test)]
pub(crate) mod testkit;
Loading