{"id":13818035,"url":"https://github.com/hamadmarri/Baby-CPU-Scheduler","last_synced_at":"2025-05-15T22:34:42.413Z","repository":{"id":78009163,"uuid":"412340563","full_name":"hamadmarri/Baby-CPU-Scheduler","owner":"hamadmarri","description":"This is a very basic/lightweight yet a very performant CPU scheduler. You can use it for learning purposes as a base ground CPU scheduler on Linux.","archived":false,"fork":false,"pushed_at":"2021-10-18T18:21:41.000Z","size":349,"stargazers_count":33,"open_issues_count":1,"forks_count":6,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-11-19T16:43:23.983Z","etag":null,"topics":["cacule-scheduler","cpu-scheduler","linux","linux-kernel","linux-scheduler"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hamadmarri.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-10-01T05:31:17.000Z","updated_at":"2024-10-31T09:26:21.000Z","dependencies_parsed_at":"2023-05-05T21:55:35.584Z","dependency_job_id":null,"html_url":"https://github.com/hamadmarri/Baby-CPU-Scheduler","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hamadmarri%2FBaby-CPU-Scheduler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hamadmarri%2FBaby-CPU-Scheduler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hamadmarri%2FBaby-CPU-Scheduler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hamadmarri%2FBaby-CPU-Scheduler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hamadmarri","download_url":"https://codeload.github.com/hamadmarri/Baby-CPU-Scheduler/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254433775,"owners_count":22070525,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cacule-scheduler","cpu-scheduler","linux","linux-kernel","linux-scheduler"],"created_at":"2024-08-04T07:00:28.953Z","updated_at":"2025-05-15T22:34:41.901Z","avatar_url":"https://github.com/hamadmarri.png","language":null,"funding_links":[],"categories":["CPU Schedulers for Linux"],"sub_categories":[],"readme":"# Baby-CPU-Scheduler\n\nThis is a very basic and lightweight yet very performant CPU scheduler.\nYou can use it for learning purposes as a base ground CPU scheduler on\nLinux. Notice that many features are disabled to make this scheduler as\nsimple as possible.\n\nBaby Scheduler is very lightweight and powerful for\nnormal usage. I am using (`baby-dl`) as my main scheduler and sometimes I\nswitch back to CacULE for testing. The throughput in Baby Scheduler is\nhigher due to the task loadbalancer that I made for Baby Scheduler. The\nloadbalancing is done via only one CPU which is CPU0 in which CPU0 scan\nall other CPUs and move one task in every tick. The balancing is only\ndepending on the number of tasks (i.e. no load, weight or other factors).\n\n\nBaby scheduler is only 1036 LOC where 254 LOC of it are just dependent\nfunctions that are copied from CFS without any changes to let the\nscheduler compile and run. You can find all Baby code in\n`bs.c`, `bs.h`, and `numa_fair.h`.\n\n## Flavors\nCurrently there are three flavors of Baby Scheduler\n* Deadline Scheduling (dl) - main\n* Virtual Run Time Scheduling (vrt)\n* Round Robin Scheduling (rr)\n\nAll the three flavors have the same tasks load balancing method.\nThey only differ in the strategy of picking the next task to run, and other minor differences.\n\n### Deadline Scheduling\nBaby's main scheduling is the deadline flavor. The scheduler picks the task with the earliest deadline.\nA new task gets a `deadline = now() + 1.180ms`. The task with earliest deadline will be picked and run\non the CPU until it gets to sleep or another task had earlier deadline. While the task is running, its\ndeadline gets updated only when its deadline is past - compared with the current time (now). So, basically\nwe don't want to update its deadline with `now() + 1.180ms` on every tick, otherwise, I call this situation\nby horse and carrot. I am using the deadline as a slice too, we don't want to keep preempting tasks in\nwhich their deadlines are very close to each other. The best solution is to give a minimum/maximum slice to\nthe running task to at least gets its next updated deadline to be not very close to the competing task. This\ncan save some context switching. Anyway, our deadline/slice is not too hight, it is only a 1.180ms. \n\n### Virtual Run Time Scheduling\nThe scheduler picks next task that has\nleast vruntime, however, all CFS load/weight for task priority are\nreplaced with a simpler mechanism. Tasks priorities are injected in\nvruntime where NICE0 priority task has a vruntime = real_exec_time,\nbut NICE-20 task has a vruntime \u003c real_exec_time in which NICE-20 task\nwill run 20 more milliseconds than NICE0 one in a race time of 40ms.\nSee the equation in kernel/sched/bs.c:convert_to_vruntime.\n\n### Round Robin Scheduling\n\n## Patch and Compile\n### Patch\nFirst, you need to patch the kernel source with one of the flavors of baby scheduler. See an example of patching linux kernel [here](https://github.com/hamadmarri/cacule-cpu-scheduler#how-to-apply-the-patch). You can use the same method to patch with baby instead of cacule.\n\n1. Download the linux kernel (https://www.kernel.org/) that is same version as the patch (i.e if patch file name is baby-5.14.patch, then download https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.14.9.tar.xz)\n2. Unzip linux kernel\n3. Download baby patch file and place it inside the just unzipped linux kernel folder\n4. cd linux-(version)\n5. patch -p1 \u003c baby-5.14.patch\n\n\n### Configure\nBaby uses periodic HZ only, and you need to disable other scheduler features such as `CGROUP_SCHED` and stat/debuging features.\n\nrun `make menuconfig`\nand choose `CONFIG_HZ_PERIODIC`\n\nYou should see this when you run `cat .config | grep -i hz`:\n\n```\nCONFIG_HZ_PERIODIC=y\n# CONFIG_NO_HZ_IDLE is not set\n# CONFIG_NO_HZ_FULL is not set\n# CONFIG_NO_HZ is not set\n```\n\nThen disable the following:\n* CONFIG_EXPERT\n* CONFIG_DEBUG_KERNEL\n* CONFIG_SCHED_DEBUG\n* CONFIG_SCHEDSTATS\n* NO_HZ\n* SCHED_AUTOGROUP\n* CGROUP_SCHED\n* UCLAMP_TASK\n* SCHED_CORE\n\n\nMake sure that `CONFIG_BS_SCHED` is selected (it appears at the top when running make menuconfig).\nConfirm by running `cat .config | grep -i bs_sched`\n```\nCONFIG_BS_SCHED=y\n```\n\nNow compile `make` \\\nThen install the modules `sudo make modules_install` \\\nThen install the kernel `sudo make install`\\\nReboot and choose the new kernel\n\nTo confirm that Baby is currently running:\n```\n$ dmesg | grep \"Baby CPU\"\nBaby CPU scheduler (dl) v5.14 by Hamad Al Marri.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhamadmarri%2FBaby-CPU-Scheduler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhamadmarri%2FBaby-CPU-Scheduler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhamadmarri%2FBaby-CPU-Scheduler/lists"}