2 * Rated submission helpers
4 * Copyright (C) 2015 Jens Axboe <axboe@kernel.dk>
11 #include "workqueue.h"
12 #include "lib/getrusage.h"
16 SW_F_RUNNING = 1 << 1,
19 SW_F_ACCOUNTED = 1 << 4,
23 static struct submit_worker *__get_submit_worker(struct workqueue *wq,
26 struct submit_worker **best)
28 struct submit_worker *sw = NULL;
30 while (start <= end) {
31 sw = &wq->workers[start];
32 if (sw->flags & SW_F_IDLE)
34 if (!(*best) || sw->seq < (*best)->seq)
42 static struct submit_worker *get_submit_worker(struct workqueue *wq)
44 unsigned int next = wq->next_free_worker;
45 struct submit_worker *sw, *best = NULL;
47 assert(next < wq->max_workers);
49 sw = __get_submit_worker(wq, next, wq->max_workers - 1, &best);
51 sw = __get_submit_worker(wq, 0, next - 1, &best);
54 * No truly idle found, use best match
59 if (sw->index == wq->next_free_worker) {
60 if (sw->index + 1 < wq->max_workers)
61 wq->next_free_worker = sw->index + 1;
63 wq->next_free_worker = 0;
69 static bool all_sw_idle(struct workqueue *wq)
73 for (i = 0; i < wq->max_workers; i++) {
74 struct submit_worker *sw = &wq->workers[i];
76 if (!(sw->flags & SW_F_IDLE))
84 * Must be serialized wrt workqueue_enqueue() by caller
86 void workqueue_flush(struct workqueue *wq)
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);
100 * Must be serialized by caller. Returns true for queued, false for busy.
102 bool workqueue_enqueue(struct workqueue *wq, struct workqueue_work *work)
104 struct submit_worker *sw;
106 sw = get_submit_worker(wq);
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;
112 pthread_mutex_unlock(&sw->lock);
114 pthread_cond_signal(&sw->cond);
121 static void handle_list(struct submit_worker *sw, struct flist_head *list)
123 struct workqueue *wq = sw->wq;
124 struct workqueue_work *work;
126 while (!flist_empty(list)) {
127 work = flist_first_entry(list, struct workqueue_work, list);
128 flist_del_init(&work->list);
129 wq->ops.fn(sw, work);
133 static void *worker_thread(void *data)
135 struct submit_worker *sw = data;
136 struct workqueue *wq = sw->wq;
137 unsigned int eflags = 0, ret;
138 FLIST_HEAD(local_list);
140 ret = workqueue_init_worker(sw);
141 pthread_mutex_lock(&sw->lock);
142 sw->flags |= SW_F_RUNNING;
144 sw->flags |= SW_F_ERROR;
145 pthread_mutex_unlock(&sw->lock);
147 pthread_mutex_lock(&wq->flush_lock);
148 pthread_cond_signal(&wq->flush_cond);
149 pthread_mutex_unlock(&wq->flush_lock);
151 if (sw->flags & SW_F_ERROR)
155 pthread_mutex_lock(&sw->lock);
157 if (flist_empty(&sw->work_list)) {
158 if (sw->flags & SW_F_EXIT) {
159 pthread_mutex_unlock(&sw->lock);
163 if (workqueue_pre_sleep_check(sw)) {
164 pthread_mutex_unlock(&sw->lock);
165 workqueue_pre_sleep(sw);
166 pthread_mutex_lock(&sw->lock);
170 * We dropped and reaquired the lock, check
173 if (!flist_empty(&sw->work_list))
176 if (sw->flags & SW_F_EXIT) {
177 pthread_mutex_unlock(&sw->lock);
179 } else if (!(sw->flags & SW_F_IDLE)) {
180 sw->flags |= SW_F_IDLE;
181 wq->next_free_worker = sw->index;
183 pthread_cond_signal(&wq->flush_cond);
185 if (wq->ops.update_acct_fn)
186 wq->ops.update_acct_fn(sw);
188 pthread_cond_wait(&sw->cond, &sw->lock);
191 flist_splice_init(&sw->work_list, &local_list);
193 pthread_mutex_unlock(&sw->lock);
194 handle_list(sw, &local_list);
197 if (wq->ops.update_acct_fn)
198 wq->ops.update_acct_fn(sw);
201 pthread_mutex_lock(&sw->lock);
202 sw->flags |= (SW_F_EXITED | eflags);
203 pthread_mutex_unlock(&sw->lock);
207 static void free_worker(struct submit_worker *sw, unsigned int *sum_cnt)
209 struct workqueue *wq = sw->wq;
211 workqueue_exit_worker(sw, sum_cnt);
213 pthread_cond_destroy(&sw->cond);
214 pthread_mutex_destroy(&sw->lock);
216 if (wq->ops.free_worker_fn)
217 wq->ops.free_worker_fn(sw);
220 static void shutdown_worker(struct submit_worker *sw, unsigned int *sum_cnt)
222 pthread_join(sw->thread, NULL);
223 free_worker(sw, sum_cnt);
226 void workqueue_exit(struct workqueue *wq)
228 unsigned int shutdown, sum_cnt = 0;
229 struct submit_worker *sw;
232 for (i = 0; i < wq->max_workers; i++) {
233 sw = &wq->workers[i];
235 pthread_mutex_lock(&sw->lock);
236 sw->flags |= SW_F_EXIT;
237 pthread_cond_signal(&sw->cond);
238 pthread_mutex_unlock(&sw->lock);
243 for (i = 0; i < wq->max_workers; i++) {
244 sw = &wq->workers[i];
245 if (sw->flags & SW_F_ACCOUNTED)
247 pthread_mutex_lock(&sw->lock);
248 sw->flags |= SW_F_ACCOUNTED;
249 pthread_mutex_unlock(&sw->lock);
250 shutdown_worker(sw, &sum_cnt);
253 } while (shutdown && shutdown != wq->max_workers);
256 pthread_mutex_destroy(&wq->flush_lock);
257 pthread_cond_destroy(&wq->flush_cond);
258 pthread_mutex_destroy(&wq->stat_lock);
261 static int start_worker(struct workqueue *wq, unsigned int index)
263 struct submit_worker *sw = &wq->workers[index];
266 INIT_FLIST_HEAD(&sw->work_list);
267 pthread_cond_init(&sw->cond, NULL);
268 pthread_mutex_init(&sw->lock, NULL);
272 if (wq->ops.alloc_worker_fn) {
273 ret = wq->ops.alloc_worker_fn(sw);
278 ret = pthread_create(&sw->thread, NULL, worker_thread, sw);
280 pthread_mutex_lock(&sw->lock);
281 sw->flags = SW_F_IDLE;
282 pthread_mutex_unlock(&sw->lock);
286 free_worker(sw, NULL);
290 int workqueue_init(struct thread_data *td, struct workqueue *wq,
291 struct workqueue_ops *ops, unsigned max_pending)
293 unsigned int running;
296 wq->max_workers = max_pending;
300 wq->next_free_worker = 0;
301 pthread_cond_init(&wq->flush_cond, NULL);
302 pthread_mutex_init(&wq->flush_lock, NULL);
303 pthread_mutex_init(&wq->stat_lock, NULL);
305 wq->workers = calloc(wq->max_workers, sizeof(struct submit_worker));
307 for (i = 0; i < wq->max_workers; i++)
308 if (start_worker(wq, i))
312 if (!wq->max_workers)
316 * Wait for them all to be started and initialized
320 struct submit_worker *sw;
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)
329 if (sw->flags & SW_F_ERROR)
331 pthread_mutex_unlock(&sw->lock);
334 if (error || running == wq->max_workers) {
335 pthread_mutex_unlock(&wq->flush_lock);
339 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
340 pthread_mutex_unlock(&wq->flush_lock);
347 log_err("Can't create rate workqueue\n");
348 td_verror(td, ESRCH, "workqueue_init");