| Crates.io | core_affinity2 |
| lib.rs | core_affinity2 |
| version | 0.15.4 |
| created_at | 2025-11-13 00:41:42.322157+00 |
| updated_at | 2025-11-13 00:41:42.322157+00 |
| description | Core Affinity crate to bind to cores, cross platform |
| homepage | |
| repository | https://github.com/AFLplusplus/LibAFL/ |
| max_upload_size | |
| id | 1930199 |
| size | 50,691 |
Core_Affinity2: Manage CPU affinities even harderA 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.
Add this to your Cargo.toml:
[dependencies]
core_affinity2 = "0.15.4" # Replace with the latest version
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.");
}
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();
core_affinity2 is cross-platform and supports the following operating systems:
x86_64 and aarch64)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 SupportThis crate has no_std support.
Contributions are welcome! Please see the main LibAFL CONTRIBUTING.md for more information.
This crate is licensed under either of the MIT License or the Apache License 2.0.