115 年 國立中正大學資訊工程學系碩士班《計算機系統》
Consider a 32-bit, byte-addressable system with a single cache that has the following specifications: total cache size = ( bytes) and cache block size = bytes. You will compare three cache organizations that all share the same total capacity and block size but differ in placement: (1) direct-mapped, (2) 4-way set-associative, and (3) fully-associative. Assume the cache is initially empty (all lines invalid). For the associative caches (4-way and fully-associative), assume LRU replacement. For writes, assume a write-back, write-allocate policy (i.e., on a write miss, the block is brought into the cache, and modified blocks are written back upon eviction).
The main memory size is increased significantly (e.g., from to ).
第 1. 題30 分
Process the following 10 memory accesses in order, where each access is either a read (R) or write (W) to the given byte address:
(1) R
(2) R
(3) W
(4) R
(5) R
(6) W
(7) R
(8) R
(9) R
(10) W
For each cache organization (direct-mapped, 4-way set-associative, and fully-associative), determine whether each of the 10 accesses is a cache hit or cache miss, and report the total number of hits and misses for that organization.
登入後即可作答並保存紀錄。
核心觀念
快取以「區塊」為單位存取。區塊大小為 bytes,因此位移欄位需要
先將每個位址除以 ,得到區塊位址;同一個區塊內的讀寫都命中同一筆快取資料。
快取共有 個快取區塊:
- 直接對映:有 條快取列,區塊位址的低 bits 決定索引。
- 4-way 組合關聯:有 組,區塊位址的低 bits 決定組索引;同組超過 4 個區塊時,以 LRU 淘汰最久未使用者。
- 全關聯:沒有組索引,區塊可放入任一快取列;本題只涉及 5 個不同區塊,容量足以容納全部。
解題方法
將不同區塊依首次出現順序標記如下:
| 標記 | 區塊位址 | 對應記憶體位址 |
|---|---|---|
| A | 0x1234567 | 0x12345670 至 0x1234567F |
| B | 0x1234667 | 0x12346670 至 0x1234667F |
| C | 0x1234767 | 0x12347670 至 0x1234767F |
| D | 0x1234867 | 0x12348670 至 0x1234867F |
| E | 0x1234967 | 0x12349670 至 0x1234967F |
10 次存取的區塊序列為:
A 至 E 的區塊位址低 8 bits 都是 0x67,因此直接對映時會全部競爭同一條快取列。它們的低 6 bits 也都相同,因此在 4-way 組合關聯時會全部落在同一組。
逐次判定
| 次數 | 區塊 | 直接對映 | 4-way 組合關聯 | 全關聯 |
|---|---|---|---|---|
| 1 | A | Miss | Miss | Miss |
| 2 | A | Hit | Hit | Hit |
| 3 | A | Hit | Hit | Hit |
| 4 | B | Miss | Miss | Miss |
| 5 | C | Miss | Miss | Miss |
| 6 | D | Miss | Miss | Miss |
| 7 | A | Miss | Hit | Hit |
| 8 | E | Miss | Miss | Miss |
| 9 | B | Miss | Miss | Hit |
| 10 | D | Miss | Hit | Hit |
直接對映
所有區塊都映射到同一條快取列。第 1 次載入 A 後,第 2、3 次仍存取 A,因此命中;第 4 次改存取 B 後,A 被取代。之後每次切換到不同區塊,都會取代列中的區塊,因此第 7 至第 10 次皆未命中。
- 命中:第 2、3 次,共 2 次
- 未命中:共 8 次
4-way 組合關聯
所有區塊都映射到同一組。前四個不同區塊 A、B、C、D 可同時放入 4 個 way。第 7 次存取 A 命中,之後依 LRU 淘汰區塊:
- 第 8 次載入 E 時,淘汰最久未使用的 B。
- 第 9 次存取 B 時未命中,淘汰此時最久未使用的 C。
- 第 10 次存取 D 時仍命中。
第 2. 題5 分
In Problem 1, if the main memory size is increased significantly (e.g., from to ), is it mandatory to increase the total cache size (currently ) for the system to function correctly?
(A) Increase
(B) Decrease
(C) No Change Required
登入後即可作答並保存紀錄。
核心觀念
快取容量決定可儲存多少資料區塊;主記憶體容量則決定可用的位址範圍。兩者相關,但主記憶體變大不代表必須增加快取容量。
題目指定位址為 32 位元、以位元組定址,因此可表示的位址數為 ,可定址範圍為 。若主記憶體擴大到 位元組,系統須能使用 34 位元實體位址,或其他可涵蓋該範圍的位址擴充機制。增加快取容量並不能取代位址擴充。
解題方法
快取區塊大小為 位元組,因此區塊內位移需要:
快取資料容量為 位元組,總區塊數為:
主記憶體增大時,快取仍可維持相同的資料容量與區塊大小。若實體位址由 32 位元擴充為 34 位元,改變的是快取標籤(tag)所需的位元數,索引與區塊內位移所需的位元數不變:
| 組織方式 | 區塊數/組數 | 索引位元數 | 34 位元位址下的標籤位元數 |
|---|---|---|---|
| 直接對映 | 256 個區塊 | ||
| 4 路組相連 | 組 | ||
| 全相連 | 無組索引 | 0 |
因此,擴大主記憶體會讓標籤欄位變寬,但不要求增加快取中可存放的資料量。若「4 KB」指資料陣列容量,資料容量仍是 ;實作上標籤等中繼資料所需的儲存空間會增加。
選項分析
第 3. 題5 分
Our favorite program runs on Computer A in 10 seconds. Computer A uses a 5-stage pipeline, has an average CPI of , and operates at a clock rate of . A computer designer is considering a new machine, Computer B, with the goal of reducing the execution time of this program to 6 seconds.
Computer B uses a deeper 10-stage pipeline. The increased pipeline depth allows for a higher clock rate; however, it also introduces additional pipeline overhead, such as larger branch misprediction penalties. As a result, the average CPI of Computer B increases to . The clock rate of Computer B is not yet determined.
Assume that the instruction count of the program is identical on Computer A and Computer B. Determine the clock rate that Computer B must achieve in order to meet the 6-second execution time target.
登入後即可作答並保存紀錄。
核心觀念
處理器的執行時間由指令數、平均每指令週期數(CPI)與時脈頻率共同決定:
題目指出兩台電腦執行相同程式,因此指令數不變。Computer B 的 CPI 已知為 ,只要求出它達成目標執行時間所需的時脈頻率。
解題方法
先利用 Computer A 的資料求出程式的指令數。其時脈頻率為 ,平均 CPI 為 ,執行時間為 秒:
因此,這段程式共有 條指令。將相同指令數、Computer B 的 CPI ,以及目標執行時間 秒代入公式:
解出 Computer B 的時脈頻率:
第 4. 題10 分
Modern ARM processors often use a big.LITTLE architecture to reduce energy consumption by executing only performance-critical code on high-power cores while running the remaining code on energy-efficient cores. Consider an ARM-based SoC with one big core and one LITTLE core, where only one core is active at any time. The LITTLE core consumes when active, while the big core consumes when active. When a core is inactive, its power consumption can be ignored.
A given program takes 10 seconds to complete when executed entirely on the LITTLE core. Profiling shows that of the execution time is performance-critical code that can benefit from the big core. The big core executes this portion faster than the LITTLE core. There is no overhead for switching between cores.
The program is executed using the following strategy:
a. The performance-critical portion runs on the big core.
b. The remaining portion runs on the LITTLE core.
What is the average power consumption (in watts) of the program when executed using the ARM big.LITTLE strategy described above?
登入後即可作答並保存紀錄。
核心觀念
平均功率是程式執行期間消耗的總能量除以總執行時間:
各核心消耗的能量則為功率乘上該核心的執行時間:
題目給的「 執行時間」以程式全程在 LITTLE 核心執行時的時間為基準,因此效能關鍵部分原本需 秒;大核心快 倍,執行時間便是原來的 。
解題方法
程式全程在 LITTLE 核心需 秒。效能關鍵部分在 LITTLE 核心上原需:
改由大核心執行後,所需時間為:
剩餘部分仍由 LITTLE 核心執行,時間為:
因此總執行時間為:
大核心執行 秒,消耗能量:
LITTLE 核心執行 秒,消耗能量:
第 5. 題10 分
CPU Scheduling & I/O Performance: CPU scheduling strategies significantly affect I/O system throughput.
a. Explain why it is crucial to let I/O-bound processes acquire the CPU as early as possible to maximize I/O throughput.
b. Using the Multilevel Feedback Queue algorithm as an example, explain how it dynamically identifies I/O-bound processes and adjusts their priorities to achieve this goal.
登入後即可作答並保存紀錄。
核心觀念
本題考的是 I/O 密集型(I/O-bound)行程與 CPU 密集型(CPU-bound)行程的差異,以及多層回饋佇列(Multilevel Feedback Queue, MLFQ)如何利用行程的執行行為調整排程優先權。
I/O 密集型行程通常只需短暫使用 CPU,便會發出 I/O 請求並等待裝置完成;CPU 密集型行程則常持續執行較長時間。I/O 裝置與 CPU 可在不同工作上重疊運作,因此及早讓 I/O 密集型行程取得 CPU,可以使它更早發出 I/O 請求,減少裝置閒置時間,提升 I/O 系統的整體吞吐量。
解題方法
先追蹤 I/O 密集型行程的執行順序:
再觀察 MLFQ 如何從 CPU 使用行為推測行程類型:短時間使用 CPU 後主動等待 I/O 的行程,通常會保留較高優先權;長時間耗盡時間片的行程,則會逐漸被移到較低優先權佇列。
(a)為何要讓 I/O 密集型行程儘早取得 CPU?
I/O 密集型行程必須先使用 CPU,才能執行產生 I/O 請求的程式指令。它越早取得 CPU,就越早能啟動磁碟、網路或其他 I/O 裝置的工作。
I/O 裝置啟動後,行程會進入等待狀態;此時 CPU 可排程其他就緒行程,I/O 裝置則持續處理該請求。若 I/O 密集型行程長時間等候 CPU,裝置便無法及時收到下一個請求,可能出現閒置,降低每單位時間完成的 I/O 工作量。
因此,及早排程 I/O 密集型行程能縮短 I/O 請求的啟動延遲,增加 CPU 與 I/O 裝置重疊工作的機會,進而提高 I/O 吞吐量。
(b)MLFQ 如何辨識並提高 I/O 密集型行程的優先權?
MLFQ 設有多個優先權佇列,通常較高層佇列的時間片較短,排程器優先選擇較高層佇列中的行程。
-
新行程從高優先權佇列開始。
新行程尚無執行紀錄時,先給予較高優先權,讓短工作及短 CPU 突發較容易迅速執行。 -
觀察行程如何使用 CPU。
I/O 密集型行程常在時間片用完前就發出 I/O 請求並阻塞。這表示它只需要短暫 CPU 時間,就能啟動裝置工作。MLFQ 通常讓這類行程保留原本的優先權;
第 6. 題10 分
Copy-on-Write (COW) Mechanism: When using the Copy-on-Write mechanism to optimize the fork() system call:
a. Explain how the Page Table Entries (PTEs) for both the Parent and the Child should be configured immediately after fork() completes.
b. When the Child process attempts to write to a shared page, a Page Fault is triggered. How does the Kernel distinguish whether this Page Fault is caused by the Copy-on-Write mechanism or by an invalid memory access?
登入後即可作答並保存紀錄。
核心觀念
fork() 建立子行程時,核心不必立刻複製父行程的所有記憶體頁面。Copy-on-Write(COW,寫入時複製)讓父、子行程先共用同一批實體頁框;只有某一方嘗試修改頁面時,核心才複製該頁。
頁表項目(PTE)通常包含頁面是否存在、是否可寫、使用者/核心權限等資訊。核心也可利用 PTE 中的軟體保留位元,標記頁面為 COW。
解題方法
a. fork() 完成後的 PTE 設定
對父行程中原本可寫的頁面,核心讓父、子行程的 PTE 都指向同一個實體頁框,並將兩邊的可寫權限關閉;核心另外將這些 PTE 標記為 COW。概念如下:
| PTE 項目 | 父行程 | 子行程 |
|---|---|---|
| 實體頁框 | 指向共用頁框 | 指向同一共用頁框 |
| 存在位元 | 設為有效 | 設為有效 |
| 可寫位元 | 關閉 | 關閉 |
| COW 標記 | 開啟 | 開啟 |
核心會記錄共用頁框的參照數。由於頁面已從可寫改成唯讀,核心也須確保處理器不會繼續使用快取中的舊可寫權限,例如透過 TLB 失效處理。
原本就是唯讀的頁面可以繼續共用,但不應標成 COW;COW 標記只適用於原本可寫、因共用而暫時設為唯讀的頁面。
b. 區分 COW 頁錯誤與無效記憶體存取
核心收到頁錯誤時,會檢查發生錯誤的虛擬位址是否有有效映射、錯誤原因是否為寫入權限違反,以及該 PTE 是否帶有 COW 標記。
若位址有效、頁面已存在、這次存取是寫入,且 PTE 標記為 COW,核心便判定這是 COW 頁錯誤。核心配置新的實體頁框、複製原頁內容,再把觸發錯誤行程的 PTE 改為指向新頁框並開啟可寫權限,同時清除 COW 標記。原共用頁框的參照數則減少。
第 7. 題30 分
Simple Reader-Writer Spinlock: Consider the following Simple Reader-Writer Spinlock implemented using atomic operations. Assume that atomic_sub(addr, val) atomically subtracts val from *addr and returns the old value (before subtraction). atomic_add performs atomic addition.
(a) Prove that mutual exclusion holds between a Writer and a Reader.
(b) Prove that multiple Readers can enter the Critical Section simultaneously (Concurrency).
#define MAXVAL 0x40000000
void init_spinlock(int* lock) {
*lock = MAXVAL;
}
void writer_lock(int* lock) {
while (1) {
int oldVal = atomic_sub(lock, MAXVAL);
if (oldVal == MAXVAL) return; // Successfully acquired lock
else atomic_add(lock, MAXVAL); // Failed, rollback
}
}
void reader_lock(int* lock) {
while (1) {
int oldVal = atomic_sub(lock, 1);
if (oldVal > 0) return; // Successfully acquired lock
else atomic_add(lock, 1); // Failed, rollback
}
}
void reader_unlock(int* lock) {
atomic_add(lock, 1);
}
void writer_unlock(int* lock) {
atomic_add(lock, MAXVAL);
}
登入後即可作答並保存紀錄。
核心觀念
這把鎖把整數 lock 同時用來記錄讀者數量與寫者占用狀態。令
在沒有操作進行到一半的穩定狀態中:
- 沒有寫者、目前有 位讀者時,
lock的值為 。 - 寫者持有鎖時,
lock的值為 。
atomic_sub(addr, val) 會以不可分割的方式執行減法,並回傳減法前的值。因此,判斷是否取得鎖,須看這次原子減法回傳的舊值。
以下證明假設原子操作具備正確的同步語意,且運算過程不發生整數溢位。
解題方法
追蹤每次原子減法發生前的 lock 值,再依照程式中的條件判斷該次操作是成功取得鎖,還是失敗並回復。
(a) 寫者與讀者互斥
寫者執行:
int oldVal = atomic_sub(lock, M);
if (oldVal == M) return;
寫者只有在減法前的值恰為 時,才會取得鎖。原子減法後,lock 變成 。
此時若有讀者嘗試取得鎖,它執行 atomic_sub(lock, 1) 時,會讀到非正值,因為在寫者持有鎖期間,沒有成功的讀者能把 lock 增加。因此讀者無法通過 oldVal > 0 的條件,會執行加一回復並繼續等待。
反過來,若至少有一位讀者已持有鎖,設讀者數為 ,穩定狀態下:
寫者執行減去 的操作時,回傳值不等於 ,所以寫者不能取得鎖,並會用 atomic_add(lock, M) 回復其減法。
即使寫者與讀者同時競爭,原子操作仍會將兩者的操作排出先後順序:寫者只有在讀者計數為零時才能成功;寫者成功後,讀者讀到的舊值不會大於零。故寫者與讀者不會同時進入 Critical Section。
(b) 多位讀者可同時進入
讀者執行:
int oldVal = atomic_sub(lock, 1);
if (oldVal > 0) return;
每位成功取得鎖的讀者,會讓 lock 減一。若目前已有 位讀者,且沒有寫者持有鎖,則: