Crate core_affinity2

Crate core_affinity2 

Source
Expand description

This crate manages CPU affinities.

§Example

This example shows how create a thread for each available processor and pin each thread to its corresponding processor.

use core_affinity2;

// Retrieve the IDs of all active CPU cores.
let core_ids = core_affinity2::get_core_ids().unwrap();

// Create a thread for each active CPU core.
let handles = core_ids
    .into_iter()
    .map(|id| {
        thread::spawn(move || {
            // Pin this thread to a single CPU core.
            id.set_affinity();
            // Do more work after this.
        })
    })
    .collect::<Vec<_>>();

for handle in handles.into_iter() {
    handle.join().unwrap();
}

This file is a fork of https://github.com/Elzair/core_affinity_rs

§Core_Affinity2: Manage CPU affinities even harder

LibAFL logo

A crate to manage CPU core affinity for threads, maintained as part of the LibAFL project.

core_affinity2 allows you to get the list of available cores on a system and to pin the current thread to a specific core.

Pinning threads to cores can improve performance by reducing cache misses and avoiding thread migration between cores. This is particularly useful in performance-sensitive applications like fuzzing, scientific computing, and real-time systems.

This crate is a fork of the original core_affinity crate, with updates and continued maintenance.

§Usage

Add this to your Cargo.toml:

[dependencies]
core_affinity2 = "0.15.4" # Replace with the latest version

§Example: Pinning threads to cores

Here is an example of how to get the available core IDs and spawn a thread for each, pinning it to the respective core.

use std::thread;
use core_affinity2::{get_core_ids, CoreId};

// Get the available core IDs
if let Some(core_ids) = get_core_ids() {
    let core_count = core_ids.len();
    println!("Found {} cores:", core_count);

    let handles: Vec<_> = core_ids.into_iter().map(|id| {
        thread::spawn(move || {
            // Pin this thread to a single CPU core.
            if id.set_affinity().is_ok() {
                println!("Thread {:?} is running on core {:?}", thread::current().id(), id);
                // Do some work here
            } else {
                eprintln!("Could not pin thread to core {:?}", id);
            }
        })
    }).collect();

    for handle in handles {
        handle.join().unwrap();
    }
} else {
    println!("Could not get core IDs.");
}

§Parsing core ranges

The Cores struct provides a convenient way to work with a set of cores, including parsing from a command-line string.

use core_affinity2::Cores;

// Parse a comma-separated list of cores and ranges
let cores = Cores::from_cmdline("0,2-4,7").unwrap();
assert_eq!(cores.ids, vec![0.into(), 2.into(), 3.into(), 4.into(), 7.into()]);

// "all" will use all available cores
// let all_cores = Cores::from_cmdline("all").unwrap();

§Supported Platforms

core_affinity2 is cross-platform and supports the following operating systems:

  • Linux
  • Windows
  • macOS (x86_64 and aarch64)
  • FreeBSD
  • NetBSD
  • OpenBSD
  • Dragonfly BSD
  • Solaris / Illumos
  • Haiku

Note that on some platforms (like macOS on aarch64), it’s not possible to pin a thread to a specific core, but the library will still try to request the highest performance for the thread.

§no_std Support

This crate has no_std support.

§Contributing

Contributions are welcome! Please see the main LibAFL CONTRIBUTING.md for more information.

§License

This crate is licensed under either of the MIT License or the Apache License 2.0.

No documented features in Cargo.toml

Structs§

CoreId
This represents a CPU core.
Cores
A list of CoreId to use for fuzzing

Functions§

get_core_ids
This function tries to retrieve information on all the “cores” active on this system.