perf_map: support max_entries=0

When a PerfMap has max_entries=0, set max_entries to the number of
available CPUs.
pull/1/head
Alessandro Decina 4 years ago
parent f56c32b46b
commit 68a633fe51

@ -9,6 +9,7 @@ use std::{
use thiserror::Error;
use crate::{
generated::bpf_map_type::BPF_MAP_TYPE_PERF_EVENT_ARRAY,
maps::{Map, MapError, MapLock, MapRef, MapRefMut},
obj::{
btf::{Btf, BtfError},
@ -16,6 +17,7 @@ use crate::{
},
programs::{KProbe, Program, ProgramData, ProgramError, SocketFilter, TracePoint, UProbe, Xdp},
sys::bpf_map_update_elem_ptr,
util::{possible_cpus, POSSIBLE_CPUS},
};
pub(crate) const BPF_OBJ_NAME_LEN: usize = 16;
@ -74,7 +76,16 @@ impl Bpf {
}
let mut maps = Vec::new();
for (_, obj) in obj.maps.drain() {
for (_, mut obj) in obj.maps.drain() {
if obj.def.map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY && obj.def.max_entries == 0 {
obj.def.max_entries = *possible_cpus()
.map_err(|error| BpfError::FileError {
path: PathBuf::from(POSSIBLE_CPUS),
error,
})?
.last()
.unwrap_or(&0);
}
let mut map = Map { obj, fd: None };
let fd = map.create()?;
if !map.obj.data.is_empty() && map.obj.name != ".bss" {

@ -1,7 +1,7 @@
use std::{fs, io, str::FromStr};
const ONLINE_CPUS: &str = "/sys/devices/system/cpu/online";
const POSSIBLE_CPUS: &str = "/sys/devices/system/cpu/possible";
pub(crate) const POSSIBLE_CPUS: &str = "/sys/devices/system/cpu/possible";
pub fn online_cpus() -> Result<Vec<u32>, io::Error> {
let data = fs::read_to_string(ONLINE_CPUS)?;

Loading…
Cancel
Save