--- title: 2020 年春季 Linux 核心設計課程作業 —— fiber image: https://repository-images.githubusercontent.com/251482008/2b046c00-7341-11ea-9096-4d78f54fdbae description: 檢驗學員對 Linux 核心 kthread 和 workqueue 處理機制的認知 --- # H11: fiber ###### tags: `linux2020` > 主講人: [jserv](http://wiki.csie.ncku.edu.tw/User/jserv) / 課程討論區: [2020 年系統軟體課程](https://www.facebook.com/groups/system.software2020/) :mega: 返回「[Linux 核心設計](http://wiki.csie.ncku.edu.tw/linux/schedule)」課程進度表 ## :memo: 預期目標 * 搭配第 8 周進度,理解執行緒實作機制 * 針對 Linux 核心的設計,實作高效率的 M:N threading model * 學習透過 eBPF 追蹤特定的 Linux 系統呼叫並解讀 ## :watch: Thread Pool 實作和視覺化分析 [ThreadKit](https://github.com/sysprog21/threadkit) 提供一系列輕量級的執行緒工具,其中包含一個簡易的 Thread Pool 實作,搭配內建視覺化的效能分析工具 (profiler),後者利用 [chrome://tracing](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool)。 取得原始程式碼並編譯測試: ```shell $ git clone https://github.com/sysprog21/threadkit $ cd threadkit $ make $ make check ``` 預期可見類似以下的輸出: ``` ThreadTracer: Wrote 30 events (0 discarded) to threadtracer.48206.json [ Verified ] ``` 在現行目錄下,會出現檔名為 `threadtracer.PID.json` 的檔案,其中 PID 是數值,即 process ID。此時,打開 Google Chrome 網頁瀏覽器,開啟網址 ==`chrome://tracing`== 並注意左上角有個 "load" 的按鈕,按下並指定剛才產生的 JSON 檔案,預期可見類似下圖: ![](https://i.imgur.com/qytK6BT.png) 上圖的 `dummy_task` 為 thread function, `Process 47041` 是 PID、粉紅色區域則是執行過程中的 CPU 使用率,你可以透過右方的視角切換功能,放大特定區域,點擊即可見下方資訊,例如在特定的時間單元中,執行時間和其比例等等。又,由於 thread pool 的運作機制是事先建立執行緒 (在本例是 `4` 個),所以無論多少 thread pool 之上的任務被新增,終究只有 4 個執行緒並行運作。 ## :fire: Fiber [fiber](http://github.com/sysprog21/fiber) 是個實作 M:N threading model 的套件,針對 Linux 核心機制發展,設計動機是提供比原生執行緒 (native thread) 更小的執行單元,一般可稱為 [Fiber](https://en.wikipedia.org/wiki/Fiber_(computer_science))。現有的實作具備以下特徵: * Preemptive user-level threads * Familiar threading concepts are available - Mutexes - Condition varialbles 取得原始程式碼並編譯測試: ```shell $ git clone https://github.com/sysprog21/fiber $ cd fiber $ make $ make check ``` 預期可見類似以下輸出: (有時無法全數執行通過,實屬正常,現行實作存在若干缺陷) ``` *** Validating tests/test-context *** Ping! ping > Pong! < pong ping > < pong ping > < pong main: exiting [ Verified ] *** Validating tests/test-mutex *** 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 ... ``` 研讀 [Linux 核心設計: 透過 eBPF 觀察作業系統行為](https://hackmd.io/@sysprog/linux-ebpf),得知 clone 系統呼叫在一定時間內的統計量和發生頻率,也能善用 [calltop](https://github.com/egobillot/calltop) 這個同樣運用 eBPF 的工具來分析系統呼叫。 ## :penguin: 作業要求 * 研讀 Paul Turner 的簡報檔案 [User-level threads](http://pdxplumbers.osuosl.org/2013/ocw//system/presentations/1653/original/LPC%20-%20User%20Threading.pdf) 及相對應的[演講錄影](https://youtu.be/KXuZi9aeGTw),記錄你的認知,並回答以下問題: 1. M:N model 的優勢在哪?請舉例說明 2. M:N model 的劣勢為何?舉例說明並談論 Linux NPTL 作為折衷的設計,做了哪些修正?搭配閱讀 [The future of M:N threading](https://mail.mozilla.org/pipermail/rust-dev/2013-November/006550.html) 討論串 3. Google 內部 (至少在 2013 年) 針對執行緒實作做了哪些調整? * 答覆 [第 8 週測驗題](https://hackmd.io/@sysprog/linux2020-quiz8) 第 2 題組的所有延伸問題,需要設計實驗和解讀各式數據 * 解釋上述 [threadpool](http://github.com/sysprog21/threadpool) 內建 profiler 運作的機制,特別是 [getrusage](http://man7.org/linux/man-pages/man2/getrusage.2.html) 和 `RUSAGE_THREAD` 的使用。可透過 eBPF 觀察及分析; * 自 GitHub [fiber](http://github.com/sysprog21/fiber) 進行 fork,目標是修正現有 [fiber](http://github.com/sysprog21/fiber) 實作的缺失並發展視覺化效能分析工具,過程中應滿足以下: 1. 解釋 Fiber 運作機制,和指出原有測試項目無法通過的原因並修正; 2. 以 `$ grep -r FIXME` 找出程式碼標注的改進事項,特別是記憶體管理相關,若能引入高效能的 [memory pool](https://en.wikipedia.org/wiki/Memory_pool) 實作,會有助益; 3. 學習 [第 8 週測驗題](https://hackmd.io/@sysprog/linux2020-quiz8) 的實作手法,對 Fiber 程式予以重構 (refactor),讓實作更清晰並易於維護; 4. 針對 Fiber,撰寫更多的測試案例,如 [Producer–consumer problem](https://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem),過程中你可能會新增若干 API,儘可能維持和 POSIX Thread 的對應; 5. 將前述的視覺化效能分析工具整合在 Fiber 並設計對應的實驗; 6. 提出改進 Fiber 效能的機制並予以落實; 7. [ThreadKit](https://github.com/sysprog21/threadkit) 提出的 tasklet 定位是 "thread without stack",思考其效益,也對照 [Protothread](https://github.com/LarryRuane/protothread-multicore) 這樣 "Threads without stacks" 的設計 (即不用依賴 ucontext) 的效能表現,和探討其限制; ## 繳交方式 編輯 [Homework6 作業區共筆](https://hackmd.io/@sysprog/linux2020-homework6),將你的觀察、上述要求的解說、應用場合探討,以及各式效能改善過程,善用 gnuplot 製圖,紀錄於新建立的共筆 ## 截止日期 * May 9, 2020 (含) 之前 > 越早在 GitHub 上有動態、越早接受 code review,評分越高