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
[24.0 backport] pkg/fileutils: GetTotalUsedFds: reduce allocations #45856
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cd44aba
[24.0] pkg/fileutils: switch to use containerd log pkg
thaJeztah 01eb483
pkg/fileutils: GetTotalUsedFds(): don't pretend to support FreeBSD
thaJeztah 5dcea89
pkg/fileutils: add BenchmarkGetTotalUsedFds
thaJeztah bb50485
pkg/fileutils: GetTotalUsedFds: reduce allocations
thaJeztah aace62f
pkg/fileutils: GetTotalUsedFds(): use fast-path for Kernel 6.2 and up
thaJeztah File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package fileutils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/containerd/containerd/log" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// GetTotalUsedFds Returns the number of used File Descriptors by | ||
// reading it via /proc filesystem. | ||
func GetTotalUsedFds() int { | ||
name := fmt.Sprintf("/proc/%d/fd", os.Getpid()) | ||
|
||
// Fast-path for Linux 6.2 (since [f1f1f2569901ec5b9d425f2e91c09a0e320768f3]). | ||
// From the [Linux docs]: | ||
// | ||
// "The number of open files for the process is stored in 'size' member of | ||
// stat() output for /proc/<pid>/fd for fast access." | ||
// | ||
// [Linux docs]: https://docs.kernel.org/filesystems/proc.html#proc-pid-fd-list-of-symlinks-to-open-files: | ||
// [f1f1f2569901ec5b9d425f2e91c09a0e320768f3]: https://github.com/torvalds/linux/commit/f1f1f2569901ec5b9d425f2e91c09a0e320768f3 | ||
var stat unix.Stat_t | ||
if err := unix.Stat(name, &stat); err == nil && stat.Size > 0 { | ||
return int(stat.Size) | ||
} | ||
|
||
f, err := os.Open(name) | ||
if err != nil { | ||
log.G(context.TODO()).WithError(err).Error("Error listing file descriptors") | ||
return -1 | ||
} | ||
defer f.Close() | ||
|
||
var fdCount int | ||
for { | ||
names, err := f.Readdirnames(100) | ||
fdCount += len(names) | ||
if err == io.EOF { | ||
break | ||
} else if err != nil { | ||
log.G(context.TODO()).WithError(err).Error("Error listing file descriptors") | ||
return -1 | ||
} | ||
} | ||
// Note that the slow path has 1 more file-descriptor, due to the open | ||
// file-handle for /proc/<pid>/fd during the calculation. | ||
return fdCount | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surprised you used Sprintf here :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha, yeah I was doubting, but in the total scheme of things, it was slightly more readable than strconv and path.Join combined