{"id":509,"date":"2021-09-17T01:47:44","date_gmt":"2021-09-16T17:47:44","guid":{"rendered":"https:\/\/blog.cauchyschwarz.com\/?p=509"},"modified":"2021-09-17T01:47:47","modified_gmt":"2021-09-16T17:47:47","slug":"perf","status":"publish","type":"post","link":"https:\/\/blog.cauchyschwarz.com\/?p=509","title":{"rendered":"perf"},"content":{"rendered":"\n<p>A simple introduction to linux perf tool. For a comprehensive description please read this <a href=\"http:\/\/www.brendangregg.com\/perf.html\">post<\/a>.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-right counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69fc454ec889c\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69fc454ec889c\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=509\/#InstallUbuntu\" >Install(Ubuntu)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=509\/#Basic_Usage\" >Basic Usage<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=509\/#demo_program\" >demo program<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=509\/#record_data\" >record data<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=509\/#generate_report\" >generate report<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=509\/#generate_flame_graph\" >generate flame graph<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.cauchyschwarz.com\/?p=509\/#Advanced_Usage\" >Advanced Usage<\/a><\/li><\/ul><\/nav><\/div>\n<h1 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"InstallUbuntu\"><\/span>Install(Ubuntu)<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install linux-tools-common<\/code><\/pre>\n\n\n\n<p>Run <code>perf<\/code> command, then you can be required to install additional packages:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>WARNING: perf not found for kernel 4.15.0-36<\/p><p>You may need to install the following packages for this specific kernel:<br>linux-tools-4.15.0-36-generic<br>linux-cloud-tools-4.15.0-36-generic<\/p><p>You may also want to install one of the following packages to keep up to date:<br>linux-tools-generic<br>linux-cloud-tools-generic<\/p><\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install linux-tools-4.15.0-36-generic<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Basic_Usage\"><\/span>Basic Usage<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"demo_program\"><\/span>demo program<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;thread&gt;\n\nusing namespace std;\n\n#define MAX_LOOP (20 * 10000 * 10000)\n\nvoid func1()\n{\n    for(int i = 0; i &lt; MAX_LOOP; i++);\n}\n\nvoid func2()\n{\n    for(int i = 0; i &lt; MAX_LOOP; i++);\n}\n\nint main(int argc, char **argv)\n{\n    thread t1(func1);\n    thread t2(func1);\n    thread t3(func2);\n\n    t1.join();\n    t2.join();\n    t3.join();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>Assume we compile this demo as <code>demo<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"record_data\"><\/span>record data<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Priviliged permissions may be needed to record data.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>start a program with <code>perf<\/code> command and record data<br><code>perf record -F 1000 .\/demo<\/code><\/li><li>attach a existing process(pid 1828) and record data<br><code>perf record -F 1000 -p 1828<\/code><\/li><\/ul>\n\n\n\n<p>You will get a file named <code>perf.data<\/code>.<\/p>\n\n\n\n<p>Some useful options:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>-g<\/code><br>Enables call-graph (stack chain\/backtrace) recording.<\/li><li><code>-s<\/code> or <code>--stat<\/code><br>Record per-thread event counts. Use it with perf report -T to see the values.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"generate_report\"><\/span>generate report<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Run <code>perf report<\/code> to inspect collected data interactively. Press <code>?<\/code> key to get help.<br>Use <code>--demangle<\/code> option to get demangled symbols but this doesn&#8217;t work on my Ubuntu 18.04. Instead try <code>perf report --stdio | c++filt<\/code> to get demangled results. The drawback is that you lose the convenience of viewing the collected data interactively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"generate_flame_graph\"><\/span>generate flame graph<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To understand the collected perf data more easily, it&#8217;s best to generate a flame graph to help you find the cpu load distribution among lots&#8217;of functions.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone https:\/\/github.com\/brendangregg\/FlameGraph  # or download it from github\ncd FlameGraph\nperf record -F 99 -a -g -- sleep 60\nperf script | .\/stackcollapse-perf.pl &gt; out.perf-folded\n.\/flamegraph.pl out.perf-folded &gt; perf-kernel.svg<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Advanced_Usage\"><\/span>Advanced Usage<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<p>Again, please read brendangregg&#8217;s post to learn details of linux perf tool.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Command <code>perf record -e &lt;event type&gt;<\/code> can trace various kinds of events.<\/li><li>Or use <code>perf stat -e &lt;event type&gt;<\/code> to just get event counters.<\/li><li>Use <code>perf probe --add &lt;user defined tracepoint&gt;<\/code> to add your customized tracepoints.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A simple introduction to linux perf tool. For a comprehensive description please read this post. Ins&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[55],"class_list":["post-509","post","type-post","status-publish","format-standard","hentry","category-10","tag-perf"],"_links":{"self":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=509"}],"version-history":[{"count":1,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/509\/revisions"}],"predecessor-version":[{"id":510,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=\/wp\/v2\/posts\/509\/revisions\/510"}],"wp:attachment":[{"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.cauchyschwarz.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}