{"id":861,"date":"2020-11-03T15:48:28","date_gmt":"2020-11-03T07:48:28","guid":{"rendered":"http:\/\/www.wangkaixuan.tech\/?p=861"},"modified":"2020-11-03T15:48:28","modified_gmt":"2020-11-03T07:48:28","slug":"cpu%e7%bb%91%e5%ae%9a%e7%9a%84%e5%87%a0%e7%a7%8d%e5%ae%9e%e7%8e%b0%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"http:\/\/www.wangkaixuan.tech\/?p=861","title":{"rendered":"CPU\u7ed1\u5b9a\u7684\u51e0\u79cd\u5b9e\u73b0\u65b9\u5f0f"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1.\u4e86\u89e3\u4f60\u7684CPU<\/h2>\n\n\n\n<p>\u5728\u73b0\u4ee3\u591a\u5904\u7406\u5668\u7cfb\u7edf\u4e2d\uff08\u5982\u4e0b\u56fe\uff09\uff0c\u6bcf\u4e2a\u7cfb\u7edf\uff08System\uff09\u53ef\u80fd\u5b89\u88c5\u591a\u4e2a\u5904\u7406\u5668\uff08Processor\/Socket\uff09\u82af\u7247,\u6bcf\u4e2a\u5904\u7406\u5668\u53ef\u80fd\u5305\u542b\u591a\u4e2a\u6838\u5fc3\uff08Core\uff09\uff0c\u6bcf\u4e2a\u6838\u5fc3\u53ef\u80fd\u5305\u542b\u591a\u4e2a\u6307\u4ee4\u6d41\u6c34\u7ebf\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"962\" height=\"338\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-11.png\" alt=\"\" class=\"wp-image-864\"\/><\/figure>\n\n\n\n<p>\u901a\u8fc7lscpu\u547d\u4ee4\u53ef\u4ee5\u67e5\u770bCPU\u7684\u4fe1\u606f\uff0c\u4e0b\u56fe\u5c55\u793a\u7684\u662f\u67d0\u53f0\u670d\u52a1\u5668\u7684\u4fe1\u606f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"340\" height=\"356\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-10.png\" alt=\"\" class=\"wp-image-862\"\/><\/figure>\n\n\n\n<p>\u4e0a\u8ff0\u4fe1\u606f\u8868\u660e\uff0c\u8be5\u670d\u52a1\u5668\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5171\u670924\u4e2a\u6838\u5fc3\uff08CPU(s)\uff09<\/li><li>\u6bcf\u4e2a\u6838\u5fc3\u67091\u4e2a\u7ebf\u7a0b\uff08\u5373\u6307\u4ee4\u6d41\u6c34\u7ebf\uff09<\/li><li>\u6bcf\u9897\u5904\u7406\u5668\uff08Socket(s)\uff09\u670912\u4e2a\u6838\u5fc3<\/li><li>\u6709\u4e24\u9897\u5904\u7406\u5668\uff08NUMA\u67b6\u6784\uff09<\/li><\/ul>\n\n\n\n<p>\u4e0a\u8ff0\u4fe1\u606f\u8fd8\u5c55\u793a\u4e86\u5404\u4e2a\u6838\u5fc3\u5728NUMA\u8282\u70b9\u4e0a\u7684\u5206\u5e03\uff0c\u6bd4\u5982CPU 0\u662f\u4f4d\u4e8eNUMA\u8282\u70b90\u4e0a\u7684\u3002CPU 0\u901a\u5e38\u88ab\u7528\u6765\u5904\u7406\u6240\u6709\u4e2d\u65ad\uff0c\u6bd4\u5982\u7f51\u7edc\u3001\u4fe1\u53f7\u7b49\uff0c\u6240\u4ee5\u6211\u4eec\u5728\u7ed1\u5b9a\u4e00\u4e9b\u9700\u8981\u9891\u7e41\u5904\u7406\u4e2d\u65ad\u7684\u7a0b\u5e8f\u65f6\uff0c\u5c31\u53ef\u4ee5\u628a\u4ed6\u4eec\u7ed1\u5b9a\u5728CPU 0\u6216\u8005\u5176\u4ed6\u4e0eCPU 0\u4f4d\u4e8e\u540c\u4e00NUMA\u8282\u70b9\u7684CPU\u4e0a\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.CPU\u7ed1\u5b9a\u7684\u610f\u4e49<\/h2>\n\n\n\n<p>\u4e00\u662f\u73b0\u4ee3\u670d\u52a1\u5668\u5927\u591a\u91c7\u7528NUMA\u591a\u5904\u7406\u5668\u67b6\u6784\uff0c\u4e00\u53f0\u670d\u52a1\u5668\u4f1a\u5b89\u88c5\u591a\u9897\u5904\u7406\u5668\uff08\u79f0\u4e3aNUMA\u8282\u70b9\uff09\uff0c\u800cNUMA\u67b6\u6784\u5404\u8282\u70b9\u8d44\u6e90\u8f83\u4e3a\u72ec\u7acb\u7684\u8bbe\u8ba1\uff0c\u51b3\u5b9a\u4e86\u5728\u4e0d\u540cNUMA\u8282\u70b9\u5171\u4eab\u6570\u636e\u7684\u6210\u672c\u9ad8\u6602\uff0c\u56e0\u6b64\u5c3d\u91cf\u5c06\u6570\u636e\u4ea4\u4e92\u8f83\u4e3a\u9891\u7e41\u7684\u7a0b\u5e8f\u7ed1\u5b9a\u5728\u540c\u4e00NUMA\u8282\u70b9\u4e0a\u662f\u5f88\u91cd\u8981\u7684\u3002<\/p>\n\n\n\n<p>\u4e8c\u662f\u8fdb\u7a0b\/\u7ebf\u7a0b\u5982\u679c\u4ece\u4e00\u4e2a\u6838\u5fc3\u5207\u6362\u81f3\u53e6\u4e00\u4e2a\u6838\u5fc3\u4e0a\u8fd0\u884c\uff0c\u9700\u8981\u9762\u4e34\u4e0a\u4e0b\u6587\u5207\u6362\u3001\u7f13\u5b58\u5931\u6548\u7b49\u95ee\u9898\uff0c\u6210\u672c\u4e5f\u5f88\u9ad8\u3002\u5728\u5bf9\u6027\u80fd\u8981\u6c42\u8f83\u9ad8\u7684\u8f6f\u4ef6\u4e2d\uff0c\u8fd9\u5df2\u7ecf\u662f\u9020\u6210\u65f6\u5ef6\u6296\u52a8\u7684\u4e00\u5927\u6765\u6e90\u4e4b\u4e00\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"964\" height=\"489\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-13.png\" alt=\"\" class=\"wp-image-866\"\/><\/figure>\n\n\n\n<p>\u4e0a\u56fe\u5c55\u793a\u4e86\u4e00\u4e2a\u5b9e\u6d4b\u7684\u4f8b\u5b50\uff0c\u5de5\u4f5c\u96c6\uff08working set\uff09\u8f83\u5927\u7684\u8fdb\u7a0b\u5728\u8fdb\u884c\u4e0a\u4e0b\u6587\u5207\u6362\u65f6\uff0c\u5176\u65f6\u5ef6\u53ef\u9ad8\u8fbe50\u5fae\u79d2\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.CPU\u7ed1\u5b9a\u539f\u7406<\/h2>\n\n\n\n<p>\u5728Linux\u7cfb\u7edf\u4e0b\uff0c\u8fdb\u7a0b\u90fd\u6709\u4e00\u4e2aCPU\u4eb2\u548c\u529b\u5c5e\u6027\uff08affinity\uff09\uff0c\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u53ef\u4ee5\u67e5\u8be2\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"345\" height=\"35\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-14.png\" alt=\"\" class=\"wp-image-869\"\/><\/figure>\n\n\n\n<p>\u4ee5\u4e0a\u67e5\u8be2\u7ed3\u679c\u7684\u542b\u4e49\u662f\uff0c\u8fdb\u7a0bid\u4e3a4500\u7684\u8fdb\u7a0b\uff0c\u53ef\u4ee5\u57280\u30011\u30012\u30013\u53f7CPU\u4e0a\u8fd0\u884c\uff0c\u6211\u7684\u7b14\u8bb0\u672c\u7684CPU\u662f\u56db\u6838\u7684\uff0c\u56e0\u6b64\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8fdb\u7a0b\u662f\u53ef\u4ee5\u5728\u4efb\u610f\u4e00\u4e2a\u6838\u5fc3\u4e0a\u8fd0\u884c\u7684\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"452\" height=\"92\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-18.png\" alt=\"\" class=\"wp-image-876\"\/><\/figure>\n\n\n\n<p>\u6211\u4eec\u7f16\u5199\u4e0b\u9762\u8fd9\u6837\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u89c2\u5bdf\u4e0bLinux\u8fdb\u7a0b\u8c03\u5ea6\u7684\u73b0\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>\n#include &lt;stdlib.h>\n#include &lt;unistd.h>\n\n\/\/ g++ -o bind_1 bind_1.c\n\nvoid print_running_cpu()\n{\n    char qry_cmd&#91;1024] = { 0 };\n    sprintf(qry_cmd, \"ps -o pid,psr -p %d | tail -n 1 | awk {'print $2'}\", getpid());\n    FILE *fp=popen(qry_cmd,\"r\");\n    if(fp == NULL)\n        return ;\n\n    char cpu_id_str&#91;200] = { 0 };\n    fgets(cpu_id_str,80,fp);\n    fclose(fp);\n\n    printf(\"current process %d is running on cpu(%d)\\n\", getpid(), atoi(cpu_id_str));\n}\n\nint main()\n{\n    print_running_cpu();\n\n    while(1)\n    {\n        long loop = 4000000000;\n        while(loop--)\n            ;\n        \n        sleep(0);\n\n        printf(\"----------------\\n\");\n        printf(\"switched !\\n\");\n        print_running_cpu();\n    }\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u6709\u4e24\u4e2a\u5173\u952e\u903b\u8f91\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u5faa\u73af\u6267\u884c\u7a7a\u8bed\u53e5\uff1a\u8fd9\u6837\u7a0b\u5e8f\u7684CPU\u5360\u7528\u7387\u4f1a\u8fbe\u5230100%,\u65b9\u4fbf\u6211\u4eec\u89c2\u5bdf\u7a0b\u5e8f\u5728\u54ea\u4e2a\u6838\u4e0a\u8fd0\u884c<\/li><li>\u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4\u6267\u884c\u4e00\u6b21sleep(0)\uff1a\u4e3b\u52a8\u653e\u5f03CPU\u5360\u7528\uff0c\u8ba9Linux\u91cd\u65b0\u8c03\u5ea6\u4e00\u6b21\uff0c\u8fd9\u6837\u8fdb\u7a0b\u5c31\u6709\u673a\u4f1a\u88ab\u5207\u6362\u5230\u5176\u4ed6\u6838\u5fc3\u4e0a\u6267\u884c<\/li><\/ul>\n\n\n\n<p>\u53e6\u5916\uff0c\u8be5\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u8fd8\u4f1a\u6253\u5370\u81ea\u5df1\u5f53\u524d\u6240\u5728\u7684CPU\u53f7\uff0c\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"470\" height=\"326\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-15.png\" alt=\"\" class=\"wp-image-870\"\/><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u8fdb\u7a0b\u4f1a\u4e0d\u65ad\u5730\u5728\u4e0d\u540c\u7684CPU\u4e4b\u95f4\u8df3\u52a8\u3002<\/p>\n\n\n\n<p>\u7528htop\u4e5f\u80fd\u89c2\u5bdf\u5230\u540c\u6837\u7684\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"331\" height=\"125\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-16.png\" alt=\"\" class=\"wp-image-871\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"338\" height=\"120\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-17.png\" alt=\"\" class=\"wp-image-872\"\/><\/figure>\n\n\n\n<p>\u5982\u679c\u8fd9\u662f\u4e2a\u65f6\u5ef6\u654f\u611f\u7684\u7cfb\u7edf\uff0c\u8fd9\u6837\u9891\u7e41\u7684\u5728CPU\u4e4b\u95f4\u8df3\u52a8\uff0c\u65e0\u7591\u4f1a\u5e26\u6765\u989d\u5916\u7684\u8fdb\u7a0b\u4e0a\u4e0b\u6587\u5207\u6362\u7b49\u5f00\u9500\uff0c\u9020\u6210\u65f6\u5ef6\u6296\u52a8\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.\u901a\u8fc7\u547d\u4ee4\u7ed1\u5b9a\uff08\u8fdb\u7a0b\uff09<\/h2>\n\n\n\n<p>\u901a\u8fc7taskset\u547d\u4ee4\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u7a0b\u5e8f\u7684\u60c5\u51b5\u4e0b\u4ece\u5916\u90e8\u5c06\u7a0b\u5e8f\u7ed1\u5b9a\u81f3\u67d0\u4e2aCPU\u6838\u5fc3\u3002<\/p>\n\n\n\n<p>\u4ee5\u5c06\u4e0a\u9762\u7684\u8fdb\u7a0b\u7ed1\u5b9a\u81f3CPU 0\u4e3a\u4f8b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"430\" height=\"58\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-19.png\" alt=\"\" class=\"wp-image-877\"\/><\/figure>\n\n\n\n<p>\u4ece\u7a0b\u5e8f\u7684\u8f93\u51fa\u6765\u770b\uff0c\u5728\u8fd0\u884c\u65f6\u4e0d\u4f1a\u88ab\u5207\u6362\u5230\u5176\u4ed6\u6838\u5fc3\u4e0a\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"353\" height=\"277\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-20.png\" alt=\"\" class=\"wp-image-878\"\/><\/figure>\n\n\n\n<p>\u4ecehtop\u4e0a\u770b\u4e5f\u662f\u5982\u6b64\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"339\" height=\"123\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-21.png\" alt=\"\" class=\"wp-image-879\"\/><\/figure>\n\n\n\n<p>\u8bc1\u660e\u7ed1\u5b9a\u7684\u6548\u679c\u8fbe\u5230\u4e86\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5c06\u7a0b\u5e8f\u7ed1\u5b9a\u5230\u4e00\u4e2aCPU\u5217\u8868\u4e0a\uff0c\u6bd4\u5982\u7ed1\u5b9a\u5230NUMA node0\u4e0a\u7684\u6240\u6709CPU\uff0c\u901a\u8fc7taskset\u547d\u4ee4\u4e5f\u53ef\u4ee5\u505a\u5230\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"363\" height=\"57\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-22.png\" alt=\"\" class=\"wp-image-880\"\/><\/figure>\n\n\n\n<p>\u8fd9\u6837\u7684\u8bdd\uff0c\u8fdb\u7a0b\u5c31\u53ea\u80fd\u57280\u53f7\u548c2\u53f7CPU\u4e0a\u8fd0\u884c\u4e86\u3002<\/p>\n\n\n\n<p>\u5728\u5b9e\u9645\u7684\u751f\u4ea7\u7cfb\u7edf\u8fd0\u7ef4\u8fc7\u7a0b\u4e2d\uff0c\u8fd0\u884c\u7a0b\u5e8f\u524d\u662f\u4e0d\u77e5\u9053\u7a0b\u5e8f\u7684pid\u7684\uff0c\u597d\u5728taskset\u547d\u4ee4\u8fd8\u652f\u6301\u542f\u52a8\u65f6\u6307\u5b9a\u4eb2\u548c\u529b\uff0c\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"371\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-24.png\" alt=\"\" class=\"wp-image-883\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">5.\u901a\u8fc7\u547d\u4ee4\u7ed1\u5b9a(\u7ebf\u7a0b)<\/h2>\n\n\n\n<p>\u6211\u4eec\u7f16\u5199\u5982\u4e0b\u7a0b\u5e8f\uff0c\u542f\u52a8\u4e24\u4e2a\u7ebf\u7a0b\uff0c\u5404\u4e2a\u7ebf\u7a0b\u5faa\u73af\u6267\u884c\u7a7a\u8bed\u53e5\uff0c\u6bcf\u9694\u4e00\u6bb5\u65f6\u95f4sleep(0)\u4e00\u6b21\uff0c\u65b9\u4fbf\u64cd\u4f5c\u7cfb\u7edf\u5c06\u7ebf\u7a0b\u5207\u6362\u5230\u5176\u4ed6\u6838\u4e0a\u8fd0\u884c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>\n#include &lt;stdlib.h>\n#include &lt;unistd.h>\n#include &lt;pthread.h>\n\n\/\/ g++ -o bind_2 bind_2.c -lpthread\n\nvoid print_running_cpu()\n{\n    char qry_cmd&#91;1024] = { 0 };\n    sprintf(qry_cmd, \"ps -o pid,spid,psr -T -p %d | grep %d | tail -n 1 | awk {'print $3'}\", \n        getpid(), gettid());\n    FILE *fp=popen(qry_cmd,\"r\");\n    if(fp == NULL)\n        return ;\n\n    char cpu_id_str&#91;200] = { 0 };\n    fgets(cpu_id_str,80,fp);\n    fclose(fp);\n\n    printf(\"&#91;%d] : current thread(%d@%d) is running on cpu(%d)\\n\", \n        gettid(), gettid(), getpid(), atoi(cpu_id_str));\n}\n\nvoid* thread_func(void* p_arg)\n{\n    print_running_cpu();\n    while(1)\n    {\n        long loop = 4000000000;\n        while(loop--)\n            ;\n        \n        sleep(0);\n\n        printf(\"&#91;%d] : ----------------\\n\", gettid());\n        printf(\"&#91;%d] : switched !\\n\", gettid());\n        print_running_cpu();\n    }\n}\n\nint main()\n{\n    print_running_cpu();\n\n    pthread_t thr_id_1, thr_id_2;\n\n    pthread_create(&amp;thr_id_1, NULL, thread_func, NULL);\n    pthread_create(&amp;thr_id_2, NULL, thread_func, NULL);\n    \n    while(1)\n        sleep(1);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u8ddf\u524d\u9762\u7528\u7684\u6d4b\u8bd5\u4ee3\u7801\u6bd4\u8f83\u7c7b\u4f3c\uff0c\u53ea\u662f\u628a\u4e00\u4e9b\u547d\u4ee4\u4ee5\u53ca\u63a5\u53e3\u4ece\u8fdb\u7a0b\u6362\u6210\u4e86\u7ebf\u7a0b\uff0c\u8fd0\u884c\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"475\" height=\"452\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-25.png\" alt=\"\" class=\"wp-image-886\"\/><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u5982\u4e0b\u4fe1\u606f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4e3b\u7ebf\u7a0bid\u4e3a30827\uff08\u4ee5\u4e0b\u7b80\u79f027\uff09<\/li><li>\u542f\u52a8\u7684\u4e24\u4e2a\u7ebf\u7a0b\u5206\u522b\u4e3a30834\uff08\u4ee5\u4e0b\u7b80\u79f034\uff09\u548c30833\uff08\u4ee5\u4e0b\u7b80\u79f033\uff09<\/li><li>\u7ebf\u7a0b34\u548c\u7ebf\u7a0b33\u5728\u8fd0\u884c\u671f\u95f4\u90fd\u8fdb\u884c\u591a\u6b21\u6838\u5fc3\u5207\u6362<\/li><\/ul>\n\n\n\n<p>\u4ee5\u4e0b\u547d\u4ee4\u53ef\u4ee5\u7528\u6765\u67e5\u770b\u8fdb\u7a0b\u4ee5\u53ca\u5b83\u542f\u52a8\u7684\u7ebf\u7a0b\uff08SPID\u5217\u4e3a\u7ebf\u7a0bid\uff09\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"378\" height=\"92\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-26.png\" alt=\"\" class=\"wp-image-887\"\/><\/figure>\n\n\n\n<p>taskset\u547d\u4ee4\u4f9d\u7136\u9002\u7528\u4e8e\u7ebf\u7a0b\uff0c\u6267\u884c\u5982\u4e0b\u547d\u4ee4\u67e5\u770bCPU\u4eb2\u548c\u6027\u8bbe\u7f6e\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"390\" height=\"112\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-27.png\" alt=\"\" class=\"wp-image-888\"\/><\/figure>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u8fdb\u7a0b\uff0837\uff09\u4ee5\u53ca\u4ed6\u7684\u4e24\u4e2a\u7ebf\u7a0b\u90fd\u662f\u53ef\u4ee5\u5728\u6240\u6709CPU\u4e0a\u8fd0\u884c\u7684\u3002<\/p>\n\n\n\n<p>\u6267\u884c\u5982\u4e0b\u547d\u4ee4\u8bbe\u7f6e\u7ebf\u7a0b\u7684CPU\u4eb2\u548c\u6027\uff08\u7ebf\u7a0b33\u7ed1\u5b9a\u52301\u53f7CPU\uff0c\u7ebf\u7a0b34\u7ed1\u5b9a\u52302\u53f7CPU\uff09\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"375\" height=\"111\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-28.png\" alt=\"\" class=\"wp-image-889\"\/><\/figure>\n\n\n\n<p>\u8bbe\u5b9a\u540e\uff0c\u7ebf\u7a0b\u4e0d\u518d\u5207\u6362\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"478\" height=\"417\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-29.png\" alt=\"\" class=\"wp-image-890\"\/><\/figure>\n\n\n\n<p>htop\u4e0a\u4e5f\u80fd\u770b\u51fa\u6548\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"336\" height=\"129\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-30.png\" alt=\"\" class=\"wp-image-891\"\/><\/figure>\n\n\n\n<p>\u7f8e\u4e2d\u4e0d\u8db3\u7684\u662f\uff0c\u8fd9\u6b21\u6211\u4eec\u4e0d\u80fd\u901a\u8fc7taskset+\u7a0b\u5e8f\u540d\u76f4\u63a5\u542f\u52a8\u7a0b\u5e8f\u5e76\u5b9e\u73b0\u5404\u4e2a\u7ebf\u7a0b<strong>\u7ed1\u5b9a\u4e0d\u540c\u6838\u5fc3<\/strong>\u4e86\uff0c\u8be5\u547d\u4ee4\u53ea\u80fd\u652f\u6301\u5230\u8fdb\u7a0b\u7ea7\u522b\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u4f1a\u628a\u8fdb\u7a0b\u4e0b\u7684\u6240\u6709\u7ebf\u7a0b\u90fd\u8bbe\u7f6e\u4e3a\u76f8\u540c\u7684\u4eb2\u548c\u5ea6\uff0c\u5982\u4e0b\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"598\" height=\"65\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-31.png\" alt=\"\" class=\"wp-image-892\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"357\" height=\"114\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-32.png\" alt=\"\" class=\"wp-image-893\"\/><\/figure>\n\n\n\n<p>\u4e0d\u8fc7\u4e5f\u4e0d\u662f\u6ca1\u6709\u89e3\u51b3\u529e\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u811a\u672c\u542f\u52a8\u5e76\u83b7\u53d6\u7ebf\u7a0bid\uff0c\u518d\u591a\u6b21\u8c03\u7528taskset\uff0c\u6307\u5b9a\u7ebf\u7a0bid\uff0c\u5c06\u5176\u7ed1\u5b9a\u5230\u4e0d\u540c\u6838\u5fc3\u4e0a\u5373\u53ef\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6.\u7f16\u7a0b\u7ed1\u5b9a\uff08C\u63a5\u53e3\uff09<\/h2>\n\n\n\n<p>\u6211\u4eec\u4f7f\u7528sched_xxxaffinity\u63a5\u53e3\u5b9e\u73b0\u901a\u8fc7\u7f16\u7a0b\u7ed1\u5b9aCPU\u6838\u5fc3\uff0c\u8be5\u63a5\u53e3\u65e2\u80fd\u7ed1\u5b9a\u8fdb\u7a0b\u53c8\u80fd\u7ed1\u5b9a\u7ebf\u7a0b\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>\n#include &lt;stdlib.h>\n#include &lt;unistd.h>\n#include &lt;pthread.h>\n#include &lt;sched.h>\n\n\/\/ g++ -o bind_3 bind_3.c -lpthread\n\nvoid print_running_cpu()\n{\n    char qry_cmd&#91;1024] = { 0 };\n    sprintf(qry_cmd, \"ps -o pid,spid,psr -T -p %d | grep %d | tail -n 1 | awk {'print $3'}\", \n        getpid(), gettid());\n    FILE *fp=popen(qry_cmd,\"r\");\n    if(fp == NULL)\n        return ;\n\n    char cpu_id_str&#91;200] = { 0 };\n    fgets(cpu_id_str,80,fp);\n    fclose(fp);\n\n    printf(\"&#91;%d] : current thread(%d@%d) is running on cpu(%d)\\n\", \n        gettid(), gettid(), getpid(), atoi(cpu_id_str));\n}\n\nvoid print_thread_affinity()\n{\n    cpu_set_t cpu_mask;\n\n    CPU_ZERO(&amp;cpu_mask);\n    sched_getaffinity(gettid(), sizeof(cpu_mask), &amp;cpu_mask);\n\n    printf(\"&#91;%d] : current thread(%d@%d) can be running at cpu(\",\n        gettid(), gettid(), getpid());\n\n    int cpu_num = sysconf(_SC_NPROCESSORS_CONF);\n    for(int i = 0; i &lt; cpu_num; ++i)\n    {\n        if (CPU_ISSET(i, &amp;cpu_mask))\/\/\u5224\u65ad\u7ebf\u7a0b\u4e0e\u54ea\u4e2aCPU\u6709\u4eb2\u548c\u529b\n        {\n            printf(\"%d, \", i);\n        }\n    }\n    printf(\")\\n\");\n}\n\nvoid bind_thread_to_cpu(int cpu_id)\n{\n    cpu_set_t cpu_mask;\n    CPU_ZERO(&amp;cpu_mask);\n\n    CPU_SET(cpu_id, &amp;cpu_mask);\n    print_thread_affinity();\n    printf(\"&#91;%d] : binding current thread(%d@%d) to cpu(%d)\\n\",\n        gettid(), gettid(), getpid(), cpu_id);\n    sched_setaffinity(gettid(), sizeof(cpu_mask), &amp;cpu_mask);\n    print_thread_affinity();\n}\n\nvoid* thread_func(void* p_arg)\n{\n    printf(\"&#91;%d] : ----------------\\n\", gettid());\n    printf(\"&#91;%d] : setting cpu affinity for thread(%d@%d) ...\\n\",\n        gettid(), gettid(), getpid());\n    int bind_cpu_id = *(int *)p_arg;\n    bind_thread_to_cpu(bind_cpu_id);\n    printf(\"&#91;%d] : ----------------\\n\\n\", gettid());\n\n    sleep(1);\n\n    print_running_cpu();\n    \n    while(1)\n    {\n        long loop = 4000000000;\n        while(loop--)\n            ;\n        \n        sleep(0);\n\n        printf(\"&#91;%d] : ----------------\\n\", gettid());\n        printf(\"&#91;%d] : switched !\\n\", gettid());\n        print_running_cpu();\n    }\n}\n\nint main()\n{\n    int cpu_id_0 = 0;\n    int cpu_id_1 = 1;\n    int cpu_id_2 = 2;\n    int cpu_id_3 = 3;\n\n    pthread_t thr_id_1, thr_id_2;\n\n    pthread_create(&amp;thr_id_1, NULL, thread_func, &amp;cpu_id_1);\n\n    sleep(1);\n\n    pthread_create(&amp;thr_id_2, NULL, thread_func, &amp;cpu_id_3);\n    \n    while(1)\n        sleep(1);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u8fd0\u884c\u6548\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"596\" height=\"474\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-33.png\" alt=\"\" class=\"wp-image-895\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"326\" height=\"122\" src=\"http:\/\/www.wangkaixuan.tech\/wp-content\/uploads\/2020\/11\/image-34.png\" alt=\"\" class=\"wp-image-896\"\/><\/figure>\n\n\n\n<p>\u7ed3\u679c\u4e0d\u8a00\u800c\u55bb\uff0c\u8fd9\u91cc\u4ec5\u7b80\u5355\u89e3\u91ca\u4e0b\u7a0b\u5e8f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>sched_xxxaffinity\u63a5\u53e3\u867d\u7136\u660e\u786e\u8bf4\u660e\u53ea\u80fd\u4f20pid\uff0c\u4f46\u4f20\u5165spid\uff08\u7ebf\u7a0bid\uff09\u4e5f\u662f\u53ef\u4ee5\u7684\uff0c\u7ebf\u7a0bid\u4f7f\u7528gettid()\u63a5\u53e3\u83b7\u5f97<\/li><li>CPU_SET\u7b49\u63a5\u53e3\u4f20\u5165\u7684cpu_id\u5e76\u4e0d\u662f\u63a9\u7801\uff0c\u800c\u662f0\u30011\u30012\u30013\u7b49\u8fd9\u6837\u7684\u6570\u5b57<\/li><li>sched_xxxaffinity\u63a5\u53e3\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u53ef\u4ee5\u586b\u62100, \u8868\u793a\u5f53\u524d\u8fdb\u7a0b\uff08\u7ebf\u7a0b\uff09<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">7.\u7f16\u7a0b\u7ed1\u5b9a\uff08pthread\u63a5\u53e3\uff09<\/h2>\n\n\n\n<p>pthread\u4e5f\u63d0\u4f9b\u4e86\u7c7b\u4f3c\u7684\u63a5\u53e3\uff0c\u793a\u4f8b\u7a0b\u5e8f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h>\n#include &lt;stdlib.h>\n#include &lt;unistd.h>\n#include &lt;pthread.h>\n\n\/\/ g++ -o bind_4 bind_4.c -lpthread\n\nvoid print_running_cpu()\n{\n    char qry_cmd&#91;1024] = { 0 };\n    sprintf(qry_cmd, \"ps -o pid,spid,psr -T -p %d | grep %d | tail -n 1 | awk {'print $3'}\", \n        getpid(), gettid());\n    FILE *fp=popen(qry_cmd,\"r\");\n    if(fp == NULL)\n        return ;\n\n    char cpu_id_str&#91;200] = { 0 };\n    fgets(cpu_id_str,80,fp);\n    fclose(fp);\n\n    printf(\"&#91;%d] : current thread(%d@%d) is running on cpu(%d)\\n\", \n        gettid(), gettid(), getpid(), atoi(cpu_id_str));\n}\n\nvoid print_thread_affinity()\n{\n    cpu_set_t cpu_mask;\n\n    CPU_ZERO(&amp;cpu_mask);\n    pthread_getaffinity_np(pthread_self(), sizeof(cpu_mask), &amp;cpu_mask);\n\n    printf(\"&#91;%d] : current thread(%d@%d) can be running at cpu(\",\n        gettid(), gettid(), getpid());\n\n    int cpu_num = sysconf(_SC_NPROCESSORS_CONF);\n    for(int i = 0; i &lt; cpu_num; ++i)\n    {\n        if (CPU_ISSET(i, &amp;cpu_mask))\/\/\u5224\u65ad\u7ebf\u7a0b\u4e0e\u54ea\u4e2aCPU\u6709\u4eb2\u548c\u529b\n        {\n            printf(\"%d, \", i);\n        }\n    }\n    printf(\")\\n\");\n}\n\nvoid bind_thread_to_cpu(int cpu_id)\n{\n    cpu_set_t cpu_mask;\n    CPU_ZERO(&amp;cpu_mask);\n\n    CPU_SET(cpu_id, &amp;cpu_mask);\n    print_thread_affinity();\n    printf(\"&#91;%d] : binding current thread(%d@%d) to cpu(%d)\\n\",\n        gettid(), gettid(), getpid(), cpu_id);\n    pthread_setaffinity_np(pthread_self(), sizeof(cpu_mask), &amp;cpu_mask);\n    print_thread_affinity();\n}\n\nvoid* thread_func(void* p_arg)\n{\n    printf(\"&#91;%d] : ----------------\\n\", gettid());\n    printf(\"&#91;%d] : setting cpu affinity for thread(%d@%d) ...\\n\",\n        gettid(), gettid(), getpid());\n    int bind_cpu_id = *(int *)p_arg;\n    bind_thread_to_cpu(bind_cpu_id);\n    printf(\"&#91;%d] : ----------------\\n\\n\", gettid());\n\n    sleep(1);\n\n    print_running_cpu();\n    \n    while(1)\n    {\n        long loop = 4000000000;\n        while(loop--)\n            ;\n        \n        sleep(0);\n\n        printf(\"&#91;%d] : ----------------\\n\", gettid());\n        printf(\"&#91;%d] : switched !\\n\", gettid());\n        print_running_cpu();\n    }\n}\n\nint main()\n{\n    int cpu_id_0 = 0;\n    int cpu_id_1 = 1;\n    int cpu_id_2 = 2;\n    int cpu_id_3 = 3;\n\n    pthread_t thr_id_1, thr_id_2;\n\n    pthread_create(&amp;thr_id_1, NULL, thread_func, &amp;cpu_id_1);\n\n    sleep(1);\n\n    pthread_create(&amp;thr_id_2, NULL, thread_func, &amp;cpu_id_3);\n    \n    while(1)\n        sleep(1);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>pthread\u63d0\u4f9b\u7684\u63a5\u53e3\u8ddfsched_xxxaffinity\u63a5\u53e3\u6781\u5176\u7c7b\u4f3c\uff0c\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u63a5\u53e3\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e0d\u540c\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4e00\u4e2a\u662f\u901a\u8fc7pthread_self()\u83b7\u5f97\uff0c\u4e00\u4e2a\u662f\u901a\u8fc7gettid()\u83b7\u5f97<\/li><li>\u4e24\u8005\u7684\u503c\u5e76\u4e0d\u76f8\u540c<\/li><li>pthread_self()\u7684\u503c\u53ea\u5728\u7a0b\u5e8f\u5185\u53ef\u89c1\uff0c\u5728\u7a0b\u5e8f\u5916\u9762\u65e0\u6cd5\u901a\u8fc7\u547d\u4ee4\u67e5\u8be2\uff0c\u800cgettid()\u7684\u53ef\u4ee5<\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u4e86\u89e3\u4f60\u7684CPU \u5728\u73b0\u4ee3\u591a\u5904\u7406\u5668\u7cfb\u7edf\u4e2d\uff08\u5982\u4e0b\u56fe\uff09\uff0c\u6bcf\u4e2a\u7cfb\u7edf\uff08System\uff09\u53ef\u80fd\u5b89\u88c5\u591a\u4e2a\u5904\u7406\u5668\uff08Processor\/Socket\uff09\u82af\u7247,\u6bcf\u4e2a\u5904\u7406\u5668\u53ef\u80fd\u5305\u542b\u591a\u4e2a\u6838&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"http:\/\/www.wangkaixuan.tech\/?p=861\"> 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":[14],"tags":[],"class_list":["post-861","post","type-post","status-publish","format-standard","hentry","category-05-01-"],"_links":{"self":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/861","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=861"}],"version-history":[{"count":0,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=\/wp\/v2\/posts\/861\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=861"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wangkaixuan.tech\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}