自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux驅(qū)動實踐:中斷處理中的【工作隊列】 Workqueue 是什么鬼?

系統(tǒng) Linux
工作隊列是Linux操作系統(tǒng)中,進行中斷下半部分處理的重要方式!從名稱上可以猜到:一個工作隊列就好像業(yè)務(wù)層常用的消息隊列一樣,里面存放著很多的工作項等待著被處理。

[[442152]]

別人的經(jīng)驗,我們的階梯!

大家好,我是道哥,今天我為大伙兒解說的技術(shù)知識點是:【中斷處理中的下半部分機制-工作隊列】。

在剛開始介紹中斷處理的時候,曾經(jīng)貼出下面這張圖:

圖中描述了中斷處理中的下半部分都有哪些機制,以及如何根據(jù)實際的業(yè)務(wù)場景、限制條件來進行選擇。

可以看出:這些不同的實現(xiàn)之間,有些是重復(fù)的,或者是相互取代的關(guān)系。

也正因為此,它們之間的使用方式幾乎是大同小異,至少是在API接口函數(shù)的使用方式上,從使用這的角度來看,都是非常類似的。

這篇文章,我們就通過實際的代碼操作,來演示一下工作隊列(workqueue)的使用方式。

工作隊列是什么

工作隊列是Linux操作系統(tǒng)中,進行中斷下半部分處理的重要方式!

從名稱上可以猜到:一個工作隊列就好像業(yè)務(wù)層常用的消息隊列一樣,里面存放著很多的工作項等待著被處理。

工作隊列中有兩個重要的結(jié)構(gòu)體:工作隊列(workqueue_struct) 和 工作項(work_struct):

  1. struct workqueue_struct { 
  2.     struct list_head        pwqs;           /* WR: all pwqs of this wq */ 
  3.     struct list_head        list;           /* PR: list of all workqueues */ 
  4.     ... 
  5.     char                    name[WQ_NAME_LEN]; /* I: workqueue name */ 
  6.     ... 
  7.     /* hot fields used during command issue, aligned to cacheline */ 
  8.     unsigned int            flags ____cacheline_aligned; /* WQ: WQ_* flags */ 
  9.     struct pool_workqueue __percpu *cpu_pwqs; /* I: per-cpu pwqs */ 
  10.     struct pool_workqueue __rcu *numa_pwq_tbl[]; /* PWR: unbound pwqs indexed by node */ 
  11. }; 
  1. struct work_struct { 
  2.         atomic_long_t data; 
  3.         struct list_head entry; 
  4.         work_func_t func;   // 指向處理函數(shù) 
  5. #ifdef CONFIG_LOCKDEP                                                                                    
  6.         struct lockdep_map lockdep_map; 
  7. #endif 
  8. }; 

在內(nèi)核中,工作隊列中的所有工作項,是通過鏈表串在一起的,并且等待著操作系統(tǒng)中的某個線程挨個取出來處理。

這些線程,可以是由驅(qū)動程序通過 kthread_create 創(chuàng)建的線程,也可以是由操作系統(tǒng)預(yù)先就創(chuàng)建好的線程。

這里就涉及到一個取舍的問題了。

如果我們的處理函數(shù)很簡單,那么就沒有必要創(chuàng)建一個單獨的線程來處理了。

原因有二:

  1. 創(chuàng)建一個內(nèi)核線程是很耗費資源的,如果函數(shù)很簡單,很快執(zhí)行結(jié)束之后再關(guān)閉線程,太劃不來了,得不償失;
  2. 如果每一個驅(qū)動程序編寫者都毫無節(jié)制地創(chuàng)建內(nèi)核線程,那么內(nèi)核中將會存在大量不必要的線程,當然了本質(zhì)上還是系統(tǒng)資源消耗和執(zhí)行效率的問題;

為了避免這種情況,于是操作系統(tǒng)就為我們預(yù)先創(chuàng)建好一些工作隊列和內(nèi)核線程。

我們只需要把需要處理的工作項,直接添加到這些預(yù)先創(chuàng)建好的工作隊列中就可以了,它們就會被相應(yīng)的內(nèi)核線程取出來處理。

