2 * Rated submission helpers
4 * Copyright (C) 2015 Jens Axboe <axboe@kernel.dk>
11 #include "workqueue.h"
12 #include "lib/getrusage.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 bool workqueue_enqueue(struct workqueue *wq, struct workqueue_work *work)
104 struct submit_worker *sw;
106 sw = get_submit_worker(wq);
108 pthread_mutex_lock(&sw->lock);
109 flist_add_tail(&work->list, &sw->work_list);
110 sw->seq = ++wq->work_seq;
111 sw->flags &= ~SW_F_IDLE;
112 pthread_mutex_unlock(&sw->lock);
114 pthread_cond_signal(&sw->cond);
121 static void handle_list(struct submit_worker *sw, struct flist_head *list)
123 struct workqueue *wq = sw->wq;
124 struct workqueue_work *work;
126 while (!flist_empty(list)) {
127 work = flist_first_entry(list, struct workqueue_work, list);
128 flist_del_init(&work->list);
129 wq->ops.fn(sw, work);
134 static void sum_val(uint64_t *dst, uint64_t *src)
137 __sync_fetch_and_add(dst, *src);
142 static void sum_val(uint64_t *dst, uint64_t *src)
151 static void pthread_double_unlock(pthread_mutex_t *lock1,
152 pthread_mutex_t *lock2)
155 pthread_mutex_unlock(lock1);
156 pthread_mutex_unlock(lock2);
160 static void pthread_double_lock(pthread_mutex_t *lock1, pthread_mutex_t *lock2)
164 pthread_mutex_lock(lock1);
165 pthread_mutex_lock(lock2);
167 pthread_mutex_lock(lock2);
168 pthread_mutex_lock(lock1);
173 static void sum_ddir(struct thread_data *dst, struct thread_data *src,
176 pthread_double_lock(&dst->io_wq.stat_lock, &src->io_wq.stat_lock);
178 sum_val(&dst->io_bytes[ddir], &src->io_bytes[ddir]);
179 sum_val(&dst->io_blocks[ddir], &src->io_blocks[ddir]);
180 sum_val(&dst->this_io_blocks[ddir], &src->this_io_blocks[ddir]);
181 sum_val(&dst->this_io_bytes[ddir], &src->this_io_bytes[ddir]);
182 sum_val(&dst->bytes_done[ddir], &src->bytes_done[ddir]);
184 pthread_double_unlock(&dst->io_wq.stat_lock, &src->io_wq.stat_lock);
187 static void update_accounting(struct submit_worker *sw)
189 struct thread_data *src = sw->private;
190 struct thread_data *dst = sw->wq->td;
193 sum_ddir(dst, src, DDIR_READ);
195 sum_ddir(dst, src, DDIR_WRITE);
197 sum_ddir(dst, src, DDIR_TRIM);
200 static void *worker_thread(void *data)
202 struct submit_worker *sw = data;
203 struct workqueue *wq = sw->wq;
204 unsigned int eflags = 0, ret;
205 FLIST_HEAD(local_list);
207 ret = workqueue_init_worker(sw);
208 pthread_mutex_lock(&sw->lock);
209 sw->flags |= SW_F_RUNNING;
211 sw->flags |= SW_F_ERROR;
212 pthread_mutex_unlock(&sw->lock);
214 pthread_mutex_lock(&wq->flush_lock);
215 pthread_cond_signal(&wq->flush_cond);
216 pthread_mutex_unlock(&wq->flush_lock);
218 if (sw->flags & SW_F_ERROR)
222 pthread_mutex_lock(&sw->lock);
224 if (flist_empty(&sw->work_list)) {
225 if (sw->flags & SW_F_EXIT) {
226 pthread_mutex_unlock(&sw->lock);
230 if (workqueue_pre_sleep_check(sw)) {
231 pthread_mutex_unlock(&sw->lock);
232 workqueue_pre_sleep(sw);
233 pthread_mutex_lock(&sw->lock);
237 * We dropped and reaquired the lock, check
240 if (!flist_empty(&sw->work_list))
243 if (sw->flags & SW_F_EXIT) {
244 pthread_mutex_unlock(&sw->lock);
246 } else if (!(sw->flags & SW_F_IDLE)) {
247 sw->flags |= SW_F_IDLE;
248 wq->next_free_worker = sw->index;
250 pthread_cond_signal(&wq->flush_cond);
252 update_accounting(sw);
253 pthread_cond_wait(&sw->cond, &sw->lock);
256 flist_splice_init(&sw->work_list, &local_list);
258 pthread_mutex_unlock(&sw->lock);
259 handle_list(sw, &local_list);
262 update_accounting(sw);
265 pthread_mutex_lock(&sw->lock);
266 sw->flags |= (SW_F_EXITED | eflags);
267 pthread_mutex_unlock(&sw->lock);
271 static void free_worker(struct submit_worker *sw)
273 struct workqueue *wq = sw->wq;
275 workqueue_exit_worker(sw);
277 pthread_cond_destroy(&sw->cond);
278 pthread_mutex_destroy(&sw->lock);
280 if (wq->ops.free_worker_fn)
281 wq->ops.free_worker_fn(sw);
284 static void shutdown_worker(struct submit_worker *sw, unsigned int *sum_cnt)
286 struct thread_data *parent = sw->wq->td;
287 struct thread_data *td = sw->private;
289 pthread_join(sw->thread, NULL);
291 sum_thread_stats(&parent->ts, &td->ts, *sum_cnt == 1);
295 void workqueue_exit(struct workqueue *wq)
297 unsigned int shutdown, sum_cnt = 0;
298 struct submit_worker *sw;
301 for (i = 0; i < wq->max_workers; i++) {
302 sw = &wq->workers[i];
304 pthread_mutex_lock(&sw->lock);
305 sw->flags |= SW_F_EXIT;
306 pthread_cond_signal(&sw->cond);
307 pthread_mutex_unlock(&sw->lock);
312 for (i = 0; i < wq->max_workers; i++) {
313 sw = &wq->workers[i];
314 if (sw->flags & SW_F_ACCOUNTED)
316 pthread_mutex_lock(&sw->lock);
317 sw->flags |= SW_F_ACCOUNTED;
318 pthread_mutex_unlock(&sw->lock);
319 shutdown_worker(sw, &sum_cnt);
322 } while (shutdown && shutdown != wq->max_workers);
325 pthread_mutex_destroy(&wq->flush_lock);
326 pthread_cond_destroy(&wq->flush_cond);
327 pthread_mutex_destroy(&wq->stat_lock);
330 static int start_worker(struct workqueue *wq, unsigned int index)
332 struct submit_worker *sw = &wq->workers[index];
335 INIT_FLIST_HEAD(&sw->work_list);
336 pthread_cond_init(&sw->cond, NULL);
337 pthread_mutex_init(&sw->lock, NULL);
341 if (wq->ops.alloc_worker_fn) {
342 ret = wq->ops.alloc_worker_fn(sw);
347 ret = pthread_create(&sw->thread, NULL, worker_thread, sw);
349 pthread_mutex_lock(&sw->lock);
350 sw->flags = SW_F_IDLE;
351 pthread_mutex_unlock(&sw->lock);
359 int workqueue_init(struct thread_data *td, struct workqueue *wq,
360 struct workqueue_ops *ops, unsigned max_pending)
362 unsigned int running;
365 wq->max_workers = max_pending;
369 wq->next_free_worker = 0;
370 pthread_cond_init(&wq->flush_cond, NULL);
371 pthread_mutex_init(&wq->flush_lock, NULL);
372 pthread_mutex_init(&wq->stat_lock, NULL);
374 wq->workers = calloc(wq->max_workers, sizeof(struct submit_worker));
376 for (i = 0; i < wq->max_workers; i++)
377 if (start_worker(wq, i))
381 if (!wq->max_workers)
385 * Wait for them all to be started and initialized
389 struct submit_worker *sw;
392 pthread_mutex_lock(&wq->flush_lock);
393 for (i = 0; i < wq->max_workers; i++) {
394 sw = &wq->workers[i];
395 pthread_mutex_lock(&sw->lock);
396 if (sw->flags & SW_F_RUNNING)
398 if (sw->flags & SW_F_ERROR)
400 pthread_mutex_unlock(&sw->lock);
403 if (error || running == wq->max_workers) {
404 pthread_mutex_unlock(&wq->flush_lock);
408 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
409 pthread_mutex_unlock(&wq->flush_lock);
416 log_err("Can't create rate workqueue\n");
417 td_verror(td, ESRCH, "workqueue_init");