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