大网数据广告活动
服务器运维问答

Use of CPUQuota in systemd

· 52CE 我爱测

I am trying to put a hard limit in CPU usage for a dd command . I have created the following unit file [Unit] Description=Virtual Distributed Ethernet [Service] ExecStart=/usr/bin/ddcommand CPUQuota=10% [Install] WantedBy=multi-u…

问题原文

原帖链接 · SteveGr2015 · CC BY-SA 3.0

I am trying to put a hard limit in CPU usage for a dd command . I have created the following unit file

[Unit]
Description=Virtual Distributed Ethernet

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

[Install]
WantedBy=multi-user.target

which call the following simple script

#!/bin/sh
dd if=/dev/zero of=/dev/null bs=1024k

As I have seen in this guide, the CPU usage for my dd service should not exceed the 10%. But when I run the system-cgtop command the usage is about 70-75% .

Any ideas of what am I doing wrong and how can I fix it?

When I execute systemctl show dd I get the following results regarding CPU

CPUShares=18446744073709551615
StartupCPUShares=18446744073709551615
CPUQuotaPerSecUSec=100ms
LimitCPU=18446744073709551615

已采纳解答

原帖链接 · TomKeegasi · CC BY-SA 4.0

The Good

Your solution is the correct and should actually be quite future-proof; by using systemd to control the services cgroup settings, eg. CPUQota.

[Unit]
Description=Virtual Distributed Ethernet

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

[Install]
WantedBy=multi-user.target

See the man systemd.resource-control for more useful cgroup settings in systemd.

The Bad

There are two caveats to this though, which I (and possibly a few others) stumpled upon. Those caveats are really difficult to track down as there does not seem to be much easily findable information about this, which is the main reason for this answer.

Caveat 1:

The CPUQuota setting is only available since systemd 213, see 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.

This is for example an issue with Debian Jessie which only comes with systemd 208. As an alternative one could configure cpu.cfs_period_us and cpu.cfs_quota_us manually using cgcreate and cgset from the cgroup-bin package, eg.

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

Caveat 2

For the settings cpu.cfs_period_us and cpu.cfs_quota_us to be available the Kernel needs to be compiled with config-flag CONFIG_CFS_BANDWIDTH. Sadly the 3.16.x Kernel for Debian Jessie is not compiled with this flag by default, see this feature request.

This will be available in Debian Stretch though. One could also use the kernel from jessie-backports, which should have the flag enabled.


I hope this answer helps a few people with the same issue as me...

PS: An easy way to test wether CPUquota is working in your environment is:

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

and watch with top or htop, the load should be spread (evenly) accross all cpus/cores, summing up to 25%.

Alternative

As an alternative tool one could use cpu-limit which should be available in most distros, eg.

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

It works by sending SIGSTOP and SIGCONT to the attached command to pause and resume its operation.

AFAIK it was difficult to control multiple separate/stand-alone processes simultaneously with this, in like group them together, but there might also be solution for this...

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-04-19)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。