linux2023
contributed by <Paintako
>
$ 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
原文提到:
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 的重做版
參考 教材 內對 CMWQ 的描述,有提到一點 目的是要簡化執行緒的建立,可以根據目前系統的 CPU 個數建立執行緒,使得並行化執行緒。
首先要比較 1. 引入 CMWQ 前
,以及 2. 引入 CMWQ 後
的性能差異
閱讀 CMWQ manual 後,在 CMWQ 被引入前,都是使用 workqueue
實做,而 workqueue
是用 kernal thread
實做
閱讀此 merge request,可以發現更改紀錄中如下
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 對 kthread_run
的描述,ktrhead_run
用於 create and wake a thread
一旦監聽到一個 request,則每次都會呼叫 kthread_run
來創建一個 thread 來處理,而更改後的版本改成 enqueue 來處理
[TODO] 重現實驗
參考 kecho
,
asynchronous process execution context
,這裡的 context 是?Ans:
參考來源 Process Management – Part I
2. CMWQ manual 提到的 workqueue 是指?
Ans:
interupt
講起.I/O device 有(共用或者獨立皆有)的 IRQs (Iterupt Requst lines),IRQs 可以把特定的硬體對應到 interupt vecotor,然後傳遞給 cpu
而 Interupt Handling (中斷處理)可以被分成兩個部份
Bottom half 在 Kernal 中有相對應的機制去處理他們,李如 softirqs
,tasklets
,work queues
softirqs
tasklets
Workqueue
參考 並行和多執行緒程式設計 內的解釋:
Concurrency 指程式架構,將程式拆開成多個可獨立運作的工作,像是驅動程式都可獨立運作
例如,手機上的驅動程式,他們都是獨立運行,不必在意他們的運行順序,但有些時候他們會合作.
Parallelism
指的是程式的規劃
Synchronization
確保多個執行單元運作並存取資源時,執行結果不會因為執行單元的時間先後的影響而導致錯誤
督 嚕嚕督嚕嚕