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.
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.
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]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
27 * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved.
28 * Copyright (c) 2014 by Delphix. All rights reserved.
31 #include <sys/zfs_context.h>
34 taskq_t
*system_taskq
;
36 #define TASKQ_ACTIVE 0x00010000
37 #define TASKQ_NAMELEN 31
40 char tq_name
[TASKQ_NAMELEN
+ 1];
42 krwlock_t tq_threadlock
;
43 kcondvar_t tq_dispatch_cv
;
44 kcondvar_t tq_wait_cv
;
45 kthread_t
**tq_threadlist
;
52 kcondvar_t tq_maxalloc_cv
;
54 taskq_ent_t
*tq_freelist
;
59 task_alloc(taskq_t
*tq
, int tqflags
)
64 again
: if ((t
= tq
->tq_freelist
) != NULL
&& tq
->tq_nalloc
>= tq
->tq_minalloc
) {
65 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
66 tq
->tq_freelist
= t
->tqent_next
;
68 if (tq
->tq_nalloc
>= tq
->tq_maxalloc
) {
69 if (!(tqflags
& KM_SLEEP
))
73 * We don't want to exceed tq_maxalloc, but we can't
74 * wait for other tasks to complete (and thus free up
75 * task structures) without risking deadlock with
76 * the caller. So, we just delay for one second
77 * to throttle the allocation rate. If we have tasks
78 * complete before one second timeout expires then
79 * taskq_ent_free will signal us and we will
80 * immediately retry the allocation.
82 tq
->tq_maxalloc_wait
++;
83 rv
= cv_timedwait(&tq
->tq_maxalloc_cv
,
84 &tq
->tq_lock
, ddi_get_lbolt() + hz
);
85 tq
->tq_maxalloc_wait
--;
87 goto again
; /* signaled */
89 mutex_exit(&tq
->tq_lock
);
91 t
= kmem_alloc(sizeof (taskq_ent_t
), tqflags
);
93 mutex_enter(&tq
->tq_lock
);
95 /* Make sure we start without any flags */
104 task_free(taskq_t
*tq
, taskq_ent_t
*t
)
106 if (tq
->tq_nalloc
<= tq
->tq_minalloc
) {
107 t
->tqent_next
= tq
->tq_freelist
;
111 mutex_exit(&tq
->tq_lock
);
112 kmem_free(t
, sizeof (taskq_ent_t
));
113 mutex_enter(&tq
->tq_lock
);
116 if (tq
->tq_maxalloc_wait
)
117 cv_signal(&tq
->tq_maxalloc_cv
);
121 taskq_dispatch(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t tqflags
)
130 mutex_enter(&tq
->tq_lock
);
131 ASSERT(tq
->tq_flags
& TASKQ_ACTIVE
);
132 if ((t
= task_alloc(tq
, tqflags
)) == NULL
) {
133 mutex_exit(&tq
->tq_lock
);
136 if (tqflags
& TQ_FRONT
) {
137 t
->tqent_next
= tq
->tq_task
.tqent_next
;
138 t
->tqent_prev
= &tq
->tq_task
;
140 t
->tqent_next
= &tq
->tq_task
;
141 t
->tqent_prev
= tq
->tq_task
.tqent_prev
;
143 t
->tqent_next
->tqent_prev
= t
;
144 t
->tqent_prev
->tqent_next
= t
;
145 t
->tqent_func
= func
;
148 cv_signal(&tq
->tq_dispatch_cv
);
149 mutex_exit(&tq
->tq_lock
);
154 taskq_dispatch_delay(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t tqflags
,
161 taskq_empty_ent(taskq_ent_t
*t
)
163 return (t
->tqent_next
== NULL
);
167 taskq_init_ent(taskq_ent_t
*t
)
169 t
->tqent_next
= NULL
;
170 t
->tqent_prev
= NULL
;
171 t
->tqent_func
= NULL
;
177 taskq_dispatch_ent(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
,
180 ASSERT(func
!= NULL
);
181 ASSERT(!(tq
->tq_flags
& TASKQ_DYNAMIC
));
184 * Mark it as a prealloc'd task. This is important
185 * to ensure that we don't free it later.
187 t
->tqent_flags
|= TQENT_FLAG_PREALLOC
;
189 * Enqueue the task to the underlying queue.
191 mutex_enter(&tq
->tq_lock
);
193 if (flags
& TQ_FRONT
) {
194 t
->tqent_next
= tq
->tq_task
.tqent_next
;
195 t
->tqent_prev
= &tq
->tq_task
;
197 t
->tqent_next
= &tq
->tq_task
;
198 t
->tqent_prev
= tq
->tq_task
.tqent_prev
;
200 t
->tqent_next
->tqent_prev
= t
;
201 t
->tqent_prev
->tqent_next
= t
;
202 t
->tqent_func
= func
;
204 cv_signal(&tq
->tq_dispatch_cv
);
205 mutex_exit(&tq
->tq_lock
);
209 taskq_wait(taskq_t
*tq
)
211 mutex_enter(&tq
->tq_lock
);
212 while (tq
->tq_task
.tqent_next
!= &tq
->tq_task
|| tq
->tq_active
!= 0)
213 cv_wait(&tq
->tq_wait_cv
, &tq
->tq_lock
);
214 mutex_exit(&tq
->tq_lock
);
218 taskq_wait_id(taskq_t
*tq
, taskqid_t id
)
224 taskq_wait_outstanding(taskq_t
*tq
, taskqid_t id
)
230 taskq_thread(void *arg
)
236 mutex_enter(&tq
->tq_lock
);
237 while (tq
->tq_flags
& TASKQ_ACTIVE
) {
238 if ((t
= tq
->tq_task
.tqent_next
) == &tq
->tq_task
) {
239 if (--tq
->tq_active
== 0)
240 cv_broadcast(&tq
->tq_wait_cv
);
241 cv_wait(&tq
->tq_dispatch_cv
, &tq
->tq_lock
);
245 t
->tqent_prev
->tqent_next
= t
->tqent_next
;
246 t
->tqent_next
->tqent_prev
= t
->tqent_prev
;
247 t
->tqent_next
= NULL
;
248 t
->tqent_prev
= NULL
;
249 prealloc
= t
->tqent_flags
& TQENT_FLAG_PREALLOC
;
250 mutex_exit(&tq
->tq_lock
);
252 rw_enter(&tq
->tq_threadlock
, RW_READER
);
253 t
->tqent_func(t
->tqent_arg
);
254 rw_exit(&tq
->tq_threadlock
);
256 mutex_enter(&tq
->tq_lock
);
261 cv_broadcast(&tq
->tq_wait_cv
);
262 mutex_exit(&tq
->tq_lock
);
268 taskq_create(const char *name
, int nthreads
, pri_t pri
,
269 int minalloc
, int maxalloc
, uint_t flags
)
271 taskq_t
*tq
= kmem_zalloc(sizeof (taskq_t
), KM_SLEEP
);
274 if (flags
& TASKQ_THREADS_CPU_PCT
) {
276 ASSERT3S(nthreads
, >=, 0);
277 ASSERT3S(nthreads
, <=, 100);
278 pct
= MIN(nthreads
, 100);
281 nthreads
= (sysconf(_SC_NPROCESSORS_ONLN
) * pct
) / 100;
282 nthreads
= MAX(nthreads
, 1); /* need at least 1 thread */
284 ASSERT3S(nthreads
, >=, 1);
287 rw_init(&tq
->tq_threadlock
, NULL
, RW_DEFAULT
, NULL
);
288 mutex_init(&tq
->tq_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
289 cv_init(&tq
->tq_dispatch_cv
, NULL
, CV_DEFAULT
, NULL
);
290 cv_init(&tq
->tq_wait_cv
, NULL
, CV_DEFAULT
, NULL
);
291 cv_init(&tq
->tq_maxalloc_cv
, NULL
, CV_DEFAULT
, NULL
);
292 (void) strncpy(tq
->tq_name
, name
, TASKQ_NAMELEN
+ 1);
293 tq
->tq_flags
= flags
| TASKQ_ACTIVE
;
294 tq
->tq_active
= nthreads
;
295 tq
->tq_nthreads
= nthreads
;
296 tq
->tq_minalloc
= minalloc
;
297 tq
->tq_maxalloc
= maxalloc
;
298 tq
->tq_task
.tqent_next
= &tq
->tq_task
;
299 tq
->tq_task
.tqent_prev
= &tq
->tq_task
;
300 tq
->tq_threadlist
= kmem_alloc(nthreads
* sizeof (kthread_t
*),
303 if (flags
& TASKQ_PREPOPULATE
) {
304 mutex_enter(&tq
->tq_lock
);
305 while (minalloc
-- > 0)
306 task_free(tq
, task_alloc(tq
, KM_SLEEP
));
307 mutex_exit(&tq
->tq_lock
);
310 for (t
= 0; t
< nthreads
; t
++)
311 VERIFY((tq
->tq_threadlist
[t
] = thread_create(NULL
, 0,
312 taskq_thread
, tq
, TS_RUN
, NULL
, 0, 0)) != NULL
);
318 taskq_destroy(taskq_t
*tq
)
320 int nthreads
= tq
->tq_nthreads
;
324 mutex_enter(&tq
->tq_lock
);
326 tq
->tq_flags
&= ~TASKQ_ACTIVE
;
327 cv_broadcast(&tq
->tq_dispatch_cv
);
329 while (tq
->tq_nthreads
!= 0)
330 cv_wait(&tq
->tq_wait_cv
, &tq
->tq_lock
);
333 while (tq
->tq_nalloc
!= 0) {
334 ASSERT(tq
->tq_freelist
!= NULL
);
335 task_free(tq
, task_alloc(tq
, KM_SLEEP
));
338 mutex_exit(&tq
->tq_lock
);
340 kmem_free(tq
->tq_threadlist
, nthreads
* sizeof (kthread_t
*));
342 rw_destroy(&tq
->tq_threadlock
);
343 mutex_destroy(&tq
->tq_lock
);
344 cv_destroy(&tq
->tq_dispatch_cv
);
345 cv_destroy(&tq
->tq_wait_cv
);
346 cv_destroy(&tq
->tq_maxalloc_cv
);
348 kmem_free(tq
, sizeof (taskq_t
));
352 taskq_member(taskq_t
*tq
, kthread_t
*t
)
359 for (i
= 0; i
< tq
->tq_nthreads
; i
++)
360 if (tq
->tq_threadlist
[i
] == t
)
367 taskq_cancel_id(taskq_t
*tq
, taskqid_t id
)
373 system_taskq_init(void)
375 system_taskq
= taskq_create("system_taskq", 64, minclsyspri
, 4, 512,
376 TASKQ_DYNAMIC
| TASKQ_PREPOPULATE
);
380 system_taskq_fini(void)
382 taskq_destroy(system_taskq
);
383 system_taskq
= NULL
; /* defensive */