2 * Rated submission helpers
4 * Copyright (C) 2015 Jens Axboe <axboe@kernel.dk>
12 #include "workqueue.h"
13 #include "lib/getrusage.h"
15 struct submit_worker {
19 struct flist_head work_list;
24 struct thread_data td;
29 SW_F_RUNNING = 1 << 1,
32 SW_F_ACCOUNTED = 1 << 4,
36 static struct submit_worker *__get_submit_worker(struct workqueue *wq,
39 struct submit_worker **best)
41 struct submit_worker *sw = NULL;
43 while (start <= end) {
44 sw = &wq->workers[start];
45 if (sw->flags & SW_F_IDLE)
47 if (!(*best) || sw->seq < (*best)->seq)
55 static struct submit_worker *get_submit_worker(struct workqueue *wq)
57 unsigned int next = wq->next_free_worker;
58 struct submit_worker *sw, *best = NULL;
60 assert(next < wq->max_workers);
62 sw = __get_submit_worker(wq, next, wq->max_workers - 1, &best);
64 sw = __get_submit_worker(wq, 0, next - 1, &best);
67 * No truly idle found, use best match
72 if (sw->index == wq->next_free_worker) {
73 if (sw->index + 1 < wq->max_workers)
74 wq->next_free_worker = sw->index + 1;
76 wq->next_free_worker = 0;
82 static bool all_sw_idle(struct workqueue *wq)
86 for (i = 0; i < wq->max_workers; i++) {
87 struct submit_worker *sw = &wq->workers[i];
89 if (!(sw->flags & SW_F_IDLE))
97 * Must be serialized wrt workqueue_enqueue() by caller
99 void workqueue_flush(struct workqueue *wq)
103 while (!all_sw_idle(wq)) {
104 pthread_mutex_lock(&wq->flush_lock);
105 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
106 pthread_mutex_unlock(&wq->flush_lock);
113 * Must be serialized by caller.
115 int workqueue_enqueue(struct workqueue *wq, struct io_u *io_u)
117 struct submit_worker *sw;
119 sw = get_submit_worker(wq);
121 const enum fio_ddir ddir = acct_ddir(io_u);
122 struct thread_data *parent = wq->td;
125 parent->io_issues[ddir]++;
126 parent->io_issue_bytes[ddir] += io_u->xfer_buflen;
127 parent->rate_io_issue_bytes[ddir] += io_u->xfer_buflen;
130 pthread_mutex_lock(&sw->lock);
131 flist_add_tail(&io_u->verify_list, &sw->work_list);
132 sw->seq = ++wq->work_seq;
133 sw->flags &= ~SW_F_IDLE;
134 pthread_mutex_unlock(&sw->lock);
136 pthread_cond_signal(&sw->cond);
143 static void handle_list(struct submit_worker *sw, struct flist_head *list)
145 struct workqueue *wq = sw->wq;
148 while (!flist_empty(list)) {
149 io_u = flist_first_entry(list, struct io_u, verify_list);
150 flist_del_init(&io_u->verify_list);
151 wq->fn(&sw->td, io_u);
155 static int init_submit_worker(struct submit_worker *sw)
157 struct thread_data *parent = sw->wq->td;
158 struct thread_data *td = &sw->td;
161 memcpy(&td->o, &parent->o, sizeof(td->o));
162 memcpy(&td->ts, &parent->ts, sizeof(td->ts));
163 td->o.uid = td->o.gid = -1U;
164 dup_files(td, parent);
166 fio_options_mem_dupe(td);
168 if (ioengine_load(td))
172 td->io_ops->flags |= FIO_RAWIO;
176 INIT_FLIST_HEAD(&td->io_log_list);
177 INIT_FLIST_HEAD(&td->io_hist_list);
178 INIT_FLIST_HEAD(&td->verify_list);
179 INIT_FLIST_HEAD(&td->trim_list);
180 INIT_FLIST_HEAD(&td->next_rand_list);
181 td->io_hist_tree = RB_ROOT;
187 fio_gettime(&td->epoch, NULL);
188 fio_getrusage(&td->ru_start);
189 clear_io_state(td, 1);
191 td_set_runstate(td, TD_RUNNING);
192 td->flags |= TD_F_CHILD;
203 static void sum_val(uint64_t *dst, uint64_t *src)
206 __sync_fetch_and_add(dst, *src);
211 static void sum_val(uint64_t *dst, uint64_t *src)
220 static void pthread_double_unlock(pthread_mutex_t *lock1,
221 pthread_mutex_t *lock2)
224 pthread_mutex_unlock(lock1);
225 pthread_mutex_unlock(lock2);
229 static void pthread_double_lock(pthread_mutex_t *lock1, pthread_mutex_t *lock2)
233 pthread_mutex_lock(lock1);
234 pthread_mutex_lock(lock2);
236 pthread_mutex_lock(lock2);
237 pthread_mutex_lock(lock1);
242 static void sum_ddir(struct thread_data *dst, struct thread_data *src,
245 pthread_double_lock(&dst->io_wq.stat_lock, &src->io_wq.stat_lock);
247 sum_val(&dst->io_bytes[ddir], &src->io_bytes[ddir]);
248 sum_val(&dst->io_blocks[ddir], &src->io_blocks[ddir]);
249 sum_val(&dst->this_io_blocks[ddir], &src->this_io_blocks[ddir]);
250 sum_val(&dst->this_io_bytes[ddir], &src->this_io_bytes[ddir]);
251 sum_val(&dst->bytes_done[ddir], &src->bytes_done[ddir]);
253 pthread_double_unlock(&dst->io_wq.stat_lock, &src->io_wq.stat_lock);
256 static void update_accounting(struct submit_worker *sw)
258 struct thread_data *src = &sw->td;
259 struct thread_data *dst = sw->wq->td;
262 sum_ddir(dst, src, DDIR_READ);
264 sum_ddir(dst, src, DDIR_WRITE);
266 sum_ddir(dst, src, DDIR_TRIM);
269 static void *worker_thread(void *data)
271 struct submit_worker *sw = data;
272 struct workqueue *wq = sw->wq;
273 struct thread_data *td = &sw->td;
274 unsigned int eflags = 0, ret;
275 FLIST_HEAD(local_list);
277 ret = init_submit_worker(sw);
278 pthread_mutex_lock(&sw->lock);
279 sw->flags |= SW_F_RUNNING;
281 sw->flags |= SW_F_ERROR;
282 pthread_mutex_unlock(&sw->lock);
284 pthread_mutex_lock(&wq->flush_lock);
285 pthread_cond_signal(&wq->flush_cond);
286 pthread_mutex_unlock(&wq->flush_lock);
288 if (sw->flags & SW_F_ERROR)
292 pthread_mutex_lock(&sw->lock);
294 if (flist_empty(&sw->work_list)) {
295 if (sw->flags & SW_F_EXIT) {
296 pthread_mutex_unlock(&sw->lock);
300 if (td->io_u_queued || td->cur_depth ||
301 td->io_u_in_flight) {
304 pthread_mutex_unlock(&sw->lock);
305 ret = io_u_quiesce(td);
307 td->cur_depth -= ret;
308 pthread_mutex_lock(&sw->lock);
312 * We dropped and reaquired the lock, check
315 if (!flist_empty(&sw->work_list))
318 if (sw->flags & SW_F_EXIT) {
319 pthread_mutex_unlock(&sw->lock);
321 } else if (!(sw->flags & SW_F_IDLE)) {
322 sw->flags |= SW_F_IDLE;
323 wq->next_free_worker = sw->index;
325 pthread_cond_signal(&wq->flush_cond);
327 update_accounting(sw);
328 pthread_cond_wait(&sw->cond, &sw->lock);
331 flist_splice_init(&sw->work_list, &local_list);
333 pthread_mutex_unlock(&sw->lock);
334 handle_list(sw, &local_list);
337 update_accounting(sw);
340 pthread_mutex_lock(&sw->lock);
341 sw->flags |= (SW_F_EXITED | eflags);
342 pthread_mutex_unlock(&sw->lock);
346 static void free_worker(struct submit_worker *sw)
348 struct thread_data *td = &sw->td;
350 fio_options_free(td);
351 close_and_free_files(td);
354 td_set_runstate(td, TD_EXITED);
356 pthread_cond_destroy(&sw->cond);
357 pthread_mutex_destroy(&sw->lock);
360 static void shutdown_worker(struct submit_worker *sw, unsigned int *sum_cnt)
362 struct thread_data *parent = sw->wq->td;
364 pthread_join(sw->thread, NULL);
366 sum_thread_stats(&parent->ts, &sw->td.ts, *sum_cnt == 1);
370 void workqueue_exit(struct workqueue *wq)
372 unsigned int shutdown, sum_cnt = 0;
373 struct submit_worker *sw;
376 for (i = 0; i < wq->max_workers; i++) {
377 sw = &wq->workers[i];
379 pthread_mutex_lock(&sw->lock);
380 sw->flags |= SW_F_EXIT;
381 pthread_cond_signal(&sw->cond);
382 pthread_mutex_unlock(&sw->lock);
387 for (i = 0; i < wq->max_workers; i++) {
388 sw = &wq->workers[i];
389 if (sw->flags & SW_F_ACCOUNTED)
391 pthread_mutex_lock(&sw->lock);
392 sw->flags |= SW_F_ACCOUNTED;
393 pthread_mutex_unlock(&sw->lock);
394 shutdown_worker(sw, &sum_cnt);
397 } while (shutdown && shutdown != wq->max_workers);
400 pthread_mutex_destroy(&wq->flush_lock);
401 pthread_cond_destroy(&wq->flush_cond);
402 pthread_mutex_destroy(&wq->stat_lock);
405 static int start_worker(struct workqueue *wq, unsigned int index)
407 struct submit_worker *sw = &wq->workers[index];
410 INIT_FLIST_HEAD(&sw->work_list);
411 pthread_cond_init(&sw->cond, NULL);
412 pthread_mutex_init(&sw->lock, NULL);
416 ret = pthread_create(&sw->thread, NULL, worker_thread, sw);
418 pthread_mutex_lock(&sw->lock);
419 sw->flags = SW_F_IDLE;
420 pthread_mutex_unlock(&sw->lock);
428 int workqueue_init(struct thread_data *td, struct workqueue *wq,
429 workqueue_fn *fn, unsigned max_pending)
431 unsigned int running;
434 wq->max_workers = max_pending;
438 wq->next_free_worker = 0;
439 pthread_cond_init(&wq->flush_cond, NULL);
440 pthread_mutex_init(&wq->flush_lock, NULL);
441 pthread_mutex_init(&wq->stat_lock, NULL);
443 wq->workers = calloc(wq->max_workers, sizeof(struct submit_worker));
445 for (i = 0; i < wq->max_workers; i++)
446 if (start_worker(wq, i))
450 if (!wq->max_workers)
454 * Wait for them all to be started and initialized
458 struct submit_worker *sw;
461 pthread_mutex_lock(&wq->flush_lock);
462 for (i = 0; i < wq->max_workers; i++) {
463 sw = &wq->workers[i];
464 pthread_mutex_lock(&sw->lock);
465 if (sw->flags & SW_F_RUNNING)
467 if (sw->flags & SW_F_ERROR)
469 pthread_mutex_unlock(&sw->lock);
472 if (error || running == wq->max_workers) {
473 pthread_mutex_unlock(&wq->flush_lock);
477 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
478 pthread_mutex_unlock(&wq->flush_lock);
485 log_err("Can't create rate workqueue\n");
486 td_verror(td, ESRCH, "workqueue_init");