xref: /titanic_51/usr/src/lib/libzpool/common/taskq.c (revision 5aeb94743e3be0c51e86f73096334611ae3a058e)
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  */
25*5aeb9474SGarrett D'Amore /*
26*5aeb9474SGarrett D'Amore  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
27*5aeb9474SGarrett D'Amore  */
28fa9e4066Sahrens 
29fa9e4066Sahrens #include <sys/zfs_context.h>
30fa9e4066Sahrens 
31fa9e4066Sahrens int taskq_now;
3288b7b0f2SMatthew Ahrens taskq_t *system_taskq;
33fa9e4066Sahrens 
34fa9e4066Sahrens #define	TASKQ_ACTIVE	0x00010000
35fa9e4066Sahrens 
36fa9e4066Sahrens struct taskq {
37fa9e4066Sahrens 	kmutex_t	tq_lock;
38fa9e4066Sahrens 	krwlock_t	tq_threadlock;
39fa9e4066Sahrens 	kcondvar_t	tq_dispatch_cv;
40fa9e4066Sahrens 	kcondvar_t	tq_wait_cv;
41fa9e4066Sahrens 	thread_t	*tq_threadlist;
42fa9e4066Sahrens 	int		tq_flags;
43fa9e4066Sahrens 	int		tq_active;
44fa9e4066Sahrens 	int		tq_nthreads;
45fa9e4066Sahrens 	int		tq_nalloc;
46fa9e4066Sahrens 	int		tq_minalloc;
47fa9e4066Sahrens 	int		tq_maxalloc;
4864109744SChris Horne 	kcondvar_t	tq_maxalloc_cv;
4964109744SChris Horne 	int		tq_maxalloc_wait;
50*5aeb9474SGarrett D'Amore 	taskq_ent_t	*tq_freelist;
51*5aeb9474SGarrett D'Amore 	taskq_ent_t	tq_task;
52fa9e4066Sahrens };
53fa9e4066Sahrens 
54*5aeb9474SGarrett D'Amore static taskq_ent_t *
55fa9e4066Sahrens task_alloc(taskq_t *tq, int tqflags)
56fa9e4066Sahrens {
57*5aeb9474SGarrett D'Amore 	taskq_ent_t *t;
5864109744SChris Horne 	int rv;
59fa9e4066Sahrens 
6064109744SChris Horne again:	if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
61*5aeb9474SGarrett D'Amore 		tq->tq_freelist = t->tqent_next;
62fa9e4066Sahrens 	} else {
63fa9e4066Sahrens 		if (tq->tq_nalloc >= tq->tq_maxalloc) {
6464109744SChris Horne 			if (!(tqflags & KM_SLEEP))
65fa9e4066Sahrens 				return (NULL);
6664109744SChris Horne 
67fa9e4066Sahrens 			/*
68fa9e4066Sahrens 			 * We don't want to exceed tq_maxalloc, but we can't
69fa9e4066Sahrens 			 * wait for other tasks to complete (and thus free up
70fa9e4066Sahrens 			 * task structures) without risking deadlock with
71fa9e4066Sahrens 			 * the caller.  So, we just delay for one second
7264109744SChris Horne 			 * to throttle the allocation rate. If we have tasks
7364109744SChris Horne 			 * complete before one second timeout expires then
7464109744SChris Horne 			 * taskq_ent_free will signal us and we will
7564109744SChris Horne 			 * immediately retry the allocation.
76fa9e4066Sahrens 			 */
7764109744SChris Horne 			tq->tq_maxalloc_wait++;
7864109744SChris Horne 			rv = cv_timedwait(&tq->tq_maxalloc_cv,
7964109744SChris Horne 			    &tq->tq_lock, ddi_get_lbolt() + hz);
8064109744SChris Horne 			tq->tq_maxalloc_wait--;
8164109744SChris Horne 			if (rv > 0)
8264109744SChris Horne 				goto again;		/* signaled */
83fa9e4066Sahrens 		}
8464109744SChris Horne 		mutex_exit(&tq->tq_lock);
8564109744SChris Horne 
86*5aeb9474SGarrett D'Amore 		t = kmem_alloc(sizeof (taskq_ent_t), tqflags);
8764109744SChris Horne 
88fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
89fa9e4066Sahrens 		if (t != NULL)
90fa9e4066Sahrens 			tq->tq_nalloc++;
91fa9e4066Sahrens 	}
92fa9e4066Sahrens 	return (t);
93fa9e4066Sahrens }
94fa9e4066Sahrens 
95fa9e4066Sahrens static void
96*5aeb9474SGarrett D'Amore task_free(taskq_t *tq, taskq_ent_t *t)
97fa9e4066Sahrens {
98fa9e4066Sahrens 	if (tq->tq_nalloc <= tq->tq_minalloc) {
99*5aeb9474SGarrett D'Amore 		t->tqent_next = tq->tq_freelist;
100fa9e4066Sahrens 		tq->tq_freelist = t;
101fa9e4066Sahrens 	} else {
102fa9e4066Sahrens 		tq->tq_nalloc--;
103fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
104*5aeb9474SGarrett D'Amore 		kmem_free(t, sizeof (taskq_ent_t));
105fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
106fa9e4066Sahrens 	}
10764109744SChris Horne 
10864109744SChris Horne 	if (tq->tq_maxalloc_wait)
10964109744SChris Horne 		cv_signal(&tq->tq_maxalloc_cv);
110fa9e4066Sahrens }
111fa9e4066Sahrens 
112fa9e4066Sahrens taskqid_t
113fa9e4066Sahrens taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
114fa9e4066Sahrens {
115*5aeb9474SGarrett D'Amore 	taskq_ent_t *t;
116fa9e4066Sahrens 
117fa9e4066Sahrens 	if (taskq_now) {
118fa9e4066Sahrens 		func(arg);
119fa9e4066Sahrens 		return (1);
120fa9e4066Sahrens 	}
121fa9e4066Sahrens 
122fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
123fa9e4066Sahrens 	ASSERT(tq->tq_flags & TASKQ_ACTIVE);
124fa9e4066Sahrens 	if ((t = task_alloc(tq, tqflags)) == NULL) {
125fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
126fa9e4066Sahrens 		return (0);
127fa9e4066Sahrens 	}
12835a5a358SJonathan Adams 	if (tqflags & TQ_FRONT) {
129*5aeb9474SGarrett D'Amore 		t->tqent_next = tq->tq_task.tqent_next;
130*5aeb9474SGarrett D'Amore 		t->tqent_prev = &tq->tq_task;
13135a5a358SJonathan Adams 	} else {
132*5aeb9474SGarrett D'Amore 		t->tqent_next = &tq->tq_task;
133*5aeb9474SGarrett D'Amore 		t->tqent_prev = tq->tq_task.tqent_prev;
13435a5a358SJonathan Adams 	}
135*5aeb9474SGarrett D'Amore 	t->tqent_next->tqent_prev = t;
136*5aeb9474SGarrett D'Amore 	t->tqent_prev->tqent_next = t;
137*5aeb9474SGarrett D'Amore 	t->tqent_func = func;
138*5aeb9474SGarrett D'Amore 	t->tqent_arg = arg;
139fa9e4066Sahrens 	cv_signal(&tq->tq_dispatch_cv);
140fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
141fa9e4066Sahrens 	return (1);
142fa9e4066Sahrens }
143fa9e4066Sahrens 
144fa9e4066Sahrens void
145*5aeb9474SGarrett D'Amore taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
146*5aeb9474SGarrett D'Amore     taskq_ent_t *t)
147*5aeb9474SGarrett D'Amore {
148*5aeb9474SGarrett D'Amore 	ASSERT(func != NULL);
149*5aeb9474SGarrett D'Amore 	ASSERT(!(tq->tq_flags & TASKQ_DYNAMIC));
150*5aeb9474SGarrett D'Amore 
151*5aeb9474SGarrett D'Amore 	/*
152*5aeb9474SGarrett D'Amore 	 * Mark it as a prealloc'd task.  This is important
153*5aeb9474SGarrett D'Amore 	 * to ensure that we don't free it later.
154*5aeb9474SGarrett D'Amore 	 */
155*5aeb9474SGarrett D'Amore 	t->tqent_flags |= TQENT_FLAG_PREALLOC;
156*5aeb9474SGarrett D'Amore 	/*
157*5aeb9474SGarrett D'Amore 	 * Enqueue the task to the underlying queue.
158*5aeb9474SGarrett D'Amore 	 */
159*5aeb9474SGarrett D'Amore 	mutex_enter(&tq->tq_lock);
160*5aeb9474SGarrett D'Amore 
161*5aeb9474SGarrett D'Amore 	if (flags & TQ_FRONT) {
162*5aeb9474SGarrett D'Amore 		t->tqent_next = tq->tq_task.tqent_next;
163*5aeb9474SGarrett D'Amore 		t->tqent_prev = &tq->tq_task;
164*5aeb9474SGarrett D'Amore 	} else {
165*5aeb9474SGarrett D'Amore 		t->tqent_next = &tq->tq_task;
166*5aeb9474SGarrett D'Amore 		t->tqent_prev = tq->tq_task.tqent_prev;
167*5aeb9474SGarrett D'Amore 	}
168*5aeb9474SGarrett D'Amore 	t->tqent_next->tqent_prev = t;
169*5aeb9474SGarrett D'Amore 	t->tqent_prev->tqent_next = t;
170*5aeb9474SGarrett D'Amore 	t->tqent_func = func;
171*5aeb9474SGarrett D'Amore 	t->tqent_arg = arg;
172*5aeb9474SGarrett D'Amore 	cv_signal(&tq->tq_dispatch_cv);
173*5aeb9474SGarrett D'Amore 	mutex_exit(&tq->tq_lock);
174*5aeb9474SGarrett D'Amore }
175*5aeb9474SGarrett D'Amore 
176*5aeb9474SGarrett D'Amore void
177fa9e4066Sahrens taskq_wait(taskq_t *tq)
178fa9e4066Sahrens {
179fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
180*5aeb9474SGarrett D'Amore 	while (tq->tq_task.tqent_next != &tq->tq_task || tq->tq_active != 0)
181fa9e4066Sahrens 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
182fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
183fa9e4066Sahrens }
184fa9e4066Sahrens 
185fa9e4066Sahrens static void *
186fa9e4066Sahrens taskq_thread(void *arg)
187fa9e4066Sahrens {
188fa9e4066Sahrens 	taskq_t *tq = arg;
189*5aeb9474SGarrett D'Amore 	taskq_ent_t *t;
190*5aeb9474SGarrett D'Amore 	boolean_t prealloc;
191fa9e4066Sahrens 
192fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
193fa9e4066Sahrens 	while (tq->tq_flags & TASKQ_ACTIVE) {
194*5aeb9474SGarrett D'Amore 		if ((t = tq->tq_task.tqent_next) == &tq->tq_task) {
195fa9e4066Sahrens 			if (--tq->tq_active == 0)
196fa9e4066Sahrens 				cv_broadcast(&tq->tq_wait_cv);
197fa9e4066Sahrens 			cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
198fa9e4066Sahrens 			tq->tq_active++;
199fa9e4066Sahrens 			continue;
200fa9e4066Sahrens 		}
201*5aeb9474SGarrett D'Amore 		t->tqent_prev->tqent_next = t->tqent_next;
202*5aeb9474SGarrett D'Amore 		t->tqent_next->tqent_prev = t->tqent_prev;
203*5aeb9474SGarrett D'Amore 		t->tqent_next = NULL;
204*5aeb9474SGarrett D'Amore 		t->tqent_prev = NULL;
205*5aeb9474SGarrett D'Amore 		prealloc = t->tqent_flags & TQENT_FLAG_PREALLOC;
206fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
207fa9e4066Sahrens 
208fa9e4066Sahrens 		rw_enter(&tq->tq_threadlock, RW_READER);
209*5aeb9474SGarrett D'Amore 		t->tqent_func(t->tqent_arg);
210fa9e4066Sahrens 		rw_exit(&tq->tq_threadlock);
211fa9e4066Sahrens 
212fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
213*5aeb9474SGarrett D'Amore 		if (!prealloc)
214fa9e4066Sahrens 			task_free(tq, t);
215fa9e4066Sahrens 	}
216fa9e4066Sahrens 	tq->tq_nthreads--;
217fa9e4066Sahrens 	cv_broadcast(&tq->tq_wait_cv);
218fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
219fa9e4066Sahrens 	return (NULL);
220fa9e4066Sahrens }
221fa9e4066Sahrens 
222fa9e4066Sahrens /*ARGSUSED*/
223fa9e4066Sahrens taskq_t *
224fa9e4066Sahrens taskq_create(const char *name, int nthreads, pri_t pri,
225fa9e4066Sahrens 	int minalloc, int maxalloc, uint_t flags)
226fa9e4066Sahrens {
227fa9e4066Sahrens 	taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
228fa9e4066Sahrens 	int t;
229fa9e4066Sahrens 
2302e0c549eSJonathan Adams 	if (flags & TASKQ_THREADS_CPU_PCT) {
2312e0c549eSJonathan Adams 		int pct;
2322e0c549eSJonathan Adams 		ASSERT3S(nthreads, >=, 0);
2332e0c549eSJonathan Adams 		ASSERT3S(nthreads, <=, 100);
2342e0c549eSJonathan Adams 		pct = MIN(nthreads, 100);
2352e0c549eSJonathan Adams 		pct = MAX(pct, 0);
2362e0c549eSJonathan Adams 
2372e0c549eSJonathan Adams 		nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100;
2382e0c549eSJonathan Adams 		nthreads = MAX(nthreads, 1);	/* need at least 1 thread */
2392e0c549eSJonathan Adams 	} else {
2402e0c549eSJonathan Adams 		ASSERT3S(nthreads, >=, 1);
2412e0c549eSJonathan Adams 	}
2422e0c549eSJonathan Adams 
243fa9e4066Sahrens 	rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
244c25056deSgw25295 	mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL);
245c25056deSgw25295 	cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL);
246c25056deSgw25295 	cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL);
24764109744SChris Horne 	cv_init(&tq->tq_maxalloc_cv, NULL, CV_DEFAULT, NULL);
248fa9e4066Sahrens 	tq->tq_flags = flags | TASKQ_ACTIVE;
249fa9e4066Sahrens 	tq->tq_active = nthreads;
250fa9e4066Sahrens 	tq->tq_nthreads = nthreads;
251fa9e4066Sahrens 	tq->tq_minalloc = minalloc;
252fa9e4066Sahrens 	tq->tq_maxalloc = maxalloc;
253*5aeb9474SGarrett D'Amore 	tq->tq_task.tqent_next = &tq->tq_task;
254*5aeb9474SGarrett D'Amore 	tq->tq_task.tqent_prev = &tq->tq_task;
255fa9e4066Sahrens 	tq->tq_threadlist = kmem_alloc(nthreads * sizeof (thread_t), KM_SLEEP);
256fa9e4066Sahrens 
257fa9e4066Sahrens 	if (flags & TASKQ_PREPOPULATE) {
258fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
259fa9e4066Sahrens 		while (minalloc-- > 0)
260fa9e4066Sahrens 			task_free(tq, task_alloc(tq, KM_SLEEP));
261fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
262fa9e4066Sahrens 	}
263fa9e4066Sahrens 
264fa9e4066Sahrens 	for (t = 0; t < nthreads; t++)
265fa9e4066Sahrens 		(void) thr_create(0, 0, taskq_thread,
266fa9e4066Sahrens 		    tq, THR_BOUND, &tq->tq_threadlist[t]);
267fa9e4066Sahrens 
268fa9e4066Sahrens 	return (tq);
269fa9e4066Sahrens }
270fa9e4066Sahrens 
271fa9e4066Sahrens void
272fa9e4066Sahrens taskq_destroy(taskq_t *tq)
273fa9e4066Sahrens {
274fa9e4066Sahrens 	int t;
275fa9e4066Sahrens 	int nthreads = tq->tq_nthreads;
276fa9e4066Sahrens 
277fa9e4066Sahrens 	taskq_wait(tq);
278fa9e4066Sahrens 
279fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
280fa9e4066Sahrens 
281fa9e4066Sahrens 	tq->tq_flags &= ~TASKQ_ACTIVE;
282fa9e4066Sahrens 	cv_broadcast(&tq->tq_dispatch_cv);
283fa9e4066Sahrens 
284fa9e4066Sahrens 	while (tq->tq_nthreads != 0)
285fa9e4066Sahrens 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
286fa9e4066Sahrens 
287fa9e4066Sahrens 	tq->tq_minalloc = 0;
288fa9e4066Sahrens 	while (tq->tq_nalloc != 0) {
289fa9e4066Sahrens 		ASSERT(tq->tq_freelist != NULL);
290fa9e4066Sahrens 		task_free(tq, task_alloc(tq, KM_SLEEP));
291fa9e4066Sahrens 	}
292fa9e4066Sahrens 
293fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
294fa9e4066Sahrens 
295fa9e4066Sahrens 	for (t = 0; t < nthreads; t++)
296fa9e4066Sahrens 		(void) thr_join(tq->tq_threadlist[t], NULL, NULL);
297fa9e4066Sahrens 
298fa9e4066Sahrens 	kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t));
299fa9e4066Sahrens 
300fa9e4066Sahrens 	rw_destroy(&tq->tq_threadlock);
301c25056deSgw25295 	mutex_destroy(&tq->tq_lock);
302c25056deSgw25295 	cv_destroy(&tq->tq_dispatch_cv);
303c25056deSgw25295 	cv_destroy(&tq->tq_wait_cv);
30464109744SChris Horne 	cv_destroy(&tq->tq_maxalloc_cv);
305fa9e4066Sahrens 
306fa9e4066Sahrens 	kmem_free(tq, sizeof (taskq_t));
307fa9e4066Sahrens }
308fa9e4066Sahrens 
309fa9e4066Sahrens int
310fa9e4066Sahrens taskq_member(taskq_t *tq, void *t)
311fa9e4066Sahrens {
312fa9e4066Sahrens 	int i;
313fa9e4066Sahrens 
314fa9e4066Sahrens 	if (taskq_now)
315fa9e4066Sahrens 		return (1);
316fa9e4066Sahrens 
317fa9e4066Sahrens 	for (i = 0; i < tq->tq_nthreads; i++)
318fa9e4066Sahrens 		if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t)
319fa9e4066Sahrens 			return (1);
320fa9e4066Sahrens 
321fa9e4066Sahrens 	return (0);
322fa9e4066Sahrens }
32388b7b0f2SMatthew Ahrens 
32488b7b0f2SMatthew Ahrens void
32588b7b0f2SMatthew Ahrens system_taskq_init(void)
32688b7b0f2SMatthew Ahrens {
32788b7b0f2SMatthew Ahrens 	system_taskq = taskq_create("system_taskq", 64, minclsyspri, 4, 512,
32888b7b0f2SMatthew Ahrens 	    TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
32988b7b0f2SMatthew Ahrens }
330d20e665cSRicardo M. Correia 
331d20e665cSRicardo M. Correia void
332d20e665cSRicardo M. Correia system_taskq_fini(void)
333d20e665cSRicardo M. Correia {
334d20e665cSRicardo M. Correia 	taskq_destroy(system_taskq);
335d20e665cSRicardo M. Correia 	system_taskq = NULL; /* defensive */
336d20e665cSRicardo M. Correia }
337