# 2023q1 Homework7 (ktcp) ###### tags: `linux2023` contributed by <`Paintako`> > [作業說明](https://hackmd.io/@sysprog/linux2023-ktcp) ## 開發環境 ```bash $ gcc --version gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 $ lscpu Architecture: x86_64 CPU op-mode(s): 32-bit,64-bit Address sizes: 39 bits physical,48 bits virtual Byte Order: Little Endian CPU(s): 12 On-line CPU(s) list: 0-11 Vendor ID: GenuineIntel Model name: Intel(R) Core(TM) i5-10400 CPU @ 2.90GHz CPU family: 6 Model: 165 Thread(s) per core: 2 Core(s) per socket: 6 Socket(s): 1 Stepping: 5 CPU max MHz: 4300.0000 CPU min MHz: 800.0000 BogoMIPS: 5799.77 ``` ## CMWQ 原文提到: `Although MT(multi threaded) wq(workque) wasted a lot of resource, the level of concurrency provided was unsatisfactory. The limitation was common to both ST(single threaded) and MT wq albeit less severe on MT. Each wq maintained its own separate worker pool. An MT wq could provide only one execution context per CPU while an ST wq one for the whole system. Work items had to compete for those very limited execution contexts leading to various problems including proneness to deadlocks around the single execution context.` 由於 workqueue run with **kernal thread**,而每一個 cpu 只會對應一條 workqueue,這導致了在 queue 內的 work item 需要競爭同一個 execution contexts (也就是只有一個執行環境,但 queue 內的每個 item 都會競爭同一 resouce),這導致了`concuerrency` 的瓶頸 Concurrency Managed Workqueue (cmwq) 是 wq 的重做版 * 傳統 Workqueue,每一個 cpu 都有自己的 workqueue (如下圖) * ![](https://hackmd.io/_uploads/r1ZlB-YNh.png) * 而 cmwq 中,每一個 worker (CPU) 共用所有的 wq,而且會自動調整 worker pool 及 level of concurrency 參考 [教材](https://hackmd.io/@sysprog/linux2023-ktcp/%2F%40sysprog%2Flinux2023-ktcp-c) 內對 CMWQ 的描述,有提到一點 `目的是要簡化執行緒的建立,可以根據目前系統的 CPU 個數建立執行緒,使得並行化執行緒。` ## kecho 內 CMWQ 優勢和用法,實驗方法重現 首先要比較 `1. 引入 CMWQ 前`,以及 `2. 引入 CMWQ 後` 的性能差異 閱讀 CMWQ manual 後,在 CMWQ 被引入前,都是使用 `workqueue` 實做,而 `workqueue` 是用 `kernal thread` 實做 閱讀此 [merge request](https://github.com/sysprog21/kecho/pull/1),可以發現更改紀錄中如下 ```diff while (!kthread_should_stop()) { /* using blocking I/O */ error = kernel_accept(param->listen_sock,&sock,0); if (error < 0) { if (signal_pending(current)) break; printk(KERN_ERR MODULE_NAME ": socket accept error = %d\n",error); continue; } - /* start server worker */ - thread = kthread_run(echo_server_worker,sock,MODULE_NAME); - if (IS_ERR(thread)) { - printk(KERN_ERR MODULE_NAME ": create worker thread error = %d\n", - error); + if (unlikely(!(work = create_work(sock)))) { + printk(KERN_ERR MODULE_NAME + ": create work error,connection closed\n"); + kernel_sock_shutdown(sock,SHUT_RDWR); + sock_release(sock); + continue; + } + /* start server worker */ + queue_work(fastecho_wq,work); ``` 根據 [ktrhead.h](https://github.com/torvalds/linux/blob/master/include/linux/kthread.h) 對 `kthread_run` 的描述,`ktrhead_run` 用於 `create and wake a thread` 一旦監聽到一個 request,則每次都會呼叫 `kthread_run` 來創建一個 thread 來處理,而更改後的版本改成 enqueue 來處理 [TODO] 重現實驗 ## 搭配閱讀《Demystifying the Linux CPU Scheduler》第 1 章和第 2 章,描述 CPU 排程器和 workqueue/CMWQ 的互動,應探究相關 Linux 核心原始程式碼 [閱讀筆記](https://hackmd.io/@Paintako/linux_schuduler) ## 引入 CWWQ ,改寫 kHTTPd 參考 `kecho`, ## 問題紀錄 ### CMWQ 1. `asynchronous process execution context`,這裡的 context 是? Ans: * 這裡 context 的定義是: 一個 cpu 執行這個程式,跟這個程式所有有關的資料稱之. * 當一個 process 因為 system call trap 到 OS 請求服務時,context 依舊保持在同一個 process. * e.g. * Process A 執行到 printf,執行 system call,jump to kernal,CPU 的狀態仍然屬於 Process A ( 也就是說有 mode change 不代表 context switch) 參考來源 [Process Management – Part I](https://www.youtube.com/watch?v=bw3aRZCfsL4) 2. CMWQ manual 提到的 workqueue 是指? Ans: * 要了解Workqueue,必須從 `interupt` 講起. I/O device 有(共用或者獨立皆有)的 IRQs (Iterupt Requst lines),IRQs 可以把特定的硬體對應到 **interupt vecotor**,然後傳遞給 cpu 而 Interupt Handling (中斷處理)可以被分成兩個部份 1. Ciritcal: Top-half (interrupt disable) * 例如處理中斷這件事情,是不能被其他 interupt 給打斷的 2. Non-critical: Top-half (interrupt enabled) * 例如 read key 3. Non-critical deferrable: Bottom half (do it later),或稱 soft-irq * 一段程式分成兩個部份,前半段是很重要一定要馬上處理的,後半段比較沒有那緊急,可以晚點再做(可以晚點做意味著可以排程!) Bottom half 在 Kernal 中有相對應的機制去處理他們,李如 `softirqs`,`tasklets`,`work queues` * softirqs * 靜態分配( at kernal compile time),也有執行時期建立的 softirqs * 它會對不同類型的任務給予不同的優先權 * e.g. * 0 -> High-priority tasklets * 1 -> Time interrupts * ... * **Ruegular tasklets** * softirq 可以 reschedule itself * this could starve some user-level processes * tasklets * 跟 softirqs 很像 * Created and destroyed **dynamically** * Only one instance of tasklet can run,even with mutilple CPUs * Workqueue * Always run with **kernal thread** * Softirqs 及 tasklets 皆跑在 interrupt context (interrpt context 指發生中斷時處理中斷所在的執行環境,而 process context 是跟這個 process 所有有關的資料) * 而 work queue 有 **process context** * 因為有 process context,故可以 **sleep** 參考來源 [Linux 核心設計: 中斷處理和現代架構考量](https://hackmd.io/@sysprog/linux-interrupt) 3. asynchronous process 是指? Ans: 只有聽過 asynchrounous I/O,不知道哪種情況下 process 是 asynchronous 的,是處理 I/O 的時候的行為嗎? 4. Concurrency 指? 參考 [並行和多執行緒程式設計](https://hackmd.io/@sysprog/concurrency/https%3A%2F%2Fhackmd.io%2F%40sysprog%2FS1AMIFt0) 內的解釋: `Concurrency 指程式架構,將程式拆開成多個可獨立運作的工作,像是驅動程式都可獨立運作` 例如,手機上的驅動程式,他們都是獨立運行,不必在意他們的運行順序,但有些時候他們會合作. `Parallelism` 指的是程式的**規劃** `Synchronization` 確保多個執行單元運作並存取資源時,執行結果不會因為執行單元的時間先後的影響而導致錯誤 督 嚕嚕督嚕嚕