Perf is a profiler tool for Linux based system which can be used to measure system performance. Our recent project requires the usuage of this tool. Mainly installed on regular Linux server and embedded system.
How to install
Examples:
- system 1: Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-131-generic x86_64)
- system 2: Ubuntu 16.04.2 LTS (4.4.77)
check kernel version
$uname -r
(return: 4.4.0-131-generic in system 1)
(or return: 4.4.77 in system 2)
You can also check if perf has been installed in your system by:
check perf version
$perf --version
If you have right one, you will see the version of perf
or by $cd /usr/lib
check if you have ‘linux-tools’ dir. If so, you can $cd linux-tools
to see the perf tool dir like ‘4.4.0-131-generic’. These two ways can be used to check after you install the perf tool.
Install perf tool
case1 (for system 1)
If you don’t have perf on the system, easiest way you can do is:
$sudo apt-get update
$sudo apt-get install linux-tools-common linux-tools-generic linux-tools-`uname -r`
or $sudo apt-get install linux-tools-common linux-tools-generic
If you kernel version and linux-tool-generic version are same, you may be done here. However, most of time, the current update version may not be the same as what required. When you check $perf --version
, you may get WARNING: perf not found for kernel and you may need to install the following packages for this specific kernel: … ,where the right version is required.
You can check if you have the right version as warning list by:
$sudo apt-cache search linux-tools
If not, you can go to search the packages required online, like what I needed for my system is the package linux-tools-4.4.0-131-generic, which I found here, select your architecture type and followed the instruction. You may need to add new mirror site which contained the package required to your apt source list.
$cd /etc/apt/
$vi sources.list
(add sudo
if permission denied)
in sources.list add one line: deb mirrors.kernel.org/ubuntu trusty-updates main
After you update the sources list and you can install the pacakage.
$sudo apt-get install linux-tools-4.4.0-131-generic
Then you can check the perf version perf --version
and you can see the perf tool version without asking to install pacakge and you are all set. You can use your pert tool now.
case 2 (for system 2)
Wait, that is not the end. If returning result of $uname -r
is like this 4.4.77
, the previous way may not work. If you do $sudo apt-get install linux-tools-4.4.77
but cannot get the tool installed. In this case, download the required linux kernel version (our case is v4.4.77) and go to ‘linux->tools->perf’ to install it.
$git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git
After download finished,
$cd linux-stable/
$git checkout v4.4.77
(you can also check your git status git status
)
$cd tools/perf/
$make
(If error came and asked for installation of ‘flex’ and ‘bison’, $sudo apt-get install flex
and $sudo apt-get install bison
)
$sudo cp perf /usr/bin
After doing this, you can try $perf --version
and hope it works for you now.
After make
, you can also try make install
and then check the perf --version
.
Basic usuage
example:
$perf stat -e cache-misses ./test
(./test is your executable file) -cache miss
$perf record -e cache-misses ./test
$perf report
when run perf record -e cache-misses ./test
, you may come with error or warning like this and cannot get perf report:
WARNING: Kernel address maps (/proc/{kallsyms,modules}) are restricted, check /proc/sys/kernel/kptr_restrict.
You can check the kptr_restrict value is 1 and it is read-only file, what you can do is like this:
echo 0 | sudo tee /proc/sys/kernel/kptr_restrict
You can check the value in kptr_restrict has been changed from 1 to 0.
Now you can run $perf record -e cache-misses ./test
and get the perf report then.
More tutorials about perf tool can be found on perf wiki
You may also check another useful Perf-tools on github.
Others: tried the same steps in container, however, cannot do the same perf operations in container.
jovyan@7eeaf145b774:~/work$ perf stat -e cache-misses ./test
Error:
You may not have permission to collect stats.
Consider tweaking /proc/sys/kernel/perf_event_paranoid:
-1 - Not paranoid at all
0 - Disallow raw tracepoint access for unpriv
1 - Disallow cpu events for unpriv
2 - Disallow kernel profiling for unpriv