Skip to content

Commit 525b60a

Browse files
fkcrazy001d-e-s-o
andcommitted
libbpf-rs: Add helper APIs and tests for OpenMap
for more details: ref to #1382 Signed-off-by: Panda Jiang <3160104094@zju.edu.cn> add test for openMapImpl Signed-off-by: Panda Jiang <3160104094@zju.edu.cn> fmt fix Signed-off-by: Panda Jiang <3160104094@zju.edu.cn> Apply suggestion from @d-e-s-o Co-authored-by: Daniel Müller <d-e-s-o@users.noreply.github.com>
1 parent cc17ebc commit 525b60a

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

libbpf-rs/src/map.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,26 @@ impl<'obj> OpenMap<'obj> {
115115
pub fn autocreate(&self) -> bool {
116116
unsafe { libbpf_sys::bpf_map__autocreate(self.ptr.as_ptr()) }
117117
}
118+
119+
/// Retrieve the map flags.
120+
pub fn map_flags(&self) -> u32 {
121+
unsafe { libbpf_sys::bpf_map__map_flags(self.ptr.as_ptr()) }
122+
}
123+
124+
/// Retrieve the map numa node.
125+
pub fn numa_node(&self) -> u32 {
126+
unsafe { libbpf_sys::bpf_map__numa_node(self.ptr.as_ptr()) }
127+
}
128+
129+
/// Retrieve the key size of the map in bytes.
130+
pub fn key_size(&self) -> u32 {
131+
unsafe { libbpf_sys::bpf_map__key_size(self.ptr.as_ptr()) }
132+
}
133+
134+
/// Retrieve the value size of the map in bytes.
135+
pub fn value_size(&self) -> u32 {
136+
unsafe { libbpf_sys::bpf_map__value_size(self.ptr.as_ptr()) }
137+
}
118138
}
119139

120140
impl<'obj> OpenMapMut<'obj> {

libbpf-rs/tests/test.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2782,6 +2782,26 @@ fn test_map_query_fdinfo() {
27822782
assert_eq!(fdinfo.owner_jited, None);
27832783
}
27842784

2785+
/// Check that `OpenMap` gets information correctly.
2786+
#[tag(root)]
2787+
#[test]
2788+
fn test_map_get_info() {
2789+
let obj = open_test_object("runqslower.bpf.o");
2790+
2791+
let start = obj
2792+
.maps()
2793+
.find(|map| map.name() == OsStr::new("start"))
2794+
.expect("failed to find `start` map");
2795+
2796+
assert!(start.autocreate());
2797+
assert_eq!(start.key_size(), size_of::<u32>() as _);
2798+
assert_eq!(start.value_size(), size_of::<u64>() as _);
2799+
assert_eq!(start.map_flags(), 0);
2800+
assert_eq!(start.map_type(), MapType::Hash);
2801+
assert_eq!(start.max_entries(), 10240);
2802+
assert_eq!(start.numa_node(), 0);
2803+
}
2804+
27852805
/// Check that we can adjust a map's value size.
27862806
#[tag(root)]
27872807
#[test]

0 commit comments

Comments
 (0)