【原创】Linux中断子系统(四)-Workqueue
背景
Read the fucking source code! --By 鲁迅
A picture is worth a thousand words. --By 高尔基
说明:
Kernel版本:4.14
ARM64处理器,Contex-A53,双核
使用工具:Source Insight 3.5, Visio
1. 概述
Workqueue工作队列是利用内核线程来异步执行工作任务的通用机制;
Workqueue工作队列可以用作中断处理的Bottom-half机制,利用进程上下文来执行中断处理中耗时的任务,因此它允许睡眠,而Softirq和Tasklet在处理任务时不能睡眠;
来一张概述图:
先看看关键的数据结构:
work_struct:工作队列调度的最小单位,work item;
workqueue_struct:工作队列,work item都挂入到工作队列中;
worker:work item的处理者,每个worker对应一个内核线程;
worker_pool:worker池(内核线程池),是一个共享资源池,提供不同的worker来对work item进行处理;
pool_workqueue:充当桥梁纽带的作用,用于连接workqueue和worker_pool,建立链接关系;
下边看看细节吧:
2.2 work
struct work_struct用来描述work,初始化一个work并添加到工作队列后,将会将其传递到合适的内核线程来进行处理,它是用于调度的最小单位。
关键字段描述如下:
struct work_struct {
atomic_long_t data; //低比特存放状态位,高比特存放worker_pool的ID或者pool_workqueue的指针
struct list_head entry; //用于添加到其他队列上
work_func_t func; //工作任务的处理函数,在内核线程中回调
#ifdef CONFIG_LOCKDEP
struct lockdep_map lockdep_map;
#endif
};
图片说明下data字段:
2.4 worker
每个worker对应一个内核线程,用于对work item的处理;
worker根据工作状态,可以添加到worker_pool的空闲链表或忙碌列表中;
worker处于空闲状态时并接收到工作处理请求,将唤醒内核线程来处理;
内核线程是在每个worker_pool中由一个初始的空闲工作线程创建的,并根据需要动态创建和销毁;
关键字段描述如下:
struct worker {
/* on idle list while idle, on busy hash table while busy */
union {
struct list_headentry;/* L: while idle */ //用于添加到worker_pool的空闲链表中
struct hlist_nodehentry;/* L: while busy */ //用于添加到worker_pool的忙碌列表中
};
struct work_struct*current_work;/* L: work being processed */ //当前正在处理的work
work_func_tcurrent_func;/* L: current_work"s fn */ //当前正在执行的work回调函数
struct pool_workqueue*current_pwq; /* L: current_work"s pwq */ //指向当前work所属的pool_workqueue
struct list_headscheduled;/* L: scheduled works */ //所有被调度执行的work都将添加到该链表中
/* 64 bytes boundary on 64bit, 32 on 32bit */
struct task_struct*task;/* I: worker task */ //指向内核线程
struct worker_pool*pool;/* I: the associated pool */ //该worker所属的worker_pool
/* L: for rescuers */
struct list_headnode;/* A: anchored at pool->workers */ //添加到worker_pool->workers链表中
/* A: runs through worker->node */
...
};
2.5 worker_pool
worker_pool是一个资源池,管理多个worker,也就是管理多个内核线程;
针对绑定类型的工作队列,worker_pool是Per-CPU创建,每个CPU都有两个worker_pool,对应不同的优先级,nice值分别为0和-20;
针对非绑定类型的工作队列,worker_pool创建后会添加到unbound_pool_hash哈希表中;
worker_pool管理一个空闲链表和一个忙碌列表,其中忙碌列表由哈希管理;
关键字段描述如下:
struct worker_pool {
spinlock_tlock;/* the pool lock */
intcpu;/* I: the associated cpu */ //绑定到CPU的workqueue,代表CPU ID
intnode;/* I: the associated node ID */ //非绑定类型的workqueue,代表内存Node ID
intid;/* I: pool ID */
unsigned intflags;/* X: flags */
unsigned longwatchdog_ts;/* L: watchdog timestamp */
struct list_headworklist;/* L: list of pending works */ //pending状态的work添加到本链表
intnr_workers;/* L: total number of workers */ //worker的数量
/* nr_idle includes the ones off idle_list for rebinding */
intnr_idle;/* L: currently idle ones */
struct list_headidle_list;/* X: list of idle workers */ //处于IDLE状态的worker添加到本链表
struct timer_listidle_timer;/* L: worker idle timeout */
struct timer_listmayday_timer;/* L: SOS timer for workers */
/* a workers is either on busy_hash or idle_list, or the manager */
DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER); //工作状态的worker添加到本哈希表中
/* L: hash of busy workers */
/* see manage_workers() for details on the two manager mutexes */
struct worker*manager;/* L: purely informational */
struct mutexattach_mutex;/* attach/detach exclusion */
struct list_headworkers;/* A: attached workers */ //worker_pool管理的worker添加到本链表中
struct completion*detach_completion; /* all workers detached */
struct idaworker_ida;/* worker IDs for task name */
struct workqueue_attrs*attrs;/* I: worker attributes */
struct hlist_nodehash_node;/* PL: unbound_pool_hash node */ //用于添加到unbound_pool_hash中
...
} ____cacheline_aligned_in_smp;
2.6 pool_workqueue
pool_workqueue充当纽带的作用,用于将workqueue和worker_pool关联起来;
关键字段描述如下:
struct pool_workqueue {
struct worker_pool*pool;/* I: the associated pool */ //指向worker_pool
struct workqueue_struct *wq;/* I: the owning workqueue */ //指向所属的workqueue
intnr_active;/* L: nr of active works */ //活跃的work数量
intmax_active;/* L: max active works */ //活跃的最大work数量
struct list_headdelayed_works;/* L: delayed works */ //延迟执行的work挂入本链表
struct list_headpwqs_node;/* WR: node on wq->pwqs */ //用于添加到workqueue链表中
struct list_headmayday_node;/* MD: node on wq->maydays */ //用于添加到workqueue链表中
...
} __aligned(1 << WORK_STRUCT_FLAG_BITS);
2.7 小结
再来张图,首尾呼应一下:
workqueue子系统早期初始化函数完成的主要工作包括:
创建pool_workqueue的SLAB缓存,用于动态分配struct pool_workqueue结构;
为每个CPU都分配两个worker_pool,其中的nice值分别为0和HIGHPRI_NICE_LEVEL,并且为每个worker_pool从worker_pool_idr中分配一个ID号;
为unbound工作队列创建默认属性,struct workqueue_attrs属性,主要描述内核线程的nice值,以及cpumask值,分别针对优先级以及允许在哪些CPU上执行;
为系统默认创建几个工作队列,这几个工作队列的描述在上文的数据结构部分提及过,不再赘述;
从图中可以看出创建工作队列的接口为:alloc_workqueue,如下图:
主要完成的工作是给之前创建好的worker_pool,添加一个初始的worker;
create_worker函数中,创建的内核线程名字为kworker/XX:YY或者kworker/uXX:YY,其中XX表示worker_pool的编号,YY表示worker的编号,u表示unbound;
workqueue子系统初始化完成后,基本就已经将数据结构的关联建立好了,当有work来进行调度的时候,就可以进行处理了。
3.2 work调度
3.2.1 schedule_work
以schedule_work接口为例进行分析:
在创建worker时,创建内核线程,执行函数为worker_thread;
worker_thread在开始执行时,设置标志位PF_WQ_WORKER,调度器在进行调度处理时会对task进行判断,针对workerqueue worker有特殊处理;
worker对应的内核线程,在没有处理work的时候是睡眠状态,当被唤醒的时候,跳转到woke_up开始执行;
woke_up之后,如果此时worker是需要销毁的,那就进行清理工作并返回。否则,离开IDLE状态,并进入recheck模块执行;
recheck部分,首先判断是否需要更多的worker来处理,如果没有任务处理,跳转到sleep地方进行睡眠。有任务需要处理时,会判断是否有空闲内核线程以及是否需要动态创建,再清除掉worker的标志位,然后遍历工作链表,对链表中的每个节点调用process_one_worker来处理;
sleep部分比较好理解,没有任务处理时,worker进入空闲状态,并将当前的内核线程设置成睡眠状态,让出CPU;
总结:
管理worker_pool的内核线程池时,如果有PENDING状态的work,并且发现没有正在运行的工作线程(worker_pool->nr_running == 0),唤醒空闲状态的内核线程,或者动态创建内核线程;
如果work已经在同一个worker_pool的其他worker中执行,不再对该work进行处理;
work的执行函数为process_one_worker:
worker_pool通过nr_running字段来在不同的状态机之间进行切换;
worker_pool中有work需要处理时,需要至少保证有一个运行状态的worker,当nr_running大于1时,将多余的worker进入IDLE状态,没有work需要处理时,所有的worker都会进入IDLE状态;
执行work时,如果回调函数阻塞运行,那么会让worker进入睡眠状态,此时调度器会进行判断是否需要唤醒另一个worker;
IDLE状态的worker都存放在idle_list链表中,如果空闲时间超过了300秒,则会将其进行销毁;
Running->Suspend
睡眠状态可以通过wake_up_worker来进行唤醒处理,最终判断如果该worker不在运行状态,则增加worker_pool的nr_running值;
3.3.2 worker的动态添加和删除
动态删除
内核线程执行worker_thread函数时,如果没有空闲的worker,会调用manage_workers接口来创建更多的worker来处理工作;
参考
Documentation/core-api/workqueue.rst
http://kernel.meizu.com/linux-workqueue.html
洗洗睡了,收工!
欢迎关注公众号,不定期分享Linux内核机制文章
【原创】Linux中断子系统(四)-Workqueue相关教程