#affinity #cpu #core #thread #process

affinity

Provides a consistent way to set core affinity for currently running threads and processes

3 releases

0.1.2 Oct 16, 2021
0.1.1 Apr 3, 2020
0.1.0 Apr 3, 2020

#159 in Operating systems

Download history 883/week @ 2023-02-04 984/week @ 2023-02-11 994/week @ 2023-02-18 856/week @ 2023-02-25 1291/week @ 2023-03-04 745/week @ 2023-03-11 815/week @ 2023-03-18 1000/week @ 2023-03-25 968/week @ 2023-04-01 1020/week @ 2023-04-08 750/week @ 2023-04-15 961/week @ 2023-04-22 1323/week @ 2023-04-29 1171/week @ 2023-05-06 1359/week @ 2023-05-13 1139/week @ 2023-05-20

5,164 downloads per month
Used in 6 crates (2 directly)

MIT/Apache

12KB
211 lines

Affinity

Build Status crates.io mio Lines of Code

This crate provides a consistent way to set core affinity for currently running threads and processes.

Usage

use affinity::*;
fn bind_even_cores() {
    // Select every second core
    let cores: Vec<usize> = (0..get_core_num()).step_by(2).collect();
    println!("Binding thread to cores : {:?}", &cores);
    // Output : "Binding thread to cores : [0, 2, 4, 6]"
    
    set_thread_affinity(&cores).unwrap();
    println!("Current thread affinity : {:?}", get_thread_affinity().unwrap());
    // Output : "Current thread affinity : [0, 2, 4, 6]"
}

Complete example here.

Features

  • Bind to multiple cores
  • Return list of currently bound cores
  • Reliably get number of cores (uses num_cpus)
  • Allow caller to handle errors
  • Supports affinity inheritance for new child processes on Windows (through set_process_affinity())

Platforms

Currently only tested on :

  • Windows
  • Linux (Arch x64)

License

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~0–4MB
~60K SLoC