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);
133 static int init_submit_worker(struct submit_worker *sw)
135 struct thread_data *parent = sw->wq->td;
136 struct thread_data *td = sw->private;
139 memcpy(&td->o, &parent->o, sizeof(td->o));
140 memcpy(&td->ts, &parent->ts, sizeof(td->ts));
141 td->o.uid = td->o.gid = -1U;
142 dup_files(td, parent);
144 fio_options_mem_dupe(td);
146 if (ioengine_load(td))
150 td->io_ops->flags |= FIO_RAWIO;
154 INIT_FLIST_HEAD(&td->io_log_list);
155 INIT_FLIST_HEAD(&td->io_hist_list);
156 INIT_FLIST_HEAD(&td->verify_list);
157 INIT_FLIST_HEAD(&td->trim_list);
158 INIT_FLIST_HEAD(&td->next_rand_list);
159 td->io_hist_tree = RB_ROOT;
165 fio_gettime(&td->epoch, NULL);
166 fio_getrusage(&td->ru_start);
167 clear_io_state(td, 1);
169 td_set_runstate(td, TD_RUNNING);
170 td->flags |= TD_F_CHILD;
181 static void sum_val(uint64_t *dst, uint64_t *src)
184 __sync_fetch_and_add(dst, *src);
189 static void sum_val(uint64_t *dst, uint64_t *src)
198 static void pthread_double_unlock(pthread_mutex_t *lock1,
199 pthread_mutex_t *lock2)
202 pthread_mutex_unlock(lock1);
203 pthread_mutex_unlock(lock2);
207 static void pthread_double_lock(pthread_mutex_t *lock1, pthread_mutex_t *lock2)
211 pthread_mutex_lock(lock1);
212 pthread_mutex_lock(lock2);
214 pthread_mutex_lock(lock2);
215 pthread_mutex_lock(lock1);
220 static void sum_ddir(struct thread_data *dst, struct thread_data *src,
223 pthread_double_lock(&dst->io_wq.stat_lock, &src->io_wq.stat_lock);
225 sum_val(&dst->io_bytes[ddir], &src->io_bytes[ddir]);
226 sum_val(&dst->io_blocks[ddir], &src->io_blocks[ddir]);
227 sum_val(&dst->this_io_blocks[ddir], &src->this_io_blocks[ddir]);
228 sum_val(&dst->this_io_bytes[ddir], &src->this_io_bytes[ddir]);
229 sum_val(&dst->bytes_done[ddir], &src->bytes_done[ddir]);
231 pthread_double_unlock(&dst->io_wq.stat_lock, &src->io_wq.stat_lock);
234 static void update_accounting(struct submit_worker *sw)
236 struct thread_data *src = sw->private;
237 struct thread_data *dst = sw->wq->td;
240 sum_ddir(dst, src, DDIR_READ);
242 sum_ddir(dst, src, DDIR_WRITE);
244 sum_ddir(dst, src, DDIR_TRIM);
247 static void *worker_thread(void *data)
249 struct submit_worker *sw = data;
250 struct workqueue *wq = sw->wq;
251 unsigned int eflags = 0, ret;
252 FLIST_HEAD(local_list);
254 ret = init_submit_worker(sw);
255 pthread_mutex_lock(&sw->lock);
256 sw->flags |= SW_F_RUNNING;
258 sw->flags |= SW_F_ERROR;
259 pthread_mutex_unlock(&sw->lock);
261 pthread_mutex_lock(&wq->flush_lock);
262 pthread_cond_signal(&wq->flush_cond);
263 pthread_mutex_unlock(&wq->flush_lock);
265 if (sw->flags & SW_F_ERROR)
269 pthread_mutex_lock(&sw->lock);
271 if (flist_empty(&sw->work_list)) {
272 if (sw->flags & SW_F_EXIT) {
273 pthread_mutex_unlock(&sw->lock);
277 if (workqueue_pre_sleep_check(sw)) {
278 pthread_mutex_unlock(&sw->lock);
279 workqueue_pre_sleep(sw);
280 pthread_mutex_lock(&sw->lock);
284 * We dropped and reaquired the lock, check
287 if (!flist_empty(&sw->work_list))
290 if (sw->flags & SW_F_EXIT) {
291 pthread_mutex_unlock(&sw->lock);
293 } else if (!(sw->flags & SW_F_IDLE)) {
294 sw->flags |= SW_F_IDLE;
295 wq->next_free_worker = sw->index;
297 pthread_cond_signal(&wq->flush_cond);
299 update_accounting(sw);
300 pthread_cond_wait(&sw->cond, &sw->lock);
303 flist_splice_init(&sw->work_list, &local_list);
305 pthread_mutex_unlock(&sw->lock);
306 handle_list(sw, &local_list);
309 update_accounting(sw);
312 pthread_mutex_lock(&sw->lock);
313 sw->flags |= (SW_F_EXITED | eflags);
314 pthread_mutex_unlock(&sw->lock);
318 static void free_worker(struct submit_worker *sw)
320 struct thread_data *td = sw->private;
321 struct workqueue *wq = sw->wq;
323 fio_options_free(td);
324 close_and_free_files(td);
327 td_set_runstate(td, TD_EXITED);
329 pthread_cond_destroy(&sw->cond);
330 pthread_mutex_destroy(&sw->lock);
332 if (wq->ops.free_worker_fn)
333 wq->ops.free_worker_fn(sw);
336 static void shutdown_worker(struct submit_worker *sw, unsigned int *sum_cnt)
338 struct thread_data *parent = sw->wq->td;
339 struct thread_data *td = sw->private;
341 pthread_join(sw->thread, NULL);
343 sum_thread_stats(&parent->ts, &td->ts, *sum_cnt == 1);
347 void workqueue_exit(struct workqueue *wq)
349 unsigned int shutdown, sum_cnt = 0;
350 struct submit_worker *sw;
353 for (i = 0; i < wq->max_workers; i++) {
354 sw = &wq->workers[i];
356 pthread_mutex_lock(&sw->lock);
357 sw->flags |= SW_F_EXIT;
358 pthread_cond_signal(&sw->cond);
359 pthread_mutex_unlock(&sw->lock);
364 for (i = 0; i < wq->max_workers; i++) {
365 sw = &wq->workers[i];
366 if (sw->flags & SW_F_ACCOUNTED)
368 pthread_mutex_lock(&sw->lock);
369 sw->flags |= SW_F_ACCOUNTED;
370 pthread_mutex_unlock(&sw->lock);
371 shutdown_worker(sw, &sum_cnt);
374 } while (shutdown && shutdown != wq->max_workers);
377 pthread_mutex_destroy(&wq->flush_lock);
378 pthread_cond_destroy(&wq->flush_cond);
379 pthread_mutex_destroy(&wq->stat_lock);
382 static int start_worker(struct workqueue *wq, unsigned int index)
384 struct submit_worker *sw = &wq->workers[index];
387 INIT_FLIST_HEAD(&sw->work_list);
388 pthread_cond_init(&sw->cond, NULL);
389 pthread_mutex_init(&sw->lock, NULL);
393 if (wq->ops.alloc_worker_fn) {
394 ret = wq->ops.alloc_worker_fn(sw);
399 ret = pthread_create(&sw->thread, NULL, worker_thread, sw);
401 pthread_mutex_lock(&sw->lock);
402 sw->flags = SW_F_IDLE;
403 pthread_mutex_unlock(&sw->lock);
411 int workqueue_init(struct thread_data *td, struct workqueue *wq,
412 struct workqueue_ops *ops, unsigned max_pending)
414 unsigned int running;
417 wq->max_workers = max_pending;
421 wq->next_free_worker = 0;
422 pthread_cond_init(&wq->flush_cond, NULL);
423 pthread_mutex_init(&wq->flush_lock, NULL);
424 pthread_mutex_init(&wq->stat_lock, NULL);
426 wq->workers = calloc(wq->max_workers, sizeof(struct submit_worker));
428 for (i = 0; i < wq->max_workers; i++)
429 if (start_worker(wq, i))
433 if (!wq->max_workers)
437 * Wait for them all to be started and initialized
441 struct submit_worker *sw;
444 pthread_mutex_lock(&wq->flush_lock);
445 for (i = 0; i < wq->max_workers; i++) {
446 sw = &wq->workers[i];
447 pthread_mutex_lock(&sw->lock);
448 if (sw->flags & SW_F_RUNNING)
450 if (sw->flags & SW_F_ERROR)
452 pthread_mutex_unlock(&sw->lock);
455 if (error || running == wq->max_workers) {
456 pthread_mutex_unlock(&wq->flush_lock);
460 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
461 pthread_mutex_unlock(&wq->flush_lock);
468 log_err("Can't create rate workqueue\n");
469 td_verror(td, ESRCH, "workqueue_init");