xref: /titanic_51/usr/src/lib/libzpool/common/taskq.c (revision 641097441a6e36fb83135a28c834761ecbb80d36)
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*64109744SChris Horne  * Copyright 2010 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;
52*64109744SChris Horne 	kcondvar_t	tq_maxalloc_cv;
53*64109744SChris Horne 	int		tq_maxalloc_wait;
54fa9e4066Sahrens 	task_t		*tq_freelist;
55fa9e4066Sahrens 	task_t		tq_task;
56fa9e4066Sahrens };
57fa9e4066Sahrens 
58fa9e4066Sahrens static task_t *
59fa9e4066Sahrens task_alloc(taskq_t *tq, int tqflags)
60fa9e4066Sahrens {
61fa9e4066Sahrens 	task_t *t;
62*64109744SChris Horne 	int rv;
63fa9e4066Sahrens 
64*64109744SChris Horne again:	if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
65fa9e4066Sahrens 		tq->tq_freelist = t->task_next;
66fa9e4066Sahrens 	} else {
67fa9e4066Sahrens 		if (tq->tq_nalloc >= tq->tq_maxalloc) {
68*64109744SChris Horne 			if (!(tqflags & KM_SLEEP))
69fa9e4066Sahrens 				return (NULL);
70*64109744SChris 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
76*64109744SChris Horne 			 * to throttle the allocation rate. If we have tasks
77*64109744SChris Horne 			 * complete before one second timeout expires then
78*64109744SChris Horne 			 * taskq_ent_free will signal us and we will
79*64109744SChris Horne 			 * immediately retry the allocation.
80fa9e4066Sahrens 			 */
81*64109744SChris Horne 			tq->tq_maxalloc_wait++;
82*64109744SChris Horne 			rv = cv_timedwait(&tq->tq_maxalloc_cv,
83*64109744SChris Horne 			    &tq->tq_lock, ddi_get_lbolt() + hz);
84*64109744SChris Horne 			tq->tq_maxalloc_wait--;
85*64109744SChris Horne 			if (rv > 0)
86*64109744SChris Horne 				goto again;		/* signaled */
87fa9e4066Sahrens 		}
88*64109744SChris Horne 		mutex_exit(&tq->tq_lock);
89*64109744SChris Horne 
90fa9e4066Sahrens 		t = kmem_alloc(sizeof (task_t), tqflags);
91*64109744SChris Horne 
92fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
93fa9e4066Sahrens 		if (t != NULL)
94fa9e4066Sahrens 			tq->tq_nalloc++;
95fa9e4066Sahrens 	}
96fa9e4066Sahrens 	return (t);
97fa9e4066Sahrens }
98fa9e4066Sahrens 
99fa9e4066Sahrens static void
100fa9e4066Sahrens task_free(taskq_t *tq, task_t *t)
101fa9e4066Sahrens {
102fa9e4066Sahrens 	if (tq->tq_nalloc <= tq->tq_minalloc) {
103fa9e4066Sahrens 		t->task_next = tq->tq_freelist;
104fa9e4066Sahrens 		tq->tq_freelist = t;
105fa9e4066Sahrens 	} else {
106fa9e4066Sahrens 		tq->tq_nalloc--;
107fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
108fa9e4066Sahrens 		kmem_free(t, sizeof (task_t));
109fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
110fa9e4066Sahrens 	}
111*64109744SChris Horne 
112*64109744SChris Horne 	if (tq->tq_maxalloc_wait)
113*64109744SChris 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 {
119fa9e4066Sahrens 	task_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) {
13335a5a358SJonathan Adams 		t->task_next = tq->tq_task.task_next;
13435a5a358SJonathan Adams 		t->task_prev = &tq->tq_task;
13535a5a358SJonathan Adams 	} else {
136fa9e4066Sahrens 		t->task_next = &tq->tq_task;
137fa9e4066Sahrens 		t->task_prev = tq->tq_task.task_prev;
13835a5a358SJonathan Adams 	}
139fa9e4066Sahrens 	t->task_next->task_prev = t;
140fa9e4066Sahrens 	t->task_prev->task_next = t;
141fa9e4066Sahrens 	t->task_func = func;
142fa9e4066Sahrens 	t->task_arg = arg;
143fa9e4066Sahrens 	cv_signal(&tq->tq_dispatch_cv);
144fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
145fa9e4066Sahrens 	return (1);
146fa9e4066Sahrens }
147fa9e4066Sahrens 
148fa9e4066Sahrens void
149fa9e4066Sahrens taskq_wait(taskq_t *tq)
150fa9e4066Sahrens {
151fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
152fa9e4066Sahrens 	while (tq->tq_task.task_next != &tq->tq_task || tq->tq_active != 0)
153fa9e4066Sahrens 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
154fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
155fa9e4066Sahrens }
156fa9e4066Sahrens 
157fa9e4066Sahrens static void *
158fa9e4066Sahrens taskq_thread(void *arg)
159fa9e4066Sahrens {
160fa9e4066Sahrens 	taskq_t *tq = arg;
161fa9e4066Sahrens 	task_t *t;
162fa9e4066Sahrens 
163fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
164fa9e4066Sahrens 	while (tq->tq_flags & TASKQ_ACTIVE) {
165fa9e4066Sahrens 		if ((t = tq->tq_task.task_next) == &tq->tq_task) {
166fa9e4066Sahrens 			if (--tq->tq_active == 0)
167fa9e4066Sahrens 				cv_broadcast(&tq->tq_wait_cv);
168fa9e4066Sahrens 			cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
169fa9e4066Sahrens 			tq->tq_active++;
170fa9e4066Sahrens 			continue;
171fa9e4066Sahrens 		}
172fa9e4066Sahrens 		t->task_prev->task_next = t->task_next;
173fa9e4066Sahrens 		t->task_next->task_prev = t->task_prev;
174fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
175fa9e4066Sahrens 
176fa9e4066Sahrens 		rw_enter(&tq->tq_threadlock, RW_READER);
177fa9e4066Sahrens 		t->task_func(t->task_arg);
178fa9e4066Sahrens 		rw_exit(&tq->tq_threadlock);
179fa9e4066Sahrens 
180fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
181fa9e4066Sahrens 		task_free(tq, t);
182fa9e4066Sahrens 	}
183fa9e4066Sahrens 	tq->tq_nthreads--;
184fa9e4066Sahrens 	cv_broadcast(&tq->tq_wait_cv);
185fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
186fa9e4066Sahrens 	return (NULL);
187fa9e4066Sahrens }
188fa9e4066Sahrens 
189fa9e4066Sahrens /*ARGSUSED*/
190fa9e4066Sahrens taskq_t *
191fa9e4066Sahrens taskq_create(const char *name, int nthreads, pri_t pri,
192fa9e4066Sahrens 	int minalloc, int maxalloc, uint_t flags)
193fa9e4066Sahrens {
194fa9e4066Sahrens 	taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
195fa9e4066Sahrens 	int t;
196fa9e4066Sahrens 
1972e0c549eSJonathan Adams 	if (flags & TASKQ_THREADS_CPU_PCT) {
1982e0c549eSJonathan Adams 		int pct;
1992e0c549eSJonathan Adams 		ASSERT3S(nthreads, >=, 0);
2002e0c549eSJonathan Adams 		ASSERT3S(nthreads, <=, 100);
2012e0c549eSJonathan Adams 		pct = MIN(nthreads, 100);
2022e0c549eSJonathan Adams 		pct = MAX(pct, 0);
2032e0c549eSJonathan Adams 
2042e0c549eSJonathan Adams 		nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100;
2052e0c549eSJonathan Adams 		nthreads = MAX(nthreads, 1);	/* need at least 1 thread */
2062e0c549eSJonathan Adams 	} else {
2072e0c549eSJonathan Adams 		ASSERT3S(nthreads, >=, 1);
2082e0c549eSJonathan Adams 	}
2092e0c549eSJonathan Adams 
210fa9e4066Sahrens 	rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
211c25056deSgw25295 	mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL);
212c25056deSgw25295 	cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL);
213c25056deSgw25295 	cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL);
214*64109744SChris Horne 	cv_init(&tq->tq_maxalloc_cv, NULL, CV_DEFAULT, NULL);
215fa9e4066Sahrens 	tq->tq_flags = flags | TASKQ_ACTIVE;
216fa9e4066Sahrens 	tq->tq_active = nthreads;
217fa9e4066Sahrens 	tq->tq_nthreads = nthreads;
218fa9e4066Sahrens 	tq->tq_minalloc = minalloc;
219fa9e4066Sahrens 	tq->tq_maxalloc = maxalloc;
220fa9e4066Sahrens 	tq->tq_task.task_next = &tq->tq_task;
221fa9e4066Sahrens 	tq->tq_task.task_prev = &tq->tq_task;
222fa9e4066Sahrens 	tq->tq_threadlist = kmem_alloc(nthreads * sizeof (thread_t), KM_SLEEP);
223fa9e4066Sahrens 
224fa9e4066Sahrens 	if (flags & TASKQ_PREPOPULATE) {
225fa9e4066Sahrens 		mutex_enter(&tq->tq_lock);
226fa9e4066Sahrens 		while (minalloc-- > 0)
227fa9e4066Sahrens 			task_free(tq, task_alloc(tq, KM_SLEEP));
228fa9e4066Sahrens 		mutex_exit(&tq->tq_lock);
229fa9e4066Sahrens 	}
230fa9e4066Sahrens 
231fa9e4066Sahrens 	for (t = 0; t < nthreads; t++)
232fa9e4066Sahrens 		(void) thr_create(0, 0, taskq_thread,
233fa9e4066Sahrens 		    tq, THR_BOUND, &tq->tq_threadlist[t]);
234fa9e4066Sahrens 
235fa9e4066Sahrens 	return (tq);
236fa9e4066Sahrens }
237fa9e4066Sahrens 
238fa9e4066Sahrens void
239fa9e4066Sahrens taskq_destroy(taskq_t *tq)
240fa9e4066Sahrens {
241fa9e4066Sahrens 	int t;
242fa9e4066Sahrens 	int nthreads = tq->tq_nthreads;
243fa9e4066Sahrens 
244fa9e4066Sahrens 	taskq_wait(tq);
245fa9e4066Sahrens 
246fa9e4066Sahrens 	mutex_enter(&tq->tq_lock);
247fa9e4066Sahrens 
248fa9e4066Sahrens 	tq->tq_flags &= ~TASKQ_ACTIVE;
249fa9e4066Sahrens 	cv_broadcast(&tq->tq_dispatch_cv);
250fa9e4066Sahrens 
251fa9e4066Sahrens 	while (tq->tq_nthreads != 0)
252fa9e4066Sahrens 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
253fa9e4066Sahrens 
254fa9e4066Sahrens 	tq->tq_minalloc = 0;
255fa9e4066Sahrens 	while (tq->tq_nalloc != 0) {
256fa9e4066Sahrens 		ASSERT(tq->tq_freelist != NULL);
257fa9e4066Sahrens 		task_free(tq, task_alloc(tq, KM_SLEEP));
258fa9e4066Sahrens 	}
259fa9e4066Sahrens 
260fa9e4066Sahrens 	mutex_exit(&tq->tq_lock);
261fa9e4066Sahrens 
262fa9e4066Sahrens 	for (t = 0; t < nthreads; t++)
263fa9e4066Sahrens 		(void) thr_join(tq->tq_threadlist[t], NULL, NULL);
264fa9e4066Sahrens 
265fa9e4066Sahrens 	kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t));
266fa9e4066Sahrens 
267fa9e4066Sahrens 	rw_destroy(&tq->tq_threadlock);
268c25056deSgw25295 	mutex_destroy(&tq->tq_lock);
269c25056deSgw25295 	cv_destroy(&tq->tq_dispatch_cv);
270c25056deSgw25295 	cv_destroy(&tq->tq_wait_cv);
271*64109744SChris Horne 	cv_destroy(&tq->tq_maxalloc_cv);
272fa9e4066Sahrens 
273fa9e4066Sahrens 	kmem_free(tq, sizeof (taskq_t));
274fa9e4066Sahrens }
275fa9e4066Sahrens 
276fa9e4066Sahrens int
277fa9e4066Sahrens taskq_member(taskq_t *tq, void *t)
278fa9e4066Sahrens {
279fa9e4066Sahrens 	int i;
280fa9e4066Sahrens 
281fa9e4066Sahrens 	if (taskq_now)
282fa9e4066Sahrens 		return (1);
283fa9e4066Sahrens 
284fa9e4066Sahrens 	for (i = 0; i < tq->tq_nthreads; i++)
285fa9e4066Sahrens 		if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t)
286fa9e4066Sahrens 			return (1);
287fa9e4066Sahrens 
288fa9e4066Sahrens 	return (0);
289fa9e4066Sahrens }
29088b7b0f2SMatthew Ahrens 
29188b7b0f2SMatthew Ahrens void
29288b7b0f2SMatthew Ahrens system_taskq_init(void)
29388b7b0f2SMatthew Ahrens {
29488b7b0f2SMatthew Ahrens 	system_taskq = taskq_create("system_taskq", 64, minclsyspri, 4, 512,
29588b7b0f2SMatthew Ahrens 	    TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
29688b7b0f2SMatthew Ahrens }
297d20e665cSRicardo M. Correia 
298d20e665cSRicardo M. Correia void
299d20e665cSRicardo M. Correia system_taskq_fini(void)
300d20e665cSRicardo M. Correia {
301d20e665cSRicardo M. Correia 	taskq_destroy(system_taskq);
302d20e665cSRicardo M. Correia 	system_taskq = NULL; /* defensive */
303d20e665cSRicardo M. Correia }
304