core_affinity2

Crates.iocore_affinity2
lib.rscore_affinity2
version0.15.4
created_at2025-11-13 00:41:42.322157+00
updated_at2025-11-13 00:41:42.322157+00
descriptionCore Affinity crate to bind to cores, cross platform
homepage
repositoryhttps://github.com/AFLplusplus/LibAFL/
max_upload_size
id1930199
size50,691
AFLplusplus (aflpp)

documentation

https://docs.rs/libafl

README

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.

Commit count: 3282

cargo fmt