{"id":4549,"date":"2013-10-30T22:34:43","date_gmt":"2013-10-30T14:34:43","guid":{"rendered":"http:\/\/ikiki.net\/?p=4549"},"modified":"2013-10-30T22:34:43","modified_gmt":"2013-10-30T14:34:43","slug":"how-linux-caculate-cpu-usage-for-process","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2013\/10\/how-linux-caculate-cpu-usage-for-process\/","title":{"rendered":"Linux\u5982\u4f55\u7edf\u8ba1\u8fdb\u7a0b\u7684CPU\u5229\u7528\u7387"},"content":{"rendered":"\n<h3>0. \u4e3a\u4ec0\u4e48\u5199\u8fd9\u7bc7\u535a\u5ba2<\/h3>\n<p>Linux\u7684top\u6216\u8005ps\u90fd\u53ef\u4ee5\u67e5\u770b\u8fdb\u7a0b\u7684cpu\u5229\u7528\u7387\uff0c\u90a3\u4e3a\u4ec0\u4e48\u8fd8\u9700\u8981\u4e86\u89e3\u8fd9\u4e2a\u7ec6\u8282\u5462\u3002\u7f16\u5199\u8fd9\u7bc7\u6587\u7ae0\u5462\u6709\u5982\u4e0b\u4e09\u4e2a\u539f\u56e0\uff1a<\/p>\n<pre><blockquote>* \u5e0c\u671b\u5728\u811a\u672c\u4e2d\uff0c\u80fd\u591f\u4ee5\u8fc7\u201d\u975e\u963b\u585e\u201d\u7684\u65b9\u5f0f\u83b7\u53d6\u8fdb\u7a0bcpu\u5229\u7528\u7387\r\n* ps\u65e0\u6cd5\u83b7\u5f97\u8fdb\u7a0b\u5f53\u524d\u65f6\u523b\u7684CPU\u5229\u7528\u7387;top\u5219\u9700\u8981\u81f3\u5c111\u79d2\u624d\u80fd\u83b7\u5f97\u8fdb\u7a0b\u5f53\u524d\u7684\u5229\u7528\u7387\r\n* * \u597d\u5947<\/blockquote><\/pre>\n<p><!--more--><\/p>\n<h3>1. \u5982\u4f55\u7edf\u8ba1\u8fdb\u7a0bCPU\u5229\u7528\u7387<\/h3>\n<h4>1.0 \u6982\u8ff0<\/h4>\n<p>\u5728Linux\u7684\/proc\u6587\u4ef6\u7cfb\u7edf\uff0c\u53ef\u4ee5\u770b\u5230\u81ea\u542f\u52a8\u65f6\u5019\u5f00\u59cb\uff0c\u6240\u6709CPU\u6d88\u8017\u7684\u65f6\u95f4\u7247\uff1b\u5bf9\u4e8e\u4e2a\u8fdb\u7a0b\uff0c\u4e5f\u53ef\u4ee5\u770b\u5230\u8fdb\u7a0b\u6d88\u8017\u7684\u65f6\u95f4\u7247\u3002\u8fd9\u662f\u4e00\u4e2a\u7d2f\u8ba1\u503c\uff0c\u53ef\u4ee5&#8221;\u975e\u963b\u585e&#8221;\u7684\u8f93\u51fa\u3002\u83b7\u5f97\u4e00\u5b9a\u65f6\u95f4\u95f4\u9694\u7684\u4e24\u6b21\u7edf\u8ba1\u5c31\u53ef\u4ee5\u8ba1\u7b97\u51fa\u8fd9\u6bb5\u65f6\u95f4\u5185\u7684\u8fdb\u7a0bCPU\u5229\u7528\u7387\u3002<\/p>\n<p><b>\u6240\u4ee5\uff0c\u662f\u5426\u5b58\u5728\u4e00\u79cd\u7b80\u5355\u7684\uff0c\u975e\u963b\u585e\u7684\u65b9\u5f0f\u83b7\u5f97\u8fdb\u7a0b\u7684CPU\u5229\u7528\u7387? \u7b54\u6848\u662f\uff1a\u201c\u6ca1\u6709\u201d\u3002<a href=\"http:\/\/unix.stackexchange.com\/questions\/58539\/top-and-ps-not-showing-the-same-cpu-result\" target=\"_blank\">\u8fd9\u91cc\u7ed9\u51fa\u6765\u4e00\u4e2a\u6709\u8da3\u7684\u6bd4\u55bb<\/a>\uff1a&#8221;\u8fd9\u5c31\u50cf\u6709\u4eba\u7ed9\u4f60\u4e00\u5f20\u7167\u7247\uff0c\u8981\u4f60\u56de\u7b54\u7167\u7247\u4e2d\u8f66\u5b50\u7684\u901f\u5ea6\u4e00\u6837&#8221;<\/b><\/p>\n<h4>1.1 \/proc\/stat \u7edf\u8ba1\u603bCPU\u6d88\u8017<\/h4>\n<p>\u8fd9\u4e2a\u6982\u5ff5\u5728\u8ba1\u7b97\u4e2d\u5e76\u4e0d\u91cd\u8981\uff0c\u4f46\u662f\u4e86\u89e3\u4e00\u4e0b\u8fd8\u662f\u6709\u76ca\u7684\u3002\u5728\/proc\/[pid\/]stat\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u7cfb\u7edf\u7edf\u8ba1\u7684CPU\u65f6\u95f4\u6d88\u8017\uff0c\u8fd9\u91cc\u90fd\u7edf\u4e00\u4f7f\u75281\/USER_HZ\u4e3a\u4e00\u4e2a\u65f6\u95f4\u7247(man proc)\uff0c\u591a\u6570\u60c5\u51b5\u4e0bUSER_HZ\u90fd\u662f\u53d6\u503c100\uff0c\u6240\u4ee5\u8fd9\u91cc\u7684\u4e00\u4e2a\u65f6\u95f4\u7247\u5c31\u662f10ms\u3002\u53ef\u4ee5\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528sysconf(_SC_CLK_TCK)\u6765\u83b7\u5f97\u51c6\u786eUSER_HZ\u7684\u53d6\u503c\u3002<\/p>\n<p>\u4f8b\u5982:<\/p>\n<pre><div class=\"mycode\"># cat \/proc\/stat|grep \"cpu \"\r\ncpu  77918485 720414 61184026 19052884316 12152363 1386 1476742 0 0<\/div><\/pre>\n<p>\u6bcf\u4e00\u5217\u5bf9\u5e94\u7684CPU\u6d88\u8017\u542b\u4e49\u662f(man proc)\uff1a\u7528\u6237\u6001(user)\u3001\u4f4e\u4f18\u5148\u7ea7\u7528\u6237\u6001(nice)\u3001\u7cfb\u7edf(sys)\u3001\u95f2\u7f6e\u3001IOWAIT(\u5185\u68382.5.41+)\u3001\u4e2d\u65ad(since 2.6.0+)\u3001\u8f6f\u7ec8\u7aef(since 2.6.0+)\u3001steal(\u865a\u62df\u73af\u5883\u4e2d\u5176\u4ed6OS\u6d88\u80172.6.11+)\u3001guest(\u4e3a\u8bbf\u5ba2OS\u8fd0\u884c\u865a\u62dfCPU\u6d88\u80172.6.24)<\/p>\n<p>\u5bf9\u5e94\u5982\u4e0b\uff1a<\/p>\n<pre><div class=\"mycode\"># cat \/proc\/stat|grep \"cpu \"\r\n     |usr      |nice   |sys      |idle        |iowait   |irq   |softirq |steal  |guest\r\ncpu  |77918485 |720414 |61184026 |19052884316 |12152363 |1386  |1476742 |0      |0<\/div><\/pre>\n<p>\u6240\u4ee5\uff0c\u8ba1\u7b97CPU\u603b\u6d88\u8017\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0bshell\u547d\u4ee4\uff1a<\/p>\n<pre><div class=\"mycode\">cat \/proc\/stat|grep \"cpu \"|awk '{for(i=2;i<=NF;i++)j+=$i;print \"cpu_total_slice \" j;}'\r\ncpu_total_slice 19208187744<\/div><\/pre>\n<p>\u5f88\u591a\u5730\u65b9\u770b\u5230\u90fd\u53ea\u662f\u7edf\u8ba1\u524d\u9762\u56db\u5217\u6216\u8005\u4e94\u5217\uff0c\u8fd9\u662f\u4e0d\u5b8c\u6574\u7684\uff1b\u4e0d\u8fc7\uff0c\u56e0\u4e3a\u901a\u5e38\u524d\u56db\u3001\u4e94\u5217\u662fCPU\u7684\u4e3b\u8981\u6d88\u8017\uff0c\u6240\u4ee5\u8fd9\u6837\u5982\u6b64\u8ba1\u7b97\u4e5f\u901a\u5e38\u662f\u51c6\u786e\u7684\u3002\u4f8b\u5982\uff0c\u4e0a\u4f8b\u4e2d\uff0c\u524d\u9762\u4e94\u5217\u7684\u6d88\u8017\u662fCPU\u6d88\u8017\u768499.99%\u3002<\/p>\n<p>(tips\uff1a\u8fd9\u91cc\u7684\u65f6\u95f4\u7247\u548cCPU\u65f6\u949f\u4e2d\u65ad\u7684jiffy\u4e0d\u662f\u4e00\u4e2a\u6982\u5ff5\uff0c\u4e00\u4e2a\u662f\u5185\u6838\u6001\u7684\uff0c\u4e00\u4e2a\u7528\u6237\u6001\u7684)<\/p>\n<h4>1.2 \u8fdb\u7a0b\u6d88\u8017\u7684CPU\u65f6\u95f4\u7247<\/h4>\n<p>\u5728proc\u6587\u4ef6\u7cfb\u7edf\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\/proc\/[pid]\/stat\u83b7\u5f97\u8fdb\u7a0b\u6d88\u8017\u7684\u65f6\u95f4\u7247\uff0c\u8f93\u51fa\u7684\u7b2c14\u300115\u300116\u300117\u5217\u5206\u522b\u5bf9\u5e94\u8fdb\u7a0b\u7528\u6237\u6001CPU\u6d88\u8017\u3001\u5185\u6838\u6001\u7684\u6d88\u8017\u3001\u7528\u6237\u6001\u7b49\u5f85\u5b50\u8fdb\u7a0b\u7684\u6d88\u8017\u3001\u5185\u6838\u6001\u7b49\u5f85\u5b50\u8fdb\u7a0b\u7684\u6d88\u8017(man proc)\u3002\u6240\u4ee5\u8fdb\u7a0b\u7684CPU\u6d88\u8017\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre><div class=\"mycode\">cat \/proc\/9583\/stat|awk '{print \"cpu_process_total_slice \" $14+$15+$16+$17}'\r\ncpu_process_total_slice 1068099<\/div><\/pre>\n<p>tips\uff1a\u4ece\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\uff0cLinux\u5e76\u6ca1\u6709\u8fdb\u7a0b\u7ea7\u522b\u7684iowait\u7edf\u8ba1\uff0c\u5982\u679c\u60f3\u77e5\u9053\u7cfb\u7edf\u7684iowait\u662f\u54ea\u4e2a\u8fdb\u7a0b\u5bfc\u81f4\uff0c\u5219\u8fd8\u9700\u8981\u4e00\u4e9b\u5176\u4ed6\u7684\u5de5\u5177\u8f85\u52a9\u3002<\/p>\n<h4>1.3 &#8220;\u975e\u963b\u585e&#8221;\u7684\u8ba1\u7b97\u8fdb\u7a0bCPU\u5229\u7528\u7387<\/h4>\n<p>\u4ece\u8fd9\u91cc\u4e5f\u770b\u5230\uff0c\u662f\u6ca1\u6709\u67d0\u4e2a\u65f6\u523bCPU\u5229\u7528\u7387\u7684\u8bf4\u6cd5\u7684\uff0c\u4e5f\u5c31\u6ca1\u6cd5\u83b7\u5f97\u67d0\u4e2a\u65f6\u523b\u7684CPU\u5229\u7528\u7387\u3002\u8fd9\u5c31\u50cf\u7269\u7406\u4e2d\u7684&#8221;\u901f\u5ea6&#8221;\u7684\u6982\u5ff5\uff0c\u6ca1\u6709\u67d0\u4e00\u65f6\u523b\u901f\u5ea6\u7684\u6982\u5ff5\uff0c\u901f\u5ea6\u4e00\u5b9a\u662f\u4e00\u4e2a\u65f6\u95f4\u6bb5\u4e4b\u5185\u7684\u3002\u90a3\u4e48\u8981&#8221;\u975e\u963b\u585e&#8221;\u8ba1\u7b97\u67d0\u4e2a\u8fdb\u7a0bCPU\u5229\u7528\u7387\uff0c\u5219\u9700\u8981\u53d6\u4e24\u6b21\u4e8b\u4ef6\u95f4\u9694\u8fdb\u884c\u8ba1\u7b97\uff0c\u8fd9\u4e24\u6b21\u4e8b\u4ef6\u95f4\u9694\u7684\u64cd\u4f5c\u53ef\u4ee5\u662f\u975e\u963b\u585e\u7684\u3002\u8ba1\u7b97\u529e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><blockquote>* \u65f6\u523bA\uff0c\u8ba1\u7b97\u64cd\u4f5c\u7cfb\u7edf\u603bCPU\u65f6\u95f4\u7247\u6d88\u8017total_cpu_slice_A\uff1b\u8ba1\u7b97\u8fdb\u7a0b\u603bCPU\u65f6\u95f4\u7247\u6d88\u8017\uff1btotal_process_slice_A\r\n* \u65f6\u523bB\uff0c\u8ba1\u7b97\u64cd\u4f5c\u7cfb\u7edf\u603bCPU\u65f6\u95f4\u7247\u6d88\u8017total_cpu_slice_B\uff1b\u8ba1\u7b97\u8fdb\u7a0b\u603bCPU\u65f6\u95f4\u7247\u6d88\u8017\uff1btotal_process_slice_B<\/blockquote><\/pre>\n<p>B\u65f6\u523b\u5c31\u53ef\u4ee5&#8221;\u975e\u963b\u585e&#8221;\u7684\u8ba1\u7b97\u8fd9\u6bb5\u65f6\u95f4\u8fdb\u7a0b\u7684CPU\u5229\u7528\u7387\u4e86\uff1a<\/p>\n<pre><div class=\"mycode\">100%*(total_process_slice_B-total_process_slice_A)\/(total_cpu_slice_B-total_cpu_slice_A)<\/div><\/pre>\n<h4>1.4 ps\u547d\u4ee4\u663e\u793a\u7684CPU\u5229\u7528\u7387\u662f\u4ec0\u4e48\u610f\u601d<\/h4>\n<pre><blockquote>man ps\r\n......\r\n       CPU usage is currently expressed as the percentage of time spent running during the entire lifetime of a process. This is\r\n       not ideal, and it does not conform to the standards that ps otherwise conforms to. CPU usage is unlikely to add up to\r\n       exactly 100%.\r\n......<\/blockquote><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0cps\u547d\u4ee4%CPU\u663e\u793a\u7684\u662f\u8fdb\u7a0b\u81ea\u542f\u52a8\u65f6\u523b\u8d77\uff0c\u76f4\u81f3\u5f53\u524d\u7684\u603b\u5e73\u5747CPU\u5229\u7528\u7387\u3002<\/p>\n<h3>2 \u53c2\u8003\u6587\u6863<\/h3>\n<p>* man proc \/ man ps<\/p>\n<p>* <a href=\"http:\/\/stackoverflow.com\/questions\/5514119\/accurately-calculating-cpu-utilization-in-linux-using-proc-stat\" target=\"_blank\">Accurately Calculating CPU Utilization in Linux using \/proc\/stat@stackoverflow<\/a><\/p>\n<p>* <a href=\"@http:\/\/git.kernel.org\/cgit\/linux\/kernel\/git\/stable\/linux-stable.git\/tree\/kernel\/sched\/core.c?id=HEAD\" target=\"_blank\">account_process_tick@Linux Kernel<\/a><\/p>\n<p>* <a href=\"http:\/\/bencane.com\/2012\/08\/06\/troubleshooting-high-io-wait-in-linux\/\" target=\"_blank\">Troubleshooting High I\/O Wait in Linux<\/a><\/p>\n<p>* <a href=\"http:\/\/unix.stackexchange.com\/questions\/58539\/top-and-ps-not-showing-the-same-cpu-result\" target=\"_blank\">Top and ps not showing the same cpu result<\/a><\/p>\n<p>\u535a\u5ba2\u7684\u8349\u7a3f\u7bb1\u91cc\u9762\u867d\u7136\u6709\u597d\u51e0\u7bc7\u7a3f\u5b50\u4e86\uff0c\u4e0d\u8fc7\u8ddd\u79bb<a href=\"http:\/\/ikiki.net\/index.php\/2013\/07\/problem-of-compiling-tcprstast\/\" target=\"_blank\">\u4e0a\u4e00\u7bc7\u535a\u5ba2<\/a>\u597d\u50cf\u90fd\u6709100\u5929\u4e86\uff0c\u5199\u5b8c\u8fd9\u4e00\u7bc7\u8fd8\u662f\u5f88\u6b23\u6170\uff0c\u867d\u7136\u5185\u5bb9\u4e0d\u591a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>0. \u4e3a\u4ec0\u4e48\u5199\u8fd9\u7bc7\u535a\u5ba2 Linux\u7684top\u6216\u8005ps\u90fd\u53ef\u4ee5\u67e5\u770b\u8fdb\u7a0b\u7684cpu\u5229\u7528\u7387\uff0c\u90a3\u4e3a\u4ec0\u4e48\u8fd8\u9700\u8981\u4e86\u89e3\u8fd9\u4e2a\u7ec6\u8282\u5462\u3002\u7f16\u5199\u8fd9\u7bc7\u6587\u7ae0\u5462\u6709\u5982\u4e0b\u4e09\u4e2a\u539f\u56e0\uff1a * \u5e0c\u671b\u5728\u811a\u672c\u4e2d\uff0c\u80fd\u591f\u4ee5\u8fc7\u201d\u975e\u963b\u585e\u201d\u7684\u65b9\u5f0f\u83b7\u53d6\u8fdb\u7a0bcpu\u5229\u7528\u7387 * ps\u65e0\u6cd5\u83b7\u5f97\u8fdb\u7a0b\u5f53\u524d\u65f6\u523b\u7684CPU\u5229\u7528\u7387;top\u5219\u9700\u8981\u81f3\u5c111\u79d2\u624d\u80fd\u83b7\u5f97\u8fdb\u7a0b\u5f53\u524d\u7684\u5229\u7528\u7387 * * \u597d\u5947<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[27,6],"tags":[],"class_list":["post-4549","post","type-post","status-publish","format-standard","hentry","category-linux","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/4549","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=4549"}],"version-history":[{"count":25,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/4549\/revisions"}],"predecessor-version":[{"id":4579,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/4549\/revisions\/4579"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=4549"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=4549"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=4549"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}