perf(userspace/libscap): rewrite /proc/pid/net/{tcp6,udp6,raw6} parsing logic#2816
Conversation
Perf diff from master - unit testsHeap diff from master - unit testsHeap diff from master - scap fileBenchmarks diff from master |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2816 +/- ##
=======================================
Coverage 74.53% 74.53%
=======================================
Files 292 292
Lines 29987 29987
Branches 4660 4660
=======================================
Hits 22350 22350
Misses 7637 7637
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
7f5ecfb to
d2c470b
Compare
…sing logic
The previous logic used 1 single `fopen()` to open the
`/proc/<pid>/net/{tcp6,udp6,raw6}`/`/proc/net/{tcp6,udp6,raw6}` files,
and `fread()` to read the content of the file in a single
heap-allocated buffer of 1MB.
The new parsing logic introduces the following optimizations:
- use `open()` and `read()` to avoid `fopen()` and `fread()`
allocation, buffering and lock acquisition overheads
- try to read the entire file content with a single `read()`. In the
case in which the file content is bigger than the stack-allocated
buffer of 32kB, the unprocessed content (the last truncated line) is
shifted at the beginning of the buffer, and a new read is attempted.
Notice that 32 kB is a good choice for the majority of the use
cases. Each file line is approximately 150 bytes. The following
table estimate how many `read()` system call are issued in the
optimistic case (e.g.: no signals):
- 100 sockets -> ~15 kB -> 1 read()
- 1000 sockets -> ~150 kB -> ~5 read()
- 10000 sockets -> ~1.5 MB -> ~50 read()
Even in the worst scenario, the cost of issuing 50 system call
should be overcome by the cache-friendly accesses using the
stack-allocated buffer.
Signed-off-by: Leonardo Di Giovanna <leonardodigiovanna1@gmail.com>
Signed-off-by: Leonardo Di Giovanna <leonardodigiovanna1@gmail.com>
d2c470b to
98afca3
Compare
|
Fixed and pushed! @terror96 |
terror96
left a comment
There was a problem hiding this comment.
My eyes can't spot more problems: /lgtm
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: ekoops, leogr, terror96 The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
What type of PR is this?
/kind cleanup
Any specific area of the project related to this PR?
/area libscap
Does this PR require a change in the driver versions?
What this PR does / why we need it:
The previous logic used 1 single
fopen()to open the/proc/<pid>/net/{tcp6,udp6,raw6}//proc/net/{tcp6,udp6,raw6}files, andfread()to read the content of the file in a single heap-allocated buffer of 1MB.The new parsing logic introduces the following optimizations:
parse_procfs_proc_pid_socket_table_file()for efficiently read the socket table file content and (this uses lighteropen()andread()system calls, a stack-allocated for cache locality and a sliding window logic for handling truncated lines).parse_ipv6_socket_table_line()uses light helper likestr_scan_u64(),scan_ipv6_socket_table_address()andmemchr(); moreover, avoid polluting the cache by repeatedly write on the line buffer while parsing the addresses and portsWhich issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
/milestone 0.24.0
Does this PR introduce a user-facing change?: