]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - zfs/lib/libzpool/taskq.c
UBUNTU: SAUCE: Update zfs to e02aaf17f15ad274fa1f24c9c826f1477911ea3f
[mirror_ubuntu-zesty-kernel.git] / zfs / 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
38 static taskq_ent_t *
39 task_alloc(taskq_t *tq, int tqflags)
40 {
41 taskq_ent_t *t;
42 int rv;
43
44 again: if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) {
45 ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC));
46 tq->tq_freelist = t->tqent_next;
47 } else {
48 if (tq->tq_nalloc >= tq->tq_maxalloc) {
49 if (!(tqflags & KM_SLEEP))
50 return (NULL);
51
52 /*
53 * We don't want to exceed tq_maxalloc, but we can't
54 * wait for other tasks to complete (and thus free up
55 * task structures) without risking deadlock with
56 * the caller. So, we just delay for one second
57 * to throttle the allocation rate. If we have tasks
58 * complete before one second timeout expires then
59 * taskq_ent_free will signal us and we will
60 * immediately retry the allocation.
61 */
62 tq->tq_maxalloc_wait++;
63 rv = cv_timedwait(&tq->tq_maxalloc_cv,
64 &tq->tq_lock, ddi_get_lbolt() + hz);
65 tq->tq_maxalloc_wait--;
66 if (rv > 0)
67 goto again; /* signaled */
68 }
69 mutex_exit(&tq->tq_lock);
70
71 t = kmem_alloc(sizeof (taskq_ent_t), tqflags);
72
73 mutex_enter(&tq->tq_lock);
74 if (t != NULL) {
75 /* Make sure we start without any flags */
76 t->tqent_flags = 0;
77 tq->tq_nalloc++;
78 }
79 }
80 return (t);
81 }
82
83 static void
84 task_free(taskq_t *tq, taskq_ent_t *t)
85 {
86 if (tq->tq_nalloc <= tq->tq_minalloc) {
87 t->tqent_next = tq->tq_freelist;
88 tq->tq_freelist = t;
89 } else {
90 tq->tq_nalloc--;
91 mutex_exit(&tq->tq_lock);
92 kmem_free(t, sizeof (taskq_ent_t));
93 mutex_enter(&tq->tq_lock);
94 }
95
96 if (tq->tq_maxalloc_wait)
97 cv_signal(&tq->tq_maxalloc_cv);
98 }
99
100 taskqid_t
101 taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags)
102 {
103 taskq_ent_t *t;
104
105 if (taskq_now) {
106 func(arg);
107 return (1);
108 }
109
110 mutex_enter(&tq->tq_lock);
111 ASSERT(tq->tq_flags & TASKQ_ACTIVE);
112 if ((t = task_alloc(tq, tqflags)) == NULL) {
113 mutex_exit(&tq->tq_lock);
114 return (0);
115 }
116 if (tqflags & TQ_FRONT) {
117 t->tqent_next = tq->tq_task.tqent_next;
118 t->tqent_prev = &tq->tq_task;
119 } else {
120 t->tqent_next = &tq->tq_task;
121 t->tqent_prev = tq->tq_task.tqent_prev;
122 }
123 t->tqent_next->tqent_prev = t;
124 t->tqent_prev->tqent_next = t;
125 t->tqent_func = func;
126 t->tqent_arg = arg;
127 t->tqent_flags = 0;
128 cv_signal(&tq->tq_dispatch_cv);
129 mutex_exit(&tq->tq_lock);
130 return (1);
131 }
132
133 taskqid_t
134 taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags,
135 clock_t expire_time)
136 {
137 return (0);
138 }
139
140 int
141 taskq_empty_ent(taskq_ent_t *t)
142 {
143 return (t->tqent_next == NULL);
144 }
145
146 void
147 taskq_init_ent(taskq_ent_t *t)
148 {
149 t->tqent_next = NULL;
150 t->tqent_prev = NULL;
151 t->tqent_func = NULL;
152 t->tqent_arg = NULL;
153 t->tqent_flags = 0;
154 }
155
156 void
157 taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags,
158 taskq_ent_t *t)
159 {
160 ASSERT(func != NULL);
161
162 /*
163 * Mark it as a prealloc'd task. This is important
164 * to ensure that we don't free it later.
165 */
166 t->tqent_flags |= TQENT_FLAG_PREALLOC;
167 /*
168 * Enqueue the task to the underlying queue.
169 */
170 mutex_enter(&tq->tq_lock);
171
172 if (flags & TQ_FRONT) {
173 t->tqent_next = tq->tq_task.tqent_next;
174 t->tqent_prev = &tq->tq_task;
175 } else {
176 t->tqent_next = &tq->tq_task;
177 t->tqent_prev = tq->tq_task.tqent_prev;
178 }
179 t->tqent_next->tqent_prev = t;
180 t->tqent_prev->tqent_next = t;
181 t->tqent_func = func;
182 t->tqent_arg = arg;
183 cv_signal(&tq->tq_dispatch_cv);
184 mutex_exit(&tq->tq_lock);
185 }
186
187 void
188 taskq_wait(taskq_t *tq)
189 {
190 mutex_enter(&tq->tq_lock);
191 while (tq->tq_task.tqent_next != &tq->tq_task || tq->tq_active != 0)
192 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
193 mutex_exit(&tq->tq_lock);
194 }
195
196 void
197 taskq_wait_id(taskq_t *tq, taskqid_t id)
198 {
199 taskq_wait(tq);
200 }
201
202 void
203 taskq_wait_outstanding(taskq_t *tq, taskqid_t id)
204 {
205 taskq_wait(tq);
206 }
207
208 static void
209 taskq_thread(void *arg)
210 {
211 taskq_t *tq = arg;
212 taskq_ent_t *t;
213 boolean_t prealloc;
214
215 mutex_enter(&tq->tq_lock);
216 while (tq->tq_flags & TASKQ_ACTIVE) {
217 if ((t = tq->tq_task.tqent_next) == &tq->tq_task) {
218 if (--tq->tq_active == 0)
219 cv_broadcast(&tq->tq_wait_cv);
220 cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock);
221 tq->tq_active++;
222 continue;
223 }
224 t->tqent_prev->tqent_next = t->tqent_next;
225 t->tqent_next->tqent_prev = t->tqent_prev;
226 t->tqent_next = NULL;
227 t->tqent_prev = NULL;
228 prealloc = t->tqent_flags & TQENT_FLAG_PREALLOC;
229 mutex_exit(&tq->tq_lock);
230
231 rw_enter(&tq->tq_threadlock, RW_READER);
232 t->tqent_func(t->tqent_arg);
233 rw_exit(&tq->tq_threadlock);
234
235 mutex_enter(&tq->tq_lock);
236 if (!prealloc)
237 task_free(tq, t);
238 }
239 tq->tq_nthreads--;
240 cv_broadcast(&tq->tq_wait_cv);
241 mutex_exit(&tq->tq_lock);
242 thread_exit();
243 }
244
245 /*ARGSUSED*/
246 taskq_t *
247 taskq_create(const char *name, int nthreads, pri_t pri,
248 int minalloc, int maxalloc, uint_t flags)
249 {
250 taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP);
251 int t;
252
253 if (flags & TASKQ_THREADS_CPU_PCT) {
254 int pct;
255 ASSERT3S(nthreads, >=, 0);
256 ASSERT3S(nthreads, <=, 100);
257 pct = MIN(nthreads, 100);
258 pct = MAX(pct, 0);
259
260 nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100;
261 nthreads = MAX(nthreads, 1); /* need at least 1 thread */
262 } else {
263 ASSERT3S(nthreads, >=, 1);
264 }
265
266 rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL);
267 mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL);
268 cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL);
269 cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL);
270 cv_init(&tq->tq_maxalloc_cv, NULL, CV_DEFAULT, NULL);
271 (void) strncpy(tq->tq_name, name, TASKQ_NAMELEN);
272 tq->tq_flags = flags | TASKQ_ACTIVE;
273 tq->tq_active = nthreads;
274 tq->tq_nthreads = nthreads;
275 tq->tq_minalloc = minalloc;
276 tq->tq_maxalloc = maxalloc;
277 tq->tq_task.tqent_next = &tq->tq_task;
278 tq->tq_task.tqent_prev = &tq->tq_task;
279 tq->tq_threadlist = kmem_alloc(nthreads * sizeof (kthread_t *),
280 KM_SLEEP);
281
282 if (flags & TASKQ_PREPOPULATE) {
283 mutex_enter(&tq->tq_lock);
284 while (minalloc-- > 0)
285 task_free(tq, task_alloc(tq, KM_SLEEP));
286 mutex_exit(&tq->tq_lock);
287 }
288
289 for (t = 0; t < nthreads; t++)
290 VERIFY((tq->tq_threadlist[t] = thread_create(NULL, 0,
291 taskq_thread, tq, 0, &p0, TS_RUN, pri)) != NULL);
292
293 return (tq);
294 }
295
296 void
297 taskq_destroy(taskq_t *tq)
298 {
299 int nthreads = tq->tq_nthreads;
300
301 taskq_wait(tq);
302
303 mutex_enter(&tq->tq_lock);
304
305 tq->tq_flags &= ~TASKQ_ACTIVE;
306 cv_broadcast(&tq->tq_dispatch_cv);
307
308 while (tq->tq_nthreads != 0)
309 cv_wait(&tq->tq_wait_cv, &tq->tq_lock);
310
311 tq->tq_minalloc = 0;
312 while (tq->tq_nalloc != 0) {
313 ASSERT(tq->tq_freelist != NULL);
314 task_free(tq, task_alloc(tq, KM_SLEEP));
315 }
316
317 mutex_exit(&tq->tq_lock);
318
319 kmem_free(tq->tq_threadlist, nthreads * sizeof (kthread_t *));
320
321 rw_destroy(&tq->tq_threadlock);
322 mutex_destroy(&tq->tq_lock);
323 cv_destroy(&tq->tq_dispatch_cv);
324 cv_destroy(&tq->tq_wait_cv);
325 cv_destroy(&tq->tq_maxalloc_cv);
326
327 kmem_free(tq, sizeof (taskq_t));
328 }
329
330 int
331 taskq_member(taskq_t *tq, kthread_t *t)
332 {
333 int i;
334
335 if (taskq_now)
336 return (1);
337
338 for (i = 0; i < tq->tq_nthreads; i++)
339 if (tq->tq_threadlist[i] == t)
340 return (1);
341
342 return (0);
343 }
344
345 int
346 taskq_cancel_id(taskq_t *tq, taskqid_t id)
347 {
348 return (ENOENT);
349 }
350
351 void
352 system_taskq_init(void)
353 {
354 system_taskq = taskq_create("system_taskq", 64, maxclsyspri, 4, 512,
355 TASKQ_DYNAMIC | TASKQ_PREPOPULATE);
356 }
357
358 void
359 system_taskq_fini(void)
360 {
361 taskq_destroy(system_taskq);
362 system_taskq = NULL; /* defensive */
363 }