Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce memory footprint when loading BTF #1172

Open
zkonge opened this issue Feb 7, 2025 · 1 comment
Open

Reduce memory footprint when loading BTF #1172

zkonge opened this issue Feb 7, 2025 · 1 comment

Comments

@zkonge
Copy link

zkonge commented Feb 7, 2025

Origin

I'm writing a very simple tracepoint eBPF program, which does not need BTF.
But I notice that while the program is running, the memory usage is far more large than I expected

Image

Investigation

After running the memory profile, I found the program contains lots of fragmental allocations.
and it doesn't return memory to OS

Memory exactly used:
Image

Memory allocation in full lifetime:
Image

Here, the Btf::from_sys_fs() is even called twice.
Image

EbpfLoader doesn't give any chance to prevent BTF loading, it load BTF at the struct initialization.

btf: Btf::from_sys_fs().ok().map(Cow::Owned),

and Ebpf::loadcauses the BTF double loading

aya/aya/src/bpf.rs

Lines 879 to 883 in ade2e2a

pub fn load_file<P: AsRef<Path>>(path: P) -> Result<Self, EbpfError> {
EbpfLoader::new()
.btf(Btf::from_sys_fs().ok().as_ref())
.load_file(path)
}

Fixing

After I remove the Btf::from_sys_fs() in the EbpfLoader, the memory usage after calling Ebpf::load reduce to half.

Once I manually build Ebpf from EbpfLoader, the heap allocation is reduced to 150 KiB

Image

More

Although not my use case, there is still a lot of room for optimization in BTF loading, such as arena (like bumpalo?) and using stack allocation instead of Vec abusing.

@dave-tucker
Copy link
Member

Per the comment on the PR, the fix for your case is: https://docs.aya-rs.dev/aya/struct.ebpfloader#method.btf
Passing None here will disable BTF, but this may not be exactly what you want.

Although not my use case, there is still a lot of room for optimization in BTF loading, such as arena (like bumpalo?) and using stack allocation instead of Vec abusing.

I think it's worth leaving this issue open just for ☝

@dave-tucker dave-tucker changed the title EbpfLoader should not load BTF from sys by default Reduce memory footprint when loading BTF Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants