Involuntary Context Switch
前面的 Voluntary context switch 可能會產生 starvation 的問題,那有什麼方式可以強迫 process 到一定的時間就進行 context switch?
思路
圖片來自 Lab 4 Spec

透過每次 Timer Interrupt 的 IRQ 進到 kernel space 檢查目前 process 的使用時間,如果超過就將 task structure 的 reschedule flag 設起來,並在回到 user space 時檢查是否需要進行 reschedule。
開啟 Core timer
schedule.c
在 process 0 初始化完 scheduler 前,把 core timer 打開。
|
|
從 Kernel stack 切換至 Interrupt Stack
將原本的 irq_exc_router
再包一層 irq_stk_switcher
。
exception_table.S
|
|
exception.c
|
|
Reschedule
在離開 interrupt handler 前檢查 task structure 的 reschedule flag,如果有設定 reschedule flag 就進行 reschedule。
exception.c
|
|
Core timer interrupt handler
若 task structure 的 counter
<= 0 時,設定 reschedule flag。
exception.c
在所有的 critical region 結束後,將 interrupt 再次打開 (for nested interrupt)。或是你也可以讓 EL1 完全不能被 interrupt,但這樣可能 responsive 會不好就是了。
|
|
請盡量保持 interrupt handler 的簡潔與效能,最好不要在 interrupt context 中進行 reschedule