Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit the percentage of CPU a process tree is allowed to use?

Tags:

linux

unix

Can I limit the percentage of CPU a running process and all its current and future children can use, combined? I've heard about the cpulimit tool, but that seems to ignore child processes.

Edit: So, the answer I found requires cpulimit to run constantly untill we want the limit to stay in effect, since it is doing the limiting by actively sending suspend and then continue signals to the process. Are there perhaps other ways to achieve this limiting effect, perhaps without the need for such a secondary process running in the background?

like image 949
Wizek Avatar asked Jul 25 '15 06:07

Wizek


People also ask

Which system call can be used to limit the amount of CPU time a process can use?

The cpulimit tool curbs the CPU usage of a process by pausing the process at different intervals to keep it under the defined ceiling.


2 Answers

Yes!

Just as I was writing this question, found out that I was trying an old version of cpulimit.

The new version supports limiting child processes too.

$ cpulimit -h
Usage: cpulimit [OPTIONS...] TARGET
   OPTIONS
      -l, --limit=N          percentage of cpu allowed from 0 to 400 (required)
      -v, --verbose          show control statistics
      -z, --lazy             exit if there is no target process, or if it dies
      -i, --include-children limit also the children processes
      -h, --help             display this help and exit
   TARGET must be exactly one of these:
      -p, --pid=N            pid of the process (implies -z)
      -e, --exe=FILE         name of the executable program file or path name
      COMMAND [ARGS]         run this command and limit it (implies -z)

Report bugs to <[email protected]>.
like image 130
Wizek Avatar answered Oct 18 '22 21:10

Wizek


I've been researching this problem for the last few days, and I found at least two more options: cgroups and CPU affinity

Given that this topic has been viewed more than 2k times, and it's been difficult to find a good source of information, let my post my notes here for future reference.

cgroups

  • Caveats: you can't use cgroups inside of Docker and you need root access for a one-time setup.

  • There is cgroups v1 and v2. As of 2020-04-22, only Red Hat has switched to v2 by default and you can't use both at the same time. I.e., you're probably on v1.

  • You need root to create a cgroup directory / configure your system to create one at startup and delegate access to your non-root user, like so:

    • v1: mkdir /sys/fs/cgroup/cpu/&lt;directory&gt;/ && chown -R user /sys/fs/cgroup/cpu/&lt;directory&gt;/ (this is specific to restricting CPU usage - there are other cgroup 'controllers' that use different directories; a process can be in multiple cgroups)

    • v2: mkdir /sys/fs/cgroup/unified/&lt;directory&gt; && chown -R user /sys/fs/cgroup/unified/&lt;directory&gt; (this is a unified cgroup hierarchy, and you can control all cgroup 'controllers' via a single cgroup; a process can be in only one cgroup, and that cgroup must not contain other cgroups - i.e., a leaf cgroup)

  • Configure the cgroup by writing to control files in this tree:

    • Configure CPU quota using cpu.cfs_quota_us and cpu.cfs_period_us, e.g., echo 10000 > cpu.cfs_quota_us

    • Add a process to the new cgroup by writing its pid to the cgroup.procs control file. (All subprocesses are automatically in the same cgroup.)

This link has more info: https://drill.apache.org/docs/configuring-cgroups-to-control-cpu-usage/

CPU affinity

You can only use CPU affinity to limit CPU usage to an integer number of logical CPUs (aka cpu cores), not to a specific percentage. On today's multi-core systems, that may be good enough.

The documentation for this feature is at $ man sched_setaffinity. Note that cgroups also support setting CPU affinity through the cpuset controller.

like image 3
Ulf Adams Avatar answered Oct 18 '22 21:10

Ulf Adams