25

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: http://www.freedesktop.org/software/systemd/man/systemd.resource-control.html 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?

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

CPUShares=18446744073709551615
StartupCPUShares=18446744073709551615
CPUQuotaPerSecUSec=100ms
LimitCPU=18446744073709551615
SteveGr2015
  • 463
  • 2
  • 7
  • 15
  • Not sayin' this is off-topic per se, but you may have better luck with this on sysadmin-focused sibling site [ServerFault](http://serverfault.com/). – mattdm Apr 17 '15 at 15:27
  • Thank you I'll try also there. – SteveGr2015 Apr 19 '15 at 21:11
  • Unfortunately not, I am using CPUShares as a temporary solution. The problem is that CPUShares does not set a hard limit but works only when there is competition for the CPU usage. – SteveGr2015 Apr 25 '15 at 19:27
  • 4
    Do you have a quad core with hyperthyroid enabled? Aka 8 logical cores? 10% of 800% is 80%. I'm just speculating but have you tried setting the limit to 1% to see what happens? – Iyad K Nov 03 '15 at 10:30
  • @IyadK this is from the man page: "The percentage specifies how much CPU time the unit shall get at maximum, relative to the total CPU time available on one CPU." The argument is still valid if by 1 CPU they do not mean individual cores, which is what [here](https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Resource_Management_Guide/sec-cpu.html) states: "Note that shares of CPU time are distributed per all CPU cores on multi-core systems" – dashesy Dec 19 '16 at 22:27
  • @SteveGr2015 CPUShares is just a weight, since default is 1024, any higher value increases its share and vice versa. Similar to nice. It does not limit. – dashesy Dec 19 '16 at 23:54
  • 4
    For reference, the ServerFault question can be found [here](https://serverfault.com/questions/683911/use-of-cpuquota-in-systemd). – TomKeegasi Jun 14 '17 at 18:40
  • Hello all, @TomKeegasi gave a detailed answer to ServerFault question which I accepted it. You may check there, why this problem probably occurred. – SteveGr2015 Mar 12 '18 at 08:11
  • @TomKeegasi Adding CONFIG_CFS_BANDWIDTH option to my kernel did the trick, thanks! – Nayfe Mar 07 '19 at 13:01

5 Answers5

2

I accidentally ran across another valid answer given on https://unix.stackexchange.com/questions/213903/linux-cgroups-limit-cpu-usage-in-absolute-values-which-do-not-depend-on-cpu-spe

If you want a hard limit on CPU bandwidth, you can use cpu.cfs_quota_us and cpu.cfs_period_us. From the Kernel's CFS docs:

The bandwidth allowed for a group is specified using a quota and period. Within each given "period" (microseconds), a group is allowed to consume only up to "quota" microseconds of CPU time. When the CPU bandwidth consumption of a group exceeds this limit (for that period), the tasks belonging to its hierarchy will be throttled and are not allowed to run again until the next period.

1

Alternate ways to limit cpu usage: 1. Use taskset command. 2. Use control groups. 3. Use docker and limit cpu utilization by using cpuset. 4. Reduce the number of threads in the application. Some calculation is required with this approach.

LakshayK
  • 162
  • 2
  • 8
1

Another thing you could try is run cpulimit as a daemon. I have not tested this; it's just an idea.

It's in the ubuntu repos:

sudo apt install cpulimit

Use systemd if you like. This should limit all instances of dd to 20% CPU.

[Unit]
Description=dd CPU Limiter

[Service]
ExecStart=/usr/bin/cpulimit -e dd -l 20

[Install]
WantedBy=multi-user.target

source

Community
  • 1
  • 1
ki9
  • 5,183
  • 5
  • 37
  • 48
-1

The option "CPUQuota=" depends on the number of CPUs on the system, because it ensures that the executed processes will never get more than the % CPU time on ONE CPU; and I think CPU usage % that you get with the cmd systemd-cgtop is on all CPUs of the system.

chafik bel
  • 151
  • 1
  • 12
-2

Implies "CPUAccounting=true" So it should be

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