Skip to content

Commit

Permalink
fix macos build
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Pyattaev committed Dec 8, 2024
1 parent a9f0a51 commit da038b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
25 changes: 8 additions & 17 deletions thread-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,9 @@ impl RuntimeManager {
self.tokio_runtimes.get(n)
}
pub fn set_process_affinity(config: &RuntimeManagerConfig) -> anyhow::Result<Vec<usize>> {
let chosen_cores_mask: Vec<usize> = {
match config.default_core_allocation {
CoreAllocation::PinnedCores { min, max } => (min..max).collect(),
CoreAllocation::DedicatedCoreSet { min, max } => (min..max).collect(),
CoreAllocation::OsDefault => vec![],
}
};
let chosen_cores_mask = config.default_core_allocation.as_core_mask_vector();

if cfg!(target_os = "linux") {
if let Err(e) = affinity::set_thread_affinity(&chosen_cores_mask) {
anyhow::bail!(e.to_string())
}
}
crate::policy::set_thread_affinity(&chosen_cores_mask);
Ok(chosen_cores_mask)
}

Expand Down Expand Up @@ -131,13 +121,14 @@ mod tests {
std::collections::HashMap,
};

// Nobody runs Agave on windows, and on Mac we can not set mask affinity without patching external crate
#[cfg(target_os = "linux")]
fn validate_affinity(expect_cores: &[usize], error_msg: &str) {
// Nobody runs Agave on windows, and on Mac we can not set mask affinity without patching external crate
if cfg!(target_os = "linux") {
let aff = affinity::get_thread_affinity().unwrap();
assert_eq!(aff, expect_cores, "{}", error_msg);
}
let aff = affinity::get_thread_affinity().unwrap();
assert_eq!(aff, expect_cores, "{}", error_msg);
}
#[cfg(not(target_os = "linux"))]
fn validate_affinity(expect_cores: &[usize], error_msg: &str) {}

#[test]
fn process_affinity() {
Expand Down
18 changes: 10 additions & 8 deletions thread-manager/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ impl CoreAllocation {
}
}

#[cfg(target_os = "linux")]
pub fn set_thread_affinity(cores: &[usize]) {
affinity::set_thread_affinity(cores).expect("Can not set thread affinity for runtime worker");
}

#[cfg(not(target_os = "linux"))]
pub fn set_thread_affinity(_cores: &[usize]) {}

///Applies policy to the calling thread
pub fn apply_policy(
alloc: &CoreAllocation,
Expand All @@ -45,19 +53,13 @@ pub fn apply_policy(
let core = lg
.pop()
.expect("Not enough cores provided for pinned allocation");
if cfg!(target_os = "linux") {
affinity::set_thread_affinity([core])
.expect("Can not set thread affinity for runtime worker");
}
set_thread_affinity(&[core]);
}
CoreAllocation::DedicatedCoreSet { min: _, max: _ } => {
let lg = chosen_cores_mask
.lock()
.expect("Can not lock core mask mutex");
if cfg!(target_os = "linux") {
affinity::set_thread_affinity(&(*lg))
.expect("Can not set thread affinity for runtime worker");
}
set_thread_affinity(&lg);
}
CoreAllocation::OsDefault => {}
}
Expand Down

0 comments on commit da038b3

Please sign in to comment.