Home | History | Annotate | Line # | Download | only in cpuctl
t_cpuctl.sh revision 1.7
      1 # $NetBSD: t_cpuctl.sh,v 1.7 2025/06/02 19:04:25 martin Exp $
      2 #
      3 # Copyright (c) 2020 The NetBSD Foundation, Inc.
      4 # All rights reserved.
      5 #
      6 # This code is derived from software contributed to The NetBSD Foundation
      7 # by Jukka Ruohonen.
      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 tmp="./cpuctl.txt"
     31 
     32 setcpu() {
     33 
     34 	ncpu=$(sysctl -n hw.ncpu)
     35 
     36 	if [ $ncpu -eq 1 ]; then
     37 		atf_pass
     38 	fi
     39 
     40 	while [ $ncpu -gt 1 ]; do
     41 
     42 		cpuid=$(( $ncpu - 1 ))
     43 		cpuctl $1 $cpuid >/dev/null 2>&1
     44 
     45 		if [ ! $? -eq 0 ]; then
     46 			$2 $3
     47 		fi
     48 
     49 		ncpu=$(( $ncpu - 1 ))
     50 	done
     51 
     52 	# Additional check that interrupts cannot be
     53 	# disabled for the primary CPU (PR kern/45117).
     54 	#
     55 	cpuctl nointr 0 >/dev/null 2>&1
     56 
     57 	if [ $? -eq 0 ]; then
     58 		$2 $3
     59 	fi
     60 }
     61 
     62 clean() {
     63 
     64 	[ -r $tmp ] || return 0
     65 
     66 	i=0
     67 
     68 	while read line; do
     69 
     70 		i=$(( $i + 1 ))
     71 
     72 		if [ $i -lt 3 ]; then
     73 			continue
     74 		fi
     75 
     76 		cpuid=$(echo $line | awk '{print $1}')
     77 		online=$(echo $line | awk '{print $3}')
     78 		intr=$(echo $line | awk '{print $4}')
     79 
     80 		cpuctl $online $cpuid
     81 		cpuctl $intr $cpuid
     82 
     83 	done < $tmp
     84 
     85 	rm $tmp
     86 }
     87 
     88 # ncpu.
     89 #
     90 atf_test_case ncpu
     91 ncpu_head() {
     92 	atf_require_prog cpuctl
     93 	atf_set "descr" "Test that cpuctl(8) returns the " \
     94 			"same number of CPUs as sysctl(8)"
     95 }
     96 
     97 ncpu_body() {
     98 
     99 	lst=$(cpuctl list | wc -l)
    100 	ncpu=$(( $lst - 2 ))
    101 
    102 	if [ $ncpu -eq 1 ]; then
    103 		atf_pass
    104 	fi
    105 
    106 	if [ $(sysctl -n hw.ncpu) -eq $ncpu ]; then
    107 		atf_pass
    108 	fi
    109 
    110 	atf_fail "Different number of CPUs"
    111 }
    112 
    113 # err
    114 #
    115 atf_test_case err cleanup
    116 err_head() {
    117 	atf_require_prog cpuctl
    118 	atf_set "require.user" "root"
    119 	atf_set "descr" "Test invalid parameters to cpuctl(8)"
    120 }
    121 
    122 err_body() {
    123 
    124 	cpuctl list > $tmp
    125 	ncpu=$(sysctl -n hw.ncpu)
    126 
    127 	atf_check -s exit:1 -e ignore \
    128 		-o empty -x cpuctl identify -1
    129 
    130 	atf_check -s exit:1 -e ignore \
    131 		-o empty -x cpuctl offline -1
    132 
    133 	atf_check -s exit:1 -e ignore \
    134 		-o empty -x cpuctl nointr -1
    135 
    136 	atf_check -s exit:1 -e ignore \
    137 		-o empty -x cpuctl identify $(( $ncpu + 1 ))
    138 
    139 	atf_check -s exit:1 -e ignore \
    140 		  -o empty -x cpuctl offline $(( $ncpu + 1 ))
    141 
    142 	atf_check -s exit:1 -e ignore \
    143 		-o empty -x cpuctl nointr $(( $ncpu + 1 ))
    144 }
    145 
    146 err_cleanup() {
    147 	clean
    148 }
    149 
    150 # identify
    151 #
    152 atf_test_case identify
    153 identify_head() {
    154 	atf_require_prog cpuctl
    155 	atf_set "descr" "Test that cpuctl(8) identifies at least " \
    156 			"something without segfaulting (PR bin/54220)"
    157 }
    158 
    159 identify_body() {
    160 
    161 	ncpu=$(sysctl -n hw.ncpu)
    162 
    163 	while [ $ncpu -gt 0 ]; do
    164 		cpuid=$(( $ncpu - 1 ))
    165 		atf_check -s exit:0 -o not-empty -x cpuctl identify $cpuid
    166 		ncpu=$(( $ncpu - 1 ))
    167 	done
    168 
    169 	atf_pass
    170 }
    171 
    172 #
    173 # check_cpuctl_ok - only run some tests if
    174 # is set ATF_USR_SBIN_CPUCTL_OFFLINE_ENABLE.
    175 check_cpuctl_ok() {
    176 	if [ -z "$ATF_USR_SBIN_CPUCTL_OFFLINE_ENABLE" ]; then
    177 		return 1
    178 	fi
    179 	return 0
    180 }
    181 
    182 # offline
    183 #
    184 atf_test_case offline cleanup
    185 offline_head() {
    186 	atf_require_prog cpuctl
    187 	atf_set "require.user" "root"
    188 	atf_set "descr" "Test setting CPUs offline"
    189 }
    190 
    191 offline_body() {
    192 
    193 	if ! check_cpuctl_ok; then
    194 		atf_skip \
    195 		   "test sometimes hangs or upsets machine"
    196 	fi
    197 
    198 	cpuctl list > $tmp
    199 	setcpu "offline" atf_fail "error in setting a CPU offline"
    200 
    201 	# Additional check that the boot processor cannot be
    202 	# set offline, as noted in the cpuctl(8) manual page.
    203 	#
    204 	cpuctl offline 0 >/dev/null 2>&1
    205 
    206 	if [ $? -eq 0 ]; then
    207 		$2 $3
    208 	fi
    209 }
    210 
    211 offline_cleanup() {
    212 	clean
    213 }
    214 
    215 atf_test_case offline_perm
    216 offline_perm_head() {
    217 	atf_require_prog cpuctl
    218 	atf_set "require.user" "unprivileged"
    219 	atf_set "descr" "Test setting CPUs offline as a user"
    220 }
    221 
    222 offline_perm_body() {
    223 	setcpu "offline" atf_pass
    224 }
    225 
    226 # nointr
    227 #
    228 atf_test_case nointr cleanup
    229 nointr_head() {
    230 	atf_require_prog cpuctl
    231 	atf_set "require.user" "root"
    232 	atf_set "descr" "Test disabling interrupts for CPUs"
    233 }
    234 
    235 nointr_body() {
    236 
    237 	if ! check_cpuctl_ok; then
    238 		atf_skip \
    239 		   "test sometimes hangs or upsets machine"
    240 	fi
    241 
    242 	cpuctl list > $tmp
    243 	setcpu "nointr" atf_fail "error in disabling interrupts"
    244 }
    245 
    246 nointr_cleanup() {
    247 	clean
    248 }
    249 
    250 atf_test_case nointr_perm
    251 nointr_perm_head() {
    252 	atf_require_prog cpuctl
    253 	atf_set "require.user" "unprivileged"
    254 	atf_set "descr" "Test disabling interrupts as a user"
    255 }
    256 
    257 nointr_perm_body() {
    258 	setcpu "nointr" atf_pass
    259 }
    260 
    261 atf_init_test_cases() {
    262 	atf_add_test_case ncpu
    263 	atf_add_test_case err
    264 	atf_add_test_case identify
    265 	atf_add_test_case offline
    266 	atf_add_test_case offline_perm
    267 	atf_add_test_case nointr
    268 	atf_add_test_case nointr_perm
    269 }
    270