]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libzpool/taskq.c
freebsd: remove __FBSDID macro use
[mirror_zfs.git] / lib / libzpool / taskq.c
CommitLineData
34dc7c2f
BB
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
1d3ba0bf 9 * or https://opensource.org/licenses/CDDL-1.0.
34dc7c2f
BB
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/*
428870ff 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
34dc7c2f
BB
23 * Use is subject to license terms.
24 */
a38718a6
GA
25/*
26 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
844793c3 27 * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved.
98b25418 28 * Copyright (c) 2014 by Delphix. All rights reserved.
a38718a6 29 */
34dc7c2f 30
34dc7c2f
BB
31#include <sys/zfs_context.h>
32
33int taskq_now;
b128c09f 34taskq_t *system_taskq;
57ddcda1 35taskq_t *system_delay_taskq;
34dc7c2f 36
b3212d2f
MA
37static pthread_key_t taskq_tsd;
38
34dc7c2f 39#define TASKQ_ACTIVE 0x00010000
34dc7c2f 40
a38718a6 41static taskq_ent_t *
34dc7c2f
BB
42task_alloc(taskq_t *tq, int tqflags)
43{
a38718a6 44 taskq_ent_t *t;
428870ff 45 int rv;
34dc7c2f 46
428870ff 47again: if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
a38718a6
GA
48 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
49 tq->tq_freelist = t->tqent_next;
34dc7c2f 50 } else {
34dc7c2f 51 if (tq->tq_nalloc >= tq->tq_maxalloc) {
428870ff 52 if (!(tqflags & KM_SLEEP))
34dc7c2f 53 return (NULL);
428870ff 54
34dc7c2f
BB
55 /*
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
428870ff
BB
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.
34dc7c2f 64 */
428870ff
BB
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--;
69 if (rv > 0)
70 goto again; /* signaled */
34dc7c2f 71 }
428870ff
BB
72 mutex_exit(&tq->tq_lock);
73
a38718a6 74 t = kmem_alloc(sizeof (taskq_ent_t), tqflags);
428870ff 75
34dc7c2f 76 mutex_enter(&tq->tq_lock);
a38718a6
GA
77 if (t != NULL) {
78 /* Make sure we start without any flags */
79 t->tqent_flags = 0;
34dc7c2f 80 tq->tq_nalloc++;
a38718a6 81 }
34dc7c2f
BB
82 }
83 return (t);
84}
85
86static void
a38718a6 87task_free(taskq_t *tq, taskq_ent_t *t)
34dc7c2f
BB
88{
89 if (tq->tq_nalloc <= tq->tq_minalloc) {
a38718a6 90 t->tqent_next = tq->tq_freelist;
34dc7c2f
BB
91 tq->tq_freelist = t;
92 } else {
93 tq->tq_nalloc--;
94 mutex_exit(&tq->tq_lock);
a38718a6 95 kmem_free(t, sizeof (taskq_ent_t));
34dc7c2f
BB
96 mutex_enter(&tq->tq_lock);
97 }
428870ff
BB
98
99 if (tq->tq_maxalloc_wait)
100 cv_signal(&tq->tq_maxalloc_cv);
34dc7c2f
BB
101}
102
103taskqid_t
104taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
105{
a38718a6 106 taskq_ent_t *t;
34dc7c2f
BB
107
108 if (taskq_now) {
109 func(arg);
110 return (1);
111 }
112
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);
117 return (0);
118 }
428870ff 119 if (tqflags & TQ_FRONT) {
a38718a6
GA
120 t->tqent_next = tq->tq_task.tqent_next;
121 t->tqent_prev = &tq->tq_task;
428870ff 122 } else {
a38718a6
GA
123 t->tqent_next = &tq->tq_task;
124 t->tqent_prev = tq->tq_task.tqent_prev;
428870ff 125 }
a38718a6
GA
126 t->tqent_next->tqent_prev = t;
127 t->tqent_prev->tqent_next = t;
128 t->tqent_func = func;
129 t->tqent_arg = arg;
844793c3 130 t->tqent_flags = 0;
34dc7c2f
BB
131 cv_signal(&tq->tq_dispatch_cv);
132 mutex_exit(&tq->tq_lock);
133 return (1);
134}
135
cc92e9d0 136taskqid_t
56050599 137taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags,
cc92e9d0
GW
138 clock_t expire_time)
139{
56050599 140 (void) tq, (void) func, (void) arg, (void) tqflags, (void) expire_time;
cc92e9d0
GW
141 return (0);
142}
143
a38718a6
GA
144int
145taskq_empty_ent(taskq_ent_t *t)
146{
d1d7e268 147 return (t->tqent_next == NULL);
a38718a6
GA
148}
149
150void
151taskq_init_ent(taskq_ent_t *t)
152{
153 t->tqent_next = NULL;
154 t->tqent_prev = NULL;
155 t->tqent_func = NULL;
156 t->tqent_arg = NULL;
157 t->tqent_flags = 0;
158}
159
160void
161taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
162 taskq_ent_t *t)
163{
164 ASSERT(func != NULL);
a38718a6
GA
165
166 /*
167 * Mark it as a prealloc'd task. This is important
168 * to ensure that we don't free it later.
169 */
170 t->tqent_flags |= TQENT_FLAG_PREALLOC;
171 /*
172 * Enqueue the task to the underlying queue.
173 */
174 mutex_enter(&tq->tq_lock);
175
176 if (flags & TQ_FRONT) {
177 t->tqent_next = tq->tq_task.tqent_next;
178 t->tqent_prev = &tq->tq_task;
179 } else {
180 t->tqent_next = &tq->tq_task;
181 t->tqent_prev = tq->tq_task.tqent_prev;
182 }
183 t->tqent_next->tqent_prev = t;
184 t->tqent_prev->tqent_next = t;
185 t->tqent_func = func;
186 t->tqent_arg = arg;
187 cv_signal(&tq->tq_dispatch_cv);
188 mutex_exit(&tq->tq_lock);
189}
190
34dc7c2f
BB
191void
192taskq_wait(taskq_t *tq)
193{
194 mutex_enter(&tq->tq_lock);
a38718a6 195 while (tq->tq_task.tqent_next != &tq->tq_task || tq->tq_active != 0)
34dc7c2f
BB
196 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
197 mutex_exit(&tq->tq_lock);
198}
199
044baf00
BB
200void
201taskq_wait_id(taskq_t *tq, taskqid_t id)
202{
56050599 203 (void) id;
044baf00
BB
204 taskq_wait(tq);
205}
206
4f34bd97
BB
207void
208taskq_wait_outstanding(taskq_t *tq, taskqid_t id)
209{
56050599 210 (void) id;
4f34bd97
BB
211 taskq_wait(tq);
212}
213
460748d4 214static __attribute__((noreturn)) void
34dc7c2f
BB
215taskq_thread(void *arg)
216{
217 taskq_t *tq = arg;
a38718a6
GA
218 taskq_ent_t *t;
219 boolean_t prealloc;
34dc7c2f 220
b3212d2f
MA
221 VERIFY0(pthread_setspecific(taskq_tsd, tq));
222
34dc7c2f
BB
223 mutex_enter(&tq->tq_lock);
224 while (tq->tq_flags & TASKQ_ACTIVE) {
a38718a6 225 if ((t = tq->tq_task.tqent_next) == &tq->tq_task) {
34dc7c2f
BB
226 if (--tq->tq_active == 0)
227 cv_broadcast(&tq->tq_wait_cv);
228 cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
229 tq->tq_active++;
230 continue;
231 }
a38718a6
GA
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;
34dc7c2f
BB
237 mutex_exit(&tq->tq_lock);
238
239 rw_enter(&tq->tq_threadlock, RW_READER);
a38718a6 240 t->tqent_func(t->tqent_arg);
34dc7c2f
BB
241 rw_exit(&tq->tq_threadlock);
242
243 mutex_enter(&tq->tq_lock);
a38718a6
GA
244 if (!prealloc)
245 task_free(tq, t);
34dc7c2f
BB
246 }
247 tq->tq_nthreads--;
248 cv_broadcast(&tq->tq_wait_cv);
249 mutex_exit(&tq->tq_lock);
1e33ac1e 250 thread_exit();
34dc7c2f
BB
251}
252
34dc7c2f
BB
253taskq_t *
254taskq_create(const char *name, int nthreads, pri_t pri,
4ea3f864 255 int minalloc, int maxalloc, uint_t flags)
34dc7c2f 256{
56050599 257 (void) pri;
34dc7c2f
BB
258 taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
259 int t;
260
9babb374
BB
261 if (flags & TASKQ_THREADS_CPU_PCT) {
262 int pct;
263 ASSERT3S(nthreads, >=, 0);
264 ASSERT3S(nthreads, <=, 100);
265 pct = MIN(nthreads, 100);
266 pct = MAX(pct, 0);
267
268 nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100;
269 nthreads = MAX(nthreads, 1); /* need at least 1 thread */
270 } else {
271 ASSERT3S(nthreads, >=, 1);
272 }
273
34dc7c2f
BB
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);
428870ff 278 cv_init(&tq->tq_maxalloc_cv, NULL, CV_DEFAULT, NULL);
7584fbe8 279 (void) strlcpy(tq->tq_name, name, sizeof (tq->tq_name));
34dc7c2f
BB
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;
a38718a6
GA
285 tq->tq_task.tqent_next = &tq->tq_task;
286 tq->tq_task.tqent_prev = &tq->tq_task;
d1d7e268
MK
287 tq->tq_threadlist = kmem_alloc(nthreads * sizeof (kthread_t *),
288 KM_SLEEP);
34dc7c2f
BB
289
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);
295 }
296
297 for (t = 0; t < nthreads; t++)
1e33ac1e 298 VERIFY((tq->tq_threadlist[t] = thread_create(NULL, 0,
498056ab 299 taskq_thread, tq, 0, &p0, TS_RUN, pri)) != NULL);
34dc7c2f
BB
300
301 return (tq);
302}
303
304void
305taskq_destroy(taskq_t *tq)
306{
34dc7c2f
BB
307 int nthreads = tq->tq_nthreads;
308
309 taskq_wait(tq);
310
311 mutex_enter(&tq->tq_lock);
312
313 tq->tq_flags &= ~TASKQ_ACTIVE;
314 cv_broadcast(&tq->tq_dispatch_cv);
315
316 while (tq->tq_nthreads != 0)
317 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
318
319 tq->tq_minalloc = 0;
320 while (tq->tq_nalloc != 0) {
321 ASSERT(tq->tq_freelist != NULL);
d62bafee
RY
322 taskq_ent_t *tqent_nexttq = tq->tq_freelist->tqent_next;
323 task_free(tq, tq->tq_freelist);
324 tq->tq_freelist = tqent_nexttq;
34dc7c2f
BB
325 }
326
327 mutex_exit(&tq->tq_lock);
328
1e33ac1e 329 kmem_free(tq->tq_threadlist, nthreads * sizeof (kthread_t *));
34dc7c2f
BB
330
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);
428870ff 335 cv_destroy(&tq->tq_maxalloc_cv);
34dc7c2f
BB
336
337 kmem_free(tq, sizeof (taskq_t));
338}
339
3bd4df38
EN
340/*
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
344 * by the caller.
345 */
346taskq_t *
347taskq_create_synced(const char *name, int nthreads, pri_t pri,
348 int minalloc, int maxalloc, uint_t flags, kthread_t ***ktpp)
349{
350 taskq_t *tq;
351 kthread_t **kthreads = kmem_zalloc(sizeof (*kthreads) * nthreads,
352 KM_SLEEP);
353
354 (void) pri; (void) minalloc; (void) maxalloc;
355
356 flags &= ~(TASKQ_DYNAMIC | TASKQ_THREADS_CPU_PCT | TASKQ_DC_BATCH);
357
358 tq = taskq_create(name, nthreads, minclsyspri, nthreads, INT_MAX,
359 flags | TASKQ_PREPOPULATE);
360 VERIFY(tq != NULL);
361 VERIFY(tq->tq_nthreads == nthreads);
362
363 for (int i = 0; i < nthreads; i++) {
364 kthreads[i] = tq->tq_threadlist[i];
365 }
366 *ktpp = kthreads;
367 return (tq);
368}
369
34dc7c2f 370int
1e33ac1e 371taskq_member(taskq_t *tq, kthread_t *t)
34dc7c2f
BB
372{
373 int i;
374
375 if (taskq_now)
376 return (1);
377
378 for (i = 0; i < tq->tq_nthreads; i++)
1e33ac1e 379 if (tq->tq_threadlist[i] == t)
34dc7c2f
BB
380 return (1);
381
382 return (0);
383}
b128c09f 384
b3212d2f
MA
385taskq_t *
386taskq_of_curthread(void)
387{
388 return (pthread_getspecific(taskq_tsd));
389}
390
cc92e9d0
GW
391int
392taskq_cancel_id(taskq_t *tq, taskqid_t id)
393{
56050599 394 (void) tq, (void) id;
cc92e9d0
GW
395 return (ENOENT);
396}
397
b128c09f
BB
398void
399system_taskq_init(void)
400{
b3212d2f 401 VERIFY0(pthread_key_create(&taskq_tsd, NULL));
1229323d 402 system_taskq = taskq_create("system_taskq", 64, maxclsyspri, 4, 512,
b128c09f 403 TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
57ddcda1
CC
404 system_delay_taskq = taskq_create("delay_taskq", 4, maxclsyspri, 4,
405 512, TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
b128c09f 406}
428870ff
BB
407
408void
409system_taskq_fini(void)
410{
411 taskq_destroy(system_taskq);
412 system_taskq = NULL; /* defensive */
57ddcda1
CC
413 taskq_destroy(system_delay_taskq);
414 system_delay_taskq = NULL;
b3212d2f 415 VERIFY0(pthread_key_delete(taskq_tsd));
428870ff 416}