2 * Generic workqueue offload mechanism
4 * Copyright (C) 2015 Jens Axboe <axboe@kernel.dk>
11 #include "workqueue.h"
16 SW_F_RUNNING = 1 << 1,
19 SW_F_ACCOUNTED = 1 << 4,
23 static struct submit_worker *__get_submit_worker(struct workqueue *wq,
26 struct submit_worker **best)
28 struct submit_worker *sw = NULL;
30 while (start <= end) {
31 sw = &wq->workers[start];
32 if (sw->flags & SW_F_IDLE)
34 if (!(*best) || sw->seq < (*best)->seq)
42 static struct submit_worker *get_submit_worker(struct workqueue *wq)
44 unsigned int next = wq->next_free_worker;
45 struct submit_worker *sw, *best = NULL;
47 assert(next < wq->max_workers);
49 sw = __get_submit_worker(wq, next, wq->max_workers - 1, &best);
51 sw = __get_submit_worker(wq, 0, next - 1, &best);
54 * No truly idle found, use best match
59 if (sw->index == wq->next_free_worker) {
60 if (sw->index + 1 < wq->max_workers)
61 wq->next_free_worker = sw->index + 1;
63 wq->next_free_worker = 0;
69 static bool all_sw_idle(struct workqueue *wq)
73 for (i = 0; i < wq->max_workers; i++) {
74 struct submit_worker *sw = &wq->workers[i];
76 if (!(sw->flags & SW_F_IDLE))
84 * Must be serialized wrt workqueue_enqueue() by caller
86 void workqueue_flush(struct workqueue *wq)
90 while (!all_sw_idle(wq)) {
91 pthread_mutex_lock(&wq->flush_lock);
92 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
93 pthread_mutex_unlock(&wq->flush_lock);
100 * Must be serialized by caller. Returns true for queued, false for busy.
102 void workqueue_enqueue(struct workqueue *wq, struct workqueue_work *work)
104 struct submit_worker *sw;
106 sw = get_submit_worker(wq);
109 pthread_mutex_lock(&sw->lock);
110 flist_add_tail(&work->list, &sw->work_list);
111 sw->seq = ++wq->work_seq;
112 sw->flags &= ~SW_F_IDLE;
113 pthread_mutex_unlock(&sw->lock);
115 pthread_cond_signal(&sw->cond);
118 static void handle_list(struct submit_worker *sw, struct flist_head *list)
120 struct workqueue *wq = sw->wq;
121 struct workqueue_work *work;
123 while (!flist_empty(list)) {
124 work = flist_first_entry(list, struct workqueue_work, list);
125 flist_del_init(&work->list);
126 wq->ops.fn(sw, work);
130 static void *worker_thread(void *data)
132 struct submit_worker *sw = data;
133 struct workqueue *wq = sw->wq;
134 unsigned int eflags = 0, ret = 0;
135 FLIST_HEAD(local_list);
137 sk_out_assign(sw->sk_out);
140 if (nice(wq->ops.nice) < 0) {
141 log_err("workqueue: nice %s\n", strerror(errno));
147 ret = workqueue_init_worker(sw);
149 pthread_mutex_lock(&sw->lock);
150 sw->flags |= SW_F_RUNNING;
152 sw->flags |= SW_F_ERROR;
153 pthread_mutex_unlock(&sw->lock);
155 pthread_mutex_lock(&wq->flush_lock);
156 pthread_cond_signal(&wq->flush_cond);
157 pthread_mutex_unlock(&wq->flush_lock);
159 if (sw->flags & SW_F_ERROR)
163 pthread_mutex_lock(&sw->lock);
165 if (flist_empty(&sw->work_list)) {
166 if (sw->flags & SW_F_EXIT) {
167 pthread_mutex_unlock(&sw->lock);
171 if (workqueue_pre_sleep_check(sw)) {
172 pthread_mutex_unlock(&sw->lock);
173 workqueue_pre_sleep(sw);
174 pthread_mutex_lock(&sw->lock);
178 * We dropped and reaquired the lock, check
181 if (!flist_empty(&sw->work_list))
184 if (sw->flags & SW_F_EXIT) {
185 pthread_mutex_unlock(&sw->lock);
187 } else if (!(sw->flags & SW_F_IDLE)) {
188 sw->flags |= SW_F_IDLE;
189 wq->next_free_worker = sw->index;
191 pthread_cond_signal(&wq->flush_cond);
193 if (wq->ops.update_acct_fn)
194 wq->ops.update_acct_fn(sw);
196 pthread_cond_wait(&sw->cond, &sw->lock);
199 flist_splice_init(&sw->work_list, &local_list);
201 pthread_mutex_unlock(&sw->lock);
202 handle_list(sw, &local_list);
205 if (wq->ops.update_acct_fn)
206 wq->ops.update_acct_fn(sw);
209 pthread_mutex_lock(&sw->lock);
210 sw->flags |= (SW_F_EXITED | eflags);
211 pthread_mutex_unlock(&sw->lock);
216 static void free_worker(struct submit_worker *sw, unsigned int *sum_cnt)
218 struct workqueue *wq = sw->wq;
220 workqueue_exit_worker(sw, sum_cnt);
222 pthread_cond_destroy(&sw->cond);
223 pthread_mutex_destroy(&sw->lock);
225 if (wq->ops.free_worker_fn)
226 wq->ops.free_worker_fn(sw);
229 static void shutdown_worker(struct submit_worker *sw, unsigned int *sum_cnt)
231 pthread_join(sw->thread, NULL);
232 free_worker(sw, sum_cnt);
235 void workqueue_exit(struct workqueue *wq)
237 unsigned int shutdown, sum_cnt = 0;
238 struct submit_worker *sw;
244 for (i = 0; i < wq->max_workers; i++) {
245 sw = &wq->workers[i];
247 pthread_mutex_lock(&sw->lock);
248 sw->flags |= SW_F_EXIT;
249 pthread_cond_signal(&sw->cond);
250 pthread_mutex_unlock(&sw->lock);
255 for (i = 0; i < wq->max_workers; i++) {
256 sw = &wq->workers[i];
257 if (sw->flags & SW_F_ACCOUNTED)
259 pthread_mutex_lock(&sw->lock);
260 sw->flags |= SW_F_ACCOUNTED;
261 pthread_mutex_unlock(&sw->lock);
262 shutdown_worker(sw, &sum_cnt);
265 } while (shutdown && shutdown != wq->max_workers);
269 pthread_mutex_destroy(&wq->flush_lock);
270 pthread_cond_destroy(&wq->flush_cond);
271 pthread_mutex_destroy(&wq->stat_lock);
274 static int start_worker(struct workqueue *wq, unsigned int index,
275 struct sk_out *sk_out)
277 struct submit_worker *sw = &wq->workers[index];
280 INIT_FLIST_HEAD(&sw->work_list);
282 ret = mutex_cond_init_pshared(&sw->lock, &sw->cond);
290 if (wq->ops.alloc_worker_fn) {
291 ret = wq->ops.alloc_worker_fn(sw);
296 ret = pthread_create(&sw->thread, NULL, worker_thread, sw);
298 pthread_mutex_lock(&sw->lock);
299 sw->flags = SW_F_IDLE;
300 pthread_mutex_unlock(&sw->lock);
304 free_worker(sw, NULL);
308 int workqueue_init(struct thread_data *td, struct workqueue *wq,
309 struct workqueue_ops *ops, unsigned int max_workers,
310 struct sk_out *sk_out)
312 unsigned int running;
316 wq->max_workers = max_workers;
320 wq->next_free_worker = 0;
322 ret = mutex_cond_init_pshared(&wq->flush_lock, &wq->flush_cond);
325 ret = mutex_init_pshared(&wq->stat_lock);
329 wq->workers = smalloc(wq->max_workers * sizeof(struct submit_worker));
331 for (i = 0; i < wq->max_workers; i++)
332 if (start_worker(wq, i, sk_out))
336 if (!wq->max_workers)
340 * Wait for them all to be started and initialized
344 struct submit_worker *sw;
347 pthread_mutex_lock(&wq->flush_lock);
348 for (i = 0; i < wq->max_workers; i++) {
349 sw = &wq->workers[i];
350 pthread_mutex_lock(&sw->lock);
351 if (sw->flags & SW_F_RUNNING)
353 if (sw->flags & SW_F_ERROR)
355 pthread_mutex_unlock(&sw->lock);
358 if (error || running == wq->max_workers) {
359 pthread_mutex_unlock(&wq->flush_lock);
363 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
364 pthread_mutex_unlock(&wq->flush_lock);
371 log_err("Can't create rate workqueue\n");
372 td_verror(td, ESRCH, "workqueue_init");