Commit | Line | Data |
---|---|---|
a9da8ab2 | 1 | /* |
51575029 | 2 | * Generic workqueue offload mechanism |
a9da8ab2 JA |
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" | |
a47591e4 | 12 | #include "smalloc.h" |
a9da8ab2 | 13 | |
a9da8ab2 JA |
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 | ||
1391052a | 69 | static 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 | */ | |
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 | /* | |
b07f6ad1 | 100 | * Must be serialized by caller. Returns true for queued, false for busy. |
a9da8ab2 | 101 | */ |
26de50cf | 102 | void workqueue_enqueue(struct workqueue *wq, struct workqueue_work *work) |
a9da8ab2 JA |
103 | { |
104 | struct submit_worker *sw; | |
105 | ||
106 | sw = get_submit_worker(wq); | |
26de50cf JA |
107 | assert(sw); |
108 | ||
109 | pthread_mutex_lock(&sw->lock); | |
110 | flist_add_tail(&work->list, &sw->work_list); | |
111 | sw->seq = ++wq->work_seq; | |
112 | sw->flags &= ~SW_F_IDLE; | |
113 | pthread_mutex_unlock(&sw->lock); | |
a9da8ab2 | 114 | |
26de50cf | 115 | pthread_cond_signal(&sw->cond); |
a9da8ab2 JA |
116 | } |
117 | ||
118 | static void handle_list(struct submit_worker *sw, struct flist_head *list) | |
119 | { | |
120 | struct workqueue *wq = sw->wq; | |
88271841 | 121 | struct workqueue_work *work; |
a9da8ab2 JA |
122 | |
123 | while (!flist_empty(list)) { | |
88271841 JA |
124 | work = flist_first_entry(list, struct workqueue_work, list); |
125 | flist_del_init(&work->list); | |
ee2b6d6e | 126 | wq->ops.fn(sw, work); |
a9da8ab2 JA |
127 | } |
128 | } | |
129 | ||
a9da8ab2 JA |
130 | static void *worker_thread(void *data) |
131 | { | |
132 | struct submit_worker *sw = data; | |
133 | struct workqueue *wq = sw->wq; | |
f6496ba7 | 134 | unsigned int eflags = 0, ret = 0; |
a9da8ab2 JA |
135 | FLIST_HEAD(local_list); |
136 | ||
24660963 JA |
137 | sk_out_assign(sw->sk_out); |
138 | ||
f6496ba7 JA |
139 | if (wq->ops.nice) { |
140 | if (nice(wq->ops.nice) < 0) { | |
141 | log_err("workqueue: nice %s\n", strerror(errno)); | |
142 | ret = 1; | |
143 | } | |
144 | } | |
145 | ||
146 | if (!ret) | |
147 | ret = workqueue_init_worker(sw); | |
148 | ||
a9da8ab2 JA |
149 | pthread_mutex_lock(&sw->lock); |
150 | sw->flags |= SW_F_RUNNING; | |
151 | if (ret) | |
152 | sw->flags |= SW_F_ERROR; | |
153 | pthread_mutex_unlock(&sw->lock); | |
154 | ||
155 | pthread_mutex_lock(&wq->flush_lock); | |
156 | pthread_cond_signal(&wq->flush_cond); | |
157 | pthread_mutex_unlock(&wq->flush_lock); | |
158 | ||
159 | if (sw->flags & SW_F_ERROR) | |
160 | goto done; | |
161 | ||
162 | while (1) { | |
163 | pthread_mutex_lock(&sw->lock); | |
164 | ||
165 | if (flist_empty(&sw->work_list)) { | |
166 | if (sw->flags & SW_F_EXIT) { | |
167 | pthread_mutex_unlock(&sw->lock); | |
168 | break; | |
169 | } | |
170 | ||
ee2b6d6e | 171 | if (workqueue_pre_sleep_check(sw)) { |
a9da8ab2 | 172 | pthread_mutex_unlock(&sw->lock); |
ee2b6d6e | 173 | workqueue_pre_sleep(sw); |
a9da8ab2 JA |
174 | pthread_mutex_lock(&sw->lock); |
175 | } | |
176 | ||
177 | /* | |
178 | * We dropped and reaquired the lock, check | |
179 | * state again. | |
180 | */ | |
181 | if (!flist_empty(&sw->work_list)) | |
182 | goto handle_work; | |
183 | ||
184 | if (sw->flags & SW_F_EXIT) { | |
185 | pthread_mutex_unlock(&sw->lock); | |
186 | break; | |
187 | } else if (!(sw->flags & SW_F_IDLE)) { | |
188 | sw->flags |= SW_F_IDLE; | |
189 | wq->next_free_worker = sw->index; | |
190 | if (wq->wake_idle) | |
191 | pthread_cond_signal(&wq->flush_cond); | |
192 | } | |
17ecadc3 JA |
193 | if (wq->ops.update_acct_fn) |
194 | wq->ops.update_acct_fn(sw); | |
195 | ||
a9da8ab2 JA |
196 | pthread_cond_wait(&sw->cond, &sw->lock); |
197 | } else { | |
198 | handle_work: | |
199 | flist_splice_init(&sw->work_list, &local_list); | |
200 | } | |
201 | pthread_mutex_unlock(&sw->lock); | |
202 | handle_list(sw, &local_list); | |
203 | } | |
204 | ||
17ecadc3 JA |
205 | if (wq->ops.update_acct_fn) |
206 | wq->ops.update_acct_fn(sw); | |
a9da8ab2 JA |
207 | |
208 | done: | |
209 | pthread_mutex_lock(&sw->lock); | |
210 | sw->flags |= (SW_F_EXITED | eflags); | |
211 | pthread_mutex_unlock(&sw->lock); | |
24660963 | 212 | sk_out_drop(); |
a9da8ab2 JA |
213 | return NULL; |
214 | } | |
215 | ||
c35c582d | 216 | static void free_worker(struct submit_worker *sw, unsigned int *sum_cnt) |
a9da8ab2 | 217 | { |
ee2b6d6e | 218 | struct workqueue *wq = sw->wq; |
a9da8ab2 | 219 | |
c35c582d | 220 | workqueue_exit_worker(sw, sum_cnt); |
a9da8ab2 JA |
221 | |
222 | pthread_cond_destroy(&sw->cond); | |
223 | pthread_mutex_destroy(&sw->lock); | |
ee2b6d6e JA |
224 | |
225 | if (wq->ops.free_worker_fn) | |
226 | wq->ops.free_worker_fn(sw); | |
a9da8ab2 JA |
227 | } |
228 | ||
229 | static void shutdown_worker(struct submit_worker *sw, unsigned int *sum_cnt) | |
230 | { | |
a9da8ab2 | 231 | pthread_join(sw->thread, NULL); |
c35c582d | 232 | free_worker(sw, sum_cnt); |
a9da8ab2 JA |
233 | } |
234 | ||
235 | void workqueue_exit(struct workqueue *wq) | |
236 | { | |
237 | unsigned int shutdown, sum_cnt = 0; | |
238 | struct submit_worker *sw; | |
239 | int i; | |
240 | ||
f8e266c9 JA |
241 | if (!wq->workers) |
242 | return; | |
243 | ||
a9da8ab2 JA |
244 | for (i = 0; i < wq->max_workers; i++) { |
245 | sw = &wq->workers[i]; | |
246 | ||
247 | pthread_mutex_lock(&sw->lock); | |
248 | sw->flags |= SW_F_EXIT; | |
249 | pthread_cond_signal(&sw->cond); | |
250 | pthread_mutex_unlock(&sw->lock); | |
251 | } | |
252 | ||
253 | do { | |
254 | shutdown = 0; | |
255 | for (i = 0; i < wq->max_workers; i++) { | |
256 | sw = &wq->workers[i]; | |
257 | if (sw->flags & SW_F_ACCOUNTED) | |
258 | continue; | |
b7d0bbf1 | 259 | pthread_mutex_lock(&sw->lock); |
a9da8ab2 | 260 | sw->flags |= SW_F_ACCOUNTED; |
b7d0bbf1 | 261 | pthread_mutex_unlock(&sw->lock); |
a9da8ab2 JA |
262 | shutdown_worker(sw, &sum_cnt); |
263 | shutdown++; | |
264 | } | |
265 | } while (shutdown && shutdown != wq->max_workers); | |
266 | ||
a47591e4 | 267 | sfree(wq->workers); |
f8e266c9 | 268 | wq->workers = NULL; |
a9da8ab2 JA |
269 | pthread_mutex_destroy(&wq->flush_lock); |
270 | pthread_cond_destroy(&wq->flush_cond); | |
2a274336 | 271 | pthread_mutex_destroy(&wq->stat_lock); |
a9da8ab2 JA |
272 | } |
273 | ||
24660963 JA |
274 | static int start_worker(struct workqueue *wq, unsigned int index, |
275 | struct sk_out *sk_out) | |
a9da8ab2 JA |
276 | { |
277 | struct submit_worker *sw = &wq->workers[index]; | |
278 | int ret; | |
279 | ||
280 | INIT_FLIST_HEAD(&sw->work_list); | |
34febb23 JA |
281 | |
282 | ret = mutex_cond_init_pshared(&sw->lock, &sw->cond); | |
f9e5b5ee JK |
283 | if (ret) |
284 | return ret; | |
34febb23 | 285 | |
a9da8ab2 JA |
286 | sw->wq = wq; |
287 | sw->index = index; | |
24660963 | 288 | sw->sk_out = sk_out; |
a9da8ab2 | 289 | |
ee2b6d6e JA |
290 | if (wq->ops.alloc_worker_fn) { |
291 | ret = wq->ops.alloc_worker_fn(sw); | |
292 | if (ret) | |
293 | return ret; | |
294 | } | |
295 | ||
a9da8ab2 JA |
296 | ret = pthread_create(&sw->thread, NULL, worker_thread, sw); |
297 | if (!ret) { | |
298 | pthread_mutex_lock(&sw->lock); | |
299 | sw->flags = SW_F_IDLE; | |
300 | pthread_mutex_unlock(&sw->lock); | |
301 | return 0; | |
302 | } | |
303 | ||
c35c582d | 304 | free_worker(sw, NULL); |
a9da8ab2 JA |
305 | return 1; |
306 | } | |
307 | ||
308 | int workqueue_init(struct thread_data *td, struct workqueue *wq, | |
24660963 JA |
309 | struct workqueue_ops *ops, unsigned int max_workers, |
310 | struct sk_out *sk_out) | |
a9da8ab2 JA |
311 | { |
312 | unsigned int running; | |
313 | int i, error; | |
f9e5b5ee | 314 | int ret; |
a9da8ab2 | 315 | |
24660963 | 316 | wq->max_workers = max_workers; |
a9da8ab2 | 317 | wq->td = td; |
5bb79f69 | 318 | wq->ops = *ops; |
a9da8ab2 JA |
319 | wq->work_seq = 0; |
320 | wq->next_free_worker = 0; | |
f9e5b5ee | 321 | |
34febb23 JA |
322 | ret = mutex_cond_init_pshared(&wq->flush_lock, &wq->flush_cond); |
323 | if (ret) | |
f9e5b5ee | 324 | goto err; |
34febb23 JA |
325 | ret = mutex_init_pshared(&wq->stat_lock); |
326 | if (ret) | |
f9e5b5ee | 327 | goto err; |
a9da8ab2 | 328 | |
a47591e4 | 329 | wq->workers = smalloc(wq->max_workers * sizeof(struct submit_worker)); |
a9da8ab2 JA |
330 | |
331 | for (i = 0; i < wq->max_workers; i++) | |
24660963 | 332 | if (start_worker(wq, i, sk_out)) |
a9da8ab2 JA |
333 | break; |
334 | ||
335 | wq->max_workers = i; | |
bddc8d16 JA |
336 | if (!wq->max_workers) |
337 | goto err; | |
a9da8ab2 JA |
338 | |
339 | /* | |
340 | * Wait for them all to be started and initialized | |
341 | */ | |
342 | error = 0; | |
343 | do { | |
344 | struct submit_worker *sw; | |
345 | ||
346 | running = 0; | |
347 | pthread_mutex_lock(&wq->flush_lock); | |
348 | for (i = 0; i < wq->max_workers; i++) { | |
349 | sw = &wq->workers[i]; | |
350 | pthread_mutex_lock(&sw->lock); | |
351 | if (sw->flags & SW_F_RUNNING) | |
352 | running++; | |
353 | if (sw->flags & SW_F_ERROR) | |
354 | error++; | |
355 | pthread_mutex_unlock(&sw->lock); | |
356 | } | |
357 | ||
358 | if (error || running == wq->max_workers) { | |
359 | pthread_mutex_unlock(&wq->flush_lock); | |
360 | break; | |
361 | } | |
362 | ||
363 | pthread_cond_wait(&wq->flush_cond, &wq->flush_lock); | |
364 | pthread_mutex_unlock(&wq->flush_lock); | |
365 | } while (1); | |
366 | ||
bddc8d16 JA |
367 | if (!error) |
368 | return 0; | |
a9da8ab2 | 369 | |
bddc8d16 JA |
370 | err: |
371 | log_err("Can't create rate workqueue\n"); | |
372 | td_verror(td, ESRCH, "workqueue_init"); | |
373 | workqueue_exit(wq); | |
374 | return 1; | |
a9da8ab2 | 375 | } |