例如下面這些工作隊列,就是內(nèi)核默認創(chuàng)建的(include/linux/workqueue.h):

  1. /* 
  2.  * System-wide workqueues which are always present. 
  3.  * 
  4.  * system_wq is the one used by schedule[_delayed]_work[_on](). 
  5.  * Multi-CPU multi-threaded.  There are users which expect relatively 
  6.  * short queue flush time.  Don't queue works which can run for too 
  7.  * long. 
  8.  * 
  9.  * system_highpri_wq is similar to system_wq but for work items which 
  10.  * require WQ_HIGHPRI. 
  11.  * 
  12.  * system_long_wq is similar to system_wq but may host long running 
  13.  * works.  Queue flushing might take relatively long. 
  14.  * 
  15.  * system_unbound_wq is unbound workqueue.  Workers are not bound to 
  16.  * any specific CPU, not concurrency managed, and all queued works are 
  17.  * executed immediately as long as max_active limit is not reached and 
  18.  * resources are available. 
  19.  * 
  20.  * system_freezable_wq is equivalent to system_wq except that it's 
  21.  * freezable. 
  22.  * 
  23.  * *_power_efficient_wq are inclined towards saving power and converted 
  24.  * into WQ_UNBOUND variants if 'wq_power_efficient' is enabled; otherwise, 
  25.  * they are same as their non-power-efficient counterparts - e.g. 
  26.  * system_power_efficient_wq is identical to system_wq if 
  27.  * 'wq_power_efficient' is disabled.  See WQ_POWER_EFFICIENT for more info. 
  28.  */ 
  29.  
  30. extern struct workqueue_struct *system_wq; 
  31. extern struct workqueue_struct *system_highpri_wq; 
  32. extern struct workqueue_struct *system_long_wq; 
  33. extern struct workqueue_struct *system_unbound_wq; 
  34. extern struct workqueue_struct *system_freezable_wq; 
  35. extern struct workqueue_struct *system_power_efficient_wq; 
  36. extern struct workqueue_struct *system_freezable_power_efficient_wq; 

以上這些默認工作隊列的創(chuàng)建代碼是(kernel/workqueue.c):

  1. int __init workqueue_init_early(void) 
  2.     ...     
  3.     system_wq = alloc_workqueue("events", 0, 0); 
  4.     system_highpri_wq = alloc_workqueue("events_highpri", WQ_HIGHPRI, 0);                            
  5.     system_long_wq = alloc_workqueue("events_long", 0, 0); 
  6.     system_unbound_wq = alloc_workqueue("events_unbound", WQ_UNBOUND, 
  7.                                             WQ_UNBOUND_MAX_ACTIVE); 
  8.     system_freezable_wq = alloc_workqueue("events_freezable"
  9.                                               WQ_FREEZABLE, 0); 
  10.     system_power_efficient_wq = alloc_workqueue("events_power_efficient"
  11.                                               WQ_POWER_EFFICIENT, 0); 
  12.     system_freezable_power_efficient_wq = alloc_workqueue("events_freezable_power_efficient"
  13.                                               WQ_FREEZABLE | WQ_POWER_EFFICIENT, 
  14.                                               0); 
  15.     ... 

