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