2 * Copyright (c) 2015 Netflix Inc. All rights reserved.
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions
6 * 1. Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
16 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
27 #include <sys/param.h>
28 #include <sys/systm.h>
30 #include <sys/cpuctl.h>
31 #include <sys/fcntl.h>
32 #include <sys/ioccom.h>
33 #include <sys/kernel.h>
34 #include <sys/libkern.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
39 #include <sys/pmckern.h>
42 #include <sys/queue.h>
43 #include <sys/sched.h>
45 #include <sys/sysctl.h>
47 #include <tests/kern_testfrwk.h>
48 #include <tests/callout_test.h>
49 #include <machine/cpu.h>
51 MALLOC_DEFINE(M_CALLTMP, "Temp callout Memory", "CalloutTest");
55 struct callout *co_array;
57 int co_number_callouts;
67 static struct callout_run *comaster[MAXCPU];
69 uint64_t callout_total = 0;
71 static void execute_the_co_test(struct callout_run *rn);
76 struct callout_run *rn;
78 rn = (struct callout_run *)arg;
79 printf("The callout test is now complete for thread %d\n",
81 printf("number_callouts:%d\n",
82 rn->co_number_callouts);
83 printf("Callouts that bailed (Not PENDING or ACTIVE cleared):%d\n",
85 printf("Callouts that completed:%d\n", rn->co_completed);
86 printf("Drain calls:%d\n", rn->drain_calls);
87 printf("Zero returns:%d non-zero:%d\n",
96 struct callout_run *rn;
98 rn = (struct callout_run *)arg;
101 mtx_unlock(&rn->lock);
105 test_callout(void *arg)
107 struct callout_run *rn;
113 rn = (struct callout_run *)arg;
114 atomic_add_int(&rn->callout_waiting, 1);
116 if (callout_pending(&rn->co_array[cpu]) ||
117 !callout_active(&rn->co_array[cpu])) {
119 atomic_subtract_int(&rn->callout_waiting, 1);
120 mtx_unlock(&rn->lock);
123 callout_deactivate(&rn->co_array[cpu]);
125 mtx_unlock(&rn->lock);
126 atomic_subtract_int(&rn->callout_waiting, 1);
130 execute_the_co_test(struct callout_run *rn)
133 uint32_t tk_s, tk_e, tk_d;
136 rn->callout_waiting = 0;
137 for (i = 0; i < rn->co_number_callouts; i++) {
138 if (rn->co_test == 1) {
139 /* start all on spread out cpu's */
141 callout_reset_sbt_on(&rn->co_array[i], 3, 0, test_callout, rn,
144 /* Start all on the same CPU */
145 callout_reset_sbt_on(&rn->co_array[i], 3, 0, test_callout, rn,
150 while (rn->callout_waiting != rn->co_number_callouts) {
158 /* OK everyone is waiting and we have the lock */
159 for (i = 0; i < rn->co_number_callouts; i++) {
160 ret = callout_async_drain(&rn->co_array[i], drainit);
167 rn->callout_waiting -= rn->cnt_one;
168 mtx_unlock(&rn->lock);
169 /* Now wait until all are done */
171 while (rn->callout_waiting > 0) {
179 co_saydone((void *)rn);
184 run_callout_test(struct kern_test *test)
186 struct callout_test *u;
189 struct callout_run *rn;
190 int index = test->tot_threads_running;
192 u = (struct callout_test *)test->test_options;
193 if (comaster[index] == NULL) {
194 rn = comaster[index] = malloc(sizeof(struct callout_run), M_CALLTMP, M_WAITOK);
195 memset(comaster[index], 0, sizeof(struct callout_run));
196 mtx_init(&rn->lock, "callouttest", NULL, MTX_DUPOK);
199 rn = comaster[index];
200 rn->co_number_callouts = rn->co_return_npa = 0;
201 rn->co_completed = rn->callout_waiting = 0;
202 rn->drain_calls = rn->cnt_zero = rn->cnt_one = 0;
204 free(rn->co_array, M_CALLTMP);
208 rn->co_number_callouts = u->number_of_callouts;
209 rn->co_test = u->test_number;
210 sz = sizeof(struct callout) * rn->co_number_callouts;
211 rn->co_array = malloc(sz, M_CALLTMP, M_WAITOK);
212 for (i = 0; i < rn->co_number_callouts; i++) {
213 callout_init(&rn->co_array[i], CALLOUT_MPSAFE);
215 execute_the_co_test(rn);
218 int callout_test_is_loaded = 0;
225 for (i = 0; i < MAXCPU; i++) {
227 if (comaster[i]->co_array) {
228 free(comaster[i]->co_array, M_CALLTMP);
229 comaster[i]->co_array = NULL;
231 free(comaster[i], M_CALLTMP);
238 callout_test_modevent(module_t mod, int type, void *data)
244 err = kern_testframework_register("callout_test",
247 printf("Can't load callout_test err:%d returned\n",
250 memset(comaster, 0, sizeof(comaster));
251 callout_test_is_loaded = 1;
255 err = kern_testframework_deregister("callout_test");
257 callout_test_is_loaded = 0;
262 if (callout_test_is_loaded) {
263 err = kern_testframework_deregister("callout_test");
266 callout_test_is_loaded = 0;
276 static moduledata_t callout_test_mod = {
277 .name = "callout_test",
278 .evhand = callout_test_modevent,
282 MODULE_DEPEND(callout_test, kern_testframework, 1, 1, 1);
283 DECLARE_MODULE(callout_test, callout_test_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);