{"id":4336,"date":"2013-04-25T15:09:11","date_gmt":"2013-04-25T07:09:11","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=4336"},"modified":"2013-11-21T11:38:14","modified_gmt":"2013-11-21T03:38:14","slug":"cgroup%e7%ae%80%e4%bb%8b-%e8%b5%84%e6%ba%90%e6%8e%a7%e5%88%b6%e7%9a%84%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/4336","title":{"rendered":"Cgroup\u7b80\u4ecb-\u8d44\u6e90\u63a7\u5236\u7684\u5b9e\u73b0"},"content":{"rendered":"<p>Cgroup\u81ea\u8eab\u901a\u8fc7\u6587\u4ef6\u7cfb\u7edf\u7684\u5f62\u5f0f\u5728\u5185\u6838\u4e2d\u5b9e\u73b0\uff0c\u901a\u8fc7\u5bf9\u5b50\u7cfb\u7edf\u914d\u7f6e\u6587\u4ef6\u7684\u8bfb\u5199\u5373\u53ef\u5b8c\u6210\u5bf9\u8fdb\u7a0b\u7ec4\u8d44\u6e90\u7684\u63a7\u5236\u3002\u4e0d\u8fc7\uff0ccgroup\u5bf9\u5404\u79cd\u8d44\u6e90\u7684\u5b9e\u9645\u63a7\u5236\u5219\u5206\u5e03\u5230\u6574\u4e2a\u5185\u6838\u4ee3\u7801\u4e2d\u3002\u4e0b\u9762\u4eceCPU\u3001\u5185\u5b58\u548cI\/O\u4e09\u4e2a\u65b9\u9762\u8bf4\u660eCgroup\u5bf9\u8d44\u6e90\u7684\u63a7\u5236\u8fc7\u7a0b\u3002<\/p>\n<h2>1 CPU\u63a7\u5236<\/h2>\n<p>Cgroup\u5bf9\u8fdb\u7a0b\u7ec4\u4f7f\u7528CPU\u7684\u9650\u5236\u662f\u901a\u8fc7cpu\u548ccpuset\u4e24\u4e2a\u5b50\u7cfb\u7edf\u6765\u5b8c\u6210\u7684\u3002cpu\u5b50\u7cfb\u7edf\u4e3b\u8981\u9650\u5236\u8fdb\u7a0b\u7684\u65f6\u95f4\u7247\u5927\u5c0f\uff0ccpuset\u5b50\u7cfb\u7edf\u53ef\u4e3a\u8fdb\u7a0b\u6307\u5b9acpu\u548c\u5185\u5b58\u8282\u70b9\u3002<\/p>\n<p><strong>1) cpu\u5b50\u7cfb\u7edf<\/strong><\/p>\n<p>cpu\u5b50\u7cfb\u7edf\u4e3b\u8981\u5206\u5e03\u5728\u5185\u6838\u7684\u8c03\u5ea6\u7cfb\u7edf\u4e2d\uff0c\u901a\u8fc7\u8be5\u5b50\u7cfb\u7edf\u4e2d\u7684cpu.shares\u6587\u4ef6\u53ef\u5bf9\u8fdb\u7a0b\u7ec4\u8bbe\u7f6e\u6743\u91cd\u3002<\/p>\n<p>\u6839\u636eCFS\u7684\u539f\u7406\uff0c\u4e00\u4e2a\u8fdb\u7a0b\u7684\u6743\u91cd\u8d8a\u5927\uff0c\u90a3\u4e48\u5b83\u7684\u88ab\u8c03\u5ea6\u7684\u53ef\u80fd\u6027\u5c31\u8d8a\u5927\u3002\u90a3\u4e48\u8fdb\u7a0b\u7ec4\u7684\u6743\u91cd\u5982\u4f55\u5728CFS\u4e2d\u4f53\u73b0\uff1fCFS\u5c06\u8fdb\u7a0b\u548c\u8fdb\u7a0b\u7ec4\u89c6\u4e3a\u540c\u4e00\u4e2a\u8c03\u5ea6\u4f53\uff0c\u5e76\u7528sched_entity\u7ed3\u6784\u6765\u8868\u793a\uff0c\u6bcf\u4e2a\u7ed3\u6784\u4e2d\u5305\u542b\u8be5\u8c03\u5ea6\u4f53\u7684\u6743\u91cd\u4ee5\u53ca\u865a\u62df\u8fd0\u884c\u65f6\u95f4\u7b49\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u7528\u6237\u53ef\u901a\u8fc7CPU\u5b50\u7cfb\u7edf\u4e2d\u7684cpu.shares\u6587\u4ef6\u6765\u63a7\u5236\u8fdb\u7a0b\u7ec4\u5bf9CPU\u7684\u4f7f\u7528\u3002<\/p>\n<p><strong>2) cpuset\u5b50\u7cfb\u7edf<\/strong><\/p>\n<p>\u901a\u8fc7cpuset\u5b50\u7cfb\u7edf\u4e2d\u7684cpuset.cpus\u548ccpuset.mems\u53ef\u5bf9\u8fdb\u7a0b\u7ec4\u8bbe\u5b9a\u53ef\u8bbf\u95ee\u7684CPU\u548c\u5185\u5b58\u8282\u70b9\u3002\u5185\u6838\u4f7f\u7528cpuset\u7ed3\u6784\u6765\u63cf\u8ff0cpuset\u5b50\u7cfb\u7edf\u7684\u5c5e\u6027\u4fe1\u606f\uff0c\u5176\u4e2d\u8be5\u7ed3\u6784\u4e2d\u7684cpus_allowed\u548cmems_allowed\u4e24\u4e2a\u5b57\u6bb5\u5206\u522b\u4fdd\u5b58\u4e0a\u8ff0\u4e24\u4e2a\u6587\u4ef6\u7684\u503c\u3002\u540c\u65f6\uff0c\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u4e2d\u4e5f\u6709cpus_allowed\u548cmems_allowed\u4e24\u4e2a\u5b57\u6bb5\uff0c\u5176\u503c\u4e0ecpuset\u7ed3\u6784\u4fdd\u6301\u540c\u6b65\u3002<\/p>\n<p>cpuset\u9650\u5236\u8fdb\u7a0b\u6240\u80fd\u8bbf\u95ee\u7684CPU\u4e3b\u8981\u901a\u8fc7\u4e24\u65b9\u9762\u3002\u9996\u5148\u4f53\u73b0\u5728\u8fdb\u7a0b\u7684\u521b\u5efa\uff0c\u5982\u679c\u7236\u8fdb\u7a0b\u65b0\u5efa\u5b50\u8fdb\u7a0b\u65f6\u6ca1\u6709\u6307\u5b9aCLONE_STOPPED\u6807\u5fd7\uff0c\u5219\u7236\u8fdb\u7a0b\u5c06\u8c03\u7528wake_up_new_task()\u5c06\u5b50\u8fdb\u7a0b\u72b6\u6001\u8bbe\u7f6e\u4e3aTASK_RUNNING\uff0c\u5e76\u5c06\u5176\u52a0\u5165\u5c31\u7eea\u961f\u5217\u3002\u4e3a\u5b50\u8fdb\u7a0b\u9009\u62e9\u5c31\u7eea\u961f\u5217\u7684\u5177\u4f53\u5de5\u4f5c\u5219\u7531select_task_rq()\u5b8c\u6210\uff0c\u5176\u5185\u90e8\u5b9e\u73b0\u5c06\u6d89\u53caCPU\u7684\u68c0\u67e5\u64cd\u4f5c\uff0c\u5373\u5728cpus_allowed\u6240\u6307\u5b9a\u7684cpu\u8303\u56f4\u5185\u4e3a\u5f53\u524d\u8fdb\u7a0b\u5206\u914dCPU\u3002<\/p>\n<p>\u5176\u6b21\uff0c\u5f53\u8c03\u5ea6\u5668\u5728\u8c03\u5ea6\u4e00\u4e2a\u8fdb\u7a0b\u65f6\uff0c\u4e5f\u8981\u901a\u8fc7select_task_rq()\u8fdb\u884c\u540c\u6837\u7684\u5bf9\u6bd4\u9009\u62e9\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u4fdd\u8bc1cgroup\u5b9e\u4f8b\u4e2d\u7684\u8fdb\u7a0b\u53ea\u5728cpus_allowed\u6240\u9650\u5b9a\u7684cpu\u4e2d\u8fd0\u884c\u3002<\/p>\n<p>cpuset\u8fdb\u7a0b\u6240\u80fd\u8bbf\u95ee\u5185\u5b58\u8282\u70b9\u7684\u9650\u5236\u8868\u73b0\u5728\u7269\u7406\u5185\u5b58\u7684\u5206\u914d\u8fc7\u7a0b\u4e2d\u3002Linux\u5185\u6838\u5c06\u7269\u7406\u5185\u5b58\u5728\u903b\u8f91\u4e0a\u5206\u4e3anode\u3001zone\u548cpage\uff0c\u5185\u6838\u901a\u8fc7alloc_pages()\u6765\u5b9e\u73b0\u7269\u7406\u5185\u5b58\u7684\u5206\u914d\u5de5\u4f5c\u3002alloc_pages()\u7684\u4e3b\u8981\u5de5\u4f5c\u662f\u5728\u6240\u6709\u7269\u7406\u5185\u5b58\u4e2d\u9009\u62e9node\uff0c\u518d\u5728\u5f53\u524dnode\u4e2d\u9009\u62e9zone\uff0c\u6700\u7ec8\u5728zone\u4e2d\u5206\u914d\u4e00\u4e2a\u7269\u7406\u9875\u6846\u3002\u5176\u4e2d\uff0c\u5728\u9009\u62e9node\u7684\u8fc7\u7a0b\u4e2d\u4f1a\u8fdb\u884cmems_allowed\u7684\u5224\u65ad\u8fc7\u7a0b\u3002<\/p>\n<h2>2 \u5185\u5b58\u63a7\u5236<\/h2>\n<p>cgroup\u5bf9\u5185\u5b58\u7684\u63a7\u5236\u901a\u8fc7memory\u5b50\u7cfb\u7edf\u5b8c\u6210\uff0c\u5176\u63a7\u5236\u4f5c\u7528\u4e3b\u8981\u4f53\u73b0\u5728\u5bf9\u5185\u5b58\u4f7f\u7528\u91cf\u7684\u9650\u5236\uff0c\u540c\u65f6\u4e3a\u5f53\u524dcgroup\u751f\u6210\u4e00\u4efd\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u62a5\u544a\u3002<\/p>\n<p>\u5728\u5177\u4f53\u5b9e\u73b0\u7684\u8fc7\u7a0b\u4e2d\uff0ccgroup\u901a\u8fc7\u5185\u6838\u4e2d\u7684resource counter\u673a\u5236\u5b9e\u73b0\u5185\u5b58\u7684\u9650\u5236\u3002resource counter\u76f8\u5f53\u4e8e\u4e00\u4e2a\u901a\u7528\u7684\u8d44\u6e90\u8ba1\u6570\u5668\uff0c\u5728\u5185\u6838\u4e2d\u901a\u8fc7res_counter\u7ed3\u6784\u6765\u63cf\u8ff0\u3002\u8be5\u7ed3\u6784\u53ef\u7528\u4e8e\u8bb0\u5f55\u67d0\u7c7b\u8d44\u6e90\u7684\u5f53\u524d\u4f7f\u7528\u91cf\u3001\u6700\u5927\u4f7f\u7528\u91cf\u4ee5\u53ca\u4e0a\u9650\u7b49\u4fe1\u606f\u3002\u5177\u4f53\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nstruct res_counter {\r\n    \/*\r\n     * the current resource consumption level\r\n     *\/\r\n    unsigned long long usage;\r\n    \/*\r\n     * the maximal value of the usage from the counter creation\r\n     *\/\r\n    unsigned long long max_usage;\r\n    \/*\r\n     * the limit that usage cannot exceed\r\n     *\/\r\n    unsigned long long limit;\r\n    \/*\r\n     * the limit that usage can be exceed\r\n     *\/\r\n    unsigned long long soft_limit;\r\n    \/*\r\n     * the number of unsuccessful attempts to consume the resource\r\n     *\/\r\n    unsigned long long failcnt;\r\n    \/*\r\n     * the lock to protect all of the above.\r\n     * the routines below consider this to be IRQ-safe\r\n     *\/\r\n    spinlock_t lock;    \/*\r\n     * Parent counter, used for hierarchial resource accounting\r\n     *\/\r\n    struct res_counter *parent;\r\n};\r\n\r\n<\/pre>\n<p>\u5185\u6838\u4f7f\u7528mem_cgroup\u7ed3\u6784\u63cf\u8ff0cgroup\u4e2d\u5185\u5b58\u7684\u4f7f\u7528\u4fe1\u606f\uff0c\u5176\u5185\u90e8\u5d4c\u5165\u4e86res_counter\u7ed3\u6784\u3002\u56e0\u6b64res_counter\u4e2d\u7684\u6bcf\u4e2a\u5b57\u6bb5\u5219\u8868\u793a\u5bf9\u5185\u5b58\u4f7f\u7528\u91cf\u7684\u8bb0\u5f55\u3002\u7528\u6237\u6001\u4e0bmemory\u5b50\u7cfb\u7edf\u6240\u5bfc\u51fa\u7684\u914d\u7f6e\u6587\u4ef6\u4e0e\u8be5\u7ed3\u6784\u4e2d\u7684\u5b57\u6bb5\u4e92\u76f8\u5bf9\u5e94\uff0c\u6bd4\u5982mem.limit_in_bytes\u8868\u793a\u5f53\u524dcgroup\u53ef\u4f7f\u7528\u5185\u5b58\u7684\u6700\u5927\u4e0a\u7ebf\uff0c\u8be5\u6587\u4ef6\u4e0eres_counter\u7ed3\u6784\u4e2d\u7684limit\u5b57\u6bb5\u5bf9\u5e94\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f53\u7528\u6237\u5728\u7528\u6237\u6001\u5411mem.limit_in_bytes\u6587\u4ef6\u5199\u5165\u503c\u540e\uff0c\u5219res_counter\u4e2d\u7684limit\u5b57\u6bb5\u76f8\u5e94\u66f4\u65b0\u3002<\/p>\n<p>\u5185\u6838\u5bf9res_counter\u8fdb\u884c\u64cd\u4f5c\u65f6\u6709\u4e09\u4e2a\u57fa\u672c\u51fd\u6570\uff1ares_counter_init()\u5bf9res_counter\u8fdb\u884c\u521d\u59cb\u5316\uff1b\u5f53\u5206\u914d\u8d44\u6e90\u65f6\uff0cres_counter_charge()\u8bb0\u5f55\u8d44\u6e90\u7684\u4f7f\u7528\u91cf\uff0c\u5e76\u4e14\u8be5\u51fd\u6570\u8fd8\u4f1a\u68c0\u67e5\u4f7f\u7528\u91cf\u662f\u5426\u8d85\u8fc7\u4e86\u4e0a\u9650\uff0c\u5e76\u4e14\u8bb0\u5f55\u5f53\u524d\u8d44\u6e90\u4f7f\u7528\u91cf\u7684\u6700\u5927\u503c\uff1b\u5f53\u8d44\u6e90\u88ab\u91ca\u653e\u65f6\uff0cres_counter_uncharge()\u5219\u51cf\u5c11\u8be5\u8d44\u6e90\u7684\u4f7f\u7528\u91cf\u3002<\/p>\n<p>cgroup\u5bf9\u5185\u5b58\u8d44\u6e90\u7684\u9650\u5236\u4e3b\u8981\u662f\u5c06\u4e0a\u8ff0\u4e09\u4e2a\u51fd\u6570\u5206\u5e03\u5230\u5185\u5b58\u7684\u5206\u914d\u5355\u5143\u4e2d\uff0c\u6bd4\u5982\uff0c\u7cfb\u7edf\u53d1\u751f\u7f3a\u9875\u5f02\u5e38\u65f6\uff0c\u7531\u4e8e\u9875\u8868\u9879\u672a\u5206\u914d\u800c\u7533\u8bf7\u5185\u5b58\u65f6\uff0c\u7531\u4e8e\u9875\u7f13\u5b58\u800c\u5206\u914d\u5185\u5b58\u7b49\u3002<\/p>\n<h2>3 \u5757I\/O\u63a7\u5236<\/h2>\n<p>Cgroup\u4e2d\u901a\u8fc7blkio\u5b50\u7cfb\u7edf\u5b8c\u6210\u5bf9\u5757\u8bbe\u5907I\/O\u7684\u63a7\u5236\u3002\u5177\u4f53\u7684\u63a7\u5236\u4e3b\u8981\u901a\u8fc7blkio.weight\u6587\u4ef6\u5728\u7528\u6237\u6001\u8bbe\u5b9a\u5f53\u524d\u8fdb\u7a0b\u7ec4\u8bbf\u95ee\u5757I\/O\u7684\u6743\u91cd\uff0c\u4e5f\u5c31\u662f\u63a7\u5236\u8fdb\u7a0b\u7ec4\u5360\u6709I\/O\u7684\u65f6\u95f4\u3002<\/p>\n<p>blkio\u5b50\u7cfb\u7edf\u5bf9\u5757I\/O\u7684\u63a7\u5236\u4ee3\u7801\u4e3b\u8981\u5206\u5e03\u5728I\/O\u8c03\u5ea6\u7b97\u6cd5\u4e2d\uff0c\u76ee\u524d\u5185\u6838\u4e2d\u9ed8\u8ba4\u7684\u8c03\u5ea6\u7b97\u6cd5\u4e3aCFQ\uff08\u5b8c\u5168\u516c\u5e73\u961f\u5217\uff09\uff0c\u8be5\u7b97\u6cd5\u4e0e\u8fdb\u7a0b\u8c03\u5ea6\u7b97\u6cd5CFS\u6bd4\u8f83\u7c7b\u4f3c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cgroup\u81ea\u8eab\u901a\u8fc7\u6587\u4ef6\u7cfb\u7edf\u7684\u5f62\u5f0f\u5728\u5185\u6838\u4e2d\u5b9e\u73b0\uff0c\u901a\u8fc7\u5bf9\u5b50\u7cfb\u7edf\u914d\u7f6e\u6587\u4ef6\u7684\u8bfb\u5199\u5373\u53ef\u5b8c\u6210\u5bf9\u8fdb\u7a0b\u7ec4\u8d44\u6e90\u7684\u63a7\u5236\u3002\u4e0d\u8fc7\uff0cc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[377],"tags":[373,378,374],"class_list":["post-4336","post","type-post","status-publish","format-standard","hentry","category-linux","tag-cgroup","tag-378","tag-374"],"views":15275,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=4336"}],"version-history":[{"count":9,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4336\/revisions"}],"predecessor-version":[{"id":4357,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/4336\/revisions\/4357"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=4336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=4336"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=4336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}