> I ran benchmarks on current code in the datadog-agent which reads the relevant data from procfs as described at the beginning of this post. I then implemented benchmarks for capturing the same data with bpf. The performance results were a major improvement.
> On a linux system with around 250 Procs it took the procfs implemention 5.45 ms vs 75.6 us for bpf (bpf is ~72x faster). On a linux system with around 10,000 Procs it took the procfs implemention ~296us vs 3ms for bpf (bpf is ~100x faster).
I use early bailout a lot (in 0x.tools xcapture) when iterating through all threads in a system and determining which ones are “active” or interesting
# Find processes connected to a specific port
psc 'socket.dstPort == uint(443)'
# Filter by PID range
psc 'process.pid > 1000 && process.pid < 2000'
It seems weird to require the user to remember that ports have to be marked uint when it doesn't look like anything else does.But, yeah, this could be solved if uint promoted to larger for the comparison.
- grep /proc/*/maps for " (deleted)" (needs root)
- exclude irrelevancies like paths starting with "/memfd:" (I have lots of other similar exclusions) with grep -v
- extract the pid from the filename part of grep's output with sed
- for each pid, generate readable output from /proc/$pid/cmdline (which is NUL separated) with tr, xargs, bash printf
- show the pid, cmdline, file path
Yes, this is what needs-restarting does too.
With this tool, this pipe chain is now just:
doas psc -o "process.pid,process.cmdline,file.path" \
'file.path.endsWith(" (deleted)") && !file.path.startsWith("/memfd:") && !...' \
| sed 1dNice use of CEL too. Neat all around.
ps aux | grep nginx | grep root | grep -v grep
can be done instead (from memory, not at a Linux machine ATM): ps -u root -C nginx
which is arguably better than their solution: psc 'process.name == "nginx" && process.user == "root"'Should you for some reason want to do the former, this is easiest done using:
pgrep -u root -f nginx
which exists on almost all platforms, with the notable exception of AIX.Their other slightly convoluted example is:
psc 'socket.state == established && socket.dstPort == uint(443)'
which is much more succinct with: lsof -i :443 -s TCP:ESTABLISHEDss -o state established '( dport = :ssh or sport = :ssh )'
Is there a trade off here?