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