{"id":969,"date":"2020-11-11T18:09:33","date_gmt":"2020-11-11T10:09:33","guid":{"rendered":"http:\/\/www.wangkaixuan.tech\/?p=969"},"modified":"2020-11-11T18:09:33","modified_gmt":"2020-11-11T10:09:33","slug":"linux%e6%80%a7%e8%83%bd%e7%9b%91%e6%b5%8b%e5%b7%a5%e5%85%b7-02-%e4%be%b5%e5%85%a5%e5%bc%8f","status":"publish","type":"post","link":"http:\/\/www.wangkaixuan.tech\/?p=969","title":{"rendered":"LINUX\u6027\u80fd\u76d1\u6d4b\u5de5\u5177-02-\u4fb5\u5165\u5f0f"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1.\u4ecb\u7ecd<\/h2>\n\n\n\n<p>\u4fb5\u5165\u5f0f\u7684\u6027\u80fd\u68c0\u6d4b\u5de5\u5177\uff0c\u987e\u540d\u601d\u4e49\u5373\u9700\u8981\u4fee\u6539\u4ee3\u7801\u624d\u80fd\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u5728\u5546\u4e1a\u9886\u57df\u6709OProf\uff0c\u7531\u4e8e\u662f\u6536\u8d39\u7684\uff0c\u56e0\u6b64\u5e94\u7528\u5e76\u4e0d\u5e7f\u6cdb\uff0c\u6211\u672c\u4eba\u4e5f\u6ca1\u7528\u8fc7\uff0c\u56e0\u6b64\u4e0d\u518d\u4ecb\u7ecd\u3002<\/p>\n\n\n\n<p>\u5728\u5f00\u6e90\u9886\u57df\u6709gprof\uff08g for GNU\uff09\uff0cgcc\u7f16\u8bd1\u5668\u81ea\u5e26\u4e86\u8be5\u5de5\u5177\u3002\u867d\u8bf4\u4e0d\u7528\u663e\u5f0f\u7684\u4fee\u6539\u6e90\u4ee3\u7801\uff08\u7f16\u8bd1\u65f6\u6dfb\u52a0-pg\u9009\u9879\u5373\u53ef\uff09\uff0c\u4f46\u7f16\u8bd1\u51fa\u6765\u7684\u7a0b\u5e8f\u4e0d\u80fd\u76f4\u63a5\u7528\u4e8e\u751f\u4ea7\u8fd0\u884c\uff0c\u56e0\u6b64\u4e5f\u7b97\u662f\u4fb5\u5165\u5f0f\u7684\u3002<\/p>\n\n\n\n<p>gprof\u867d\u7136\u662f\u514d\u8d39\u7684\uff0c\u8fd8\u6709GNU\u5b98\u65b9\u80cc\u4e66\uff0c\u4f46\u6709\u51e0\u4e2a\u81f4\u547d\u7f3a\u9677\uff1a\u4e00\u662f\u4e0d\u652f\u6301\u591a\u7ebf\u7a0b\u5e94\u7528\uff0c\u53ea\u80fd\u5206\u6790\u4e3b\u7ebf\u7a0b\u6027\u80fd\uff1b\u4e8c\u662f\u4e0d\u652f\u6301\u8ddf\u8e2a\u52a8\u6001\u5e93\u4e2d\u7684\u51fd\u6570\u8c03\u7528\u60c5\u51b5\u3002<\/p>\n\n\n\n<p>\u4eca\u5929\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e00\u4e2ag\u5f00\u5934\u7684\u6027\u80fd\u5206\u6790\u5de5\u5177-google performance tools\uff0c\u529f\u80fd\u5b8c\u5907\uff0c\u652f\u6301\u591a\u7ebf\u7a0b\u4e0b\u7684\u6027\u80fd\u5206\u6790\uff0c\u5de5\u5177\u94fe\u4e5f\u6bd4\u8f83\u5b8c\u5584\uff0c\u80fd\u751f\u6210\u706b\u7130\u56fe\u7b49\u76f4\u89c2\u70ab\u9177\u7684\u7ed3\u679c\u5c55\u793a\u5f62\u5f0f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.\u5b89\u88c5<\/h2>\n\n\n\n<p>\u5728Redhat\u4e0b\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u5b89\u88c5(\u5305\u542b\u706b\u7130\u56fe\u7b49\u56fe\u5f62\u751f\u6210\u5de5\u5177)\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yum install gperftools\nyum install pprof\nyum install graphviz\ngit clone https:\/\/github.com\/brendangregg\/FlameGraph.git<\/code><\/pre>\n\n\n\n<p>\u5728Ubuntu\u4e0b\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt-get install google-perftools\ngit clone https:\/\/github.com\/gperftools\/gperftools.git\napt-get install libgoogle-perftools-dev\ngit clone https:\/\/github.com\/brendangregg\/FlameGraph.git<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u4f7f\u7528<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">a) \u6e90\u7801\u53ca\u7f16\u8bd1<\/h3>\n\n\n\n<p>\u5728\u6e90\u4ee3\u7801\u4e2d\u5e94\u8be5\u5305\u542b\u5982\u4e0b\u51e0\u4e2a\u8981\u7d20\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5305\u542b\u5934\u6587\u4ef6\uff1a<strong>gperftools\/profiler.h<\/strong><\/li><li>\u51c6\u5907\u5f00\u59cb\u641c\u96c6\u6027\u80fd\u6570\u636e\u65f6\uff0c\u8c03\u7528\uff1a<strong>ProfilerStart(&#8220;gperf_capture.prof&#8221;)<\/strong>\uff08\u53c2\u6570\u662f\u4fdd\u5b58\u6027\u80fd\u6570\u636e\u7684\u6587\u4ef6\u8def\u5f84\uff09<\/li><li>\u5982\u679c\u9700\u8981\u5206\u6790\u7ebf\u7a0b\u7684\u6027\u80fd\u6570\u636e\uff0c\u5728<strong>\u9700\u8981\u5206\u6790\u7684\u7ebf\u7a0b\u4e2d<\/strong>\u8c03\u7528\uff1a<strong>ProfilerRegisterThread()<\/strong><\/li><li>\u7ed3\u675f\u5206\u6790\u65f6\uff0c\u8c03\u7528<strong>ProfilerStop()<\/strong>\uff08\u6ce8\u610f\uff0c\u7528\u6237\u53ea\u6709\u8c03\u7528\u4e86\u8be5\u63a5\u53e3\uff0c\u6027\u80fd\u6570\u636e\u6587\u4ef6\u4e2d\u624d\u4f1a\u6709\u6570\u636e\uff0c\u7a0b\u5e8f\u6b63\u5e38\u6216\u5f02\u5e38\u9000\u51fa\u65f6\uff0c\u6027\u80fd\u6570\u636e\u6587\u4ef6\u4e2d\u4e0d\u4f1a\u81ea\u52a8\u5199\u5165\u6570\u636e\uff09<\/li><\/ul>\n\n\n\n<p>\u4e0b\u9762\u5c55\u793a\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;gperftools\/profiler.h>\n#include &lt;pthread.h>\n\n\/\/ g++ -o gperf_test gperf_test.cpp -lpthread -lprofiler\n\nvoid* thread_func_1(void* p_arg)\n{\n    ProfilerRegisterThread();\n\n    long loop = 4000000000;\n    while(loop--)\n        ;\n\n    return NULL;\n}\n\nvoid* thread_func_2(void* p_arg)\n{\n    ProfilerRegisterThread();\n\n    long loop = 4000000000;\n    while(loop--)\n        ;\n\n    return NULL;\n}\n\nint main()\n{\n    ProfilerStart(\"gperf_capture.prof\");\n\n    pthread_t thr_id_1, thr_id_2;\n    pthread_create(&amp;thr_id_1, NULL, thread_func_1, NULL);\n    pthread_create(&amp;thr_id_2, NULL, thread_func_2, NULL);\n    pthread_join(thr_id_1, NULL);\n    pthread_join(thr_id_2, NULL);\n\n    ProfilerStop();\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><br>\u6211\u4eec\u542f\u52a8\u4e86\u4e24\u4e2a\u7ebf\u7a0b\uff0c\u5728\u6bcf\u4e2a\u7ebf\u7a0b\u4e2d\u5404\u8dd1\u4e00\u4e2a\u7a7a\u5faa\u73af\uff0c\u5faa\u73af\u7ed3\u675f\u540e\u9000\u51fa\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a\u7f16\u8bd1\u7684\u65f6\u5019\uff0c\u8981\u52a0\u4e0a<strong>-lprofiler<\/strong>\u9009\u9879\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">b) \u8fd0\u884c\u751f\u6210\u6027\u80fd\u5206\u6790\u6587\u4ef6<\/h3>\n\n\n\n<p>\u6dfb\u52a0<strong>-lprofiler<\/strong>\u7f16\u8bd1\u94fe\u63a5\u751f\u6210\u7a0b\u5e8f\u540e\uff0c\u8fd8\u9700\u8981\u5b9e\u9645\u8fd0\u884c\u4e00\u6b21\uff0c\u624d\u80fd\u751f\u6210\u6027\u80fd\u6570\u636e\u6587\u4ef6\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"604\" height=\"86\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-59.png\" alt=\"\" class=\"wp-image-972\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">c) \u56fe\u5f62\u5316\u5c55\u793a<\/h3>\n\n\n\n<p>\u4f7f\u7528pprof\u811a\u672c\uff0c\u53ef\u4ee5\u57fa\u4e8e\u4e0a\u9762\u751f\u6210\u7684\u6027\u80fd\u6570\u636e\u6587\u4ef6\u751f\u6210\u56fe\u5f62\u6587\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/gperftools\/src\/pprof .\/gperf_test gperf_capture.prof --svg >func_call.svg<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"876\" height=\"180\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-60.png\" alt=\"\" class=\"wp-image-973\"\/><\/figure>\n\n\n\n<p>\u6ce8\u610f\uff1aRedhat\u7cfb\u7edf\u4e0b\uff0c\u8c03\u7528pprof\u4e0d\u7528\u90a3\u4e48\u9ebb\u70e6\uff0c\u76f4\u63a5pprof\u5c31\u884c<\/p>\n\n\n\n<p>\u751f\u6210\u7684\u56fe\u5f62\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1194\" height=\"139\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-61.png\" alt=\"\" class=\"wp-image-974\"\/><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u4e24\u4e2a\u7ebf\u7a0b\u5404\u5360\u4e86\u5927\u7ea6\u4e00\u534a\u7684\u6267\u884c\u65f6\u95f4\u3002<\/p>\n\n\n\n<p>\u9664\u4e86\u4e0a\u9762\u7684\u8fd9\u79cd\u51fd\u6570\u8c03\u7528\u56fe\u5916\uff0c\u8fd8\u80fd\u7528FlameGraph\u5de5\u5177\u751f\u6210\u70ab\u9177\u7684\u706b\u7130\u56fe\uff1a<\/p>\n\n\n\n<p>\u98ce\u683c1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/gperftools\/src\/pprof .\/gperf_test gperf_capture.prof --collapsed > func_call.cbt\n.\/FlameGraph\/flamegraph.pl func_call.cbt > flame_1.svg<\/code><\/pre>\n\n\n\n<p>\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1189\" height=\"99\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-62.png\" alt=\"\" class=\"wp-image-975\"\/><\/figure>\n\n\n\n<p>\u98ce\u683c2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\/gperftools\/src\/pprof .\/gperf_test gperf_capture.prof --collapsed > func_call.cbt\n.\/FlameGraph\/flamegraph.pl --invert --color aqua func_call.cbt > flame_2.svg<\/code><\/pre>\n\n\n\n<p>\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1190\" height=\"102\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-63.png\" alt=\"\" class=\"wp-image-976\"\/><\/figure>\n\n\n\n<p>\u533a\u522b\u662f\u5c55\u793a\u8c03\u7528\u6808\u7684\u65b9\u5411\u4e0d\u540c\u3002<\/p>\n\n\n\n<p>\u6ce8\u610f\uff1a\u5c06\u4e0a\u8ff0svg\u56fe\u5f62\u7528\u6d4f\u89c8\u5668\u6253\u5f00\uff0c\u9f20\u6807\u79fb\u52a8\u5230\u5bf9\u5e94\u7684\u51fd\u6570\u65f6\uff0c\u5c4f\u5e55\u4f1a\u663e\u793a\u5173\u4e8e\u8be5\u51fd\u6570\u7684\u5168\u90e8\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e\u6211\u4eec\u7684\u4f8b\u5b50\u7a0b\u5e8f\u592a\u8fc7\u7b80\u5355\uff0c\u56e0\u6b64\u5c55\u793a\u7684\u6548\u679c\u4e0d\u592a\u9707\u64bc\uff0c\u7ed9\u5927\u5bb6\u770b\u770b\u5176\u4ed6\u7684\u51e0\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"834\" height=\"483\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-64.png\" alt=\"\" class=\"wp-image-977\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"435\" height=\"186\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-65.png\" alt=\"\" class=\"wp-image-978\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">4.\u53c2\u8003\u94fe\u63a5<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5927\u795eBrendan Gregg\u4e2a\u4eba\u7f51\u7ad9\uff1a<a href=\"http:\/\/www.brendangregg.com\">\u94fe\u63a5\u5728\u6b64<\/a><\/li><li>\u706b\u7130\u56fe\u4ecb\u7ecd\uff1a<a href=\"http:\/\/www.brendangregg.com\/perf.html#FlameGraphs\">\u94fe\u63a5\u5728\u6b64<\/a><\/li><li>\u706b\u7130\u56fe\u4ee3\u7801\uff1a<a href=\"https:\/\/github.com\/brendangregg\/FlameGraph\">\u94fe\u63a5\u5728\u6b64<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>1.\u4ecb\u7ecd \u4fb5\u5165\u5f0f\u7684\u6027\u80fd\u68c0\u6d4b\u5de5\u5177\uff0c\u987e\u540d\u601d\u4e49\u5373\u9700\u8981\u4fee\u6539\u4ee3\u7801\u624d\u80fd\u4f7f\u7528\u3002 \u5728\u5546\u4e1a\u9886\u57df\u6709OProf\uff0c\u7531\u4e8e\u662f\u6536\u8d39\u7684\uff0c\u56e0\u6b64\u5e94\u7528\u5e76\u4e0d\u5e7f\u6cdb\uff0c\u6211\u672c\u4eba\u4e5f\u6ca1\u7528\u8fc7\uff0c\u56e0\u6b64\u4e0d\u518d\u4ecb\u7ecd\u3002 \u5728\u5f00\u6e90&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"http:\/\/www.wangkaixuan.tech\/?p=969\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-969","post","type-post","status-publish","format-standard","hentry","category-05-02-"],"_links":{"self":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=969"}],"version-history":[{"count":0,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/969\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=969"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}