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