此外,由于工作隊列 system_wq 被使用的頻率很高,于是內(nèi)核就封裝了一個簡單的函數(shù)(schedule_work)給我們使用:

  1. /** 
  2.  * schedule_work - put work task in global workqueue 
  3.  * @work: job to be done 
  4.  * 
  5.  * Returns %false if @work was already on the kernel-global workqueue and 
  6.  * %true otherwise. 
  7.  * 
  8.  * This puts a job in the kernel-global workqueue if it was not already 
  9.  * queued and leaves it in the same position on the kernel-global 
  10.  * workqueue otherwise. 
  11.  */ 
  12.  
  13. static inline bool schedule_work(struct work_struct *work){    
  14.     return queue_work(system_wq, work); 

當然了,任何事情有利就有弊!

由于內(nèi)核默認創(chuàng)建的工作隊列,是被所有的驅(qū)動程序共享的。

如果所有的驅(qū)動程序都把等待處理的工作項委托給它們來處理,那么就會導致某個工作隊列中過于擁擠。

根據(jù)先來后到的原則,工作隊列中后加入的工作項,就可能因為前面工作項的處理函數(shù)執(zhí)行的時間太長,從而導致時效性無法保證。

因此,這里存在一個系統(tǒng)平衡的問題。

關(guān)于工作隊列的基本知識點就介紹到這里,下面來實際操作驗證一下。

驅(qū)動程序

之前的幾篇文章,在驅(qū)動程序中測試中斷處理的操作流程都是一樣的,因此這里就不在操作流程上進行贅述了。

這里直接給出驅(qū)動程序的全貌代碼,然后查看 dmesg 的輸出信息。

創(chuàng)建驅(qū)動程序源文件和 Makefile:

  1. $ cd tmp/linux-4.15/drivers 
  2. $ mkdir my_driver_interrupt_wq 
  3. $ touch my_driver_interrupt_wq.c 
  4. $ touch Makefile 

示例代碼全貌

測試場景是:加載驅(qū)動模塊之后,如果監(jiān)測到鍵盤上的ESC鍵被按下,那么就往內(nèi)核默認的工作隊列system_wq中增加一個工作項,然后觀察該工作項對應(yīng)的處理函數(shù)是否被調(diào)用。

  1. #include <linux/kernel.h> 
  2. #include <linux/module.h> 
  3. #include <linux/interrupt.h> 
  4.  
  5. static int irq;                  
  6. static char * devname;       
  7.  
  8. static struct work_struct mywork;    
  9.              
  10.  // 接收驅(qū)動模塊加載時傳入的參數(shù) 
  11. module_param(irq, int, 0644); 
  12. module_param(devname, charp, 0644); 
  13.  
  14. // 定義驅(qū)動程序的 ID,在中斷處理函數(shù)中用來判斷是否需要處理             
  15. #define MY_DEV_ID           1226 
  16.  
  17. // 驅(qū)動程序數(shù)據(jù)結(jié)構(gòu) 
  18. struct myirq 
  19.     int devid; 
  20. }; 
  21.   
  22. struct myirq mydev  ={ MY_DEV_ID }; 
  23.  
  24. #define KBD_DATA_REG        0x60   
  25. #define KBD_STATUS_REG      0x64 
  26. #define KBD_SCANCODE_MASK   0x7f 
  27. #define KBD_STATUS_MASK     0x80 
  28.  
  29. // 工作項綁定的處理函數(shù) 
  30. static void mywork_handler(struct work_struct *work
  31.     printk("mywork_handler is called. \n"); 
  32.     // do some other things 
  33.          
  34. //中斷處理函數(shù) 
  35. static irqreturn_t myirq_handler(int irq, void * dev) 
  36.     struct myirq mydev; 
  37.     unsigned char key_code; 
  38.     mydev = *(struct myirq*)dev;     
  39.      
  40.     // 檢查設(shè)備 id,只有當相等的時候才需要處理 
  41.     if (MY_DEV_ID == mydev.devid) 
  42.     { 
  43.         // 讀取鍵盤掃描碼 
  44.         key_code = inb(KBD_DATA_REG); 
  45.      
  46.         if (key_code == 0x01) 
  47.         { 
  48.             printk("ESC key is pressed! \n"); 
  49.              
  50.             // 初始化工作項 
  51.             INIT_WORK(&mywork, mywork_handler); 
  52.              
  53.             // 加入到工作隊列 system_wq 
  54.                     schedule_work(&mywork); 
  55.         } 
  56.     }    
  57.  
  58.     return IRQ_HANDLED; 
  59.   
  60. // 驅(qū)動模塊初始化函數(shù) 
  61. static int __init myirq_init(void) 
  62.     printk("myirq_init is called. \n"); 
  63.  
  64.     // 注冊中斷處理函數(shù) 
  65.     if(request_irq(irq, myirq_handler, IRQF_SHARED, devname, &mydev)!=0) 
  66.     { 
  67.         printk("register irq[%d] handler failed. \n", irq); 
  68.         return -1; 
  69.     } 
  70.  
  71.     printk("register irq[%d] handler success. \n", irq); 
  72.     return 0; 
  73.   
  74. // 驅(qū)動模塊退出函數(shù) 
  75. static void __exit myirq_exit(void) 
  76.     printk("myirq_exit is called. \n"); 
  77.  
  78.     // 釋放中斷處理函數(shù) 
  79.     free_irq(irq, &mydev); 
  80.   
  81. MODULE_LICENSE("GPL"); 
  82. module_init(myirq_init); 
  83. module_exit(myirq_exit); 

ntk("myirq_exit is called. \n"); // 釋放中斷處理函數(shù) free_irq(irq, &mydev);} MODULE_LICENSE("GPL");module_init(myirq_init);module_exit(myirq_exit);

Makefile 文件

  1. ifneq ($(KERNELRELEASE),) 
  2.     obj-m := my_driver_interrupt_wq.o 
  3. else 
  4.     KERNELDIR ?= /lib/modules/$(shell uname -r)/build 
  5.     PWD := $(shell pwd) 
  6. default
  7.     $(MAKE) -C $(KERNELDIR) M=$(PWD) modules 
  8. clean: 
  9.     $(MAKE) -C $(KERNEL_PATH) M=$(PWD) clean 
  10. endif 

編譯、測試

  1. $ make 
  2. $ sudo insmod my_driver_interrupt_wq.ko irq=1 devname=mydev 

檢查驅(qū)動模塊是否加載成功:

  1. $ lsmod | grep my_driver_interrupt_wq 
  2. my_driver_interrupt_wq    16384  0 

再看一下 dmesg 的輸出信息:

  1. $ dmesg 
  2. ... 
  3. [  188.247636] myirq_init is called.  
  4. [  188.247642] register irq[1] handler success. 

說明:驅(qū)動程序的初始化函數(shù) myirq_init 被調(diào)用了,并且成功注冊了 1 號中斷的處理程序。

此時,按一下鍵盤上的 ESC 鍵。

操作系統(tǒng)在捕獲到鍵盤中斷之后,會依次調(diào)用此中斷的所有中斷處理程序,其中就包括我們注冊的 myirq_handler 函數(shù)。

在這個函數(shù)中,當判斷出是ESC按鍵時,就初始化一個工作項(把結(jié)構(gòu)體 work_struct 類型的變量與一個處理函數(shù)綁定起來),然后丟給操作系統(tǒng)預(yù)先創(chuàng)建好的工作隊列(system_wq)去處理,如下所示:

  1. if (key_code == 0x01) 
  2.     printk("ESC key is pressed! \n"); 
  3.     INIT_WORK(&mywork, mywork_handler); 
  4.     schedule_work(&mywork); 

因此,當相應(yīng)的內(nèi)核線程從這個工作隊列(system_wq)中取出工作項(mywork)來處理的時候,函數(shù) mywork_handler 就會被調(diào)用。

現(xiàn)在來看一下 dmesg 的輸出信息:

  1. [  305.053155] ESC key is pressed!  
  2. [  305.053177] mywork_handler is called. 

可以看到:mywork_handler函數(shù)被正確調(diào)用了。

完美!

本文轉(zhuǎn)載自微信公眾號「IOT物聯(lián)網(wǎng)小鎮(zhèn)」

【編輯推薦】

 

責任編輯:姜華 來源: IOT物聯(lián)網(wǎng)小鎮(zhèn)
相關(guān)推薦

2022-01-12 12:35:36

Linuxworkqueue工作隊列

2021-08-10 12:05:19

Linuxworkqueue內(nèi)核

2011-06-20 06:14:15

ibmdwLinux

2015-03-27 13:04:20

Beanstalkd工

2015-11-12 10:03:34

前端H5web

2021-12-20 07:51:16

Linux函數(shù)應(yīng)用層

2021-11-10 12:13:02

HostonlyCookie瀏覽器

2021-09-03 09:12:09

Linux中斷軟件

2020-09-27 06:53:57

MavenCDNwrapper

2017-04-03 15:35:13

知識體系架構(gòu)

2023-05-18 22:51:08

2020-11-04 13:01:38

FastThreadLocalJDK

2021-01-07 05:22:47

MySQL字段存儲

2021-08-10 11:30:30

Linux代碼中斷控制器

2021-08-03 15:10:26

Linux代碼驅(qū)動

2015-03-17 10:13:52

HTML5什么鬼

2019-10-30 10:13:15

區(qū)塊鏈技術(shù)支付寶

2021-07-06 10:17:07

Python LaunLinuxWindows

2019-04-28 10:30:30

Linux操作系統(tǒng)Namespace

2023-03-02 08:48:43

Linuxsubshell
點贊
收藏

51CTO技術(shù)棧公眾號