在橡树岭国家实验室举办的性能调优教程
slidenumbers: true
Nick Thompson
^ Thanks for coming. First let's give a shoutout to Matt Wolf for putting this tutorial together, and to Barney Maccabe for putting the support behind it to make it happen.
Session 1: Using perf
$ git clone https://github.com/NAThompson/performance_tuning_tutorial
$ cd performance_tuning_tutorial
$ make
$ ./dot 100000000^ This is a tutorial, so definitely follow along. I will be pacing this under the assumption you will be following along, so you'll get bored if you're watching. In addition, at the end of the tutorial we'll do a short quiz, not to stress anyone out, but to solidify the concepts. I hope that'll galvanize us to bring a bit more intensity than usually brought to a six hour training session! If the stakes are too low, we're just gonna waste two good mornings.
^ Please get the notes from github, and attempt to issue the commands.
perf?perf: Ubuntu$ sudo apt install linux-tools-common
$ sudo apt install linux-tools-generic
$ sudo apt install linux-tools-`uname -r`^ Installation is pretty easy on Ubuntu.
perf: CentOS$ yum install perfperf:perf is available on Summit (summit.olcf.ornl.gov), Andes (andes.olcf.ornl.gov) and the SNS nodes (analysis.sns.gov)
I have verified that all the commands of this tutorial work on Andes.
perf: Source build$ git clone --depth=1 https://github.com/torvalds/linux.git
$ cd linux/tools/perf;
$ make
$ ./perf^ I like doing source builds of perf. Not only because I often don't have root, but also because perf improves over time, so I like to get the latest version. For example, new hardware counters were recently added for the Power9 architecture.
A source build is the first step to owning your tools, and will help us all be on the same page.
$ sudo apt install -y bison flex libslang2-dev systemtap-sdt-dev \
libnuma-dev libcap-dev libbabeltrace-ctf-dev libiberty-dev python-devperf_permissions.sh#!/bin/bash
# Taken from Milian Wolf's talk "Linux perf for Qt developers"
sudo mount -o remount,mode=755 /sys/kernel/debug
sudo mount -o remount,mode=755 /sys/kernel/debug/tracing
echo "0" | sudo tee /proc/sys/kernel/kptr_restrict
echo "-1" | sudo tee /proc/sys/kernel/perf_event_paranoid
sudo chown `whoami` /sys/kernel/debug/tracing/uprobe_events
sudo chmod a+rw /sys/kernel/debug/tracing/uprobe_events^ If we have root, we have the ability to extract more information from perf traces. Kernel debug symbols are a nice to have, but not a need to have, so if you don't have root, don't fret too much.
perf MWE…^ This is the perf "hello world". You might see something a bit different depending on your architecture and perf version.
perf?There are lots of great performance analysis tools (Intel VTune, Score-P, tau, cachegrind), but my opinion is that perf should be the first tool you reach for.
perf?sshperf?^ I was trained in mathematics, and I love learning math because it feels permanent. The situation in computer science is much worse. For example, if no one decides to write a Fortran compiler that targets the new Apple M1 chip, there's no Fortran on the Apple M1! So learning tools which will last is important to me.
^ perf is part of the linux kernel, so it has credibility that it will survive for a long time. It also works on any architecture Linux compiles on, so it's widely available. As a sampling profiler, it relies on statistics, not a model of your program.
perf?src/mwe.cpp…$ perf list List of pre-defined events (to be used in -e):
branch-misses [Hardware event] cache-misses [Hardware event] cache-references [Hardware event] instructions [Hardware event] task-clock [Software event]
L1-dcache-load-misses [Hardware cache event] L1-dcache-loads [Hardware cache event] LLC-load-misses [Hardware cache event] LLC-loads [Hardware cache event]
cache-misses OR cpu/cache-misses/ [Kernel PMU event] cache-references OR cpu/cache-references/ [Kernel PMU event] power/energy-cores/ [Kernel PMU event] power/energy-pkg/ [Kernel PMU event] power/energy-ram/ [Kernel PMU event]
^ Every architecture has a different set of PMCs, so this list will be different for everyone. I like the `power` measurements, since speed is not the only sensible objective we might want to pursue.
---
## Custom events
perf stat -e instructions,cycles,L1-dcache-load-misses,L1-dcache-loads,LLC-load-misses,LLC-loads ./dot 100000000 a.b = 9.99999e+07
Performance counter stats for './dot 100000000':
8,564,368,466 instructions:u # 1.41 insn per cycle (49.98%)
6,060,955,584 cycles:u (66.65%)
34,089,080 L1-dcache-load-misses:u # 0.90% of all L1-dcache hits (83.34%)
3,805,929,303 L1-dcache-loads:u (83.32%)
854,522 LLC-load-misses:u # 39.87% of all LL-cache hits (33.31%)
2,143,437 LLC-loads:u (33.31%)
5.045450844 seconds time elapsed
2.856660000 seconds user
2.185739000 seconds sys…$ perf list
cycle_activity.stalls_l1d_pending
[Execution stalls due to L1 data cache misses]
cycle_activity.stalls_l2_pending
[Execution stalls due to L2 cache misses]
cycle_activity.stalls_ldm_pending
[Execution stalls due to memory subsystem]
$ perf stat -e cycle_activity.stalls_ldm_pending,cycle_activity.stalls_l2_pending,cycle_activity.stalls_l1d_pending,cycles ./dot 10000000
a.b = 9.99999e+07
Performance counter stats for './dot 100000000':
509,998,525 cycle_activity.stalls_ldm_pending:u
127,137,070 cycle_activity.stalls_l2_pending:u
70,555,574 cycle_activity.stalls_l1d_pending:u
5,708,220,052 cycles:u
3.637099623 seconds time elapsed
2.463966000 seconds user
1.172459000 seconds sys
---
## Kinda painful typing these events: Use `-d` (`--detailed`)
$ perf stat -d ./dot 100000000 Performance counter stats for './dot 100000000':
1,945.17 msec task-clock:u # 0.970 CPUs utilized
0 context-switches:u # 0.000 K/sec
0 cpu-migrations:u # 0.000 K/sec
390,463 page-faults:u # 0.201 M/sec
3,329,516,701 cycles:u # 1.712 GHz (49.97%)
1,272,884,914 instructions:u # 0.38 insn per cycle (62.50%)
150,445,759 branches:u # 77.343 M/sec (62.55%)
14,766 branch-misses:u # 0.01% of all branches (62.53%)
76,672,490 L1-dcache-loads:u # 39.417 M/sec (62.53%)
51,315,841 L1-dcache-load-misses:u # 66.93% of all L1-dcache hits (62.52%)
7,867,383 LLC-loads:u # 4.045 M/sec (49.94%)
7,618,746 LLC-load-misses:u # 96.84% of all LL-cache hits (49.96%)
2.005801176 seconds time elapsed
0.982545000 seconds user
0.963534000 seconds sys
---
## `perf stat -d` output on Andes
[nthompson@andes-login1]~/performance_tuning_tutorial% perf stat -d ./dot 1000000000 a.b = 1e+09
Performance counter stats for './dot 1000000000':
2,242.43 msec task-clock:u # 0.999 CPUs utilized
0 context-switches:u # 0.000 K/sec
0 cpu-migrations:u # 0.000 K/sec
8,456 page-faults:u # 0.004 M/sec
2,972,264,893 cycles:u # 1.325 GHz (29.99%)
1,366,982 stalled-cycles-frontend:u # 0.05% frontend cycles idle (30.02%)
747,429,126 stalled-cycles-backend:u # 25.15% backend cycles idle (30.07%)
3,499,896,128 instructions:u # 1.18 insn per cycle
# 0.21 stalled cycles per insn (30.06%)
749,888,957 branches:u # 334.410 M/sec (30.02%)
9,206 branch-misses:u # 0.00% of all branches (29.98%)
1,108,395,106 L1-dcache-loads:u # 494.284 M/sec (29.97%)
36,998,921 L1-dcache-load-misses:u # 3.34% of all L1-dcache accesses (29.97%)
0 LLC-loads:u # 0.000 K/sec (29.97%)
0 LLC-load-misses:u # 0.00% of all LL-cache accesses (29.97%)
2.244079417 seconds time elapsed
1.000742000 seconds user
1.214037000 seconds sys
^ Lots of backend cycles stalled in this one. This could be from high latency operations like divisions or from slow memory accesses.
---
## `perf stat` on a different type of computation
[nthompson@andes-login1]~/performance_tuning_tutorial% perf stat -d git archive --format=tar.gz --prefix=HEAD/ HEAD > HEAD.tar.gz
Performance counter stats for 'git archive --format=tar.gz --prefix=HEAD/ HEAD':
99.81 msec task-clock:u # 0.795 CPUs utilized
0 context-switches:u # 0.000 K/sec
0 cpu-migrations:u # 0.000 K/sec
1,408 page-faults:u # 0.014 M/sec
276,165,489 cycles:u # 2.767 GHz (28.07%)
72,227,873 stalled-cycles-frontend:u # 26.15% frontend cycles idle (28.36%)
60,614,109 stalled-cycles-backend:u # 21.95% backend cycles idle (29.37%)
394,352,577 instructions:u # 1.43 insn per cycle
# 0.18 stalled cycles per insn (30.58%)
66,882,750 branches:u # 670.113 M/sec (31.95%)
2,974,856 branch-misses:u # 4.暂无开放 Issues,或尚未同步最近议题。