[CentOS] how to limit cpu usage for specified processes

Wed Sep 7 09:18:11 UTC 2016
Gianluca Cecchi <gianluca.cecchi at gmail.com>

On Wed, Sep 7, 2016 at 9:52 AM, Sander Kuusemets <sander.kuusemets at ut.ee>
wrote:

> Hello,
>
> You could do it with Cgroups, if your specified processes run or can be
> made to run in group permissions.
>
> https://access.redhat.com/documentation/en-US/Red_Hat_Enterp
> rise_Linux/6/html/Resource_Management_Guide/sec-cpu_and_
> memory-use_case.html
>
> Regards,
>
> --
> Sander Kuusemets
> University of Tartu, High Performance Computing, IT Specialist
> Skype: sander.kuusemets1
> +372 737 5694
>
>
hello,
In the past I mainly used blkio controller in control groups, to limit disk
I/O bandwidth of a process.
Just to notice that the cpu controller also has the cfs_quota_us and
cfs_period_us which can provide absolute limits, in respect with relative
ones of the shares parameter.
In Red Hat manual I only see the shares parameter, but you can find more
info about the other two installing kernel-doc package and reading
/usr/share/doc/kernel-doc-2.6.32/Documentation/scheduler/sched-bwc.txt

A rapid test on a CentOS 6.5 system with 2 physical cpus, 8 cores each and
HT enabled

yum install libcgroup

service start cgconfig

cgcreate -g cpu:/half_cpu

echo "2000" > /cgroup/cpu/half_cpu/cpu.cfs_quota_us
echo "4000" > /cgroup/cpu/half_cpu/cpu.cfs_period_us

You can tune latency/burst working on period value... see the doc
I use a program that makes matrix multiplication (1024x1024 in example
below) and fully uses one cpu

./bmm 1024
elapsed 10 seconds
using top during execution I see line detail of this type around the 32
threads:
Cpu18 :100.0%us,  0.0%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  0.0%si,
 0.0%st

cgexec -g cpu:half_cpu ./bmm 1024
elapsed 20 seconds
using top I see line detail of this type during execution:
Cpu0  : 49.8%us,  0.0%sy,  0.0%ni, 50.2%id,  0.0%wa,  0.0%hi,  0.0%si,
 0.0%st

HIH,
Gianluca