workqueue: move private accounting to 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 static void *worker_thread(void *data)
134 {
135         struct submit_worker *sw = data;
136         struct workqueue *wq = sw->wq;
137         unsigned int eflags = 0, ret;
138         FLIST_HEAD(local_list);
139
140         ret = workqueue_init_worker(sw);
141         pthread_mutex_lock(&sw->lock);
142         sw->flags |= SW_F_RUNNING;
143         if (ret)
144                 sw->flags |= SW_F_ERROR;
145         pthread_mutex_unlock(&sw->lock);
146
147         pthread_mutex_lock(&wq->flush_lock);
148         pthread_cond_signal(&wq->flush_cond);
149         pthread_mutex_unlock(&wq->flush_lock);
150
151         if (sw->flags & SW_F_ERROR)
152                 goto done;
153
154         while (1) {
155                 pthread_mutex_lock(&sw->lock);
156
157                 if (flist_empty(&sw->work_list)) {
158                         if (sw->flags & SW_F_EXIT) {
159                                 pthread_mutex_unlock(&sw->lock);
160                                 break;
161                         }
162
163                         if (workqueue_pre_sleep_check(sw)) {
164                                 pthread_mutex_unlock(&sw->lock);
165                                 workqueue_pre_sleep(sw);
166                                 pthread_mutex_lock(&sw->lock);
167                         }
168
169                         /*
170                          * We dropped and reaquired the lock, check
171                          * state again.
172                          */
173                         if (!flist_empty(&sw->work_list))
174                                 goto handle_work;
175
176                         if (sw->flags & SW_F_EXIT) {
177                                 pthread_mutex_unlock(&sw->lock);
178                                 break;
179                         } else if (!(sw->flags & SW_F_IDLE)) {
180                                 sw->flags |= SW_F_IDLE;
181                                 wq->next_free_worker = sw->index;
182                                 if (wq->wake_idle)
183                                         pthread_cond_signal(&wq->flush_cond);
184                         }
185                         if (wq->ops.update_acct_fn)
186                                 wq->ops.update_acct_fn(sw);
187
188                         pthread_cond_wait(&sw->cond, &sw->lock);
189                 } else {
190 handle_work:
191                         flist_splice_init(&sw->work_list, &local_list);
192                 }
193                 pthread_mutex_unlock(&sw->lock);
194                 handle_list(sw, &local_list);
195         }
196
197         if (wq->ops.update_acct_fn)
198                 wq->ops.update_acct_fn(sw);
199
200 done:
201         pthread_mutex_lock(&sw->lock);
202         sw->flags |= (SW_F_EXITED | eflags);
203         pthread_mutex_unlock(&sw->lock);
204         return NULL;
205 }
206
207 static void free_worker(struct submit_worker *sw)
208 {
209         struct workqueue *wq = sw->wq;
210
211         workqueue_exit_worker(sw);
212
213         pthread_cond_destroy(&sw->cond);
214         pthread_mutex_destroy(&sw->lock);
215
216         if (wq->ops.free_worker_fn)
217                 wq->ops.free_worker_fn(sw);
218 }
219
220 static void shutdown_worker(struct submit_worker *sw, unsigned int *sum_cnt)
221 {
222         struct thread_data *parent = sw->wq->td;
223         struct thread_data *td = sw->private;
224
225         pthread_join(sw->thread, NULL);
226         (*sum_cnt)++;
227         sum_thread_stats(&parent->ts, &td->ts, *sum_cnt == 1);
228         free_worker(sw);
229 }
230
231 void workqueue_exit(struct workqueue *wq)
232 {
233         unsigned int shutdown, sum_cnt = 0;
234         struct submit_worker *sw;
235         int i;
236
237         for (i = 0; i < wq->max_workers; i++) {
238                 sw = &wq->workers[i];
239
240                 pthread_mutex_lock(&sw->lock);
241                 sw->flags |= SW_F_EXIT;
242                 pthread_cond_signal(&sw->cond);
243                 pthread_mutex_unlock(&sw->lock);
244         }
245
246         do {
247                 shutdown = 0;
248                 for (i = 0; i < wq->max_workers; i++) {
249                         sw = &wq->workers[i];
250                         if (sw->flags & SW_F_ACCOUNTED)
251                                 continue;
252                         pthread_mutex_lock(&sw->lock);
253                         sw->flags |= SW_F_ACCOUNTED;
254                         pthread_mutex_unlock(&sw->lock);
255                         shutdown_worker(sw, &sum_cnt);
256                         shutdown++;
257                 }
258         } while (shutdown && shutdown != wq->max_workers);
259
260         free(wq->workers);
261         pthread_mutex_destroy(&wq->flush_lock);
262         pthread_cond_destroy(&wq->flush_cond);
263         pthread_mutex_destroy(&wq->stat_lock);
264 }
265
266 static int start_worker(struct workqueue *wq, unsigned int index)
267 {
268         struct submit_worker *sw = &wq->workers[index];
269         int ret;
270
271         INIT_FLIST_HEAD(&sw->work_list);
272         pthread_cond_init(&sw->cond, NULL);
273         pthread_mutex_init(&sw->lock, NULL);
274         sw->wq = wq;
275         sw->index = index;
276
277         if (wq->ops.alloc_worker_fn) {
278                 ret = wq->ops.alloc_worker_fn(sw);
279                 if (ret)
280                         return ret;
281         }
282
283         ret = pthread_create(&sw->thread, NULL, worker_thread, sw);
284         if (!ret) {
285                 pthread_mutex_lock(&sw->lock);
286                 sw->flags = SW_F_IDLE;
287                 pthread_mutex_unlock(&sw->lock);
288                 return 0;
289         }
290
291         free_worker(sw);
292         return 1;
293 }
294
295 int workqueue_init(struct thread_data *td, struct workqueue *wq,
296                    struct workqueue_ops *ops, unsigned max_pending)
297 {
298         unsigned int running;
299         int i, error;
300
301         wq->max_workers = max_pending;
302         wq->td = td;
303         wq->ops = *ops;
304         wq->work_seq = 0;
305         wq->next_free_worker = 0;
306         pthread_cond_init(&wq->flush_cond, NULL);
307         pthread_mutex_init(&wq->flush_lock, NULL);
308         pthread_mutex_init(&wq->stat_lock, NULL);
309
310         wq->workers = calloc(wq->max_workers, sizeof(struct submit_worker));
311
312         for (i = 0; i < wq->max_workers; i++)
313                 if (start_worker(wq, i))
314                         break;
315
316         wq->max_workers = i;
317         if (!wq->max_workers)
318                 goto err;
319
320         /*
321          * Wait for them all to be started and initialized
322          */
323         error = 0;
324         do {
325                 struct submit_worker *sw;
326
327                 running = 0;
328                 pthread_mutex_lock(&wq->flush_lock);
329                 for (i = 0; i < wq->max_workers; i++) {
330                         sw = &wq->workers[i];
331                         pthread_mutex_lock(&sw->lock);
332                         if (sw->flags & SW_F_RUNNING)
333                                 running++;
334                         if (sw->flags & SW_F_ERROR)
335                                 error++;
336                         pthread_mutex_unlock(&sw->lock);
337                 }
338
339                 if (error || running == wq->max_workers) {
340                         pthread_mutex_unlock(&wq->flush_lock);
341                         break;
342                 }
343
344                 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
345                 pthread_mutex_unlock(&wq->flush_lock);
346         } while (1);
347
348         if (!error)
349                 return 0;
350
351 err:
352         log_err("Can't create rate workqueue\n");
353         td_verror(td, ESRCH, "workqueue_init");
354         workqueue_exit(wq);
355         return 1;
356 }