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 churn on module load by reusing kernel BTF spec #487

Merged
merged 1 commit into from
Feb 18, 2025

Conversation

patrickpichler
Copy link
Contributor

@patrickpichler patrickpichler commented Feb 18, 2025

Before this change we always parsed the given BTFPath spec. In 99% of the cases, the path pointed to the default kernel BTF location. Due to the way the cilium/ebpf library handles passing BTF spec, this caused the kernels BTF spec to be effectively parsed twice, causing quite the memory churn.

The docs mention that not passing KernelTypes for the program option will implicitly use the kernel BTF spec. While this is true, it also causes a lot of memory churn in the process, as each time the btf.LoadKernelSpec function gets called, the spec is copied.

To work around this, kvisor detects if the given BTFPath is the default BTF location, parses the kernel BTF spec once and passes it to the ebpf lib via the KernelTypes option.

Running benchmarks, this change allows to cut the number of allocations by ~1/3.

Benchmark before optimization

5 224041936 ns/op 111770155 B/op 1213041 allocs/op

Benchmark after optimization

6 173737552 ns/op 73678120 B/op 848361 allocs/op

Before this change we always parsed the given BTFPath spec. In 99% of
the cases, the path pointed to the default kernel BTF location. Due to
the way the cilium/ebpf library handles passing BTF spec, this caused
the kernels BTF spec to be effectively parsed twice, causing quite the
memory churn.

The docs mention that not passing `KernelTypes` for the  program option
will implicitly use the kernel BTF spec. While this is true, it also
causes a lot of memory churn in the process, as each time the
`btf.LoadKernelSpec` function gets called, the spec is copied.

To work around this, kvisor detects if the given `BTFPath` is the
default BTF location, parses the kernel BTF spec once and passes it to
the ebpf lib via the `KernelTypes` option.

Running benchmarks, this change allows to cut the number of
allocations by ~1/3.

  Benchmark before optimization
  5  224041936 ns/op 111770155 B/op 1213041 allocs/op

  Benchmark after optimization
  6  173737552 ns/op  73678120 B/op  848361 allocs/op
@patrickpichler
Copy link
Contributor Author

Related fix in the cilium/ebpf library: cilium/ebpf#1690

@patrickpichler patrickpichler merged commit 0f65cfe into main Feb 18, 2025
2 checks passed
@patrickpichler patrickpichler deleted the feature-reduce-memory-churn-on-startup branch February 18, 2025 11:59
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 this pull request may close these issues.

2 participants