Some Bits: Nelson's log

Rudimentary perf notes

Still working my way through learning ARM64; I can write loops now! I have my first “real” program, something which takes a 64 bit number and prints it out in hexadecimal. This got me interested in how the CPU is really running my code, which leads me to the perf tool in the Linux kernel. This is an strace or prof-like tool that monitors CPU performance counters, of which ARM64 has an awful lot.

Notes here are from a rank beginner; anyone who’s used perf before will not learn anything here.

I installed it on Raspberry Pi OS with apt install linux-perf. That installs perf_5.10 which you need to invoke with the version number; the perf shell script wrapper is trying to run perf-5.15 to match my kernel version.

$ perf_5.10 stat ./ch4printword

 Performance counter stats for './ch4printword':

              0.22 msec task-clock:u              #    0.196 CPUs utilized
                 0      context-switches:u        #    0.000 K/sec
                 0      cpu-migrations:u          #    0.000 K/sec
                 2      page-faults:u             #    0.009 M/sec
             1,611      cycles:u                  #    0.007 GHz
               167      instructions:u            #    0.10  insn per cycle
   <not supported>      branches:u
                13      branch-misses:u

       0.001135464 seconds time elapsed

       0.000000000 seconds user
       0.001426000 seconds sys

Neat! A little surprised this worked as me; let’s try it as root

$ sudo perf_5.10 stat ./ch4printword 2>&1 | grep cycles
           318,276      cycles                    #    1.720 GHz
$ sudo perf_5.10 stat --all-user ./ch4printword 2>&1 | grep cycles
             1,132      cycles                    #    0.006 GHz

Running perf as root also lets you see all the cycles the kernel used, which for my toy program is most of it. Presumably overhead setting up the process? The --all-user flag restricts to only show user time. Or alternately, don’t run as root.

Note the number of cycles it takes varies each time I run it. Why? Not sure, but I assume it has to do with some unpredictable interaction with other system activity and/or the way the process got created. But while cycles varies, instructions don’t. My program is deterministic.

$ for i in $(seq 1 5); do perf_5.10 stat ./ch4printword 2>&1 | grep cycles; done
             1,288      cycles:u                  #    0.004 GHz
             1,034      cycles:u                  #    0.004 GHz
             1,376      cycles:u                  #    0.005 GHz
             1,039      cycles:u                  #    0.004 GHz
               957      cycles:u                  #    0.005 GHz

perf supports running a job a bunch of times and showing average and SD

$ perf_5.10 stat -r 1000  ./ch4printword > /dev/null

 Performance counter stats for './ch4printword' (1000 runs):

              0.18 msec task-clock:u        #    0.145 CPUs utilized ( +-  0.67% )
             1,130      cycles:u            #    0.006 GHz  ( +-  0.58% )
               167      instructions:u      #    0.15  insn per cycle
                 6      branch-misses:u     ( +-  1.62% )

        0.00126752 +- 0.00000893 seconds time elapsed  ( +-  0.70% )

perf by default is printing counters for events it thinks I care about. You can see all possible events with perf list. One chunk are performance counters from the armv8_cortex_a72 itself. There’s a lot of data here, like cache misses and bus access and stuff. I was interested in branch prediction, so

$ perf_5.10 stat -r 1000 -e mem_access,cpu_cycles,instructions,br_pred,br_mis_pred  ./ch4printword > /dev/null

 Performance counter stats for './ch4printword' (1000 runs):

                20      mem_access:u                              ( +-  0.21% )
               985      cpu_cycles:u                              ( +-  0.72% )
               167      instructions:u
                63      br_pred:u                                 ( +-  0.22% )
                 5      br_mis_pred:u                             ( +-  1.81% )

        0.00122416 +- 0.00000844 seconds time elapsed  ( +-  0.69% )

One small mystery; my program has 32-48 branch instructions executed. why is br_pred bigger than 48? Perhaps this has to do with speculative execution?

167 instructions in 1000ish cycles is not very good. I thought ARM was RISCy enough you could expect 1 instruction per cycle but in reality it’s way more complicated. The Cortex-A72 is a superscalar CPU with a 15 stage pipeline and can be running five instructions at once! But this is a very short program so maybe not the best test.

I did some quick tries with perf on CPU-bound jobs.

  • 1.10 instructions / cycle: stress -c 1 -t 5:
  • 0.94: zstd -19
  • 0.44: zstd -3
  • 0.89: gzip

Those last three are I/O intensive so probably not a great test.

This blog post on performance tuning demonstrates 2.053 instructions / cycle for matrix multiplication. It also says “In scientific code with a mix of integer and floating point operations, an IPC of 2 is a good starting goal.”

Learning all this reminds me how complex micro-optimization is. Particularly for C code; you’re having to not only predict what the CPU is doing but also what the C optimizer is capable of emitting. I’d naively thought programming in assembly would make it easier to get some highly optimized code for things but I’m quickly discovering it’s so complex you probably get better results letting a compiler do the real work.