summaryrefslogtreecommitdiff
path: root/tests/modules/threadpool_tester/threadpool_tester.c
blob: 2d79868c3bc573ecd4ca042e0cf1a3db1b24f292 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*	$NetBSD: threadpool_tester.c,v 1.1 2019/01/25 18:33:59 christos Exp $	*/

/*-
 * Copyright (c) 2018 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jason R. Thorpe.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: threadpool_tester.c,v 1.1 2019/01/25 18:33:59 christos Exp $");

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/sysctl.h>
#include <sys/threadpool.h>

MODULE(MODULE_CLASS_MISC, threadpool_tester, NULL);

#ifdef THREADPOOL_VERBOSE
#define	TP_LOG(x)		printf x
#else
#define	TP_LOG(x)		/* nothing */
#endif /* THREADPOOL_VERBOSE */

static struct tester_context {
	kmutex_t ctx_mutex;
	struct sysctllog *ctx_sysctllog;
	struct threadpool *ctx_unbound[PRI_COUNT + 1];
	struct threadpool_percpu *ctx_percpu[PRI_COUNT + 1];
	unsigned int ctx_value;
	struct threadpool_job ctx_job;
} tester_ctx;

#define	pri_to_idx(pri)		((pri) == PRI_NONE ? PRI_COUNT : (pri))

static bool
pri_is_valid(pri_t pri)
{
	return (pri == PRI_NONE || (pri >= PRI_USER && pri < PRI_COUNT));
}

static int
threadpool_tester_get_unbound(SYSCTLFN_ARGS)
{
	struct tester_context *ctx;
	struct threadpool *pool, *opool = NULL;
	struct sysctlnode node;
	int error, val;

	node = *rnode;
	ctx = node.sysctl_data;

	val = -1;
	node.sysctl_data = &val;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL)
		return error;

	if (! pri_is_valid(val))
		return EINVAL;

	error = threadpool_get(&pool, val);
	if (error) {
		TP_LOG(("%s: threadpool_get(..., %d) failed -> %d\n",
		    __func__, val, error));
		return error;
	}

	mutex_enter(&ctx->ctx_mutex);
	if (ctx->ctx_unbound[pri_to_idx(val)] == NULL)
		ctx->ctx_unbound[pri_to_idx(val)] = pool;
	else
		opool = ctx->ctx_unbound[pri_to_idx(val)];
	mutex_exit(&ctx->ctx_mutex);

	if (opool != NULL) {
		/* Should have gotten reference to existing pool. */
		TP_LOG(("%s: found existing unbound pool for pri %d (%s)\n",
		    __func__, val, opool == pool ? "match" : "NO MATCH"));
		KASSERT(opool == pool);
		threadpool_put(pool, val);
		error = EEXIST;
	} else {
		TP_LOG(("%s: created unbound pool for pri %d\n",
		    __func__, val));
	}

	return error;
}

static int
threadpool_tester_put_unbound(SYSCTLFN_ARGS)
{
	struct tester_context *ctx;
	struct threadpool *pool;
	struct sysctlnode node;
	int error, val;

	node = *rnode;
	ctx = node.sysctl_data;

	val = -1;
	node.sysctl_data = &val;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL)
		return error;

	if (! pri_is_valid(val))
		return EINVAL;

	mutex_enter(&ctx->ctx_mutex);
	/* We only ever maintain a single reference. */
	pool = ctx->ctx_unbound[pri_to_idx(val)];
	ctx->ctx_unbound[pri_to_idx(val)] = NULL;
	mutex_exit(&ctx->ctx_mutex);

	if (pool == NULL) {
		TP_LOG(("%s: no unbound pool for pri %d\n",
		    __func__, val));
		return ENODEV;
	}

	threadpool_put(pool, val);
	TP_LOG(("%s: released unbound pool for pri %d\n",
	    __func__, val));

	return 0;
}

static int
threadpool_tester_run_unbound(SYSCTLFN_ARGS)
{
	struct tester_context *ctx;
	struct threadpool *pool;
	struct sysctlnode node;
	int error, val;

	node = *rnode;
	ctx = node.sysctl_data;

	val = -1;
	node.sysctl_data = &val;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL)
		return error;

	if (! pri_is_valid(val))
		return EINVAL;

	mutex_enter(&ctx->ctx_mutex);
	pool = ctx->ctx_unbound[pri_to_idx(val)];
	if (pool == NULL) {
		TP_LOG(("%s: no unbound pool for pri %d\n",
		    __func__, val));
		mutex_exit(&ctx->ctx_mutex);
		return ENODEV;
	}

	threadpool_schedule_job(pool, &ctx->ctx_job);
	TP_LOG(("%s: scheduled job on unbound pool for pri %d\n",
	    __func__, val));
	mutex_exit(&ctx->ctx_mutex);

	return 0;
}

