Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 06 · How Computers Work · Build the machine's software · about 17 min to read

Play it as a journeyTutor comes with a plan

Watching a live machine: perf, strace and flame graphs

Processes Threads and the Kernel
Processes Threads and the Kernel · Open board

: a running program on one side, the kernel on the other, and the doors between them. This lesson watches both sides of that line while the program is alive.

Brendan Gregg was looking at a MySQL database that used more processor time than it should. He had the usual tools. They gave him what he later called walls of text: page after page of lines, and no way to see which few lines mattered. So he built a new picture from the same data, and he released it in December 2011 as the flame graph. One page of coloured boxes now answers the question he could not answer from the text.

By the end of this lesson you will be able to take a slow running program, find where its time goes with a sampling profile and a flame graph, and find what it asks the kernel for with strace.

The history (optional)

Gregg’s own page on flame graphs says he invented them while working on a MySQL performance problem. Regular profilers and tracers had produced walls of text. He first traced every function call, but the cost of tracing was too high and the output too dense. He switched to taking samples on a timer, dropped time from the sideways axis, and sorted the samples so that matching boxes merged. He chose warm colours, and because the result looked like flames, the name stuck. The page says flame graphs were released in December 2011. He wrote the idea up as “The Flame Graph” in Communications of the ACM in May 2016. Nothing later in this lesson depends on this history.

Before you start. Processes, Threads & the Kernel gave you the process, the kernel, and the system call: the request a program makes when it needs the kernel to do something for it. Everything is a file descriptor gave you the small numbers (file descriptors) that a program passes to read and write. The tap on the shoulder showed the kernel interrupting a running program with a signal, and this lesson uses that same tap to take samples. From text to machine code: compilers, interpreters and the JIT showed how a line of code becomes steps, so you know that a call to a function is one step inside another function.

Lesson 33 of 41 in How Computers Work

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • A sample: one look at where the program is right now
  • Self time and total time: counting each function twice
  • A flame graph: the stacks drawn so that wide means expensive
  • strace: the other view, every request to the kernel
  • On the processor and off it
  • How the profile above was taken
  • A sampler can be wrong: the bias I measured
  • perf: the same idea, for any program, from the kernel
  • strace: options that matter
  • ltrace, and what it is for
  • Which tool when

Terms it explains: sample, stack, sampling profile, total time, self time, flame graph, strace, on-CPU, off-CPU, folded stack, sampling bias, context switch, fork/exec, init/systemd, kernel, MMU + page table, namespaces + cgroups, page (~4KB) / frame, page fault, PID, preemption, process, scheduler, segfault, swap, syscall, thread, user vs kernel mode (ring 3/0), virtual memory.

Lab

The program needs twelve pages at once. What happens when it gets eleven frames?

Drag Frames of real memory from 24 down to 11, then back to 12, and watch Time spent working.

Open The working set

Three long jobs are running when you press a key. How long until the keypress gets the CPU?

Click Take turns, drag How long one turn lasts from 100 ms down to 1 ms, and watch The keypress waits.

Open The time slice

How many workers should a pool have, and why does the answer flip when the tasks mostly wait?

Drag Workers on shift down to the core count, then push Share of each task spent waiting to 90% and watch Finished each second.

Open The thread pool

Related papers

Review How Computers Work