{"id":9379,"date":"2024-07-24T14:34:25","date_gmt":"2024-07-24T06:34:25","guid":{"rendered":"\/?p=9379"},"modified":"2024-07-24T14:37:41","modified_gmt":"2024-07-24T06:37:41","slug":"cgroup%e4%bd%bf%e7%94%a8cgroup-tools","status":"publish","type":"post","link":"\/?p=9379","title":{"rendered":"cgroup\u4f7f\u7528cgroup-tools"},"content":{"rendered":"<p><a href=\"https:\/\/segmentfault.com\/a\/1190000007241437\">https:\/\/segmentfault.com\/a\/1190000007241437<\/a><\/p>\n<p>\u5728 Python \u4e2d\u4f7f\u7528 cgroup \u5bf9\u8fdb\u7a0b\u8fdb\u884c\u8d44\u6e90\u9650\u5236\uff0c\u9700\u8981\u7ed3\u5408 Python \u7684\u5b50\u8fdb\u7a0b\u7ba1\u7406\u548c cgroup \u7684\u547d\u4ee4\u884c\u5de5\u5177\u6765\u5b9e\u73b0\u3002cgroup\uff08Control Groups\uff09\u662f Linux \u5185\u6838\u7684\u4e00\u9879\u7279\u6027\uff0c\u7528\u4e8e\u9650\u5236\u3001\u63a7\u5236\u548c\u9694\u79bb\u8fdb\u7a0b\u7684\u8d44\u6e90\u4f7f\u7528\uff08\u5982 CPU\u3001\u5185\u5b58\u3001\u78c1\u76d8 I\/O \u7b49\uff09\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 cgroup \u5bf9\u67d0\u4e2a\u5b50\u8fdb\u7a0b\u8fdb\u884c CPU \u548c\u5185\u5b58\u8d44\u6e90\u9650\u5236\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5\u5fc5\u8981\u7684\u5de5\u5177<\/strong>\uff1a\u786e\u4fdd\u4f60\u7684\u7cfb\u7edf\u5df2\u7ecf\u5b89\u88c5\u4e86 <code>cgroup-tools<\/code>\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo apt-get install cgroup-tools<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u5e76\u914d\u7f6e cgroup<\/strong>\uff1a\u4f7f\u7528 Python \u811a\u672c\u521b\u5efa\u4e00\u4e2a cgroup\uff0c\u5e76\u914d\u7f6e\u5176\u8d44\u6e90\u9650\u5236\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u542f\u52a8\u5b50\u8fdb\u7a0b\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230 cgroup \u4e2d<\/strong>\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b Python \u811a\u672c\uff0c\u6f14\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\u4e0a\u8ff0\u6b65\u9aa4\uff1a<\/p>\n<pre><code class=\"language-python\">import os\nimport subprocess\n\n# \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 cgroup\ncgroup_name = &quot;my_cgroup&quot;\nsubprocess.run([&quot;sudo&quot;, &quot;cgcreate&quot;, &quot;-g&quot;, f&quot;cpu,memory:\/{cgroup_name}&quot;])\n\n# \u8bbe\u7f6e cgroup \u7684 CPU \u9650\u5236\uff0c\u4f8b\u5982\u9650\u5236\u4e3a 50% \u7684 CPU \u4f7f\u7528\u7387\ncpu_quota = 50000  # \u8868\u793a 50% \u7684 CPU \u65f6\u95f4\ncpu_period = 100000  # \u8868\u793a\u5468\u671f\u4e3a 100ms\nsubprocess.run([&quot;sudo&quot;, &quot;cgset&quot;, &quot;-r&quot;, f&quot;cpu.cfs_quota_us={cpu_quota}&quot;, cgroup_name])\nsubprocess.run([&quot;sudo&quot;, &quot;cgset&quot;, &quot;-r&quot;, f&quot;cpu.cfs_period_us={cpu_period}&quot;, cgroup_name])\n\n# \u8bbe\u7f6e cgroup \u7684\u5185\u5b58\u9650\u5236\uff0c\u4f8b\u5982\u9650\u5236\u4e3a 100MB\nmemory_limit = 100 * 1024 * 1024  # 100MB\nsubprocess.run([&quot;sudo&quot;, &quot;cgset&quot;, &quot;-r&quot;, f&quot;memory.limit_in_bytes={memory_limit}&quot;, cgroup_name])\n\n# \u542f\u52a8\u5b50\u8fdb\u7a0b\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230 cgroup \u4e2d\ncommand = [&quot;python3&quot;, &quot;-c&quot;, &quot;import time; time.sleep(30)&quot;]  # \u793a\u4f8b\u547d\u4ee4\nprocess = subprocess.Popen(command)\n\n# \u83b7\u53d6\u5b50\u8fdb\u7a0b\u7684 PID\npid = process.pid\n\n# \u5c06\u5b50\u8fdb\u7a0b\u6dfb\u52a0\u5230 cgroup \u4e2d\nsubprocess.run([&quot;sudo&quot;, &quot;cgclassify&quot;, &quot;-g&quot;, f&quot;cpu,memory:\/{cgroup_name}&quot;, str(pid)])\n\n# \u7b49\u5f85\u5b50\u8fdb\u7a0b\u5b8c\u6210\nprocess.wait()\n\n# \u6e05\u7406 cgroup\nsubprocess.run([&quot;sudo&quot;, &quot;cgdelete&quot;, &quot;-g&quot;, f&quot;cpu,memory:\/{cgroup_name}&quot;])<\/code><\/pre>\n<h3>\u8be6\u7ec6\u89e3\u91ca\uff1a<\/h3>\n<ol>\n<li><strong>\u521b\u5efa cgroup<\/strong>\uff1a\u4f7f\u7528 <code>cgcreate<\/code> \u547d\u4ee4\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 cgroup\uff0c\u540d\u5b57\u4e3a <code>my_cgroup<\/code>\uff0c\u5e76\u6307\u5b9a\u63a7\u5236\u8d44\u6e90\u4e3a CPU \u548c\u5185\u5b58\u3002<\/li>\n<li><strong>\u914d\u7f6e CPU \u9650\u5236<\/strong>\uff1a\u901a\u8fc7 <code>cgset<\/code> \u547d\u4ee4\u8bbe\u7f6e <code>cpu.cfs_quota_us<\/code> \u548c <code>cpu.cfs_period_us<\/code> \u6765\u9650\u5236 CPU \u4f7f\u7528\u7387\u3002\u8fd9\u91cc\u8bbe\u7f6e\u7684\u503c\u8868\u793a\u5b50\u8fdb\u7a0b\u53ea\u80fd\u4f7f\u7528 50% \u7684 CPU \u65f6\u95f4\u3002<\/li>\n<li><strong>\u914d\u7f6e\u5185\u5b58\u9650\u5236<\/strong>\uff1a\u901a\u8fc7 <code>cgset<\/code> \u547d\u4ee4\u8bbe\u7f6e <code>memory.limit_in_bytes<\/code> \u6765\u9650\u5236\u5185\u5b58\u4f7f\u7528\u91cf\u4e3a 100MB\u3002<\/li>\n<li><strong>\u542f\u52a8\u5b50\u8fdb\u7a0b<\/strong>\uff1a\u4f7f\u7528 <code>subprocess.Popen<\/code> \u542f\u52a8\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0c\u8fd9\u91cc\u793a\u4f8b\u5b50\u8fdb\u7a0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684 Python \u811a\u672c\uff0c\u5b83\u4f1a\u7761\u7720 30 \u79d2\u3002<\/li>\n<li><strong>\u5c06\u5b50\u8fdb\u7a0b\u6dfb\u52a0\u5230 cgroup \u4e2d<\/strong>\uff1a\u4f7f\u7528 <code>cgclassify<\/code> \u547d\u4ee4\u5c06\u5b50\u8fdb\u7a0b\u7684 PID \u6dfb\u52a0\u5230\u6307\u5b9a\u7684 cgroup \u4e2d\u3002<\/li>\n<li><strong>\u7b49\u5f85\u5b50\u8fdb\u7a0b\u5b8c\u6210<\/strong>\uff1a\u8c03\u7528 <code>process.wait()<\/code> \u7b49\u5f85\u5b50\u8fdb\u7a0b\u5b8c\u6210\u3002<\/li>\n<li><strong>\u6e05\u7406 cgroup<\/strong>\uff1a\u4f7f\u7528 <code>cgdelete<\/code> \u547d\u4ee4\u5220\u9664\u521b\u5efa\u7684 cgroup\u3002<\/li>\n<\/ol>\n<p>\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7 Python \u811a\u672c\u7ed3\u5408 cgroup \u547d\u4ee4\u884c\u5de5\u5177\u5bf9\u8fdb\u7a0b\u8fdb\u884c\u8d44\u6e90\u9650\u5236\u3002\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574 CPU \u548c\u5185\u5b58\u7684\u9650\u5236\u53c2\u6570\u3002<\/p>\n<p>\u8981\u67e5\u770b\u5df2\u6709\u7684 cgroup \u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>cgget<\/code> \u547d\u4ee4\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5 cgroup-tools<\/strong>\uff08\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff09\uff1a<\/p>\n<pre><code class=\"language-bash\">sudo apt-get install cgroup-tools<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u5217\u51fa\u6240\u6709 cgroup \u7ec4<\/strong>\uff1a<br \/>\n\u4f7f\u7528 <code>lscgroup<\/code> \u547d\u4ee4\u6765\u5217\u51fa\u6240\u6709\u5f53\u524d\u5b58\u5728\u7684 cgroup \u7ec4\uff1a<\/p>\n<pre><code class=\"language-bash\">lscgroup<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>\u67e5\u770b\u7279\u5b9a cgroup \u7ec4\u7684\u5c5e\u6027<\/strong>\uff1a<br \/>\n\u4f7f\u7528 <code>cgget<\/code> \u547d\u4ee4\u6765\u67e5\u770b\u7279\u5b9a cgroup \u7ec4\u7684\u8be6\u7ec6\u5c5e\u6027\u3002\u4f8b\u5982\uff0c\u8981\u67e5\u770b\u540d\u4e3a <code>test<\/code> \u7684 cgroup \u7ec4\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-bash\">cgget -r all \/test<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u8fd9\u4e9b\u547d\u4ee4\u5c06\u5e2e\u52a9\u4f60\u7ba1\u7406\u548c\u67e5\u770b\u73b0\u6709\u7684 cgroup \u7ec4\u3002\u5982\u679c\u6709\u66f4\u591a\u7684\u9700\u6c42\u6216\u9047\u5230\u95ee\u9898\uff0c\u8bf7\u968f\u65f6\u544a\u8bc9\u6211\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/segmentfault.com\/a\/1190000007241437 \u5728 Python \u4e2d\u4f7f\u7528 cgroup \u5bf9\u8fdb\u7a0b\u8fdb   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[14],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9379"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9379"}],"version-history":[{"count":2,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9379\/revisions"}],"predecessor-version":[{"id":9381,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9379\/revisions\/9381"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9379"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}