static int
threadpool_tester_get_percpu(SYSCTLFN_ARGS)
{
	struct tester_context *ctx;
	struct threadpool_percpu *pcpu, *opcpu = NULL;
	struct sysctlnode node;
	int error, val;

	node = *rnode;
	ctx = node.sysctl_data;

	val = -1;
	node.sysctl_data = &val;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL)
		return error;

	if (! pri_is_valid(val))
		return EINVAL;

	error = threadpool_percpu_get(&pcpu, val);
	if (error) {
		TP_LOG(("%s: threadpool_percpu_get(..., %d) failed -> %d\n",
		    __func__, val, error));
		return error;
	}

	mutex_enter(&ctx->ctx_mutex);
	if (ctx->ctx_percpu[pri_to_idx(val)] == NULL)
		ctx->ctx_percpu[pri_to_idx(val)] = pcpu;
	else
		opcpu = ctx->ctx_percpu[pri_to_idx(val)];
	mutex_exit(&ctx->ctx_mutex);

	if (opcpu != NULL) {
		/* Should have gotten reference to existing pool. */
		TP_LOG(("%s: found existing unbound pool for pri %d (%s)\n",
		    __func__, val, opcpu == pcpu ? "match" : "NO MATCH"));
		KASSERT(opcpu == pcpu);
		threadpool_percpu_put(pcpu, val);
		error = EEXIST;
	} else {
		TP_LOG(("%s: created percpu pool for pri %d\n",
		    __func__, val));
	}

	return error;
}

static int
threadpool_tester_put_percpu(SYSCTLFN_ARGS)
{
	struct tester_context *ctx;
	struct threadpool_percpu *pcpu;
	struct sysctlnode node;
	int error, val;

	node = *rnode;
	ctx = node.sysctl_data;

	val = -1;
	node.sysctl_data = &val;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL)
		return error;

	if (! pri_is_valid(val))
		return EINVAL;

	mutex_enter(&ctx->ctx_mutex);
	/* We only ever maintain a single reference. */
	pcpu = ctx->ctx_percpu[pri_to_idx(val)];
	ctx->ctx_percpu[pri_to_idx(val)] = NULL;
	mutex_exit(&ctx->ctx_mutex);

	if (pcpu == NULL) {
		TP_LOG(("%s: no percpu pool for pri %d\n",
		    __func__, val));
		return ENODEV;
	}

	threadpool_percpu_put(pcpu, val);
	TP_LOG(("%s: released percpu pool for pri %d\n",
	    __func__, val));

	return 0;
}

static int
threadpool_tester_run_percpu(SYSCTLFN_ARGS)
{
	struct tester_context *ctx;
	struct threadpool_percpu *pcpu;
	struct threadpool *pool;
	struct sysctlnode node;
	int error, val;

	node = *rnode;
	ctx = node.sysctl_data;

	val = -1;
	node.sysctl_data = &val;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL)
		return error;

	if (! pri_is_valid(val))
		return EINVAL;

	mutex_enter(&ctx->ctx_mutex);
	pcpu = ctx->ctx_percpu[pri_to_idx(val)];
	if (pcpu == NULL) {
		TP_LOG(("%s: no percpu pool for pri %d\n",
		    __func__, val));
		mutex_exit(&ctx->ctx_mutex);
		return ENODEV;
	}

	pool = threadpool_percpu_ref(pcpu);
	KASSERT(pool != NULL);

	threadpool_schedule_job(pool, &ctx->ctx_job);
	TP_LOG(("%s: scheduled job on percpu pool for pri %d\n",
	    __func__, val));
	mutex_exit(&ctx->ctx_mutex);

	return 0;
}

static int
threadpool_tester_test_value(SYSCTLFN_ARGS)
{
	struct tester_context *ctx;
	struct sysctlnode node;
	unsigned int val;
	int error;

	node = *rnode;
	ctx = node.sysctl_data;

	mutex_enter(&ctx->ctx_mutex);
	val = ctx->ctx_value;
	node.sysctl_data = &val;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL) {
		mutex_exit(&ctx->ctx_mutex);
		return error;
	}
	ctx->ctx_value = val;
	mutex_exit(&ctx->ctx_mutex);

	return 0;
}

static void
threadpool_tester_job(struct threadpool_job *job)
{
	struct tester_context *ctx =
	    container_of(job, struct tester_context, ctx_job);
	unsigned int oval, nval;

	TP_LOG(("%s: job = %p, ctx = %p\n", __func__, job, ctx));

	mutex_enter(&ctx->ctx_mutex);
	oval = ctx->ctx_value;
	nval = oval + 1;	/* always reference oval and nval */
	ctx->ctx_value = nval;
	mutex_exit(&ctx->ctx_mutex);

	TP_LOG(("%s: %u -> %u\n", __func__, oval, nval));
	(void) kpause("tptestjob", false, hz, NULL);

	mutex_enter(&ctx->ctx_mutex);
	threadpool_job_done(job);
	mutex_exit(&ctx->ctx_mutex);
}

#define	RETURN_ERROR	if (error) goto return_error

