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 int 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;
129 pthread_mutex_lock(&sw->lock);
130 flist_add_tail(&io_u->verify_list, &sw->work_list);
131 sw->seq = ++wq->work_seq;
132 sw->flags &= ~SW_F_IDLE;
133 pthread_mutex_unlock(&sw->lock);
135 pthread_cond_signal(&sw->cond);
142 static void handle_list(struct submit_worker *sw, struct flist_head *list)
144 struct workqueue *wq = sw->wq;
147 while (!flist_empty(list)) {
148 io_u = flist_first_entry(list, struct io_u, verify_list);
149 flist_del_init(&io_u->verify_list);
150 wq->fn(&sw->td, io_u);
154 static int init_submit_worker(struct submit_worker *sw)
156 struct thread_data *parent = sw->wq->td;
157 struct thread_data *td = &sw->td;
160 memcpy(&td->o, &parent->o, sizeof(td->o));
161 memcpy(&td->ts, &parent->ts, sizeof(td->ts));
162 td->o.uid = td->o.gid = -1U;
163 dup_files(td, parent);
164 fio_options_mem_dupe(td);
166 if (ioengine_load(td))
170 td->io_ops->flags |= FIO_RAWIO;
174 INIT_FLIST_HEAD(&td->io_log_list);
175 INIT_FLIST_HEAD(&td->io_hist_list);
176 INIT_FLIST_HEAD(&td->verify_list);
177 INIT_FLIST_HEAD(&td->trim_list);
178 INIT_FLIST_HEAD(&td->next_rand_list);
179 td->io_hist_tree = RB_ROOT;
185 fio_gettime(&td->epoch, NULL);
186 fio_getrusage(&td->ru_start);
189 td_set_runstate(td, TD_RUNNING);
190 td->flags |= TD_F_CHILD;
200 static void sum_val(uint64_t *dst, uint64_t *src)
203 __sync_fetch_and_add(dst, *src);
208 static void sum_ddir(struct thread_data *dst, struct thread_data *src,
211 sum_val(&dst->io_bytes[ddir], &src->io_bytes[ddir]);
212 sum_val(&dst->io_blocks[ddir], &src->io_blocks[ddir]);
213 sum_val(&dst->this_io_blocks[ddir], &src->this_io_blocks[ddir]);
214 sum_val(&dst->this_io_bytes[ddir], &src->this_io_bytes[ddir]);
215 sum_val(&dst->bytes_done[ddir], &src->bytes_done[ddir]);
218 static void update_accounting(struct submit_worker *sw)
220 struct thread_data *src = &sw->td;
221 struct thread_data *dst = sw->wq->td;
224 sum_ddir(dst, src, DDIR_READ);
226 sum_ddir(dst, src, DDIR_WRITE);
228 sum_ddir(dst, src, DDIR_TRIM);
231 static void *worker_thread(void *data)
233 struct submit_worker *sw = data;
234 struct workqueue *wq = sw->wq;
235 struct thread_data *td = &sw->td;
236 unsigned int eflags = 0, ret;
237 FLIST_HEAD(local_list);
239 ret = init_submit_worker(sw);
240 pthread_mutex_lock(&sw->lock);
241 sw->flags |= SW_F_RUNNING;
243 sw->flags |= SW_F_ERROR;
244 pthread_mutex_unlock(&sw->lock);
246 pthread_mutex_lock(&wq->flush_lock);
247 pthread_cond_signal(&wq->flush_cond);
248 pthread_mutex_unlock(&wq->flush_lock);
250 if (sw->flags & SW_F_ERROR)
254 pthread_mutex_lock(&sw->lock);
256 if (flist_empty(&sw->work_list)) {
257 if (sw->flags & SW_F_EXIT) {
258 pthread_mutex_unlock(&sw->lock);
262 if (td->io_u_queued || td->cur_depth ||
263 td->io_u_in_flight) {
264 pthread_mutex_unlock(&sw->lock);
266 pthread_mutex_lock(&sw->lock);
270 * We dropped and reaquired the lock, check
273 if (!flist_empty(&sw->work_list))
276 if (sw->flags & SW_F_EXIT) {
277 pthread_mutex_unlock(&sw->lock);
279 } else if (!(sw->flags & SW_F_IDLE)) {
280 sw->flags |= SW_F_IDLE;
281 wq->next_free_worker = sw->index;
283 pthread_cond_signal(&wq->flush_cond);
285 update_accounting(sw);
286 pthread_cond_wait(&sw->cond, &sw->lock);
289 flist_splice_init(&sw->work_list, &local_list);
291 pthread_mutex_unlock(&sw->lock);
292 handle_list(sw, &local_list);
295 update_accounting(sw);
298 pthread_mutex_lock(&sw->lock);
299 sw->flags |= (SW_F_EXITED | eflags);
300 pthread_mutex_unlock(&sw->lock);
304 static void free_worker(struct submit_worker *sw)
306 struct thread_data *td = &sw->td;
308 fio_options_free(td);
309 close_and_free_files(td);
312 td_set_runstate(td, TD_EXITED);
314 pthread_cond_destroy(&sw->cond);
315 pthread_mutex_destroy(&sw->lock);
318 static void shutdown_worker(struct submit_worker *sw, unsigned int *sum_cnt)
320 struct thread_data *parent = sw->wq->td;
322 pthread_join(sw->thread, NULL);
324 sum_thread_stats(&parent->ts, &sw->td.ts, *sum_cnt);
328 void workqueue_exit(struct workqueue *wq)
330 unsigned int shutdown, sum_cnt = 0;
331 struct submit_worker *sw;
334 for (i = 0; i < wq->max_workers; i++) {
335 sw = &wq->workers[i];
337 pthread_mutex_lock(&sw->lock);
338 sw->flags |= SW_F_EXIT;
339 pthread_cond_signal(&sw->cond);
340 pthread_mutex_unlock(&sw->lock);
345 for (i = 0; i < wq->max_workers; i++) {
346 sw = &wq->workers[i];
347 if (sw->flags & SW_F_ACCOUNTED)
349 sw->flags |= SW_F_ACCOUNTED;
350 shutdown_worker(sw, &sum_cnt);
353 } while (shutdown && shutdown != wq->max_workers);
356 pthread_mutex_destroy(&wq->flush_lock);
357 pthread_cond_destroy(&wq->flush_cond);
360 static int start_worker(struct workqueue *wq, unsigned int index)
362 struct submit_worker *sw = &wq->workers[index];
365 INIT_FLIST_HEAD(&sw->work_list);
366 pthread_cond_init(&sw->cond, NULL);
367 pthread_mutex_init(&sw->lock, NULL);
371 ret = pthread_create(&sw->thread, NULL, worker_thread, sw);
373 pthread_mutex_lock(&sw->lock);
374 sw->flags = SW_F_IDLE;
375 pthread_mutex_unlock(&sw->lock);
383 int workqueue_init(struct thread_data *td, struct workqueue *wq,
384 workqueue_fn *fn, unsigned max_pending)
386 unsigned int running;
389 wq->max_workers = max_pending;
393 wq->next_free_worker = 0;
394 pthread_cond_init(&wq->flush_cond, NULL);
395 pthread_mutex_init(&wq->flush_lock, NULL);
397 wq->workers = calloc(wq->max_workers, sizeof(struct submit_worker));
399 for (i = 0; i < wq->max_workers; i++)
400 if (start_worker(wq, i))
404 if (!wq->max_workers)
408 * Wait for them all to be started and initialized
412 struct submit_worker *sw;
415 pthread_mutex_lock(&wq->flush_lock);
416 for (i = 0; i < wq->max_workers; i++) {
417 sw = &wq->workers[i];
418 pthread_mutex_lock(&sw->lock);
419 if (sw->flags & SW_F_RUNNING)
421 if (sw->flags & SW_F_ERROR)
423 pthread_mutex_unlock(&sw->lock);
426 if (error || running == wq->max_workers) {
427 pthread_mutex_unlock(&wq->flush_lock);
431 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
432 pthread_mutex_unlock(&wq->flush_lock);
439 log_err("Can't create rate workqueue\n");
440 td_verror(td, ESRCH, "workqueue_init");