{"id":2258,"date":"2019-02-21T22:19:37","date_gmt":"2019-02-21T19:19:37","guid":{"rendered":"http:\/\/kifarunix.com\/?p=2258"},"modified":"2019-02-21T22:19:37","modified_gmt":"2019-02-21T19:19:37","slug":"installing-perf-performance-analysis-tool-on-ubuntu-18-04","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/installing-perf-performance-analysis-tool-on-ubuntu-18-04\/","title":{"rendered":"Installing Perf Performance Analysis Tool on Ubuntu 18.04"},"content":{"rendered":"<p>Good day all. This guides is about installing Perf performance analysis tool on Ubuntu 18.04. Perf, also known as <code>perf_events<\/code> is a powerful performance counter for Linux systems that gathers data about hardware events such as instructions executed, branches mispredicted or cache-misses suffered.<\/p>\n<p>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.<\/p>\n<h3>Installing Perf Performance Analysis Tool on Ubuntu 18.04<\/h3>\n<p>Perf utility is provided by the <code>linux-tools<\/code> 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 <code>linux-tools<\/code> for your specific kernel release.<\/p>\n<pre>apt install linux-tools-$(uname -r) linux-tools-generic<\/code><\/pre>\n<p>Once the installation is done, you should be able to have perf utility in your PATH.<\/p>\n<pre>which perf\r\n\/usr\/bin\/perf<\/code><\/pre>\n<p>There you go.<\/p>\n<p>There are quite a number of sub-commands that can be used with perf command. These include;<\/p>\n<ul>\n<li><code>stat<\/code>: runs a command and gathers performance counter statistics from it.<\/li>\n<li><code>top<\/code>: generates and displays a performance counter profile in real time.<\/li>\n<li><code>record<\/code>: runs a command and gathers a performance counter profile from it. The gathered data is saved into <code>perf.data<\/code> without displaying anything.<\/li>\n<li><code>report<\/code>: It reads <code>perf.data<\/code> to display the performance counter profile information recorded via perf record sub-command.<\/li>\n<li><code>list<\/code>: displays the symbolic event types which can be selected in the various perf commands with the -e option.<\/li>\n<\/ul>\n<p>The basic synopsis of the perf command is;<\/p>\n<pre>perf [--version] [--help] [OPTIONS] COMMAND [ARGS]<\/code><\/pre>\n<p>To obtain a comprehensive list of sub-commands that can be used with perf command, pass the <code>--help<\/code> option to perf.<\/p>\n<pre>perf --help<\/code><\/pre>\n<p>To obtain help about a specific sub-command, just execute;<\/p>\n<pre>perf help SUB-COMMAND<\/code><\/pre>\n<p>Next, let us go over a few example usage of the perf command;<\/p>\n<p>To run a command and gather performance statistics, you would run perf as shown below;<\/p>\n<pre>perf stat ls -ld \/etc\/<\/code><\/pre>\n<pre>drwxr-xr-x 87 root root 4096 Feb 21 18:10 \/etc\/\r\n\r\n Performance counter stats for 'ls -ld \/etc\/':\r\n\r\n          2.485653      task-clock (msec)         #    0.649 CPUs utilized          \r\n                 2      context-switches          #    0.805 K\/sec                  \r\n                 0      cpu-migrations            #    0.000 K\/sec                  \r\n               127      page-faults               #    0.051 M\/sec                  \r\n   &lt;not supported&gt;      cycles                                                      \r\n   &lt;not supported&gt;      instructions                                                \r\n   &lt;not supported&gt;      branches                                                    \r\n   &lt;not supported&gt;      branch-misses                                               \r\n\r\n       0.003827501 seconds time elapsed<\/code><\/pre>\n<p>For a further insight on how to use stat sub-command, check <code>man perf-stat<\/code>.<\/p>\n<p>To display a performance counter profile in real time, use top sub-command;<\/p>\n<pre>perf top<\/code><\/pre>\n<pre>Samples: 71  of event 'cpu-clock', Event count (approx.): 14105457                                                                                             \r\nOverhead  Shared Object      Symbol                                                                                                                            \r\n   9.50%  [kernel]           [k] memcpy\r\n   9.50%  [kernel]           [k] vsnprintf\r\n   6.65%  [kernel]           [k] e1000_alloc_rx_buffers\r\n   5.43%  [kernel]           [k] format_decode\r\n   4.87%  [kernel]           [k] e1000_xmit_frame\r\n   4.07%  [kernel]           [k] kallsyms_expand_symbol.constprop.1\r\n   4.07%  libc-2.27.so       [.] getdelim\r\n   4.07%  perf               [.] 0x00000000001f8804\r\n   3.13%  [kernel]           [k] __softirqentry_text_start\r\n   3.10%  [kernel]           [k] _raw_spin_unlock_irqrestore\r\n   2.71%  [kernel]           [k] number\r\n   2.71%  [kernel]           [k] pointer_string\r\n...<\/code><\/pre>\n<p>To learn more on this, check <code>man perf-top<\/code>.<\/p>\n<p>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 <code>man perf-record<\/code>.<\/p>\n<pre>perf record [-e &lt;EVENT&gt; | --event=EVENT] [-a] &lt;command&gt;<\/code><\/pre>\n<p>To list list all pre-defined events, run <code>perf list<\/code>.<\/p>\n<pre>perf list<\/code><\/pre>\n<pre>List of pre-defined events (to be used in -e):\r\n\r\n  alignment-faults                                   [Software event]\r\n  bpf-output                                         [Software event]\r\n  context-switches OR cs                             [Software event]\r\n  cpu-clock                                          [Software event]\r\n  cpu-migrations OR migrations                       [Software event]\r\n  dummy                                              [Software event]\r\n  emulation-faults                                   [Software event]\r\n  major-faults                                       [Software event]\r\n  minor-faults                                       [Software event]\r\n  page-faults OR faults                              [Software event]\r\n  task-clock                                         [Software event]\r\n\r\n  msr\/smi\/                                           [Kernel PMU event]\r\n  msr\/tsc\/                                           [Kernel PMU event]\r\n...<\/code><\/pre>\n<pre>perf record -e cpu-clock -a -g -- sleep 3<\/code><\/pre>\n<pre>[ perf record: Woken up 5 times to write data ]\r\n[ perf record: Captured and wrote 1.329 MB perf.data (8755 samples) ]<\/code><\/pre>\n<p>Check the man page for the explanation of the options used.<\/p>\n<p>To read the performance record, use the report sub-command. The command syntax is <code>perf report [-i &lt;file&gt; | --input=file]<\/code><\/p>\n<pre>perf report -i perf.data<\/code><\/pre>\n<pre>Samples: 8K of event 'cpu-clock', Event count (approx.): 2188750000                                                                                            \r\n  Children      Self  Command       Shared Object       Symbol                                                                                                 \r\n+   99.94%     0.00%  swapper       [kernel.kallsyms]   [k] secondary_startup_64\r\n+   99.94%     0.00%  swapper       [kernel.kallsyms]   [k] x86_64_start_kernel\r\n+   99.94%     0.00%  swapper       [kernel.kallsyms]   [k] x86_64_start_reservations\r\n+   99.94%     0.00%  swapper       [kernel.kallsyms]   [k] start_kernel\r\n+   99.94%     0.00%  swapper       [kernel.kallsyms]   [k] rest_init\r\n+   99.94%     0.00%  swapper       [kernel.kallsyms]   [k] cpu_startup_entry\r\n+   99.94%     0.00%  swapper       [kernel.kallsyms]   [k] do_idle\r\n+   99.93%    99.93%  swapper       [kernel.kallsyms]   [k] mwait_idle\r\n+   99.93%     0.00%  swapper       [kernel.kallsyms]   [k] default_idle_call\r\n+   99.93%     0.00%  swapper       [kernel.kallsyms]   [k] arch_cpu_idle\r\n     0.03%     0.00%  kworker\/0:1   [kernel.kallsyms]   [k] ret_from_fork\r\n     0.03%     0.00%  kworker\/0:1   [kernel.kallsyms]   [k] kthread\r\n     0.03%     0.00%  kworker\/0:1   [kernel.kallsyms]   [k] worker_thread\r\n...<\/code><\/pre>\n<p>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.<\/p>\n<p>You\u00a0 can also check Perf examples provided <a href=\"http:\/\/www.brendangregg.com\/perf.html\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<p>Well, this just a little introduction into installing Perf performance analysis tool on Ubuntu 18.04. Enjoy<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,49],"tags":[332,67],"class_list":["post-2258","post","type-post","status-publish","format-standard","hentry","category-howtos","category-command-cheatsheets","tag-perf","tag-ubuntu-18-04","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/2258"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=2258"}],"version-history":[{"count":1,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/2258\/revisions"}],"predecessor-version":[{"id":2259,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/2258\/revisions\/2259"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=2258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=2258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=2258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}