xref: /titanic_52/usr/src/lib/libzpool/common/taskq.c (revision 36e852a172cba914383d7341c988128b2c667fbd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/zfs_context.h>
27 
28 int taskq_now;
29 taskq_t *system_taskq;
30 
31 typedef struct task {
32 	struct task	*task_next;
33 	struct task	*task_prev;
34 	task_func_t	*task_func;
35 	void		*task_arg;
36 } task_t;
37 
38 #define	TASKQ_ACTIVE	0x00010000
39 
40 struct taskq {
41 	kmutex_t	tq_lock;
42 	krwlock_t	tq_threadlock;
43 	kcondvar_t	tq_dispatch_cv;
44 	kcondvar_t	tq_wait_cv;
45 	thread_t	*tq_threadlist;
46 	int		tq_flags;
47 	int		tq_active;
48 	int		tq_nthreads;
49 	int		tq_nalloc;
50 	int		tq_minalloc;
51 	int		tq_maxalloc;
52 	task_t		*tq_freelist;
53 	task_t		tq_task;
54 };
55 
56 static task_t *
57 task_alloc(taskq_t *tq, int tqflags)
58 {
59 	task_t *t;
60 
61 	if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
62 		tq->tq_freelist = t->task_next;
63 	} else {
64 		mutex_exit(&tq->tq_lock);
65 		if (tq->tq_nalloc >= tq->tq_maxalloc) {
66 			if (!(tqflags & KM_SLEEP)) {
67 				mutex_enter(&tq->tq_lock);
68 				return (NULL);
69 			}
70 			/*
71 			 * We don't want to exceed tq_maxalloc, but we can't
72 			 * wait for other tasks to complete (and thus free up
73 			 * task structures) without risking deadlock with
74 			 * the caller.  So, we just delay for one second
75 			 * to throttle the allocation rate.
76 			 */
77 			delay(hz);
78 		}
79 		t = kmem_alloc(sizeof (task_t), tqflags);
80 		mutex_enter(&tq->tq_lock);
81 		if (t != NULL)
82 			tq->tq_nalloc++;
83 	}
84 	return (t);
85 }
86 
87 static void
88 task_free(taskq_t *tq, task_t *t)
89 {
90 	if (tq->tq_nalloc <= tq->tq_minalloc) {
91 		t->task_next = tq->tq_freelist;
92 		tq->tq_freelist = t;
93 	} else {
94 		tq->tq_nalloc--;
95 		mutex_exit(&tq->tq_lock);
96 		kmem_free(t, sizeof (task_t));
97 		mutex_enter(&tq->tq_lock);
98 	}
99 }
100 
101 taskqid_t
102 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
103 {
104 	task_t *t;
105 
106 	if (taskq_now) {
107 		func(arg);
108 		return (1);
109 	}
110 
111 	mutex_enter(&tq->tq_lock);
112 	ASSERT(tq->tq_flags & TASKQ_ACTIVE);
113 	if ((t = task_alloc(tq, tqflags)) == NULL) {
114 		mutex_exit(&tq->tq_lock);
115 		return (0);
116 	}
117 	if (tqflags & TQ_FRONT) {
118 		t->task_next = tq->tq_task.task_next;
119 		t->task_prev = &tq->tq_task;
120 	} else {
121 		t->task_next = &tq->tq_task;
122 		t->task_prev = tq->tq_task.task_prev;
123 	}
124 	t->task_next->task_prev = t;
125 	t->task_prev->task_next = t;
126 	t->task_func = func;
127 	t->task_arg = arg;
128 	cv_signal(&tq->tq_dispatch_cv);
129 	mutex_exit(&tq->tq_lock);
130 	return (1);
131 }
132 
133 void
134 taskq_wait(taskq_t *tq)
135 {
136 	mutex_enter(&tq->tq_lock);
137 	while (tq->tq_task.task_next != &tq->tq_task || tq->tq_active != 0)
138 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
139 	mutex_exit(&tq->tq_lock);
140 }
141 
142 static void *
143 taskq_thread(void *arg)
144 {
145 	taskq_t *tq = arg;
146 	task_t *t;
147 
148 	mutex_enter(&tq->tq_lock);
149 	while (tq->tq_flags & TASKQ_ACTIVE) {
150 		if ((t = tq->tq_task.task_next) == &tq->tq_task) {
151 			if (--tq->tq_active == 0)
152 				cv_broadcast(&tq->tq_wait_cv);
153 			cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
154 			tq->tq_active++;
155 			continue;
156 		}
157 		t->task_prev->task_next = t->task_next;
158 		t->task_next->task_prev = t->task_prev;
159 		mutex_exit(&tq->tq_lock);
160 
161 		rw_enter(&tq->tq_threadlock, RW_READER);
162 		t->task_func(t->task_arg);
163 		rw_exit(&tq->tq_threadlock);
164 
165 		mutex_enter(&tq->tq_lock);
166 		task_free(tq, t);
167 	}
168 	tq->tq_nthreads--;
169 	cv_broadcast(&tq->tq_wait_cv);
170 	mutex_exit(&tq->tq_lock);
171 	return (NULL);
172 }
173 
174 /*ARGSUSED*/
175 taskq_t *
176 taskq_create(const char *name, int nthreads, pri_t pri,
177 	int minalloc, int maxalloc, uint_t flags)
178 {
179 	taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
180 	int t;
181 
182 	if (flags & TASKQ_THREADS_CPU_PCT) {
183 		int pct;
184 		ASSERT3S(nthreads, >=, 0);
185 		ASSERT3S(nthreads, <=, 100);
186 		pct = MIN(nthreads, 100);
187 		pct = MAX(pct, 0);
188 
189 		nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100;
190 		nthreads = MAX(nthreads, 1);	/* need at least 1 thread */
191 	} else {
192 		ASSERT3S(nthreads, >=, 1);
193 	}
194 
195 	rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
196 	mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL);
197 	cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL);
198 	cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL);
199 	tq->tq_flags = flags | TASKQ_ACTIVE;
200 	tq->tq_active = nthreads;
201 	tq->tq_nthreads = nthreads;
202 	tq->tq_minalloc = minalloc;
203 	tq->tq_maxalloc = maxalloc;
204 	tq->tq_task.task_next = &tq->tq_task;
205 	tq->tq_task.task_prev = &tq->tq_task;
206 	tq->tq_threadlist = kmem_alloc(nthreads * sizeof (thread_t), KM_SLEEP);
207 
208 	if (flags & TASKQ_PREPOPULATE) {
209 		mutex_enter(&tq->tq_lock);
210 		while (minalloc-- > 0)
211 			task_free(tq, task_alloc(tq, KM_SLEEP));
212 		mutex_exit(&tq->tq_lock);
213 	}
214 
215 	for (t = 0; t < nthreads; t++)
216 		(void) thr_create(0, 0, taskq_thread,
217 		    tq, THR_BOUND, &tq->tq_threadlist[t]);
218 
219 	return (tq);
220 }
221 
222 void
223 taskq_destroy(taskq_t *tq)
224 {
225 	int t;
226 	int nthreads = tq->tq_nthreads;
227 
228 	taskq_wait(tq);
229 
230 	mutex_enter(&tq->tq_lock);
231 
232 	tq->tq_flags &= ~TASKQ_ACTIVE;
233 	cv_broadcast(&tq->tq_dispatch_cv);
234 
235 	while (tq->tq_nthreads != 0)
236 		cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
237 
238 	tq->tq_minalloc = 0;
239 	while (tq->tq_nalloc != 0) {
240 		ASSERT(tq->tq_freelist != NULL);
241 		task_free(tq, task_alloc(tq, KM_SLEEP));
242 	}
243 
244 	mutex_exit(&tq->tq_lock);
245 
246 	for (t = 0; t < nthreads; t++)
247 		(void) thr_join(tq->tq_threadlist[t], NULL, NULL);
248 
249 	kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t));
250 
251 	rw_destroy(&tq->tq_threadlock);
252 	mutex_destroy(&tq->tq_lock);
253 	cv_destroy(&tq->tq_dispatch_cv);
254 	cv_destroy(&tq->tq_wait_cv);
255 
256 	kmem_free(tq, sizeof (taskq_t));
257 }
258 
259 int
260 taskq_member(taskq_t *tq, void *t)
261 {
262 	int i;
263 
264 	if (taskq_now)
265 		return (1);
266 
267 	for (i = 0; i < tq->tq_nthreads; i++)
268 		if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t)
269 			return (1);
270 
271 	return (0);
272 }
273 
274 void
275 system_taskq_init(void)
276 {
277 	system_taskq = taskq_create("system_taskq", 64, minclsyspri, 4, 512,
278 	    TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
279 }
280 
281 void
282 system_taskq_fini(void)
283 {
284 	taskq_destroy(system_taskq);
285 	system_taskq = NULL; /* defensive */
286 }
287