# Linux Perf

## In this learning path

- [Introduction](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/rpi/)
- [Introduction to the Raspberry Pi 4](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/rpi/intro/)
- [Setup a Raspberry Pi 4 and an Arm cloud instance](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/rpi/setup/)
- [Identifying the hardware](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/rpi/id/)
- [Linux Kernel Compile](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/rpi/kernel/)
- [TensorFlow](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/rpi/tf/)
- [Docker](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/rpi/docker/)
- [Linux Perf](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/rpi/perf/)
- [Next Steps](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/rpi/_next-steps/)

Linux software developers use `perf` to analyze performance.

Install and enable `perf` using the commands below. Make sure to become root where indicated.

For Ubuntu running on the Arm server run this command to install `perf`:

```
sudo apt install linux-tools-common linux-tools-generic linux-tools-`uname -r` -y
```

For the Raspberry Pi 4 run this command:

```
sudo apt-get install linux-perf -y
```

Become root to enable access to the perf data:

```
sudo su -
```

As root run the commands shown:

```
echo -1 > /proc/sys/kernel/perf_event_paranoid
echo 0 > /proc/sys/kernel/kptr_restrict
```

Exit root:

```
exit
```

If you don’t have the Linux source tree used in the [Linux Kernel Compile section](https://learn.arm.com/learning-paths/embedded-and-microcontrollers/rpi/kernel/), retrieve it again using git:

```
git clone --depth=1 https://github.com/raspberrypi/linux
```

In some cases, `perf` may not work on the Raspberry Pi 4 due to a mismatch between your running kernel and the `linux-perf` Debian package. To overcome this, build `perf` from source code.

Install the required tools for kernel building:

```
sudo apt install git bc bison flex libssl-dev make
```

Build `perf` using `make`:

```
cd linux
make -C tools/perf/
```

The `perf` command should now be available in the directory `$HOME/linux/tools/perf`.

To list the available `perf` events:

```
perf list
```

If you built `perf` from source in `$HOME/linux` list the events using:

```
$HOME/linux/tools/perf/perf list
```

Linux `perf` works on both the Arm server and the Raspberry Pi 4. Arm servers have additional perf events because of the different processor type.

An example event which is included on the Arm server is `stalled-cycles-backend`. This event is not available on the Raspberry Pi 4.

Run a `tar` command to compress the Linux kernel source tree and count the `stalled-cycles-backend` event.

On the Arm server, run `perf` on a `tar` command to see the events:

```
perf stat -e stalled-cycles-backend tar cfz test.tgz ./linux/
```

The expected output should be similar to:

```
__output__ Performance counter stats for 'tar cfz test.tgz ./linux/':
__output__
__output__ 27,701,570,350      stalled-cycles-backend    #    0.00% backend cycles idle
__output__
__output__  44.461783841 seconds time elapsed
__output__
__output__  43.283230000 seconds user
__output__   2.121127000 seconds sys
```

The Raspberry Pi doesn’t have the `stalled-cycles-backend` event.

```
perf stat -e stalled-cycles-backend tar cfz test.tgz ./linux/
```

The expected output shows the `stalled-cycles-backend` is not available.

```
__output__ Performance counter stats for 'tar cfz test.tgz ./linux/':
__output__
__output__ <not supported>      stalled-cycles-backend
__output__
__output__  143.177396310 seconds time elapsed
__output__
__output__  129.623757000 seconds user
__output__    8.292624000 seconds sys
```

Linux `perf` is available on the Arm server and the Raspberry Pi 4. The events are slightly different due to the processor, but `perf` generally works the same on both.
