參考:
https://segmentfault.com/a/1190000008323952
1。 找到cgroup設置的地方。
[root@D128 ~]# mount -l |grep cpu cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,nosuid,nodev,noexec,relatime,cpuset) cgroup on /sys/fs/cgroup/cpu,cpuacct type cgroup (rw,nosuid,nodev,noexec,relatime,cpuacct,cpu) [root@D128 ~]# cd /sys/fs/cgroup/cpu,cpuacct/ [root@D128 cpu,cpuacct]# mkdir nlb [root@D128 cpu,cpuacct]# cd nlb/ [root@D128 nlb]# ls cgroup.clone_children cgroup.procs cpuacct.usage cpu.cfs_period_us cpu.rt_period_us cpu.shares notify_on_release cgroup.event_control cpuacct.stat cpuacct.usage_percpu cpu.cfs_quota_us cpu.rt_runtime_us cpu.stat tasks [root@D128 nlb]#
2. 關聯到線程。
[root@D128 nlb]# pidof qemu-system-x86_64 7429 [root@D128 nlb]# echo 7429 > cgroup.procs [root@D128 nlb]# cat cgroup.procs 7429 [root@D128 nlb]#
3. 限制使用一顆CPU的25%資源
cpu.cfs_period_us和cpu.cfs_quota_us來限制該組中的所有進程在單位時間里可以使用的cpu時間。這里的cfs是完全公平調度器的縮寫。cpu.cfs_period_us就是時間周期(微秒),默認為100000,即百毫秒。cpu.cfs_quota_us就是在這期間內可使用的cpu時間(微秒),默認-1,即無限制。(cfs_quota_us是cfs_period_us的兩倍即可限定在雙核上完全使用)。
[root@D128 nlb]# echo 50000 > cpu.cfs_period_us [root@D128 nlb]# echo 12500 > cpu.cfs_quota_us [root@D128 nlb]# cat cpu.cfs_period_us 50000 [root@D128 nlb]# cat cpu.cfs_quota_us 12500
4. 使用top查看
[root@D128 nlb]# echo 50000 > cpu.cfs_period_us [root@D128 nlb]# echo 12500 > cpu.cfs_quota_us [root@D128 nlb]# cat cpu.cfs_period_us 50000 [root@D128 nlb]# cat cpu.cfs_quota_us 12500
5. 演示腳本
qemu-system-x86_64 -enable-kvm xxx yyy &
PID=$?
echo $PID > /sys/fs/cgroup/cpu,cpuacct/nlb/cgroup.procs echo 50000 > /sys/fs/cgroup/cpu,cpuacct/nlb/cpu.cfs_period_us echo 50000 > /sys/fs/cgroup/cpu,cpuacct/nlb/cpu.cfs_quota_us
6. 移除cgroup
要把進程移出控制組,把 pid 寫入到根 cgroup 的 tasks 文件即可。因為每個進程都屬於且只屬於一個 cgroup,加入到新的 cgroup 后,原有關系也就解除了。要刪除一個 cgroup,
可以用 rmdir 刪除相應目錄。不過在刪除前,必須先讓其中的進程全部退出,對應子系統的資源都已經釋放,否則是無法刪除的。
https://xiezhenye.com/2013/10/linux-cgroups-%E6%A6%82%E8%BF%B0.html