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