2 * Rated submission helpers
4 * Copyright (C) 2015 Jens Axboe <axboe@kernel.dk>
11 #include "workqueue.h"
12 #include "lib/getrusage.h"
14 struct submit_worker {
18 struct flist_head work_list;
23 struct thread_data td;
28 SW_F_RUNNING = 1 << 1,
31 SW_F_ACCOUNTED = 1 << 4,
35 static struct submit_worker *__get_submit_worker(struct workqueue *wq,
38 struct submit_worker **best)
40 struct submit_worker *sw = NULL;
42 while (start <= end) {
43 sw = &wq->workers[start];
44 if (sw->flags & SW_F_IDLE)
46 if (!(*best) || sw->seq < (*best)->seq)
54 static struct submit_worker *get_submit_worker(struct workqueue *wq)
56 unsigned int next = wq->next_free_worker;
57 struct submit_worker *sw, *best = NULL;
59 assert(next < wq->max_workers);
61 sw = __get_submit_worker(wq, next, wq->max_workers - 1, &best);
63 sw = __get_submit_worker(wq, 0, next - 1, &best);
66 * No truly idle found, use best match
71 if (sw->index == wq->next_free_worker) {
72 if (sw->index + 1 < wq->max_workers)
73 wq->next_free_worker = sw->index + 1;
75 wq->next_free_worker = 0;
81 static bool all_sw_idle(struct workqueue *wq)
85 for (i = 0; i < wq->max_workers; i++) {
86 struct submit_worker *sw = &wq->workers[i];
88 if (!(sw->flags & SW_F_IDLE))
96 * Must be serialized wrt workqueue_enqueue() by caller
98 void workqueue_flush(struct workqueue *wq)
102 while (!all_sw_idle(wq)) {
103 pthread_mutex_lock(&wq->flush_lock);
104 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
105 pthread_mutex_unlock(&wq->flush_lock);
112 * Must be serialized by caller. Returns true for queued, false for busy.
114 bool workqueue_enqueue(struct workqueue *wq, struct workqueue_work *work)
116 struct submit_worker *sw;
118 sw = get_submit_worker(wq);
120 pthread_mutex_lock(&sw->lock);
121 flist_add_tail(&work->list, &sw->work_list);
122 sw->seq = ++wq->work_seq;
123 sw->flags &= ~SW_F_IDLE;
124 pthread_mutex_unlock(&sw->lock);
126 pthread_cond_signal(&sw->cond);
133 static void handle_list(struct submit_worker *sw, struct flist_head *list)
135 struct workqueue *wq = sw->wq;
136 struct workqueue_work *work;
138 while (!flist_empty(list)) {
139 work = flist_first_entry(list, struct workqueue_work, list);
140 flist_del_init(&work->list);
141 wq->fn(&sw->td, work);
145 static int init_submit_worker(struct submit_worker *sw)
147 struct thread_data *parent = sw->wq->td;
148 struct thread_data *td = &sw->td;
151 memcpy(&td->o, &parent->o, sizeof(td->o));
152 memcpy(&td->ts, &parent->ts, sizeof(td->ts));
153 td->o.uid = td->o.gid = -1U;
154 dup_files(td, parent);
156 fio_options_mem_dupe(td);
158 if (ioengine_load(td))
162 td->io_ops->flags |= FIO_RAWIO;
166 INIT_FLIST_HEAD(&td->io_log_list);
167 INIT_FLIST_HEAD(&td->io_hist_list);
168 INIT_FLIST_HEAD(&td->verify_list);
169 INIT_FLIST_HEAD(&td->trim_list);
170 INIT_FLIST_HEAD(&td->next_rand_list);
171 td->io_hist_tree = RB_ROOT;
177 fio_gettime(&td->epoch, NULL);
178 fio_getrusage(&td->ru_start);
179 clear_io_state(td, 1);
181 td_set_runstate(td, TD_RUNNING);
182 td->flags |= TD_F_CHILD;
193 static void sum_val(uint64_t *dst, uint64_t *src)
196 __sync_fetch_and_add(dst, *src);
201 static void sum_val(uint64_t *dst, uint64_t *src)
210 static void pthread_double_unlock(pthread_mutex_t *lock1,
211 pthread_mutex_t *lock2)
214 pthread_mutex_unlock(lock1);
215 pthread_mutex_unlock(lock2);
219 static void pthread_double_lock(pthread_mutex_t *lock1, pthread_mutex_t *lock2)
223 pthread_mutex_lock(lock1);
224 pthread_mutex_lock(lock2);
226 pthread_mutex_lock(lock2);
227 pthread_mutex_lock(lock1);
232 static void sum_ddir(struct thread_data *dst, struct thread_data *src,
235 pthread_double_lock(&dst->io_wq.stat_lock, &src->io_wq.stat_lock);
237 sum_val(&dst->io_bytes[ddir], &src->io_bytes[ddir]);
238 sum_val(&dst->io_blocks[ddir], &src->io_blocks[ddir]);
239 sum_val(&dst->this_io_blocks[ddir], &src->this_io_blocks[ddir]);
240 sum_val(&dst->this_io_bytes[ddir], &src->this_io_bytes[ddir]);
241 sum_val(&dst->bytes_done[ddir], &src->bytes_done[ddir]);
243 pthread_double_unlock(&dst->io_wq.stat_lock, &src->io_wq.stat_lock);
246 static void update_accounting(struct submit_worker *sw)
248 struct thread_data *src = &sw->td;
249 struct thread_data *dst = sw->wq->td;
252 sum_ddir(dst, src, DDIR_READ);
254 sum_ddir(dst, src, DDIR_WRITE);
256 sum_ddir(dst, src, DDIR_TRIM);
259 static void *worker_thread(void *data)
261 struct submit_worker *sw = data;
262 struct workqueue *wq = sw->wq;
263 struct thread_data *td = &sw->td;
264 unsigned int eflags = 0, ret;
265 FLIST_HEAD(local_list);
267 ret = init_submit_worker(sw);
268 pthread_mutex_lock(&sw->lock);
269 sw->flags |= SW_F_RUNNING;
271 sw->flags |= SW_F_ERROR;
272 pthread_mutex_unlock(&sw->lock);
274 pthread_mutex_lock(&wq->flush_lock);
275 pthread_cond_signal(&wq->flush_cond);
276 pthread_mutex_unlock(&wq->flush_lock);
278 if (sw->flags & SW_F_ERROR)
282 pthread_mutex_lock(&sw->lock);
284 if (flist_empty(&sw->work_list)) {
285 if (sw->flags & SW_F_EXIT) {
286 pthread_mutex_unlock(&sw->lock);
290 if (td->io_u_queued || td->cur_depth ||
291 td->io_u_in_flight) {
294 pthread_mutex_unlock(&sw->lock);
295 ret = io_u_quiesce(td);
297 td->cur_depth -= ret;
298 pthread_mutex_lock(&sw->lock);
302 * We dropped and reaquired the lock, check
305 if (!flist_empty(&sw->work_list))
308 if (sw->flags & SW_F_EXIT) {
309 pthread_mutex_unlock(&sw->lock);
311 } else if (!(sw->flags & SW_F_IDLE)) {
312 sw->flags |= SW_F_IDLE;
313 wq->next_free_worker = sw->index;
315 pthread_cond_signal(&wq->flush_cond);
317 update_accounting(sw);
318 pthread_cond_wait(&sw->cond, &sw->lock);
321 flist_splice_init(&sw->work_list, &local_list);
323 pthread_mutex_unlock(&sw->lock);
324 handle_list(sw, &local_list);
327 update_accounting(sw);
330 pthread_mutex_lock(&sw->lock);
331 sw->flags |= (SW_F_EXITED | eflags);
332 pthread_mutex_unlock(&sw->lock);
336 static void free_worker(struct submit_worker *sw)
338 struct thread_data *td = &sw->td;
340 fio_options_free(td);
341 close_and_free_files(td);
344 td_set_runstate(td, TD_EXITED);
346 pthread_cond_destroy(&sw->cond);
347 pthread_mutex_destroy(&sw->lock);
350 static void shutdown_worker(struct submit_worker *sw, unsigned int *sum_cnt)
352 struct thread_data *parent = sw->wq->td;
354 pthread_join(sw->thread, NULL);
356 sum_thread_stats(&parent->ts, &sw->td.ts, *sum_cnt == 1);
360 void workqueue_exit(struct workqueue *wq)
362 unsigned int shutdown, sum_cnt = 0;
363 struct submit_worker *sw;
366 for (i = 0; i < wq->max_workers; i++) {
367 sw = &wq->workers[i];
369 pthread_mutex_lock(&sw->lock);
370 sw->flags |= SW_F_EXIT;
371 pthread_cond_signal(&sw->cond);
372 pthread_mutex_unlock(&sw->lock);
377 for (i = 0; i < wq->max_workers; i++) {
378 sw = &wq->workers[i];
379 if (sw->flags & SW_F_ACCOUNTED)
381 pthread_mutex_lock(&sw->lock);
382 sw->flags |= SW_F_ACCOUNTED;
383 pthread_mutex_unlock(&sw->lock);
384 shutdown_worker(sw, &sum_cnt);
387 } while (shutdown && shutdown != wq->max_workers);
390 pthread_mutex_destroy(&wq->flush_lock);
391 pthread_cond_destroy(&wq->flush_cond);
392 pthread_mutex_destroy(&wq->stat_lock);
395 static int start_worker(struct workqueue *wq, unsigned int index)
397 struct submit_worker *sw = &wq->workers[index];
400 INIT_FLIST_HEAD(&sw->work_list);
401 pthread_cond_init(&sw->cond, NULL);
402 pthread_mutex_init(&sw->lock, NULL);
406 ret = pthread_create(&sw->thread, NULL, worker_thread, sw);
408 pthread_mutex_lock(&sw->lock);
409 sw->flags = SW_F_IDLE;
410 pthread_mutex_unlock(&sw->lock);
418 int workqueue_init(struct thread_data *td, struct workqueue *wq,
419 workqueue_fn *fn, unsigned max_pending)
421 unsigned int running;
424 wq->max_workers = max_pending;
428 wq->next_free_worker = 0;
429 pthread_cond_init(&wq->flush_cond, NULL);
430 pthread_mutex_init(&wq->flush_lock, NULL);
431 pthread_mutex_init(&wq->stat_lock, NULL);
433 wq->workers = calloc(wq->max_workers, sizeof(struct submit_worker));
435 for (i = 0; i < wq->max_workers; i++)
436 if (start_worker(wq, i))
440 if (!wq->max_workers)
444 * Wait for them all to be started and initialized
448 struct submit_worker *sw;
451 pthread_mutex_lock(&wq->flush_lock);
452 for (i = 0; i < wq->max_workers; i++) {
453 sw = &wq->workers[i];
454 pthread_mutex_lock(&sw->lock);
455 if (sw->flags & SW_F_RUNNING)
457 if (sw->flags & SW_F_ERROR)
459 pthread_mutex_unlock(&sw->lock);
462 if (error || running == wq->max_workers) {
463 pthread_mutex_unlock(&wq->flush_lock);
467 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
468 pthread_mutex_unlock(&wq->flush_lock);
475 log_err("Can't create rate workqueue\n");
476 td_verror(td, ESRCH, "workqueue_init");