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