2 .\" Copyright (c) 2015 Netflix Inc.
3 .\" 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 DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
15 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 .\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
18 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 .Nd A kernel testing framework
34 kld_load kern_testfrwk
36 .\" This whole section is not written in manual page style and should be ripped
37 .\" out and replaced. -CEM
38 So what is this sys/tests directory in the kernel all about?
40 Have you ever wanted to test a part of the FreeBSD kernel in some way and you
41 had no real way from user-land to make what you want to occur happen?
42 Say an error path or situation where locking occurs in a particular manner that
43 happens only once in a blue moon?
45 If so, then the kernel test framework is just what you are looking for.
46 It is designed to help you create the situation you want.
48 There are two components to the system: the test framework and your test.
49 This document will describe both components and use the test submitted with the
50 initial commit of this code to discuss the test
51 .Xr ( callout_test 4 ) .
52 All of the tests become kernel loadable modules.
53 The test you write should have a dependency on the test framework.
54 That way it will be loaded automatically with your test.
55 For example, you can see how to do this in the bottom of callout_test.c in
56 .Pa sys/tests/callout_test/callout_test.c .
58 The framework itself is in
59 .Pa sys/tests/framework/kern_testfrwk.c .
60 Its job is to manage the tests that are loaded.
61 (More than one can be loaded.)
62 The idea is pretty simple; you load the test framework and then load your test.
64 When your test loads, you register your tests with the kernel test framework.
65 You do that through a call to
66 .Fn kern_testframework_register .
67 Usually this is done at the module load event as shown below:
68 .Bd -literal -offset indent
71 err = kern_testframework_register("callout_test",
75 Here the test is "callout_test" and it is registered to run the function
78 .Fa struct kern_test *ptr .
81 structure is defined in
83 .Bd -literal -offset indent
85 char name[TEST_NAME_LEN];
86 int num_threads; /* Fill in how many threads you want */
87 int tot_threads_running; /* Private to framework */
88 uint8_t test_options[TEST_OPTION_SPACE];
92 The user sends this structure down via a sysctl to start your test.
93 He or she places the same name you registered ("callout_test"
94 in our example) in the
97 The user can also set the number of threads to run with
100 The framework will start the requested number of kernel threads, all running
101 your test at the same time.
102 The user does not specify anything in
103 .Va tot_threads_running ;
104 it is private to the framework.
105 As the framework calls each of your tests, it will set the
106 .Va tot_threads_running
107 to the index of the thread that your call is made from.
108 For example, if the user sets
110 to 2, then the function
112 will be called once with
113 .Va tot_threads_running
114 to 0, and a second time with
115 .Va tot_threads_running
120 field is a test-specific set of information that is an opaque blob.
121 It is passed in from user space and has a maximum size of 256 bytes.
122 You can pass arbitrary test input in the space.
123 In the case of callout_test we reshape that to:
124 .Bd -literal -offset indent
125 struct callout_test {
126 int number_of_callouts;
131 So the first lines of
133 does the following to get at the user specific data:
134 .\" This is a bad example and violates strict aliasing. It should be replaced.
135 .Bd -literal -offset indent
136 struct callout_test *u;
139 struct callout_run *rn;
140 int index = test->tot_threads_running;
142 u = (struct callout_test *)test->test_options;
145 That way it can access:
147 (there are two types of tests provided with this test)
149 .Va u->number_of_callouts
150 (how many simultaneous callouts to run).
152 Your test can do anything with these bytes.
153 So the callout_test in question wants to create a situation where multiple
154 callouts are all run, that is the
155 .Va number_of_callouts ,
156 and it tries to cancel the callout with the new
157 .Fn callout_async_drain .
158 The threads do this by acquiring the lock in question, and then
159 starting each of the callouts.
160 It waits for the callouts to all go off (the executor spins waits).
161 This forces the situation that the callouts have expired and are all waiting on
162 the lock that the executor holds.
163 After the callouts are all blocked, the executor calls
164 .Fn callout_async_drain
165 on each callout and releases the lock.
167 .\" callout_test(4) specific documentation should probably be moved to its own
169 After all the callouts are done, a total status is printed
170 showing the results via
172 The human tester can run
175 In this case it is expected that if you are running test 0, all the callouts
176 expire on the same CPU so only one callout_drain function would have been
178 the number of zero_returns should match the number of callout_drains that were
180 The one_returns should be the remainder of the callouts.
181 If the test number was 1, the callouts were spread across all CPUs.
182 The number of zero_returns will again match the number of drain calls made
183 which matches the number of CPUs that were put in use.
185 More than one thread can be used with this test, though in the example case it
186 is probably not necessary.
188 You should not need to change the framework.
189 Just add tests and register them after loading.
191 The kernel test framework was written by
192 .An Randall Stewart Aq Mt rrs@FreeBSD.org
194 .An John Mark Gurney Aq Mt jmg@FreeBSD.org .