static int
threadpool_tester_init(void)
{
	struct sysctllog **log = &tester_ctx.ctx_sysctllog;
	const struct sysctlnode *rnode, *cnode;
	int error;

	mutex_init(&tester_ctx.ctx_mutex, MUTEX_DEFAULT, IPL_NONE);
	threadpool_job_init(&tester_ctx.ctx_job, threadpool_tester_job,
	    &tester_ctx.ctx_mutex, "tptest");

	error = sysctl_createv(log, 0, NULL, &rnode, CTLFLAG_PERMANENT,
	    CTLTYPE_NODE, "threadpool_tester",
	    SYSCTL_DESCR("threadpool testing interface"),
	    NULL, 0, NULL, 0, CTL_KERN, CTL_CREATE, CTL_EOL);
	RETURN_ERROR;

	error = sysctl_createv(log, 0, &rnode, &cnode,
	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "get_unbound",
	    SYSCTL_DESCR("get unbound pool of specified priority"),
	    threadpool_tester_get_unbound, 0,
	    (void *)&tester_ctx, 0, CTL_CREATE, CTL_EOL);
	RETURN_ERROR;

	error = sysctl_createv(log, 0, &rnode, &cnode,
	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "put_unbound",
	    SYSCTL_DESCR("put unbound pool of specified priority"),
	    threadpool_tester_put_unbound, 0,
	    (void *)&tester_ctx, 0, CTL_CREATE, CTL_EOL);
	RETURN_ERROR;

	error = sysctl_createv(log, 0, &rnode, &cnode,
	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "run_unbound",
	    SYSCTL_DESCR("run on unbound pool of specified priority"),
	    threadpool_tester_run_unbound, 0,
	    (void *)&tester_ctx, 0, CTL_CREATE, CTL_EOL);
	RETURN_ERROR;

	error = sysctl_createv(log, 0, &rnode, &cnode,
	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "get_percpu",
	    SYSCTL_DESCR("get percpu pool of specified priority"),
	    threadpool_tester_get_percpu, 0,
	    (void *)&tester_ctx, 0, CTL_CREATE, CTL_EOL);
	RETURN_ERROR;

	error = sysctl_createv(log, 0, &rnode, &cnode,
	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "put_percpu",
	    SYSCTL_DESCR("put percpu pool of specified priority"),
	    threadpool_tester_put_percpu, 0,
	    (void *)&tester_ctx, 0, CTL_CREATE, CTL_EOL);
	RETURN_ERROR;

	error = sysctl_createv(log, 0, &rnode, &cnode,
	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "run_percpu",
	    SYSCTL_DESCR("run on percpu pool of specified priority"),
	    threadpool_tester_run_percpu, 0,
	    (void *)&tester_ctx, 0, CTL_CREATE, CTL_EOL);
	RETURN_ERROR;

	error = sysctl_createv(log, 0, &rnode, &cnode,
	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, "test_value",
	    SYSCTL_DESCR("test value that jobs increment"),
	    threadpool_tester_test_value, 0,
	    (void *)&tester_ctx, 0, CTL_CREATE, CTL_EOL);
	RETURN_ERROR;

	return 0;

 return_error:
 	sysctl_teardown(log);
	return error;
}

static int
threadpool_tester_fini(void)
{
	pri_t pri;

	mutex_enter(&tester_ctx.ctx_mutex);
	for (pri = PRI_NONE/*-1*/; pri < PRI_COUNT; pri++) {
		struct threadpool *pool =
		    tester_ctx.ctx_unbound[pri_to_idx(pri)];
		struct threadpool_percpu *pcpu =
		    tester_ctx.ctx_percpu[pri_to_idx(pri)];

		/*
		 * threadpool_cancel_job() may be called on a pool
		 * other than what the job is scheduled on. This is
		 * safe; see comment in threadpool_cancel_job_async().
		 */

		if (pool != NULL) {
			threadpool_cancel_job(pool, &tester_ctx.ctx_job);
			threadpool_put(pool, pri);
			tester_ctx.ctx_unbound[pri_to_idx(pri)] = NULL;
		}
		if (pcpu != NULL) {
			pool = threadpool_percpu_ref(pcpu);
			threadpool_cancel_job(pool, &tester_ctx.ctx_job);
			threadpool_percpu_put(pcpu, pri);
			tester_ctx.ctx_percpu[pri_to_idx(pri)] = NULL;
		}
	}
	mutex_exit(&tester_ctx.ctx_mutex);
	threadpool_job_destroy(&tester_ctx.ctx_job);
	mutex_destroy(&tester_ctx.ctx_mutex);

	sysctl_teardown(&tester_ctx.ctx_sysctllog);

	return 0;
}

static int
threadpool_tester_modcmd(modcmd_t cmd, void *arg __unused)
{
	int error;

	switch (cmd) {
	case MODULE_CMD_INIT:
		error = threadpool_tester_init();
		break;

	case MODULE_CMD_FINI:
		error = threadpool_tester_fini();
		break;

	case MODULE_CMD_STAT:
	default:
		error = ENOTTY;
	}

	return error;
}