Scheduling on many cores
About this lesson
With one core the scheduler only asks who runs next and for how long. With several cores it must also choose where. Watch jobs on a two-core machine: first sharing one queue, where every pick waits for a lock and every job keeps landing on a core whose cache has forgotten it, then with a queue per core, where caches stay warm until the queues fall out of balance and an idle core steals work. The Hardware lesson on multicore processors explains why each core's cache matters; here we see what the scheduler does about it, and how Linux does it.
Two cores, two caches
A multicore chip has several cores. Each core is a CPU that runs one job at a time. Here there are two. Each has its own small, fast cache, and both share one big, slow main memory.
core runs one job at a time cache small and fast, one per core memory big and slow, shared by every core a value already in the cache: a few nanoseconds a value fetched from memory: tens to hundreds of ns (OSTEP ch. 10) new question for the scheduler: not only who runs next and for how long, but also on WHICH core
Read the lesson step by step
Two cores, two caches
A multicore chip has several cores. Each core is a CPU that runs one job at a time. Here there are two. Each has its own small, fast cache, and both share one big, slow main memory.
core runs one job at a time cache small and fast, one per core memory big and slow, shared by every core a value already in the cache: a few nanoseconds a value fetched from memory: tens to hundreds of ns (OSTEP ch. 10) new question for the scheduler: not only who runs next and for how long, but also on WHICH coreOne shared queue
The simplest plan: one queue for all cores, single-queue scheduling (SQMS). Jobs A, B and C wait in it. Each free core takes the front job, A to core 0, B to core 1, and loads its data. The counter tallies slices and reloads.
SQMS: single-queue multiprocessor scheduling queue A B C core 0 takes A cache 0: (empty) -> load A core 1 takes B cache 1: (empty) -> load B queue C any one-core policy works unchanged: with 2 cores it just picks the best 2 jobsTaking turns at the lock
After one time slice, A and B go to the back of the queue. Both cores want the queue at once, so it has a lock. Core 0 takes the lock first and gets C; core 1 waits, then gets A.
end of slice 1 queue C A B core 0 lock, take C, unlock core 1 waits for the lock ... take A without the lock both cores could take C at once more cores = more waiting at the one lock: "the system spends more and more time in lock overhead" (OSTEP). SQMS does not scale well.Jobs bounce between cores
A ran on core 0 but now runs on core 1, whose cache lacks A's data, so it reloads. Every job that runs again lands on the other core and reloads. Running again where its data is warm, called cache affinity, is lost.
slice core 0 core 1 1 A B all cold (first run) 2 C A C new, A moved: reload 3 B C B moved, C moved: reload after 3 slices: 6 reloads in 6 runs cache affinity: run a job where its data still isA queue per core
Start again with one queue per core: multi-queue scheduling (MQMS). Four jobs arrive, and each goes into one queue: A and C to core 0, B and D to core 1. Each core picks from its own queue, so no shared lock.
MQMS: multi-queue multiprocessor scheduling a new job goes into exactly ONE queue (random, or the one with fewer jobs) queue 0 A C work left: A 4, C 2 slices queue 1 B D work left: B 6, D 6 slices each core runs round robin on its own queueCaches stay warm
Each core runs round robin on its own queue: A, C, A, C on core 0 and B, D, B, D on core 1. Each job loads its data once, then finds it still in its core's cache while it stays there.
slice core 0 core 1 1 A load B load 2 C load D load 3 A warm B warm 4 C warm D warm C is done 4 reloads in 8 runs; no more while no job changes core more cores = more queues: no single lock to wait forLoad imbalance
C finished after slice 4. Now core 0 has only A, while B and D still share core 1. A gets a whole core, twice as much CPU as B or D. Each queue only sees its own jobs.
slice core 0 core 1 5 A B 6 A D A is done share of a core, slices 5-6: A 1 B 0.5 D 0.5 A gets twice as much CPU as B or DAn idle core
A finished after slice 6. Core 0's queue is empty, so core 0 sits idle while B and D still take turns on core 1. Half the machine does nothing while work waits.
slice 7 begins queue 0 (empty) core 0 idle queue 1 B D core 1 runs B, D waits left alone, the last job ends after slice 12Work stealing
Core 0 peeks at queue 1, sees two jobs against its none, and steals D: a migration. D reloads its data once on core 0, then runs warm. All the work is done after slice 9, not 12.
work stealing: an empty (or short) queue peeks at a fuller one and takes a job from it slice core 0 core 1 7 D load B 8 D warm B 9 D warm B both done all done after 9 slices (12 without stealing) peek too often: overhead; too rarely: imbalanceHow Linux does it
Linux keeps a run queue for every CPU. On a regular tick each CPU checks the balance and pulls tasks from the busiest one. A program can also pin a thread to chosen cores with sched_setaffinity().
per-CPU run queues: O(1), CFS (2.6.23), EEVDF (6.6) balancing: sched_tick() -> sched_balance_rq(): find the busiest group, its busiest run queue, lock both, move tasks to this CPU levels: hyperthreads, then cores, then NUMA nodes pin this thread to core 0 (Linux, C99): #define _GNU_SOURCE #include <sched.h> #include <stdio.h> int main(void) { cpu_set_t set; CPU_ZERO(&set); CPU_SET(0, &set); /* core 0 only */ if (sched_setaffinity(0, sizeof set, &set) == -1) { perror("sched_setaffinity"); return 1; } printf("running on core %d\n", sched_getcpu()); return 0; } prints: running on core 0Balance against affinity
One shared queue balances load but needs a lock and loses cache affinity. A queue per core scales and keeps caches warm, but drifts out of balance, so it moves jobs now and then, not too often.
both plans as a Python simulation: CAP = 2 # jobs each cache can hold def run_on(cache, job): # True if the data must reload miss = job not in cache if not miss: cache.remove(job) cache.append(job) if len(cache) > CAP: cache.pop(0) # forget the oldest return miss def sqms(jobs, slices): # one shared queue queue, caches, runs, reloads = list(jobs), [[], []], 0, 0 for t in range(slices): picked = [queue.pop(0) for core in (0, 1)] # under the lock for core, job in enumerate(picked): runs += 1; reloads += run_on(caches[core], job) queue += picked # back to the end of the queue return runs, reloads def mqms(queues, left, steal): # one queue per core caches, runs, reloads, t = [[], []], 0, 0, 0 while any(queues): t += 1 for core in (0, 1): other = queues[1 - core] if steal and not queues[core] and len(other) >= 2: queues[core].append(other.pop()) # steal for core in (0, 1): if not queues[core]: continue job = queues[core].pop(0) runs += 1; reloads += run_on(caches[core], job) left[job] -= 1 if left[job]: queues[core].append(job) return runs, reloads, t work = lambda: dict(A=4, B=6, C=2, D=6) print("one queue (runs, reloads):", sqms("ABC", 6)) print("per core (runs, reloads, slices):", mqms([["A", "C"], ["B", "D"]], work(), True)) print("no stealing:", mqms([["A", "C"], ["B", "D"]], work(), False)) prints: one queue (runs, reloads): (12, 12) per core (runs, reloads, slices): (18, 5, 9) no stealing: (18, 4, 12)
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01What is cache affinity?
A job leaves state in the cache of the core it ran on. Run it there again and some of that state is still warm; run it elsewhere and it must reload from main memory, which is much slower.
02Two cores share one run queue (SQMS). Why does that queue need a lock?
Both cores update the same list. The lock makes each pick happen one core at a time. With many cores, waiting for that one lock becomes a real cost, so SQMS does not scale well.
03With a queue per core, Q0 holds A and C and Q1 holds B and D, each run round robin. C finishes. What happens?
Each queue only sees its own jobs. Core 0 now runs only A, while B and D share core 1, so A gets twice as much CPU. Only a migration between queues can fix that.
04In work stealing, which queue starts the move?
An empty or short queue occasionally looks at another queue and, if it is notably fuller, steals work. Peeking too often costs overhead; too rarely leaves the queues out of balance.
05On Linux, what does sched_setaffinity(0, sizeof set, &set) do when set holds only CPU 0?
pid 0 means the calling thread. The mask lists the CPUs it may run on; if it is on a CPU outside the mask, the kernel migrates it. Other threads may still share CPU 0.
Glossary
- Core
- One processor on a multicore chip. Each core runs one job (thread) at a time.
- Cache
- A small, fast memory next to each core that keeps copies of recently used data, so the core does not have to wait for main memory.
- Cache affinity
- The benefit of running a job again on the core it ran on before, where its data may still be in the cache.
- Run queue
- The list of jobs that are ready to run and waiting for a core.
- SQMS
- Single-queue multiprocessor scheduling: one run queue shared by every core, protected by a lock.
- MQMS
- Multi-queue multiprocessor scheduling: one run queue per core. Each job sits in exactly one queue.
- Load imbalance
- When some cores have more work waiting than others, so jobs get unequal shares or a core sits idle.
- Work stealing
- A balancing method: a core whose queue is empty or short takes jobs from a fuller queue. Moving a job like this is a migration.
Notes and sources
- The figure plays a slice-by-slice simulation. Taking the lock, switching jobs and reloading data are drawn as free; on a real machine each costs time, which is why affinity and fewer locks matter.
- Each cache is drawn holding the data of the last two jobs that ran on its core. A real cache holds small lines of data from many jobs, so a moved job loses much, not all, of its warm data. Hardware cache coherence keeps every copy correct either way: moving costs speed, never correctness.
- OSTEP shows the bouncing with five jobs on four processors. Three jobs on two cores bounce the same way; with four jobs on two cores the pairs would happen to repeat and keep their cores by luck.
- Linux keeps one run queue per CPU in all its recent schedulers: the O(1) scheduler, CFS (default since 2.6.23) and EEVDF (since 6.6). Balancing works level by level over scheduling domains: hyperthreads of one core, the cores of a node, then the whole machine.
- sched_setaffinity() and sched_getcpu() are Linux-only and need _GNU_SOURCE. If the thread is on a core outside the new set, the kernel migrates it, and a child made with fork() inherits the set.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 10: Multiprocessor Scheduling (Advanced)
- Berkeley CS162, Lecture 11: Scheduling 2 (multi-core and affinity scheduling)
- MIT 6.1810 (2024), lecture notes: Threads (a scheduler per CPU)
- Linux kernel documentation: Scheduler Domains
- Linux kernel documentation: EEVDF Scheduler
- Linux man-pages: sched(7)
- Linux man-pages: sched_setaffinity(2)
- Linux man-pages: CPU_SET(3)
- Linux man-pages: sched_getcpu(3)
Quiz Unlocks after the last step.