加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 在systemd中使用CPUQuota

发布时间:2020-12-13 16:41:08 所属栏目:Linux 来源:网络整理
导读:我试图为dd命令的CPU使用量设置一个硬限制.我创建了以下单元文件 [Unit]Description=Virtual Distributed Ethernet[Service]ExecStart=/usr/bin/ddcommandCPUQuota=10%[Install]WantedBy=multi-user.target 它调用以下简单脚本 #!/bin/shdd if=/dev/zero of=
我试图为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难以同时控制多个独立/独立的流程,在类似的组中它们在一起,但也可能有解决方案……

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读