<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0"><channel><title><![CDATA[Amir Karimi]]></title><description><![CDATA[Software Engineer, LLMs enthusiast, open-source contributor]]></description><link>https://amirkarimi.substack.com</link><image><url>https://substackcdn.com/image/fetch/$s_!-Of9!,w_256,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F37b4e799-a1af-4791-a478-c82deccc9470_640x640.jpeg</url><title>Amir Karimi</title><link>https://amirkarimi.substack.com</link></image><generator>Substack</generator><lastBuildDate>Sat, 25 Jul 2026 14:39:33 GMT</lastBuildDate><atom:link href="https://amirkarimi.substack.com/feed" rel="self" type="application/rss+xml"/><copyright><![CDATA[Amir Karimi]]></copyright><language><![CDATA[en]]></language><webMaster><![CDATA[amirkarimi@substack.com]]></webMaster><itunes:owner><itunes:email><![CDATA[amirkarimi@substack.com]]></itunes:email><itunes:name><![CDATA[Amir Karimi]]></itunes:name></itunes:owner><itunes:author><![CDATA[Amir Karimi]]></itunes:author><googleplay:owner><![CDATA[amirkarimi@substack.com]]></googleplay:owner><googleplay:email><![CDATA[amirkarimi@substack.com]]></googleplay:email><googleplay:author><![CDATA[Amir Karimi]]></googleplay:author><itunes:block><![CDATA[Yes]]></itunes:block><item><title><![CDATA[Wasted Time to Peak Performance: Behind the Code of Linux's Scheduler Evolution [Originally published on Medium, Apr 2024]]]></title><description><![CDATA[Tracing the Linux scheduler from round-robin roots to the Completely Fair Scheduler and the ideas that keep pushing it forward.]]></description><link>https://amirkarimi.substack.com/p/wasted-time-to-peak-performance-behind</link><guid isPermaLink="false">https://amirkarimi.substack.com/p/wasted-time-to-peak-performance-behind</guid><dc:creator><![CDATA[Amir Karimi]]></dc:creator><pubDate>Sat, 13 Jun 2026 17:08:02 GMT</pubDate><enclosure url="https://substackcdn.com/image/fetch/$s_!pGOn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a2d931-b53c-47f1-ac11-836961452e73_800x800.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<div><hr></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!pGOn!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a2d931-b53c-47f1-ac11-836961452e73_800x800.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!pGOn!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a2d931-b53c-47f1-ac11-836961452e73_800x800.png 424w, https://substackcdn.com/image/fetch/$s_!pGOn!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a2d931-b53c-47f1-ac11-836961452e73_800x800.png 848w, https://substackcdn.com/image/fetch/$s_!pGOn!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a2d931-b53c-47f1-ac11-836961452e73_800x800.png 1272w, https://substackcdn.com/image/fetch/$s_!pGOn!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a2d931-b53c-47f1-ac11-836961452e73_800x800.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!pGOn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a2d931-b53c-47f1-ac11-836961452e73_800x800.png" width="800" height="800" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a0a2d931-b53c-47f1-ac11-836961452e73_800x800.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:800,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:false,&quot;topImage&quot;:true,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!pGOn!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a2d931-b53c-47f1-ac11-836961452e73_800x800.png 424w, https://substackcdn.com/image/fetch/$s_!pGOn!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a2d931-b53c-47f1-ac11-836961452e73_800x800.png 848w, https://substackcdn.com/image/fetch/$s_!pGOn!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a2d931-b53c-47f1-ac11-836961452e73_800x800.png 1272w, https://substackcdn.com/image/fetch/$s_!pGOn!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa0a2d931-b53c-47f1-ac11-836961452e73_800x800.png 1456w" sizes="100vw" fetchpriority="high"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Credit: Canva AI Image generator</figcaption></figure></div><p>(Originally posted here in April, 2024: <br><a href="https://medium.com/@jgalt99/wasted-time-to-peak-performance-behind-the-code-of-linuxs-scheduler-evolution-d15a7a3e32cf">https://medium.com/@jgalt99/wasted-time-to-peak-performance-behind-the-code-of-linuxs-scheduler-evolution-d15a7a3e32cf)</a></p><p>TL;DR:</p><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://amirkarimi.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div><ul><li><p>Linux Kernel used a simple round-robin scheduler in the first years. Obviously, not useful for a modern computer as it was not efficient in terms of time complexity (O(n)) and also differentiating between processes.</p></li><li><p>In 2003, the new implementation of the Linux scheduler, named O(1), was introduced. More efficient, but unfair to interactive tasks.</p></li><li><p>The CFS algorithm was a suggested alternative to the O(1). Merged into the kernel in 2007. Got patches afterward.</p></li><li><p>New ideas for Linux&#8217;s scheduler, like using ML, are on the way for specific use cases.</p></li></ul><div><hr></div><p>But hold on, geeks! Shun the shallows!</p><p>Linux&#8217;s Kernel is widely used in more than 3 billion devices(!), and no doubt the scheduler plays a vital role in Linux&#8217;s Kernel. A scheduler is a system software component that manages how tasks are executed on a computer. It is responsible for deciding which task to run next and how long. In a multitasking operating system, there are typically many tasks running simultaneously, so the scheduler must carefully manage the CPU to ensure that all tasks can progress. It must consider various factors, such as the priority of each process, the amount of CPU time each process has already used, and the availability of other resources. Maybe the most heated debates were over this part of Kernel. In this article, I want to elaborate briefly on its background and how it evolved to become what we now have. Let&#8217;s get a better understanding of the Kernel scheduler, which is also helpful in understanding other scheduling systems.</p><div><hr></div><h3>Part I: Genesis of Kernel&#8217;s Scheduler</h3><blockquote><p>&#8216;schedule()&#8217; is the scheduler function. It&#8217;s a very simple and nice<br>scheduler: it&#8217;s not perfect, but certainly works for most things.</p><p>Linus Torvalds comment on the schedule() function</p></blockquote><p>From 1991 when Torvalds published the first version of Kernel to 2003, in which O(1) scheduler was introduced, Kernel&#8217;s scheduler was a simple round-robin algorithm. In a round-robin scheduler, each task is given an equal amount of time to run, and then the scheduler switches to the next task in the queue. Easy to implement and understand.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!OY1R!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa056336f-5ed8-49a0-a86e-98f61e595e80_800x957.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!OY1R!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa056336f-5ed8-49a0-a86e-98f61e595e80_800x957.png 424w, https://substackcdn.com/image/fetch/$s_!OY1R!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa056336f-5ed8-49a0-a86e-98f61e595e80_800x957.png 848w, https://substackcdn.com/image/fetch/$s_!OY1R!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa056336f-5ed8-49a0-a86e-98f61e595e80_800x957.png 1272w, https://substackcdn.com/image/fetch/$s_!OY1R!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa056336f-5ed8-49a0-a86e-98f61e595e80_800x957.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!OY1R!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa056336f-5ed8-49a0-a86e-98f61e595e80_800x957.png" width="800" height="957" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/a056336f-5ed8-49a0-a86e-98f61e595e80_800x957.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:957,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!OY1R!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa056336f-5ed8-49a0-a86e-98f61e595e80_800x957.png 424w, https://substackcdn.com/image/fetch/$s_!OY1R!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa056336f-5ed8-49a0-a86e-98f61e595e80_800x957.png 848w, https://substackcdn.com/image/fetch/$s_!OY1R!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa056336f-5ed8-49a0-a86e-98f61e595e80_800x957.png 1272w, https://substackcdn.com/image/fetch/$s_!OY1R!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fa056336f-5ed8-49a0-a86e-98f61e595e80_800x957.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Kernel&#8217;s first scheduler</figcaption></figure></div><p>As you can see <em><a href="https://github.com/AMK9978/ArchaicKernel/blob/4b5fd829cb2d8aa9be3e1dc48149c7ea00d5a0d3/kernel/sched.c">here</a></em> or download it from <em><a href="https://mirrors.edge.kernel.org/pub/linux/kernel/Historic/old-versions/">here</a></em>, first, the scheduler checks for any tasks that have an alarm that has expired. If there are any such tasks, they are marked as runnable (<em>TASK_RUNNING</em>). Then, there is a while loop over all of the runnable tasks to find the task with the <strong>highest</strong> <strong>counter</strong>. The counter is a value that is decremented each time a task runs. When the counter reaches zero, the task is preempted. If there is no runnable task with a counter greater than zero, the <strong>schedule()</strong> function increments the counter of each runnable task by half. This ensures that all runnable tasks will eventually be able to run. Once the <strong>schedule() </strong>function has found the task with the highest counter, it switches to that task. This code stayed almost the same till the 2.6 version was introduced 12 years later in 2003.</p><div><hr></div><h3>Part II: In The Ring: The Wasteful Scheduler</h3><blockquote><p>Linus, sharing queue and lock between CPUs for a &#8220;thing&#8221; highly frequency<br>( schedule()s + wakeup()s ) accessed like the scheduler it&#8217;s quite ugly<br>and it&#8217;s not that much funny. And it&#8217;s not only performance wise, it&#8217;s<br>more design wise&#8230;</p><p>Why not 2.5.x ?</p><p>Davide Libenzi</p></blockquote><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!zYXV!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14a9345a-0d47-4893-a85e-1ac5bc6382ad_500x759.jpeg" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!zYXV!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14a9345a-0d47-4893-a85e-1ac5bc6382ad_500x759.jpeg 424w, https://substackcdn.com/image/fetch/$s_!zYXV!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14a9345a-0d47-4893-a85e-1ac5bc6382ad_500x759.jpeg 848w, https://substackcdn.com/image/fetch/$s_!zYXV!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14a9345a-0d47-4893-a85e-1ac5bc6382ad_500x759.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!zYXV!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14a9345a-0d47-4893-a85e-1ac5bc6382ad_500x759.jpeg 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!zYXV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14a9345a-0d47-4893-a85e-1ac5bc6382ad_500x759.jpeg" width="500" height="759" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/14a9345a-0d47-4893-a85e-1ac5bc6382ad_500x759.jpeg&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:759,&quot;width&quot;:500,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!zYXV!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14a9345a-0d47-4893-a85e-1ac5bc6382ad_500x759.jpeg 424w, https://substackcdn.com/image/fetch/$s_!zYXV!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14a9345a-0d47-4893-a85e-1ac5bc6382ad_500x759.jpeg 848w, https://substackcdn.com/image/fetch/$s_!zYXV!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14a9345a-0d47-4893-a85e-1ac5bc6382ad_500x759.jpeg 1272w, https://substackcdn.com/image/fetch/$s_!zYXV!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F14a9345a-0d47-4893-a85e-1ac5bc6382ad_500x759.jpeg 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a></figure></div><p>In Dec 2001, <em><strong><a href="https://lkml.iu.edu/hypermail/linux/kernel/0112.2/0333.html">Scheduler ( was: Just a second )</a> </strong></em>broke out a series of debates over the deficits of the initial kernel scheduler. The whole thread and its following threads are worth a glimpse, but for brevity, I just highlight its important discussions leading to the O(1) algorithm. The biggest issues with the kernel&#8217;s round-robin scheduler were:</p><p>1. Global <strong>runqueue</strong> for all CPU processors, leading to wasted time locking and unlocking the jobs. Ideally, each processor should have its own locking and individual runqueue. A runqueue is the list of runnable processes from which the scheduler chooses. Therefore, the scheduler needs to have an optimized way to group tasks on each core, and only do <strong>work stealing</strong> (migrate tasks from one core to another) to resolve imbalances in runqueue length.</p><p>2. O(n) time complexity. Therefore, the new scheduler should provide O(1) scheduling.</p><p>The interesting takeaway from their discussion is how Libenzi and other new scheduler proponents presented the old scheduler&#8217;s pros and cons, and how Linus defends the old one. Linus in his <a href="https://lkml.iu.edu/hypermail/linux/kernel/0112.2/0336.html">first response</a> wrote:</p><blockquote><p>&#8220;Design wise&#8221; is highly overrated.</p><p>Simplicity is _much_ more important, if something commonly is only done a<br>few hundred times a second. Locking overhead is basically zero for that<br>case&#8230;<br>4 cpu&#8217;s are &#8220;high end&#8221; today. We can probably point to tens of thousands<br>of UP machines for each 4-way out there. The ratio gets even worse for 8,<br>and 16 CPU&#8217;s is basically a rounding error.</p><p>You have to prioritize. Scheduling overhead is way down the list.</p><p>Linus</p><p>So are we talking about &#8220;we spend 40&#8211;60% of all CPU cycles in the<br>scheduler&#8221; or are we talking about &#8220;we spend 40&#8211;60% of the CPU power of<br>_one_ CPU out of 8 in the scheduler&#8221;.</p><p>Yes, 40&#8211;60% sounds like a lot (&#8220;Wow! About half the time is spent in the<br>scheduler&#8221;), but I bet it&#8217;s 40&#8211;60% of _one_ CPU, which really translates<br>to &#8220;The worst scheduler case I&#8217;ve ever seen under a real load spent 5&#8211;8%<br>of the machine CPU resources on scheduling&#8221;.</p><p>Linus</p><p>&#8230;I&#8217;ve told you a number of times that I&#8217;d like to see the preliminary<br>implementation publicly discussed and some uses outside of private<br>companies that I have no insight into..</p><p>Linus</p></blockquote><p>A few days later, Ingo Moln&#225;r suggested the <a href="https://lkml.iu.edu/hypermail/linux/kernel/0201.0/0810.html">O(1) scheduler patch</a>.</p><h3>Part III: O(1) comes into play</h3><p>In 2003, the Linux kernel scheduler was upgraded from a simple round-robin algorithm to the O(1) scheduler. This means that the O(1) scheduler can schedule tasks in constant time. A significant improvement.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!Ng1A!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23a78e74-71e1-4b1e-90fa-fdf8b1d5e843_800x855.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!Ng1A!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23a78e74-71e1-4b1e-90fa-fdf8b1d5e843_800x855.png 424w, https://substackcdn.com/image/fetch/$s_!Ng1A!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23a78e74-71e1-4b1e-90fa-fdf8b1d5e843_800x855.png 848w, https://substackcdn.com/image/fetch/$s_!Ng1A!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23a78e74-71e1-4b1e-90fa-fdf8b1d5e843_800x855.png 1272w, https://substackcdn.com/image/fetch/$s_!Ng1A!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23a78e74-71e1-4b1e-90fa-fdf8b1d5e843_800x855.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!Ng1A!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23a78e74-71e1-4b1e-90fa-fdf8b1d5e843_800x855.png" width="800" height="855" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/23a78e74-71e1-4b1e-90fa-fdf8b1d5e843_800x855.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:855,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!Ng1A!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23a78e74-71e1-4b1e-90fa-fdf8b1d5e843_800x855.png 424w, https://substackcdn.com/image/fetch/$s_!Ng1A!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23a78e74-71e1-4b1e-90fa-fdf8b1d5e843_800x855.png 848w, https://substackcdn.com/image/fetch/$s_!Ng1A!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23a78e74-71e1-4b1e-90fa-fdf8b1d5e843_800x855.png 1272w, https://substackcdn.com/image/fetch/$s_!Ng1A!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F23a78e74-71e1-4b1e-90fa-fdf8b1d5e843_800x855.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">Simplified O(1) code. See its original implementation <a href="https://github.com/AMK9978/ArchaicKernel/blob/415b3effd8b37744e690db1c87b6f9ce059034f0/kernel/sched.c#L1471">here</a></figcaption></figure></div><p>The O(1) scheduler achieves constant-time scheduling by using per-CPU runqueues and two arrays: the <strong>active array</strong> and the <strong>expired array</strong>. The active array contains all the tasks currently ready to run. The expired array contains all of the tasks that have been running and have now expired (their counters have reached 0). When a task expires, it is moved from the active array to the expired array, and its counter is replenished by half of its initial counter. Therefore, it can be run again, and the scheduler allows new tasks not to be starved. When the scheduler needs to select a new task to run, it simply selects the first task in the active array. When all tasks expire, a switch between active and expired arrays happens via swapping their pointers to be efficient.</p><p>Please note that the above explanation was for the normal tasks (<a href="https://github.com/AMK9978/ArchaicKernel/blob/415b3effd8b37744e690db1c87b6f9ce059034f0/include/linux/sched.h#L126">SCHED_NORMAL</a>) and not real-time ones. The real-time tasks are always considered active and if multiple real-time tasks happen to have the same priority, the scheduler applies the FIFO method.</p><p>The O(1) scheduler has several advantages over the round-robin scheduler:</p><ul><li><p><strong>Improved time complexity:</strong> The O(1) scheduler can schedule tasks much faster than the round-robin scheduler. This is because the O(1) scheduler does not need to iterate through all of the tasks in the runqueue to find the next task to run. Instead, it simply selects the first task in the active array.</p></li><li><p><strong>Improved scalability:</strong> In the previous one, the more processors, the more lock contention because of the global runqueue. Now, we&#8217;ve got a multi-queue scheduler, each processor one queue.</p></li><li><p><strong>Improved affinity:</strong> Instead of bouncing processes between multiple processors (ping-pong effect), now, thanks to the multi-queue design, processes stay on the same processor. Just in case of balancing the number of processes between processors, a special load balancer mechanism migrates processes to even out the runqueues, which is infrequent.</p></li><li><p><strong>Better support for interactive tasks:</strong> The O(1) scheduler uses a priority-based scheduling algorithm, which ensures that interactive tasks (tasks that require immediate user feedback, such as typing or clicking) are given more CPU time than non-interactive tasks. It marks a process interactive based on its average time sleep (time spent to receive input from the user).</p></li><li><p><strong>More flexible:</strong> The O(1) scheduler allows for tasks to be assigned priorities, giving the scheduler more control over how CPU time is allocated to tasks. This is in contrast to the round-robin scheduler, which does not allow for tasks to be assigned different priorities.</p></li></ul><p>Overall, the O(1) scheduler represented a notable advancement over the round-robin scheduler in performance, scalability, and flexibility.</p><div><hr></div><h3>Part IV: Controversies</h3><p>Despite all of the improvements that the O(1) scheduler had brought with itself, within a short time, it went through a series of heated debates over its fairness and effectiveness.</p><p>One of the main criticisms of the O(1) scheduler was that it could be unfair to interactive tasks. This was because the O(1) scheduler used a priority-based algorithm, prioritizing tasks running for longer periods. This means that interactive tasks, which typically have shorter runtimes, could be starved of CPU time by longer-running tasks.</p><p>Another criticism of the O(1) scheduler was that it could be inefficient for handling I/O-bound tasks. I/O-bound tasks are tasks that spend a lot of time waiting for I/O operations to complete. The O(1) scheduler treated all tasks equally, regardless of whether they were I/O-bound or CPU-bound. This meant that I/O-bound tasks could waste CPU time by spinning in tight loops while waiting for I/O operations to complete.</p><p>One example of such debate is a debate that took place in 2003 between Linus Torvalds and Andrew Morton, a prominent Linux kernel developer. Morton argued that the O(1) scheduler was unfair to interactive tasks because it prioritized tasks running longer. Torvalds argued that the O(1) scheduler was necessary to improve performance and scalability and that he was willing to sacrifice fairness to achieve those goals.</p><p>Based on the &#8220;<a href="https://blog.acolyer.org/2016/04/26/the-linux-scheduler-a-decade-of-wasted-cores/">The Linux Scheduler: a Decade of Wasted Cores</a>&#8221;, it calls the O(1) scheduler &#8220;wasted cores&#8221; because it can lead to underutilization of CPU cores because the O(1) scheduler does not always choose the best task to run on a given CPU core. For example, imagine once a task entered the expired queue, it would not get a chance to run again until all other tasks in the active queue had executed for their timeslices. This caused issues for interactive tasks, introducing jitter into interactions that should appear seamless and not making any fairness guarantees.</p><p>Another example is when the O(1) scheduler is used on a NUMA system. A NUMA system is a type of computer architecture that has multiple memory nodes. Each memory node has its CPU cores and memory. The O(1) scheduler can sometimes choose to run tasks on CPU cores that are not close to the memory node that the task is using. This can lead to decreased performance because the task will have to access memory from a remote node.</p><div><hr></div><h3>Part V: Transition to CFS</h3><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!PGkz!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850cf1c6-0789-4be1-a548-13f65c67e7db_459x293.gif" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!PGkz!,w_424,c_limit,f_webp,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850cf1c6-0789-4be1-a548-13f65c67e7db_459x293.gif 424w, https://substackcdn.com/image/fetch/$s_!PGkz!,w_848,c_limit,f_webp,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850cf1c6-0789-4be1-a548-13f65c67e7db_459x293.gif 848w, https://substackcdn.com/image/fetch/$s_!PGkz!,w_1272,c_limit,f_webp,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850cf1c6-0789-4be1-a548-13f65c67e7db_459x293.gif 1272w, https://substackcdn.com/image/fetch/$s_!PGkz!,w_1456,c_limit,f_webp,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850cf1c6-0789-4be1-a548-13f65c67e7db_459x293.gif 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!PGkz!,w_1456,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850cf1c6-0789-4be1-a548-13f65c67e7db_459x293.gif" width="459" height="293" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/850cf1c6-0789-4be1-a548-13f65c67e7db_459x293.gif&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:293,&quot;width&quot;:459,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!PGkz!,w_424,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850cf1c6-0789-4be1-a548-13f65c67e7db_459x293.gif 424w, https://substackcdn.com/image/fetch/$s_!PGkz!,w_848,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850cf1c6-0789-4be1-a548-13f65c67e7db_459x293.gif 848w, https://substackcdn.com/image/fetch/$s_!PGkz!,w_1272,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850cf1c6-0789-4be1-a548-13f65c67e7db_459x293.gif 1272w, https://substackcdn.com/image/fetch/$s_!PGkz!,w_1456,c_limit,f_auto,q_auto:good,fl_lossy/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F850cf1c6-0789-4be1-a548-13f65c67e7db_459x293.gif 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">CFS algorithm for scheduling tasks using <strong>Red-Black tree</strong> structure. <a href="https://svalaks.medium.com/linux-internals-completely-fair-scheduling-cfs-cpu-scheduler-algorithm-7412c08d2e37">source</a></figcaption></figure></div><blockquote><p>&#8220;CFS is a good scheduler. It&#8217;s fair, it&#8217;s efficient, and it scales well. It&#8217;s the right scheduler for the Linux kernel.&#8221;</p><p>Linus Torvalds on CFS</p></blockquote><p>The Linux kernel integrated the <strong>Completely Fair Scheduler</strong> (CFS) into its 2.6.23 release, making it the default scheduler for tasks in the <strong>SCHED_NORMAL</strong> class (i.e., those without real-time execution constraints). CFS manages CPU resource allocation for processes, striving to optimize overall CPU utilization and enhance interactive performance.</p><p>It works as such:</p><ol><li><p>The leftmost node of the scheduling tree is chosen (as it will have the lowest spent <em>execution time</em>) and sent for execution.</p></li><li><p>If the process simply completes execution, it is removed from the system and scheduling tree.</p></li><li><p>If the process reaches its <em>maximum execution time</em> or is otherwise stopped (voluntarily or via interrupt) it is reinserted into the scheduling tree based on its newly spent <em>execution time</em>.</p></li><li><p>The new leftmost node will then be selected from the tree, repeating the iteration.</p></li></ol><p>CFS uses the Red-Black tree, whose time complexity for insert, search, and delete is O(log(n)). Moreover, CFS applies to groups instead of each task, meaning there are two groups: A and B. A has 4 tasks to run while B has 6. What CFS does is share the CPU power equally between them to 50%, and then the power is distributed among those tasks based on their weight (priority) and the time they have waited for the CPU. In other words, while there is one runqueue per CPU, there can be multiple runqueues (runqueue hierarchy) attributed to each of those runqueues because of multiple groups.</p><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!kIrh!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeff8530-1ed6-4b00-a652-34425e75dda2_800x400.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!kIrh!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeff8530-1ed6-4b00-a652-34425e75dda2_800x400.png 424w, https://substackcdn.com/image/fetch/$s_!kIrh!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeff8530-1ed6-4b00-a652-34425e75dda2_800x400.png 848w, https://substackcdn.com/image/fetch/$s_!kIrh!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeff8530-1ed6-4b00-a652-34425e75dda2_800x400.png 1272w, https://substackcdn.com/image/fetch/$s_!kIrh!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeff8530-1ed6-4b00-a652-34425e75dda2_800x400.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!kIrh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeff8530-1ed6-4b00-a652-34425e75dda2_800x400.png" width="800" height="400" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/deff8530-1ed6-4b00-a652-34425e75dda2_800x400.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:400,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!kIrh!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeff8530-1ed6-4b00-a652-34425e75dda2_800x400.png 424w, https://substackcdn.com/image/fetch/$s_!kIrh!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeff8530-1ed6-4b00-a652-34425e75dda2_800x400.png 848w, https://substackcdn.com/image/fetch/$s_!kIrh!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeff8530-1ed6-4b00-a652-34425e75dda2_800x400.png 1272w, https://substackcdn.com/image/fetch/$s_!kIrh!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2Fdeff8530-1ed6-4b00-a652-34425e75dda2_800x400.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">A piece of <a href="https://github.com/torvalds/linux/blob/ddf28352b80c86754a6424e3a61e8bdf9213b3c7/include/linux/sched.h#L1084">sched.h</a> code</figcaption></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!4auA!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e5936a0-08b0-4be7-91b8-5d3de435ef65_800x428.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!4auA!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e5936a0-08b0-4be7-91b8-5d3de435ef65_800x428.png 424w, https://substackcdn.com/image/fetch/$s_!4auA!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e5936a0-08b0-4be7-91b8-5d3de435ef65_800x428.png 848w, https://substackcdn.com/image/fetch/$s_!4auA!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e5936a0-08b0-4be7-91b8-5d3de435ef65_800x428.png 1272w, https://substackcdn.com/image/fetch/$s_!4auA!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e5936a0-08b0-4be7-91b8-5d3de435ef65_800x428.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!4auA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e5936a0-08b0-4be7-91b8-5d3de435ef65_800x428.png" width="800" height="428" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/9e5936a0-08b0-4be7-91b8-5d3de435ef65_800x428.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:428,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!4auA!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e5936a0-08b0-4be7-91b8-5d3de435ef65_800x428.png 424w, https://substackcdn.com/image/fetch/$s_!4auA!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e5936a0-08b0-4be7-91b8-5d3de435ef65_800x428.png 848w, https://substackcdn.com/image/fetch/$s_!4auA!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e5936a0-08b0-4be7-91b8-5d3de435ef65_800x428.png 1272w, https://substackcdn.com/image/fetch/$s_!4auA!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F9e5936a0-08b0-4be7-91b8-5d3de435ef65_800x428.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">A piece of<a href="https://github.com/torvalds/linux/blob/ddf28352b80c86754a6424e3a61e8bdf9213b3c7/kernel/sched.c#L4257"> sched.c</a> code</figcaption></figure></div><div class="captioned-image-container"><figure><a class="image-link image2 is-viewable-img" target="_blank" href="https://substackcdn.com/image/fetch/$s_!EhYu!,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F73a26509-1141-4b58-93e4-c89d5da08ad4_800x1133.png" data-component-name="Image2ToDOM"><div class="image2-inset"><picture><source type="image/webp" srcset="https://substackcdn.com/image/fetch/$s_!EhYu!,w_424,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F73a26509-1141-4b58-93e4-c89d5da08ad4_800x1133.png 424w, https://substackcdn.com/image/fetch/$s_!EhYu!,w_848,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F73a26509-1141-4b58-93e4-c89d5da08ad4_800x1133.png 848w, https://substackcdn.com/image/fetch/$s_!EhYu!,w_1272,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F73a26509-1141-4b58-93e4-c89d5da08ad4_800x1133.png 1272w, https://substackcdn.com/image/fetch/$s_!EhYu!,w_1456,c_limit,f_webp,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F73a26509-1141-4b58-93e4-c89d5da08ad4_800x1133.png 1456w" sizes="100vw"><img src="https://substackcdn.com/image/fetch/$s_!EhYu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F73a26509-1141-4b58-93e4-c89d5da08ad4_800x1133.png" width="800" height="1133" data-attrs="{&quot;src&quot;:&quot;https://substack-post-media.s3.amazonaws.com/public/images/73a26509-1141-4b58-93e4-c89d5da08ad4_800x1133.png&quot;,&quot;srcNoWatermark&quot;:null,&quot;fullscreen&quot;:null,&quot;imageSize&quot;:null,&quot;height&quot;:1133,&quot;width&quot;:800,&quot;resizeWidth&quot;:null,&quot;bytes&quot;:null,&quot;alt&quot;:null,&quot;title&quot;:null,&quot;type&quot;:null,&quot;href&quot;:null,&quot;belowTheFold&quot;:true,&quot;topImage&quot;:false,&quot;internalRedirect&quot;:null,&quot;isProcessing&quot;:false,&quot;align&quot;:null,&quot;offset&quot;:false}" class="sizing-normal" alt="" srcset="https://substackcdn.com/image/fetch/$s_!EhYu!,w_424,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F73a26509-1141-4b58-93e4-c89d5da08ad4_800x1133.png 424w, https://substackcdn.com/image/fetch/$s_!EhYu!,w_848,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F73a26509-1141-4b58-93e4-c89d5da08ad4_800x1133.png 848w, https://substackcdn.com/image/fetch/$s_!EhYu!,w_1272,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F73a26509-1141-4b58-93e4-c89d5da08ad4_800x1133.png 1272w, https://substackcdn.com/image/fetch/$s_!EhYu!,w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fsubstack-post-media.s3.amazonaws.com%2Fpublic%2Fimages%2F73a26509-1141-4b58-93e4-c89d5da08ad4_800x1133.png 1456w" sizes="100vw" loading="lazy"></picture><div class="image-link-expand"><div class="pencraft pc-display-flex pc-gap-8 pc-reset"><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container restack-image"><svg aria-hidden="true" width="20" height="20" viewBox="0 0 20 20" fill="none" stroke-width="1.5" stroke="var(--color-fg-primary)" stroke-linecap="round" stroke-linejoin="round" xmlns="http://www.w3.org/2000/svg"><g><path d="M2.53001 7.81595C3.49179 4.73911 6.43281 2.5 9.91173 2.5C13.1684 2.5 15.9537 4.46214 17.0852 7.23684L17.6179 8.67647M17.6179 8.67647L18.5002 4.26471M17.6179 8.67647L13.6473 6.91176M17.4995 12.1841C16.5378 15.2609 13.5967 17.5 10.1178 17.5C6.86118 17.5 4.07589 15.5379 2.94432 12.7632L2.41165 11.3235M2.41165 11.3235L1.5293 15.7353M2.41165 11.3235L6.38224 13.0882"></path></g></svg></button><button tabindex="0" type="button" class="pencraft pc-reset pencraft icon-container view-image"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-maximize2 lucide-maximize-2"><polyline points="15 3 21 3 21 9"></polyline><polyline points="9 21 3 21 3 15"></polyline><line x1="21" x2="14" y1="3" y2="10"></line><line x1="3" x2="10" y1="21" y2="14"></line></svg></button></div></div></div></a><figcaption class="image-caption">A piece of <a href="https://github.com/torvalds/linux/blob/ddf28352b80c86754a6424e3a61e8bdf9213b3c7/kernel/sched_fair.c#L1939">sched_fair.c</a> code</figcaption></figure></div><p>As you can see above and in the source code, the scheduler code is modularized into different implementation codes (sched_fair.c, sched_rt.c) based on the policy (<a href="https://github.com/torvalds/linux/blob/ddf28352b80c86754a6424e3a61e8bdf9213b3c7/include/linux/sched.h#L36">SCHED_NORMAL, SCHED_RR, etc</a>). In the above example, you can see how <em>pick_next_task,</em> which is called in the <em>sched.c</em> is implemented in <em>sched_fair.c.</em></p><p>CFS was chosen as the default scheduler for Linux kernel v2.6.23 because it addresses the main drawbacks of the O(1) scheduler:</p><ul><li><p><strong>Fairness</strong>: CFS is fairer to interactive tasks than the O(1) scheduler. This is because CFS uses a priority-based scheduling algorithm, which gives higher priority to interactive tasks.</p></li><li><p><strong>Efficiency</strong>: CFS is more efficient than the O(1) scheduler for handling I/O-bound tasks. This is because CFS does not waste CPU time by spinning I/O-bound tasks in tight loops while waiting for I/O operations to complete.</p></li><li><p><strong>Scalability</strong>: CFS is more scalable than the O(1) scheduler. This is because CFS can handle a large number of tasks running on the system without sacrificing performance, while its time complexity is O(log(n)).</p></li></ul><p>One of the main reasons why Torvalds changed his mind about CFS was because of the work of <strong>Ingo Moln&#225;r</strong>, the main developer of CFS. Moln&#225;r could demonstrate that CFS could achieve performance and scalability comparable to the O(1) scheduler, while also being fairer and more efficient.</p><p>Another reason why Torvalds changed his mind about CFS was because of the feedback from Linux kernel users. Many users reported that CFS was a major improvement over the O(1) scheduler, especially for interactive tasks and I/O-bound tasks.</p><p>In 2007, Torvalds merged CFS into the Linux kernel and made it the default scheduler for the <strong>SCHED_NORMAL</strong> class. This was a significant decision, as it meant that CFS would be used by the vast majority of tasks running on Linux systems.</p><div><hr></div><h3>Part VI: Continued developments</h3><p>The Linux kernel scheduler has continued to evolve since the introduction of the Completely Fair Scheduler (CFS) in 2007. New features and performance improvements have been added to CFS over the years, making it even more efficient and fair.</p><p>One of the notable developments in the Linux kernel scheduler has been supporting heterogeneous multi-core processors (<strong>HMPs</strong>). HMPs are processors that have a mix of different cores with varying clock speeds or instruction sets. The Linux kernel scheduler now supports HMPs by using a technique called dynamic voltage and frequency scaling (<strong>DVFS</strong>). DVFS allows the scheduler to adjust both the clock speed and voltage of each core based on the workload. This helps to improve performance and reduce power consumption.</p><p>One area of active research in the Linux kernel scheduler community is the use of <strong>machine learning </strong>to improve scheduling decisions. Machine learning could be used to predict the future workload and to adjust the scheduler accordingly. This could help to improve performance and fairness.</p><p>Another area of active research is the use of <strong>distributed scheduling</strong> to improve scalability. Distributed scheduling would allow the scheduler to distribute the load of scheduling tasks across multiple CPU cores. This could help to improve scalability and performance on large systems.</p><div><hr></div><h3>Conclusion</h3><p>In this blog post, we went through the evolution of the Kernel&#8217;s scheduler in Linux. We began with the simplest form of a scheduler. Then, over time, it evolved in terms of time complexity, but brought new challenges. Furthermore, O(1) was replaced by CFS to perform more fairly and more efficiently. Finally, we glanced over further works on the Kernel&#8217;s scheduler. For further reading, you can look at the <a href="https://github.com/torvalds/linux/blob/master/Documentation/scheduler/sched-design-CFS.rst">Kernel&#8217;s scheduler documentation</a>.</p><p>Thanks a lot for reading so far. I would appreciate it if you hit the claps button to support my first Medium blog post! Further, you can find me on <a href="https://github.com/amk9978/">GitHub</a> and <a href="https://twitter.com/amir_k9978">X</a>.</p><div><hr></div><h3>References</h3><ul><li><p><a href="https://www.linuxjournal.com/article/6530">Introducing the 2.6 Kernel</a></p></li><li><p><a href="https://www.linuxjournal.com/node/10267">Completely Fair Scheduler</a></p></li></ul><div class="subscription-widget-wrap-editor" data-attrs="{&quot;url&quot;:&quot;https://amirkarimi.substack.com/subscribe?&quot;,&quot;text&quot;:&quot;Subscribe&quot;,&quot;language&quot;:&quot;en&quot;}" data-component-name="SubscribeWidgetToDOM"><div class="subscription-widget show-subscribe"><div class="preamble"><p class="cta-caption">Thanks for reading! Subscribe for free to receive new posts and support my work.</p></div><form class="subscription-widget-subscribe"><input type="email" class="email-input" name="email" placeholder="Type your email&#8230;" tabindex="-1"><input type="submit" class="button primary" value="Subscribe"><div class="fake-input-wrapper"><div class="fake-input"></div><div class="fake-button"></div></div></form></div></div>]]></content:encoded></item></channel></rss>