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 void workqueue_enqueue(struct workqueue *wq, struct workqueue_work *work)
104 struct submit_worker *sw;
106 sw = get_submit_worker(wq);
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);
115 pthread_cond_signal(&sw->cond);
118 static void handle_list(struct submit_worker *sw, struct flist_head *list)
120 struct workqueue *wq = sw->wq;
121 struct workqueue_work *work;
123 while (!flist_empty(list)) {
124 work = flist_first_entry(list, struct workqueue_work, list);
125 flist_del_init(&work->list);
126 wq->ops.fn(sw, work);
130 static void *worker_thread(void *data)
132 struct submit_worker *sw = data;
133 struct workqueue *wq = sw->wq;
134 unsigned int eflags = 0, ret = 0;
135 FLIST_HEAD(local_list);
138 if (nice(wq->ops.nice) < 0) {
139 log_err("workqueue: nice %s\n", strerror(errno));
145 ret = workqueue_init_worker(sw);
147 pthread_mutex_lock(&sw->lock);
148 sw->flags |= SW_F_RUNNING;
150 sw->flags |= SW_F_ERROR;
151 pthread_mutex_unlock(&sw->lock);
153 pthread_mutex_lock(&wq->flush_lock);
154 pthread_cond_signal(&wq->flush_cond);
155 pthread_mutex_unlock(&wq->flush_lock);
157 if (sw->flags & SW_F_ERROR)
161 pthread_mutex_lock(&sw->lock);
163 if (flist_empty(&sw->work_list)) {
164 if (sw->flags & SW_F_EXIT) {
165 pthread_mutex_unlock(&sw->lock);
169 if (workqueue_pre_sleep_check(sw)) {
170 pthread_mutex_unlock(&sw->lock);
171 workqueue_pre_sleep(sw);
172 pthread_mutex_lock(&sw->lock);
176 * We dropped and reaquired the lock, check
179 if (!flist_empty(&sw->work_list))
182 if (sw->flags & SW_F_EXIT) {
183 pthread_mutex_unlock(&sw->lock);
185 } else if (!(sw->flags & SW_F_IDLE)) {
186 sw->flags |= SW_F_IDLE;
187 wq->next_free_worker = sw->index;
189 pthread_cond_signal(&wq->flush_cond);
191 if (wq->ops.update_acct_fn)
192 wq->ops.update_acct_fn(sw);
194 pthread_cond_wait(&sw->cond, &sw->lock);
197 flist_splice_init(&sw->work_list, &local_list);
199 pthread_mutex_unlock(&sw->lock);
200 handle_list(sw, &local_list);
203 if (wq->ops.update_acct_fn)
204 wq->ops.update_acct_fn(sw);
207 pthread_mutex_lock(&sw->lock);
208 sw->flags |= (SW_F_EXITED | eflags);
209 pthread_mutex_unlock(&sw->lock);
213 static void free_worker(struct submit_worker *sw, unsigned int *sum_cnt)
215 struct workqueue *wq = sw->wq;
217 workqueue_exit_worker(sw, sum_cnt);
219 pthread_cond_destroy(&sw->cond);
220 pthread_mutex_destroy(&sw->lock);
222 if (wq->ops.free_worker_fn)
223 wq->ops.free_worker_fn(sw);
226 static void shutdown_worker(struct submit_worker *sw, unsigned int *sum_cnt)
228 pthread_join(sw->thread, NULL);
229 free_worker(sw, sum_cnt);
232 void workqueue_exit(struct workqueue *wq)
234 unsigned int shutdown, sum_cnt = 0;
235 struct submit_worker *sw;
238 for (i = 0; i < wq->max_workers; i++) {
239 sw = &wq->workers[i];
241 pthread_mutex_lock(&sw->lock);
242 sw->flags |= SW_F_EXIT;
243 pthread_cond_signal(&sw->cond);
244 pthread_mutex_unlock(&sw->lock);
249 for (i = 0; i < wq->max_workers; i++) {
250 sw = &wq->workers[i];
251 if (sw->flags & SW_F_ACCOUNTED)
253 pthread_mutex_lock(&sw->lock);
254 sw->flags |= SW_F_ACCOUNTED;
255 pthread_mutex_unlock(&sw->lock);
256 shutdown_worker(sw, &sum_cnt);
259 } while (shutdown && shutdown != wq->max_workers);
262 pthread_mutex_destroy(&wq->flush_lock);
263 pthread_cond_destroy(&wq->flush_cond);
264 pthread_mutex_destroy(&wq->stat_lock);
267 static int start_worker(struct workqueue *wq, unsigned int index)
269 struct submit_worker *sw = &wq->workers[index];
272 INIT_FLIST_HEAD(&sw->work_list);
273 pthread_cond_init(&sw->cond, NULL);
274 pthread_mutex_init(&sw->lock, NULL);
278 if (wq->ops.alloc_worker_fn) {
279 ret = wq->ops.alloc_worker_fn(sw);
284 ret = pthread_create(&sw->thread, NULL, worker_thread, sw);
286 pthread_mutex_lock(&sw->lock);
287 sw->flags = SW_F_IDLE;
288 pthread_mutex_unlock(&sw->lock);
292 free_worker(sw, NULL);
296 int workqueue_init(struct thread_data *td, struct workqueue *wq,
297 struct workqueue_ops *ops, unsigned max_pending)
299 unsigned int running;
302 wq->max_workers = max_pending;
306 wq->next_free_worker = 0;
307 pthread_cond_init(&wq->flush_cond, NULL);
308 pthread_mutex_init(&wq->flush_lock, NULL);
309 pthread_mutex_init(&wq->stat_lock, NULL);
311 wq->workers = calloc(wq->max_workers, sizeof(struct submit_worker));
313 for (i = 0; i < wq->max_workers; i++)
314 if (start_worker(wq, i))
318 if (!wq->max_workers)
322 * Wait for them all to be started and initialized
326 struct submit_worker *sw;
329 pthread_mutex_lock(&wq->flush_lock);
330 for (i = 0; i < wq->max_workers; i++) {
331 sw = &wq->workers[i];
332 pthread_mutex_lock(&sw->lock);
333 if (sw->flags & SW_F_RUNNING)
335 if (sw->flags & SW_F_ERROR)
337 pthread_mutex_unlock(&sw->lock);
340 if (error || running == wq->max_workers) {
341 pthread_mutex_unlock(&wq->flush_lock);
345 pthread_cond_wait(&wq->flush_cond, &wq->flush_lock);
346 pthread_mutex_unlock(&wq->flush_lock);
353 log_err("Can't create rate workqueue\n");
354 td_verror(td, ESRCH, "workqueue_init");