xref: /titanic_51/usr/src/lib/libzpool/common/taskq.c (revision 738f37bc3dcd61e8a893af0f2d466d76690b70ec)
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 /*
2264109744SChris Horne  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
255aeb9474SGarrett D'Amore /*
265aeb9474SGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
27aa846ad9SGarrett D'Amore  * Copyright 2012 Garrett D'Amore <garrett@damore.org>.  All rights reserved.
28*738f37bcSGeorge Wilson  * Copyright (c) 2014 by Delphix. All rights reserved.
295aeb9474SGarrett D'Amore  */
30fa9e4066Sahrens 
31fa9e4066Sahrens #include <sys/zfs_context.h>
32fa9e4066Sahrens 
33fa9e4066Sahrens int taskq_now;
3488b7b0f2SMatthew Ahrens taskq_t *system_taskq;
35fa9e4066Sahrens 
36fa9e4066Sahrens #define	TASKQ_ACTIVE	0x00010000
37*738f37bcSGeorge Wilson #define	TASKQ_NAMELEN	31
38fa9e4066Sahrens 
39fa9e4066Sahrens struct taskq {
40*738f37bcSGeorge Wilson 	char		tq_name[TASKQ_NAMELEN + 1];
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;
5264109744SChris Horne 	kcondvar_t	tq_maxalloc_cv;
5364109744SChris Horne 	int		tq_maxalloc_wait;
545aeb9474SGarrett D'Amore 	taskq_ent_t	*tq_freelist;
555aeb9474SGarrett D'Amore 	taskq_ent_t	tq_task;
56fa9e4066Sahrens };
57fa9e4066Sahrens 
585aeb9474SGarrett D'Amore static taskq_ent_t *
59fa9e4066Sahrens task_alloc(taskq_t *tq, int tqflags)
60fa9e4066Sahrens {
615aeb9474SGarrett D'Amore 	taskq_ent_t *t;
6264109744SChris Horne 	int rv;
63fa9e4066Sahrens 
6464109744SChris Horne again:	if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
655aeb9474SGarrett D'Amore 		tq->tq_freelist = t->tqent_next;
66fa9e4066Sahrens 	} else {
67fa9e4066Sahrens 		if (tq->tq_nalloc >= tq->tq_maxalloc) {
6864109744SChris Horne 			if (!(tqflags & KM_SLEEP))
69fa9e4066Sahrens 				return (NULL);
7064109744SChris Horne 
71fa9e4066Sahrens 			/*
72fa9e4066Sahrens 			 * We don't want to exceed tq_maxalloc, but we can't
73fa9e4066Sahrens 			 * wait for other tasks to complete (and thus free up
74fa9e4066Sahrens 			 * task structures) without risking deadlock with
75fa9e4066Sahrens 			 * the caller.  So, we just delay for one second
7664109744SChris Horne 			 * to throttle the allocation rate. If we have tasks
7764109744SChris Horne 			 * complete before one second timeout expires then
7864109744SChris Horne 			 * taskq_ent_free will signal us and we will
7964109744SChris Horne 			 * immediately retry the allocation.
80fa9e4066Sahrens 			 */
8164109744SChris Horne 			tq->tq_maxalloc_wait++;
8264109744SChris Horne 			rv = cv_timedwait(&tq->tq_maxalloc_cv,
8364109744SChris Horne 			    &tq->tq_lock, ddi_get_lbolt() + hz);
8464109744SChris Horne 			tq->tq_maxalloc_wait--;
8564109744SChris Horne 			if (rv > 0)
8664109744SChris Horne 				goto again;		/* signaled */
87fa9e4066Sahrens 		}
8864109744SChris Horne 		mutex_exit(&tq->tq_lock);
8964109744SChris Horne 
905aeb9474SGarrett D'Amore 		t = kmem_alloc(sizeof (taskq_ent_t), tqflags);
9164109744SChris Horne 
92fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
93fa9e4066Sahrens 		if (t != NULL)
94fa9e4066Sahrens 			tq->tq_nalloc++;
95fa9e4066Sahrens 	}
96fa9e4066Sahrens 	return (t);
97fa9e4066Sahrens }
98fa9e4066Sahrens 
99fa9e4066Sahrens static void
1005aeb9474SGarrett D'Amore task_free(taskq_t *tq, taskq_ent_t *t)
101fa9e4066Sahrens {
102fa9e4066Sahrens 	if (tq->tq_nalloc <= tq->tq_minalloc) {
1035aeb9474SGarrett D'Amore 		t->tqent_next = tq->tq_freelist;
104fa9e4066Sahrens 		tq->tq_freelist = t;
105fa9e4066Sahrens 	} else {
106fa9e4066Sahrens 		tq->tq_nalloc--;
107fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
1085aeb9474SGarrett D'Amore 		kmem_free(t, sizeof (taskq_ent_t));
109fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
110fa9e4066Sahrens 	}
11164109744SChris Horne 
11264109744SChris Horne 	if (tq->tq_maxalloc_wait)
11364109744SChris Horne 		cv_signal(&tq->tq_maxalloc_cv);
114fa9e4066Sahrens }
115fa9e4066Sahrens 
116fa9e4066Sahrens taskqid_t
117fa9e4066Sahrens taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
118fa9e4066Sahrens {
1195aeb9474SGarrett D'Amore 	taskq_ent_t *t;
120fa9e4066Sahrens 
121fa9e4066Sahrens 	if (taskq_now) {
122fa9e4066Sahrens 		func(arg);
123fa9e4066Sahrens 		return (1);
124fa9e4066Sahrens 	}
125fa9e4066Sahrens 
126fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
127fa9e4066Sahrens 	ASSERT(tq->tq_flags & TASKQ_ACTIVE);
128fa9e4066Sahrens 	if ((t = task_alloc(tq, tqflags)) == NULL) {
129fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
130fa9e4066Sahrens 		return (0);
131fa9e4066Sahrens 	}
13235a5a358SJonathan Adams 	if (tqflags & TQ_FRONT) {
1335aeb9474SGarrett D'Amore 		t->tqent_next = tq->tq_task.tqent_next;
1345aeb9474SGarrett D'Amore 		t->tqent_prev = &tq->tq_task;
13535a5a358SJonathan Adams 	} else {
1365aeb9474SGarrett D'Amore 		t->tqent_next = &tq->tq_task;
1375aeb9474SGarrett D'Amore 		t->tqent_prev = tq->tq_task.tqent_prev;
13835a5a358SJonathan Adams 	}
1395aeb9474SGarrett D'Amore 	t->tqent_next->tqent_prev = t;
1405aeb9474SGarrett D'Amore 	t->tqent_prev->tqent_next = t;
1415aeb9474SGarrett D'Amore 	t->tqent_func = func;
1425aeb9474SGarrett D'Amore 	t->tqent_arg = arg;
143aa846ad9SGarrett D'Amore 	t->tqent_flags = 0;
144fa9e4066Sahrens 	cv_signal(&tq->tq_dispatch_cv);
145fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
146fa9e4066Sahrens 	return (1);
147fa9e4066Sahrens }
148fa9e4066Sahrens 
149fa9e4066Sahrens void
1505aeb9474SGarrett D'Amore taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
1515aeb9474SGarrett D'Amore     taskq_ent_t *t)
1525aeb9474SGarrett D'Amore {
1535aeb9474SGarrett D'Amore 	ASSERT(func != NULL);
1545aeb9474SGarrett D'Amore 	ASSERT(!(tq->tq_flags & TASKQ_DYNAMIC));
1555aeb9474SGarrett D'Amore 
1565aeb9474SGarrett D'Amore 	/*
1575aeb9474SGarrett D'Amore 	 * Mark it as a prealloc'd task.  This is important
1585aeb9474SGarrett D'Amore 	 * to ensure that we don't free it later.
1595aeb9474SGarrett D'Amore 	 */
1605aeb9474SGarrett D'Amore 	t->tqent_flags |= TQENT_FLAG_PREALLOC;
1615aeb9474SGarrett D'Amore 	/*
1625aeb9474SGarrett D'Amore 	 * Enqueue the task to the underlying queue.
1635aeb9474SGarrett D'Amore 	 */
1645aeb9474SGarrett D'Amore 	mutex_enter(&tq->tq_lock);
1655aeb9474SGarrett D'Amore 
1665aeb9474SGarrett D'Amore 	if (flags & TQ_FRONT) {
1675aeb9474SGarrett D'Amore 		t->tqent_next = tq->tq_task.tqent_next;
1685aeb9474SGarrett D'Amore 		t->tqent_prev = &tq->tq_task;
1695aeb9474SGarrett D'Amore 	} else {
1705aeb9474SGarrett D'Amore 		t->tqent_next = &tq->tq_task;
1715aeb9474SGarrett D'Amore 		t->tqent_prev = tq->tq_task.tqent_prev;
1725aeb9474SGarrett D'Amore 	}
1735aeb9474SGarrett D'Amore 	t->tqent_next->tqent_prev = t;
1745aeb9474SGarrett D'Amore 	t->tqent_prev->tqent_next = t;
1755aeb9474SGarrett D'Amore 	t->tqent_func = func;
1765aeb9474SGarrett D'Amore 	t->tqent_arg = arg;
1775aeb9474SGarrett D'Amore 	cv_signal(&tq->tq_dispatch_cv);
1785aeb9474SGarrett D'Amore 	mutex_exit(&tq->tq_lock);
1795aeb9474SGarrett D'Amore }
1805aeb9474SGarrett D'Amore 
1815aeb9474SGarrett D'Amore void
182fa9e4066Sahrens taskq_wait(taskq_t *tq)
183fa9e4066Sahrens {
184fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
1855aeb9474SGarrett D'Amore 	while (tq->tq_task.tqent_next != &tq->tq_task || tq->tq_active != 0)
186fa9e4066Sahrens 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
187fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
188fa9e4066Sahrens }
189fa9e4066Sahrens 
190fa9e4066Sahrens static void *
191fa9e4066Sahrens taskq_thread(void *arg)
192fa9e4066Sahrens {
193fa9e4066Sahrens 	taskq_t *tq = arg;
1945aeb9474SGarrett D'Amore 	taskq_ent_t *t;
1955aeb9474SGarrett D'Amore 	boolean_t prealloc;
196fa9e4066Sahrens 
197fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
198fa9e4066Sahrens 	while (tq->tq_flags & TASKQ_ACTIVE) {
1995aeb9474SGarrett D'Amore 		if ((t = tq->tq_task.tqent_next) == &tq->tq_task) {
200fa9e4066Sahrens 			if (--tq->tq_active == 0)
201fa9e4066Sahrens 				cv_broadcast(&tq->tq_wait_cv);
202fa9e4066Sahrens 			cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
203fa9e4066Sahrens 			tq->tq_active++;
204fa9e4066Sahrens 			continue;
205fa9e4066Sahrens 		}
2065aeb9474SGarrett D'Amore 		t->tqent_prev->tqent_next = t->tqent_next;
2075aeb9474SGarrett D'Amore 		t->tqent_next->tqent_prev = t->tqent_prev;
2085aeb9474SGarrett D'Amore 		t->tqent_next = NULL;
2095aeb9474SGarrett D'Amore 		t->tqent_prev = NULL;
2105aeb9474SGarrett D'Amore 		prealloc = t->tqent_flags & TQENT_FLAG_PREALLOC;
211fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
212fa9e4066Sahrens 
213fa9e4066Sahrens 		rw_enter(&tq->tq_threadlock, RW_READER);
2145aeb9474SGarrett D'Amore 		t->tqent_func(t->tqent_arg);
215fa9e4066Sahrens 		rw_exit(&tq->tq_threadlock);
216fa9e4066Sahrens 
217fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
2185aeb9474SGarrett D'Amore 		if (!prealloc)
219fa9e4066Sahrens 			task_free(tq, t);
220fa9e4066Sahrens 	}
221fa9e4066Sahrens 	tq->tq_nthreads--;
222fa9e4066Sahrens 	cv_broadcast(&tq->tq_wait_cv);
223fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
224fa9e4066Sahrens 	return (NULL);
225fa9e4066Sahrens }
226fa9e4066Sahrens 
227fa9e4066Sahrens /*ARGSUSED*/
228fa9e4066Sahrens taskq_t *
229fa9e4066Sahrens taskq_create(const char *name, int nthreads, pri_t pri,
230fa9e4066Sahrens 	int minalloc, int maxalloc, uint_t flags)
231fa9e4066Sahrens {
232fa9e4066Sahrens 	taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
233fa9e4066Sahrens 	int t;
234fa9e4066Sahrens 
2352e0c549eSJonathan Adams 	if (flags & TASKQ_THREADS_CPU_PCT) {
2362e0c549eSJonathan Adams 		int pct;
2372e0c549eSJonathan Adams 		ASSERT3S(nthreads, >=, 0);
2382e0c549eSJonathan Adams 		ASSERT3S(nthreads, <=, 100);
2392e0c549eSJonathan Adams 		pct = MIN(nthreads, 100);
2402e0c549eSJonathan Adams 		pct = MAX(pct, 0);
2412e0c549eSJonathan Adams 
2422e0c549eSJonathan Adams 		nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100;
2432e0c549eSJonathan Adams 		nthreads = MAX(nthreads, 1);	/* need at least 1 thread */
2442e0c549eSJonathan Adams 	} else {
2452e0c549eSJonathan Adams 		ASSERT3S(nthreads, >=, 1);
2462e0c549eSJonathan Adams 	}
2472e0c549eSJonathan Adams 
248fa9e4066Sahrens 	rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
249c25056deSgw25295 	mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL);
250c25056deSgw25295 	cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL);
251c25056deSgw25295 	cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL);
25264109744SChris Horne 	cv_init(&tq->tq_maxalloc_cv, NULL, CV_DEFAULT, NULL);
253*738f37bcSGeorge Wilson 	(void) strncpy(tq->tq_name, name, TASKQ_NAMELEN + 1);
254fa9e4066Sahrens 	tq->tq_flags = flags | TASKQ_ACTIVE;
255fa9e4066Sahrens 	tq->tq_active = nthreads;
256fa9e4066Sahrens 	tq->tq_nthreads = nthreads;
257fa9e4066Sahrens 	tq->tq_minalloc = minalloc;
258fa9e4066Sahrens 	tq->tq_maxalloc = maxalloc;
2595aeb9474SGarrett D'Amore 	tq->tq_task.tqent_next = &tq->tq_task;
2605aeb9474SGarrett D'Amore 	tq->tq_task.tqent_prev = &tq->tq_task;
261fa9e4066Sahrens 	tq->tq_threadlist = kmem_alloc(nthreads * sizeof (thread_t), KM_SLEEP);
262fa9e4066Sahrens 
263fa9e4066Sahrens 	if (flags & TASKQ_PREPOPULATE) {
264fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
265fa9e4066Sahrens 		while (minalloc-- > 0)
266fa9e4066Sahrens 			task_free(tq, task_alloc(tq, KM_SLEEP));
267fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
268fa9e4066Sahrens 	}
269fa9e4066Sahrens 
270fa9e4066Sahrens 	for (t = 0; t < nthreads; t++)
271fa9e4066Sahrens 		(void) thr_create(0, 0, taskq_thread,
272fa9e4066Sahrens 		    tq, THR_BOUND, &tq->tq_threadlist[t]);
273fa9e4066Sahrens 
274fa9e4066Sahrens 	return (tq);
275fa9e4066Sahrens }
276fa9e4066Sahrens 
277fa9e4066Sahrens void
278fa9e4066Sahrens taskq_destroy(taskq_t *tq)
279fa9e4066Sahrens {
280fa9e4066Sahrens 	int t;
281fa9e4066Sahrens 	int nthreads = tq->tq_nthreads;
282fa9e4066Sahrens 
283fa9e4066Sahrens 	taskq_wait(tq);
284fa9e4066Sahrens 
285fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
286fa9e4066Sahrens 
287fa9e4066Sahrens 	tq->tq_flags &= ~TASKQ_ACTIVE;
288fa9e4066Sahrens 	cv_broadcast(&tq->tq_dispatch_cv);
289fa9e4066Sahrens 
290fa9e4066Sahrens 	while (tq->tq_nthreads != 0)
291fa9e4066Sahrens 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
292fa9e4066Sahrens 
293fa9e4066Sahrens 	tq->tq_minalloc = 0;
294fa9e4066Sahrens 	while (tq->tq_nalloc != 0) {
295fa9e4066Sahrens 		ASSERT(tq->tq_freelist != NULL);
296fa9e4066Sahrens 		task_free(tq, task_alloc(tq, KM_SLEEP));
297fa9e4066Sahrens 	}
298fa9e4066Sahrens 
299fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
300fa9e4066Sahrens 
301fa9e4066Sahrens 	for (t = 0; t < nthreads; t++)
302fa9e4066Sahrens 		(void) thr_join(tq->tq_threadlist[t], NULL, NULL);
303fa9e4066Sahrens 
304fa9e4066Sahrens 	kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t));
305fa9e4066Sahrens 
306fa9e4066Sahrens 	rw_destroy(&tq->tq_threadlock);
307c25056deSgw25295 	mutex_destroy(&tq->tq_lock);
308c25056deSgw25295 	cv_destroy(&tq->tq_dispatch_cv);
309c25056deSgw25295 	cv_destroy(&tq->tq_wait_cv);
31064109744SChris Horne 	cv_destroy(&tq->tq_maxalloc_cv);
311fa9e4066Sahrens 
312fa9e4066Sahrens 	kmem_free(tq, sizeof (taskq_t));
313fa9e4066Sahrens }
314fa9e4066Sahrens 
315fa9e4066Sahrens int
316fa9e4066Sahrens taskq_member(taskq_t *tq, void *t)
317fa9e4066Sahrens {
318fa9e4066Sahrens 	int i;
319fa9e4066Sahrens 
320fa9e4066Sahrens 	if (taskq_now)
321fa9e4066Sahrens 		return (1);
322fa9e4066Sahrens 
323fa9e4066Sahrens 	for (i = 0; i < tq->tq_nthreads; i++)
324fa9e4066Sahrens 		if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t)
325fa9e4066Sahrens 			return (1);
326fa9e4066Sahrens 
327fa9e4066Sahrens 	return (0);
328fa9e4066Sahrens }
32988b7b0f2SMatthew Ahrens 
33088b7b0f2SMatthew Ahrens void
33188b7b0f2SMatthew Ahrens system_taskq_init(void)
33288b7b0f2SMatthew Ahrens {
33388b7b0f2SMatthew Ahrens 	system_taskq = taskq_create("system_taskq", 64, minclsyspri, 4, 512,
33488b7b0f2SMatthew Ahrens 	    TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
33588b7b0f2SMatthew Ahrens }
336d20e665cSRicardo M. Correia 
337d20e665cSRicardo M. Correia void
338d20e665cSRicardo M. Correia system_taskq_fini(void)
339d20e665cSRicardo M. Correia {
340d20e665cSRicardo M. Correia 	taskq_destroy(system_taskq);
341d20e665cSRicardo M. Correia 	system_taskq = NULL; /* defensive */
342d20e665cSRicardo M. Correia }
343