f26d9d7bae1f3f75a2c9794b0ee0e41656759c82
[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 workqueue *wq = sw->wq;
274
275         workqueue_exit_worker(sw);
276
277         pthread_cond_destroy(&sw->cond);
278         pthread_mutex_destroy(&sw->lock);
279
280         if (wq->ops.free_worker_fn)
281                 wq->ops.free_worker_fn(sw);
282 }
283
284 static void shutdown_worker(struct submit_worker *sw, unsigned int *sum_cnt)
285 {
286         struct thread_data *parent = sw->wq->td;
287         struct thread_data *td = sw->private;
288
289         pthread_join(sw->thread, NULL);
290         (*sum_cnt)++;
291         sum_thread_stats(&parent->ts, &td->ts, *sum_cnt == 1);
292         free_worker(sw);
293 }
294
295 void workqueue_exit(struct workqueue *wq)
296 {
297         unsigned int shutdown, sum_cnt = 0;
298         struct submit_worker *sw;
299         int i;
300
301         for (i = 0; i < wq->max_workers; i++) {
302                 sw = &wq->workers[i];
303
304                 pthread_mutex_lock(&sw->lock);
305                 sw->flags |= SW_F_EXIT;
306                 pthread_cond_signal(&sw->cond);
307                 pthread_mutex_unlock(&sw->lock);
308         }
309
310         do {
311                 shutdown = 0;
312                 for (i = 0; i < wq->max_workers; i++) {
313                         sw = &wq->workers[i];
314                         if (sw->flags & SW_F_ACCOUNTED)
315                                 continue;
316                         pthread_mutex_lock(&sw->lock);
317                         sw->flags |= SW_F_ACCOUNTED;
318                         pthread_mutex_unlock(&sw->lock);
319                         shutdown_worker(sw, &sum_cnt);
320                         shutdown++;
321                 }
322         } while (shutdown && shutdown != wq->max_workers);
323
324         free(wq->workers);
325         pthread_mutex_destroy(&wq->flush_lock);
326         pthread_cond_destroy(&wq->flush_cond);
327         pthread_mutex_destroy(&wq->stat_lock);
328 }
329
330 static int start_worker(struct workqueue *wq, unsigned int index)
331 {
332         struct submit_worker *sw = &wq->workers[index];
333         int ret;
334
335         INIT_FLIST_HEAD(&sw->work_list);
336         pthread_cond_init(&sw->cond, NULL);
337         pthread_mutex_init(&sw->lock, NULL);
338         sw->wq = wq;
339         sw->index = index;
340
341         if (wq->ops.alloc_worker_fn) {
342                 ret = wq->ops.alloc_worker_fn(sw);
343                 if (ret)
344                         return ret;
345         }
346
347         ret = pthread_create(&sw->thread, NULL, worker_thread, sw);
348         if (!ret) {
349                 pthread_mutex_lock(&sw->lock);
350                 sw->flags = SW_F_IDLE;
351                 pthread_mutex_unlock(&sw->lock);
352                 return 0;
353         }
354
355         free_worker(sw);
356         return 1;
357 }
358
359 int workqueue_init(struct thread_data *td, struct workqueue *wq,
360                    struct workqueue_ops *ops, unsigned max_pending)
361 {
362         unsigned int running;
363         int i, error;
364
365         wq->max_workers = max_pending;
366         wq->td = td;
367         wq->ops = *ops;
368         wq->work_seq = 0;
369         wq->next_free_worker = 0;
370         pthread_cond_init(&wq->flush_cond, NULL);
371         pthread_mutex_init(&wq->flush_lock, NULL);
372         pthread_mutex_init(&wq->stat_lock, NULL);
373
374         wq->workers = calloc(wq->max_workers, sizeof(struct submit_worker));
375
376         for (i = 0; i < wq->max_workers; i++)
377                 if (start_worker(wq, i))
378                         break;
379
380         wq->max_workers = i;
381         if (!wq->max_workers)
382                 goto err;
383
384         /*
385          * Wait for them all to be started and initialized
386          */
387         error = 0;
388         do {
389                 struct submit_worker *sw;
390
391                 running = 0;
392                 pthread_mutex_lock(&wq->flush_lock);
393                 for (i = 0; i < wq->max_workers; i++) {
394                         sw = &wq->workers[i];
395                         pthread_mutex_lock(&sw->lock);
396                         if (sw->flags & SW_F_RUNNING)
397                                 running++;
398                         if (sw->flags & SW_F_ERROR)
399                                 error++;
400                         pthread_mutex_unlock(&sw->lock);
401                 }
402
403                 if (error || running == wq->max_workers) {
404                         pthread_mutex_unlock(&wq->flush_lock);
405                         break;
406                 }
407
408                 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
409                 pthread_mutex_unlock(&wq->flush_lock);
410         } while (1);
411
412         if (!error)
413                 return 0;
414
415 err:
416         log_err("Can't create rate workqueue\n");
417         td_verror(td, ESRCH, "workqueue_init");
418         workqueue_exit(wq);
419         return 1;
420 }