#affinity #cpu #core #thread #process

affinity

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

2 releases

0.1.1 Apr 3, 2020
0.1.0 Apr 3, 2020

#140 in Operating systems

Download history 76/week @ 2021-02-25 228/week @ 2021-03-04 153/week @ 2021-03-11 139/week @ 2021-03-18 154/week @ 2021-03-25 55/week @ 2021-04-01 211/week @ 2021-04-08 83/week @ 2021-04-15 110/week @ 2021-04-22 130/week @ 2021-04-29 74/week @ 2021-05-06 92/week @ 2021-05-13 42/week @ 2021-05-20 43/week @ 2021-05-27 140/week @ 2021-06-03 143/week @ 2021-06-10

568 downloads per month
Used in 5 crates (via switchyard)

MIT/Apache

12KB
180 lines

Affinity

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

~82KB