io_uring: fix pointer cast warning on 32-bit
[fio.git] / rate-submit.c
CommitLineData
51575029
JA
1/*
2 * Rated submission helpers
3 *
4 * Copyright (C) 2015 Jens Axboe <axboe@kernel.dk>
5 *
6 */
40511301 7#include "fio.h"
618ee94c 8#include "ioengines.h"
40511301 9#include "lib/getrusage.h"
24660963 10#include "rate-submit.h"
40511301 11
c06379a6
VF
12static void check_overlap(struct io_u *io_u)
13{
14 int i;
15 struct thread_data *td;
16 bool overlap = false;
17
18 do {
19 /*
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
23 * each other
817ae977
VF
24 *
25 * If an overlap is found, release the lock and
26 * re-acquire it before checking again to give other
27 * threads a chance to make progress
28 *
29 * If an overlap is not found, release the lock when the
30 * io_u's IO_U_F_FLIGHT flag is set so that this io_u
31 * can be checked by other threads as they assess overlap
c06379a6
VF
32 */
33 pthread_mutex_lock(&overlap_check);
34 for_each_td(td, i) {
35 if (td->runstate <= TD_SETTING_UP ||
36 td->runstate >= TD_FINISHING ||
37 !td->o.serialize_overlap ||
38 td->o.io_submit_mode != IO_MODE_OFFLOAD)
39 continue;
40
41 overlap = in_flight_overlap(&td->io_u_all, io_u);
42 if (overlap) {
43 pthread_mutex_unlock(&overlap_check);
44 break;
45 }
46 }
47 } while (overlap);
48}
49
155f2f02
JA
50static int io_workqueue_fn(struct submit_worker *sw,
51 struct workqueue_work *work)
40511301
JA
52{
53 struct io_u *io_u = container_of(work, struct io_u, work);
54 const enum fio_ddir ddir = io_u->ddir;
b86ad8f1 55 struct thread_data *td = sw->priv;
d28174f0 56 int ret, error;
40511301 57
c06379a6
VF
58 if (td->o.serialize_overlap)
59 check_overlap(io_u);
60
40511301
JA
61 dprint(FD_RATE, "io_u %p queued by %u\n", io_u, gettid());
62
1651e431 63 io_u_set(td, io_u, IO_U_F_NO_FILE_PUT);
40511301
JA
64
65 td->cur_depth++;
66
67 do {
68 ret = td_io_queue(td, io_u);
69 if (ret != FIO_Q_BUSY)
70 break;
71 ret = io_u_queued_complete(td, 1);
72 if (ret > 0)
73 td->cur_depth -= ret;
d28174f0
JA
74 else if (ret < 0)
75 break;
1651e431 76 io_u_clear(td, io_u, IO_U_F_FLIGHT);
40511301
JA
77 } while (1);
78
79 dprint(FD_RATE, "io_u %p ret %d by %u\n", io_u, ret, gettid());
80
d28174f0 81 error = io_queue_event(td, io_u, &ret, ddir, NULL, 0, NULL);
40511301
JA
82
83 if (ret == FIO_Q_COMPLETED)
84 td->cur_depth--;
85 else if (ret == FIO_Q_QUEUED) {
86 unsigned int min_evts;
87
88 if (td->o.iodepth == 1)
89 min_evts = 1;
90 else
91 min_evts = 0;
92
93 ret = io_u_queued_complete(td, min_evts);
94 if (ret > 0)
95 td->cur_depth -= ret;
40511301 96 }
155f2f02 97
d28174f0
JA
98 if (error || td->error)
99 pthread_cond_signal(&td->parent->free_cond);
100
155f2f02 101 return 0;
40511301
JA
102}
103
104static bool io_workqueue_pre_sleep_flush_fn(struct submit_worker *sw)
105{
b86ad8f1 106 struct thread_data *td = sw->priv;
40511301 107
d28174f0
JA
108 if (td->error)
109 return false;
40511301
JA
110 if (td->io_u_queued || td->cur_depth || td->io_u_in_flight)
111 return true;
112
113 return false;
114}
115
116static void io_workqueue_pre_sleep_fn(struct submit_worker *sw)
117{
b86ad8f1 118 struct thread_data *td = sw->priv;
40511301
JA
119 int ret;
120
121 ret = io_u_quiesce(td);
122 if (ret > 0)
123 td->cur_depth -= ret;
124}
125
126static int io_workqueue_alloc_fn(struct submit_worker *sw)
127{
128 struct thread_data *td;
129
130 td = calloc(1, sizeof(*td));
b86ad8f1 131 sw->priv = td;
40511301
JA
132 return 0;
133}
134
135static void io_workqueue_free_fn(struct submit_worker *sw)
136{
b86ad8f1
CB
137 free(sw->priv);
138 sw->priv = NULL;
40511301
JA
139}
140
141static int io_workqueue_init_worker_fn(struct submit_worker *sw)
142{
143 struct thread_data *parent = sw->wq->td;
b86ad8f1 144 struct thread_data *td = sw->priv;
40511301
JA
145
146 memcpy(&td->o, &parent->o, sizeof(td->o));
147 memcpy(&td->ts, &parent->ts, sizeof(td->ts));
148 td->o.uid = td->o.gid = -1U;
149 dup_files(td, parent);
150 td->eo = parent->eo;
151 fio_options_mem_dupe(td);
152
153 if (ioengine_load(td))
154 goto err;
155
40511301
JA
156 td->pid = gettid();
157
158 INIT_FLIST_HEAD(&td->io_log_list);
159 INIT_FLIST_HEAD(&td->io_hist_list);
160 INIT_FLIST_HEAD(&td->verify_list);
161 INIT_FLIST_HEAD(&td->trim_list);
40511301
JA
162 td->io_hist_tree = RB_ROOT;
163
164 td->o.iodepth = 1;
165 if (td_io_init(td))
166 goto err_io_init;
167
3aea75b1 168 set_epoch_time(td, td->o.log_unix_epoch);
40511301
JA
169 fio_getrusage(&td->ru_start);
170 clear_io_state(td, 1);
171
172 td_set_runstate(td, TD_RUNNING);
b7aae4ba 173 td->flags |= TD_F_CHILD | TD_F_NEED_LOCK;
40511301
JA
174 td->parent = parent;
175 return 0;
176
177err_io_init:
178 close_ioengine(td);
179err:
180 return 1;
181
182}
183
184static void io_workqueue_exit_worker_fn(struct submit_worker *sw,
185 unsigned int *sum_cnt)
186{
b86ad8f1 187 struct thread_data *td = sw->priv;
40511301
JA
188
189 (*sum_cnt)++;
190 sum_thread_stats(&sw->wq->td->ts, &td->ts, *sum_cnt == 1);
191
192 fio_options_free(td);
193 close_and_free_files(td);
194 if (td->io_ops)
195 close_ioengine(td);
196 td_set_runstate(td, TD_EXITED);
197}
198
199#ifdef CONFIG_SFAA
200static void sum_val(uint64_t *dst, uint64_t *src)
201{
202 if (*src) {
203 __sync_fetch_and_add(dst, *src);
204 *src = 0;
205 }
206}
207#else
208static void sum_val(uint64_t *dst, uint64_t *src)
209{
210 if (*src) {
211 *dst += *src;
212 *src = 0;
213 }
214}
215#endif
216
217static void pthread_double_unlock(pthread_mutex_t *lock1,
218 pthread_mutex_t *lock2)
219{
220#ifndef CONFIG_SFAA
221 pthread_mutex_unlock(lock1);
222 pthread_mutex_unlock(lock2);
223#endif
224}
225
226static void pthread_double_lock(pthread_mutex_t *lock1, pthread_mutex_t *lock2)
227{
228#ifndef CONFIG_SFAA
229 if (lock1 < lock2) {
230 pthread_mutex_lock(lock1);
231 pthread_mutex_lock(lock2);
232 } else {
233 pthread_mutex_lock(lock2);
234 pthread_mutex_lock(lock1);
235 }
236#endif
237}
238
239static void sum_ddir(struct thread_data *dst, struct thread_data *src,
240 enum fio_ddir ddir)
241{
242 pthread_double_lock(&dst->io_wq.stat_lock, &src->io_wq.stat_lock);
243
244 sum_val(&dst->io_bytes[ddir], &src->io_bytes[ddir]);
245 sum_val(&dst->io_blocks[ddir], &src->io_blocks[ddir]);
246 sum_val(&dst->this_io_blocks[ddir], &src->this_io_blocks[ddir]);
247 sum_val(&dst->this_io_bytes[ddir], &src->this_io_bytes[ddir]);
248 sum_val(&dst->bytes_done[ddir], &src->bytes_done[ddir]);
249
250 pthread_double_unlock(&dst->io_wq.stat_lock, &src->io_wq.stat_lock);
251}
252
253static void io_workqueue_update_acct_fn(struct submit_worker *sw)
254{
b86ad8f1 255 struct thread_data *src = sw->priv;
40511301
JA
256 struct thread_data *dst = sw->wq->td;
257
258 if (td_read(src))
259 sum_ddir(dst, src, DDIR_READ);
260 if (td_write(src))
261 sum_ddir(dst, src, DDIR_WRITE);
262 if (td_trim(src))
263 sum_ddir(dst, src, DDIR_TRIM);
264
265}
266
103b174e 267static struct workqueue_ops rated_wq_ops = {
40511301
JA
268 .fn = io_workqueue_fn,
269 .pre_sleep_flush_fn = io_workqueue_pre_sleep_flush_fn,
270 .pre_sleep_fn = io_workqueue_pre_sleep_fn,
271 .update_acct_fn = io_workqueue_update_acct_fn,
272 .alloc_worker_fn = io_workqueue_alloc_fn,
273 .free_worker_fn = io_workqueue_free_fn,
274 .init_worker_fn = io_workqueue_init_worker_fn,
275 .exit_worker_fn = io_workqueue_exit_worker_fn,
276};
103b174e 277
24660963 278int rate_submit_init(struct thread_data *td, struct sk_out *sk_out)
103b174e
JA
279{
280 if (td->o.io_submit_mode != IO_MODE_OFFLOAD)
281 return 0;
282
24660963 283 return workqueue_init(td, &td->io_wq, &rated_wq_ops, td->o.iodepth, sk_out);
103b174e
JA
284}
285
286void rate_submit_exit(struct thread_data *td)
287{
288 if (td->o.io_submit_mode != IO_MODE_OFFLOAD)
289 return;
290
291 workqueue_exit(&td->io_wq);
292}