]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/man/man9/kern_testfrwk.9
accept_filter(9): Fix a mandoc related error
[FreeBSD/FreeBSD.git] / share / man / man9 / kern_testfrwk.9
1 .\"
2 .\" Copyright (c) 2015 Netflix, Inc.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
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.
12 .\"
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.
23 .\"
24 .\" $FreeBSD$
25 .\"
26 .Dd November 12, 2015
27 .Dt KERN_TESTFRWK 9
28 .Os
29 .Sh NAME
30 .Nm kern_testfrwk
31 .Nd A kernel testing framework
32 .Sh SYNOPSIS
33 kldload kern_testfrwk
34 .Sh DESCRIPTION
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?
38 .Pp
39 Have you ever wanted to test a part of the
40 .Fx
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?
45 .Pp
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.
48 .Pp
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 .
58 .Pp
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.
64 .Pp
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
70         switch (type) {
71         case MOD_LOAD:
72                 err = kern_testframework_register("callout_test",
73                     run_callout_test);
74 .Ed
75 .Pp
76 Here the test is "callout_test" and it is registered to run the function
77 .Fn run_callout_test
78 passing it a
79 .Fa struct kern_test *ptr .
80 The
81 .Vt kern_test
82 structure is defined in
83 .Pa kern_testfrwk.h .
84 .Bd -literal -offset indent
85 struct kern_test {
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];
90 };
91 .Ed
92 .Pp
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
96 .Va name
97 field.
98 The user can also set the number of threads to run with
99 .Va num_threads .
100 .Pp
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
110 .Va num_threads
111 to 2, then the function
112 .Fn run_callout_test
113 will be called once with
114 .Va tot_threads_running
115 to 0, and a second time with
116 .Va tot_threads_running
117 set to 1.
118 .Pp
119 The
120 .Va test_options
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;
128         int test_number;
129 };
130 .Ed
131 .Pp
132 So the first lines of
133 .Fn run_callout_test
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;
138         size_t sz;
139         int i;
140         struct callout_run *rn;
141         int index = test->tot_threads_running;
142
143         u = (struct callout_test *)test->test_options;
144 .Ed
145 .Pp
146 That way it can access:
147 .Va u->test_number
148 (there are two types of tests provided with this test)
149 and
150 .Va u->number_of_callouts
151 (how many simultaneous callouts to run).
152 .Pp
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.
167 .Pp
168 .\" callout_test(4) specific documentation should probably be moved to its own
169 .\" page.
170 After all the callouts are done, a total status is printed
171 showing the results via
172 .Xr printf 9 .
173 The human tester can run
174 .Xr dmesg 8
175 to see the results.
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
178 called.
179 the number of zero_returns should match the number of callout_drains that were
180 called, i.e., 1.
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.
185 .Pp
186 More than one thread can be used with this test, though in the example case it
187 is probably not necessary.
188 .Pp
189 You should not need to change the framework.
190 Just add tests and register them after loading.
191 .Sh AUTHORS
192 The kernel test framework was written by
193 .An Randall Stewart Aq Mt rrs@FreeBSD.org
194 with help from
195 .An John Mark Gurney Aq Mt jmg@FreeBSD.org .