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