2 * Copyright (c) 2018, Matthew Macy <mmacy@freebsd.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * 2. Neither the name of Matthew Macy nor the names of its
11 * contributors may be used to endorse or promote products derived from
12 * this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND 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 COPYRIGHT OWNER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/types.h>
33 #include <sys/counter.h>
34 #include <sys/epoch.h>
35 #include <sys/gtaskqueue.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
43 #include <sys/sched.h>
45 #include <sys/sysctl.h>
46 #include <sys/systm.h>
49 struct epoch_test_instance {
54 static int iterations;
55 #define ET_EXITING 0x1
56 static volatile int state_flags;
57 static struct mtx state_mtx __aligned(CACHE_LINE_SIZE*2);
58 MTX_SYSINIT(state_mtx, &state_mtx, "epoch state mutex", MTX_DEF);
59 static struct mtx mutexA __aligned(CACHE_LINE_SIZE*2);
60 MTX_SYSINIT(mutexA, &mutexA, "epoch mutexA", MTX_DEF);
61 static struct mtx mutexB __aligned(CACHE_LINE_SIZE*2);
62 MTX_SYSINIT(mutexB, &mutexB, "epoch mutexB", MTX_DEF);
66 epoch_testcase1(struct epoch_test_instance *eti)
70 struct epoch_tracker et;
74 if (eti->threadid & 0x1)
79 while (i < iterations) {
80 epoch_enter_preempt(test_epoch, &et);
84 epoch_exit_preempt(test_epoch, &et);
85 epoch_wait_preempt(test_epoch);
87 printf("test1: thread: %d took %d ticks to complete %d iterations\n",
88 eti->threadid, ticks - startticks, iterations);
92 epoch_testcase2(struct epoch_test_instance *eti)
96 struct epoch_tracker et;
102 while (i < iterations) {
103 epoch_enter_preempt(test_epoch, &et);
108 epoch_exit_preempt(test_epoch, &et);
109 epoch_wait_preempt(test_epoch);
111 printf("test2: thread: %d took %d ticks to complete %d iterations\n",
112 eti->threadid, ticks - startticks, iterations);
116 testloop(void *arg) {
118 mtx_lock(&state_mtx);
119 while ((state_flags & ET_EXITING) == 0) {
120 msleep(&state_mtx, &state_mtx, 0, "epoch start wait", 0);
121 if (state_flags & ET_EXITING)
123 mtx_unlock(&state_mtx);
124 epoch_testcase2(arg);
126 epoch_testcase1(arg);
127 mtx_lock(&state_mtx);
130 mtx_unlock(&state_mtx);
134 static struct thread *testthreads[MAXCPU];
135 static struct epoch_test_instance etilist[MAXCPU];
141 int i, error, pri_range, pri_off;
143 pri_range = PRI_MIN_TIMESHARE - PRI_MIN_REALTIME;
144 test_epoch = epoch_alloc(EPOCH_PREEMPT);
145 for (i = 0; i < mp_ncpus*2; i++) {
146 etilist[i].threadid = i;
147 error = kthread_add(testloop, &etilist[i], NULL, &testthreads[i],
148 0, 0, "epoch_test_%d", i);
150 printf("%s: kthread_add(epoch_test): error %d", __func__,
153 pri_off = (i*4)%pri_range;
156 sched_prio(td, PRI_MIN_REALTIME + pri_off);
165 epochtest_execute(SYSCTL_HANDLER_ARGS)
173 error = sysctl_handle_int(oidp, &v, 0, req);
176 if (req->newptr == NULL)
180 mtx_lock(&state_mtx);
183 mtx_unlock(&state_mtx);
188 SYSCTL_NODE(_kern, OID_AUTO, epochtest, CTLFLAG_RW, 0, "Epoch Test Framework");
189 SYSCTL_PROC(_kern_epochtest, OID_AUTO, runtest, (CTLTYPE_INT | CTLFLAG_RW),
190 0, 0, epochtest_execute, "I", "Execute an epoch test");
193 epoch_test_module_event_handler(module_t mod, int what, void *arg __unused)
199 if ((err = test_modinit()) != 0)
203 mtx_lock(&state_mtx);
204 state_flags = ET_EXITING;
206 mtx_unlock(&state_mtx);
208 pause("epoch unload", 3*hz);
217 static moduledata_t epoch_test_moduledata = {
219 epoch_test_module_event_handler,
223 MODULE_VERSION(epoch_test, 1);
224 DECLARE_MODULE(epoch_test, epoch_test_moduledata, SI_SUB_PSEUDO, SI_ORDER_ANY);