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 https://opensource.org/licenses/CDDL-1.0.
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
;
35 taskq_t
*system_delay_taskq
;
37 static pthread_key_t taskq_tsd
;
39 #define TASKQ_ACTIVE 0x00010000
42 task_alloc(taskq_t
*tq
, int tqflags
)
47 again
: if ((t
= tq
->tq_freelist
) != NULL
&& tq
->tq_nalloc
>= tq
->tq_minalloc
) {
48 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
49 tq
->tq_freelist
= t
->tqent_next
;
51 if (tq
->tq_nalloc
>= tq
->tq_maxalloc
) {
52 if (!(tqflags
& KM_SLEEP
))
56 * We don't want to exceed tq_maxalloc, but we can't
57 * wait for other tasks to complete (and thus free up
58 * task structures) without risking deadlock with
59 * the caller. So, we just delay for one second
60 * to throttle the allocation rate. If we have tasks
61 * complete before one second timeout expires then
62 * taskq_ent_free will signal us and we will
63 * immediately retry the allocation.
65 tq
->tq_maxalloc_wait
++;
66 rv
= cv_timedwait(&tq
->tq_maxalloc_cv
,
67 &tq
->tq_lock
, ddi_get_lbolt() + hz
);
68 tq
->tq_maxalloc_wait
--;
70 goto again
; /* signaled */
72 mutex_exit(&tq
->tq_lock
);
74 t
= kmem_alloc(sizeof (taskq_ent_t
), tqflags
);
76 mutex_enter(&tq
->tq_lock
);
78 /* Make sure we start without any flags */
87 task_free(taskq_t
*tq
, taskq_ent_t
*t
)
89 if (tq
->tq_nalloc
<= tq
->tq_minalloc
) {
90 t
->tqent_next
= tq
->tq_freelist
;
94 mutex_exit(&tq
->tq_lock
);
95 kmem_free(t
, sizeof (taskq_ent_t
));
96 mutex_enter(&tq
->tq_lock
);
99 if (tq
->tq_maxalloc_wait
)
100 cv_signal(&tq
->tq_maxalloc_cv
);
104 taskq_dispatch(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t tqflags
)
113 mutex_enter(&tq
->tq_lock
);
114 ASSERT(tq
->tq_flags
& TASKQ_ACTIVE
);
115 if ((t
= task_alloc(tq
, tqflags
)) == NULL
) {
116 mutex_exit(&tq
->tq_lock
);
119 if (tqflags
& TQ_FRONT
) {
120 t
->tqent_next
= tq
->tq_task
.tqent_next
;
121 t
->tqent_prev
= &tq
->tq_task
;
123 t
->tqent_next
= &tq
->tq_task
;
124 t
->tqent_prev
= tq
->tq_task
.tqent_prev
;
126 t
->tqent_next
->tqent_prev
= t
;
127 t
->tqent_prev
->tqent_next
= t
;
128 t
->tqent_func
= func
;
131 cv_signal(&tq
->tq_dispatch_cv
);
132 mutex_exit(&tq
->tq_lock
);
137 taskq_dispatch_delay(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t tqflags
,
140 (void) tq
, (void) func
, (void) arg
, (void) tqflags
, (void) expire_time
;
145 taskq_empty_ent(taskq_ent_t
*t
)
147 return (t
->tqent_next
== NULL
);
151 taskq_init_ent(taskq_ent_t
*t
)
153 t
->tqent_next
= NULL
;
154 t
->tqent_prev
= NULL
;
155 t
->tqent_func
= NULL
;
161 taskq_dispatch_ent(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
,
164 ASSERT(func
!= NULL
);
167 * Mark it as a prealloc'd task. This is important
168 * to ensure that we don't free it later.
170 t
->tqent_flags
|= TQENT_FLAG_PREALLOC
;
172 * Enqueue the task to the underlying queue.
174 mutex_enter(&tq
->tq_lock
);
176 if (flags
& TQ_FRONT
) {
177 t
->tqent_next
= tq
->tq_task
.tqent_next
;
178 t
->tqent_prev
= &tq
->tq_task
;
180 t
->tqent_next
= &tq
->tq_task
;
181 t
->tqent_prev
= tq
->tq_task
.tqent_prev
;
183 t
->tqent_next
->tqent_prev
= t
;
184 t
->tqent_prev
->tqent_next
= t
;
185 t
->tqent_func
= func
;
187 cv_signal(&tq
->tq_dispatch_cv
);
188 mutex_exit(&tq
->tq_lock
);
192 taskq_wait(taskq_t
*tq
)
194 mutex_enter(&tq
->tq_lock
);
195 while (tq
->tq_task
.tqent_next
!= &tq
->tq_task
|| tq
->tq_active
!= 0)
196 cv_wait(&tq
->tq_wait_cv
, &tq
->tq_lock
);
197 mutex_exit(&tq
->tq_lock
);
201 taskq_wait_id(taskq_t
*tq
, taskqid_t id
)
208 taskq_wait_outstanding(taskq_t
*tq
, taskqid_t id
)
214 static __attribute__((noreturn
)) void
215 taskq_thread(void *arg
)
221 VERIFY0(pthread_setspecific(taskq_tsd
, tq
));
223 mutex_enter(&tq
->tq_lock
);
224 while (tq
->tq_flags
& TASKQ_ACTIVE
) {
225 if ((t
= tq
->tq_task
.tqent_next
) == &tq
->tq_task
) {
226 if (--tq
->tq_active
== 0)
227 cv_broadcast(&tq
->tq_wait_cv
);
228 cv_wait(&tq
->tq_dispatch_cv
, &tq
->tq_lock
);
232 t
->tqent_prev
->tqent_next
= t
->tqent_next
;
233 t
->tqent_next
->tqent_prev
= t
->tqent_prev
;
234 t
->tqent_next
= NULL
;
235 t
->tqent_prev
= NULL
;
236 prealloc
= t
->tqent_flags
& TQENT_FLAG_PREALLOC
;
237 mutex_exit(&tq
->tq_lock
);
239 rw_enter(&tq
->tq_threadlock
, RW_READER
);
240 t
->tqent_func(t
->tqent_arg
);
241 rw_exit(&tq
->tq_threadlock
);
243 mutex_enter(&tq
->tq_lock
);
248 cv_broadcast(&tq
->tq_wait_cv
);
249 mutex_exit(&tq
->tq_lock
);
254 taskq_create(const char *name
, int nthreads
, pri_t pri
,
255 int minalloc
, int maxalloc
, uint_t flags
)
258 taskq_t
*tq
= kmem_zalloc(sizeof (taskq_t
), KM_SLEEP
);
261 if (flags
& TASKQ_THREADS_CPU_PCT
) {
263 ASSERT3S(nthreads
, >=, 0);
264 ASSERT3S(nthreads
, <=, 100);
265 pct
= MIN(nthreads
, 100);
268 nthreads
= (sysconf(_SC_NPROCESSORS_ONLN
) * pct
) / 100;
269 nthreads
= MAX(nthreads
, 1); /* need at least 1 thread */
271 ASSERT3S(nthreads
, >=, 1);
274 rw_init(&tq
->tq_threadlock
, NULL
, RW_DEFAULT
, NULL
);
275 mutex_init(&tq
->tq_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
276 cv_init(&tq
->tq_dispatch_cv
, NULL
, CV_DEFAULT
, NULL
);
277 cv_init(&tq
->tq_wait_cv
, NULL
, CV_DEFAULT
, NULL
);
278 cv_init(&tq
->tq_maxalloc_cv
, NULL
, CV_DEFAULT
, NULL
);
279 (void) strlcpy(tq
->tq_name
, name
, sizeof (tq
->tq_name
));
280 tq
->tq_flags
= flags
| TASKQ_ACTIVE
;
281 tq
->tq_active
= nthreads
;
282 tq
->tq_nthreads
= nthreads
;
283 tq
->tq_minalloc
= minalloc
;
284 tq
->tq_maxalloc
= maxalloc
;
285 tq
->tq_task
.tqent_next
= &tq
->tq_task
;
286 tq
->tq_task
.tqent_prev
= &tq
->tq_task
;
287 tq
->tq_threadlist
= kmem_alloc(nthreads
* sizeof (kthread_t
*),
290 if (flags
& TASKQ_PREPOPULATE
) {
291 mutex_enter(&tq
->tq_lock
);
292 while (minalloc
-- > 0)
293 task_free(tq
, task_alloc(tq
, KM_SLEEP
));
294 mutex_exit(&tq
->tq_lock
);
297 for (t
= 0; t
< nthreads
; t
++)
298 VERIFY((tq
->tq_threadlist
[t
] = thread_create_named(tq
->tq_name
,
299 NULL
, 0, taskq_thread
, tq
, 0, &p0
, TS_RUN
, pri
)) != NULL
);
305 taskq_destroy(taskq_t
*tq
)
307 int nthreads
= tq
->tq_nthreads
;
311 mutex_enter(&tq
->tq_lock
);
313 tq
->tq_flags
&= ~TASKQ_ACTIVE
;
314 cv_broadcast(&tq
->tq_dispatch_cv
);
316 while (tq
->tq_nthreads
!= 0)
317 cv_wait(&tq
->tq_wait_cv
, &tq
->tq_lock
);
320 while (tq
->tq_nalloc
!= 0) {
321 ASSERT(tq
->tq_freelist
!= NULL
);
322 taskq_ent_t
*tqent_nexttq
= tq
->tq_freelist
->tqent_next
;
323 task_free(tq
, tq
->tq_freelist
);
324 tq
->tq_freelist
= tqent_nexttq
;
327 mutex_exit(&tq
->tq_lock
);
329 kmem_free(tq
->tq_threadlist
, nthreads
* sizeof (kthread_t
*));
331 rw_destroy(&tq
->tq_threadlock
);
332 mutex_destroy(&tq
->tq_lock
);
333 cv_destroy(&tq
->tq_dispatch_cv
);
334 cv_destroy(&tq
->tq_wait_cv
);
335 cv_destroy(&tq
->tq_maxalloc_cv
);
337 kmem_free(tq
, sizeof (taskq_t
));
341 * Create a taskq with a specified number of pool threads. Allocate
342 * and return an array of nthreads kthread_t pointers, one for each
343 * thread in the pool. The array is not ordered and must be freed
347 taskq_create_synced(const char *name
, int nthreads
, pri_t pri
,
348 int minalloc
, int maxalloc
, uint_t flags
, kthread_t
***ktpp
)
351 kthread_t
**kthreads
= kmem_zalloc(sizeof (*kthreads
) * nthreads
,
354 (void) pri
; (void) minalloc
; (void) maxalloc
;
356 flags
&= ~(TASKQ_DYNAMIC
| TASKQ_THREADS_CPU_PCT
| TASKQ_DC_BATCH
);
358 tq
= taskq_create(name
, nthreads
, minclsyspri
, nthreads
, INT_MAX
,
359 flags
| TASKQ_PREPOPULATE
);
361 VERIFY(tq
->tq_nthreads
== nthreads
);
363 for (int i
= 0; i
< nthreads
; i
++) {
364 kthreads
[i
] = tq
->tq_threadlist
[i
];
371 taskq_member(taskq_t
*tq
, kthread_t
*t
)
378 for (i
= 0; i
< tq
->tq_nthreads
; i
++)
379 if (tq
->tq_threadlist
[i
] == t
)
386 taskq_of_curthread(void)
388 return (pthread_getspecific(taskq_tsd
));
392 taskq_cancel_id(taskq_t
*tq
, taskqid_t id
)
394 (void) tq
, (void) id
;
399 system_taskq_init(void)
401 VERIFY0(pthread_key_create(&taskq_tsd
, NULL
));
402 system_taskq
= taskq_create("system_taskq", 64, maxclsyspri
, 4, 512,
403 TASKQ_DYNAMIC
| TASKQ_PREPOPULATE
);
404 system_delay_taskq
= taskq_create("delay_taskq", 4, maxclsyspri
, 4,
405 512, TASKQ_DYNAMIC
| TASKQ_PREPOPULATE
);
409 system_taskq_fini(void)
411 taskq_destroy(system_taskq
);
412 system_taskq
= NULL
; /* defensive */
413 taskq_destroy(system_delay_taskq
);
414 system_delay_taskq
= NULL
;
415 VERIFY0(pthread_key_delete(taskq_tsd
));