Checking how many agents are working

How this works
Contact
← Blog
A dark silicon chip drawn as a map, with olive light pooling at one slow spot and a small teal clock beside it, the AMARBARO mark centered.
✎blogbericht

corewait: asking your CPU why a program is slow

The short version

Each instruction row in a corewait table gets one cause, and each of its four probes shows its designed dominant cause on every device it is measured on.

Part 3 of 20 in Tools we built and why. Previous: heaptide: finding the memory a program forgot to give back · Next: wavewait: seeing what a graphics card waits for

What this is about

Your processor is fast, but it spends much of its time waiting. It waits for data to arrive from memory. It waits for a decision about which way a program will go. It waits for the result of a step before it can start the next one. The processor counts these waits, in hardware, as it runs.

corewait reads those counts and tells you, for each instruction of your program, why it was slow. Each instruction gets one cause: memory, branch, dependency, front end, or busy. It runs on Linux desktops and on Android phones.

Why it matters

Say your program is slow and an ordinary profiler tells you one loop is hot. That is a start, but it does not say what to do. If the loop waits for memory, the fix is to change how data is laid out. If it waits for a chain of steps that each need the last result, the fix is to break the chain. These are different jobs. A wrong guess costs a day.

On a phone the choice matters even more, because the memory system is smaller and the battery counts every cycle. A cause beats a guess.

How it works

Here is the idea in four steps.

  1. Record. corewait record runs your program under perf on Linux, or under simpleperf on a phone. It samples cycles and the memory and branch events. It then runs the program a second time to count whole-run totals.
  2. Join. It matches each sample to the machine code, the actual instructions the processor ran.
  3. Blame. A sample lands a few instructions after the one that stalled. So corewait gives the cycles to the nearest earlier load or branch, looking back a few rows. Then the first matching cause wins, in this order: memory, branch, dependency, front end, busy.
  4. Show. corewait show prints one row per instruction with cycles, events, cause and the instruction text.

An example of a rule: a row that reads its own result, such as adding to a running total, counts as a dependency. The row xor eax, eax, which only sets a register to zero, does not.

To keep itself honest, corewait ships probes: four small programs, each built to be slow for one known reason. A memory probe should show memory as its top cause. The claim we test is that each probe shows its designed cause on every device where we measured it. If a probe shows a different cause, the tool is wrong on that device.

How to do it, step by step

The project page lists these commands.

  1. Build the probe for your device: corewait build desktop.
  2. Record your program: corewait record desktop OUT -- BINARY ARGS.
  3. Read the top ten rows with their causes: corewait show OUT --top 10 --causes.
  4. Change your code, record again, and compare with corewait diff OUT_A OUT_B, which shows the cycle change row by row.

Two other recipes are handy. To profile a Python job, record it with --no-stat, as in corewait record desktop OUT --no-stat -- .venv/bin/python job.py. To sample a server that is already running, use --pid SERVER.

There is also a budget command that can fail a build when a kernel's mix of causes drifts. That keeps a fix from silently coming back.

What it does not prove

  • A cause is a rule applied near a position. Treat one row's cause as a pointer, and check the top causes against the probes.
  • The desktop runs at perf's paranoid level 2, so corewait records only user-space events and needs no root.
  • Phones are profiled from a command that the tool starts, through adb. A phone is locked while a recording runs.
  • A BLAS or OpenMP thread pool spins while it waits, and the spin can top a profile with no real work behind it. Set the thread count to 1, or pick the library you care about.
  • Large libraries are slow to disassemble. A 440 MB libtorch library adds about 50 seconds.

A worked picture

Imagine a loop that adds up a long list of numbers. Each trip it fetches one number from memory and adds it to a total. If the list is large, the processor often has to wait for the next number to arrive. corewait would put the cause memory on the load row and a small cause on the add. The fix is then about how the numbers are laid out, not about the adding. If instead the list fits in the fast cache, the same loop may wait on the running total, because each add needs the last result. That would show as a dependency, and the fix would be to keep two or more totals and add them at the end. Same loop, two causes, two different fixes. This is the choice the tool helps you make.

Words you may not know

  • Instruction: one small step a processor performs, such as adding two numbers.
  • Stall: time a processor spends waiting instead of working.
  • Cache: a small, fast memory close to the processor.
  • Hardware counter: a count the processor keeps of events like cache misses.
  • Profiler: a tool that shows where time goes.
  • Disassembly: a readable listing of a program's machine code.
  • Probe: a small test program built to be slow for one known reason.

Try it

  • Read the corewait project page.
  • Clone the code from github.com/amarbarosys/corewait. Its CPU-only check script is ./check.sh; it builds the probe, compares golden fixtures and runs a planted mutant.
  • Pick one slow loop of your own. Record it, read the top causes, and make one change. Then diff.
  • See the companion tool for graphics cards in the next post.

Reacties

Nog geen reacties.

Inloggen om te reageren.