]>
git.proxmox.com Git - mirror_zfs-debian.git/blob - lib/libzpool/taskq.c
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
;
35 taskq_t
*system_delay_taskq
;
37 #define TASKQ_ACTIVE 0x00010000
40 task_alloc(taskq_t
*tq
, int tqflags
)
45 again
: if ((t
= tq
->tq_freelist
) != NULL
&& tq
->tq_nalloc
>= tq
->tq_minalloc
) {
46 ASSERT(!(t
->tqent_flags
& TQENT_FLAG_PREALLOC
));
47 tq
->tq_freelist
= t
->tqent_next
;
49 if (tq
->tq_nalloc
>= tq
->tq_maxalloc
) {
50 if (!(tqflags
& KM_SLEEP
))
54 * We don't want to exceed tq_maxalloc, but we can't
55 * wait for other tasks to complete (and thus free up
56 * task structures) without risking deadlock with
57 * the caller. So, we just delay for one second
58 * to throttle the allocation rate. If we have tasks
59 * complete before one second timeout expires then
60 * taskq_ent_free will signal us and we will
61 * immediately retry the allocation.
63 tq
->tq_maxalloc_wait
++;
64 rv
= cv_timedwait(&tq
->tq_maxalloc_cv
,
65 &tq
->tq_lock
, ddi_get_lbolt() + hz
);
66 tq
->tq_maxalloc_wait
--;
68 goto again
; /* signaled */
70 mutex_exit(&tq
->tq_lock
);
72 t
= kmem_alloc(sizeof (taskq_ent_t
), tqflags
);
74 mutex_enter(&tq
->tq_lock
);
76 /* Make sure we start without any flags */
85 task_free(taskq_t
*tq
, taskq_ent_t
*t
)
87 if (tq
->tq_nalloc
<= tq
->tq_minalloc
) {
88 t
->tqent_next
= tq
->tq_freelist
;
92 mutex_exit(&tq
->tq_lock
);
93 kmem_free(t
, sizeof (taskq_ent_t
));
94 mutex_enter(&tq
->tq_lock
);
97 if (tq
->tq_maxalloc_wait
)
98 cv_signal(&tq
->tq_maxalloc_cv
);
102 taskq_dispatch(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t tqflags
)
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
);
117 if (tqflags
& TQ_FRONT
) {
118 t
->tqent_next
= tq
->tq_task
.tqent_next
;
119 t
->tqent_prev
= &tq
->tq_task
;
121 t
->tqent_next
= &tq
->tq_task
;
122 t
->tqent_prev
= tq
->tq_task
.tqent_prev
;
124 t
->tqent_next
->tqent_prev
= t
;
125 t
->tqent_prev
->tqent_next
= t
;
126 t
->tqent_func
= func
;
129 cv_signal(&tq
->tq_dispatch_cv
);
130 mutex_exit(&tq
->tq_lock
);
135 taskq_dispatch_delay(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t tqflags
,
142 taskq_empty_ent(taskq_ent_t
*t
)
144 return (t
->tqent_next
== NULL
);
148 taskq_init_ent(taskq_ent_t
*t
)
150 t
->tqent_next
= NULL
;
151 t
->tqent_prev
= NULL
;
152 t
->tqent_func
= NULL
;
158 taskq_dispatch_ent(taskq_t
*tq
, task_func_t func
, void *arg
, uint_t flags
,
161 ASSERT(func
!= NULL
);
164 * Mark it as a prealloc'd task. This is important
165 * to ensure that we don't free it later.
167 t
->tqent_flags
|= TQENT_FLAG_PREALLOC
;
169 * Enqueue the task to the underlying queue.
171 mutex_enter(&tq
->tq_lock
);
173 if (flags
& TQ_FRONT
) {
174 t
->tqent_next
= tq
->tq_task
.tqent_next
;
175 t
->tqent_prev
= &tq
->tq_task
;
177 t
->tqent_next
= &tq
->tq_task
;
178 t
->tqent_prev
= tq
->tq_task
.tqent_prev
;
180 t
->tqent_next
->tqent_prev
= t
;
181 t
->tqent_prev
->tqent_next
= t
;
182 t
->tqent_func
= func
;
184 cv_signal(&tq
->tq_dispatch_cv
);
185 mutex_exit(&tq
->tq_lock
);
189 taskq_wait(taskq_t
*tq
)
191 mutex_enter(&tq
->tq_lock
);
192 while (tq
->tq_task
.tqent_next
!= &tq
->tq_task
|| tq
->tq_active
!= 0)
193 cv_wait(&tq
->tq_wait_cv
, &tq
->tq_lock
);
194 mutex_exit(&tq
->tq_lock
);
198 taskq_wait_id(taskq_t
*tq
, taskqid_t id
)
204 taskq_wait_outstanding(taskq_t
*tq
, taskqid_t id
)
210 taskq_thread(void *arg
)
216 mutex_enter(&tq
->tq_lock
);
217 while (tq
->tq_flags
& TASKQ_ACTIVE
) {
218 if ((t
= tq
->tq_task
.tqent_next
) == &tq
->tq_task
) {
219 if (--tq
->tq_active
== 0)
220 cv_broadcast(&tq
->tq_wait_cv
);
221 cv_wait(&tq
->tq_dispatch_cv
, &tq
->tq_lock
);
225 t
->tqent_prev
->tqent_next
= t
->tqent_next
;
226 t
->tqent_next
->tqent_prev
= t
->tqent_prev
;
227 t
->tqent_next
= NULL
;
228 t
->tqent_prev
= NULL
;
229 prealloc
= t
->tqent_flags
& TQENT_FLAG_PREALLOC
;
230 mutex_exit(&tq
->tq_lock
);
232 rw_enter(&tq
->tq_threadlock
, RW_READER
);
233 t
->tqent_func(t
->tqent_arg
);
234 rw_exit(&tq
->tq_threadlock
);
236 mutex_enter(&tq
->tq_lock
);
241 cv_broadcast(&tq
->tq_wait_cv
);
242 mutex_exit(&tq
->tq_lock
);
248 taskq_create(const char *name
, int nthreads
, pri_t pri
,
249 int minalloc
, int maxalloc
, uint_t flags
)
251 taskq_t
*tq
= kmem_zalloc(sizeof (taskq_t
), KM_SLEEP
);
254 if (flags
& TASKQ_THREADS_CPU_PCT
) {
256 ASSERT3S(nthreads
, >=, 0);
257 ASSERT3S(nthreads
, <=, 100);
258 pct
= MIN(nthreads
, 100);
261 nthreads
= (sysconf(_SC_NPROCESSORS_ONLN
) * pct
) / 100;
262 nthreads
= MAX(nthreads
, 1); /* need at least 1 thread */
264 ASSERT3S(nthreads
, >=, 1);
267 rw_init(&tq
->tq_threadlock
, NULL
, RW_DEFAULT
, NULL
);
268 mutex_init(&tq
->tq_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
269 cv_init(&tq
->tq_dispatch_cv
, NULL
, CV_DEFAULT
, NULL
);
270 cv_init(&tq
->tq_wait_cv
, NULL
, CV_DEFAULT
, NULL
);
271 cv_init(&tq
->tq_maxalloc_cv
, NULL
, CV_DEFAULT
, NULL
);
272 (void) strncpy(tq
->tq_name
, name
, TASKQ_NAMELEN
);
273 tq
->tq_flags
= flags
| TASKQ_ACTIVE
;
274 tq
->tq_active
= nthreads
;
275 tq
->tq_nthreads
= nthreads
;
276 tq
->tq_minalloc
= minalloc
;
277 tq
->tq_maxalloc
= maxalloc
;
278 tq
->tq_task
.tqent_next
= &tq
->tq_task
;
279 tq
->tq_task
.tqent_prev
= &tq
->tq_task
;
280 tq
->tq_threadlist
= kmem_alloc(nthreads
* sizeof (kthread_t
*),
283 if (flags
& TASKQ_PREPOPULATE
) {
284 mutex_enter(&tq
->tq_lock
);
285 while (minalloc
-- > 0)
286 task_free(tq
, task_alloc(tq
, KM_SLEEP
));
287 mutex_exit(&tq
->tq_lock
);
290 for (t
= 0; t
< nthreads
; t
++)
291 VERIFY((tq
->tq_threadlist
[t
] = thread_create(NULL
, 0,
292 taskq_thread
, tq
, 0, &p0
, TS_RUN
, pri
)) != NULL
);
298 taskq_destroy(taskq_t
*tq
)
300 int nthreads
= tq
->tq_nthreads
;
304 mutex_enter(&tq
->tq_lock
);
306 tq
->tq_flags
&= ~TASKQ_ACTIVE
;
307 cv_broadcast(&tq
->tq_dispatch_cv
);
309 while (tq
->tq_nthreads
!= 0)
310 cv_wait(&tq
->tq_wait_cv
, &tq
->tq_lock
);
313 while (tq
->tq_nalloc
!= 0) {
314 ASSERT(tq
->tq_freelist
!= NULL
);
315 task_free(tq
, task_alloc(tq
, KM_SLEEP
));
318 mutex_exit(&tq
->tq_lock
);
320 kmem_free(tq
->tq_threadlist
, nthreads
* sizeof (kthread_t
*));
322 rw_destroy(&tq
->tq_threadlock
);
323 mutex_destroy(&tq
->tq_lock
);
324 cv_destroy(&tq
->tq_dispatch_cv
);
325 cv_destroy(&tq
->tq_wait_cv
);
326 cv_destroy(&tq
->tq_maxalloc_cv
);
328 kmem_free(tq
, sizeof (taskq_t
));
332 taskq_member(taskq_t
*tq
, kthread_t
*t
)
339 for (i
= 0; i
< tq
->tq_nthreads
; i
++)
340 if (tq
->tq_threadlist
[i
] == t
)
347 taskq_cancel_id(taskq_t
*tq
, taskqid_t id
)
353 system_taskq_init(void)
355 system_taskq
= taskq_create("system_taskq", 64, maxclsyspri
, 4, 512,
356 TASKQ_DYNAMIC
| TASKQ_PREPOPULATE
);
357 system_delay_taskq
= taskq_create("delay_taskq", 4, maxclsyspri
, 4,
358 512, TASKQ_DYNAMIC
| TASKQ_PREPOPULATE
);
362 system_taskq_fini(void)
364 taskq_destroy(system_taskq
);
365 system_taskq
= NULL
; /* defensive */
366 taskq_destroy(system_delay_taskq
);
367 system_delay_taskq
= NULL
;