Installing Perf Performance Analysis Tool on Ubuntu 18.04

|
Published:
|
|

Good day all. This guides is about installing Perf performance analysis tool on Ubuntu 18.04. Perf, also known as perf_events is a powerful performance counter for Linux systems that gathers data about hardware events such as instructions executed, branches mispredicted or cache-misses suffered.

It also provides very low overhead profiling of applications to trace dynamic control flow and identify hotspots, per task, per CPU and per-workload counters, sampling and source code event annotation, dynamic creation of tracepoints using the kprobes and uprobes frameworks, for kernel and userspace dynamic tracing respectively.

Installing Perf Performance Analysis Tool on Ubuntu 18.04

Perf utility is provided by the linux-tools packages that is available on the default Ubuntu repositories. To use perf, you need to install this package by running the command below. Note that in order for perf to function as expected, you need to install the linux-tools for your specific kernel release.

apt install linux-tools-$(uname -r) linux-tools-generic

Once the installation is done, you should be able to have perf utility in your PATH.

which perf
/usr/bin/perf

There you go.

There are quite a number of sub-commands that can be used with perf command. These include;

  • stat: runs a command and gathers performance counter statistics from it.
  • top: generates and displays a performance counter profile in real time.
  • record: runs a command and gathers a performance counter profile from it. The gathered data is saved into perf.data without displaying anything.
  • report: It reads perf.data to display the performance counter profile information recorded via perf record sub-command.
  • list: displays the symbolic event types which can be selected in the various perf commands with the -e option.

The basic synopsis of the perf command is;

perf [--version] [--help] [OPTIONS] COMMAND [ARGS]

To obtain a comprehensive list of sub-commands that can be used with perf command, pass the --help option to perf.

perf --help

To obtain help about a specific sub-command, just execute;

perf help SUB-COMMAND

Next, let us go over a few example usage of the perf command;

To run a command and gather performance statistics, you would run perf as shown below;

perf stat ls -ld /etc/
drwxr-xr-x 87 root root 4096 Feb 21 18:10 /etc/

 Performance counter stats for 'ls -ld /etc/':

          2.485653      task-clock (msec)         #    0.649 CPUs utilized          
                 2      context-switches          #    0.805 K/sec                  
                 0      cpu-migrations            #    0.000 K/sec                  
               127      page-faults               #    0.051 M/sec                  
   <not supported>      cycles                                                      
   <not supported>      instructions                                                
   <not supported>      branches                                                    
   <not supported>      branch-misses                                               

       0.003827501 seconds time elapsed

For a further insight on how to use stat sub-command, check man perf-stat.

To display a performance counter profile in real time, use top sub-command;

perf top
Samples: 71  of event 'cpu-clock', Event count (approx.): 14105457                                                                                             
Overhead  Shared Object      Symbol                                                                                                                            
   9.50%  [kernel]           [k] memcpy
   9.50%  [kernel]           [k] vsnprintf
   6.65%  [kernel]           [k] e1000_alloc_rx_buffers
   5.43%  [kernel]           [k] format_decode
   4.87%  [kernel]           [k] e1000_xmit_frame
   4.07%  [kernel]           [k] kallsyms_expand_symbol.constprop.1
   4.07%  libc-2.27.so       [.] getdelim
   4.07%  perf               [.] 0x00000000001f8804
   3.13%  [kernel]           [k] __softirqentry_text_start
   3.10%  [kernel]           [k] _raw_spin_unlock_irqrestore
   2.71%  [kernel]           [k] number
   2.71%  [kernel]           [k] pointer_string
...

To learn more on this, check man perf-top.

To run a command and record its profile into perf.data, use the record sub-command. The basic syntax of using record command is stated on man perf-record.

perf record [-e <EVENT> | --event=EVENT] [-a] <command>

To list list all pre-defined events, run perf list.

perf list
List of pre-defined events (to be used in -e):

  alignment-faults                                   [Software event]
  bpf-output                                         [Software event]
  context-switches OR cs                             [Software event]
  cpu-clock                                          [Software event]
  cpu-migrations OR migrations                       [Software event]
  dummy                                              [Software event]
  emulation-faults                                   [Software event]
  major-faults                                       [Software event]
  minor-faults                                       [Software event]
  page-faults OR faults                              [Software event]
  task-clock                                         [Software event]

  msr/smi/                                           [Kernel PMU event]
  msr/tsc/                                           [Kernel PMU event]
...
perf record -e cpu-clock -a -g -- sleep 3
[ perf record: Woken up 5 times to write data ]
[ perf record: Captured and wrote 1.329 MB perf.data (8755 samples) ]

Check the man page for the explanation of the options used.

To read the performance record, use the report sub-command. The command syntax is perf report [-i <file> | --input=file]

perf report -i perf.data
Samples: 8K of event 'cpu-clock', Event count (approx.): 2188750000                                                                                            
  Children      Self  Command       Shared Object       Symbol                                                                                                 
+   99.94%     0.00%  swapper       [kernel.kallsyms]   [k] secondary_startup_64
+   99.94%     0.00%  swapper       [kernel.kallsyms]   [k] x86_64_start_kernel
+   99.94%     0.00%  swapper       [kernel.kallsyms]   [k] x86_64_start_reservations
+   99.94%     0.00%  swapper       [kernel.kallsyms]   [k] start_kernel
+   99.94%     0.00%  swapper       [kernel.kallsyms]   [k] rest_init
+   99.94%     0.00%  swapper       [kernel.kallsyms]   [k] cpu_startup_entry
+   99.94%     0.00%  swapper       [kernel.kallsyms]   [k] do_idle
+   99.93%    99.93%  swapper       [kernel.kallsyms]   [k] mwait_idle
+   99.93%     0.00%  swapper       [kernel.kallsyms]   [k] default_idle_call
+   99.93%     0.00%  swapper       [kernel.kallsyms]   [k] arch_cpu_idle
     0.03%     0.00%  kworker/0:1   [kernel.kallsyms]   [k] ret_from_fork
     0.03%     0.00%  kworker/0:1   [kernel.kallsyms]   [k] kthread
     0.03%     0.00%  kworker/0:1   [kernel.kallsyms]   [k] worker_thread
...

Well, this tool is quite comprehensive and we can exhaust all if its usage in a simple introductory guide. Therefore feel free to explore this tool. Consult man pages for an insightful help on varios usage and command options.

You  can also check Perf examples provided here.

Well, this just a little introduction into installing Perf performance analysis tool on Ubuntu 18.04. Enjoy

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
koromicha
I am the Co-founder of Kifarunix.com, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

1 thought on “Installing Perf Performance Analysis Tool on Ubuntu 18.04”

Leave a Comment