{"id":16600,"date":"2024-11-18T17:33:55","date_gmt":"2024-11-18T09:33:55","guid":{"rendered":"https:\/\/fwq.ai\/blog\/16600\/"},"modified":"2024-11-18T17:33:55","modified_gmt":"2024-11-18T09:33:55","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8linux%e8%bf%9b%e8%a1%8ccpu%e5%88%a9%e7%94%a8%e7%8e%87%e4%bc%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/16600\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Linux\u8fdb\u884cCPU\u5229\u7528\u7387\u4f18\u5316"},"content":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528\u8fdb\u884ccpu\u5229\u7528\u7387\u4f18\u5316<\/p>\n<p>\u6458\u8981\uff1a\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u4f18\u5316CPU\u5229\u7528\u7387\u53ef\u4ee5\u63d0\u9ad8\u7cfb\u7edf\u6027\u80fd\u548c\u8d44\u6e90\u5229\u7528\u6548\u7387\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u89c1\u7684CPU\u5229\u7528\u7387\u4f18\u5316\u6280\u5de7\uff0c\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u4f7f\u7528\u8fdb\u7a0b\u8c03\u5ea6\u7b97\u6cd5<\/p>\n<ol>\n<li>\u4fee\u6539\u8fdb\u7a0b\u8c03\u5ea6\u7b56\u7565\uff1aLinux\u7cfb\u7edf\u9ed8\u8ba4\u4f7f\u7528\u5b8c\u5168\u516c\u5e73\u8c03\u5ea6\uff08CFS\uff09\u7b97\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u4fee\u6539\u8c03\u5ea6\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u5c06\u8fdb\u7a0b\u8c03\u5ea6\u7b56\u7565\u8bbe\u7f6e\u4e3a\u5b9e\u65f6\u8c03\u5ea6\uff08RT\uff09\u7b97\u6cd5\uff0c\u53ef\u4ee5\u4e3a\u5173\u952e\u4efb\u52a1\u5206\u914d\u66f4\u591a\u7684CPU\u65f6\u95f4\u7247\u3002<\/li>\n<\/ol>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre># \u4fee\u6539\u8fdb\u7a0b\u8c03\u5ea6\u7b56\u7565\u4e3a\u5b9e\u65f6\u8c03\u5ea6\u7b97\u6cd5\nchrt -r -p 99 &lt;pid&gt;&lt;\/pid&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u4f18\u5148\u7ea7\u8bbe\u7f6e\uff1a\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u8fdb\u7a0b\u7684\u4f18\u5148\u7ea7\uff0c\u6765\u63a7\u5236\u8fdb\u7a0b\u83b7\u53d6CPU\u65f6\u95f4\u7247\u7684\u987a\u5e8f\u3002\u8f83\u9ad8\u4f18\u5148\u7ea7\u7684\u8fdb\u7a0b\u5c06\u83b7\u5f97\u66f4\u591a\u7684CPU\u65f6\u95f4\uff0c\u5e76\u66f4\u5feb\u5730\u6267\u884c\u3002<\/li>\n<\/ol>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre># \u8bbe\u7f6e\u8fdb\u7a0b\u7684\u4f18\u5148\u7ea7\u4e3a\u8f83\u9ad8\u7ea7\u522b\uff08\u8303\u56f4\uff1a-20 ~ 19\uff0c-20\u8868\u793a\u6700\u9ad8\u4f18\u5148\u7ea7\uff09\nnice -n -10 &lt;command&gt;&lt;\/command&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e8c\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u6280\u672f<\/p>\n<ol>\n<li>\u5e76\u53d1\u7f16\u7a0b\uff1a\u4f7f\u7528\u591a\u7ebf\u7a0b\u6280\u672f\u53ef\u4ee5\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u5145\u5206\u5229\u7528\u591a\u6838CPU\u7684\u6027\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u5e93\uff08\u5982pthread\uff09\u8fdb\u884c\u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u7ba1\u7406\u3002<\/li>\n<\/ol>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\n#include &lt;pthread.h&gt;\n\nvoid* task(void* arg) {\n  \/\/ \u7ebf\u7a0b\u7684\u5177\u4f53\u4efb\u52a1\n  printf(\"Hello from thread!\n\");\n  return NULL;\n}\n\nint main() {\n  pthread_t tid;\n  pthread_create(&amp;amp;tid, NULL, task, NULL);\n  printf(\"Hello from main thread!\n\");\n  pthread_join(tid, NULL);  \/\/ \u7b49\u5f85\u5b50\u7ebf\u7a0b\u7ed3\u675f\n  return 0;\n}&lt;\/pthread.h&gt;&lt;\/stdio.h&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e09\u3001\u4f7f\u7528\u9ad8\u6548\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784<\/p>\n<ol>\n<li>\u7a7a\u95f4\u6362\u65f6\u95f4\uff1a\u4f7f\u7528\u9ad8\u6548\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u51cf\u5c11CPU\u7684\u8ba1\u7b97\u65f6\u95f4\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u54c8\u5e0c\u8868\u53ef\u4ee5\u5feb\u901f\u67e5\u627e\u5143\u7d20\uff0c\u800c\u4e0d\u9700\u8981\u904d\u5386\u6574\u4e2a\u6570\u636e\u96c6\u3002<\/li>\n<\/ol>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>#include &lt;iostream&gt;\n#include &lt;unordered_map&gt;\n\nint main() {\n  std::unordered_map&lt;int std::string&gt; map;\n  map[1] = \"Hello\";\n  map[2] = \"World\";\n  std::cout &lt;p&gt;\u56db\u3001\u4f7f\u7528\u7cfb\u7edf\u5de5\u5177&lt;\/p&gt;\n&lt;ol&gt;&lt;li&gt;\u76d1\u6d4bCPU\u5229\u7528\u7387\uff1a\u4f7f\u7528\u7cfb\u7edf\u5de5\u5177\u53ef\u4ee5\u76d1\u6d4bCPU\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u5e2e\u52a9\u4f18\u5316CPU\u5229\u7528\u7387\u3002\u5e38\u7528\u7684\u7cfb\u7edf\u5de5\u5177\u5305\u62ectop\u3001htop\u3001powertop\u7b49\u3002&lt;\/li&gt;&lt;\/ol&gt;\n&lt;p&gt;\u4ee3\u7801\u793a\u4f8b\uff1a&lt;\/p&gt;\n&lt;pre class=\"brush:bash;toolbar:false;\"&gt;# \u4f7f\u7528top\u547d\u4ee4\u67e5\u770bCPU\u5229\u7528\u7387\ntop<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u8c03\u6574CPU\u9891\u7387\uff1a\u5728\u90e8\u5206\u5177\u6709\u52a8\u6001\u9891\u7387\u8c03\u8282\u529f\u80fd\u7684CPU\u4e0a\uff0c\u53ef\u4ee5\u8c03\u6574CPU\u7684\u9891\u7387\u4ee5\u964d\u4f4e\u529f\u8017\u548c\u6e29\u5ea6\uff0c\u5e76\u63d0\u9ad8CPU\u7684\u6027\u80fd\u3002<\/li>\n<\/ol>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre># \u8bbe\u7f6eCPU\u9891\u7387\u4e3a\u6700\u9ad8\u6027\u80fd\u6a21\u5f0f\nsudo cpupower frequency-set --governor performance<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u603b\u7ed3\uff1a\u901a\u8fc7\u4f7f\u7528\u8fdb\u7a0b\u8c03\u5ea6\u7b97\u6cd5\u3001\u591a\u7ebf\u7a0b\u6280\u672f\u3001\u9ad8\u6548\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\uff0c\u4ee5\u53ca\u7cfb\u7edf\u5de5\u5177\u7684\u914d\u5408\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u4f18\u5316Linux\u7cfb\u7edf\u7684CPU\u5229\u7528\u7387\u3002\u5408\u7406\u8c03\u5ea6\u4efb\u52a1\u3001\u5145\u5206\u5229\u7528\u591a\u6838CPU\u3001\u4f7f\u7528\u9ad8\u6548\u7684\u8ba1\u7b97\u65b9\u5f0f\uff0c\u90fd\u80fd\u591f\u63d0\u9ad8\u7cfb\u7edf\u6027\u80fd\u548c\u8d44\u6e90\u5229\u7528\u6548\u7387\u3002<\/p>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<\/p>\n<ol>\n<li>Linux Manual Pages<\/li>\n<li>pthread Library Documentation<\/li>\n<li>C++ Standard Library Documentation<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Linux\u8fdb\u884cCPU\u5229\u7528\u7387\u4f18\u5316\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528\u8fdb\u884ccpu\u5229\u7528\u7387\u4f18\u5316 \u6458\u8981\uff1a\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u4f18\u5316CPU\u5229\u7528\u7387\u53ef\u4ee5\u63d0\u9ad8\u7cfb\u7edf\u6027\u80fd\u548c\u8d44\u6e90\u5229\u7528\u6548\u7387\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u89c1\u7684CPU\u5229\u7528\u7387\u4f18\u5316\u6280\u5de7\uff0c\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u4f7f\u7528\u8fdb\u7a0b\u8c03\u5ea6\u7b97\u6cd5 \u4fee\u6539\u8fdb\u7a0b\u8c03\u5ea6\u7b56\u7565\uff1aLinux\u7cfb\u7edf\u9ed8\u8ba4\u4f7f\u7528\u5b8c\u5168\u516c\u5e73\u8c03\u5ea6\uff08CFS\uff09\u7b97\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u4fee\u6539\u8c03\u5ea6\u7b56\u7565\u3002\u4f8b\u5982\uff0c\u5c06\u8fdb\u7a0b\u8c03\u5ea6\u7b56\u7565\u8bbe\u7f6e\u4e3a\u5b9e\u65f6\u8c03\u5ea6\uff08RT\uff09\u7b97\u6cd5\uff0c\u53ef\u4ee5\u4e3a\u5173\u952e\u4efb\u52a1\u5206\u914d\u66f4\u591a\u7684CPU\u65f6\u95f4\u7247\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a # \u4fee\u6539\u8fdb\u7a0b\u8c03\u5ea6\u7b56\u7565\u4e3a\u5b9e\u65f6\u8c03\u5ea6\u7b97\u6cd5 chrt -r -p 99 &lt;pid&gt;&lt;\/pid&gt; \u767b\u5f55\u540e\u590d\u5236 \u4f18\u5148\u7ea7\u8bbe\u7f6e\uff1a\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u8fdb\u7a0b\u7684\u4f18\u5148\u7ea7\uff0c\u6765\u63a7\u5236\u8fdb\u7a0b\u83b7\u53d6CPU\u65f6\u95f4\u7247\u7684\u987a\u5e8f\u3002\u8f83\u9ad8\u4f18\u5148\u7ea7\u7684\u8fdb\u7a0b\u5c06\u83b7\u5f97\u66f4\u591a\u7684CPU\u65f6\u95f4\uff0c\u5e76\u66f4\u5feb\u5730\u6267\u884c\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a # \u8bbe\u7f6e\u8fdb\u7a0b\u7684\u4f18\u5148\u7ea7\u4e3a\u8f83\u9ad8\u7ea7\u522b\uff08\u8303\u56f4\uff1a-20 ~ 19\uff0c-20\u8868\u793a\u6700\u9ad8\u4f18\u5148\u7ea7\uff09 nice -n -10 &lt;command&gt;&lt;\/command&gt; \u767b\u5f55\u540e\u590d\u5236 \u4e8c\u3001\u4f7f\u7528\u591a\u7ebf\u7a0b\u6280\u672f \u5e76\u53d1\u7f16\u7a0b\uff1a\u4f7f\u7528\u591a\u7ebf\u7a0b\u6280\u672f\u53ef\u4ee5\u540c\u65f6\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u5145\u5206\u5229\u7528\u591a\u6838CPU\u7684\u6027\u80fd\u3002\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u5e93\uff08\u5982pthread\uff09\u8fdb\u884c\u7ebf\u7a0b\u7684\u521b\u5efa\u548c\u7ba1\u7406\u3002 \u4ee3\u7801\u793a\u4f8b\uff1a #include &lt;stdio.h&gt; #include &lt;pthread.h&gt; void* task(void* arg) { \/\/ \u7ebf\u7a0b\u7684\u5177\u4f53\u4efb\u52a1 printf(&#8220;Hello from thread! &#8220;); return NULL; } int main() { pthread_t tid; pthread_create(&amp;amp;tid, NULL, task, NULL); printf(&#8220;Hello from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-16600","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=16600"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/16600\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=16600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=16600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=16600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}