xref: /titanic_51/usr/src/lib/libzpool/common/taskq.c (revision c25056de36a33f2a76f79dcf64593f731d258013)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5*c25056deSgw25295  * Common Development and Distribution License (the "License").
6*c25056deSgw25295  * 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 /*
22*c25056deSgw25295  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <sys/zfs_context.h>
29fa9e4066Sahrens 
30fa9e4066Sahrens int taskq_now;
31fa9e4066Sahrens 
32fa9e4066Sahrens typedef struct task {
33fa9e4066Sahrens 	struct task	*task_next;
34fa9e4066Sahrens 	struct task	*task_prev;
35fa9e4066Sahrens 	task_func_t	*task_func;
36fa9e4066Sahrens 	void		*task_arg;
37fa9e4066Sahrens } task_t;
38fa9e4066Sahrens 
39fa9e4066Sahrens #define	TASKQ_ACTIVE	0x00010000
40fa9e4066Sahrens 
41fa9e4066Sahrens struct taskq {
42fa9e4066Sahrens 	kmutex_t	tq_lock;
43fa9e4066Sahrens 	krwlock_t	tq_threadlock;
44fa9e4066Sahrens 	kcondvar_t	tq_dispatch_cv;
45fa9e4066Sahrens 	kcondvar_t	tq_wait_cv;
46fa9e4066Sahrens 	thread_t	*tq_threadlist;
47fa9e4066Sahrens 	int		tq_flags;
48fa9e4066Sahrens 	int		tq_active;
49fa9e4066Sahrens 	int		tq_nthreads;
50fa9e4066Sahrens 	int		tq_nalloc;
51fa9e4066Sahrens 	int		tq_minalloc;
52fa9e4066Sahrens 	int		tq_maxalloc;
53fa9e4066Sahrens 	task_t		*tq_freelist;
54fa9e4066Sahrens 	task_t		tq_task;
55fa9e4066Sahrens };
56fa9e4066Sahrens 
57fa9e4066Sahrens static task_t *
58fa9e4066Sahrens task_alloc(taskq_t *tq, int tqflags)
59fa9e4066Sahrens {
60fa9e4066Sahrens 	task_t *t;
61fa9e4066Sahrens 
62fa9e4066Sahrens 	if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
63fa9e4066Sahrens 		tq->tq_freelist = t->task_next;
64fa9e4066Sahrens 	} else {
65fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
66fa9e4066Sahrens 		if (tq->tq_nalloc >= tq->tq_maxalloc) {
67fa9e4066Sahrens 			if (!(tqflags & KM_SLEEP)) {
68fa9e4066Sahrens 				mutex_enter(&tq->tq_lock);
69fa9e4066Sahrens 				return (NULL);
70fa9e4066Sahrens 			}
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
76fa9e4066Sahrens 			 * to throttle the allocation rate.
77fa9e4066Sahrens 			 */
78fa9e4066Sahrens 			delay(hz);
79fa9e4066Sahrens 		}
80fa9e4066Sahrens 		t = kmem_alloc(sizeof (task_t), tqflags);
81fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
82fa9e4066Sahrens 		if (t != NULL)
83fa9e4066Sahrens 			tq->tq_nalloc++;
84fa9e4066Sahrens 	}
85fa9e4066Sahrens 	return (t);
86fa9e4066Sahrens }
87fa9e4066Sahrens 
88fa9e4066Sahrens static void
89fa9e4066Sahrens task_free(taskq_t *tq, task_t *t)
90fa9e4066Sahrens {
91fa9e4066Sahrens 	if (tq->tq_nalloc <= tq->tq_minalloc) {
92fa9e4066Sahrens 		t->task_next = tq->tq_freelist;
93fa9e4066Sahrens 		tq->tq_freelist = t;
94fa9e4066Sahrens 	} else {
95fa9e4066Sahrens 		tq->tq_nalloc--;
96fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
97fa9e4066Sahrens 		kmem_free(t, sizeof (task_t));
98fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
99fa9e4066Sahrens 	}
100fa9e4066Sahrens }
101fa9e4066Sahrens 
102fa9e4066Sahrens taskqid_t
103fa9e4066Sahrens taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
104fa9e4066Sahrens {
105fa9e4066Sahrens 	task_t *t;
106fa9e4066Sahrens 
107fa9e4066Sahrens 	if (taskq_now) {
108fa9e4066Sahrens 		func(arg);
109fa9e4066Sahrens 		return (1);
110fa9e4066Sahrens 	}
111fa9e4066Sahrens 
112fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
113fa9e4066Sahrens 	ASSERT(tq->tq_flags & TASKQ_ACTIVE);
114fa9e4066Sahrens 	if ((t = task_alloc(tq, tqflags)) == NULL) {
115fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
116fa9e4066Sahrens 		return (0);
117fa9e4066Sahrens 	}
118fa9e4066Sahrens 	t->task_next = &tq->tq_task;
119fa9e4066Sahrens 	t->task_prev = tq->tq_task.task_prev;
120fa9e4066Sahrens 	t->task_next->task_prev = t;
121fa9e4066Sahrens 	t->task_prev->task_next = t;
122fa9e4066Sahrens 	t->task_func = func;
123fa9e4066Sahrens 	t->task_arg = arg;
124fa9e4066Sahrens 	cv_signal(&tq->tq_dispatch_cv);
125fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
126fa9e4066Sahrens 	return (1);
127fa9e4066Sahrens }
128fa9e4066Sahrens 
129fa9e4066Sahrens void
130fa9e4066Sahrens taskq_wait(taskq_t *tq)
131fa9e4066Sahrens {
132fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
133fa9e4066Sahrens 	while (tq->tq_task.task_next != &tq->tq_task || tq->tq_active != 0)
134fa9e4066Sahrens 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
135fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
136fa9e4066Sahrens }
137fa9e4066Sahrens 
138fa9e4066Sahrens static void *
139fa9e4066Sahrens taskq_thread(void *arg)
140fa9e4066Sahrens {
141fa9e4066Sahrens 	taskq_t *tq = arg;
142fa9e4066Sahrens 	task_t *t;
143fa9e4066Sahrens 
144fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
145fa9e4066Sahrens 	while (tq->tq_flags & TASKQ_ACTIVE) {
146fa9e4066Sahrens 		if ((t = tq->tq_task.task_next) == &tq->tq_task) {
147fa9e4066Sahrens 			if (--tq->tq_active == 0)
148fa9e4066Sahrens 				cv_broadcast(&tq->tq_wait_cv);
149fa9e4066Sahrens 			cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
150fa9e4066Sahrens 			tq->tq_active++;
151fa9e4066Sahrens 			continue;
152fa9e4066Sahrens 		}
153fa9e4066Sahrens 		t->task_prev->task_next = t->task_next;
154fa9e4066Sahrens 		t->task_next->task_prev = t->task_prev;
155fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
156fa9e4066Sahrens 
157fa9e4066Sahrens 		rw_enter(&tq->tq_threadlock, RW_READER);
158fa9e4066Sahrens 		t->task_func(t->task_arg);
159fa9e4066Sahrens 		rw_exit(&tq->tq_threadlock);
160fa9e4066Sahrens 
161fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
162fa9e4066Sahrens 		task_free(tq, t);
163fa9e4066Sahrens 	}
164fa9e4066Sahrens 	tq->tq_nthreads--;
165fa9e4066Sahrens 	cv_broadcast(&tq->tq_wait_cv);
166fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
167fa9e4066Sahrens 	return (NULL);
168fa9e4066Sahrens }
169fa9e4066Sahrens 
170fa9e4066Sahrens /*ARGSUSED*/
171fa9e4066Sahrens taskq_t *
172fa9e4066Sahrens taskq_create(const char *name, int nthreads, pri_t pri,
173fa9e4066Sahrens 	int minalloc, int maxalloc, uint_t flags)
174fa9e4066Sahrens {
175fa9e4066Sahrens 	taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
176fa9e4066Sahrens 	int t;
177fa9e4066Sahrens 
178fa9e4066Sahrens 	rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
179*c25056deSgw25295 	mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL);
180*c25056deSgw25295 	cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL);
181*c25056deSgw25295 	cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL);
182fa9e4066Sahrens 	tq->tq_flags = flags | TASKQ_ACTIVE;
183fa9e4066Sahrens 	tq->tq_active = nthreads;
184fa9e4066Sahrens 	tq->tq_nthreads = nthreads;
185fa9e4066Sahrens 	tq->tq_minalloc = minalloc;
186fa9e4066Sahrens 	tq->tq_maxalloc = maxalloc;
187fa9e4066Sahrens 	tq->tq_task.task_next = &tq->tq_task;
188fa9e4066Sahrens 	tq->tq_task.task_prev = &tq->tq_task;
189fa9e4066Sahrens 	tq->tq_threadlist = kmem_alloc(nthreads * sizeof (thread_t), KM_SLEEP);
190fa9e4066Sahrens 
191fa9e4066Sahrens 	if (flags & TASKQ_PREPOPULATE) {
192fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
193fa9e4066Sahrens 		while (minalloc-- > 0)
194fa9e4066Sahrens 			task_free(tq, task_alloc(tq, KM_SLEEP));
195fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
196fa9e4066Sahrens 	}
197fa9e4066Sahrens 
198fa9e4066Sahrens 	for (t = 0; t < nthreads; t++)
199fa9e4066Sahrens 		(void) thr_create(0, 0, taskq_thread,
200fa9e4066Sahrens 		    tq, THR_BOUND, &tq->tq_threadlist[t]);
201fa9e4066Sahrens 
202fa9e4066Sahrens 	return (tq);
203fa9e4066Sahrens }
204fa9e4066Sahrens 
205fa9e4066Sahrens void
206fa9e4066Sahrens taskq_destroy(taskq_t *tq)
207fa9e4066Sahrens {
208fa9e4066Sahrens 	int t;
209fa9e4066Sahrens 	int nthreads = tq->tq_nthreads;
210fa9e4066Sahrens 
211fa9e4066Sahrens 	taskq_wait(tq);
212fa9e4066Sahrens 
213fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
214fa9e4066Sahrens 
215fa9e4066Sahrens 	tq->tq_flags &= ~TASKQ_ACTIVE;
216fa9e4066Sahrens 	cv_broadcast(&tq->tq_dispatch_cv);
217fa9e4066Sahrens 
218fa9e4066Sahrens 	while (tq->tq_nthreads != 0)
219fa9e4066Sahrens 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
220fa9e4066Sahrens 
221fa9e4066Sahrens 	tq->tq_minalloc = 0;
222fa9e4066Sahrens 	while (tq->tq_nalloc != 0) {
223fa9e4066Sahrens 		ASSERT(tq->tq_freelist != NULL);
224fa9e4066Sahrens 		task_free(tq, task_alloc(tq, KM_SLEEP));
225fa9e4066Sahrens 	}
226fa9e4066Sahrens 
227fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
228fa9e4066Sahrens 
229fa9e4066Sahrens 	for (t = 0; t < nthreads; t++)
230fa9e4066Sahrens 		(void) thr_join(tq->tq_threadlist[t], NULL, NULL);
231fa9e4066Sahrens 
232fa9e4066Sahrens 	kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t));
233fa9e4066Sahrens 
234fa9e4066Sahrens 	rw_destroy(&tq->tq_threadlock);
235*c25056deSgw25295 	mutex_destroy(&tq->tq_lock);
236*c25056deSgw25295 	cv_destroy(&tq->tq_dispatch_cv);
237*c25056deSgw25295 	cv_destroy(&tq->tq_wait_cv);
238fa9e4066Sahrens 
239fa9e4066Sahrens 	kmem_free(tq, sizeof (taskq_t));
240fa9e4066Sahrens }
241fa9e4066Sahrens 
242fa9e4066Sahrens int
243fa9e4066Sahrens taskq_member(taskq_t *tq, void *t)
244fa9e4066Sahrens {
245fa9e4066Sahrens 	int i;
246fa9e4066Sahrens 
247fa9e4066Sahrens 	if (taskq_now)
248fa9e4066Sahrens 		return (1);
249fa9e4066Sahrens 
250fa9e4066Sahrens 	for (i = 0; i < tq->tq_nthreads; i++)
251fa9e4066Sahrens 		if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t)
252fa9e4066Sahrens 			return (1);
253fa9e4066Sahrens 
254fa9e4066Sahrens 	return (0);
255fa9e4066Sahrens }
256