Linux Kernel 5.14
I&#x27;m very excited for the better CFS quota enforcement mechanism in 5.14 (<a href="https:&#x2F;&#x2F;lwn.net&#x2F;Articles&#x2F;844976&#x2F;" rel="nofollow">https:&#x2F;&#x2F;lwn.net&#x2F;Articles&#x2F;844976&#x2F;</a>).<p>Kubernetes implements CPU and memory limits for containers (<a href="https:&#x2F;&#x2F;kubernetes.io&#x2F;docs&#x2F;concepts&#x2F;configuration&#x2F;manage-resources-containers&#x2F;" rel="nofollow">https:&#x2F;&#x2F;kubernetes.io&#x2F;docs&#x2F;concepts&#x2F;configuration&#x2F;manage-res...</a>). The memory limit works the way you&#x27;d expect: there&#x27;s a memory cgroup, and Linux implements the memory-allocation algorithm based on the memory cgroup&#x27;s limit and not the total system memory. Wildly-overboard allocations get denied; somewhat-overboard allocations are permitted if you have overcommit turned on, but cause an OOM kill if you try to use all of it. This is at least expected behavior for existing non-Kubernetes applications and (more importantly) for people used to non-Kubernetes application development&#x2F;deployment: it&#x27;s just like if you were running on a Linux system with the limited memory.<p>CPU limits are much wackier. Because CPUs are a discrete resource and Kubernetes supports fractional CPU allocations, they can&#x27;t use CPU pinning. Instead, they use the CFS quota controls, which aren&#x27;t commonly used outside Kubernetes as far as I can tell. The quota is expressed as basically a fraction: for a process with, say, 0.5 vCPUs allocated, it gets to use 50 ms of CPU time every 100 ms of real time. The way it works at the kernel level, as best as I&#x27;ve been able to understand, is that after a process runs on the CPU for a bit, the kernel looks at the quota and sees if it has exceeded the quota. If it has, the process becomes unschedulable until it has &quot;re-earned&quot; sufficient quota. It does <i>not</i> slow down or proactively de-schedule the process, analogous to how it would have run on a slower or fewer-CPU computer.<p>So, if you have multiple processes or threads running in your container, and there are free cores for them to run, you can easily use up multiple seconds of CPU time in those 100 ms. Then you have to wait to re-earn your quota in 50 ms chunks before your process can run. And all the while the process appears to be runnable, not stuck, it just seems like the kernel is never choosing to schedule it.<p>We have seen, in production, processes stalled for <i>minutes</i> because they evidently ran so far past their quota. We have seen entire system deadlocks when such a stalled process is holding some kernel lock for network I&#x2F;O, in our case, Ceph (if the kernel releases the process into interruptible sleep, then that process can still hold locks; it will release those locks if signaled and rescheduled, but in this case the process isn&#x27;t being scheduled at all).<p>And we&#x27;re far from the only people who have seen awful behavior from CFS quotas. Tim Hockin, Kubernetes co-creator and tech lead, advises people to just never use the CPU limit feature! <a href="https:&#x2F;&#x2F;twitter.com&#x2F;thockin&#x2F;status&#x2F;1134193838841401345" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;thockin&#x2F;status&#x2F;1134193838841401345</a> We have done as he advises and just turned it off cluster-wide with --cpu-cfs-quota=false - it helps the Kubernetes scheduler if you set your CPU&#x2F;memory limits equal to your requests, so this gets us the right Kubernetes behavior without incurring the wacky kernel behavior. If you Google that flag you can find many others who have done the same in their public configurations. There&#x27;s a Kubernetes feature to use CPU pinning instead, which mentions improved performance as an advantage <a href="https:&#x2F;&#x2F;kubernetes.io&#x2F;blog&#x2F;2018&#x2F;07&#x2F;24&#x2F;feature-highlight-cpu-manager&#x2F;" rel="nofollow">https:&#x2F;&#x2F;kubernetes.io&#x2F;blog&#x2F;2018&#x2F;07&#x2F;24&#x2F;feature-highlight-cpu-...</a> , but it only works if you&#x27;re scheduling whole numbers of CPUs always.<p>A whole lot of applications on our Kubernetes clusters - lightly-accessed web servers, monitoring daemons, etc. - are best described as wanting 0.1 or 0.5 CPUs, in the sense that on (say) an 8-core machine, you could run many dozens of instances of these applications just fine. But that&#x27;s not how the existing CFS quota mechanism interprets 0.1 or 0.5 CPUs. It seems like the &quot;burst&quot; mechanism here is likely to handle processes that, in a random 100 ms period, actually do run for more than 10 ms, but on an ongoing basis don&#x27;t average out to using more than one tenth of a CPU. We might be able to turn --cpu-cfs-quota back on as a result.