opsengine / cpulimit

CPU usage limiter for Linux
Other
1.7k stars 270 forks source link

sytemd service file ? #103

Open promeneur opened 2 years ago

promeneur commented 2 years ago

Can you supply a systemd service file ?

Thanks

Freso commented 8 months ago

What would you want a service file for this to do/provide?

Mikaela commented 5 months ago

I am also curious considering systemd service files can already do this for what it manages.

[Service]
CPUQuota=20%

See also man systemd.resource-control

promeneur commented 5 months ago

What would you want a service file for this to do/provide?

Sorry @Freso I am late.

In the past, before systemd, we put the statement cpulimit in a special system file according to start it during system startup.

With systemd now all is started with a systemd service file. I speak about a service file to start cpulimit.

Please I am not a techie person about systemd so a ref to systemd doc is not for me.

Thanks

Mikaela commented 5 months ago

What I am saying is that when you write a systemd service file, in addition to ExecStart=/usr/local/bin/mydaemon you can add CPUQuota=20% without having to call cpulimit separately in the service.

A fuller example (although where I cut Unit.{After,Wants} and Service.{Environment,WorkingDirectory,PIDFile,ExecReload,Restart,RestartSec}) would be

[Unit]
Description=R-66Y Limnoria instance (IRC bot)

[Service]
Type=simple
ExecStart=/home/limnoria/venv/bin/limnoria /home/limnoria/R-66Y/R-66Y.conf
User=limnoria
CPUQuota=30%

[Install]
WantedBy=multi-user.target
promeneur commented 5 months ago

@Mikaela thanks

But the problem is a way complex for my case.

An app FAH is started by systemd service file.

FAH launches randomly others app named "FahCore_a8", "Fahcore_22".

So we must launch a service file which detects that "Fahcore_a8" and "Fahcore_22" are running and limits their cpu consumption to 20 %.

Mikaela commented 5 months ago

I think what you should do is systemctl edit whatever-starts-fah.service and there add the

[Service]
CPUQuota=20%

and it should just work without having to even touch the actual service file (as this creates a drop-in file e.g. /etc/systemd/system/whatever-starts-fah.service.d/override.conf).

promeneur commented 5 months ago

@Mikaela

thanks

I will try this as soon as I have some time.

promeneur commented 5 months ago

20% is not enough according Fahcore_a8 consumes 20 %. We need CPUQuota=200%.

More

With CPUQuota=200% FahCore_22 consumes only 5 % instead of 20 % !

My conf is :

I choose how many cores are used, but Fah decides how to dispatch them between Fahcore_a8 and Fahcore_22.

Mikaela commented 5 months ago

I don't understand what is going on there, but I do have another idea (that actually includes cpulimit this time 😅)

  1. Check what is the service that starts fah in /usr/lib/systemd/system/whatever.service and take note on the ExecStart= line.
  2. sudo systemctl edit whatever.service
  3. Add the following override
[Service]
# Empty ExecStart erases whatever ExecStart has been specified before
ExecStart=
# and do adjust the flags to be more suitable for you
ExecStart=/usr/bin/cpulimit --lazy --include-children --limit=30 /usr/bin/fahcore-or-whatever
  1. sudo systemctl daemon-reload
  2. sudo systemctl restart whatever.service

My third idea would be creating a systemd oneshot service and a timer for running cpulimit with --pid or --exe, but at that point it would be shorter to just use crontab for it assuming it was available and I am still unconvinced about it being a systemd service file for cpulimit, while I guess my second idea could be documented somewhere.

PS. Sorry for long response time, I seem to have went a couple of days without reading my email.

promeneur commented 5 months ago

@Mikaela

No problem.

The ExecStart of Fah-client service :

ExecStart=/usr/local/bin/fah-client --config=/etc/fah-client/config.xml --log=/var/log/fah-client/log.txt --log-rotate-dir=/var/log/fah-client/

Are you sure the new ExecStart runs without any problem ?

ExecStart=/usr/bin/cpulimit --lazy --include-children --limit=30 /usr/local/bin/fah-client --config=/etc/fah-client/config.xml --log=/var/log/fah-client/log.txt --log-rotate-dir=/var/log/fah-client/

promeneur commented 5 months ago

I created a cpulimit service.

[Unit] Description=Run cpulimit

[Service] Type=oneshot RemainAfterExit=yes ExecStart=cpulimit --include-children --exe fah-client --limit 75

[Install] WantedBy=multi-user.target

--include-children is not an option of cpulimit. This lead to an exit code when sarting cpulimit service.

promeneur commented 5 months ago

I discovered a big problem.

openSUSE supplies a cpulimit package which is in fact Limitcpu 3.0 see https://limitcpu.sourceforge.net/

it is not the original cpulimit from here

promeneur commented 5 months ago

i keep my previous solution.

a script cpulimit.sh

cpulimit --exe FahCore_a7 --limit 50 & cpulimit --exe FahCore_a8 --limit 50 & cpulimit --exe FahCore_22 --limit 75 & cpulimit --exe FahCore_23 --limit 75 &

when it is needed

a service to launch the script

[Unit] Description=Run cpulimit

[Service] Type=oneshot RemainAfterExit=yes ExecStart=/home/roubach/bin/cpulimit.sh

[Install] WantedBy=multi-user.target

Anyway, thanks.