#process #thread #cpu #core #api-bindings

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

#223 in Operating systems

Download history 2042/week @ 2024-07-20 2886/week @ 2024-07-27 1986/week @ 2024-08-03 1878/week @ 2024-08-10 1737/week @ 2024-08-17 2033/week @ 2024-08-24 1903/week @ 2024-08-31 1916/week @ 2024-09-07 1471/week @ 2024-09-14 2476/week @ 2024-09-21 1731/week @ 2024-09-28 2071/week @ 2024-10-05 1621/week @ 2024-10-12 1477/week @ 2024-10-19 1317/week @ 2024-10-26 1635/week @ 2024-11-02

6,498 downloads per month
Used in 8 crates (3 directly)

MIT/Apache

13KB
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–6.5MB
~36K SLoC