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