| 1 | /* |
| 2 | * Generic workqueue offload mechanism |
| 3 | * |
| 4 | * Copyright (C) 2015 Jens Axboe <axboe@kernel.dk> |
| 5 | * |
| 6 | */ |
| 7 | #include <unistd.h> |
| 8 | |
| 9 | #include "fio.h" |
| 10 | #include "flist.h" |
| 11 | #include "workqueue.h" |
| 12 | #include "smalloc.h" |
| 13 | #include "pshared.h" |
| 14 | |
| 15 | enum { |
| 16 | SW_F_IDLE = 1 << 0, |
| 17 | SW_F_RUNNING = 1 << 1, |
| 18 | SW_F_EXIT = 1 << 2, |
| 19 | SW_F_ACCOUNTED = 1 << 3, |
| 20 | SW_F_ERROR = 1 << 4, |
| 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 | |
| 69 | static bool all_sw_idle(struct workqueue *wq) |
| 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)) |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Must be serialized wrt workqueue_enqueue() by caller |
| 85 | */ |
| 86 | void workqueue_flush(struct workqueue *wq) |
| 87 | { |
| 88 | pthread_mutex_lock(&wq->flush_lock); |
| 89 | wq->wake_idle = 1; |
| 90 | |
| 91 | while (!all_sw_idle(wq)) |
| 92 | pthread_cond_wait(&wq->flush_cond, &wq->flush_lock); |
| 93 | |
| 94 | wq->wake_idle = 0; |
| 95 | pthread_mutex_unlock(&wq->flush_lock); |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Must be serialized by caller. |
| 100 | */ |
| 101 | void workqueue_enqueue(struct workqueue *wq, struct workqueue_work *work) |
| 102 | { |
| 103 | struct submit_worker *sw; |
| 104 | |
| 105 | sw = get_submit_worker(wq); |
| 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; |
| 112 | |
| 113 | pthread_cond_signal(&sw->cond); |
| 114 | pthread_mutex_unlock(&sw->lock); |
| 115 | } |
| 116 | |
| 117 | static void handle_list(struct submit_worker *sw, struct flist_head *list) |
| 118 | { |
| 119 | struct workqueue *wq = sw->wq; |
| 120 | struct workqueue_work *work; |
| 121 | |
| 122 | while (!flist_empty(list)) { |
| 123 | work = flist_first_entry(list, struct workqueue_work, list); |
| 124 | flist_del_init(&work->list); |
| 125 | wq->ops.fn(sw, work); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | static void *worker_thread(void *data) |
| 130 | { |
| 131 | struct submit_worker *sw = data; |
| 132 | struct workqueue *wq = sw->wq; |
| 133 | unsigned int ret = 0; |
| 134 | FLIST_HEAD(local_list); |
| 135 | |
| 136 | sk_out_assign(sw->sk_out); |
| 137 | |
| 138 | if (wq->ops.nice) { |
| 139 | if (nice(wq->ops.nice) < 0) { |
| 140 | log_err("workqueue: nice %s\n", strerror(errno)); |
| 141 | ret = 1; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (!ret) |
| 146 | ret = workqueue_init_worker(sw); |
| 147 | |
| 148 | pthread_mutex_lock(&sw->lock); |
| 149 | sw->flags |= SW_F_RUNNING; |
| 150 | if (ret) |
| 151 | sw->flags |= SW_F_ERROR; |
| 152 | pthread_mutex_unlock(&sw->lock); |
| 153 | |
| 154 | pthread_mutex_lock(&wq->flush_lock); |
| 155 | pthread_cond_signal(&wq->flush_cond); |
| 156 | pthread_mutex_unlock(&wq->flush_lock); |
| 157 | |
| 158 | if (sw->flags & SW_F_ERROR) |
| 159 | goto done; |
| 160 | |
| 161 | pthread_mutex_lock(&sw->lock); |
| 162 | while (1) { |
| 163 | if (flist_empty(&sw->work_list)) { |
| 164 | if (sw->flags & SW_F_EXIT) { |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | if (workqueue_pre_sleep_check(sw)) { |
| 169 | pthread_mutex_unlock(&sw->lock); |
| 170 | workqueue_pre_sleep(sw); |
| 171 | pthread_mutex_lock(&sw->lock); |
| 172 | } |
| 173 | } |
| 174 | /* |
| 175 | * We may have dropped and reaquired the lock, check state |
| 176 | * again. |
| 177 | */ |
| 178 | if (flist_empty(&sw->work_list)) { |
| 179 | if (sw->flags & SW_F_EXIT) { |
| 180 | break; |
| 181 | } |
| 182 | if (!(sw->flags & SW_F_IDLE)) { |
| 183 | sw->flags |= SW_F_IDLE; |
| 184 | wq->next_free_worker = sw->index; |
| 185 | pthread_mutex_unlock(&sw->lock); |
| 186 | pthread_mutex_lock(&wq->flush_lock); |
| 187 | if (wq->wake_idle) |
| 188 | pthread_cond_signal(&wq->flush_cond); |
| 189 | pthread_mutex_unlock(&wq->flush_lock); |
| 190 | pthread_mutex_lock(&sw->lock); |
| 191 | } |
| 192 | } |
| 193 | if (flist_empty(&sw->work_list)) { |
| 194 | if (sw->flags & SW_F_EXIT) { |
| 195 | break; |
| 196 | } |
| 197 | pthread_cond_wait(&sw->cond, &sw->lock); |
| 198 | } else { |
| 199 | flist_splice_init(&sw->work_list, &local_list); |
| 200 | } |
| 201 | pthread_mutex_unlock(&sw->lock); |
| 202 | handle_list(sw, &local_list); |
| 203 | if (wq->ops.update_acct_fn) |
| 204 | wq->ops.update_acct_fn(sw); |
| 205 | pthread_mutex_lock(&sw->lock); |
| 206 | } |
| 207 | pthread_mutex_unlock(&sw->lock); |
| 208 | |
| 209 | done: |
| 210 | sk_out_drop(); |
| 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | static void free_worker(struct submit_worker *sw, unsigned int *sum_cnt) |
| 215 | { |
| 216 | struct workqueue *wq = sw->wq; |
| 217 | |
| 218 | workqueue_exit_worker(sw, sum_cnt); |
| 219 | |
| 220 | pthread_cond_destroy(&sw->cond); |
| 221 | pthread_mutex_destroy(&sw->lock); |
| 222 | |
| 223 | if (wq->ops.free_worker_fn) |
| 224 | wq->ops.free_worker_fn(sw); |
| 225 | } |
| 226 | |
| 227 | static void shutdown_worker(struct submit_worker *sw, unsigned int *sum_cnt) |
| 228 | { |
| 229 | pthread_join(sw->thread, NULL); |
| 230 | free_worker(sw, sum_cnt); |
| 231 | } |
| 232 | |
| 233 | void workqueue_exit(struct workqueue *wq) |
| 234 | { |
| 235 | unsigned int shutdown, sum_cnt = 0; |
| 236 | struct submit_worker *sw; |
| 237 | int i; |
| 238 | |
| 239 | if (!wq->workers) |
| 240 | return; |
| 241 | |
| 242 | for (i = 0; i < wq->max_workers; i++) { |
| 243 | sw = &wq->workers[i]; |
| 244 | |
| 245 | pthread_mutex_lock(&sw->lock); |
| 246 | sw->flags |= SW_F_EXIT; |
| 247 | pthread_cond_signal(&sw->cond); |
| 248 | pthread_mutex_unlock(&sw->lock); |
| 249 | } |
| 250 | |
| 251 | do { |
| 252 | shutdown = 0; |
| 253 | for (i = 0; i < wq->max_workers; i++) { |
| 254 | sw = &wq->workers[i]; |
| 255 | if (sw->flags & SW_F_ACCOUNTED) |
| 256 | continue; |
| 257 | pthread_mutex_lock(&sw->lock); |
| 258 | sw->flags |= SW_F_ACCOUNTED; |
| 259 | pthread_mutex_unlock(&sw->lock); |
| 260 | shutdown_worker(sw, &sum_cnt); |
| 261 | shutdown++; |
| 262 | } |
| 263 | } while (shutdown && shutdown != wq->max_workers); |
| 264 | |
| 265 | sfree(wq->workers); |
| 266 | wq->workers = NULL; |
| 267 | pthread_mutex_destroy(&wq->flush_lock); |
| 268 | pthread_cond_destroy(&wq->flush_cond); |
| 269 | pthread_mutex_destroy(&wq->stat_lock); |
| 270 | } |
| 271 | |
| 272 | static int start_worker(struct workqueue *wq, unsigned int index, |
| 273 | struct sk_out *sk_out) |
| 274 | { |
| 275 | struct submit_worker *sw = &wq->workers[index]; |
| 276 | int ret; |
| 277 | |
| 278 | INIT_FLIST_HEAD(&sw->work_list); |
| 279 | |
| 280 | ret = mutex_cond_init_pshared(&sw->lock, &sw->cond); |
| 281 | if (ret) |
| 282 | return ret; |
| 283 | |
| 284 | sw->wq = wq; |
| 285 | sw->index = index; |
| 286 | sw->sk_out = sk_out; |
| 287 | |
| 288 | if (wq->ops.alloc_worker_fn) { |
| 289 | ret = wq->ops.alloc_worker_fn(sw); |
| 290 | if (ret) |
| 291 | return ret; |
| 292 | } |
| 293 | |
| 294 | ret = pthread_create(&sw->thread, NULL, worker_thread, sw); |
| 295 | if (!ret) { |
| 296 | pthread_mutex_lock(&sw->lock); |
| 297 | sw->flags = SW_F_IDLE; |
| 298 | pthread_mutex_unlock(&sw->lock); |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | free_worker(sw, NULL); |
| 303 | return 1; |
| 304 | } |
| 305 | |
| 306 | int workqueue_init(struct thread_data *td, struct workqueue *wq, |
| 307 | struct workqueue_ops *ops, unsigned int max_workers, |
| 308 | struct sk_out *sk_out) |
| 309 | { |
| 310 | unsigned int running; |
| 311 | int i, error; |
| 312 | int ret; |
| 313 | |
| 314 | wq->max_workers = max_workers; |
| 315 | wq->td = td; |
| 316 | wq->ops = *ops; |
| 317 | wq->work_seq = 0; |
| 318 | wq->next_free_worker = 0; |
| 319 | |
| 320 | ret = mutex_cond_init_pshared(&wq->flush_lock, &wq->flush_cond); |
| 321 | if (ret) |
| 322 | goto err; |
| 323 | ret = mutex_init_pshared(&wq->stat_lock); |
| 324 | if (ret) |
| 325 | goto err; |
| 326 | |
| 327 | wq->workers = smalloc(wq->max_workers * sizeof(struct submit_worker)); |
| 328 | if (!wq->workers) |
| 329 | goto err; |
| 330 | |
| 331 | for (i = 0; i < wq->max_workers; i++) |
| 332 | if (start_worker(wq, i, sk_out)) |
| 333 | break; |
| 334 | |
| 335 | wq->max_workers = i; |
| 336 | if (!wq->max_workers) |
| 337 | goto err; |
| 338 | |
| 339 | /* |
| 340 | * Wait for them all to be started and initialized |
| 341 | */ |
| 342 | error = 0; |
| 343 | pthread_mutex_lock(&wq->flush_lock); |
| 344 | do { |
| 345 | struct submit_worker *sw; |
| 346 | |
| 347 | running = 0; |
| 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 | break; |
| 360 | |
| 361 | pthread_cond_wait(&wq->flush_cond, &wq->flush_lock); |
| 362 | } while (1); |
| 363 | pthread_mutex_unlock(&wq->flush_lock); |
| 364 | |
| 365 | if (!error) |
| 366 | return 0; |
| 367 | |
| 368 | err: |
| 369 | log_err("Can't create rate workqueue\n"); |
| 370 | td_verror(td, ESRCH, "workqueue_init"); |
| 371 | workqueue_exit(wq); |
| 372 | return 1; |
| 373 | } |