threadpool_tester.c revision 1.3.2.2 1 1.3.2.2 christos /* $NetBSD: threadpool_tester.c,v 1.3.2.2 2019/06/10 22:10:02 christos Exp $ */
2 1.3.2.2 christos
3 1.3.2.2 christos /*-
4 1.3.2.2 christos * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 1.3.2.2 christos * All rights reserved.
6 1.3.2.2 christos *
7 1.3.2.2 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 christos * by Jason R. Thorpe.
9 1.3.2.2 christos *
10 1.3.2.2 christos * Redistribution and use in source and binary forms, with or without
11 1.3.2.2 christos * modification, are permitted provided that the following conditions
12 1.3.2.2 christos * are met:
13 1.3.2.2 christos * 1. Redistributions of source code must retain the above copyright
14 1.3.2.2 christos * notice, this list of conditions and the following disclaimer.
15 1.3.2.2 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.2.2 christos * notice, this list of conditions and the following disclaimer in the
17 1.3.2.2 christos * documentation and/or other materials provided with the distribution.
18 1.3.2.2 christos *
19 1.3.2.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.3.2.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.3.2.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.3.2.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.3.2.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.3.2.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.3.2.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.3.2.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.3.2.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.3.2.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.3.2.2 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.3.2.2 christos */
31 1.3.2.2 christos
32 1.3.2.2 christos #include <sys/cdefs.h>
33 1.3.2.2 christos __KERNEL_RCSID(0, "$NetBSD: threadpool_tester.c,v 1.3.2.2 2019/06/10 22:10:02 christos Exp $");
34 1.3.2.2 christos
35 1.3.2.2 christos #include <sys/param.h>
36 1.3.2.2 christos #include <sys/kernel.h>
37 1.3.2.2 christos #include <sys/module.h>
38 1.3.2.2 christos #include <sys/sysctl.h>
39 1.3.2.2 christos #include <sys/threadpool.h>
40 1.3.2.2 christos
41 1.3.2.2 christos MODULE(MODULE_CLASS_MISC, threadpool_tester, NULL);
42 1.3.2.2 christos
43 1.3.2.2 christos #ifdef THREADPOOL_VERBOSE
44 1.3.2.2 christos #define TP_LOG(x) printf x
45 1.3.2.2 christos #else
46 1.3.2.2 christos #define TP_LOG(x) /* nothing */
47 1.3.2.2 christos #endif /* THREADPOOL_VERBOSE */
48 1.3.2.2 christos
49 1.3.2.2 christos static struct tester_context {
50 1.3.2.2 christos kmutex_t ctx_mutex;
51 1.3.2.2 christos struct sysctllog *ctx_sysctllog;
52 1.3.2.2 christos struct threadpool *ctx_unbound[PRI_COUNT + 1];
53 1.3.2.2 christos struct threadpool_percpu *ctx_percpu[PRI_COUNT + 1];
54 1.3.2.2 christos unsigned int ctx_value;
55 1.3.2.2 christos struct threadpool_job ctx_job;
56 1.3.2.2 christos } tester_ctx;
57 1.3.2.2 christos
58 1.3.2.2 christos #define pri_to_idx(pri) ((pri) == PRI_NONE ? PRI_COUNT : (pri))
59 1.3.2.2 christos
60 1.3.2.2 christos static bool
61 1.3.2.2 christos pri_is_valid(pri_t pri)
62 1.3.2.2 christos {
63 1.3.2.2 christos return (pri == PRI_NONE || (pri >= PRI_USER && pri < PRI_COUNT));
64 1.3.2.2 christos }
65 1.3.2.2 christos
66 1.3.2.2 christos static int
67 1.3.2.2 christos threadpool_tester_get_unbound(SYSCTLFN_ARGS)
68 1.3.2.2 christos {
69 1.3.2.2 christos struct tester_context *ctx;
70 1.3.2.2 christos struct threadpool *pool, *opool = NULL;
71 1.3.2.2 christos struct sysctlnode node;
72 1.3.2.2 christos int error, val;
73 1.3.2.2 christos
74 1.3.2.2 christos node = *rnode;
75 1.3.2.2 christos ctx = node.sysctl_data;
76 1.3.2.2 christos
77 1.3.2.2 christos val = -1;
78 1.3.2.2 christos node.sysctl_data = &val;
79 1.3.2.2 christos error = sysctl_lookup(SYSCTLFN_CALL(&node));
80 1.3.2.2 christos if (error || newp == NULL)
81 1.3.2.2 christos return error;
82 1.3.2.2 christos
83 1.3.2.2 christos if (! pri_is_valid(val))
84 1.3.2.2 christos return EINVAL;
85 1.3.2.2 christos
86 1.3.2.2 christos error = threadpool_get(&pool, val);
87 1.3.2.2 christos if (error) {
88 1.3.2.2 christos TP_LOG(("%s: threadpool_get(..., %d) failed -> %d\n",
89 1.3.2.2 christos __func__, val, error));
90 1.3.2.2 christos return error;
91 1.3.2.2 christos }
92 1.3.2.2 christos
93 1.3.2.2 christos mutex_enter(&ctx->ctx_mutex);
94 1.3.2.2 christos if (ctx->ctx_unbound[pri_to_idx(val)] == NULL)
95 1.3.2.2 christos ctx->ctx_unbound[pri_to_idx(val)] = pool;
96 1.3.2.2 christos else
97 1.3.2.2 christos opool = ctx->ctx_unbound[pri_to_idx(val)];
98 1.3.2.2 christos mutex_exit(&ctx->ctx_mutex);
99 1.3.2.2 christos
100 1.3.2.2 christos if (opool != NULL) {
101 1.3.2.2 christos /* Should have gotten reference to existing pool. */
102 1.3.2.2 christos TP_LOG(("%s: found existing unbound pool for pri %d (%s)\n",
103 1.3.2.2 christos __func__, val, opool == pool ? "match" : "NO MATCH"));
104 1.3.2.2 christos KASSERT(opool == pool);
105 1.3.2.2 christos threadpool_put(pool, val);
106 1.3.2.2 christos error = EEXIST;
107 1.3.2.2 christos } else {
108 1.3.2.2 christos TP_LOG(("%s: created unbound pool for pri %d\n",
109 1.3.2.2 christos __func__, val));
110 1.3.2.2 christos }
111 1.3.2.2 christos
112 1.3.2.2 christos return error;
113 1.3.2.2 christos }
114 1.3.2.2 christos
115 1.3.2.2 christos static int
116 1.3.2.2 christos threadpool_tester_put_unbound(SYSCTLFN_ARGS)
117 1.3.2.2 christos {
118 1.3.2.2 christos struct tester_context *ctx;
119 1.3.2.2 christos struct threadpool *pool;
120 1.3.2.2 christos struct sysctlnode node;
121 1.3.2.2 christos int error, val;
122 1.3.2.2 christos
123 1.3.2.2 christos node = *rnode;
124 1.3.2.2 christos ctx = node.sysctl_data;
125 1.3.2.2 christos
126 1.3.2.2 christos val = -1;
127 1.3.2.2 christos node.sysctl_data = &val;
128 1.3.2.2 christos error = sysctl_lookup(SYSCTLFN_CALL(&node));
129 1.3.2.2 christos if (error || newp == NULL)
130 1.3.2.2 christos return error;
131 1.3.2.2 christos
132 1.3.2.2 christos if (! pri_is_valid(val))
133 1.3.2.2 christos return EINVAL;
134 1.3.2.2 christos
135 1.3.2.2 christos mutex_enter(&ctx->ctx_mutex);
136 1.3.2.2 christos /* We only ever maintain a single reference. */
137 1.3.2.2 christos pool = ctx->ctx_unbound[pri_to_idx(val)];
138 1.3.2.2 christos ctx->ctx_unbound[pri_to_idx(val)] = NULL;
139 1.3.2.2 christos mutex_exit(&ctx->ctx_mutex);
140 1.3.2.2 christos
141 1.3.2.2 christos if (pool == NULL) {
142 1.3.2.2 christos TP_LOG(("%s: no unbound pool for pri %d\n",
143 1.3.2.2 christos __func__, val));
144 1.3.2.2 christos return ENODEV;
145 1.3.2.2 christos }
146 1.3.2.2 christos
147 1.3.2.2 christos threadpool_put(pool, val);
148 1.3.2.2 christos TP_LOG(("%s: released unbound pool for pri %d\n",
149 1.3.2.2 christos __func__, val));
150 1.3.2.2 christos
151 1.3.2.2 christos return 0;
152 1.3.2.2 christos }
153 1.3.2.2 christos
154 1.3.2.2 christos static int
155 1.3.2.2 christos threadpool_tester_run_unbound(SYSCTLFN_ARGS)
156 1.3.2.2 christos {
157 1.3.2.2 christos struct tester_context *ctx;
158 1.3.2.2 christos struct threadpool *pool;
159 1.3.2.2 christos struct sysctlnode node;
160 1.3.2.2 christos int error, val;
161 1.3.2.2 christos
162 1.3.2.2 christos node = *rnode;
163 1.3.2.2 christos ctx = node.sysctl_data;
164 1.3.2.2 christos
165 1.3.2.2 christos val = -1;
166 1.3.2.2 christos node.sysctl_data = &val;
167 1.3.2.2 christos error = sysctl_lookup(SYSCTLFN_CALL(&node));
168 1.3.2.2 christos if (error || newp == NULL)
169 1.3.2.2 christos return error;
170 1.3.2.2 christos
171 1.3.2.2 christos if (! pri_is_valid(val))
172 1.3.2.2 christos return EINVAL;
173 1.3.2.2 christos
174 1.3.2.2 christos mutex_enter(&ctx->ctx_mutex);
175 1.3.2.2 christos pool = ctx->ctx_unbound[pri_to_idx(val)];
176 1.3.2.2 christos if (pool == NULL) {
177 1.3.2.2 christos TP_LOG(("%s: no unbound pool for pri %d\n",
178 1.3.2.2 christos __func__, val));
179 1.3.2.2 christos mutex_exit(&ctx->ctx_mutex);
180 1.3.2.2 christos return ENODEV;
181 1.3.2.2 christos }
182 1.3.2.2 christos
183 1.3.2.2 christos threadpool_schedule_job(pool, &ctx->ctx_job);
184 1.3.2.2 christos TP_LOG(("%s: scheduled job on unbound pool for pri %d\n",
185 1.3.2.2 christos __func__, val));
186 1.3.2.2 christos mutex_exit(&ctx->ctx_mutex);
187 1.3.2.2 christos
188 1.3.2.2 christos return 0;
189 1.3.2.2 christos }
190 1.3.2.2 christos
191 1.3.2.2 christos static int
192 1.3.2.2 christos threadpool_tester_get_percpu(SYSCTLFN_ARGS)
193 1.3.2.2 christos {
194 1.3.2.2 christos struct tester_context *ctx;
195 1.3.2.2 christos struct threadpool_percpu *pcpu, *opcpu = NULL;
196 1.3.2.2 christos struct sysctlnode node;
197 1.3.2.2 christos int error, val;
198 1.3.2.2 christos
199 1.3.2.2 christos node = *rnode;
200 1.3.2.2 christos ctx = node.sysctl_data;
201 1.3.2.2 christos
202 1.3.2.2 christos val = -1;
203 1.3.2.2 christos node.sysctl_data = &val;
204 1.3.2.2 christos error = sysctl_lookup(SYSCTLFN_CALL(&node));
205 1.3.2.2 christos if (error || newp == NULL)
206 1.3.2.2 christos return error;
207 1.3.2.2 christos
208 1.3.2.2 christos if (! pri_is_valid(val))
209 1.3.2.2 christos return EINVAL;
210 1.3.2.2 christos
211 1.3.2.2 christos error = threadpool_percpu_get(&pcpu, val);
212 1.3.2.2 christos if (error) {
213 1.3.2.2 christos TP_LOG(("%s: threadpool_percpu_get(..., %d) failed -> %d\n",
214 1.3.2.2 christos __func__, val, error));
215 1.3.2.2 christos return error;
216 1.3.2.2 christos }
217 1.3.2.2 christos
218 1.3.2.2 christos mutex_enter(&ctx->ctx_mutex);
219 1.3.2.2 christos if (ctx->ctx_percpu[pri_to_idx(val)] == NULL)
220 1.3.2.2 christos ctx->ctx_percpu[pri_to_idx(val)] = pcpu;
221 1.3.2.2 christos else
222 1.3.2.2 christos opcpu = ctx->ctx_percpu[pri_to_idx(val)];
223 1.3.2.2 christos mutex_exit(&ctx->ctx_mutex);
224 1.3.2.2 christos
225 1.3.2.2 christos if (opcpu != NULL) {
226 1.3.2.2 christos /* Should have gotten reference to existing pool. */
227 1.3.2.2 christos TP_LOG(("%s: found existing unbound pool for pri %d (%s)\n",
228 1.3.2.2 christos __func__, val, opcpu == pcpu ? "match" : "NO MATCH"));
229 1.3.2.2 christos KASSERT(opcpu == pcpu);
230 1.3.2.2 christos threadpool_percpu_put(pcpu, val);
231 1.3.2.2 christos error = EEXIST;
232 1.3.2.2 christos } else {
233 1.3.2.2 christos TP_LOG(("%s: created percpu pool for pri %d\n",
234 1.3.2.2 christos __func__, val));
235 1.3.2.2 christos }
236 1.3.2.2 christos
237 1.3.2.2 christos return error;
238 1.3.2.2 christos }
239 1.3.2.2 christos
240 1.3.2.2 christos static int
241 1.3.2.2 christos threadpool_tester_put_percpu(SYSCTLFN_ARGS)
242 1.3.2.2 christos {
243 1.3.2.2 christos struct tester_context *ctx;
244 1.3.2.2 christos struct threadpool_percpu *pcpu;
245 1.3.2.2 christos struct sysctlnode node;
246 1.3.2.2 christos int error, val;
247 1.3.2.2 christos
248 1.3.2.2 christos node = *rnode;
249 1.3.2.2 christos ctx = node.sysctl_data;
250 1.3.2.2 christos
251 1.3.2.2 christos val = -1;
252 1.3.2.2 christos node.sysctl_data = &val;
253 1.3.2.2 christos error = sysctl_lookup(SYSCTLFN_CALL(&node));
254 1.3.2.2 christos if (error || newp == NULL)
255 1.3.2.2 christos return error;
256 1.3.2.2 christos
257 1.3.2.2 christos if (! pri_is_valid(val))
258 1.3.2.2 christos return EINVAL;
259 1.3.2.2 christos
260 1.3.2.2 christos mutex_enter(&ctx->ctx_mutex);
261 1.3.2.2 christos /* We only ever maintain a single reference. */
262 1.3.2.2 christos pcpu = ctx->ctx_percpu[pri_to_idx(val)];
263 1.3.2.2 christos ctx->ctx_percpu[pri_to_idx(val)] = NULL;
264 1.3.2.2 christos mutex_exit(&ctx->ctx_mutex);
265 1.3.2.2 christos
266 1.3.2.2 christos if (pcpu == NULL) {
267 1.3.2.2 christos TP_LOG(("%s: no percpu pool for pri %d\n",
268 1.3.2.2 christos __func__, val));
269 1.3.2.2 christos return ENODEV;
270 1.3.2.2 christos }
271 1.3.2.2 christos
272 1.3.2.2 christos threadpool_percpu_put(pcpu, val);
273 1.3.2.2 christos TP_LOG(("%s: released percpu pool for pri %d\n",
274 1.3.2.2 christos __func__, val));
275 1.3.2.2 christos
276 1.3.2.2 christos return 0;
277 1.3.2.2 christos }
278 1.3.2.2 christos
279 1.3.2.2 christos static int
280 1.3.2.2 christos threadpool_tester_run_percpu(SYSCTLFN_ARGS)
281 1.3.2.2 christos {
282 1.3.2.2 christos struct tester_context *ctx;
283 1.3.2.2 christos struct threadpool_percpu *pcpu;
284 1.3.2.2 christos struct threadpool *pool;
285 1.3.2.2 christos struct sysctlnode node;
286 1.3.2.2 christos int error, val;
287 1.3.2.2 christos
288 1.3.2.2 christos node = *rnode;
289 1.3.2.2 christos ctx = node.sysctl_data;
290 1.3.2.2 christos
291 1.3.2.2 christos val = -1;
292 1.3.2.2 christos node.sysctl_data = &val;
293 1.3.2.2 christos error = sysctl_lookup(SYSCTLFN_CALL(&node));
294 1.3.2.2 christos if (error || newp == NULL)
295 1.3.2.2 christos return error;
296 1.3.2.2 christos
297 1.3.2.2 christos if (! pri_is_valid(val))
298 1.3.2.2 christos return EINVAL;
299 1.3.2.2 christos
300 1.3.2.2 christos mutex_enter(&ctx->ctx_mutex);
301 1.3.2.2 christos pcpu = ctx->ctx_percpu[pri_to_idx(val)];
302 1.3.2.2 christos if (pcpu == NULL) {
303 1.3.2.2 christos TP_LOG(("%s: no percpu pool for pri %d\n",
304 1.3.2.2 christos __func__, val));
305 1.3.2.2 christos mutex_exit(&ctx->ctx_mutex);
306 1.3.2.2 christos return ENODEV;
307 1.3.2.2 christos }
308 1.3.2.2 christos
309 1.3.2.2 christos pool = threadpool_percpu_ref(pcpu);
310 1.3.2.2 christos KASSERT(pool != NULL);
311 1.3.2.2 christos
312 1.3.2.2 christos threadpool_schedule_job(pool, &ctx->ctx_job);
313 1.3.2.2 christos TP_LOG(("%s: scheduled job on percpu pool for pri %d\n",
314 1.3.2.2 christos __func__, val));
315 1.3.2.2 christos mutex_exit(&ctx->ctx_mutex);
316 1.3.2.2 christos
317 1.3.2.2 christos return 0;
318 1.3.2.2 christos }
319 1.3.2.2 christos
320 1.3.2.2 christos static int
321 1.3.2.2 christos threadpool_tester_test_value(SYSCTLFN_ARGS)
322 1.3.2.2 christos {
323 1.3.2.2 christos struct tester_context *ctx;
324 1.3.2.2 christos struct sysctlnode node;
325 1.3.2.2 christos unsigned int val;
326 1.3.2.2 christos int error;
327 1.3.2.2 christos
328 1.3.2.2 christos node = *rnode;
329 1.3.2.2 christos ctx = node.sysctl_data;
330 1.3.2.2 christos
331 1.3.2.2 christos mutex_enter(&ctx->ctx_mutex);
332 1.3.2.2 christos val = ctx->ctx_value;
333 1.3.2.2 christos node.sysctl_data = &val;
334 1.3.2.2 christos error = sysctl_lookup(SYSCTLFN_CALL(&node));
335 1.3.2.2 christos if (error || newp == NULL) {
336 1.3.2.2 christos mutex_exit(&ctx->ctx_mutex);
337 1.3.2.2 christos return error;
338 1.3.2.2 christos }
339 1.3.2.2 christos ctx->ctx_value = val;
340 1.3.2.2 christos mutex_exit(&ctx->ctx_mutex);
341 1.3.2.2 christos
342 1.3.2.2 christos return 0;
343 1.3.2.2 christos }
344 1.3.2.2 christos
345 1.3.2.2 christos static void
346 1.3.2.2 christos threadpool_tester_job(struct threadpool_job *job)
347 1.3.2.2 christos {
348 1.3.2.2 christos struct tester_context *ctx =
349 1.3.2.2 christos container_of(job, struct tester_context, ctx_job);
350 1.3.2.2 christos unsigned int oval, nval;
351 1.3.2.2 christos
352 1.3.2.2 christos TP_LOG(("%s: job = %p, ctx = %p\n", __func__, job, ctx));
353 1.3.2.2 christos
354 1.3.2.2 christos mutex_enter(&ctx->ctx_mutex);
355 1.3.2.2 christos oval = ctx->ctx_value;
356 1.3.2.2 christos nval = oval + 1; /* always reference oval and nval */
357 1.3.2.2 christos ctx->ctx_value = nval;
358 1.3.2.2 christos mutex_exit(&ctx->ctx_mutex);
359 1.3.2.2 christos
360 1.3.2.2 christos TP_LOG(("%s: %u -> %u\n", __func__, oval, nval));
361 1.3.2.2 christos (void) kpause("tptestjob", false, hz, NULL);
362 1.3.2.2 christos
363 1.3.2.2 christos mutex_enter(&ctx->ctx_mutex);
364 1.3.2.2 christos threadpool_job_done(job);
365 1.3.2.2 christos mutex_exit(&ctx->ctx_mutex);
366 1.3.2.2 christos }
367 1.3.2.2 christos
368 1.3.2.2 christos #define RETURN_ERROR if (error) goto return_error
369 1.3.2.2 christos
370 1.3.2.2 christos static int
371 1.3.2.2 christos threadpool_tester_init(void)
372 1.3.2.2 christos {
373 1.3.2.2 christos struct sysctllog **log = &tester_ctx.ctx_sysctllog;
374 1.3.2.2 christos const struct sysctlnode *rnode, *cnode;
375 1.3.2.2 christos int error;
376 1.3.2.2 christos
377 1.3.2.2 christos mutex_init(&tester_ctx.ctx_mutex, MUTEX_DEFAULT, IPL_NONE);
378 1.3.2.2 christos threadpool_job_init(&tester_ctx.ctx_job, threadpool_tester_job,
379 1.3.2.2 christos &tester_ctx.ctx_mutex, "tptest");
380 1.3.2.2 christos
381 1.3.2.2 christos error = sysctl_createv(log, 0, NULL, &rnode, CTLFLAG_PERMANENT,
382 1.3.2.2 christos CTLTYPE_NODE, "threadpool_tester",
383 1.3.2.2 christos SYSCTL_DESCR("threadpool testing interface"),
384 1.3.2.2 christos NULL, 0, NULL, 0, CTL_KERN, CTL_CREATE, CTL_EOL);
385 1.3.2.2 christos RETURN_ERROR;
386 1.3.2.2 christos
387 1.3.2.2 christos error = sysctl_createv(log, 0, &rnode, &cnode,
388 1.3.2.2 christos CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "get_unbound",
389 1.3.2.2 christos SYSCTL_DESCR("get unbound pool of specified priority"),
390 1.3.2.2 christos threadpool_tester_get_unbound, 0,
391 1.3.2.2 christos (void *)&tester_ctx, 0, CTL_CREATE, CTL_EOL);
392 1.3.2.2 christos RETURN_ERROR;
393 1.3.2.2 christos
394 1.3.2.2 christos error = sysctl_createv(log, 0, &rnode, &cnode,
395 1.3.2.2 christos CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "put_unbound",
396 1.3.2.2 christos SYSCTL_DESCR("put unbound pool of specified priority"),
397 1.3.2.2 christos threadpool_tester_put_unbound, 0,
398 1.3.2.2 christos (void *)&tester_ctx, 0, CTL_CREATE, CTL_EOL);
399 1.3.2.2 christos RETURN_ERROR;
400 1.3.2.2 christos
401 1.3.2.2 christos error = sysctl_createv(log, 0, &rnode, &cnode,
402 1.3.2.2 christos CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "run_unbound",
403 1.3.2.2 christos SYSCTL_DESCR("run on unbound pool of specified priority"),
404 1.3.2.2 christos threadpool_tester_run_unbound, 0,
405 1.3.2.2 christos (void *)&tester_ctx, 0, CTL_CREATE, CTL_EOL);
406 1.3.2.2 christos RETURN_ERROR;
407 1.3.2.2 christos
408 1.3.2.2 christos error = sysctl_createv(log, 0, &rnode, &cnode,
409 1.3.2.2 christos CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "get_percpu",
410 1.3.2.2 christos SYSCTL_DESCR("get percpu pool of specified priority"),
411 1.3.2.2 christos threadpool_tester_get_percpu, 0,
412 1.3.2.2 christos (void *)&tester_ctx, 0, CTL_CREATE, CTL_EOL);
413 1.3.2.2 christos RETURN_ERROR;
414 1.3.2.2 christos
415 1.3.2.2 christos error = sysctl_createv(log, 0, &rnode, &cnode,
416 1.3.2.2 christos CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "put_percpu",
417 1.3.2.2 christos SYSCTL_DESCR("put percpu pool of specified priority"),
418 1.3.2.2 christos threadpool_tester_put_percpu, 0,
419 1.3.2.2 christos (void *)&tester_ctx, 0, CTL_CREATE, CTL_EOL);
420 1.3.2.2 christos RETURN_ERROR;
421 1.3.2.2 christos
422 1.3.2.2 christos error = sysctl_createv(log, 0, &rnode, &cnode,
423 1.3.2.2 christos CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "run_percpu",
424 1.3.2.2 christos SYSCTL_DESCR("run on percpu pool of specified priority"),
425 1.3.2.2 christos threadpool_tester_run_percpu, 0,
426 1.3.2.2 christos (void *)&tester_ctx, 0, CTL_CREATE, CTL_EOL);
427 1.3.2.2 christos RETURN_ERROR;
428 1.3.2.2 christos
429 1.3.2.2 christos error = sysctl_createv(log, 0, &rnode, &cnode,
430 1.3.2.2 christos CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "test_value",
431 1.3.2.2 christos SYSCTL_DESCR("test value that jobs increment"),
432 1.3.2.2 christos threadpool_tester_test_value, 0,
433 1.3.2.2 christos (void *)&tester_ctx, 0, CTL_CREATE, CTL_EOL);
434 1.3.2.2 christos RETURN_ERROR;
435 1.3.2.2 christos
436 1.3.2.2 christos return 0;
437 1.3.2.2 christos
438 1.3.2.2 christos return_error:
439 1.3.2.2 christos sysctl_teardown(log);
440 1.3.2.2 christos return error;
441 1.3.2.2 christos }
442 1.3.2.2 christos
443 1.3.2.2 christos static int
444 1.3.2.2 christos threadpool_tester_fini(void)
445 1.3.2.2 christos {
446 1.3.2.2 christos pri_t pri;
447 1.3.2.2 christos
448 1.3.2.2 christos mutex_enter(&tester_ctx.ctx_mutex);
449 1.3.2.2 christos for (pri = PRI_NONE/*-1*/; pri < PRI_COUNT; pri++) {
450 1.3.2.2 christos struct threadpool *pool =
451 1.3.2.2 christos tester_ctx.ctx_unbound[pri_to_idx(pri)];
452 1.3.2.2 christos struct threadpool_percpu *pcpu =
453 1.3.2.2 christos tester_ctx.ctx_percpu[pri_to_idx(pri)];
454 1.3.2.2 christos
455 1.3.2.2 christos /*
456 1.3.2.2 christos * threadpool_cancel_job() may be called on a pool
457 1.3.2.2 christos * other than what the job is scheduled on. This is
458 1.3.2.2 christos * safe; see comment in threadpool_cancel_job_async().
459 1.3.2.2 christos */
460 1.3.2.2 christos
461 1.3.2.2 christos if (pool != NULL) {
462 1.3.2.2 christos threadpool_cancel_job(pool, &tester_ctx.ctx_job);
463 1.3.2.2 christos threadpool_put(pool, pri);
464 1.3.2.2 christos tester_ctx.ctx_unbound[pri_to_idx(pri)] = NULL;
465 1.3.2.2 christos }
466 1.3.2.2 christos if (pcpu != NULL) {
467 1.3.2.2 christos pool = threadpool_percpu_ref(pcpu);
468 1.3.2.2 christos threadpool_cancel_job(pool, &tester_ctx.ctx_job);
469 1.3.2.2 christos threadpool_percpu_put(pcpu, pri);
470 1.3.2.2 christos tester_ctx.ctx_percpu[pri_to_idx(pri)] = NULL;
471 1.3.2.2 christos }
472 1.3.2.2 christos }
473 1.3.2.2 christos mutex_exit(&tester_ctx.ctx_mutex);
474 1.3.2.2 christos threadpool_job_destroy(&tester_ctx.ctx_job);
475 1.3.2.2 christos mutex_destroy(&tester_ctx.ctx_mutex);
476 1.3.2.2 christos
477 1.3.2.2 christos sysctl_teardown(&tester_ctx.ctx_sysctllog);
478 1.3.2.2 christos
479 1.3.2.2 christos return 0;
480 1.3.2.2 christos }
481 1.3.2.2 christos
482 1.3.2.2 christos static int
483 1.3.2.2 christos threadpool_tester_modcmd(modcmd_t cmd, void *arg __unused)
484 1.3.2.2 christos {
485 1.3.2.2 christos int error;
486 1.3.2.2 christos
487 1.3.2.2 christos switch (cmd) {
488 1.3.2.2 christos case MODULE_CMD_INIT:
489 1.3.2.2 christos error = threadpool_tester_init();
490 1.3.2.2 christos break;
491 1.3.2.2 christos
492 1.3.2.2 christos case MODULE_CMD_FINI:
493 1.3.2.2 christos error = threadpool_tester_fini();
494 1.3.2.2 christos break;
495 1.3.2.2 christos
496 1.3.2.2 christos case MODULE_CMD_STAT:
497 1.3.2.2 christos default:
498 1.3.2.2 christos error = ENOTTY;
499 1.3.2.2 christos }
500 1.3.2.2 christos
501 1.3.2.2 christos return error;
502 1.3.2.2 christos }
503