workqueue: don't use ioengine return codes
[fio.git] / workqueue.c
1 /*
2  * Rated submission helpers
3  *
4  * Copyright (C) 2015 Jens Axboe <axboe@kernel.dk>
5  *
6  */
7 #include <unistd.h>
8
9 #include "fio.h"
10 #include "ioengine.h"
11 #include "flist.h"
12 #include "workqueue.h"
13 #include "lib/getrusage.h"
14
15 struct submit_worker {
16         pthread_t thread;
17         pthread_mutex_t lock;
18         pthread_cond_t cond;
19         struct flist_head work_list;
20         unsigned int flags;
21         unsigned int index;
22         uint64_t seq;
23         struct workqueue *wq;
24         struct thread_data td;
25 };
26
27 enum {
28         SW_F_IDLE       = 1 << 0,
29         SW_F_RUNNING    = 1 << 1,
30         SW_F_EXIT       = 1 << 2,
31         SW_F_EXITED     = 1 << 3,
32         SW_F_ACCOUNTED  = 1 << 4,
33         SW_F_ERROR      = 1 << 5,
34 };
35
36 static struct submit_worker *__get_submit_worker(struct workqueue *wq,
37                                                  unsigned int start,
38                                                  unsigned int end,
39                                                  struct submit_worker **best)
40 {
41         struct submit_worker *sw = NULL;
42
43         while (start <= end) {
44                 sw = &wq->workers[start];
45                 if (sw->flags & SW_F_IDLE)
46                         return sw;
47                 if (!(*best) || sw->seq < (*best)->seq)
48                         *best = sw;
49                 start++;
50         }
51
52         return NULL;
53 }
54
55 static struct submit_worker *get_submit_worker(struct workqueue *wq)
56 {
57         unsigned int next = wq->next_free_worker;
58         struct submit_worker *sw, *best = NULL;
59
60         assert(next < wq->max_workers);
61
62         sw = __get_submit_worker(wq, next, wq->max_workers - 1, &best);
63         if (!sw && next)
64                 sw = __get_submit_worker(wq, 0, next - 1, &best);
65
66         /*
67          * No truly idle found, use best match
68          */
69         if (!sw)
70                 sw = best;
71
72         if (sw->index == wq->next_free_worker) {
73                 if (sw->index + 1 < wq->max_workers)
74                         wq->next_free_worker = sw->index + 1;
75                 else
76                         wq->next_free_worker = 0;
77         }
78
79         return sw;
80 }
81
82 static bool all_sw_idle(struct workqueue *wq)
83 {
84         int i;
85
86         for (i = 0; i < wq->max_workers; i++) {
87                 struct submit_worker *sw = &wq->workers[i];
88
89                 if (!(sw->flags & SW_F_IDLE))
90                         return false;
91         }
92
93         return true;
94 }
95
96 /*
97  * Must be serialized wrt workqueue_enqueue() by caller
98  */
99 void workqueue_flush(struct workqueue *wq)
100 {
101         wq->wake_idle = 1;
102
103         while (!all_sw_idle(wq)) {
104                 pthread_mutex_lock(&wq->flush_lock);
105                 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
106                 pthread_mutex_unlock(&wq->flush_lock);
107         }
108
109         wq->wake_idle = 0;
110 }
111
112 /*
113  * Must be serialized by caller. Returns true for queued, false for busy.
114  */
115 bool workqueue_enqueue(struct workqueue *wq, struct io_u *io_u)
116 {
117         struct submit_worker *sw;
118
119         sw = get_submit_worker(wq);
120         if (sw) {
121                 pthread_mutex_lock(&sw->lock);
122                 flist_add_tail(&io_u->verify_list, &sw->work_list);
123                 sw->seq = ++wq->work_seq;
124                 sw->flags &= ~SW_F_IDLE;
125                 pthread_mutex_unlock(&sw->lock);
126
127                 pthread_cond_signal(&sw->cond);
128                 return true;
129         }
130
131         return false;
132 }
133
134 static void handle_list(struct submit_worker *sw, struct flist_head *list)
135 {
136         struct workqueue *wq = sw->wq;
137         struct io_u *io_u;
138
139         while (!flist_empty(list)) {
140                 io_u = flist_first_entry(list, struct io_u, verify_list);
141                 flist_del_init(&io_u->verify_list);
142                 wq->fn(&sw->td, io_u);
143         }
144 }
145
146 static int init_submit_worker(struct submit_worker *sw)
147 {
148         struct thread_data *parent = sw->wq->td;
149         struct thread_data *td = &sw->td;
150         int fio_unused ret;
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
159         if (ioengine_load(td))
160                 goto err;
161
162         if (td->o.odirect)
163                 td->io_ops->flags |= FIO_RAWIO;
164
165         td->pid = gettid();
166
167         INIT_FLIST_HEAD(&td->io_log_list);
168         INIT_FLIST_HEAD(&td->io_hist_list);
169         INIT_FLIST_HEAD(&td->verify_list);
170         INIT_FLIST_HEAD(&td->trim_list);
171         INIT_FLIST_HEAD(&td->next_rand_list);
172         td->io_hist_tree = RB_ROOT;
173
174         td->o.iodepth = 1;
175         if (td_io_init(td))
176                 goto err_io_init;
177
178         fio_gettime(&td->epoch, NULL);
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;
184         td->parent = parent;
185         return 0;
186
187 err_io_init:
188         close_ioengine(td);
189 err:
190         return 1;
191 }
192
193 #ifdef CONFIG_SFAA
194 static void sum_val(uint64_t *dst, uint64_t *src)
195 {
196         if (*src) {
197                 __sync_fetch_and_add(dst, *src);
198                 *src = 0;
199         }
200 }
201 #else
202 static void sum_val(uint64_t *dst, uint64_t *src)
203 {
204         if (*src) {
205                 *dst += *src;
206                 *src = 0;
207         }
208 }
209 #endif
210
211 static void pthread_double_unlock(pthread_mutex_t *lock1,
212                                   pthread_mutex_t *lock2)
213 {
214 #ifndef CONFIG_SFAA
215         pthread_mutex_unlock(lock1);
216         pthread_mutex_unlock(lock2);
217 #endif
218 }
219
220 static void pthread_double_lock(pthread_mutex_t *lock1, pthread_mutex_t *lock2)
221 {
222 #ifndef CONFIG_SFAA
223         if (lock1 < lock2) {
224                 pthread_mutex_lock(lock1);
225                 pthread_mutex_lock(lock2);
226         } else {
227                 pthread_mutex_lock(lock2);
228                 pthread_mutex_lock(lock1);
229         }
230 #endif
231 }
232
233 static void sum_ddir(struct thread_data *dst, struct thread_data *src,
234                      enum fio_ddir ddir)
235 {
236         pthread_double_lock(&dst->io_wq.stat_lock, &src->io_wq.stat_lock);
237
238         sum_val(&dst->io_bytes[ddir], &src->io_bytes[ddir]);
239         sum_val(&dst->io_blocks[ddir], &src->io_blocks[ddir]);
240         sum_val(&dst->this_io_blocks[ddir], &src->this_io_blocks[ddir]);
241         sum_val(&dst->this_io_bytes[ddir], &src->this_io_bytes[ddir]);
242         sum_val(&dst->bytes_done[ddir], &src->bytes_done[ddir]);
243
244         pthread_double_unlock(&dst->io_wq.stat_lock, &src->io_wq.stat_lock);
245 }
246
247 static void update_accounting(struct submit_worker *sw)
248 {
249         struct thread_data *src = &sw->td;
250         struct thread_data *dst = sw->wq->td;
251
252         if (td_read(src))
253                 sum_ddir(dst, src, DDIR_READ);
254         if (td_write(src))
255                 sum_ddir(dst, src, DDIR_WRITE);
256         if (td_trim(src))
257                 sum_ddir(dst, src, DDIR_TRIM);
258 }
259
260 static void *worker_thread(void *data)
261 {
262         struct submit_worker *sw = data;
263         struct workqueue *wq = sw->wq;
264         struct thread_data *td = &sw->td;
265         unsigned int eflags = 0, ret;
266         FLIST_HEAD(local_list);
267
268         ret = init_submit_worker(sw);
269         pthread_mutex_lock(&sw->lock);
270         sw->flags |= SW_F_RUNNING;
271         if (ret)
272                 sw->flags |= SW_F_ERROR;
273         pthread_mutex_unlock(&sw->lock);
274
275         pthread_mutex_lock(&wq->flush_lock);
276         pthread_cond_signal(&wq->flush_cond);
277         pthread_mutex_unlock(&wq->flush_lock);
278
279         if (sw->flags & SW_F_ERROR)
280                 goto done;
281
282         while (1) {
283                 pthread_mutex_lock(&sw->lock);
284
285                 if (flist_empty(&sw->work_list)) {
286                         if (sw->flags & SW_F_EXIT) {
287                                 pthread_mutex_unlock(&sw->lock);
288                                 break;
289                         }
290
291                         if (td->io_u_queued || td->cur_depth ||
292                             td->io_u_in_flight) {
293                                 int ret;
294
295                                 pthread_mutex_unlock(&sw->lock);
296                                 ret = io_u_quiesce(td);
297                                 if (ret > 0)
298                                         td->cur_depth -= ret;
299                                 pthread_mutex_lock(&sw->lock);
300                         }
301
302                         /*
303                          * We dropped and reaquired the lock, check
304                          * state again.
305                          */
306                         if (!flist_empty(&sw->work_list))
307                                 goto handle_work;
308
309                         if (sw->flags & SW_F_EXIT) {
310                                 pthread_mutex_unlock(&sw->lock);
311                                 break;
312                         } else if (!(sw->flags & SW_F_IDLE)) {
313                                 sw->flags |= SW_F_IDLE;
314                                 wq->next_free_worker = sw->index;
315                                 if (wq->wake_idle)
316                                         pthread_cond_signal(&wq->flush_cond);
317                         }
318                         update_accounting(sw);
319                         pthread_cond_wait(&sw->cond, &sw->lock);
320                 } else {
321 handle_work:
322                         flist_splice_init(&sw->work_list, &local_list);
323                 }
324                 pthread_mutex_unlock(&sw->lock);
325                 handle_list(sw, &local_list);
326         }
327
328         update_accounting(sw);
329
330 done:
331         pthread_mutex_lock(&sw->lock);
332         sw->flags |= (SW_F_EXITED | eflags);
333         pthread_mutex_unlock(&sw->lock);
334         return NULL;
335 }
336
337 static void free_worker(struct submit_worker *sw)
338 {
339         struct thread_data *td = &sw->td;
340
341         fio_options_free(td);
342         close_and_free_files(td);
343         if (td->io_ops)
344                 close_ioengine(td);
345         td_set_runstate(td, TD_EXITED);
346
347         pthread_cond_destroy(&sw->cond);
348         pthread_mutex_destroy(&sw->lock);
349 }
350
351 static void shutdown_worker(struct submit_worker *sw, unsigned int *sum_cnt)
352 {
353         struct thread_data *parent = sw->wq->td;
354
355         pthread_join(sw->thread, NULL);
356         (*sum_cnt)++;
357         sum_thread_stats(&parent->ts, &sw->td.ts, *sum_cnt == 1);
358         free_worker(sw);
359 }
360
361 void workqueue_exit(struct workqueue *wq)
362 {
363         unsigned int shutdown, sum_cnt = 0;
364         struct submit_worker *sw;
365         int i;
366
367         for (i = 0; i < wq->max_workers; i++) {
368                 sw = &wq->workers[i];
369
370                 pthread_mutex_lock(&sw->lock);
371                 sw->flags |= SW_F_EXIT;
372                 pthread_cond_signal(&sw->cond);
373                 pthread_mutex_unlock(&sw->lock);
374         }
375
376         do {
377                 shutdown = 0;
378                 for (i = 0; i < wq->max_workers; i++) {
379                         sw = &wq->workers[i];
380                         if (sw->flags & SW_F_ACCOUNTED)
381                                 continue;
382                         pthread_mutex_lock(&sw->lock);
383                         sw->flags |= SW_F_ACCOUNTED;
384                         pthread_mutex_unlock(&sw->lock);
385                         shutdown_worker(sw, &sum_cnt);
386                         shutdown++;
387                 }
388         } while (shutdown && shutdown != wq->max_workers);
389
390         free(wq->workers);
391         pthread_mutex_destroy(&wq->flush_lock);
392         pthread_cond_destroy(&wq->flush_cond);
393         pthread_mutex_destroy(&wq->stat_lock);
394 }
395
396 static int start_worker(struct workqueue *wq, unsigned int index)
397 {
398         struct submit_worker *sw = &wq->workers[index];
399         int ret;
400
401         INIT_FLIST_HEAD(&sw->work_list);
402         pthread_cond_init(&sw->cond, NULL);
403         pthread_mutex_init(&sw->lock, NULL);
404         sw->wq = wq;
405         sw->index = index;
406
407         ret = pthread_create(&sw->thread, NULL, worker_thread, sw);
408         if (!ret) {
409                 pthread_mutex_lock(&sw->lock);
410                 sw->flags = SW_F_IDLE;
411                 pthread_mutex_unlock(&sw->lock);
412                 return 0;
413         }
414
415         free_worker(sw);
416         return 1;
417 }
418
419 int workqueue_init(struct thread_data *td, struct workqueue *wq,
420                    workqueue_fn *fn, unsigned max_pending)
421 {
422         unsigned int running;
423         int i, error;
424
425         wq->max_workers = max_pending;
426         wq->td = td;
427         wq->fn = fn;
428         wq->work_seq = 0;
429         wq->next_free_worker = 0;
430         pthread_cond_init(&wq->flush_cond, NULL);
431         pthread_mutex_init(&wq->flush_lock, NULL);
432         pthread_mutex_init(&wq->stat_lock, NULL);
433
434         wq->workers = calloc(wq->max_workers, sizeof(struct submit_worker));
435
436         for (i = 0; i < wq->max_workers; i++)
437                 if (start_worker(wq, i))
438                         break;
439
440         wq->max_workers = i;
441         if (!wq->max_workers)
442                 goto err;
443
444         /*
445          * Wait for them all to be started and initialized
446          */
447         error = 0;
448         do {
449                 struct submit_worker *sw;
450
451                 running = 0;
452                 pthread_mutex_lock(&wq->flush_lock);
453                 for (i = 0; i < wq->max_workers; i++) {
454                         sw = &wq->workers[i];
455                         pthread_mutex_lock(&sw->lock);
456                         if (sw->flags & SW_F_RUNNING)
457                                 running++;
458                         if (sw->flags & SW_F_ERROR)
459                                 error++;
460                         pthread_mutex_unlock(&sw->lock);
461                 }
462
463                 if (error || running == wq->max_workers) {
464                         pthread_mutex_unlock(&wq->flush_lock);
465                         break;
466                 }
467
468                 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
469                 pthread_mutex_unlock(&wq->flush_lock);
470         } while (1);
471
472         if (!error)
473                 return 0;
474
475 err:
476         log_err("Can't create rate workqueue\n");
477         td_verror(td, ESRCH, "workqueue_init");
478         workqueue_exit(wq);
479         return 1;
480 }