OSTEP:多处理器调度
OSTEP:多处理器调度
本篇对应 OSTEP Version 1.10 的 “Multiprocessor Scheduling (Advanced)”。单 CPU 的 scheduler 迁移到多 CPU 后,必须同时处理 cache coherence、synchronization、cache affinity、scalability 和 load imbalance。
这是一个 advanced chapter,最好在 concurrency 之后学习:只有理解 threads 和 synchronization,才能充分理解多个 CPU 同时运行 shared code 时的问题。multicore processor 的普及,一方面来自 single CPU 很难在不显著增加 power consumption 的情况下继续变快,另一方面也使普通 desktop、laptop 和 mobile device 都拥有多个 CPU cores。但给 single-threaded application 增加 CPU 并不会自动让它变快;application 必须重写为 parallel program,通常借助 threads 把工作分散到多个 CPUs。
ASIDE:ADVANCED CHAPTERS
Advanced chapter 需要来自全书较大范围的 prerequisite material 才能真正理解,但在逻辑上又属于更早的章节。例如,多处理器调度在读过 concurrency 的中间部分后会更容易理解;不过从整体结构看,它仍然属于 virtualization(一般意义上)和 CPU scheduling。因此,这类 chapter 可以不按书中顺序学习;本章适合放在第二个 easy piece 之后。
THE CRUX:HOW TO SCHEDULE JOBS ON MULTIPLE CPUS
OS 应如何在多个 CPUs 上调度 jobs?会出现哪些新问题?原有的 techniques 是否仍然有效,还是需要新的 ideas?
类型与定位
- 笔记类型:深度分析
- 翻译基准:官方
cpu-sched-multi.pdf,Version 1.10。 - 图件说明:原文 Figure 10.1–10.3 的 cache/queue 图用 Mermaid 或代码保留语义;无独立 raster 图片。
TL;DR
- 多 CPU 共享 memory 时,每个 CPU 的 cache 会引入 cache coherence 问题。
- coherence 只保证 cache 对共享 memory 的视图基本一致,不会自动保证 linked list 等 shared data structure 的原子更新;仍需要 locks。
- SQMS(single queue)简单、load balance 好,但会遇到 lock contention、scalability 和 cache affinity 问题。
- MQMS(per-CPU queues)扩展性和 affinity 好,但会遇到 load imbalance,需要 migration/work stealing。
- 没有适用于所有 workload 的简单方案;多处理器 scheduler 是机制、硬件和 workload 权衡的结合。
目录
| 章节 | 内容 |
|---|---|
| 10.1 Multiprocessor Architecture | cache 与 coherence |
| 10.2 Synchronization | shared data correctness |
| 10.3 Cache Affinity | 尽量留在同一 CPU |
| 10.4 SQMS | 单队列调度 |
| 10.5 MQMS | 多队列调度与迁移 |
| 10.6 Linux | Linux 变体 |
| 10.7 Summary | 对比结论 |
10.1 Background: Multiprocessor Architecture
现代系统常有多个 physical CPUs,每个 CPU 有自己的 cache,但共享 main memory。单 CPU cache 利用 temporal locality 和 spatial locality 运行良好;多 CPU 下,cache 中可能同时保存同一地址的不同值。
Figure 10.1:Single CPU With Cache
CPU │ Cache │ Memory单 CPU 通过 cache 保存 main memory 中常用 data 的副本;小而快的 cache 让大而慢的 memory 看起来更快。
例如 CPU 1 从 address A 读到 D,修改自己的 cache 为 D′,尚未写回 main memory;OS 随后把 process 移到 CPU 2,CPU 2 从 memory 读到旧值 D。这个问题叫 cache coherence。
hardware 通常通过监视 memory accesses 保持共享 memory 的一致视图。bus-based system 可使用 bus snooping:每个 cache 监听连接 CPU 与 main memory 的 bus;发现某个 data item 被更新时,就 invalidate 自己的 copy 或更新为新值。write-back cache 会让问题更复杂,但基本思路相同。
Figure 10.2:Two CPUs With Caches Sharing Memory
Component Shared connection CPU 1 CPU 1 → Cache 1 → Bus CPU 2 CPU 2 → Cache 2 → Bus Main memory Bus → Shared Memory 两个 CPUs 通过 bus 共享 main memory,同时各自拥有 cache;cache coherence protocol 需要让它们对共享 memory 保持一致的视图。
cache 之所以有效,是因为 program 通常具有 locality:temporal locality 表示刚访问过的数据很可能很快再次访问,例如 loop 中反复使用的 variable;spatial locality 表示访问 address x 后,很可能访问靠近 x 的其他 addresses,例如顺序遍历 array。cache 用少量而快速的 memory 保存 main memory 中热门 data,使 large、slow memory 看起来更快。
在 single-CPU system 中,program 第一次通过 load instruction 读取某个 memory value 时,数据通常要从 main memory 取出,可能花费 tens 甚至 hundreds of nanoseconds;CPU 会把它的 copy 放进 cache,之后再次读取同一 data item 时,只需几 nanoseconds。多 CPU system 的难点在于:共享同一个 main memory 的多个 CPUs,可能各自 cache 了同一地址的不同 values。
10.2 Don’t Forget Synchronization
cache coherence 不等于 shared data structure 的正确性。多个 CPUs 并发更新 shared queue 或 linked list 时,仍需 mutual-exclusion primitives,例如 locks。
1 typedef struct __Node_t {
2 int value;
3 struct __Node_t *next;
4 } Node_t;
5
6 int List_Pop() {
7 Node_t *tmp = head; // remember old head
8 int value = head->value; // ... and its value
9 head = head->next; // advance to next
10 free(tmp); // free old head
11 return value; // return value @head
12 }
如果两个 threads 同时执行,可能都把相同的 head 存入自己的 tmp,随后都 free 同一个 node,产生 double free,并可能返回两次相同的 value。给 routine 加 mutex lock/unlock 可以保证正确性,但 CPU 数量增加后,对 shared data structure 的同步访问会变慢。
Figure 10.3:Simple List Delete Code
上面的
List_Pop()是原文示例:它依次读取head、读取 value、推进head、释放旧 node 并返回 value。两个 threads 同时执行时,可能拿到同一个 old head,最终 double free 或返回重复 value;这段代码必须通过 locking 或其他并发正确性机制保护。
更具体地说,Thread 1 执行第一行后,将当前 head 放入自己的 tmp;Thread 2 随后也执行第一行,于是得到相同的 head。tmp 位于各自 thread 的 stack 上,因此每个 thread 有自己的 private copy;但两个 threads 仍然指向同一个 list node。之后两者都可能读取相同的 value、推进 head,并对同一个 node 调用 free()。结果可能是 double free,甚至导致 heap corruption。coherence protocol 只能保证 memory location 的视图一致,不能把整段 list update 自动变成 atomic operation。
这里 coherence protocol 只保证两个 CPU 对某个 memory location 的视图不会长期互相矛盾,并不保证“读取 head、读取 value、更新 head、释放 old node”这几个步骤作为一个 atomic operation 完成。要让 shared queue 或 linked list 正确更新,必须使用 locks;lock-free data structures 也可以解决问题,但实现复杂,通常只在确有需要时采用。
10.3 One Final Issue: Cache Affinity
process 在某个 CPU 上运行后,会在该 CPU 的 caches 和 TLBs 中积累 state。下次继续在同一 CPU 上运行,通常可以复用这些 cached contents;频繁迁移到其他 CPU 虽然仍能正确运行,但需要重新加载 state,性能会下降。因此 scheduler 应考虑 cache affinity,尽可能让 process 留在原 CPU。
10.4 Single-Queue Scheduling
SQMS 把所有 jobs 放在一个 global queue,多个 CPUs 从中选择 job。优点是简单:已有单 CPU policy 后,只需让 scheduler 选择前几个 jobs。
缺点有两个:
- Scalability:global queue 需要 lock;CPU 越多,lock contention 越严重,时间更多地消耗在 synchronization overhead。
- Cache affinity:不同 CPU 从同一 queue 轮流取 job,process 会在 CPU 之间 bouncing,无法复用 cache。
可以加入 affinity mechanism,只迁移部分 jobs,在 affinity 和 load balance 间折中,但实现会复杂。
例如有五个 jobs A B C D E 和四个 CPUs。如果每个 CPU 都从同一个 global queue 轮流取下一个 job,jobs 会不断在 CPU 之间 bouncing,正好破坏 cache affinity。加入 affinity 后,可以让 A–D 尽量留在原 CPU,只让 E 在 CPU 之间迁移,从而在保留大部分 cache state 的同时实现一定的 load balance;但这种选择本身会让 scheduler 更复杂。
原文的一个 possible schedule 可以表示为:
CPU 3: D C B A E D C B A E ...
CPU 2: C B A E D C B A E D ...
CPU 1: B A E D C B A E D C ...
CPU 0: A E D C B A E D C B ...
在这个 arrangement 中,每个 job 都从 global queue 取出,因此会在 CPUs 之间来回 bouncing。若加入 affinity mechanism,可以让 A–D 固定在原 CPU,只让 E 迁移:
CPU 3: D D D D E ...
CPU 2: C C C E C ...
CPU 1: B B E B B ...
CPU 0: A E A A A ...
10.5 Multi-Queue Scheduling
MQMS 通常为每个 CPU 建立一个 scheduling queue。job 进入 system 时,根据 heuristic(随机,或选择 jobs 较少的 queue)放入一个 queue,之后主要由对应 CPU 独立调度。
MQMS 的优势是 scalability 更好、lock/cache contention 更低,并且天然保持 cache affinity。问题是 load imbalance:一个 queue 中的 jobs 结束后,对应 CPU 可能 idle,而另一个 CPU 仍有许多 jobs。
例如两个 CPUs 分别拥有 Q0: A 和 Q1: B D;若 A 很快结束,CPU 0 会 idle,而 CPU 1 仍在 B、D 之间轮换。更糟糕的是,如果所有工作集中在一个 queue,另一个 CPU 可能长期没有任何工作。MQMS 因此把共享 global queue 的 contention 换成了负载重新平衡问题。
例如,jobs 初始可能这样分配:
Q0: A C Q1: B D
若每个 queue 使用 round-robin,两个 CPUs 可以分别运行 A A C C ... 与 B B D D ...。但当 C 完成后,队列变成 Q0: A、Q1: B D,CPU 0 会持续运行 A,而 CPU 1 只能在 B、D 之间切换;当 A 和 C 都完成时,Q0 为空,CPU 0 完全 idle,造成严重的 load imbalance。
THE CRUX:HOW TO DEAL WITH LOAD IMBALANCE
multi-queue scheduler 应如何处理负载不均,使调度目标更好地实现?
解决办法是 migration:把 job 从忙碌 CPU 移到 idle CPU。如果一个 CPU 没有 jobs,迁移一个即可;若一个 CPU 只有 A,另一个 CPU 在 B、D 间轮换,可能需要持续迁移,才能让两边都保持忙碌。
一种常见方法是 work stealing:jobs 较少的 source queue 偶尔查看较满的 target queue,并“偷走”一个或多个 jobs。查看太频繁会产生 overhead,查看太少又会造成 load imbalance;阈值需要结合 workload 调优。
脚注:Decepticons
原文在讨论 load imbalance 时开玩笑说,要如何击败 Decepticons;随后又自嘲这是本书第一次、也是最后一次 Transformers 引用,并虚构 Cybertron 是因糟糕的 CPU scheduling decisions 而毁灭。这里保留其作为旁注的幽默,不把它当作系统事实。
在更棘手的 Q0: A、Q1: B D 场景中,一次 migration 可能仍然不够。一个 possible timeline 是:
CPU 0: A A A A B A B A B B B B ...
CPU 1: B D B D D D D D A D A D ...
这里 B 在某个时刻迁移到 CPU 0,使 D 暂时独占 CPU 1;随后还可能需要继续迁移,才能同时保持两个 CPUs 忙碌。work stealing 的 source queue 会偶尔查看 target queue;只有当 target 明显更满时,才从中“偷走”一个或多个 jobs。查看过于频繁会破坏 MQMS 原本的 scalability;查看过少又会容忍严重 imbalance,因此合适的 threshold 往往需要通过 workload 实验确定。
迁移并不总是一次就能解决问题:当 Q0 只有 A,而 Q1 由 B、D 轮换时,可能需要持续把 B 或 D 在两个 queues 之间移动,才能让两个 CPUs 都保持忙碌。work stealing 正是把这种 decision 分散给较空的 queue:它偶尔查看其他 queue,只有在 target 明显更满时才偷走一个或多个 jobs。查看频率越高,balance 越好但 synchronization overhead 越大;查看频率越低,扩展性更好却可能容忍严重 imbalance。
10.6 Linux Multiprocessor Schedulers
Linux 历史上出现过多种 scheduler:O(1)、Completely Fair Scheduler(CFS)和 BFS。
- O(1) 与 CFS 使用 multiple queues;BFS 使用 single queue。
- O(1) 是 priority-based scheduler,会随时间改变 process priority,尤其关注 interactivity。
- CFS 是 deterministic proportional-share 方法,类似 stride scheduling。
- BFS 也是 proportional-share,但使用更复杂的 Earliest Eligible Virtual Deadline First(EEVDF)。
不同设计都可能成功,关键在于 workload、lock、affinity 和调度开销的综合权衡。这里的 O(1)/CFS/BFS 是官方 Version 1.10 正文讨论的历史背景;笔记另外保留 Linux 6.6 起 EEVDF 成为默认 scheduler 的更新信息,并不把它误当作原版 1.10 的正文。
10.7 Summary
| 方案 | 优势 | 问题 |
|---|---|---|
| SQMS | 实现直接;global queue 便于 balance load | lock contention;扩展性差;affinity 差 |
| MQMS | scalability 好;天然 cache affinity | load imbalance;需要 migration;更复杂 |
| work stealing | 动态修复 queue 不均衡 | 检查频率与迁移阈值难调 |
多处理器 scheduler 没有简单答案。少量代码变化就可能带来显著不同的行为;只有清楚理解 hardware、synchronization 和 workload 后,才应修改这类 scheduler。
Homework(Simulation)
使用配套的 multi.py simulator 观察 multi-processor CPU scheduler;具体选项见相关 README。
- 运行一个 runtime 为 30、working-set size 为 200 的 job
a,在一个 simulated CPU 上执行:./multi.py -n 1 -L a:30:200。它需要多长时间完成?使用-c查看最终答案,使用-t查看逐 tick trace。 - 增大 cache size,使 working set(200)能够放入 cache(默认大小为 100):
./multi.py -n 1 -L a:30:200 -M 300。job 进入 cache 后会多快?提示:考虑由-r控制的 warm rate;使用-c检查。 - 在上一个 simulation 中打开 time-left tracing(
-T)。该 flag 会显示每个 time step 中 CPU 上调度的 job,以及该 job 在每个 tick 后剩余的 runtime。第二列是如何递减的? - 再加上
-C,观察每个 CPU cache 对每个 job 的状态:空格表示该 job 的 cache 是 cold,w表示 warm。joba何时变为 warm?把 warmup time parameter(-w)调低或调高会怎样? - 在 two-CPU system 上运行三个 jobs:
./multi.py -n 2 -L a:100:100,b:100:50,c:100:50。假设使用 centralized round-robin scheduler,预测完成时间;用-c检查,再用-t和-C查看 step-by-step trace 及 cache 是否有效 warm up。 - 使用
-A显式控制 cache affinity:把b、c放在 CPU 1,把a限制在 CPU 0:./multi.py -n 2 -L a:100:100,b:100:50,c:100:50 -A a:0,b:1,c:1。预测完成速度并解释为什么更快;尝试其他 jobs 与 CPUs 的组合,比较性能。 - 研究 super-linear speedup:使用
-L a:100:100,b:100:100,c:100:100 -M 50,分别在 1、2、3 个 CPUs(-n 1、-n 2、-n 3)上运行;然后将每-CPU cache size 调大到 100。观察 CPU 数量增加时的性能变化,并用-c与 tracing flags 验证。 - 研究 per-CPU scheduling option(
-p):仍使用 two-CPU 与-L a:100:100,b:100:50,c:100:50的配置。它和前面手动设置的 affinity limits 相比如何?改变 peek interval(-P)的高低会怎样?CPU 数量扩展时,这种 per-CPU approach 如何变化? - 生成随机 workloads,尝试预测不同 processor 数量、cache sizes 和 scheduling options 下的 performance;再通过 simulator 验证预测。
与已有笔记的关系
- OSTEP 系列索引:本系列总目录。
- OSTEP:Limited Direct Execution:context switch 和 timer interrupt 是迁移 process 的基础。
- OSTEP:多级反馈队列:单 CPU priority policy 的基础。
- OSTEP:比例份额调度:CFS 与 proportional-share 的关联。
- OSTEP:并发导论:locks、shared data structures 和 synchronization 的补充。
参考资料
References
| 标识 | 资料 |
|---|---|
[A90] | Thomas E. Anderson, “The Performance of Spin Lock Alternatives for Shared-Memory Multiprocessors”, IEEE TPDS, 1990。用于说明共享 lock contention 的扩展性代价。 |
[B+10] | Silas Boyd-Wickizer et al., “An Analysis of Linux Scalability to Many Cores”, OSDI 2010。 |
[CSG99] | David E. Culler, Jaswinder Pal Singh, Anoop Gupta, Parallel Computer Architecture: A Hardware/Software Approach, Morgan Kaufmann, 1999。 |
[FLR98] | Matteo Frigo, Charles E. Leiserson, Keith Randall, “The Implementation of the Cilk-5 Multithreaded Language”, PLDI 1998;work stealing 的经典实例。 |
[G83] | James R. Goodman, “Using Cache Memory to Reduce Processor-Memory Traffic”, ISCA 1983;bus snooping 的早期工作。 |
[M11] | Joseph T. Meehean, Towards Transparent CPU Scheduling, University of Wisconsin–Madison doctoral dissertation, 2011。 |
[SHW11] | Daniel J. Sorin, Mark D. Hill, David A. Wood, A Primer on Memory Consistency and Cache Coherence, Synthesis Lectures on Computer Architecture, Morgan & Claypool, 2011。 |
[SA96] | Ion Stoica, Hussein Abdel-Wahab, “Earliest Eligible Virtual Deadline First: A Flexible and Accurate Mechanism for Proportional Share Resource Allocation”, Technical Report TR-95-22, 1996。 |
[TTG95] | Josep Torrellas, Andrew Tucker, Anoop Gupta, “Evaluating the Performance of Cache-Affinity Scheduling in Shared-Memory Multiprocessors”, Journal of Parallel and Distributed Computing, 1995。 |
原文 References 还带有作者评论和推荐语;这里保留资料的 bibliographic identity 及其在本章中的用途。
原文逐段复核补充
- 官方 cpu-sched-multi.pdf 为 Version 1.10,共 13 页;正文编号为 10.1–10.7,图件为 Figure 10.1–10.3,均为 cache/queue/code 的示意,没有需要另存的独立 raster 图片。本笔记以表格、代码块和语义示意保留其位置。
- 已核对 Figure 10.1/10.2 的 single-CPU 与 two-CPU cache 关系、D→D′ 的 cache-coherence 示例、bus snooping、Figure 10.3 的完整 12 行
List_Pop()code,以及无 lock 时 double free/重复返回 value 的具体 interleaving。 - 已核对 SQMS 的五 jobs/四 CPUs 两种 schedule、global queue 的 lock contention 与 cache bouncing;MQMS 的 Q0/Q1 分配、C 完成后的负载不均、CPU idle 场景、continuous migration 和 work stealing 的 source/target/threshold 权衡。
- 原文脚注 ¹ 是 Decepticons/Cybertron 的 Transformers 玩笑,脚注 ² 是让读者自行查 BF 的含义;笔记已将其作为幽默旁注处理。原版 simulator homework 共 9 题,笔记已逐项保留。
评论 (0)