linux – 在systemd中使用CPUQuota

我试图为dd命令的cpu使用量设置一个硬限制.我创建了以下单元文件
[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

正如我所见,07d,我的dd服务的cpu使用率不应超过10%.但是当我运行system-cgtop命令时,使用率约为70-75%.

关于我做错了什么以及如何解决它的任何想法?

当我执行systemctl show dd时,我得到了关于cpu的以下结果

cpuShares=18446744073709551615
StartupcpuShares=18446744073709551615
cpuQuotaPerSecUSec=100ms
Limitcpu=18446744073709551615

解决方法

好的

你的解决方案是正确的,实际上应该是非常适合未来的;通过使用systemd来控制服务cgroup设置,例如. cpuQota.

[Unit]
Description=Virtual distributed Ethernet

[Service]
ExecStart=/usr/bin/ddcommand
cpuQuota=10%

[Install]
WantedBy=multi-user.target

有关systemd中更有用的cgroup设置,请参阅man systemd.resource-control.

虽然有两点需要注意,我(可能还有其他几个)会对此进行讨论.这些警告很难追查,因为似乎没有太多容易找到的信息,这是这个答案的主要原因.

警告1:

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包中的cgcreate和cgset手动配置cpu.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

警告2

对于可用的设置cpu.cfs_period_us和cpu.cfs_quota_us,需要使用config-flag CONfig_CFS_BANDWIDTH编译内核.遗憾的是,认情况下,Debian Jessie的3.16.x内核未使用此标志进行编译,请参阅此feature request.

这将在Debian Stretch中提供.也可以使用jessie-backports中的内核,它应该启用标志.

我希望这个答案能帮助一些与我有同样问题的人……

PS:在您的环境中测试更好的cpuquota的简单方法是:

$apt-get install stress
$systemd-run -p cpuQuota=25% --slice stress --cpu <your cpu count>

并且使用顶部或顶部观察,负载应该(均匀地)分布在所有cpus /核心上,总计高达25%.

替代

作为替代工具,可以使用cpu-limit,这应该在大多数发行版中可用,例如.

$apt-get install cpulimit
$cpulimit -l 10 -P /usr/bin/ddcommand

它的工作原理是将SIGSTOP和SIGCONT发送到附加命令以暂停和恢复其操作.

AFAIK难以同时控制多个独立/独立的流程,在类似的组中它们在一起,但也可能有解决方案……

相关文章

1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...
如何抑制stable_secret读取关键的“net.ipv6.conf.all.stabl...
1 删除0字节文件 find -type f -size 0 -exec rm -rf {} ...
## 步骤 1:安装必要的软件包 首先,需要确保系统已安装 `dh...