]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
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 | /* | |
428870ff | 22 | * Copyright 2010 Sun Microsystems, Inc. All rights reserved. |
34dc7c2f BB |
23 | * Use is subject to license terms. |
24 | */ | |
a38718a6 GA |
25 | /* |
26 | * Copyright 2011 Nexenta Systems, Inc. All rights reserved. | |
844793c3 | 27 | * Copyright 2012 Garrett D'Amore <garrett@damore.org>. All rights reserved. |
a38718a6 | 28 | */ |
34dc7c2f | 29 | |
34dc7c2f BB |
30 | #include <sys/zfs_context.h> |
31 | ||
32 | int taskq_now; | |
b128c09f | 33 | taskq_t *system_taskq; |
34dc7c2f | 34 | |
34dc7c2f BB |
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; | |
1e33ac1e | 42 | kthread_t **tq_threadlist; |
34dc7c2f BB |
43 | int tq_flags; |
44 | int tq_active; | |
45 | int tq_nthreads; | |
46 | int tq_nalloc; | |
47 | int tq_minalloc; | |
48 | int tq_maxalloc; | |
428870ff BB |
49 | kcondvar_t tq_maxalloc_cv; |
50 | int tq_maxalloc_wait; | |
a38718a6 GA |
51 | taskq_ent_t *tq_freelist; |
52 | taskq_ent_t tq_task; | |
34dc7c2f BB |
53 | }; |
54 | ||
a38718a6 | 55 | static taskq_ent_t * |
34dc7c2f BB |
56 | task_alloc(taskq_t *tq, int tqflags) |
57 | { | |
a38718a6 | 58 | taskq_ent_t *t; |
428870ff | 59 | int rv; |
34dc7c2f | 60 | |
428870ff | 61 | again: if ((t = tq->tq_freelist) != NULL && tq->tq_nalloc >= tq->tq_minalloc) { |
a38718a6 GA |
62 | ASSERT(!(t->tqent_flags & TQENT_FLAG_PREALLOC)); |
63 | tq->tq_freelist = t->tqent_next; | |
34dc7c2f | 64 | } else { |
34dc7c2f | 65 | if (tq->tq_nalloc >= tq->tq_maxalloc) { |
428870ff | 66 | if (!(tqflags & KM_SLEEP)) |
34dc7c2f | 67 | return (NULL); |
428870ff | 68 | |
34dc7c2f BB |
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 | |
428870ff BB |
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. | |
34dc7c2f | 78 | */ |
428870ff BB |
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 */ | |
34dc7c2f | 85 | } |
428870ff BB |
86 | mutex_exit(&tq->tq_lock); |
87 | ||
a38718a6 | 88 | t = kmem_alloc(sizeof (taskq_ent_t), tqflags); |
428870ff | 89 | |
34dc7c2f | 90 | mutex_enter(&tq->tq_lock); |
a38718a6 GA |
91 | if (t != NULL) { |
92 | /* Make sure we start without any flags */ | |
93 | t->tqent_flags = 0; | |
34dc7c2f | 94 | tq->tq_nalloc++; |
a38718a6 | 95 | } |
34dc7c2f BB |
96 | } |
97 | return (t); | |
98 | } | |
99 | ||
100 | static void | |
a38718a6 | 101 | task_free(taskq_t *tq, taskq_ent_t *t) |
34dc7c2f BB |
102 | { |
103 | if (tq->tq_nalloc <= tq->tq_minalloc) { | |
a38718a6 | 104 | t->tqent_next = tq->tq_freelist; |
34dc7c2f BB |
105 | tq->tq_freelist = t; |
106 | } else { | |
107 | tq->tq_nalloc--; | |
108 | mutex_exit(&tq->tq_lock); | |
a38718a6 | 109 | kmem_free(t, sizeof (taskq_ent_t)); |
34dc7c2f BB |
110 | mutex_enter(&tq->tq_lock); |
111 | } | |
428870ff BB |
112 | |
113 | if (tq->tq_maxalloc_wait) | |
114 | cv_signal(&tq->tq_maxalloc_cv); | |
34dc7c2f BB |
115 | } |
116 | ||
117 | taskqid_t | |
118 | taskq_dispatch(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags) | |
119 | { | |
a38718a6 | 120 | taskq_ent_t *t; |
34dc7c2f BB |
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 | } | |
428870ff | 133 | if (tqflags & TQ_FRONT) { |
a38718a6 GA |
134 | t->tqent_next = tq->tq_task.tqent_next; |
135 | t->tqent_prev = &tq->tq_task; | |
428870ff | 136 | } else { |
a38718a6 GA |
137 | t->tqent_next = &tq->tq_task; |
138 | t->tqent_prev = tq->tq_task.tqent_prev; | |
428870ff | 139 | } |
a38718a6 GA |
140 | t->tqent_next->tqent_prev = t; |
141 | t->tqent_prev->tqent_next = t; | |
142 | t->tqent_func = func; | |
143 | t->tqent_arg = arg; | |
844793c3 | 144 | t->tqent_flags = 0; |
34dc7c2f BB |
145 | cv_signal(&tq->tq_dispatch_cv); |
146 | mutex_exit(&tq->tq_lock); | |
147 | return (1); | |
148 | } | |
149 | ||
cc92e9d0 GW |
150 | taskqid_t |
151 | taskq_dispatch_delay(taskq_t *tq, task_func_t func, void *arg, uint_t tqflags, | |
152 | clock_t expire_time) | |
153 | { | |
154 | return (0); | |
155 | } | |
156 | ||
a38718a6 GA |
157 | int |
158 | taskq_empty_ent(taskq_ent_t *t) | |
159 | { | |
160 | return t->tqent_next == NULL; | |
161 | } | |
162 | ||
163 | void | |
164 | taskq_init_ent(taskq_ent_t *t) | |
165 | { | |
166 | t->tqent_next = NULL; | |
167 | t->tqent_prev = NULL; | |
168 | t->tqent_func = NULL; | |
169 | t->tqent_arg = NULL; | |
170 | t->tqent_flags = 0; | |
171 | } | |
172 | ||
173 | void | |
174 | taskq_dispatch_ent(taskq_t *tq, task_func_t func, void *arg, uint_t flags, | |
175 | taskq_ent_t *t) | |
176 | { | |
177 | ASSERT(func != NULL); | |
178 | ASSERT(!(tq->tq_flags & TASKQ_DYNAMIC)); | |
179 | ||
180 | /* | |
181 | * Mark it as a prealloc'd task. This is important | |
182 | * to ensure that we don't free it later. | |
183 | */ | |
184 | t->tqent_flags |= TQENT_FLAG_PREALLOC; | |
185 | /* | |
186 | * Enqueue the task to the underlying queue. | |
187 | */ | |
188 | mutex_enter(&tq->tq_lock); | |
189 | ||
190 | if (flags & TQ_FRONT) { | |
191 | t->tqent_next = tq->tq_task.tqent_next; | |
192 | t->tqent_prev = &tq->tq_task; | |
193 | } else { | |
194 | t->tqent_next = &tq->tq_task; | |
195 | t->tqent_prev = tq->tq_task.tqent_prev; | |
196 | } | |
197 | t->tqent_next->tqent_prev = t; | |
198 | t->tqent_prev->tqent_next = t; | |
199 | t->tqent_func = func; | |
200 | t->tqent_arg = arg; | |
201 | cv_signal(&tq->tq_dispatch_cv); | |
202 | mutex_exit(&tq->tq_lock); | |
203 | } | |
204 | ||
34dc7c2f BB |
205 | void |
206 | taskq_wait(taskq_t *tq) | |
207 | { | |
208 | mutex_enter(&tq->tq_lock); | |
a38718a6 | 209 | while (tq->tq_task.tqent_next != &tq->tq_task || tq->tq_active != 0) |
34dc7c2f BB |
210 | cv_wait(&tq->tq_wait_cv, &tq->tq_lock); |
211 | mutex_exit(&tq->tq_lock); | |
212 | } | |
213 | ||
044baf00 BB |
214 | void |
215 | taskq_wait_id(taskq_t *tq, taskqid_t id) | |
216 | { | |
217 | taskq_wait(tq); | |
218 | } | |
219 | ||
1e33ac1e | 220 | static void |
34dc7c2f BB |
221 | taskq_thread(void *arg) |
222 | { | |
223 | taskq_t *tq = arg; | |
a38718a6 GA |
224 | taskq_ent_t *t; |
225 | boolean_t prealloc; | |
34dc7c2f BB |
226 | |
227 | mutex_enter(&tq->tq_lock); | |
228 | while (tq->tq_flags & TASKQ_ACTIVE) { | |
a38718a6 | 229 | if ((t = tq->tq_task.tqent_next) == &tq->tq_task) { |
34dc7c2f BB |
230 | if (--tq->tq_active == 0) |
231 | cv_broadcast(&tq->tq_wait_cv); | |
232 | cv_wait(&tq->tq_dispatch_cv, &tq->tq_lock); | |
233 | tq->tq_active++; | |
234 | continue; | |
235 | } | |
a38718a6 GA |
236 | t->tqent_prev->tqent_next = t->tqent_next; |
237 | t->tqent_next->tqent_prev = t->tqent_prev; | |
238 | t->tqent_next = NULL; | |
239 | t->tqent_prev = NULL; | |
240 | prealloc = t->tqent_flags & TQENT_FLAG_PREALLOC; | |
34dc7c2f BB |
241 | mutex_exit(&tq->tq_lock); |
242 | ||
243 | rw_enter(&tq->tq_threadlock, RW_READER); | |
a38718a6 | 244 | t->tqent_func(t->tqent_arg); |
34dc7c2f BB |
245 | rw_exit(&tq->tq_threadlock); |
246 | ||
247 | mutex_enter(&tq->tq_lock); | |
a38718a6 GA |
248 | if (!prealloc) |
249 | task_free(tq, t); | |
34dc7c2f BB |
250 | } |
251 | tq->tq_nthreads--; | |
252 | cv_broadcast(&tq->tq_wait_cv); | |
253 | mutex_exit(&tq->tq_lock); | |
1e33ac1e | 254 | thread_exit(); |
34dc7c2f BB |
255 | } |
256 | ||
257 | /*ARGSUSED*/ | |
258 | taskq_t * | |
259 | taskq_create(const char *name, int nthreads, pri_t pri, | |
260 | int minalloc, int maxalloc, uint_t flags) | |
261 | { | |
262 | taskq_t *tq = kmem_zalloc(sizeof (taskq_t), KM_SLEEP); | |
263 | int t; | |
264 | ||
9babb374 BB |
265 | if (flags & TASKQ_THREADS_CPU_PCT) { |
266 | int pct; | |
267 | ASSERT3S(nthreads, >=, 0); | |
268 | ASSERT3S(nthreads, <=, 100); | |
269 | pct = MIN(nthreads, 100); | |
270 | pct = MAX(pct, 0); | |
271 | ||
272 | nthreads = (sysconf(_SC_NPROCESSORS_ONLN) * pct) / 100; | |
273 | nthreads = MAX(nthreads, 1); /* need at least 1 thread */ | |
274 | } else { | |
275 | ASSERT3S(nthreads, >=, 1); | |
276 | } | |
277 | ||
34dc7c2f BB |
278 | rw_init(&tq->tq_threadlock, NULL, RW_DEFAULT, NULL); |
279 | mutex_init(&tq->tq_lock, NULL, MUTEX_DEFAULT, NULL); | |
280 | cv_init(&tq->tq_dispatch_cv, NULL, CV_DEFAULT, NULL); | |
281 | cv_init(&tq->tq_wait_cv, NULL, CV_DEFAULT, NULL); | |
428870ff | 282 | cv_init(&tq->tq_maxalloc_cv, NULL, CV_DEFAULT, NULL); |
34dc7c2f BB |
283 | tq->tq_flags = flags | TASKQ_ACTIVE; |
284 | tq->tq_active = nthreads; | |
285 | tq->tq_nthreads = nthreads; | |
286 | tq->tq_minalloc = minalloc; | |
287 | tq->tq_maxalloc = maxalloc; | |
a38718a6 GA |
288 | tq->tq_task.tqent_next = &tq->tq_task; |
289 | tq->tq_task.tqent_prev = &tq->tq_task; | |
1e33ac1e | 290 | tq->tq_threadlist = kmem_alloc(nthreads*sizeof(kthread_t *), KM_SLEEP); |
34dc7c2f BB |
291 | |
292 | if (flags & TASKQ_PREPOPULATE) { | |
293 | mutex_enter(&tq->tq_lock); | |
294 | while (minalloc-- > 0) | |
295 | task_free(tq, task_alloc(tq, KM_SLEEP)); | |
296 | mutex_exit(&tq->tq_lock); | |
297 | } | |
298 | ||
299 | for (t = 0; t < nthreads; t++) | |
1e33ac1e BB |
300 | VERIFY((tq->tq_threadlist[t] = thread_create(NULL, 0, |
301 | taskq_thread, tq, TS_RUN, NULL, 0, 0)) != NULL); | |
34dc7c2f BB |
302 | |
303 | return (tq); | |
304 | } | |
305 | ||
306 | void | |
307 | taskq_destroy(taskq_t *tq) | |
308 | { | |
34dc7c2f BB |
309 | int nthreads = tq->tq_nthreads; |
310 | ||
311 | taskq_wait(tq); | |
312 | ||
313 | mutex_enter(&tq->tq_lock); | |
314 | ||
315 | tq->tq_flags &= ~TASKQ_ACTIVE; | |
316 | cv_broadcast(&tq->tq_dispatch_cv); | |
317 | ||
318 | while (tq->tq_nthreads != 0) | |
319 | cv_wait(&tq->tq_wait_cv, &tq->tq_lock); | |
320 | ||
321 | tq->tq_minalloc = 0; | |
322 | while (tq->tq_nalloc != 0) { | |
323 | ASSERT(tq->tq_freelist != NULL); | |
324 | task_free(tq, task_alloc(tq, KM_SLEEP)); | |
325 | } | |
326 | ||
327 | mutex_exit(&tq->tq_lock); | |
328 | ||
1e33ac1e | 329 | kmem_free(tq->tq_threadlist, nthreads * sizeof (kthread_t *)); |
34dc7c2f BB |
330 | |
331 | rw_destroy(&tq->tq_threadlock); | |
332 | mutex_destroy(&tq->tq_lock); | |
333 | cv_destroy(&tq->tq_dispatch_cv); | |
334 | cv_destroy(&tq->tq_wait_cv); | |
428870ff | 335 | cv_destroy(&tq->tq_maxalloc_cv); |
34dc7c2f BB |
336 | |
337 | kmem_free(tq, sizeof (taskq_t)); | |
338 | } | |
339 | ||
340 | int | |
1e33ac1e | 341 | taskq_member(taskq_t *tq, kthread_t *t) |
34dc7c2f BB |
342 | { |
343 | int i; | |
344 | ||
345 | if (taskq_now) | |
346 | return (1); | |
347 | ||
348 | for (i = 0; i < tq->tq_nthreads; i++) | |
1e33ac1e | 349 | if (tq->tq_threadlist[i] == t) |
34dc7c2f BB |
350 | return (1); |
351 | ||
352 | return (0); | |
353 | } | |
b128c09f | 354 | |
cc92e9d0 GW |
355 | int |
356 | taskq_cancel_id(taskq_t *tq, taskqid_t id) | |
357 | { | |
358 | return (ENOENT); | |
359 | } | |
360 | ||
b128c09f BB |
361 | void |
362 | system_taskq_init(void) | |
363 | { | |
364 | system_taskq = taskq_create("system_taskq", 64, minclsyspri, 4, 512, | |
365 | TASKQ_DYNAMIC | TASKQ_PREPOPULATE); | |
366 | } | |
428870ff BB |
367 | |
368 | void | |
369 | system_taskq_fini(void) | |
370 | { | |
371 | taskq_destroy(system_taskq); | |
372 | system_taskq = NULL; /* defensive */ | |
373 | } |