-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.rs
More file actions
54 lines (41 loc) · 1.63 KB
/
main.rs
File metadata and controls
54 lines (41 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use affinity::*;
use std::error::Error;
#[cfg(target_os = "windows")]
fn bind_process() -> Result<(), Box<dyn Error>> {
// Sets the whole proccess affinity
println!("Binding process to cores : [0]");
println!("(This should overwrite threads affinities previously set)");
set_process_affinity(&[0])?;
println!("\tCurrent thread affinity : {:?}", get_thread_affinity()?);
println!("\tCurrent process affinity : {:?}", get_process_affinity()?);
println!("\tTotal cores : {}", get_core_num());
Ok(())
}
#[cfg(not(target_os = "macos"))]
pub fn main() -> Result<(), Box<dyn Error>> {
println!("Total cores : {}", get_core_num());
let cores = (0..get_core_num()).step_by(2).collect::<Vec<usize>>();
println!("Binding thread to cores : {:?}", &cores);
set_thread_affinity(&cores)?;
let bound_cores = get_thread_affinity()?;
println!("\tCurrent thread affinity : {:?}", bound_cores);
println!("\tTotal cores : {}", get_core_num());
assert_eq!(bound_cores, cores.as_slice());
#[cfg(target_os = "windows")]
bind_process()?;
Ok(())
}
#[cfg(target_os = "macos")]
pub fn main() -> Result<(), Box<dyn Error>> {
println!("Total cores : {}", get_core_num());
let cores = vec![42]; //42 is an affinity tag, see the readme
println!("Binding thread to cores : {:?}", &cores);
set_thread_affinity(&cores)?;
let bound_cores = get_thread_affinity()?;
println!("\tCurrent thread affinity : {:?}", bound_cores);
println!("\tTotal cores : {}", get_core_num());
assert_eq!(bound_cores, cores.as_slice());
#[cfg(target_os = "windows")]
bind_process()?;
Ok(())
}