Confusion in CPU Shares and the need for CPU Limits

2/21/2021

I've recently been trying to wrap my head around CPU Shares and CPU Limits after seeing my application getting CPU Throttled while running in Kubernetes. I've read a lot of articles but still has some confusion. I'll try to explain my understanding.

CPU Shares: CPU Shares are relative weights which define how much CPU time a particular process can get if there are multiple runnable processes in the system.

CPU Limits: In a defined time period (say 100 ms), what's the quota (maximum time) a process can run within that period. Once the process exhausts its quota, it has to wait for the next period and will remain throttled in the existing period even if its in runnable state.

Suppose a 1 CPU Machine with processes A and B in runnable state with respective CPU shares being 2048 and 1024 and no CPU Limits set. According to this, when both the processes are runnable, the CPU time will be divided in a 2:1 ratio and process A would run 66.66% of the time (or 666ms in 1 sec) and process B would run 33.33% of the time (or 333ms in 1 sec).

  • If the processes remain in Runnable state throughout, do they get contiguous time slice of first 666ms and then 333ms? Is the 100ms of CPU period involved in breaking it further down to 66ms and 33ms for every 100ms period?

Since a running process can also get into a waiting state (Disk I/O, Network I/O), the waiting time would actually be wasted and during this time if any other process in the system is in runnable state and is waiting for CPU, it would be allocated the CPU and allowed to run.

  • In this case, will the 2:1 ratio of CPU time be honored? If yes, how?

  • CPU Shares are frequently called "soft limits". Is it because if some process who's exhausted its CPU time as per CPU shares but some other process who hasn't yet exhausted its CPU goes into a waiting state and suddenly there's a waste of that allotted CPU time?

  • How can not setting CPU limits for processes starve a process of CPU time if CPU shares have the job of allocating CPU time so that no CPU starvation doesn't occur?

-- Shubham
cpu
cpu-usage
kubernetes
linux-kernel

0 Answers