]> git.proxmox.com Git - mirror_zfs.git/blob - lib/libzpool/taskq.c
Add taskq_wait_outstanding() function
[mirror_zfs.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
36 #define TASKQ_ACTIVE 0x00010000
37 #define TASKQ_NAMELEN 31
38
39 struct taskq {
40 char tq_name[TASKQ_NAMELEN + 1];
41 kmutex_t tq_lock;
42 krwlock_t tq_threadlock;
43 kcondvar_t tq_dispatch_cv;
44 kcondvar_t tq_wait_cv;
45 kthread_t **tq_threadlist;
46 int tq_flags;
47 int tq_active;
48 int tq_nthreads;
49 int tq_nalloc;
50 int tq_minalloc;
51 int tq_maxalloc;
52 kcondvar_t tq_maxalloc_cv;
53 int tq_maxalloc_wait;
54 taskq_ent_t *tq_freelist;
55 taskq_ent_t tq_task;
56 };
57
58 static taskq_ent_t *
59 task_alloc(taskq_t *tq, int tqflags)
60 {
61 taskq_ent_t *t;
62 int rv;
63
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;
67 } else {
68 if (tq->tq_nalloc >= tq->tq_maxalloc) {
69 if (!(tqflags & KM_SLEEP))
70 return (NULL);
71
72 /*
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.
81 */
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--;
86 if (rv > 0)
87 goto again; /* signaled */
88 }
89 mutex_exit(&tq->tq_lock);
90
91 t = kmem_alloc(sizeof (taskq_ent_t), tqflags);
92
93 mutex_enter(&tq->tq_lock);
94 if (t != NULL) {
95 /* Make sure we start without any flags */
96 t->tqent_flags = 0;
97 tq->tq_nalloc++;
98 }
99 }
100 return (t);
101 }
102
103 static void
104 task_free(taskq_t *tq, taskq_ent_t *t)
105 {
106 if (tq->tq_nalloc <= tq->tq_minalloc) {
107 t->tqent_next = tq->tq_freelist;
108 tq->tq_freelist = t;
109 } else {
110 tq->tq_nalloc--;
111 mutex_exit(&tq->tq_lock);
112 kmem_free(t, sizeof (taskq_ent_t));
113 mutex_enter(&tq->tq_lock);
114 }
115
116 if (tq->tq_maxalloc_wait)
117 cv_signal(&tq->tq_maxalloc_cv);
118 }
119
120 taskqid_t
121 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
122 {
123 taskq_ent_t *t;
124
125 if (taskq_now) {
126 func(arg);
127 return (1);
128 }
129
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);
134 return (0);
135 }
136 if (tqflags & TQ_FRONT) {
137 t->tqent_next = tq->tq_task.tqent_next;
138 t->tqent_prev = &tq->tq_task;
139 } else {
140 t->tqent_next = &tq->tq_task;
141 t->tqent_prev = tq->tq_task.tqent_prev;
142 }
143 t->tqent_next->tqent_prev = t;
144 t->tqent_prev->tqent_next = t;
145 t->tqent_func = func;
146 t->tqent_arg = arg;
147 t->tqent_flags = 0;
148 cv_signal(&tq->tq_dispatch_cv);
149 mutex_exit(&tq->tq_lock);
150 return (1);
151 }
152
153 taskqid_t
154 taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags,
155 clock_t expire_time)
156 {
157 return (0);
158 }
159
160 int
161 taskq_empty_ent(taskq_ent_t *t)
162 {
163 return (t->tqent_next == NULL);
164 }
165
166 void
167 taskq_init_ent(taskq_ent_t *t)
168 {
169 t->tqent_next = NULL;
170 t->tqent_prev = NULL;
171 t->tqent_func = NULL;
172 t->tqent_arg = NULL;
173 t->tqent_flags = 0;
174 }
175
176 void
177 taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
178 taskq_ent_t *t)
179 {
180 ASSERT(func != NULL);
181 ASSERT(!(tq->tq_flags & TASKQ_DYNAMIC));
182
183 /*
184 * Mark it as a prealloc'd task. This is important
185 * to ensure that we don't free it later.
186 */
187 t->tqent_flags |= TQENT_FLAG_PREALLOC;
188 /*
189 * Enqueue the task to the underlying queue.
190 */
191 mutex_enter(&tq->tq_lock);
192
193 if (flags & TQ_FRONT) {
194 t->tqent_next = tq->tq_task.tqent_next;
195 t->tqent_prev = &tq->tq_task;
196 } else {
197 t->tqent_next = &tq->tq_task;
198 t->tqent_prev = tq->tq_task.tqent_prev;
199 }
200 t->tqent_next->tqent_prev = t;
201 t->tqent_prev->tqent_next = t;
202 t->tqent_func = func;
203 t->tqent_arg = arg;
204 cv_signal(&tq->tq_dispatch_cv);
205 mutex_exit(&tq->tq_lock);
206 }
207
208 void
209 taskq_wait(taskq_t *tq)
210 {
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);
215 }
216
217 void
218 taskq_wait_id(taskq_t *tq, taskqid_t id)
219 {
220 taskq_wait(tq);
221 }
222
223 void
224 taskq_wait_outstanding(taskq_t *tq, taskqid_t id)
225 {
226 taskq_wait(tq);
227 }
228
229 static void
230 taskq_thread(void *arg)
231 {
232 taskq_t *tq = arg;
233 taskq_ent_t *t;
234 boolean_t prealloc;
235
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);
242 tq->tq_active++;
243 continue;
244 }
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);
251
252 rw_enter(&tq->tq_threadlock, RW_READER);
253 t->tqent_func(t->tqent_arg);
254 rw_exit(&tq->tq_threadlock);
255
256 mutex_enter(&tq->tq_lock);
257 if (!prealloc)
258 task_free(tq, t);
259 }
260 tq->tq_nthreads--;
261 cv_broadcast(&tq->tq_wait_cv);
262 mutex_exit(&tq->tq_lock);
263 thread_exit();
264 }
265
266 /*ARGSUSED*/
267 taskq_t *
268 taskq_create(const char *name, int nthreads, pri_t pri,
269 int minalloc, int maxalloc, uint_t flags)
270 {
271 taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
272 int t;
273
274 if (flags & TASKQ_THREADS_CPU_PCT) {
275 int pct;
276 ASSERT3S(nthreads, >=, 0);
277 ASSERT3S(nthreads, <=, 100);
278 pct = MIN(nthreads, 100);
279 pct = MAX(pct, 0);
280
281 nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100;
282 nthreads = MAX(nthreads, 1); /* need at least 1 thread */
283 } else {
284 ASSERT3S(nthreads, >=, 1);
285 }
286
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 *),
301 KM_SLEEP);
302
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);
308 }
309
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);
313
314 return (tq);
315 }
316
317 void
318 taskq_destroy(taskq_t *tq)
319 {
320 int nthreads = tq->tq_nthreads;
321
322 taskq_wait(tq);
323
324 mutex_enter(&tq->tq_lock);
325
326 tq->tq_flags &= ~TASKQ_ACTIVE;
327 cv_broadcast(&tq->tq_dispatch_cv);
328
329 while (tq->tq_nthreads != 0)
330 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
331
332 tq->tq_minalloc = 0;
333 while (tq->tq_nalloc != 0) {
334 ASSERT(tq->tq_freelist != NULL);
335 task_free(tq, task_alloc(tq, KM_SLEEP));
336 }
337
338 mutex_exit(&tq->tq_lock);
339
340 kmem_free(tq->tq_threadlist, nthreads * sizeof (kthread_t *));
341
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);
347
348 kmem_free(tq, sizeof (taskq_t));
349 }
350
351 int
352 taskq_member(taskq_t *tq, kthread_t *t)
353 {
354 int i;
355
356 if (taskq_now)
357 return (1);
358
359 for (i = 0; i < tq->tq_nthreads; i++)
360 if (tq->tq_threadlist[i] == t)
361 return (1);
362
363 return (0);
364 }
365
366 int
367 taskq_cancel_id(taskq_t *tq, taskqid_t id)
368 {
369 return (ENOENT);
370 }
371
372 void
373 system_taskq_init(void)
374 {
375 system_taskq = taskq_create("system_taskq", 64, minclsyspri, 4, 512,
376 TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
377 }
378
379 void
380 system_taskq_fini(void)
381 {
382 taskq_destroy(system_taskq);
383 system_taskq = NULL; /* defensive */
384 }