pub struct PerCpuHashMap<T, K: Pod, V: Pod> { /* private fields */ }Expand description
Similar to HashMap but each CPU holds a separate value for a given key. Typically used to
minimize lock contention in eBPF programs.
This type can be used with eBPF maps of type BPF_MAP_TYPE_PERCPU_HASH and
BPF_MAP_TYPE_LRU_PERCPU_HASH.
§Minimum kernel version
The minimum kernel version required to use this feature is 4.6.
§Examples
use aya::maps::PerCpuHashMap;
const CPU_IDS: u8 = 1;
const WAKEUPS: u8 = 2;
let mut hm = PerCpuHashMap::<_, u8, u32>::try_from(bpf.map_mut("PER_CPU_STORAGE").unwrap())?;
let cpu_ids = unsafe { hm.get(&CPU_IDS, 0)? };
let wakeups = unsafe { hm.get(&WAKEUPS, 0)? };
for (cpu_id, wakeups) in cpu_ids.iter().zip(wakeups.iter()) {
println!("cpu {} woke up {} times", cpu_id, wakeups);
}Implementations§
Source§impl<T: Borrow<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V>
impl<T: Borrow<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V>
Sourcepub fn get(&self, key: &K, flags: u64) -> Result<PerCpuValues<V>, MapError>
pub fn get(&self, key: &K, flags: u64) -> Result<PerCpuValues<V>, MapError>
Returns a slice of values - one for each CPU - associated with the key.
Sourcepub fn iter(&self) -> MapIter<'_, K, PerCpuValues<V>, Self> ⓘ
pub fn iter(&self) -> MapIter<'_, K, PerCpuValues<V>, Self> ⓘ
An iterator visiting all key-value pairs in arbitrary order. The
iterator item type is Result<(K, PerCpuValues<V>), MapError>.
Source§impl<T: BorrowMut<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V>
impl<T: BorrowMut<MapData>, K: Pod, V: Pod> PerCpuHashMap<T, K, V>
Sourcepub fn insert(
&mut self,
key: impl Borrow<K>,
values: PerCpuValues<V>,
flags: u64,
) -> Result<(), MapError>
pub fn insert( &mut self, key: impl Borrow<K>, values: PerCpuValues<V>, flags: u64, ) -> Result<(), MapError>
Inserts a slice of values - one for each CPU - for the given key.
§Examples
use aya::maps::{PerCpuHashMap, PerCpuValues};
use aya::util::nr_cpus;
const RETRIES: u8 = 1;
let nr_cpus = nr_cpus().map_err(|(_, error)| error)?;
let mut hm = PerCpuHashMap::<_, u8, u32>::try_from(bpf.map_mut("PER_CPU_STORAGE").unwrap())?;
hm.insert(
RETRIES,
PerCpuValues::try_from(vec![3u32; nr_cpus])?,
0,
)?;Trait Implementations§
Source§impl<'a, T: Borrow<MapData>, K: Pod, V: Pod> IntoIterator for &'a PerCpuHashMap<T, K, V>
impl<'a, T: Borrow<MapData>, K: Pod, V: Pod> IntoIterator for &'a PerCpuHashMap<T, K, V>
Source§type Item = Result<(K, PerCpuValues<V>), MapError>
type Item = Result<(K, PerCpuValues<V>), MapError>
The type of the elements being iterated over.
Source§type IntoIter = MapIter<'a, K, PerCpuValues<V>, PerCpuHashMap<T, K, V>>
type IntoIter = MapIter<'a, K, PerCpuValues<V>, PerCpuHashMap<T, K, V>>
Which kind of iterator are we turning this into?
Source§impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<K, PerCpuValues<V>> for PerCpuHashMap<T, K, V>
impl<T: Borrow<MapData>, K: Pod, V: Pod> IterableMap<K, PerCpuValues<V>> for PerCpuHashMap<T, K, V>
Auto Trait Implementations§
impl<T, K, V> Freeze for PerCpuHashMap<T, K, V>where
T: Freeze,
impl<T, K, V> RefUnwindSafe for PerCpuHashMap<T, K, V>
impl<T, K, V> Send for PerCpuHashMap<T, K, V>
impl<T, K, V> Sync for PerCpuHashMap<T, K, V>
impl<T, K, V> Unpin for PerCpuHashMap<T, K, V>
impl<T, K, V> UnsafeUnpin for PerCpuHashMap<T, K, V>where
T: UnsafeUnpin,
impl<T, K, V> UnwindSafe for PerCpuHashMap<T, K, V>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more