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