1# $NetBSD: t_threadpool.sh,v 1.3 2024/09/08 09:36:53 rillig Exp $
2#
3# Copyright (c) 2018 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# This code is derived from software contributed to The NetBSD Foundation
7# by Jason R. Thorpe.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions
11# are met:
12# 1. Redistributions of source code must retain the above copyright
13#    notice, this list of conditions and the following disclaimer.
14# 2. Redistributions in binary form must reproduce the above copyright
15#    notice, this list of conditions and the following disclaimer in the
16#    documentation and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
31# Pick an arbitrary priority that is not likely to be used.
32tp_pri=5
33
34# The kernel test jig includes a 1 second delay in the job.  We need to
35# wait longer for it to complete.
36job_delay=2
37
38read_sysctl() {
39          echo "${1} = ${2}" >expout
40          atf_check -s exit:0 -o file:expout -e empty sysctl ${1}
41}
42
43write_sysctl() {
44          atf_check -s exit:0 -o ignore -e empty sysctl -w "${1}=${2}"
45}
46
47write_sysctl_fail() {
48          echo "${3}" >experr
49          atf_check -s exit:1 -o ignore -e file:experr sysctl -w "${1}=${2}"
50}
51
52atf_test_case unbound cleanup
53unbound_head() {
54          atf_set "descr" "Test unbound thread pools"
55          atf_set "require.user" "root"
56}
57unbound_body() {
58          modload $(atf_get_srcdir)/threadpool_tester/threadpool_tester.kmod
59          if [ $? -ne 0 ]; then
60                    atf_skip "cannot load threadpool_tester.kmod"
61          fi
62
63          # Ensure that the state is clean.
64          read_sysctl kern.threadpool_tester.test_value 0
65
66          # Create an unbound pool.
67          write_sysctl kern.threadpool_tester.get_unbound $tp_pri
68
69          # Do it again.  We expect this to fail, but the test jig will
70          # do some additional threadpool object lifecycle validation.
71          # (It will not hold the additional reference.)
72          write_sysctl_fail kern.threadpool_tester.get_unbound $tp_pri \
73              "sysctl: kern.threadpool_tester.get_unbound: File exists"
74
75          # Schedule the test jig job on the pool.
76          # Wait for a short period of time and then check that the job
77          # successfully ran.
78          write_sysctl kern.threadpool_tester.run_unbound $tp_pri
79          sleep $job_delay
80          read_sysctl kern.threadpool_tester.test_value 1
81
82          # ...and again.
83          write_sysctl kern.threadpool_tester.run_unbound $tp_pri
84          sleep $job_delay
85          read_sysctl kern.threadpool_tester.test_value 2
86
87          # Now destroy the threadpool.
88          write_sysctl kern.threadpool_tester.put_unbound $tp_pri
89}
90unbound_cleanup() {
91          modunload threadpool_tester >/dev/null 2>&1
92}
93
94atf_test_case percpu cleanup
95percpu_head() {
96          atf_set "descr" "Test percpu thread pools"
97          atf_set "require.user" "root"
98}
99percpu_body() {
100          modload $(atf_get_srcdir)/threadpool_tester/threadpool_tester.kmod
101          if [ $? -ne 0 ]; then
102                    atf_skip "cannot load threadpool_tester.kmod"
103          fi
104
105          # Ensure that the state is clean.
106          read_sysctl kern.threadpool_tester.test_value 0
107
108          # Create a percpu pool.
109          write_sysctl kern.threadpool_tester.get_percpu $tp_pri
110
111          # Do it again.  We expect this to fail, but the test jig will
112          # do some additional threadpool object lifecycle validation.
113          # (It will not hold the additional reference.)
114          write_sysctl_fail kern.threadpool_tester.get_percpu $tp_pri \
115              "sysctl: kern.threadpool_tester.get_percpu: File exists"
116
117          # Schedule the test jig job on the pool.
118          # Wait for a short period of time and then check that the job
119          # successfully ran.
120          write_sysctl kern.threadpool_tester.run_percpu $tp_pri
121          sleep $job_delay
122          read_sysctl kern.threadpool_tester.test_value 1
123
124          # ...and again.
125          write_sysctl kern.threadpool_tester.run_percpu $tp_pri
126          sleep $job_delay
127          read_sysctl kern.threadpool_tester.test_value 2
128
129          # Now destroy the threadpool.
130          write_sysctl kern.threadpool_tester.put_percpu $tp_pri
131}
132percpu_cleanup() {
133          modunload threadpool_tester >/dev/null 2>&1
134}
135
136atf_init_test_cases()
137{
138          atf_add_test_case unbound
139          atf_add_test_case percpu
140}
141