Why isn’t this systemd service resource limited

cgroupssystemd

Ok to get my hands dirty with cgroups and systemd, I wrote the most moronic C program I could think of (just a timer and a spinlocking while loop) and named it idiot, which I accompanied with the following idiot.service file in /sys/fs/systemd/system/:

[Unit]
Description=Idiot - pretty idiotic imo

[Service]
Type=simple
ExecStart=/path/to/idiot
User=bruno
CPUShares=100

[Install]
WantedBy=default.target

Then I did sudo systemctl start idiot.service; top | grep idiot, which predictably told me idiot used 100% of CPU. Now, according to link, we should be able to limit the resources of this service by the following:

sudo systemctl set-property idiot.service CPUShares=100
sudo systemctl daemon-reload
sudo systemctl restart idiot.service

which I did, followed by top. But this still tells me that idiot is using 100% of CPU! What am I doing wrong?

Note: I also tried adding CPUShares=100 to the unit file, to no avail

Best Answer

According to man systemd.resource.control, CPUShares=weight would work as follows:

The available CPU time is split up among all units within one slice relative to their CPU time share weight.

Since you've told us nothing about other members of the same slice, I presume there are no other members, thus it would be appropriate for service to use all the CPU.

If you want to play with CPU control, try CPUQuota=20%. This directive is documented like this:

CPUQuota=20% ensures that the executed processes will never get more than 20% CPU time on one CPU.

Related Question