If you read this blog, you might know that I love strace, so much that I wrote a zine about it. But strace has not ever been able to solve all my problems -- it only tells me about system calls, it can slow down my code up to 50x. Not ideal!
perf. We learned about perf a couple of years ago, when we found out that it can tell you about how many CPU cycles your program is using. That was cool, but ultimately not that useful to me right now (I'm trying to make Ruby programs fast!).
But! A couple of months ago I learned about
perf top, which tells you which functions are in use on your computer right now (the same way
top does for programs). I have three stories for you about how perf top is the best and has helped me solve problems.
First, I'm going to show you what
sudo perf top gives me on my computer right now:
You can see that some mystery function in Chrome is responsible for 26% of CPU usage, perf itself has some overhead,
MusicManager_x86_64 is using sqlite (what? why?), and there are various things going on inside my kernel. Neat.
Now, let's see some real examples of perf in action!
the case of the sad Node program
I needed to debug a node program recently. It was super slow, and spending a ton of time on the CPU. I had no idea why.
Since my new hobby is to run
perf top any time I have a question about CPU usage, I ran perf! I don't have the real results here, but I basically saw something like this:
I didn't know what all of it meant, but it seemed clear that the program was spending most of its time doing garbage collection. No good! We didn't manage to figure out why it was garbage collecting, but it was awesome to be able to quickly triage what was going on.
the case of the swapping computer
Today at work, I had a program that was slow. Surprise! A lot of stories these days start this way. The computer was using a lot of CPU, and I wanted to know why.
perf top had to say about that:
This is a totally different story from our node story -- in this case, the linux kernel is using all our CPU. What?! Why?
It turns out that the computer was swapping its memory to disk, and that the swap partition was encrypted. So every time it saved memory to disk or read it back, it needed to encrypt and decrypt all that memory. The CPU load on that machine was like 15. It was having a bad day.
We fixed the memory usage on the machine, and everything was all better ❤.
the case of the HTTP request
Our last case is a Python mystery! This one is a fake mystery that I made up, but it illustrates a real possible slow program.
So! I ran a Python program to download several files, and it used 100% of my CPU for several seconds. What was it doing? Let's ask perf top!!
It seems to be doing... a lot of crypto? Why? Here's the program:
It turns out that opening a HTTPS connection is pretty slow! You need to spend a bunch of time in crypto functions in order to be secure. And perf tells us immediately that that's what's going on! Awesome.
But there's more
So I've gone through a few examples of how perf can sometimes help triage what a program is spending its CPU time on, even if the program is written in node or Python or something.
There's a limitation here that you may have noticed -- perf will only tell us about C functions (like
EVP_DecodeUpdate or something).
perf is even more magical than you might expect, though! You can tell perf about your Java and node functions. This blew my mind when I learned it and is continuing to blow my mind. If you want to make perf amazing for Java, read this blog post Java in flames
Brendan Gregg's page on perf has more about perf and how great it is and how to use it to help debug your node.js programs.
a toolbox of delightful tools
I'm slowly building a toolbox of easy-to-use tools that will help me understand what my programs are doing. There are a few things that are farther down the list (ftrace and systemtap are still very confusing to me and I do not know how to use them.)
perf top is so simple (just one command!), and so straightforward, and I think it deserves to go in your toolbox. It works on Linux. Try it out! See what happens! Run it everywhere! It's safe to run in production.
sudo perf top