3 * Netflix Incorporated, All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/callout.h>
34 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
42 #include <sys/sysctl.h>
43 #include <sys/taskqueue.h>
44 #include <sys/queue.h>
45 #include <tests/kern_testfrwk.h>
47 #include <machine/cpu.h>
50 struct kern_test_list {
51 TAILQ_ENTRY(kern_test_list) next;
52 char name[TEST_NAME_LEN];
56 TAILQ_HEAD(ktestlist, kern_test_list);
58 struct kern_test_entry {
59 TAILQ_ENTRY(kern_test_entry) next;
60 struct kern_test_list *kt_e;
61 struct kern_test kt_data;
64 TAILQ_HEAD(ktestqueue, kern_test_entry);
66 MALLOC_DEFINE(M_KTFRWK, "kern_tfrwk", "Kernel Test Framework");
68 struct taskqueue *kfrwk_tq;
69 struct task kfrwk_que;
70 struct ktestlist kfrwk_testlist;
71 struct ktestqueue kfrwk_testq;
76 struct kern_totfrwk kfrwk;
77 static int ktest_frwk_inited = 0;
79 #define KTFRWK_MUTEX_INIT() mtx_init(&kfrwk.kfrwk_mtx, "kern_test_frwk", "tfrwk", MTX_DEF)
81 #define KTFRWK_DESTROY() mtx_destroy(&kfrwk.kfrwk_mtx)
83 #define KTFRWK_LOCK() mtx_lock(&kfrwk.kfrwk_mtx)
85 #define KTFRWK_UNLOCK() mtx_unlock(&kfrwk.kfrwk_mtx)
88 kfrwk_task(void *context, int pending)
90 struct kern_totfrwk *tf;
91 struct kern_test_entry *wk;
93 struct kern_test kt_data;
96 memset(&kt_data, 0, sizeof(kt_data));
98 tf = (struct kern_totfrwk *)context;
100 wk = TAILQ_FIRST(&tf->kfrwk_testq);
102 wk->kt_data.tot_threads_running--;
104 memcpy(&kt_data, &wk->kt_data, sizeof(kt_data));
105 if (wk->kt_data.tot_threads_running == 0) {
106 TAILQ_REMOVE(&tf->kfrwk_testq, wk, next);
109 /* Wake one of my colleages up to help too */
110 taskqueue_enqueue(tf->kfrwk_tq, &tf->kfrwk_que);
113 ktf = wk->kt_e->func;
117 if (wk && free_mem) {
120 /* Execute the test */
125 atomic_add_int(&tf->kfrwk_waiting, 1);
129 kerntest_frwk_init(void)
131 u_int ncpus = mp_ncpus ? mp_ncpus : MAXCPU;
134 TAILQ_INIT(&kfrwk.kfrwk_testq);
135 TAILQ_INIT(&kfrwk.kfrwk_testlist);
136 /* Now lets start up a number of tasks to do the work */
137 TASK_INIT(&kfrwk.kfrwk_que, 0, kfrwk_task, &kfrwk);
138 kfrwk.kfrwk_tq = taskqueue_create_fast("sbtls_task", M_NOWAIT,
139 taskqueue_thread_enqueue, &kfrwk.kfrwk_tq);
140 if (kfrwk.kfrwk_tq == NULL) {
141 printf("Can't start taskqueue for Kernel Test Framework\n");
142 panic("Taskqueue init fails for kfrwk");
144 taskqueue_start_threads(&kfrwk.kfrwk_tq, ncpus, PI_NET, "[kt_frwk task]");
145 kfrwk.kfrwk_waiting = ncpus;
146 ktest_frwk_inited = 1;
151 kerntest_frwk_fini(void)
154 if (!TAILQ_EMPTY(&kfrwk.kfrwk_testlist)) {
155 /* Still modules registered */
159 ktest_frwk_inited = 0;
161 taskqueue_free(kfrwk.kfrwk_tq);
162 /* Ok lets destroy the mutex on the way outs */
168 static int kerntest_execute(SYSCTL_HANDLER_ARGS);
170 SYSCTL_NODE(_kern, OID_AUTO, testfrwk, CTLFLAG_RW, 0, "Kernel Test Framework");
171 SYSCTL_PROC(_kern_testfrwk, OID_AUTO, runtest, (CTLTYPE_STRUCT | CTLFLAG_RW),
172 0, 0, kerntest_execute, "IU", "Execute a kernel test");
175 kerntest_execute(SYSCTL_HANDLER_ARGS)
178 struct kern_test_list *li, *te = NULL;
179 struct kern_test_entry *kte = NULL;
182 if (ktest_frwk_inited == 0) {
185 /* Find the entry if possible */
186 error = SYSCTL_IN(req, &kt, sizeof(struct kern_test));
190 if (kt.num_threads <= 0) {
193 /* Grab some memory */
194 kte = malloc(sizeof(struct kern_test_entry), M_KTFRWK, M_WAITOK);
200 TAILQ_FOREACH(li, &kfrwk.kfrwk_testlist, next) {
201 if (strcmp(li->name, kt.name) == 0) {
207 printf("Can't find the test %s\n", kt.name);
212 /* Ok we have a test item to run, can we? */
213 if (!TAILQ_EMPTY(&kfrwk.kfrwk_testq)) {
214 /* We don't know if there is enough threads */
219 if (kfrwk.kfrwk_waiting < kt.num_threads) {
224 kt.tot_threads_running = kt.num_threads;
225 /* Ok it looks like we can do it, lets get an entry */
227 memcpy(&kte->kt_data, &kt, sizeof(kt));
228 TAILQ_INSERT_TAIL(&kfrwk.kfrwk_testq, kte, next);
229 taskqueue_enqueue(kfrwk.kfrwk_tq, &kfrwk.kfrwk_que);
236 kern_testframework_register(const char *name, kerntfunc func)
239 struct kern_test_list *li, *te = NULL;
243 if (len >= TEST_NAME_LEN) {
246 te = malloc(sizeof(struct kern_test_list), M_KTFRWK, M_WAITOK);
252 /* First does it already exist? */
253 TAILQ_FOREACH(li, &kfrwk.kfrwk_testlist, next) {
254 if (strcmp(li->name, name) == 0) {
260 /* Ok we can do it, lets add it to the list */
262 strcpy(te->name, name);
263 TAILQ_INSERT_TAIL(&kfrwk.kfrwk_testlist, te, next);
270 kern_testframework_deregister(const char *name)
272 struct kern_test_list *li, *te = NULL;
273 u_int ncpus = mp_ncpus ? mp_ncpus : MAXCPU;
277 /* First does it already exist? */
278 TAILQ_FOREACH(li, &kfrwk.kfrwk_testlist, next) {
279 if (strcmp(li->name, name) == 0) {
285 /* It is not registered so no problem */
288 if (ncpus != kfrwk.kfrwk_waiting) {
289 /* We are busy executing something -- can't unload */
293 if (!TAILQ_EMPTY(&kfrwk.kfrwk_testq)) {
294 /* Something still to execute */
298 /* Ok we can remove the dude safely */
299 TAILQ_REMOVE(&kfrwk.kfrwk_testlist, te, next);
300 memset(te, 0, sizeof(struct kern_test_list));
308 kerntest_mod_init(module_t mod, int type, void *data)
314 err = kerntest_frwk_init();
318 if (TAILQ_EMPTY(&kfrwk.kfrwk_testlist)) {
326 err = kerntest_frwk_fini();
334 static moduledata_t kern_test_framework = {
335 .name = "kernel_testfrwk",
336 .evhand = kerntest_mod_init,
340 MODULE_VERSION(kern_testframework, 1);
341 DECLARE_MODULE(kern_testframework, kern_test_framework, SI_SUB_PSEUDO, SI_ORDER_ANY);