硬體組織與程式執行 (Hardware Organization & Program Execution)
Overview Table
| 小節 | 主題 | 核心結論 |
|---|---|---|
| 1.4 (p.43) | Shell 與程式執行 | shell 是命令列直譯器;非內建命令即視為可執行檔,載入並執行 |
| 1.4.1 (p.44-46) | 硬體組織 | 系統由 buses、I/O devices、main memory、processor (CPU) 構成 |
| 1.4.2 (p.46-47) | 執行 hello |
鍵盤輸入 → 載入(DMA 直達記憶體)→ CPU 執行 → 輸出到顯示器 |
| 1.5 (p.47-49) | Caches Matter | 系統大量時間花在搬移資料;以 SRAM cache 彌補 processor–memory gap |
| 1.6 (p.50) | 儲存階層 | 儲存裝置形成 memory hierarchy;上層是下層的 cache |
1.4 Processors Read and Interpret Instructions Stored in Memory (p.43)
編譯系統產出的可執行檔 hello 存於磁碟。在 Unix 系統上輸入 ./hello 交給 shell 執行:
- shell 是命令列直譯器 (command-line interpreter):印出提示字元 → 等待輸入命令列 → 執行命令。
- 若命令列第一個字不是內建 shell 命令,shell 便假設它是可執行檔名稱,將其載入並執行 (load and run),並等待它終止。
hello印出訊息後終止;shell 再印提示字元等待下一個命令。
linux> ./hello ← shell 讀入命令
hello, world ← hello 程式輸出
linux> ← hello 終止,shell 重新印提示字元
shell 如何真正「載入並執行」程式(fork + execve、loader、run-time 記憶體映像)在 08-Exceptional-Control-Flow/04-Process-Control 與 07-Linking/03-Executable-Loading-and-Dynamic-Linking 詳述;本節只給 big picture。
1.4.1 Hardware Organization of a System (p.44-46)
典型系統(以近代 Intel 系統為原型,但各家系統大同小異)的硬體組織:
CPU
+-------------------+
| Register file |
| +----+ |
| | PC | ALU |
| +----+ |
| | System bus Memory bus
| Bus interface <=|=================+==================+
+-------------------+ | |
+---------+ +---------+
| I/O | | Main |
| bridge | | memory |
+---------+ | (DRAM) |
| +---------+
======================================+===================== I/O bus
| | | |
+--------+ +----------+ +------------+ Expansion slots
| USB | | Graphics | | Disk | (e.g. network
| ctrlr | | adapter | | controller | adapters)
+--------+ +----------+ +------------+
Mouse Display Disk ← hello executable
Keyboard stored on disk
Buses(匯流排)
- 貫穿系統的電氣導管 (electrical conduits),在元件間來回搬運位元組。
- 通常以固定大小的位元組區塊 —— word(字組) —— 為傳輸單位。
- word size 是基本系統參數,依系統而異;現今多為 4 bytes (32-bit) 或 8 bytes (64-bit)。
本書不假設固定的 word size 定義;凡需要精確意義的語境都會另行指明「word」所指為何。(x86-64 組語中的 "word" 慣例上反而是 16-bit,見 03-Machine-Level-Programs/01-Program-Encodings-and-Data-Formats。)
I/O Devices(輸入/輸出裝置)
- 系統與外部世界的連接。範例系統有四個:keyboard、mouse(輸入)、display(輸出)、disk(長期儲存;
hello最初就放在這)。 - 每個 I/O 裝置經由 controller 或 adapter 連上 I/O bus,兩者差異主要在封裝 (packaging):
| Controller | Adapter | |
|---|---|---|
| 形式 | 裝置本身或主機板 (motherboard) 上的晶片組 (chip sets) | 插在主機板插槽上的介面卡 |
| 目的 | 相同:在 I/O bus 與 I/O 裝置間來回傳遞資訊 | 同左 |
Main Memory(主記憶體)
- 暫時性 (temporary) 儲存裝置:在處理器執行程式期間,同時存放程式與其操作的資料。
- 物理上:一組 DRAM (dynamic random access memory) 晶片。
- 邏輯上:線性位元組陣列 (linear array of bytes),每個 byte 有唯一位址(陣列索引),從 0 起算。
- 機器指令可由可變數目的位元組構成;C 變數大小依型別而定 —— x86-64 Linux 上:
short2 bytes、int/float4 bytes、long/double8 bytes。
Processor (CPU)
- 中央處理單元:解讀(執行)存放在主記憶體中指令的引擎。
- 核心是 word-size 暫存器 PC (program counter):任一時刻都指向(存放)主記憶體中某條機器語言指令的位址。
- 從開機到斷電,處理器不斷重複:執行 PC 所指的指令 → 更新 PC 指向下一條指令(下一條不一定與剛執行的指令在記憶體中相鄰,如 jump 之後)。
- 指令依 instruction set architecture (ISA) 定義的模型「看似」嚴格循序 (in strict sequence) 執行;單條指令的執行包含一連串步驟:讀取指令 → 解讀位元 → 執行簡單操作 → 更新 PC。
簡單操作環繞三個部件:main memory、register file(一組各有唯一名稱的 word-size 暫存器)、ALU (arithmetic/logic unit)(計算新的資料值與位址值):
| 操作 | 效果 |
|---|---|
| Load | 從主記憶體複製 byte 或 word 到暫存器(覆寫原內容) |
| Store | 從暫存器複製 byte 或 word 到主記憶體某位置(覆寫原內容) |
| Operate | 兩個暫存器內容送 ALU 做算術運算,結果存回暫存器(覆寫原內容) |
| Jump | 從指令本身取出一個 word,複製進 PC(覆寫 PC) |
- ISA (instruction set architecture):描述每條機器指令的效果(抽象模型,Ch.3 的層次)。
- Microarchitecture:描述處理器實際如何實作。現代處理器用遠更複雜的機制(pipelining、out-of-order 等)加速執行,只是「看起來像」簡單循序模型 → 見 04-Processor-Architecture/04-Pipelining-Principles 與 05-Program-Optimization/02-Modern-Processor-Operation。
1.4.2 Running the hello Program (p.46-47)
執行 ./hello 的完整資料流(big picture,省略許多細節):
[Step 1] 讀取命令 (Figure 1.5)
Keyboard --(USB ctrlr → I/O bus → I/O bridge)--> CPU register
CPU register --> Main memory ("./hello" 逐字元存入記憶體)
[Step 2] 按下 Enter → shell 載入可執行檔 (Figure 1.6)
Disk ====(DMA: 不經過 CPU)====> Main memory
(hello 的 code + data,含字串 "hello, world\n")
[Step 3] CPU 執行 hello 的 main routine (Figure 1.7)
Main memory --> Register file --> Display (graphics adapter)
("hello, world\n" 逐位元組送往螢幕顯示)
- Step 1:使用者鍵入
./hello時,shell 將每個字元讀入暫存器、再存入記憶體。 - Step 2:shell 執行一連串指令,把
helloobject file 的 code 與 data 從磁碟複製到主記憶體;透過 DMA (direct memory access) 技術,資料直接從磁碟到主記憶體、不經過處理器(Ch.6 詳述)。 - Step 3:載入完成後處理器開始執行
hello的 main routine 中的機器指令,把hello, world\n的位元組從記憶體複製到 register file,再送往顯示裝置顯示。
1.5 Caches Matter (p.47-49)
關鍵教訓:系統花大量時間在把資訊從一處搬到另一處(disk → memory → CPU → display)。對程式設計師而言這些複製都是拖慢「真正工作」的 overhead,故系統設計者的主要目標是讓複製操作盡可能快。
物理定律造成的基本取捨:
- 越大的儲存裝置越慢;越快的裝置造價越貴。
- 典型數字(p.49):disk 可能比 main memory 大 1,000 倍,但處理器讀 disk 上一個 word 可能比讀 memory 慢 10,000,000 倍。
- register file 只存幾百 bytes(vs. memory 數十億 bytes),但處理器讀 register file 比讀 memory 快近 100 倍。
隨半導體技術演進,這個差距持續擴大:讓處理器變快比讓主記憶體變快更容易也更便宜。
解法:插入更小、更快的 cache memories(快取) 作為「處理器近期可能需要的資訊」的暫存集結區 (temporary staging areas):
CPU chip
+-----------------------------+
| Register file |
| +--------+ |
| | Cache | ALU |
| | mems | |
| +--------+ | System bus Memory bus
| Bus interface <=====|=========+============+
+-----------------------------+ +--------+ +--------+
| I/O | | Main |
| bridge | | memory |
+--------+ +--------+
| 層級 | 容量 | 速度 | 技術 |
|---|---|---|---|
| L1 cache | 數萬 bytes | 幾乎與 register file 一樣快 | SRAM |
| L2 cache | 數十萬~數百萬 bytes | 存取比 L1 慢約 5 倍;仍比 main memory 快 5~10 倍 | SRAM(經特殊 bus 連接處理器) |
| L3 cache | 更大 | 更新、更強的系統才有三層 | SRAM |
| Main memory | 數十億 bytes | 慢 | DRAM |
- 快取奏效的原理是 locality(區域性):程式傾向於在局部區域存取資料與程式碼。讓快取保存可能常被存取的資料,多數記憶體操作就能由快速的 cache 完成 → 系統同時獲得「大記憶體」與「快記憶體」的效果。
本書最重要的教訓之一:了解 cache 的應用程式設計師,可將程式效能提升一個數量級 (order of magnitude)。詳見 06-Memory-Hierarchy/03-Cache-Memories 與 06-Memory-Hierarchy/04-Cache-Friendly-Code-and-Memory-Mountain。
1.6 Storage Devices Form a Hierarchy (p.50)
「在處理器與較大較慢的裝置之間插入較小較快的儲存裝置」是普遍性的想法:每個電腦系統的儲存裝置都組織成 memory hierarchy(記憶體階層):
/\ L0: Regs (CPU registers)
/ \ L1: L1 cache (SRAM)
Smaller, / \ L2: L2 cache (SRAM)
faster, / \ L3: L3 cache (SRAM)
costlier \ L4: Main memory (DRAM)
per byte ↑ \ L5: Local secondary storage (local disks)
| \ L6: Remote secondary storage
Larger, ↓ \ (distributed file systems, Web servers)
slower, cheaper \
per byte ----------->
- 由上到下:更慢、更大、每 byte 更便宜。
- register file 在最頂層 = L0;L1~L3 cache 佔階層第 1~3 層;main memory 是 L4;以下依序類推。
- 核心思想:某一層的儲存,作為下一個較低層儲存的 cache:
| 這一層 | 是誰的 cache |
|---|---|
| Register file (L0) | L1 cache 的 cache(保存從 L1 取回的 words) |
| L1 cache | L2 cache 的 cache(保存 cache lines) |
| L2 cache | L3 cache 的 cache |
| L3 cache | Main memory 的 cache |
| Main memory (L4) | disk 的 cache(保存 disk blocks) |
| Local disk (L5) | 遠端網路伺服器磁碟的 cache(如 distributed file systems、Web servers) |
正如程式設計師可利用對各級 cache 的了解提升效能,也可利用對整個 memory hierarchy 的理解;完整討論(quantitative 分析、locality 原則)在 06-Memory-Hierarchy/02-Locality-and-Memory-Hierarchy。
- 「上層是下層的 cache」是概念模型;實務上各層管理者不同(registers 由編譯器、cache 由硬體、main memory 由 OS+硬體,Ch.6/Ch.9 詳述,見 09-Virtual-Memory/01-Address-Spaces-and-VM-Caching)。
- 「local disk 是遠端磁碟的 cache」僅適用於部分網路化系統(some networked systems with distributed file systems)。
Exam/Test Patterns
| 情境/關鍵字 | 答案 |
|---|---|
| shell 收到非內建命令的第一個字 | 假設是可執行檔名,載入並執行,等待其終止 |
| 匯流排傳輸的固定大小單位 | word;word size 為系統參數,今多為 4 bytes (32-bit) 或 8 bytes (64-bit) |
| controller vs. adapter 的差別 | 主要是封裝:controller 是裝置或主機板上的晶片組;adapter 是插槽上的介面卡;功能相同(I/O bus ↔ 裝置) |
| 主記憶體的物理/邏輯組成 | 物理:DRAM 晶片;邏輯:線性位元組陣列,位址從 0 起 |
x86-64 Linux 上 short / int / float / long / double 大小 |
2 / 4 / 4 / 8 / 8 bytes |
| PC (program counter) 是什麼 | CPU 內 word-size 暫存器,存放下一條要執行的機器指令的位址 |
| CPU 四種基本操作 | Load(mem→reg)、Store(reg→mem)、Operate(reg,reg→ALU→reg)、Jump(指令內 word→PC) |
| ISA vs. microarchitecture | ISA 描述指令效果(抽象);microarchitecture 描述實際實作(現代 CPU 遠比循序模型複雜) |
載入 hello 時資料不經 CPU 直達記憶體的技術 |
DMA (direct memory access) |
| disk vs. memory 的大小/速度對比 | disk 約大 1,000 倍,但讀取慢約 10,000,000 倍 |
| register file vs. memory 速度 | 讀 register file 快近 100 倍 |
| L1 / L2 cache 速度關係 | L2 比 L1 慢約 5 倍;L2 仍比 main memory 快 5~10 倍 |
| cache 用什麼技術、main memory 用什麼 | cache = SRAM;main memory = DRAM |
| 快取為何有效 | locality:程式傾向存取局部區域的資料與程式碼 |
| memory hierarchy 由上到下的趨勢 | 更慢、更大、每 byte 更便宜;每層是下一層的 cache |
| L0 / L4 分別是什麼 | L0 = register file;L4 = main memory(L1~L3 = caches,L5 = local disk,L6 = remote storage) |
| processor–memory gap 為何擴大 | 讓處理器變快比讓記憶體變快更容易、更便宜 |
Related Notes
- 01-Computer-Systems-Tour/01-Information-and-Program-Translation
- 01-Computer-Systems-Tour/03-OS-Abstractions-and-Networks
- 01-Computer-Systems-Tour/04-Amdahls-Law-and-Concurrency-Themes
- 06-Memory-Hierarchy/02-Locality-and-Memory-Hierarchy
- 06-Memory-Hierarchy/03-Cache-Memories
- 04-Processor-Architecture/04-Pipelining-Principles
- 08-Exceptional-Control-Flow/04-Process-Control