<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Bpf on Besterry — Linux &amp; DevOps Notes</title><link>https://besterry.com/tags/bpf/</link><description>Recent content in Bpf on Besterry — Linux &amp; DevOps Notes</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Thu, 02 May 2024 00:00:00 +0000</lastBuildDate><atom:link href="https://besterry.com/tags/bpf/index.xml" rel="self" type="application/rss+xml"/><item><title>Useful bpftrace One-Liners for System Debugging</title><link>https://besterry.com/posts/bpftrace-oneliners/</link><pubDate>Thu, 02 May 2024 00:00:00 +0000</pubDate><guid>https://besterry.com/posts/bpftrace-oneliners/</guid><description>&lt;p&gt;bpftrace makes the kernel event space accessible from a bash one-liner. Here are the scripts I keep reaching for.&lt;/p&gt;
&lt;h2 id="count-syscalls-by-process"&gt;Count syscalls by process&lt;/h2&gt;
&lt;pre&gt;&lt;code&gt;bpftrace -e 'tracepoint:raw_syscalls:sys_enter { @[comm] = count(); }'
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id="distribution-of-file-read-sizes"&gt;Distribution of file read sizes&lt;/h2&gt;
&lt;pre&gt;&lt;code&gt;bpftrace -e 'tracepoint:syscalls:sys_enter_read { @ = hist(args-&amp;gt;count); }'
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id="tcp-retransmissions-by-remote-address"&gt;TCP retransmissions by remote address&lt;/h2&gt;
&lt;pre&gt;&lt;code&gt;bpftrace -e '
kprobe:tcp_retransmit_skb {
$sk = (struct sock *)arg0;
$daddr = $sk-&amp;gt;__sk_common.skc_daddr;
@[ntop($daddr)] = count();
}'
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id="process-creation-stream"&gt;Process creation stream&lt;/h2&gt;
&lt;pre&gt;&lt;code&gt;bpftrace -e 'tracepoint:sched:sched_process_exec { printf(&amp;quot;%s\n&amp;quot;, str(args-&amp;gt;filename)); }'
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id="when-to-use-bpftrace-vs-perf-vs-strace"&gt;When to use bpftrace vs perf vs strace&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;strace: simple, but adds significant overhead. Fine for debugging a single misbehaving process.&lt;/li&gt;
&lt;li&gt;perf: best for sampling-based profiling (CPU time, cache misses). Low overhead.&lt;/li&gt;
&lt;li&gt;bpftrace: best for event-driven tracing across the whole system. Tiny overhead if used sparingly.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All three should be in your toolbox.&lt;/p&gt;</description></item></channel></rss>