4

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

  • I think I have a vague idea - my service is probably not attached to the cpu controller... but I thought systemctl was supposed to handle this for me. Ugghh – embedded_crysis Feb 28 '17 at 14:31
  • CPUShares are relative to the CPUShares of other processes on the system - are there other processes on your system that could use the CPU at the time idiot is running? – Wieland Feb 28 '17 at 14:35
  • Well, they want more than 0% at least, although nothing huge. But I thought systemctl was supposed to function as a strict resource limiter... – embedded_crysis Feb 28 '17 at 14:39
5

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.

| improve this answer | |
  • Ah, I had no idea the unit of CPUShares was weight! Fantastic! – embedded_crysis Feb 28 '17 at 14:51
  • Haha, this actually worked perfectly but actually got my computer to freeze up to the point of not even responding to REISUB. Although RT_PREEMPT may be part of the reason here. – embedded_crysis Feb 28 '17 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.