<p>I am confused of the concept of <a href="https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#meaning-of-cpu" rel="nofollow noreferrer">millicores</a> in Kubernetes. As per my programming knowledge, only <strong>one</strong> thread can run per core so why would I set a limit in millicores?</p>
<p>For example, if I set a CPU limit of <code>600m</code> to a container, can I use <code>400m</code> for another pod or container? is that possible?
I have tried installing minikube and ran on it.
Will both containers or pods run different threads? Please if anyone can explain.</p>

<p>It's best to see <em>millicores</em> as a way to express fractions, <em>x</em> millicores correspond to the fraction x/1000 (e.g. 250millicores = 250/1000 = 1/4).<br>
The value 1 represent the complete usage of 1 core (or hardware thread if hyperthreading or any other SMT is enabled).  </p>

<p>So 100mcpu means the process is using 1/10th of a single CPU time. This means that it is using 1 second out of 10, or 100ms out of a second or 10us out of 100.<br>
Just take any unit of time, divide it into ten parts, the process is running only for one of them.<br>
Of course, if you take a too short interval (say, 1us), the overhead of the scheduler becomes non-negligeable but that's not important.</p>

<p>If the value is above 1, then the process is using more than one CPU. A value of 2300mcpu means that out of, say, 10 seconds, the process is running for... 23!<br>
This is used to mean that the process is using 2 whole CPUs and a 3/10 of a third one.<br>
This may sound weird but it's no different to saying: "I work out 3.5 times a week" to mean that "I work out 7 days every 2 weeks".</p>

<p>Remember: millicores represent a <em>fraction of CPU time</em> not of CPU number. So 2300mcpu is 230% the time of a single CPU.</p>

<hr>

<p>What I hate about technologies like Kubernetes and Docker is that they hide too much, confusing seasoned programmers.</p>

<p>The <em>millicores</em> unit arises, at its base, from the way the Linux scheduler works. It doesn't divide the time into quanta and assigns each thread the CPU for a quantum, instead, it runs a thread until it's unfair to keep it running. So a thread can run for a variable time.  </p>

<p>The current Linux scheduler, <a href="https://www.linuxjournal.com/node/10267" rel="noreferrer">named CFS</a>, works with the concept of <em>waiting time</em>.<br>
Each thread has a waiting time, a counter that is incremented each nanosecond (but any sufficiently fine unit of time will do) that the thread is waiting to execute and that is decremented each nanosecond the thread is executing.<br>
The threads are then ordered by their wait time <em>divided</em> the total number of threads, the thread with the greatest wait time is picked up and run <em>until its wait time (that now is decreasing) falls below the wait time of another thread (which will be then scheduled)</em>.<br>
So if we have one core (without HyperThreading or any other SMT) and four threads, after, say, a second, the scheduler will have allocated 1/4 of that second (250ms) to each thread.<br>
You can say that each thread used 250millicores. This means it uses 250/1000 = 1/4 of the core time on average. The "core time" can be any amount of time, granted it is far greater than the scheduler wallclock. So 250millicores means 1 minute of time every 4, or 2 days every 8. </p>

<p>When a system has multiple CPUs/cores, the waiting time is scaled to account for that.
Now if a thread is scheduled, over the course of 1 second, to two CPUs for the whole second, we have an usage of 1/1 for the first CPU and 1/1 for the second one. A total of 1/1 + 1/1 = 2 or 2000mcpu.<br>
This way of counting CPU times, albeit weird at first, at the advantage that it is absolute. 100mcpu means 1/10 of a CPU, no matter how many CPUs there are, this is by design.<br>
If we counted time in a relative matter (i.e. where the value 1 means all the CPUs) then a value like 0.5 would mean 24 CPUs in a 48 CPUs system and 4 in an 8 CPUs system.<br>
It would be hard to compare timings.</p>

<p>The Linux scheduler doesn't actually know about <em>millicores</em>, as we have seen it uses the <em>waiting time</em> and doesn't need any other measurement unit.<br>
That <em>millicores</em> unit is just a unit we make up, so far, for our convenience.<br>
However, it will turn out this unit will arise naturally due to how containers are constrained.  </p>

<p>As implied by its name, the Linux scheduler is fair: all threads are equals. But you don't always want that, a process in a container should not hog all the cores on a machine.<br>
This is where <em>cgroups</em> comes into play. It is a kernel feature that is used, along with namespace and union fs, to implement containers.<br>
Its main goal is to restrict processes, including their CPU bandwidth.</p>

<p><a href="https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt" rel="noreferrer">This is done with two parameters, a <em>period</em> and a <em>quota</em></a>.<br>
The restricted thread is allowed, by the scheduler, to run for <em>quota</em> microseconds (us) every <em>period</em> us.<br>
Here, again, a <em>quota</em> greater than the <em>period</em> means using more than one CPU. Quoting the kernel documentation:</p>

<blockquote>
  <ol>
  <li><p>Limit a group to 1 CPU worth of runtime.
  If period is 250ms and quota is also 250ms, the group will get
  1 CPU worth of runtime every 250ms.</p></li>
  <li><p>Limit a group to 2 CPUs worth of runtime on a multi-CPU machine.
  With 500ms period and 1000ms quota, the group can get 2 CPUs worth of
  runtime every 500ms.</p></li>
  </ol>
</blockquote>

<p>We see how, given <em>x</em> millicores, we can compute the quota and the period.<br>
We can fix the period to 100ms and the quota to (100 * x) / 1000.<br>
This is how <a href="https://docs.docker.com/config/containers/resource_constraints/#cpu" rel="noreferrer">Docker does it</a>.<br>
Of course, we have an infinite choice of pairs, we set the period to 100ms but indeed we can use any value (actually, there aren't infinite value but still).<br>
Larger values of the period mean the thread can run for a longer time but will also pause for a longer time.<br>
Here is where Docker is hiding things from the programmer, using an arbitrary value for the period in order to compute the quota (given the <em>millicores</em>, which the authors dub as more "user-friendly").</p>

<p><a href="https://kubernetes.io/docs/tasks/configure-pod-container/assign-cpu-resource/#specify-a-cpu-request-and-a-cpu-limit" rel="noreferrer">Kubernetes is designed around Docker</a> (yes, it can use other container managers but they must expose an interface similar to the Docker's one), and the Kubernetes <em>millicores</em> unit match the unit used by Docker in its <code>--cpus</code> parameter.  </p>

<hr>

<p>So, long story short, <em>millicores</em> are the fractions of <em>time</em> of a single CPU (not the fraction of number of CPUs).<br>
<em>Cgroups</em>, and hence Docker, and hence Kubernetes, doesn't restrict CPU usage by assigning cores to processes (like VMs do), instead it restricts CPU usage by restricting the amount of time (quota over period) the process can run on each CPU (with each CPU taking up to 1000mcpus worth of allowed time).</p>
