moby/pkg/fileutils
Sebastiaan van Stijn aace62f6d3
pkg/fileutils: GetTotalUsedFds(): use fast-path for Kernel 6.2 and up
Linux 6.2 and up (commit [f1f1f2569901ec5b9d425f2e91c09a0e320768f3][1])
provides a fast path for the number of open files for the process.

From the [Linux docs][2]:

> The number of open files for the process is stored in 'size' member of
> `stat()` output for /proc/<pid>/fd for fast access.

[1]: f1f1f25699
[2]: https://docs.kernel.org/filesystems/proc.html#proc-pid-fd-list-of-symlinks-to-open-files

This patch adds a fast-path for Kernels that support this, and falls back
to the slow path if the Size fields is zero.

Comparing on a Fedora 38 (kernel 6.2.9-300.fc38.x86_64):

Before/After:

    go test -bench ^BenchmarkGetTotalUsedFds$ -run ^$ ./pkg/fileutils/
    BenchmarkGetTotalUsedFds        57264     18595 ns/op     408 B/op      10 allocs/op
    BenchmarkGetTotalUsedFds       370392      3271 ns/op      40 B/op       3 allocs/op

Note that the slow path has 1 more file-descriptor, due to the open
file-handle for /proc/<pid>/fd during the calculation.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit ec79d0fc05)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-06-30 18:30:34 +02:00
..
fileutils.go pkg/fileutils: ReadSymlinkedDirectory: preserve underlying error 2022-10-16 20:15:08 +02:00
fileutils_darwin.go Add canonical import comment 2018-02-05 16:51:57 -05:00
fileutils_linux.go pkg/fileutils: GetTotalUsedFds(): use fast-path for Kernel 6.2 and up 2023-06-30 18:30:34 +02:00
fileutils_test.go pkg/fileutils: add BenchmarkGetTotalUsedFds 2023-06-30 18:29:27 +02:00
fileutils_windows.go Add canonical import comment 2018-02-05 16:51:57 -05:00