What happens when you have many levels of sub-directory of CPU cgroup? what's the expect behavior for each process inside or outside those sub-directory? I hope this post helps.
TL;DR
When Linux scheduler processes, each cgroup with running processes will be considered as a single entity the same other processes at the same level. Scheduler will assign CPU shares to each entity based on its weight (priority). Each group will then distribute CPU share to all processes and cgroups under it, follow the same rule
Assumptions:
1. We have N running (running, not all) processes/threads all need lots of CPU (if there's on competition, then each process will get the CPU they need)
2. All processes have default priority/nice value
3. We only have 1 CPU (to make calculation simple)
Case 1:
No sub-directory/sub-group (this is same as without CPU cgroup)
each process will get 1/N CPU time, so if N is 10, then each one will get 10% CPU
Case 2:
create several, say 3, sub-directories under CPU cgroup root without changing default values, and put some process, into each of them. Let's assume we put 1, 2 and 3 processes into different group.
Now, total processes under root cgroup is N-(1+2+3) = N - 6, when calculate CPU shares, the 3 cgroup will be same as these N - 6 processes (remember, all processes we are talking here are running proccesses), no matter how many processes within these cgroup requires CPU.
so each process under root cgroup as well as each cgroup will get 1/( N -6 + 3) of total CPU. 1/(N - 6 + 3) = 1/(N - 3), this CPU time will be distribute to all processes within same cgroup. so
process in subgroup1 will get 1/(N - 3)
each process in subgroup2 will get 1/(N-3) / 2
each process in subgroup3 will get 1/(N-3) / 3
Case 3:
you create a sub-group under subgroup2 (subgroup2_1)and put some new processes into it. Now, within subgroup2, we have 2 processes and 1 cgroup, since theres no change at the same level as subgroup2, total CPU share subgroup2 get is still 1/(N-3), and each process as well as subgroup2_1 will get 1/(N-3) / (1+2) CPU. And each process within subgroup2_1 will get equal share from 1/(N-3) / (1+2)
Till now, we know how to calculate CPU shares for each process with multiple levels of cgroup with default setting. What happens if we change a process' priority/nice or cgroup's cpu.shares?
by default, a process' weight for scheduling is 1024 which corresponding to nice level 0, when increase/decrease priority by renice, each level will be roughly 1.25x or 1/1.25x of previous one.
so the highest nice value (-20) will get weight 88761 and lowest (+15) will get weight 15.
cgroup's weight is defined in cpu.shares, and default is 1024.
Let's use Wi (i from 1 to N) for weight of each running process and cgroup with running process at the same level, then total weight will be W=W1+W2+W3+ ...... + Wn , first entity (process or cgroup) will get W1/W CPU shares, second one will get W2/W and so on and so forth. If the entity is a cgroup, then CPU shares assigned to the group will distribute to all running processes and (if any) cgroups under it follow the same rule.
Comments
Post a Comment