2 * Rated submission helpers
4 * Copyright (C) 2015 Jens Axboe <axboe@kernel.dk>
9 #include "lib/getrusage.h"
10 #include "rate-submit.h"
12 static void check_overlap(struct io_u *io_u)
15 struct thread_data *td;
20 * Allow only one thread to check for overlap at a
21 * time to prevent two threads from thinking the coast
22 * is clear and then submitting IOs that overlap with
25 pthread_mutex_lock(&overlap_check);
27 if (td->runstate <= TD_SETTING_UP ||
28 td->runstate >= TD_FINISHING ||
29 !td->o.serialize_overlap ||
30 td->o.io_submit_mode != IO_MODE_OFFLOAD)
33 overlap = in_flight_overlap(&td->io_u_all, io_u);
35 pthread_mutex_unlock(&overlap_check);
42 static int io_workqueue_fn(struct submit_worker *sw,
43 struct workqueue_work *work)
45 struct io_u *io_u = container_of(work, struct io_u, work);
46 const enum fio_ddir ddir = io_u->ddir;
47 struct thread_data *td = sw->priv;
50 if (td->o.serialize_overlap)
53 dprint(FD_RATE, "io_u %p queued by %u\n", io_u, gettid());
55 io_u_set(td, io_u, IO_U_F_NO_FILE_PUT);
60 ret = td_io_queue(td, io_u);
61 if (ret != FIO_Q_BUSY)
63 ret = io_u_queued_complete(td, 1);
66 io_u_clear(td, io_u, IO_U_F_FLIGHT);
69 dprint(FD_RATE, "io_u %p ret %d by %u\n", io_u, ret, gettid());
71 io_queue_event(td, io_u, &ret, ddir, NULL, 0, NULL);
73 if (ret == FIO_Q_COMPLETED)
75 else if (ret == FIO_Q_QUEUED) {
76 unsigned int min_evts;
78 if (td->o.iodepth == 1)
83 ret = io_u_queued_complete(td, min_evts);
91 static bool io_workqueue_pre_sleep_flush_fn(struct submit_worker *sw)
93 struct thread_data *td = sw->priv;
95 if (td->io_u_queued || td->cur_depth || td->io_u_in_flight)
101 static void io_workqueue_pre_sleep_fn(struct submit_worker *sw)
103 struct thread_data *td = sw->priv;
106 ret = io_u_quiesce(td);
108 td->cur_depth -= ret;
111 static int io_workqueue_alloc_fn(struct submit_worker *sw)
113 struct thread_data *td;
115 td = calloc(1, sizeof(*td));
120 static void io_workqueue_free_fn(struct submit_worker *sw)
126 static int io_workqueue_init_worker_fn(struct submit_worker *sw)
128 struct thread_data *parent = sw->wq->td;
129 struct thread_data *td = sw->priv;
131 memcpy(&td->o, &parent->o, sizeof(td->o));
132 memcpy(&td->ts, &parent->ts, sizeof(td->ts));
133 td->o.uid = td->o.gid = -1U;
134 dup_files(td, parent);
136 fio_options_mem_dupe(td);
138 if (ioengine_load(td))
143 INIT_FLIST_HEAD(&td->io_log_list);
144 INIT_FLIST_HEAD(&td->io_hist_list);
145 INIT_FLIST_HEAD(&td->verify_list);
146 INIT_FLIST_HEAD(&td->trim_list);
147 td->io_hist_tree = RB_ROOT;
153 set_epoch_time(td, td->o.log_unix_epoch);
154 fio_getrusage(&td->ru_start);
155 clear_io_state(td, 1);
157 td_set_runstate(td, TD_RUNNING);
158 td->flags |= TD_F_CHILD | TD_F_NEED_LOCK;
169 static void io_workqueue_exit_worker_fn(struct submit_worker *sw,
170 unsigned int *sum_cnt)
172 struct thread_data *td = sw->priv;
175 sum_thread_stats(&sw->wq->td->ts, &td->ts, *sum_cnt == 1);
177 fio_options_free(td);
178 close_and_free_files(td);
181 td_set_runstate(td, TD_EXITED);
185 static void sum_val(uint64_t *dst, uint64_t *src)
188 __sync_fetch_and_add(dst, *src);
193 static void sum_val(uint64_t *dst, uint64_t *src)
202 static void pthread_double_unlock(pthread_mutex_t *lock1,
203 pthread_mutex_t *lock2)
206 pthread_mutex_unlock(lock1);
207 pthread_mutex_unlock(lock2);
211 static void pthread_double_lock(pthread_mutex_t *lock1, pthread_mutex_t *lock2)
215 pthread_mutex_lock(lock1);
216 pthread_mutex_lock(lock2);
218 pthread_mutex_lock(lock2);
219 pthread_mutex_lock(lock1);
224 static void sum_ddir(struct thread_data *dst, struct thread_data *src,
227 pthread_double_lock(&dst->io_wq.stat_lock, &src->io_wq.stat_lock);
229 sum_val(&dst->io_bytes[ddir], &src->io_bytes[ddir]);
230 sum_val(&dst->io_blocks[ddir], &src->io_blocks[ddir]);
231 sum_val(&dst->this_io_blocks[ddir], &src->this_io_blocks[ddir]);
232 sum_val(&dst->this_io_bytes[ddir], &src->this_io_bytes[ddir]);
233 sum_val(&dst->bytes_done[ddir], &src->bytes_done[ddir]);
235 pthread_double_unlock(&dst->io_wq.stat_lock, &src->io_wq.stat_lock);
238 static void io_workqueue_update_acct_fn(struct submit_worker *sw)
240 struct thread_data *src = sw->priv;
241 struct thread_data *dst = sw->wq->td;
244 sum_ddir(dst, src, DDIR_READ);
246 sum_ddir(dst, src, DDIR_WRITE);
248 sum_ddir(dst, src, DDIR_TRIM);
252 static struct workqueue_ops rated_wq_ops = {
253 .fn = io_workqueue_fn,
254 .pre_sleep_flush_fn = io_workqueue_pre_sleep_flush_fn,
255 .pre_sleep_fn = io_workqueue_pre_sleep_fn,
256 .update_acct_fn = io_workqueue_update_acct_fn,
257 .alloc_worker_fn = io_workqueue_alloc_fn,
258 .free_worker_fn = io_workqueue_free_fn,
259 .init_worker_fn = io_workqueue_init_worker_fn,
260 .exit_worker_fn = io_workqueue_exit_worker_fn,
263 int rate_submit_init(struct thread_data *td, struct sk_out *sk_out)
265 if (td->o.io_submit_mode != IO_MODE_OFFLOAD)
268 return workqueue_init(td, &td->io_wq, &rated_wq_ops, td->o.iodepth, sk_out);
271 void rate_submit_exit(struct thread_data *td)
273 if (td->o.io_submit_mode != IO_MODE_OFFLOAD)
276 workqueue_exit(&td->io_wq);