2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#include <sys/zfs_context.h>
2N/A
2N/Aint taskq_now;
2N/Ataskq_t *system_taskq;
2N/A
2N/Atypedef struct task {
2N/A struct task *task_next;
2N/A struct task *task_prev;
2N/A task_func_t *task_func;
2N/A void *task_arg;
2N/A} task_t;
2N/A
2N/A#define TASKQ_ACTIVE 0x00010000
2N/A
2N/Astruct taskq {
2N/A kmutex_t tq_lock;
2N/A krwlock_t tq_threadlock;
2N/A kcondvar_t tq_dispatch_cv;
2N/A kcondvar_t tq_wait_cv;
2N/A thread_t *tq_threadlist;
2N/A int tq_flags;
2N/A int tq_active;
2N/A int tq_nthreads;
2N/A int tq_nalloc;
2N/A int tq_minalloc;
2N/A int tq_maxalloc;
2N/A kcondvar_t tq_maxalloc_cv;
2N/A int tq_maxalloc_wait;
2N/A task_t *tq_freelist;
2N/A task_t tq_task;
2N/A};
2N/A
2N/Astatic task_t *
2N/Atask_alloc(taskq_t *tq, int tqflags)
2N/A{
2N/A task_t *t;
2N/A int rv;
2N/A
2N/Aagain: if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
2N/A tq->tq_freelist = t->task_next;
2N/A } else {
2N/A if (tq->tq_nalloc >= tq->tq_maxalloc) {
2N/A if (!(tqflags & KM_SLEEP))
2N/A return (NULL);
2N/A
2N/A /*
2N/A * We don't want to exceed tq_maxalloc, but we can't
2N/A * wait for other tasks to complete (and thus free up
2N/A * task structures) without risking deadlock with
2N/A * the caller. So, we just delay for one second
2N/A * to throttle the allocation rate. If we have tasks
2N/A * complete before one second timeout expires then
2N/A * taskq_ent_free will signal us and we will
2N/A * immediately retry the allocation.
2N/A */
2N/A tq->tq_maxalloc_wait++;
2N/A rv = cv_timedwait(&tq->tq_maxalloc_cv,
2N/A &tq->tq_lock, ddi_get_lbolt() + hz);
2N/A tq->tq_maxalloc_wait--;
2N/A if (rv > 0)
2N/A goto again; /* signaled */
2N/A }
2N/A mutex_exit(&tq->tq_lock);
2N/A
2N/A t = kmem_alloc(sizeof (task_t), tqflags);
2N/A
2N/A mutex_enter(&tq->tq_lock);
2N/A if (t != NULL)
2N/A tq->tq_nalloc++;
2N/A }
2N/A return (t);
2N/A}
2N/A
2N/Astatic void
2N/Atask_free(taskq_t *tq, task_t *t)
2N/A{
2N/A if (tq->tq_nalloc <= tq->tq_minalloc) {
2N/A t->task_next = tq->tq_freelist;
2N/A tq->tq_freelist = t;
2N/A } else {
2N/A tq->tq_nalloc--;
2N/A mutex_exit(&tq->tq_lock);
2N/A kmem_free(t, sizeof (task_t));
2N/A mutex_enter(&tq->tq_lock);
2N/A }
2N/A
2N/A if (tq->tq_maxalloc_wait)
2N/A cv_signal(&tq->tq_maxalloc_cv);
2N/A}
2N/A
2N/Ataskqid_t
2N/Ataskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
2N/A{
2N/A task_t *t;
2N/A
2N/A if (taskq_now) {
2N/A func(arg);
2N/A return (1);
2N/A }
2N/A
2N/A mutex_enter(&tq->tq_lock);
2N/A ASSERT(tq->tq_flags & TASKQ_ACTIVE);
2N/A if ((t = task_alloc(tq, tqflags)) == NULL) {
2N/A mutex_exit(&tq->tq_lock);
2N/A return (0);
2N/A }
2N/A if (tqflags & TQ_FRONT) {
2N/A t->task_next = tq->tq_task.task_next;
2N/A t->task_prev = &tq->tq_task;
2N/A } else {
2N/A t->task_next = &tq->tq_task;
2N/A t->task_prev = tq->tq_task.task_prev;
2N/A }
2N/A t->task_next->task_prev = t;
2N/A t->task_prev->task_next = t;
2N/A t->task_func = func;
2N/A t->task_arg = arg;
2N/A cv_signal(&tq->tq_dispatch_cv);
2N/A mutex_exit(&tq->tq_lock);
2N/A return (1);
2N/A}
2N/A
2N/Avoid
2N/Ataskq_wait(taskq_t *tq)
2N/A{
2N/A mutex_enter(&tq->tq_lock);
2N/A while (tq->tq_task.task_next != &tq->tq_task || tq->tq_active != 0)
2N/A cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
2N/A mutex_exit(&tq->tq_lock);
2N/A}
2N/A
2N/Astatic void *
2N/Ataskq_thread(void *arg)
2N/A{
2N/A taskq_t *tq = arg;
2N/A task_t *t;
2N/A
2N/A mutex_enter(&tq->tq_lock);
2N/A while (tq->tq_flags & TASKQ_ACTIVE) {
2N/A if ((t = tq->tq_task.task_next) == &tq->tq_task) {
2N/A if (--tq->tq_active == 0)
2N/A cv_broadcast(&tq->tq_wait_cv);
2N/A cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
2N/A tq->tq_active++;
2N/A continue;
2N/A }
2N/A t->task_prev->task_next = t->task_next;
2N/A t->task_next->task_prev = t->task_prev;
2N/A mutex_exit(&tq->tq_lock);
2N/A
2N/A rw_enter(&tq->tq_threadlock, RW_READER);
2N/A t->task_func(t->task_arg);
2N/A rw_exit(&tq->tq_threadlock);
2N/A
2N/A mutex_enter(&tq->tq_lock);
2N/A task_free(tq, t);
2N/A }
2N/A tq->tq_nthreads--;
2N/A cv_broadcast(&tq->tq_wait_cv);
2N/A mutex_exit(&tq->tq_lock);
2N/A return (NULL);
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Ataskq_t *
2N/Ataskq_create(const char *name, int nthreads, pri_t pri,
2N/A int minalloc, int maxalloc, uint_t flags)
2N/A{
2N/A taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
2N/A int t;
2N/A
2N/A if (flags & TASKQ_THREADS_CPU_PCT) {
2N/A int pct;
2N/A ASSERT3S(nthreads, >=, 0);
2N/A ASSERT3S(nthreads, <=, 100);
2N/A pct = MIN(nthreads, 100);
2N/A pct = MAX(pct, 0);
2N/A
2N/A nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100;
2N/A nthreads = MAX(nthreads, 1); /* need at least 1 thread */
2N/A } else {
2N/A ASSERT3S(nthreads, >=, 1);
2N/A }
2N/A
2N/A rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
2N/A mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL);
2N/A cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL);
2N/A cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL);
2N/A cv_init(&tq->tq_maxalloc_cv, NULL, CV_DEFAULT, NULL);
2N/A tq->tq_flags = flags | TASKQ_ACTIVE;
2N/A tq->tq_active = nthreads;
2N/A tq->tq_nthreads = nthreads;
2N/A tq->tq_minalloc = minalloc;
2N/A tq->tq_maxalloc = maxalloc;
2N/A tq->tq_task.task_next = &tq->tq_task;
2N/A tq->tq_task.task_prev = &tq->tq_task;
2N/A tq->tq_threadlist = kmem_alloc(nthreads * sizeof (thread_t), KM_SLEEP);
2N/A
2N/A if (flags & TASKQ_PREPOPULATE) {
2N/A mutex_enter(&tq->tq_lock);
2N/A while (minalloc-- > 0)
2N/A task_free(tq, task_alloc(tq, KM_SLEEP));
2N/A mutex_exit(&tq->tq_lock);
2N/A }
2N/A
2N/A for (t = 0; t < nthreads; t++)
2N/A (void) thr_create(0, 0, taskq_thread,
2N/A tq, THR_BOUND, &tq->tq_threadlist[t]);
2N/A
2N/A return (tq);
2N/A}
2N/A
2N/Avoid
2N/Ataskq_destroy(taskq_t *tq)
2N/A{
2N/A int t;
2N/A int nthreads = tq->tq_nthreads;
2N/A
2N/A taskq_wait(tq);
2N/A
2N/A mutex_enter(&tq->tq_lock);
2N/A
2N/A tq->tq_flags &= ~TASKQ_ACTIVE;
2N/A cv_broadcast(&tq->tq_dispatch_cv);
2N/A
2N/A while (tq->tq_nthreads != 0)
2N/A cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
2N/A
2N/A tq->tq_minalloc = 0;
2N/A while (tq->tq_nalloc != 0) {
2N/A ASSERT(tq->tq_freelist != NULL);
2N/A task_free(tq, task_alloc(tq, KM_SLEEP));
2N/A }
2N/A
2N/A mutex_exit(&tq->tq_lock);
2N/A
2N/A for (t = 0; t < nthreads; t++)
2N/A (void) thr_join(tq->tq_threadlist[t], NULL, NULL);
2N/A
2N/A kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t));
2N/A
2N/A rw_destroy(&tq->tq_threadlock);
2N/A mutex_destroy(&tq->tq_lock);
2N/A cv_destroy(&tq->tq_dispatch_cv);
2N/A cv_destroy(&tq->tq_wait_cv);
2N/A cv_destroy(&tq->tq_maxalloc_cv);
2N/A
2N/A kmem_free(tq, sizeof (taskq_t));
2N/A}
2N/A
2N/Aint
2N/Ataskq_member(taskq_t *tq, void *t)
2N/A{
2N/A int i;
2N/A
2N/A if (taskq_now)
2N/A return (1);
2N/A
2N/A for (i = 0; i < tq->tq_nthreads; i++)
2N/A if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t)
2N/A return (1);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Avoid
2N/Asystem_taskq_init(void)
2N/A{
2N/A system_taskq = taskq_create("system_taskq", 64, minclsyspri, 4, 512,
2N/A TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
2N/A}
2N/A
2N/Avoid
2N/Asystem_taskq_fini(void)
2N/A{
2N/A taskq_destroy(system_taskq);
2N/A system_taskq = NULL; /* defensive */
2N/A}