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