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

Linux 工作隊(duì)列 workqueue 是什么鬼?

系統(tǒng) Linux
這篇文章,我們就通過實(shí)際的代碼操作,來演示一下工作隊(duì)列(workqueue)的使用方式。

 

目錄

  • 工作隊(duì)列是什么
  •  驅(qū)動(dòng)程序
  •  編譯、測試

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

Linux中斷處理可用下圖總結(jié):

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

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

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

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

工作隊(duì)列是什么

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

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

工作隊(duì)列中有兩個(gè)重要的結(jié)構(gòu)體:工作隊(duì)列(workqueue_struct) 和 工作項(xiàng)(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)核中,工作隊(duì)列中的所有工作項(xiàng),是通過鏈表串在一起的,并且等待著操作系統(tǒng)中的某個(gè)線程挨個(gè)取出來處理。

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

這里就涉及到一個(gè)取舍的問題了。

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

原因有二:

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

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

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

例如下面這些工作隊(duì)列,就是內(nèi)核默認(rèn)創(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. extern struct workqueue_struct *system_wq;  
  30. extern struct workqueue_struct *system_highpri_wq;  
  31. extern struct workqueue_struct *system_long_wq;  
  32. extern struct workqueue_struct *system_unbound_wq;  
  33. extern struct workqueue_struct *system_freezable_wq;  
  34. extern struct workqueue_struct *system_power_efficient_wq;  
  35. extern struct workqueue_struct *system_freezable_power_efficient_wq; 

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

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

此外,由于工作隊(duì)列 system_wq 被使用的頻率很高,于是內(nèi)核就封裝了一個(gè)簡單的函數(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. static inline bool schedule_work(struct work_struct *work){     
  13.     return queue_work(system_wq, work);  

當(dāng)然了,任何事情有利就有弊!

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

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

根據(jù)先來后到的原則,工作隊(duì)列中后加入的工作項(xiàng),就可能因?yàn)榍懊婀ぷ黜?xiàng)的處理函數(shù)執(zhí)行的時(shí)間太長,從而導(dǎo)致時(shí)效性無法保證。

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

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

驅(qū)動(dòng)程序

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

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

創(chuàng)建驅(qū)動(dòng)程序源文件和 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ū)動(dòng)模塊之后,如果監(jiān)測到鍵盤上的ESC鍵被按下,那么就往內(nèi)核默認(rèn)的工作隊(duì)列system_wq中增加一個(gè)工作項(xiàng),然后觀察該工作項(xiàng)對應(yīng)的處理函數(shù)是否被調(diào)用。 

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

  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ū)動(dòng)程序的初始化函數(shù) myirq_init 被調(diào)用了,并且成功注冊了 1 號中斷的處理程序。

此時(shí),按一下鍵盤上的 ESC 鍵。

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

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

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

因此,當(dāng)相應(yīng)的內(nèi)核線程從這個(gè)工作隊(duì)列(system_wq)中取出工作項(xiàng)(mywork)來處理的時(shí)候,函數(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)用了。

完美! 

 

責(zé)任編輯:龐桂玉 來源: 良許Linux
相關(guān)推薦

2021-12-27 07:55:59

Linux 中斷處理Linux 系統(tǒng)

2015-03-27 13:04:20

Beanstalkd工

2015-11-12 10:03:34

前端H5web

2011-06-20 06:14:15

ibmdwLinux

2021-11-10 12:13:02

HostonlyCookie瀏覽器

2020-09-27 06:53:57

MavenCDNwrapper

2017-04-03 15:35:13

知識體系架構(gòu)

2015-03-17 10:13:52

HTML5什么鬼

2019-10-30 10:13:15

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

2021-07-06 10:17:07

Python LaunLinuxWindows

2020-11-04 13:01:38

FastThreadLocalJDK

2015-09-29 09:47:14

2019-01-07 12:40:19

2015-09-22 09:25:16

RTORPO災(zāi)備技術(shù)

2021-01-07 05:22:47

MySQL字段存儲

2022-09-07 08:41:57

SpringIstio分布式

2015-07-16 10:49:31

虛擬化容器技術(shù)

2019-01-17 14:35:01

2018-01-16 08:47:23

2021-08-10 12:05:19

Linuxworkqueue內(nèi)核
點(diǎn)贊
收藏

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