Repository navigation
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
processStatsFromProcs walks /proc/<pid>/fd for every pid in the cgroup on every housekeeping tick and resolves each link to count sockets. Per fd it paid for os.ReadDir (sorts entries, allocates a DirEntry each), path.Join (path.Clean per call) and os.Readlink (a heap string per link target). On a host exporting a few hundred containers this function dominated the exporter's CPU profile, and most of the cost was this bookkeeping rather than the getdents64/readlinkat syscalls themselves. Read the directory with Readdirnames(-1) (unsorted, names only) and resolve links with unix.Readlinkat into one reused 64-byte buffer, comparing the "socket" prefix in place. Counts, error handling and log lines are unchanged; a synthetic /proc tree test asserts process/fd/socket counts. BenchmarkProcessStatsFromProcs on a synthetic tree (same syscall sequence as a real host, minus procfs kernel cost), master vs this change: shape ns/op B/op allocs/op pids=1 / fds=128 299 us -> 146 us 65 KB -> 7 KB 911 -> 272 pids=8 / fds=512 10.6 ms -> 4.9 ms 2.1 MB -> 185 KB 28.7k -> 8.2k pids=32 / fds=2048 187 ms -> 99 ms 36 MB -> 3.9 MB 459k -> 131k
lollllcat
force-pushed
the
fd-readlinkat
branch
from
October 26, 2026 16:16
c57738f to
5ff642c
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
processStatsFromProcswalks/proc/<pid>/fdfor every pid in the cgroup on every housekeeping tick and resolves each link to count sockets. Per fd, the current loop pays for:os.ReadDir, which sorts the entries and allocates aDirEntryper fd;path.Join(dirPath, fd.Name()), which runspath.Cleanon every call;os.Readlink, which allocates a heap string per link target.On hosts exporting a few hundred containers with socket-heavy processes this function dominated the exporter's CPU profile, and most of the cost was this bookkeeping rather than the
getdents64/readlinkatsyscalls themselves.This PR reads the directory with
Readdirnames(-1)(unsorted, names only) and resolves links withunix.Readlinkatinto one reused 64-byte buffer, comparing the"socket"prefix in place. Counts, error handling and log lines are unchanged.Testing
TestProcessStatsFromProcsSyntheticbuilds a synthetic/proctree and asserts process / fd / socket counts.BenchmarkProcessStatsFromProcsagainst the same tree (identical syscall sequence to a real host, minus procfs kernel cost). master vs this branch on the same machine:On real hosts the syscalls are a larger share than on tmpfs, so the wall-clock win is smaller than the benchmark's ~50%, but the ~90% drop in allocations also removes GC pressure.
go test -race ./lib/container/libcontainer/...passes.