1fa9e4066Sahrens /* 2fa9e4066Sahrens * CDDL HEADER START 3fa9e4066Sahrens * 4fa9e4066Sahrens * The contents of this file are subject to the terms of the 5c25056deSgw25295 * Common Development and Distribution License (the "License"). 6c25056deSgw25295 * You may not use this file except in compliance with the License. 7fa9e4066Sahrens * 8fa9e4066Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fa9e4066Sahrens * or http://www.opensolaris.org/os/licensing. 10fa9e4066Sahrens * See the License for the specific language governing permissions 11fa9e4066Sahrens * and limitations under the License. 12fa9e4066Sahrens * 13fa9e4066Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14fa9e4066Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fa9e4066Sahrens * If applicable, add the following below this CDDL HEADER, with the 16fa9e4066Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17fa9e4066Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18fa9e4066Sahrens * 19fa9e4066Sahrens * CDDL HEADER END 20fa9e4066Sahrens */ 21fa9e4066Sahrens /* 222e0c549eSJonathan Adams * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23fa9e4066Sahrens * Use is subject to license terms. 24fa9e4066Sahrens */ 25fa9e4066Sahrens 26fa9e4066Sahrens #include <sys/zfs_context.h> 27fa9e4066Sahrens 28fa9e4066Sahrens int taskq_now; 2988b7b0f2SMatthew Ahrens taskq_t *system_taskq; 30fa9e4066Sahrens 31fa9e4066Sahrens typedef struct task { 32fa9e4066Sahrens struct task *task_next; 33fa9e4066Sahrens struct task *task_prev; 34fa9e4066Sahrens task_func_t *task_func; 35fa9e4066Sahrens void *task_arg; 36fa9e4066Sahrens } task_t; 37fa9e4066Sahrens 38fa9e4066Sahrens #define TASKQ_ACTIVE 0x00010000 39fa9e4066Sahrens 40fa9e4066Sahrens struct taskq { 41fa9e4066Sahrens kmutex_t tq_lock; 42fa9e4066Sahrens krwlock_t tq_threadlock; 43fa9e4066Sahrens kcondvar_t tq_dispatch_cv; 44fa9e4066Sahrens kcondvar_t tq_wait_cv; 45fa9e4066Sahrens thread_t *tq_threadlist; 46fa9e4066Sahrens int tq_flags; 47fa9e4066Sahrens int tq_active; 48fa9e4066Sahrens int tq_nthreads; 49fa9e4066Sahrens int tq_nalloc; 50fa9e4066Sahrens int tq_minalloc; 51fa9e4066Sahrens int tq_maxalloc; 52fa9e4066Sahrens task_t *tq_freelist; 53fa9e4066Sahrens task_t tq_task; 54fa9e4066Sahrens }; 55fa9e4066Sahrens 56fa9e4066Sahrens static task_t * 57fa9e4066Sahrens task_alloc(taskq_t *tq, int tqflags) 58fa9e4066Sahrens { 59fa9e4066Sahrens task_t *t; 60fa9e4066Sahrens 61fa9e4066Sahrens if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) { 62fa9e4066Sahrens tq->tq_freelist = t->task_next; 63fa9e4066Sahrens } else { 64fa9e4066Sahrens mutex_exit(&tq->tq_lock); 65fa9e4066Sahrens if (tq->tq_nalloc >= tq->tq_maxalloc) { 66fa9e4066Sahrens if (!(tqflags & KM_SLEEP)) { 67fa9e4066Sahrens mutex_enter(&tq->tq_lock); 68fa9e4066Sahrens return (NULL); 69fa9e4066Sahrens } 70fa9e4066Sahrens /* 71fa9e4066Sahrens * We don't want to exceed tq_maxalloc, but we can't 72fa9e4066Sahrens * wait for other tasks to complete (and thus free up 73fa9e4066Sahrens * task structures) without risking deadlock with 74fa9e4066Sahrens * the caller. So, we just delay for one second 75fa9e4066Sahrens * to throttle the allocation rate. 76fa9e4066Sahrens */ 77fa9e4066Sahrens delay(hz); 78fa9e4066Sahrens } 79fa9e4066Sahrens t = kmem_alloc(sizeof (task_t), tqflags); 80fa9e4066Sahrens mutex_enter(&tq->tq_lock); 81fa9e4066Sahrens if (t != NULL) 82fa9e4066Sahrens tq->tq_nalloc++; 83fa9e4066Sahrens } 84fa9e4066Sahrens return (t); 85fa9e4066Sahrens } 86fa9e4066Sahrens 87fa9e4066Sahrens static void 88fa9e4066Sahrens task_free(taskq_t *tq, task_t *t) 89fa9e4066Sahrens { 90fa9e4066Sahrens if (tq->tq_nalloc <= tq->tq_minalloc) { 91fa9e4066Sahrens t->task_next = tq->tq_freelist; 92fa9e4066Sahrens tq->tq_freelist = t; 93fa9e4066Sahrens } else { 94fa9e4066Sahrens tq->tq_nalloc--; 95fa9e4066Sahrens mutex_exit(&tq->tq_lock); 96fa9e4066Sahrens kmem_free(t, sizeof (task_t)); 97fa9e4066Sahrens mutex_enter(&tq->tq_lock); 98fa9e4066Sahrens } 99fa9e4066Sahrens } 100fa9e4066Sahrens 101fa9e4066Sahrens taskqid_t 102fa9e4066Sahrens taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags) 103fa9e4066Sahrens { 104fa9e4066Sahrens task_t *t; 105fa9e4066Sahrens 106fa9e4066Sahrens if (taskq_now) { 107fa9e4066Sahrens func(arg); 108fa9e4066Sahrens return (1); 109fa9e4066Sahrens } 110fa9e4066Sahrens 111fa9e4066Sahrens mutex_enter(&tq->tq_lock); 112fa9e4066Sahrens ASSERT(tq->tq_flags & TASKQ_ACTIVE); 113fa9e4066Sahrens if ((t = task_alloc(tq, tqflags)) == NULL) { 114fa9e4066Sahrens mutex_exit(&tq->tq_lock); 115fa9e4066Sahrens return (0); 116fa9e4066Sahrens } 117*35a5a358SJonathan Adams if (tqflags & TQ_FRONT) { 118*35a5a358SJonathan Adams t->task_next = tq->tq_task.task_next; 119*35a5a358SJonathan Adams t->task_prev = &tq->tq_task; 120*35a5a358SJonathan Adams } else { 121fa9e4066Sahrens t->task_next = &tq->tq_task; 122fa9e4066Sahrens t->task_prev = tq->tq_task.task_prev; 123*35a5a358SJonathan Adams } 124fa9e4066Sahrens t->task_next->task_prev = t; 125fa9e4066Sahrens t->task_prev->task_next = t; 126fa9e4066Sahrens t->task_func = func; 127fa9e4066Sahrens t->task_arg = arg; 128fa9e4066Sahrens cv_signal(&tq->tq_dispatch_cv); 129fa9e4066Sahrens mutex_exit(&tq->tq_lock); 130fa9e4066Sahrens return (1); 131fa9e4066Sahrens } 132fa9e4066Sahrens 133fa9e4066Sahrens void 134fa9e4066Sahrens taskq_wait(taskq_t *tq) 135fa9e4066Sahrens { 136fa9e4066Sahrens mutex_enter(&tq->tq_lock); 137fa9e4066Sahrens while (tq->tq_task.task_next != &tq->tq_task || tq->tq_active != 0) 138fa9e4066Sahrens cv_wait(&tq->tq_wait_cv, &tq->tq_lock); 139fa9e4066Sahrens mutex_exit(&tq->tq_lock); 140fa9e4066Sahrens } 141fa9e4066Sahrens 142fa9e4066Sahrens static void * 143fa9e4066Sahrens taskq_thread(void *arg) 144fa9e4066Sahrens { 145fa9e4066Sahrens taskq_t *tq = arg; 146fa9e4066Sahrens task_t *t; 147fa9e4066Sahrens 148fa9e4066Sahrens mutex_enter(&tq->tq_lock); 149fa9e4066Sahrens while (tq->tq_flags & TASKQ_ACTIVE) { 150fa9e4066Sahrens if ((t = tq->tq_task.task_next) == &tq->tq_task) { 151fa9e4066Sahrens if (--tq->tq_active == 0) 152fa9e4066Sahrens cv_broadcast(&tq->tq_wait_cv); 153fa9e4066Sahrens cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock); 154fa9e4066Sahrens tq->tq_active++; 155fa9e4066Sahrens continue; 156fa9e4066Sahrens } 157fa9e4066Sahrens t->task_prev->task_next = t->task_next; 158fa9e4066Sahrens t->task_next->task_prev = t->task_prev; 159fa9e4066Sahrens mutex_exit(&tq->tq_lock); 160fa9e4066Sahrens 161fa9e4066Sahrens rw_enter(&tq->tq_threadlock, RW_READER); 162fa9e4066Sahrens t->task_func(t->task_arg); 163fa9e4066Sahrens rw_exit(&tq->tq_threadlock); 164fa9e4066Sahrens 165fa9e4066Sahrens mutex_enter(&tq->tq_lock); 166fa9e4066Sahrens task_free(tq, t); 167fa9e4066Sahrens } 168fa9e4066Sahrens tq->tq_nthreads--; 169fa9e4066Sahrens cv_broadcast(&tq->tq_wait_cv); 170fa9e4066Sahrens mutex_exit(&tq->tq_lock); 171fa9e4066Sahrens return (NULL); 172fa9e4066Sahrens } 173fa9e4066Sahrens 174fa9e4066Sahrens /*ARGSUSED*/ 175fa9e4066Sahrens taskq_t * 176fa9e4066Sahrens taskq_create(const char *name, int nthreads, pri_t pri, 177fa9e4066Sahrens int minalloc, int maxalloc, uint_t flags) 178fa9e4066Sahrens { 179fa9e4066Sahrens taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP); 180fa9e4066Sahrens int t; 181fa9e4066Sahrens 1822e0c549eSJonathan Adams if (flags & TASKQ_THREADS_CPU_PCT) { 1832e0c549eSJonathan Adams int pct; 1842e0c549eSJonathan Adams ASSERT3S(nthreads, >=, 0); 1852e0c549eSJonathan Adams ASSERT3S(nthreads, <=, 100); 1862e0c549eSJonathan Adams pct = MIN(nthreads, 100); 1872e0c549eSJonathan Adams pct = MAX(pct, 0); 1882e0c549eSJonathan Adams 1892e0c549eSJonathan Adams nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100; 1902e0c549eSJonathan Adams nthreads = MAX(nthreads, 1); /* need at least 1 thread */ 1912e0c549eSJonathan Adams } else { 1922e0c549eSJonathan Adams ASSERT3S(nthreads, >=, 1); 1932e0c549eSJonathan Adams } 1942e0c549eSJonathan Adams 195fa9e4066Sahrens rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL); 196c25056deSgw25295 mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL); 197c25056deSgw25295 cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL); 198c25056deSgw25295 cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL); 199fa9e4066Sahrens tq->tq_flags = flags | TASKQ_ACTIVE; 200fa9e4066Sahrens tq->tq_active = nthreads; 201fa9e4066Sahrens tq->tq_nthreads = nthreads; 202fa9e4066Sahrens tq->tq_minalloc = minalloc; 203fa9e4066Sahrens tq->tq_maxalloc = maxalloc; 204fa9e4066Sahrens tq->tq_task.task_next = &tq->tq_task; 205fa9e4066Sahrens tq->tq_task.task_prev = &tq->tq_task; 206fa9e4066Sahrens tq->tq_threadlist = kmem_alloc(nthreads * sizeof (thread_t), KM_SLEEP); 207fa9e4066Sahrens 208fa9e4066Sahrens if (flags & TASKQ_PREPOPULATE) { 209fa9e4066Sahrens mutex_enter(&tq->tq_lock); 210fa9e4066Sahrens while (minalloc-- > 0) 211fa9e4066Sahrens task_free(tq, task_alloc(tq, KM_SLEEP)); 212fa9e4066Sahrens mutex_exit(&tq->tq_lock); 213fa9e4066Sahrens } 214fa9e4066Sahrens 215fa9e4066Sahrens for (t = 0; t < nthreads; t++) 216fa9e4066Sahrens (void) thr_create(0, 0, taskq_thread, 217fa9e4066Sahrens tq, THR_BOUND, &tq->tq_threadlist[t]); 218fa9e4066Sahrens 219fa9e4066Sahrens return (tq); 220fa9e4066Sahrens } 221fa9e4066Sahrens 222fa9e4066Sahrens void 223fa9e4066Sahrens taskq_destroy(taskq_t *tq) 224fa9e4066Sahrens { 225fa9e4066Sahrens int t; 226fa9e4066Sahrens int nthreads = tq->tq_nthreads; 227fa9e4066Sahrens 228fa9e4066Sahrens taskq_wait(tq); 229fa9e4066Sahrens 230fa9e4066Sahrens mutex_enter(&tq->tq_lock); 231fa9e4066Sahrens 232fa9e4066Sahrens tq->tq_flags &= ~TASKQ_ACTIVE; 233fa9e4066Sahrens cv_broadcast(&tq->tq_dispatch_cv); 234fa9e4066Sahrens 235fa9e4066Sahrens while (tq->tq_nthreads != 0) 236fa9e4066Sahrens cv_wait(&tq->tq_wait_cv, &tq->tq_lock); 237fa9e4066Sahrens 238fa9e4066Sahrens tq->tq_minalloc = 0; 239fa9e4066Sahrens while (tq->tq_nalloc != 0) { 240fa9e4066Sahrens ASSERT(tq->tq_freelist != NULL); 241fa9e4066Sahrens task_free(tq, task_alloc(tq, KM_SLEEP)); 242fa9e4066Sahrens } 243fa9e4066Sahrens 244fa9e4066Sahrens mutex_exit(&tq->tq_lock); 245fa9e4066Sahrens 246fa9e4066Sahrens for (t = 0; t < nthreads; t++) 247fa9e4066Sahrens (void) thr_join(tq->tq_threadlist[t], NULL, NULL); 248fa9e4066Sahrens 249fa9e4066Sahrens kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t)); 250fa9e4066Sahrens 251fa9e4066Sahrens rw_destroy(&tq->tq_threadlock); 252c25056deSgw25295 mutex_destroy(&tq->tq_lock); 253c25056deSgw25295 cv_destroy(&tq->tq_dispatch_cv); 254c25056deSgw25295 cv_destroy(&tq->tq_wait_cv); 255fa9e4066Sahrens 256fa9e4066Sahrens kmem_free(tq, sizeof (taskq_t)); 257fa9e4066Sahrens } 258fa9e4066Sahrens 259fa9e4066Sahrens int 260fa9e4066Sahrens taskq_member(taskq_t *tq, void *t) 261fa9e4066Sahrens { 262fa9e4066Sahrens int i; 263fa9e4066Sahrens 264fa9e4066Sahrens if (taskq_now) 265fa9e4066Sahrens return (1); 266fa9e4066Sahrens 267fa9e4066Sahrens for (i = 0; i < tq->tq_nthreads; i++) 268fa9e4066Sahrens if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t) 269fa9e4066Sahrens return (1); 270fa9e4066Sahrens 271fa9e4066Sahrens return (0); 272fa9e4066Sahrens } 27388b7b0f2SMatthew Ahrens 27488b7b0f2SMatthew Ahrens void 27588b7b0f2SMatthew Ahrens system_taskq_init(void) 27688b7b0f2SMatthew Ahrens { 27788b7b0f2SMatthew Ahrens system_taskq = taskq_create("system_taskq", 64, minclsyspri, 4, 512, 27888b7b0f2SMatthew Ahrens TASKQ_DYNAMIC | TASKQ_PREPOPULATE); 27988b7b0f2SMatthew Ahrens } 280d20e665cSRicardo M. Correia 281d20e665cSRicardo M. Correia void 282d20e665cSRicardo M. Correia system_taskq_fini(void) 283d20e665cSRicardo M. Correia { 284d20e665cSRicardo M. Correia taskq_destroy(system_taskq); 285d20e665cSRicardo M. Correia system_taskq = NULL; /* defensive */ 286d20e665cSRicardo M. Correia } 287