Linux 工作队列
1.work的关系总览:
1.workqueue_struct
struct workqueue_struct {
struct list_head pwqs; //所有pool workqueue 都添加到链表中
struct list_head list; //用于将工作队列添加到全局链表workqueues中
struct mutex mutex;
int work_color;
int flush_color;
atomic_t nr_pwqs_to_flush;
struct wq_flusher *first_flusher;
struct list_head flusher_queue;
struct list_head flusher_overflow;
struct list_head maydays; //rescue状态下的pool_workqueue添加到本链表中
struct worker *rescuer; //rescuer内核线程,用于处理内存紧张时创建工作线程失败的情况
int nr_drainers;
int saved_max_active;
struct workqueue_attrs *unbound_attrs;
struct pool_workqueue *dfl_pwq;
#ifdef CONFIG_SYSFS
struct wq_device *wq_dev;
#endif
#ifdef CONFIG_LOCKDEP
struct lockdep_map lockdep_map;
#endif
char name[WQ_NAME_LEN];
struct rcu_head rcu;
unsigned int flags ____cacheline_aligned;
struct pool_workqueue __percpu *cpu_pwqs; //Per-CPU都创建pool_workqueue
struct pool_workqueue __rcu *numa_pwq_tbl[]; //Per-Node创建pool_workqueue
};
2.worker struct
struct worker {
union {
struct list_head entry; //用于添加到worker_pool的空闲链表中
struct hlist_node hentry; //用于添加到worker_pool的忙碌列表中
};
struct work_struct *current_work; //当前正在处理的work
work_func_t current_func; //当前正在执行的work回调函数
struct pool_workqueue *current_pwq; //指向当前work所属的pool_workqueue
struct list_head scheduled; //所有被调度执行的work都将添加到该链表中
struct task_struct *task; //指向内核线程
struct worker_pool *pool; //该worker所属的worker_pool
struct list_head node; //添加到worker_pool->workers链表中
unsigned long last_active;
unsigned int flags;
int id;
char desc[WORKER_DESC_LEN];
struct workqueue_struct *rescue_wq;
work_func_t last_func;
};
3.work struct
struct work_struct {
atomic_long_t data;
struct list_head entry;
work_func_t func;
#ifdef CONFIG_LOCKDEP
struct lockdep_map lockdep_map;
#endif
};
4.worker_pool
struct worker_pool {
spinlock_t lock;
int cpu;
int node;
int id;
unsigned int flags;
unsigned long watchdog_ts;
struct list_head worklist;
int nr_workers;
int nr_idle;
struct list_head idle_list;
struct timer_list idle_timer;
struct timer_list mayday_timer;
DECLARE_HASHTABLE(busy_hash, BUSY_WORKER_HASH_ORDER);
struct worker *manager;
struct list_head workers;
struct completion *detach_completion;
struct ida worker_ida;
struct workqueue_attrs *attrs;
struct hlist_node hash_node;
int refcnt;
atomic_t nr_running ____cacheline_aligned_in_smp;
struct rcu_head rcu;
} ____cacheline_aligned_in_smp;
5.pool_workqueue
struct pool_workqueue {
struct worker_pool *pool;
struct workqueue_struct *wq;
int work_color;
int flush_color;
int refcnt;
int nr_in_flight[WORK_NR_COLORS];
int nr_active;
int max_active;
struct list_head delayed_works;
struct list_head pwqs_node;
struct list_head mayday_node;
struct work_struct unbound_release_work;
struct rcu_head rcu;
} __aligned(1 << WORK_STRUCT_FLAG_BITS);
2.work各个子系统工作流程
1.schedule work的工作流程
2.workqueue init early