我正在尝试在dd命令中使用CPU使用的硬限制。 我创build了以下单元文件
[Unit] Description=Virtual Distributed Ethernet [Service] ExecStart=/usr/bin/ddcommand CPUQuota=10% [Install] WantedBy=multi-user.target
其中调用以下简单的脚本
#!/bin/sh dd if=/dev/zero of=/dev/null bs=1024k
正如我在本指南中看到的,我的dd服务的CPU使用率不应超过10%。 但是当我运行system-cgtop命令时,使用率大约是70-75%。
任何想法,我做错了什么,我该如何解决?
当我执行systemctl show dd我得到以下关于CPU的结果
CPUShares=18446744073709551615 StartupCPUShares=18446744073709551615 CPUQuotaPerSecUSec=100ms LimitCPU=18446744073709551615
你的解决scheme是正确的,应该是相当有前途的; 通过使用systemd来控制服务cgroup设置,例如。 CPUQota。
[Unit] Description=Virtual Distributed Ethernet [Service] ExecStart=/usr/bin/ddcommand CPUQuota=10% [Install] WantedBy=multi-user.target
在systemd中查看man systemd.resource-control更多有用的cgroup设置。
有两个警告,虽然,我( 可能还有一些其他 )践踏。 这些警告真的很难追查,因为似乎没有太多容易find的信息,这是这个答案的主要原因。
CPUQuota设置仅在systemd 213之后可用,请参阅https://github.com/systemd/systemd/blob/master/NEWS
* The CFS CPU quota cgroup attribute is now exposed for services. The new CPUQuota= switch has been added for this which takes a percentage value. Setting this will have the result that a service may never get more CPU time than the specified percentage, even if the machine is otherwise idle.
这是Debian Jessie的一个问题,只有systemd 208才有。另一种方法是使用cgroup-bin软件包中的cpu.cfs_period_us和cpu.cfs_quota_us手动configurationcpu.cfs_period_us和cpu.cfs_quota_us ,例如。
sudo cgcreate -g cpu:/cpulimited sudo cgset -r cpu.cfs_period_us=50000 cpulimited sudo cgset -r cpu.cfs_quota_us=10000 cpulimited sudo cgexec -g cpu:cpulimited /usr/bin/ddcommand
要使cpu.cfs_period_us和cpu.cfs_quota_us可用,需要使用config-flag CONFIG_CFS_BANDWIDTH编译内核。 不幸的是, Debian Jessie的3.16.x内核默认情况下不会使用此标志进行编译,请参阅此function请求 。
这将在Debian Stretch中可用。 也可以使用jessie-backports中的内核,该内核应该启用标志。
我希望这个答案可以帮助一些和我有同样问题的人。
PS:一个简单的方法来testing你的CPUquota在你的环境中工作的是:
$ apt-get install stress $ systemd-run -p CPUQuota=25% --slice stress --cpu <your cpu count>
并观察top或top ,负载应分散(均匀)横跨所有的cpus / cores,总计达25%。
作为一个替代工具,可以使用cpu-limit,在大多数发行版中都可以使用,例如。
$ apt-get install cpulimit $ cpulimit -l 10 -P /usr/bin/ddcommand
它通过向附加命令发送SIGSTOP和SIGCONT来暂停和恢复操作。
AFAIK很难同时控制多个单独的/独立的进程,就像把它们组合在一起 ,但也可能有解决scheme…