xref: /titanic_51/usr/src/lib/libzpool/common/taskq.c (revision 2e0c549eb612aef12ce21941172e3abe2e62ba85)
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 /*
22*2e0c549eSJonathan 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 	}
117fa9e4066Sahrens 	t->task_next = &tq->tq_task;
118fa9e4066Sahrens 	t->task_prev = tq->tq_task.task_prev;
119fa9e4066Sahrens 	t->task_next->task_prev = t;
120fa9e4066Sahrens 	t->task_prev->task_next = t;
121fa9e4066Sahrens 	t->task_func = func;
122fa9e4066Sahrens 	t->task_arg = arg;
123fa9e4066Sahrens 	cv_signal(&tq->tq_dispatch_cv);
124fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
125fa9e4066Sahrens 	return (1);
126fa9e4066Sahrens }
127fa9e4066Sahrens 
128fa9e4066Sahrens void
129fa9e4066Sahrens taskq_wait(taskq_t *tq)
130fa9e4066Sahrens {
131fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
132fa9e4066Sahrens 	while (tq->tq_task.task_next != &tq->tq_task || tq->tq_active != 0)
133fa9e4066Sahrens 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
134fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
135fa9e4066Sahrens }
136fa9e4066Sahrens 
137fa9e4066Sahrens static void *
138fa9e4066Sahrens taskq_thread(void *arg)
139fa9e4066Sahrens {
140fa9e4066Sahrens 	taskq_t *tq = arg;
141fa9e4066Sahrens 	task_t *t;
142fa9e4066Sahrens 
143fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
144fa9e4066Sahrens 	while (tq->tq_flags & TASKQ_ACTIVE) {
145fa9e4066Sahrens 		if ((t = tq->tq_task.task_next) == &tq->tq_task) {
146fa9e4066Sahrens 			if (--tq->tq_active == 0)
147fa9e4066Sahrens 				cv_broadcast(&tq->tq_wait_cv);
148fa9e4066Sahrens 			cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
149fa9e4066Sahrens 			tq->tq_active++;
150fa9e4066Sahrens 			continue;
151fa9e4066Sahrens 		}
152fa9e4066Sahrens 		t->task_prev->task_next = t->task_next;
153fa9e4066Sahrens 		t->task_next->task_prev = t->task_prev;
154fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
155fa9e4066Sahrens 
156fa9e4066Sahrens 		rw_enter(&tq->tq_threadlock, RW_READER);
157fa9e4066Sahrens 		t->task_func(t->task_arg);
158fa9e4066Sahrens 		rw_exit(&tq->tq_threadlock);
159fa9e4066Sahrens 
160fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
161fa9e4066Sahrens 		task_free(tq, t);
162fa9e4066Sahrens 	}
163fa9e4066Sahrens 	tq->tq_nthreads--;
164fa9e4066Sahrens 	cv_broadcast(&tq->tq_wait_cv);
165fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
166fa9e4066Sahrens 	return (NULL);
167fa9e4066Sahrens }
168fa9e4066Sahrens 
169fa9e4066Sahrens /*ARGSUSED*/
170fa9e4066Sahrens taskq_t *
171fa9e4066Sahrens taskq_create(const char *name, int nthreads, pri_t pri,
172fa9e4066Sahrens 	int minalloc, int maxalloc, uint_t flags)
173fa9e4066Sahrens {
174fa9e4066Sahrens 	taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
175fa9e4066Sahrens 	int t;
176fa9e4066Sahrens 
177*2e0c549eSJonathan Adams 	if (flags & TASKQ_THREADS_CPU_PCT) {
178*2e0c549eSJonathan Adams 		int pct;
179*2e0c549eSJonathan Adams 		ASSERT3S(nthreads, >=, 0);
180*2e0c549eSJonathan Adams 		ASSERT3S(nthreads, <=, 100);
181*2e0c549eSJonathan Adams 		pct = MIN(nthreads, 100);
182*2e0c549eSJonathan Adams 		pct = MAX(pct, 0);
183*2e0c549eSJonathan Adams 
184*2e0c549eSJonathan Adams 		nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100;
185*2e0c549eSJonathan Adams 		nthreads = MAX(nthreads, 1);	/* need at least 1 thread */
186*2e0c549eSJonathan Adams 	} else {
187*2e0c549eSJonathan Adams 		ASSERT3S(nthreads, >=, 1);
188*2e0c549eSJonathan Adams 	}
189*2e0c549eSJonathan Adams 
190fa9e4066Sahrens 	rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
191c25056deSgw25295 	mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL);
192c25056deSgw25295 	cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL);
193c25056deSgw25295 	cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL);
194fa9e4066Sahrens 	tq->tq_flags = flags | TASKQ_ACTIVE;
195fa9e4066Sahrens 	tq->tq_active = nthreads;
196fa9e4066Sahrens 	tq->tq_nthreads = nthreads;
197fa9e4066Sahrens 	tq->tq_minalloc = minalloc;
198fa9e4066Sahrens 	tq->tq_maxalloc = maxalloc;
199fa9e4066Sahrens 	tq->tq_task.task_next = &tq->tq_task;
200fa9e4066Sahrens 	tq->tq_task.task_prev = &tq->tq_task;
201fa9e4066Sahrens 	tq->tq_threadlist = kmem_alloc(nthreads * sizeof (thread_t), KM_SLEEP);
202fa9e4066Sahrens 
203fa9e4066Sahrens 	if (flags & TASKQ_PREPOPULATE) {
204fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
205fa9e4066Sahrens 		while (minalloc-- > 0)
206fa9e4066Sahrens 			task_free(tq, task_alloc(tq, KM_SLEEP));
207fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
208fa9e4066Sahrens 	}
209fa9e4066Sahrens 
210fa9e4066Sahrens 	for (t = 0; t < nthreads; t++)
211fa9e4066Sahrens 		(void) thr_create(0, 0, taskq_thread,
212fa9e4066Sahrens 		    tq, THR_BOUND, &tq->tq_threadlist[t]);
213fa9e4066Sahrens 
214fa9e4066Sahrens 	return (tq);
215fa9e4066Sahrens }
216fa9e4066Sahrens 
217fa9e4066Sahrens void
218fa9e4066Sahrens taskq_destroy(taskq_t *tq)
219fa9e4066Sahrens {
220fa9e4066Sahrens 	int t;
221fa9e4066Sahrens 	int nthreads = tq->tq_nthreads;
222fa9e4066Sahrens 
223fa9e4066Sahrens 	taskq_wait(tq);
224fa9e4066Sahrens 
225fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
226fa9e4066Sahrens 
227fa9e4066Sahrens 	tq->tq_flags &= ~TASKQ_ACTIVE;
228fa9e4066Sahrens 	cv_broadcast(&tq->tq_dispatch_cv);
229fa9e4066Sahrens 
230fa9e4066Sahrens 	while (tq->tq_nthreads != 0)
231fa9e4066Sahrens 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
232fa9e4066Sahrens 
233fa9e4066Sahrens 	tq->tq_minalloc = 0;
234fa9e4066Sahrens 	while (tq->tq_nalloc != 0) {
235fa9e4066Sahrens 		ASSERT(tq->tq_freelist != NULL);
236fa9e4066Sahrens 		task_free(tq, task_alloc(tq, KM_SLEEP));
237fa9e4066Sahrens 	}
238fa9e4066Sahrens 
239fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
240fa9e4066Sahrens 
241fa9e4066Sahrens 	for (t = 0; t < nthreads; t++)
242fa9e4066Sahrens 		(void) thr_join(tq->tq_threadlist[t], NULL, NULL);
243fa9e4066Sahrens 
244fa9e4066Sahrens 	kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t));
245fa9e4066Sahrens 
246fa9e4066Sahrens 	rw_destroy(&tq->tq_threadlock);
247c25056deSgw25295 	mutex_destroy(&tq->tq_lock);
248c25056deSgw25295 	cv_destroy(&tq->tq_dispatch_cv);
249c25056deSgw25295 	cv_destroy(&tq->tq_wait_cv);
250fa9e4066Sahrens 
251fa9e4066Sahrens 	kmem_free(tq, sizeof (taskq_t));
252fa9e4066Sahrens }
253fa9e4066Sahrens 
254fa9e4066Sahrens int
255fa9e4066Sahrens taskq_member(taskq_t *tq, void *t)
256fa9e4066Sahrens {
257fa9e4066Sahrens 	int i;
258fa9e4066Sahrens 
259fa9e4066Sahrens 	if (taskq_now)
260fa9e4066Sahrens 		return (1);
261fa9e4066Sahrens 
262fa9e4066Sahrens 	for (i = 0; i < tq->tq_nthreads; i++)
263fa9e4066Sahrens 		if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t)
264fa9e4066Sahrens 			return (1);
265fa9e4066Sahrens 
266fa9e4066Sahrens 	return (0);
267fa9e4066Sahrens }
26888b7b0f2SMatthew Ahrens 
26988b7b0f2SMatthew Ahrens void
27088b7b0f2SMatthew Ahrens system_taskq_init(void)
27188b7b0f2SMatthew Ahrens {
27288b7b0f2SMatthew Ahrens 	system_taskq = taskq_create("system_taskq", 64, minclsyspri, 4, 512,
27388b7b0f2SMatthew Ahrens 	    TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
27488b7b0f2SMatthew Ahrens }
275