TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How does perf work? (in which we read the Linux kernel source)

78 pointsby bartbesabout 9 years ago

7 comments

cthalupaabout 9 years ago
I&#x27;m not an expert, but I believe I can answer some of the questions at the end of the article.<p>&gt;are kprobes and ftrace the same kernel system? I feel like they are but I am confused.<p>They are not. kprobes and kretprobes have been around in the kernel for a lot longer than ftrace, and are exposed to multiple tracing programs. ftrace, perf, bpf, etc, all can make use of kprobes. (ftrace is pretty heavily dependent on kprobes for being useful, kprobes are still useful without ftrace)<p>&gt;what is the relationship between perf and kprobes? if I just want to sample the registers &#x2F; address of the instruction pointer from ls&#x27;s execution, does that have anything to do with kprobes? with ftrace? I think it doesn&#x27;t, and that I only need kprobes if I want to instrument a kernel function (like a system call), but I&#x27;m not sure.<p>Kprobes make a copy of the instruction you are probing, and replaces the first bytes of the instruction with a breakpoint instruction. When the CPU hits this, a trap occurs, and the registers are saved, and passed to the kprobe<p>Perf has the ability to collect CPU performance counters, tracepoints, kprobes, and uprobes. Tracepoints are added to the code of the application - they will include a definition in the header, and the actual tracepoint statement in the code itself. uprobes allow dynamic tracing of user level and library calls. You echo in a probe name, executable location, and offset, and then you can start tracing that probe.
srcmapabout 9 years ago
Nice!<p>Here&#x27;s another way to view&#x2F;understand the kernel and user space code:<p><a href="http:&#x2F;&#x2F;www.srcmap.org&#x2F;sd_share&#x2F;14&#x2F;a2313bc5&#x2F;Cross_Reference_Code_on_Perf_in_Linux_Kernel_and_User_Space.html" rel="nofollow">http:&#x2F;&#x2F;www.srcmap.org&#x2F;sd_share&#x2F;14&#x2F;a2313bc5&#x2F;Cross_Reference_C...</a>
cushychickenabout 9 years ago
&quot;rdpmc&quot; is shorthand for &quot;read performance monitoring register&quot;. It&#x27;s exactly what the author guessed - a register that increments every time an instruction is executed.
chris_wotabout 9 years ago
rr uses perf_events_open - I know because it gives me an error when I run it on VirtualBox... Initially I thought it was a capabilities issue, so I ran it as root. Then it segfaulted.<p>Not sure what&#x27;s going on. I probably need to file an issue.
lttlrckabout 9 years ago
rdpmc will give a cycle count, so presumably perf stat hooks into the scheduler to the maintain the count per process?
nkurzabout 9 years ago
I made a comment directly on the blog post which I&#x27;ll copy here:<p>I&#x27;ve also spent a lot of time recently trying to understand how &#x27;perf&#x27; works at a low level. I&#x27;m getting closer, but a lot of it is still pretty impenetrable. There are four main resources that I&#x27;d recommend.<p>The first, which you&#x27;ve almost surely found, is the &quot;Perf Wiki&quot;: <a href="https:&#x2F;&#x2F;perf.wiki.kernel.org&#x2F;index.php&#x2F;Main_Page" rel="nofollow">https:&#x2F;&#x2F;perf.wiki.kernel.org&#x2F;index.php&#x2F;Main_Page</a> There&#x27;s not a lot there, but it&#x27;s a good introduction.<p>The second, which you might have stumbled across, is the text documentation scattered throughout the kernel source. Most are in <a href="https:&#x2F;&#x2F;github.com&#x2F;torvalds&#x2F;linux&#x2F;tree&#x2F;master&#x2F;tools&#x2F;perf&#x2F;Documentation" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;torvalds&#x2F;linux&#x2F;tree&#x2F;master&#x2F;tools&#x2F;perf&#x2F;Doc...</a>, but the most useful one is up one directory at <a href="https:&#x2F;&#x2F;github.com&#x2F;torvalds&#x2F;linux&#x2F;blob&#x2F;master&#x2F;tools&#x2F;perf&#x2F;design.txt" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;torvalds&#x2F;linux&#x2F;blob&#x2F;master&#x2F;tools&#x2F;perf&#x2F;des...</a>.<p>The third is Andi Kleen&#x27;s PMU Tools: <a href="https:&#x2F;&#x2F;github.com&#x2F;andikleen&#x2F;pmu-tools" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;andikleen&#x2F;pmu-tools</a> The &#x27;jevents&#x27; library within this illustrates how to use &#x27;perf&#x27; to set up the counters while using &#x27;rdpmc&#x27; from userspace to read them.<p>The fourth is Vince Weaver&#x27;s Unofficial Linux Perf Events Web-Page (<a href="http:&#x2F;&#x2F;web.eece.maine.edu&#x2F;~vweaver&#x2F;projects&#x2F;perf_events&#x2F;" rel="nofollow">http:&#x2F;&#x2F;web.eece.maine.edu&#x2F;~vweaver&#x2F;projects&#x2F;perf_events&#x2F;</a>) and his associated Perf Event Testsuite (<a href="https:&#x2F;&#x2F;github.com&#x2F;deater&#x2F;perf_event_tests" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;deater&#x2F;perf_event_tests</a>). Tests make wonderful examples.<p>The deeper I got into it, the more I realized is that &#x27;perf&#x27; is still evolving, and there is a lot of anger and discontent below the surface. There were (and are) competing alternatives, but &#x27;perf&#x27; is politically in control. Much of what you read about &#x27;perf&#x27; should be probably be viewed through the lens of &quot;history written by the victor&quot;, and &quot;the vanquished&quot; may have different perspectives.<p>---<p>Separately, in case anyone is already familiar with the internals, here&#x27;s an aspect where I&#x27;m currently stuck. There is an &quot;offset&quot; field which one is supposed to add to the result read from &quot;rdpmc&quot;, but when I do so I get strange problems: <a href="https:&#x2F;&#x2F;github.com&#x2F;nkurz&#x2F;pmu-tools&#x2F;commit&#x2F;f2ab49207d4c7b7dddf37a00aa293b47c7f7b897" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;nkurz&#x2F;pmu-tools&#x2F;commit&#x2F;f2ab49207d4c7b7ddd...</a>
b3h3mothabout 9 years ago
perf is simply awesome.