![为什么 cpu.cfs_quota_us 不限制 LXC 容器的 CPU 带宽?](https://linux22.com/image/94973/%E4%B8%BA%E4%BB%80%E4%B9%88%20cpu.cfs_quota_us%20%E4%B8%8D%E9%99%90%E5%88%B6%20LXC%20%E5%AE%B9%E5%99%A8%E7%9A%84%20CPU%20%E5%B8%A6%E5%AE%BD%EF%BC%9F.png)
我想将容器限制为系统总 CPU 带宽的 25%。
这是我的设置:
- LXC版本1.0.2
- 内核3.2.45
- 一位用户为 LXC 容器创建了 cgroup (foo)
- 主机上有 40 个可用内核
- 主机和容器对每个其他 cgroup 子系统都有默认值,除了:
/sys/fs/cgroup/cpu/lxc/foo/cpu.cfs_quota_us = 400000
/sys/fs/cgroup/cpu/lxc/foo/cpu.cfs_period_us = 100000
/sys/fs/cgroup/cpuset/lxc/foo/cpuset.cpus = 0-15
我使用以下公式计算了配额:
(# of cpus available to container) * (cpu.cfs_period_us) * (.25) so 16 * 100000 * .25 = 400000
我在容器内部和外部同时运行了基本的stress-ng,以衡量每秒允许内部和外部的操作次数,结果与使用配额“-1”运行基本相同,也就是说没有配额。
外跑:
$ ./stress-ng --cpu-load 50 -c 40 --timeout 20s --metrics-brief
stress-ng: info: [25649] dispatching hogs: 40 cpu
stress-ng: info: [25649] successful run completed in 20.44s
stress-ng: info: [25649] stressor bogo ops real time usr time sys time bogo ops/s bogo ops/s
stress-ng: info: [25649] (secs) (secs) (secs) (real time) (usr+sys time)
stress-ng: info: [25649] cpu 37348 20.18 380.56 0.58 1850.85 97.99
内跑:
$ ./stress-ng --cpu-load 100 -c 16 --timeout 20s --metrics-brief
stress-ng: info: [34256] dispatching hogs: 16 cpu
stress-ng: info: [34256] successful run completed in 20.10s
stress-ng: info: [34256] stressor bogo ops real time usr time sys time bogo ops/s bogo ops/s
stress-ng: info: [34256] (secs) (secs) (secs) (real time) (usr+sys time)
stress-ng: info: [34256] cpu 24147 20.03 205.20 0.17 1205.67 117.58
根据操作,我得到了 39%。为什么会发生这种情况?难道不应该被限制吗cpu.cfs_quota_us
?
我在这里先向您的帮助表示感谢。
答案1
想发布这个问题的答案,以防其他人看到类似的令人困惑的结果。看来我有两个问题:
需要使用主机上的 cpu 数,而不是 cgroups cpuset 中的可用 CPU 数来估计 CPU 带宽:
(# of cpus on the host) * (cpu.cfs_period_us) * (.25) so 40 * 100000 * .25 = 1000000
我在容器内运行的stress-ng使用/lxc/foo cgroup的cpu和cpuset控制器,而在容器外部运行的stress-ng使用/system/sshd.service cgroup
为了更好地模拟我的现实世界应用程序,我应该通过使用指定要使用的控制器执行程序:
$ cgexec -g cpuset:/lxc/foo -g cpu:/lxc/foo ./stress-ng --cpu-load 100 -c 48 --times --timeout 10s --metrics-brief
stress-ng: info: [6252] dispatching hogs: 48 cpu
stress-ng: info: [6252] successful run completed in 10.36s
stress-ng: info: [6252] stressor bogo ops real time usr time sys time bogo ops/s bogo ops/s
stress-ng: info: [6252] (secs) (secs) (secs) (real time) (usr+sys time)
stress-ng: info: [6252] cpu 11152 10.09 102.83 0.12 1105.60 108.32
stress-ng: info: [6252] for a 10.36s run time:
stress-ng: info: [6252] 414.46s available CPU time
stress-ng: info: [6252] 102.85s user time ( 24.82%)
stress-ng: info: [6252] 0.12s system time ( 0.03%)
stress-ng: info: [6252] 102.97s total time ( 24.84%)