硬體組織與程式執行 (Hardware Organization & Program Execution)

Overview Table

小節 主題 核心結論
1.4 (p.43) Shell 與程式執行 shell 是命令列直譯器;非內建命令即視為可執行檔,載入並執行
1.4.1 (p.44-46) 硬體組織 系統由 busesI/O devicesmain memoryprocessor (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 執行:

linux> ./hello        ← shell 讀入命令
hello, world          ← hello 程式輸出
linux>                ← hello 終止,shell 重新印提示字元
Tip

shell 如何真正「載入並執行」程式(fork + execve、loader、run-time 記憶體映像)在 08-Exceptional-Control-Flow/04-Process-Control07-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(匯流排)

例外/注意

本書不假設固定的 word size 定義;凡需要精確意義的語境都會另行指明「word」所指為何。(x86-64 組語中的 "word" 慣例上反而是 16-bit,見 03-Machine-Level-Programs/01-Program-Encodings-and-Data-Formats。)

I/O Devices(輸入/輸出裝置)

Controller Adapter
形式 裝置本身或主機板 (motherboard) 上的晶片組 (chip sets) 插在主機板插槽上的介面卡
目的 相同:在 I/O bus 與 I/O 裝置間來回傳遞資訊 同左

Main Memory(主記憶體)

Processor (CPU)

簡單操作環繞三個部件:main memoryregister file(一組各有唯一名稱的 word-size 暫存器)、ALU (arithmetic/logic unit)(計算新的資料值與位址值):

操作 效果
Load 從主記憶體複製 byte 或 word 到暫存器(覆寫原內容)
Store 從暫存器複製 byte 或 word 到主記憶體某位置(覆寫原內容)
Operate 兩個暫存器內容送 ALU 做算術運算,結果存回暫存器(覆寫原內容)
Jump 指令本身取出一個 word,複製進 PC(覆寫 PC)
ISA vs. Microarchitecture

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" 逐位元組送往螢幕顯示)

1.5 Caches Matter (p.47-49)

關鍵教訓:系統花大量時間在把資訊從一處搬到另一處(disk → memory → CPU → display)。對程式設計師而言這些複製都是拖慢「真正工作」的 overhead,故系統設計者的主要目標是讓複製操作盡可能快。

物理定律造成的基本取捨:

Processor–Memory Gap

隨半導體技術演進,這個差距持續擴大:讓處理器變快比讓主記憶體變快更容易也更便宜

解法:插入更小、更快的 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
Important

本書最重要的教訓之一:了解 cache 的應用程式設計師,可將程式效能提升一個數量級 (order of magnitude)。詳見 06-Memory-Hierarchy/03-Cache-Memories06-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  ----------->
這一層 是誰的 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)
Tip

正如程式設計師可利用對各級 cache 的了解提升效能,也可利用對整個 memory hierarchy 的理解;完整討論(quantitative 分析、locality 原則)在 06-Memory-Hierarchy/02-Locality-and-Memory-Hierarchy

Simplification-with-exceptions

  • 「上層是下層的 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 為何擴大 讓處理器變快比讓記憶體變快更容易、更便宜