]> git.proxmox.com Git - mirror_zfs-debian.git/blob - lib/libzpool/taskq.c
New upstream version 0.7.6
[mirror_zfs-debian.git] / lib / libzpool / taskq.c
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 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
24 */
25 /*
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.
29 */
30
31 #include <sys/zfs_context.h>
32
33 int taskq_now;
34 taskq_t *system_taskq;
35 taskq_t *system_delay_taskq;
36
37 #define TASKQ_ACTIVE 0x00010000
38
39 static taskq_ent_t *
40 task_alloc(taskq_t *tq, int tqflags)
41 {
42 taskq_ent_t *t;
43 int rv;
44
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;
48 } else {
49 if (tq->tq_nalloc >= tq->tq_maxalloc) {
50 if (!(tqflags & KM_SLEEP))
51 return (NULL);
52
53 /*
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.
62 */
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--;
67 if (rv > 0)
68 goto again; /* signaled */
69 }
70 mutex_exit(&tq->tq_lock);
71
72 t = kmem_alloc(sizeof (taskq_ent_t), tqflags);
73
74 mutex_enter(&tq->tq_lock);
75 if (t != NULL) {
76 /* Make sure we start without any flags */
77 t->tqent_flags = 0;
78 tq->tq_nalloc++;
79 }
80 }
81 return (t);
82 }
83
84 static void
85 task_free(taskq_t *tq, taskq_ent_t *t)
86 {
87 if (tq->tq_nalloc <= tq->tq_minalloc) {
88 t->tqent_next = tq->tq_freelist;
89 tq->tq_freelist = t;
90 } else {
91 tq->tq_nalloc--;
92 mutex_exit(&tq->tq_lock);
93 kmem_free(t, sizeof (taskq_ent_t));
94 mutex_enter(&tq->tq_lock);
95 }
96
97 if (tq->tq_maxalloc_wait)
98 cv_signal(&tq->tq_maxalloc_cv);
99 }
100
101 taskqid_t
102 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
103 {
104 taskq_ent_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 if (tqflags & TQ_FRONT) {
118 t->tqent_next = tq->tq_task.tqent_next;
119 t->tqent_prev = &tq->tq_task;
120 } else {
121 t->tqent_next = &tq->tq_task;
122 t->tqent_prev = tq->tq_task.tqent_prev;
123 }
124 t->tqent_next->tqent_prev = t;
125 t->tqent_prev->tqent_next = t;
126 t->tqent_func = func;
127 t->tqent_arg = arg;
128 t->tqent_flags = 0;
129 cv_signal(&tq->tq_dispatch_cv);
130 mutex_exit(&tq->tq_lock);
131 return (1);
132 }
133
134 taskqid_t
135 taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags,
136 clock_t expire_time)
137 {
138 return (0);
139 }
140
141 int
142 taskq_empty_ent(taskq_ent_t *t)
143 {
144 return (t->tqent_next == NULL);
145 }
146
147 void
148 taskq_init_ent(taskq_ent_t *t)
149 {
150 t->tqent_next = NULL;
151 t->tqent_prev = NULL;
152 t->tqent_func = NULL;
153 t->tqent_arg = NULL;
154 t->tqent_flags = 0;
155 }
156
157 void
158 taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
159 taskq_ent_t *t)
160 {
161 ASSERT(func != NULL);
162
163 /*
164 * Mark it as a prealloc'd task. This is important
165 * to ensure that we don't free it later.
166 */
167 t->tqent_flags |= TQENT_FLAG_PREALLOC;
168 /*
169 * Enqueue the task to the underlying queue.
170 */
171 mutex_enter(&tq->tq_lock);
172
173 if (flags & TQ_FRONT) {
174 t->tqent_next = tq->tq_task.tqent_next;
175 t->tqent_prev = &tq->tq_task;
176 } else {
177 t->tqent_next = &tq->tq_task;
178 t->tqent_prev = tq->tq_task.tqent_prev;
179 }
180 t->tqent_next->tqent_prev = t;
181 t->tqent_prev->tqent_next = t;
182 t->tqent_func = func;
183 t->tqent_arg = arg;
184 cv_signal(&tq->tq_dispatch_cv);
185 mutex_exit(&tq->tq_lock);
186 }
187
188 void
189 taskq_wait(taskq_t *tq)
190 {
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);
195 }
196
197 void
198 taskq_wait_id(taskq_t *tq, taskqid_t id)
199 {
200 taskq_wait(tq);
201 }
202
203 void
204 taskq_wait_outstanding(taskq_t *tq, taskqid_t id)
205 {
206 taskq_wait(tq);
207 }
208
209 static void
210 taskq_thread(void *arg)
211 {
212 taskq_t *tq = arg;
213 taskq_ent_t *t;
214 boolean_t prealloc;
215
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);
222 tq->tq_active++;
223 continue;
224 }
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);
231
232 rw_enter(&tq->tq_threadlock, RW_READER);
233 t->tqent_func(t->tqent_arg);
234 rw_exit(&tq->tq_threadlock);
235
236 mutex_enter(&tq->tq_lock);
237 if (!prealloc)
238 task_free(tq, t);
239 }
240 tq->tq_nthreads--;
241 cv_broadcast(&tq->tq_wait_cv);
242 mutex_exit(&tq->tq_lock);
243 thread_exit();
244 }
245
246 /*ARGSUSED*/
247 taskq_t *
248 taskq_create(const char *name, int nthreads, pri_t pri,
249 int minalloc, int maxalloc, uint_t flags)
250 {
251 taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
252 int t;
253
254 if (flags & TASKQ_THREADS_CPU_PCT) {
255 int pct;
256 ASSERT3S(nthreads, >=, 0);
257 ASSERT3S(nthreads, <=, 100);
258 pct = MIN(nthreads, 100);
259 pct = MAX(pct, 0);
260
261 nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100;
262 nthreads = MAX(nthreads, 1); /* need at least 1 thread */
263 } else {
264 ASSERT3S(nthreads, >=, 1);
265 }
266
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 *),
281 KM_SLEEP);
282
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);
288 }
289
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);
293
294 return (tq);
295 }
296
297 void
298 taskq_destroy(taskq_t *tq)
299 {
300 int nthreads = tq->tq_nthreads;
301
302 taskq_wait(tq);
303
304 mutex_enter(&tq->tq_lock);
305
306 tq->tq_flags &= ~TASKQ_ACTIVE;
307 cv_broadcast(&tq->tq_dispatch_cv);
308
309 while (tq->tq_nthreads != 0)
310 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
311
312 tq->tq_minalloc = 0;
313 while (tq->tq_nalloc != 0) {
314 ASSERT(tq->tq_freelist != NULL);
315 task_free(tq, task_alloc(tq, KM_SLEEP));
316 }
317
318 mutex_exit(&tq->tq_lock);
319
320 kmem_free(tq->tq_threadlist, nthreads * sizeof (kthread_t *));
321
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);
327
328 kmem_free(tq, sizeof (taskq_t));
329 }
330
331 int
332 taskq_member(taskq_t *tq, kthread_t *t)
333 {
334 int i;
335
336 if (taskq_now)
337 return (1);
338
339 for (i = 0; i < tq->tq_nthreads; i++)
340 if (tq->tq_threadlist[i] == t)
341 return (1);
342
343 return (0);
344 }
345
346 int
347 taskq_cancel_id(taskq_t *tq, taskqid_t id)
348 {
349 return (ENOENT);
350 }
351
352 void
353 system_taskq_init(void)
354 {
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);
359 }
360
361 void
362 system_taskq_fini(void)
363 {
364 taskq_destroy(system_taskq);
365 system_taskq = NULL; /* defensive */
366 taskq_destroy(system_delay_taskq);
367 system_delay_taskq = NULL;
368 }