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

Use readlink instead of filepath.EvalSymlinks for fewer syscalls #264

Merged
merged 1 commit into from
Sep 13, 2023

Conversation

bobrik
Copy link
Contributor

@bobrik bobrik commented Sep 13, 2023

Restarting a systemd unit:

ivan@vm:~$ sudo systemctl restart systemd-resolved

Before: 10x newfstatat + 2x readlinkat

ivan@vm:~$ sudo strace -f -TT -e open_by_handle_at,newfstatat,readlinkat -p $(pidof ebpf_exporter)
strace: Process 13503 attached with 9 threads
[pid 13507] open_by_handle_at(7, {handle_bytes=8, handle_type=254, f_handle="\x36\x00\x00\x00\x00\x00\x00\x00"}, O_RDONLY) = 13 <0.000519>
[pid 13507] newfstatat(AT_FDCWD, "/proc", {st_mode=S_IFDIR|0555, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.000821>
[pid 13507] newfstatat(AT_FDCWD, "/proc/self", {st_mode=S_IFLNK|0777, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.001389>
[pid 13507] readlinkat(AT_FDCWD, "/proc/self", "13503", 128) = 5 <0.000577>
[pid 13507] newfstatat(AT_FDCWD, "/proc/13503", {st_mode=S_IFDIR|0555, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.000835>
[pid 13507] newfstatat(AT_FDCWD, "/proc/13503/fd", {st_mode=S_IFDIR|0500, st_size=14, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.000872>
[pid 13507] newfstatat(AT_FDCWD, "/proc/13503/fd/13", {st_mode=S_IFLNK|0500, st_size=64, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.000373>
[pid 13507] readlinkat(AT_FDCWD, "/proc/13503/fd/13", "/sys/fs/cgroup/system.slice", 128) = 27 <0.000750>
[pid 13507] newfstatat(AT_FDCWD, "/sys", {st_mode=S_IFDIR|0555, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.000855>
[pid 13507] newfstatat(AT_FDCWD, "/sys/fs", {st_mode=S_IFDIR|0755, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.000695>
[pid 13507] newfstatat(AT_FDCWD, "/sys/fs/cgroup", {st_mode=S_IFDIR|0555, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.001007>
[pid 13507] newfstatat(AT_FDCWD, "/sys/fs/cgroup/system.slice", {st_mode=S_IFDIR|0755, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.000586>
[pid 13507] newfstatat(AT_FDCWD, "/sys/fs/cgroup/system.slice/systemd-resolved.service", {st_mode=S_IFDIR|0755, st_size=0, ...}, 0) = 0 <0.000598>

After: 1x newfstatat + 1x readlinkat

ivan@vm:~$ sudo strace -f -TT -e open_by_handle_at,newfstatat,readlinkat -p $(pidof ebpf_exporter)
strace: Process 13290 attached with 10 threads
[pid 13290] open_by_handle_at(7, {handle_bytes=8, handle_type=254, f_handle="\x36\x00\x00\x00\x00\x00\x00\x00"}, O_RDONLY) = 13 <0.000319>
[pid 13290] readlinkat(AT_FDCWD, "/proc/self/fd/13", "/sys/fs/cgroup/system.slice", 128) = 27 <0.000027>
[pid 13290] newfstatat(AT_FDCWD, "/sys/fs/cgroup/system.slice/systemd-resolved.service", {st_mode=S_IFDIR|0755, st_size=0, ...}, 0) = 0 <0.001083>

Hopefully this helps with super-short lived cgroup detection.

Restarting a systemd unit:

```
ivan@vm:~$ sudo systemctl restart systemd-resolved
```

Before: 10x `newfstatat` + 2x `readlinkat`

```
ivan@vm:~$ sudo strace -f -TT -e open_by_handle_at,newfstatat,readlinkat -p $(pidof ebpf_exporter)
strace: Process 13503 attached with 9 threads
[pid 13507] open_by_handle_at(7, {handle_bytes=8, handle_type=254, f_handle="\x36\x00\x00\x00\x00\x00\x00\x00"}, O_RDONLY) = 13 <0.000519>
[pid 13507] newfstatat(AT_FDCWD, "/proc", {st_mode=S_IFDIR|0555, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.000821>
[pid 13507] newfstatat(AT_FDCWD, "/proc/self", {st_mode=S_IFLNK|0777, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.001389>
[pid 13507] readlinkat(AT_FDCWD, "/proc/self", "13503", 128) = 5 <0.000577>
[pid 13507] newfstatat(AT_FDCWD, "/proc/13503", {st_mode=S_IFDIR|0555, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.000835>
[pid 13507] newfstatat(AT_FDCWD, "/proc/13503/fd", {st_mode=S_IFDIR|0500, st_size=14, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.000872>
[pid 13507] newfstatat(AT_FDCWD, "/proc/13503/fd/13", {st_mode=S_IFLNK|0500, st_size=64, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.000373>
[pid 13507] readlinkat(AT_FDCWD, "/proc/13503/fd/13", "/sys/fs/cgroup/system.slice", 128) = 27 <0.000750>
[pid 13507] newfstatat(AT_FDCWD, "/sys", {st_mode=S_IFDIR|0555, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.000855>
[pid 13507] newfstatat(AT_FDCWD, "/sys/fs", {st_mode=S_IFDIR|0755, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.000695>
[pid 13507] newfstatat(AT_FDCWD, "/sys/fs/cgroup", {st_mode=S_IFDIR|0555, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.001007>
[pid 13507] newfstatat(AT_FDCWD, "/sys/fs/cgroup/system.slice", {st_mode=S_IFDIR|0755, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0 <0.000586>
[pid 13507] newfstatat(AT_FDCWD, "/sys/fs/cgroup/system.slice/systemd-resolved.service", {st_mode=S_IFDIR|0755, st_size=0, ...}, 0) = 0 <0.000598>
```

After: 1x `newfstatat` + 1x `readlinkat`

```
ivan@vm:~$ sudo strace -f -TT -e open_by_handle_at,newfstatat,readlinkat -p $(pidof ebpf_exporter)
strace: Process 13290 attached with 10 threads
[pid 13290] open_by_handle_at(7, {handle_bytes=8, handle_type=254, f_handle="\x36\x00\x00\x00\x00\x00\x00\x00"}, O_RDONLY) = 13 <0.000319>
[pid 13290] readlinkat(AT_FDCWD, "/proc/self/fd/13", "/sys/fs/cgroup/system.slice", 128) = 27 <0.000027>
[pid 13290] newfstatat(AT_FDCWD, "/sys/fs/cgroup/system.slice/systemd-resolved.service", {st_mode=S_IFDIR|0755, st_size=0, ...}, 0) = 0 <0.001083>
```

Hopefully this helps with super-short lived cgroup detection.
@bobrik bobrik merged commit 1fe0f1a into cloudflare:master Sep 13, 2023
7 checks passed
@bobrik bobrik deleted the ivan/readlink branch September 13, 2023 02:05
@bobrik
Copy link
Contributor Author

bobrik commented Sep 13, 2023

One can probably use fstatat() with the result of open_by_handle_at() and then do a readlinkat() to get the dir name. That way inode is resolved sooner.

bobrik added a commit to bobrik/ebpf_exporter that referenced this pull request Sep 13, 2023
In cloudflare#264 we reduced
the amount of syscalls, here we reorder them to get the inode first.

Before:

```
ivan@vm:~$ sudo strace -f -TT -e open_by_handle_at,newfstatat,readlinkat -p $(pidof ebpf_exporter)
strace: Process 14153 attached with 11 threads
[pid 14153] open_by_handle_at(7, {handle_bytes=8, handle_type=254, f_handle="\x36\x00\x00\x00\x00\x00\x00\x00"}, O_RDONLY) = 13 <0.000764>
[pid 14153] readlinkat(AT_FDCWD, "/proc/self/fd/13", "/sys/fs/cgroup/system.slice", 128) = 27 <0.000815>
[pid 14153] newfstatat(AT_FDCWD, "/sys/fs/cgroup/system.slice/systemd-resolved.service", {st_mode=S_IFDIR|0755, st_size=0, ...}, 0) = 0 <0.000839>
```

After:

```
ivan@vm:~$ sudo strace -f -TT -e open_by_handle_at,newfstatat,readlinkat -p $(pidof ebpf_exporter)
strace: Process 13925 attached with 10 threads
[pid 13931] open_by_handle_at(7, {handle_bytes=8, handle_type=254, f_handle="\x36\x00\x00\x00\x00\x00\x00\x00"}, O_RDONLY) = 13 <0.000700>
[pid 13931] newfstatat(13, "systemd-resolved.service", {st_mode=S_IFDIR|0755, st_size=0, ...}, 0) = 0 <0.001069>
[pid 13931] readlinkat(AT_FDCWD, "/proc/self/fd/13", "/sys/fs/cgroup/system.slice", 128) = 27 <0.000774>
```
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.

1 participant