FAQ Style Article

CGroup – CPU allocation (cpu.shares) examples

In Linux Scheduler, Work In Progress on September 2, 2012 at 9:07 am

This article shows how cpu.shares can be used to limit CPU allocation to processes


What are Cgroups?
Cgroups (also known as control groups) is a Linux mechanism to control allocation of resources like CPU, Memory to processes.

Which aspects of CPU can be controlled?
(a) CPU time for processes using cpu cgroup
(b) CPU on which processes will run in case of multi-core system using cpuset cgroup

Briefly explain cpu cgroup?
cpu cgroup provides a mechanism to control cpu allocation to various processes. Each cpu cgroup contains a tuneable called cpu.shares. This tuneable allows user to limit the amount of CPU available to all processes within that cgroup.

Briefly explain various cgroup related Linux tools
(a) cgcreate – create cgroup
(b) cgdelete – remove cgroup
(c) cgclassify – Move a process to cgroup
(d) cgexec – run the task in given control groups

How to install cgroup tools?
sudo apt-get install cgroup-bin


#Create a mount directory and mount the cpu cgroup
mkdir cgmntdir
mount -t cgroup -o cpu test cgmntdir

Example – Create two busy wait processes such that each get 50% time.
(a) Create a cgroup A and B. By default, cpu.shares will be set to 1024.
sudo cgcreate -g cpu:A
sudo cgcreate -g cpu:B
(b) List the current cpu.shares value – should be 1024
cgget -r cpu.shares A
cgget -r cpu.shares B
(c) Create two processes and assign one each to each cgroup
sudo cgexec -g cpu:A dd if=/dev/zero of=/dev/null &
sudo cgexec -g cpu:B dd if=/dev/zero of=/dev/null &
(d) Check that processes take equal amount of CPU

Example – Create two processes such that one takes 75% and other 25%
Create processes P1 and P2.
Set P1 to A Cgroup and P2 to B Cgroup.
Set cpu.shares of A to 768 and cpu.shares of B to 256.
Now P1 will get 3 times as much CPU as P2.
sudo cgset -r cpu.shares=768 A
sudo cgset -r cpu.shares=256 B
sudo cgexec -g cpu:A dd if=/dev/zero of=/dev/null &
sudo cgexec -g cpu:B dd if=/dev/zero of=/dev/null &

Example – What will be CPU allocation for various scenarios

In a cgroup hierarchy, A and B are at the top. A has two sub cgroups A1 and A2 while B has one subgroup B1.
A cpu.shares is 1024
B cpu.shares is 2048
A1 cpu.shares is 512
A2 cpu.shares is 1024
B1 cpu.shares is 2048

Question – How will CPU be distributed between A and B
A share = 1024/(1024+2048) = 1/3
B share is 2048/(1024+2048) = 2/3

Question – How will CPU be distributed between A1 and A2 (subgroups within A)
Case 1: No processes within A cgroup.
A1 and A2 will share the CPU allocated to A cgroup.
A1 share = 512/(512+1024) = 1/3
A2 share = 1024/(512+1024) = 2/3

Case 2: Processes within A cgroup.
A processes share = 1024 / (1024 + 512 + 1024) = 2/5
A1 processes share = 512 / (1024 + 512 + 1024) = 1/5
A2 processes share = 1024 / (1024 + 512 + 1024) = 2/5

Question – What will be B1 (subgroup within B) share of CPU?
Case 1 : No processes running in B cgroup
B1 share = B CPU share and B1 cpu.shares value does not matter.

Case 2:
If there are some processes, then share will calculated using following formula
B1 = 2048/(2048+1024) = 2/3
B = 1024/(2048+1024) = 1/3

Question – What will be CPU allocation in following case
A – 1 process
B – 1 process
A1 – 1 process

B process will get 2/3 CPU.
A and A1 will share rest 1/3 CPU.
Within that A share = 1024/(512+1024) = 2/3 and A1 share = 512/(512 + 1024) = 1/3
In effect, B share = 2/3 = 6/9; A share = 2/3 * 1/3 = 2/9; A1 share = 1/9


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: