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