workqueue: move last bits of end accounting to caller
[fio.git] / workqueue.c
CommitLineData
a9da8ab2
JA
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"
a9da8ab2
JA
10#include "flist.h"
11#include "workqueue.h"
12#include "lib/getrusage.h"
13
a9da8ab2
JA
14enum {
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
23static 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
42static 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
1391052a 69static bool all_sw_idle(struct workqueue *wq)
a9da8ab2
JA
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))
1391052a 77 return false;
a9da8ab2
JA
78 }
79
1391052a 80 return true;
a9da8ab2
JA
81}
82
83/*
84 * Must be serialized wrt workqueue_enqueue() by caller
85 */
86void 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/*
b07f6ad1 100 * Must be serialized by caller. Returns true for queued, false for busy.
a9da8ab2 101 */
88271841 102bool workqueue_enqueue(struct workqueue *wq, struct workqueue_work *work)
a9da8ab2
JA
103{
104 struct submit_worker *sw;
105
106 sw = get_submit_worker(wq);
107 if (sw) {
a9da8ab2 108 pthread_mutex_lock(&sw->lock);
88271841 109 flist_add_tail(&work->list, &sw->work_list);
a9da8ab2
JA
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);
b07f6ad1 115 return true;
a9da8ab2
JA
116 }
117
b07f6ad1 118 return false;
a9da8ab2
JA
119}
120
121static void handle_list(struct submit_worker *sw, struct flist_head *list)
122{
123 struct workqueue *wq = sw->wq;
88271841 124 struct workqueue_work *work;
a9da8ab2
JA
125
126 while (!flist_empty(list)) {
88271841
JA
127 work = flist_first_entry(list, struct workqueue_work, list);
128 flist_del_init(&work->list);
ee2b6d6e 129 wq->ops.fn(sw, work);
a9da8ab2
JA
130 }
131}
132
a9da8ab2
JA
133static void *worker_thread(void *data)
134{
135 struct submit_worker *sw = data;
136 struct workqueue *wq = sw->wq;
a9da8ab2
JA
137 unsigned int eflags = 0, ret;
138 FLIST_HEAD(local_list);
139
80ea26a8 140 ret = workqueue_init_worker(sw);
a9da8ab2
JA
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
ee2b6d6e 163 if (workqueue_pre_sleep_check(sw)) {
a9da8ab2 164 pthread_mutex_unlock(&sw->lock);
ee2b6d6e 165 workqueue_pre_sleep(sw);
a9da8ab2
JA
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 }
17ecadc3
JA
185 if (wq->ops.update_acct_fn)
186 wq->ops.update_acct_fn(sw);
187
a9da8ab2
JA
188 pthread_cond_wait(&sw->cond, &sw->lock);
189 } else {
190handle_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
17ecadc3
JA
197 if (wq->ops.update_acct_fn)
198 wq->ops.update_acct_fn(sw);
a9da8ab2
JA
199
200done:
201 pthread_mutex_lock(&sw->lock);
202 sw->flags |= (SW_F_EXITED | eflags);
203 pthread_mutex_unlock(&sw->lock);
204 return NULL;
205}
206
c35c582d 207static void free_worker(struct submit_worker *sw, unsigned int *sum_cnt)
a9da8ab2 208{
ee2b6d6e 209 struct workqueue *wq = sw->wq;
a9da8ab2 210
c35c582d 211 workqueue_exit_worker(sw, sum_cnt);
a9da8ab2
JA
212
213 pthread_cond_destroy(&sw->cond);
214 pthread_mutex_destroy(&sw->lock);
ee2b6d6e
JA
215
216 if (wq->ops.free_worker_fn)
217 wq->ops.free_worker_fn(sw);
a9da8ab2
JA
218}
219
220static void shutdown_worker(struct submit_worker *sw, unsigned int *sum_cnt)
221{
a9da8ab2 222 pthread_join(sw->thread, NULL);
c35c582d 223 free_worker(sw, sum_cnt);
a9da8ab2
JA
224}
225
226void workqueue_exit(struct workqueue *wq)
227{
228 unsigned int shutdown, sum_cnt = 0;
229 struct submit_worker *sw;
230 int i;
231
232 for (i = 0; i < wq->max_workers; i++) {
233 sw = &wq->workers[i];
234
235 pthread_mutex_lock(&sw->lock);
236 sw->flags |= SW_F_EXIT;
237 pthread_cond_signal(&sw->cond);
238 pthread_mutex_unlock(&sw->lock);
239 }
240
241 do {
242 shutdown = 0;
243 for (i = 0; i < wq->max_workers; i++) {
244 sw = &wq->workers[i];
245 if (sw->flags & SW_F_ACCOUNTED)
246 continue;
b7d0bbf1 247 pthread_mutex_lock(&sw->lock);
a9da8ab2 248 sw->flags |= SW_F_ACCOUNTED;
b7d0bbf1 249 pthread_mutex_unlock(&sw->lock);
a9da8ab2
JA
250 shutdown_worker(sw, &sum_cnt);
251 shutdown++;
252 }
253 } while (shutdown && shutdown != wq->max_workers);
254
255 free(wq->workers);
256 pthread_mutex_destroy(&wq->flush_lock);
257 pthread_cond_destroy(&wq->flush_cond);
2a274336 258 pthread_mutex_destroy(&wq->stat_lock);
a9da8ab2
JA
259}
260
261static int start_worker(struct workqueue *wq, unsigned int index)
262{
263 struct submit_worker *sw = &wq->workers[index];
264 int ret;
265
266 INIT_FLIST_HEAD(&sw->work_list);
267 pthread_cond_init(&sw->cond, NULL);
268 pthread_mutex_init(&sw->lock, NULL);
269 sw->wq = wq;
270 sw->index = index;
271
ee2b6d6e
JA
272 if (wq->ops.alloc_worker_fn) {
273 ret = wq->ops.alloc_worker_fn(sw);
274 if (ret)
275 return ret;
276 }
277
a9da8ab2
JA
278 ret = pthread_create(&sw->thread, NULL, worker_thread, sw);
279 if (!ret) {
280 pthread_mutex_lock(&sw->lock);
281 sw->flags = SW_F_IDLE;
282 pthread_mutex_unlock(&sw->lock);
283 return 0;
284 }
285
c35c582d 286 free_worker(sw, NULL);
a9da8ab2
JA
287 return 1;
288}
289
290int workqueue_init(struct thread_data *td, struct workqueue *wq,
5bb79f69 291 struct workqueue_ops *ops, unsigned max_pending)
a9da8ab2
JA
292{
293 unsigned int running;
294 int i, error;
295
296 wq->max_workers = max_pending;
297 wq->td = td;
5bb79f69 298 wq->ops = *ops;
a9da8ab2
JA
299 wq->work_seq = 0;
300 wq->next_free_worker = 0;
301 pthread_cond_init(&wq->flush_cond, NULL);
302 pthread_mutex_init(&wq->flush_lock, NULL);
2a274336 303 pthread_mutex_init(&wq->stat_lock, NULL);
a9da8ab2
JA
304
305 wq->workers = calloc(wq->max_workers, sizeof(struct submit_worker));
306
307 for (i = 0; i < wq->max_workers; i++)
308 if (start_worker(wq, i))
309 break;
310
311 wq->max_workers = i;
bddc8d16
JA
312 if (!wq->max_workers)
313 goto err;
a9da8ab2
JA
314
315 /*
316 * Wait for them all to be started and initialized
317 */
318 error = 0;
319 do {
320 struct submit_worker *sw;
321
322 running = 0;
323 pthread_mutex_lock(&wq->flush_lock);
324 for (i = 0; i < wq->max_workers; i++) {
325 sw = &wq->workers[i];
326 pthread_mutex_lock(&sw->lock);
327 if (sw->flags & SW_F_RUNNING)
328 running++;
329 if (sw->flags & SW_F_ERROR)
330 error++;
331 pthread_mutex_unlock(&sw->lock);
332 }
333
334 if (error || running == wq->max_workers) {
335 pthread_mutex_unlock(&wq->flush_lock);
336 break;
337 }
338
339 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
340 pthread_mutex_unlock(&wq->flush_lock);
341 } while (1);
342
bddc8d16
JA
343 if (!error)
344 return 0;
a9da8ab2 345
bddc8d16
JA
346err:
347 log_err("Can't create rate workqueue\n");
348 td_verror(td, ESRCH, "workqueue_init");
349 workqueue_exit(wq);
350 return 1;
a9da8ab2 351}