workqueue: style cleanup
[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"
10#include "ioengine.h"
11#include "flist.h"
12#include "workqueue.h"
13#include "lib/getrusage.h"
14
15struct 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
27enum {
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
36static 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
55static 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
82static int 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 0;
91 }
92
93 return 1;
94}
95
96/*
97 * Must be serialized wrt workqueue_enqueue() by caller
98 */
99void 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.
114 */
115int 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 const enum fio_ddir ddir = acct_ddir(io_u);
122 struct thread_data *parent = wq->td;
123
124 if (ddir_rw(ddir)) {
125 parent->io_issues[ddir]++;
126 parent->io_issue_bytes[ddir] += io_u->xfer_buflen;
127 }
128
129 pthread_mutex_lock(&sw->lock);
130 flist_add_tail(&io_u->verify_list, &sw->work_list);
131 sw->seq = ++wq->work_seq;
132 sw->flags &= ~SW_F_IDLE;
133 pthread_mutex_unlock(&sw->lock);
134
135 pthread_cond_signal(&sw->cond);
136 return FIO_Q_QUEUED;
137 }
138
139 return FIO_Q_BUSY;
140}
141
142static void handle_list(struct submit_worker *sw, struct flist_head *list)
143{
144 struct workqueue *wq = sw->wq;
145 struct io_u *io_u;
146
147 while (!flist_empty(list)) {
148 io_u = flist_first_entry(list, struct io_u, verify_list);
149 flist_del_init(&io_u->verify_list);
150 wq->fn(&sw->td, io_u);
151 }
152}
153
154static int init_submit_worker(struct submit_worker *sw)
155{
156 struct thread_data *parent = sw->wq->td;
157 struct thread_data *td = &sw->td;
158 int fio_unused ret;
159
160 memcpy(&td->o, &parent->o, sizeof(td->o));
161 memcpy(&td->ts, &parent->ts, sizeof(td->ts));
162 td->o.uid = td->o.gid = -1U;
163 dup_files(td, parent);
164 fio_options_mem_dupe(td);
165
166 if (ioengine_load(td))
167 goto err;
168
169 if (td->o.odirect)
170 td->io_ops->flags |= FIO_RAWIO;
171
172 td->pid = gettid();
173
174 INIT_FLIST_HEAD(&td->io_log_list);
175 INIT_FLIST_HEAD(&td->io_hist_list);
176 INIT_FLIST_HEAD(&td->verify_list);
177 INIT_FLIST_HEAD(&td->trim_list);
178 INIT_FLIST_HEAD(&td->next_rand_list);
179 td->io_hist_tree = RB_ROOT;
180
181 td->o.iodepth = 1;
182 if (td_io_init(td))
183 goto err_io_init;
184
185 fio_gettime(&td->epoch, NULL);
186 fio_getrusage(&td->ru_start);
187 clear_io_state(td);
188
189 td_set_runstate(td, TD_RUNNING);
190 td->flags |= TD_F_CHILD;
191 td->parent = parent;
192 return 0;
193
194err_io_init:
195 close_ioengine(td);
196err:
197 return 1;
198}
199
200static void sum_val(uint64_t *dst, uint64_t *src)
201{
202 if (*src) {
203 __sync_fetch_and_add(dst, *src);
204 *src = 0;
205 }
206}
207
208static void sum_ddir(struct thread_data *dst, struct thread_data *src,
209 enum fio_ddir ddir)
210{
211 sum_val(&dst->io_bytes[ddir], &src->io_bytes[ddir]);
212 sum_val(&dst->io_blocks[ddir], &src->io_blocks[ddir]);
213 sum_val(&dst->this_io_blocks[ddir], &src->this_io_blocks[ddir]);
214 sum_val(&dst->this_io_bytes[ddir], &src->this_io_bytes[ddir]);
215 sum_val(&dst->bytes_done[ddir], &src->bytes_done[ddir]);
216}
217
218static void update_accounting(struct submit_worker *sw)
219{
220 struct thread_data *src = &sw->td;
221 struct thread_data *dst = sw->wq->td;
222
223 if (td_read(src))
224 sum_ddir(dst, src, DDIR_READ);
225 if (td_write(src))
226 sum_ddir(dst, src, DDIR_WRITE);
227 if (td_trim(src))
228 sum_ddir(dst, src, DDIR_TRIM);
229}
230
231static void *worker_thread(void *data)
232{
233 struct submit_worker *sw = data;
234 struct workqueue *wq = sw->wq;
235 struct thread_data *td = &sw->td;
236 unsigned int eflags = 0, ret;
237 FLIST_HEAD(local_list);
238
239 ret = init_submit_worker(sw);
240 pthread_mutex_lock(&sw->lock);
241 sw->flags |= SW_F_RUNNING;
242 if (ret)
243 sw->flags |= SW_F_ERROR;
244 pthread_mutex_unlock(&sw->lock);
245
246 pthread_mutex_lock(&wq->flush_lock);
247 pthread_cond_signal(&wq->flush_cond);
248 pthread_mutex_unlock(&wq->flush_lock);
249
250 if (sw->flags & SW_F_ERROR)
251 goto done;
252
253 while (1) {
254 pthread_mutex_lock(&sw->lock);
255
256 if (flist_empty(&sw->work_list)) {
257 if (sw->flags & SW_F_EXIT) {
258 pthread_mutex_unlock(&sw->lock);
259 break;
260 }
261
262 if (td->io_u_queued || td->cur_depth ||
263 td->io_u_in_flight) {
264 pthread_mutex_unlock(&sw->lock);
265 io_u_quiesce(td);
266 pthread_mutex_lock(&sw->lock);
267 }
268
269 /*
270 * We dropped and reaquired the lock, check
271 * state again.
272 */
273 if (!flist_empty(&sw->work_list))
274 goto handle_work;
275
276 if (sw->flags & SW_F_EXIT) {
277 pthread_mutex_unlock(&sw->lock);
278 break;
279 } else if (!(sw->flags & SW_F_IDLE)) {
280 sw->flags |= SW_F_IDLE;
281 wq->next_free_worker = sw->index;
282 if (wq->wake_idle)
283 pthread_cond_signal(&wq->flush_cond);
284 }
285 update_accounting(sw);
286 pthread_cond_wait(&sw->cond, &sw->lock);
287 } else {
288handle_work:
289 flist_splice_init(&sw->work_list, &local_list);
290 }
291 pthread_mutex_unlock(&sw->lock);
292 handle_list(sw, &local_list);
293 }
294
295 update_accounting(sw);
296
297done:
298 pthread_mutex_lock(&sw->lock);
299 sw->flags |= (SW_F_EXITED | eflags);
300 pthread_mutex_unlock(&sw->lock);
301 return NULL;
302}
303
304static void free_worker(struct submit_worker *sw)
305{
306 struct thread_data *td = &sw->td;
307
308 fio_options_free(td);
309 close_and_free_files(td);
310 if (td->io_ops)
311 close_ioengine(td);
312 td_set_runstate(td, TD_EXITED);
313
314 pthread_cond_destroy(&sw->cond);
315 pthread_mutex_destroy(&sw->lock);
316}
317
318static void shutdown_worker(struct submit_worker *sw, unsigned int *sum_cnt)
319{
320 struct thread_data *parent = sw->wq->td;
321
322 pthread_join(sw->thread, NULL);
323 (*sum_cnt)++;
324 sum_thread_stats(&parent->ts, &sw->td.ts, *sum_cnt);
325 free_worker(sw);
326}
327
328void workqueue_exit(struct workqueue *wq)
329{
330 unsigned int shutdown, sum_cnt = 0;
331 struct submit_worker *sw;
332 int i;
333
334 for (i = 0; i < wq->max_workers; i++) {
335 sw = &wq->workers[i];
336
337 pthread_mutex_lock(&sw->lock);
338 sw->flags |= SW_F_EXIT;
339 pthread_cond_signal(&sw->cond);
340 pthread_mutex_unlock(&sw->lock);
341 }
342
343 do {
344 shutdown = 0;
345 for (i = 0; i < wq->max_workers; i++) {
346 sw = &wq->workers[i];
347 if (sw->flags & SW_F_ACCOUNTED)
348 continue;
349 sw->flags |= SW_F_ACCOUNTED;
350 shutdown_worker(sw, &sum_cnt);
351 shutdown++;
352 }
353 } while (shutdown && shutdown != wq->max_workers);
354
355 free(wq->workers);
356 pthread_mutex_destroy(&wq->flush_lock);
357 pthread_cond_destroy(&wq->flush_cond);
358}
359
360static int start_worker(struct workqueue *wq, unsigned int index)
361{
362 struct submit_worker *sw = &wq->workers[index];
363 int ret;
364
365 INIT_FLIST_HEAD(&sw->work_list);
366 pthread_cond_init(&sw->cond, NULL);
367 pthread_mutex_init(&sw->lock, NULL);
368 sw->wq = wq;
369 sw->index = index;
370
371 ret = pthread_create(&sw->thread, NULL, worker_thread, sw);
372 if (!ret) {
373 pthread_mutex_lock(&sw->lock);
374 sw->flags = SW_F_IDLE;
375 pthread_mutex_unlock(&sw->lock);
376 return 0;
377 }
378
379 free_worker(sw);
380 return 1;
381}
382
383int workqueue_init(struct thread_data *td, struct workqueue *wq,
384 workqueue_fn *fn, unsigned max_pending)
385{
386 unsigned int running;
387 int i, error;
388
389 wq->max_workers = max_pending;
390 wq->td = td;
391 wq->fn = fn;
392 wq->work_seq = 0;
393 wq->next_free_worker = 0;
394 pthread_cond_init(&wq->flush_cond, NULL);
395 pthread_mutex_init(&wq->flush_lock, NULL);
396
397 wq->workers = calloc(wq->max_workers, sizeof(struct submit_worker));
398
399 for (i = 0; i < wq->max_workers; i++)
400 if (start_worker(wq, i))
401 break;
402
403 wq->max_workers = i;
bddc8d16
JA
404 if (!wq->max_workers)
405 goto err;
a9da8ab2
JA
406
407 /*
408 * Wait for them all to be started and initialized
409 */
410 error = 0;
411 do {
412 struct submit_worker *sw;
413
414 running = 0;
415 pthread_mutex_lock(&wq->flush_lock);
416 for (i = 0; i < wq->max_workers; i++) {
417 sw = &wq->workers[i];
418 pthread_mutex_lock(&sw->lock);
419 if (sw->flags & SW_F_RUNNING)
420 running++;
421 if (sw->flags & SW_F_ERROR)
422 error++;
423 pthread_mutex_unlock(&sw->lock);
424 }
425
426 if (error || running == wq->max_workers) {
427 pthread_mutex_unlock(&wq->flush_lock);
428 break;
429 }
430
431 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
432 pthread_mutex_unlock(&wq->flush_lock);
433 } while (1);
434
bddc8d16
JA
435 if (!error)
436 return 0;
a9da8ab2 437
bddc8d16
JA
438err:
439 log_err("Can't create rate workqueue\n");
440 td_verror(td, ESRCH, "workqueue_init");
441 workqueue_exit(wq);
442 return 1;
a9da8ab2 443}