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

btf: Optimize string table for globally increasing offsets #1210

Merged
merged 1 commit into from
Nov 7, 2023

Conversation

dylandreimerink
Copy link
Member

@dylandreimerink dylandreimerink commented Nov 7, 2023

After doing some observations while parsing VMLinux, it seems that the lookup access pattern for strings is mostly globally increasing with with some patches of seemingly unpridicable offsets.

This is a sample of the access pattern, numbers are the index into the string table, not the offset:

1112
1113
1114
348
78
1115
1116
372
1117
1118
1119

This commit adds logic to track this globally increasing offset and checks if the if the offset at the next expected index matches the offset we are looking for. If it does, we can skip the binary search. In VMLinux this fast path is taken about 50% of the time, resulting in about a 7% speedup.

goos: linux
goarch: amd64
pkg: github.com/cilium/ebpf/btf
cpu: 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
                │ before.txt  │          after.txt          │
                │   sec/op    │   sec/op     vs base        │
ParseVmlinux-16   48.52m ± 1%   44.79m ± 1%  -7.69% (n=100)

                │  before.txt  │            after.txt            │
                │     B/op     │     B/op      vs base           │
ParseVmlinux-16   31.45Mi ± 0%   31.45Mi ± 0%  ~ (p=0.127 n=100)

                │ before.txt  │           after.txt            │
                │  allocs/op  │  allocs/op   vs base           │
ParseVmlinux-16   534.1k ± 0%   534.1k ± 0%  ~ (p=0.130 n=100)

btf/strings.go Outdated Show resolved Hide resolved
btf/strings.go Outdated Show resolved Hide resolved
@dylandreimerink dylandreimerink force-pushed the feature/btf-string-incr-fastpath branch 2 times, most recently from cbc466b to 406033c Compare November 7, 2023 11:43
After doing some observations while parsing VMLinux, it seems that the
lookup access pattern for strings is mostly globally increasing with
with some patches of seemingly unpridicable offsets.

This is a sample of the access pattern, numbers are the index into the
string table, not the offset:
```
1112
1113
1114
348
78
1115
1116
372
1117
1118
1119
```

This commit adds logic to track this globally increasing offset and
checks if the if the offset at the next expected index matches the
offset we are looking for. If it does, we can skip the binary search.
In VMLinux this fast path is taken about 50% of the time, resulting in
about a 7% speedup.

```
goos: linux
goarch: amd64
pkg: github.com/cilium/ebpf/btf
cpu: 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
                │ before.txt  │          after.txt          │
                │   sec/op    │   sec/op     vs base        │
ParseVmlinux-16   48.52m ± 1%   44.79m ± 1%  -7.69% (n=100)

                │  before.txt  │            after.txt            │
                │     B/op     │     B/op      vs base           │
ParseVmlinux-16   31.45Mi ± 0%   31.45Mi ± 0%  ~ (p=0.127 n=100)

                │ before.txt  │           after.txt            │
                │  allocs/op  │  allocs/op   vs base           │
ParseVmlinux-16   534.1k ± 0%   534.1k ± 0%  ~ (p=0.130 n=100)
```

Signed-off-by: Dylan Reimerink <dylan.reimerink@isovalent.com>
@lmb
Copy link
Collaborator

lmb commented Nov 7, 2023

I get this on my machine:

$ benchstat base.txt opt.txt 
goos: linux
goarch: amd64
pkg: github.com/cilium/ebpf/btf
cpu: 12th Gen Intel(R) Core(TM) i7-1260P
                │  base.txt   │              opt.txt              │
                │   sec/op    │   sec/op     vs base              │
ParseVmlinux-16   42.76m ± 3%   40.35m ± 2%  -5.66% (p=0.002 n=6)

                │   base.txt   │            opt.txt            │
                │     B/op     │     B/op      vs base         │
ParseVmlinux-16   31.45Mi ± 0%   31.45Mi ± 0%  ~ (p=0.132 n=6)

                │  base.txt   │           opt.txt            │
                │  allocs/op  │  allocs/op   vs base         │
ParseVmlinux-16   534.1k ± 0%   534.1k ± 0%  ~ (p=0.165 n=6)

The laptop is plugged in and I ran sudo cpupower frequency-set -g performance.

Copy link
Collaborator

@lmb lmb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@lmb lmb merged commit 4e460ee into cilium:main Nov 7, 2023
13 checks passed
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