Home | History | Annotate | Line # | Download | only in cpuctl
t_cpuctl.sh revision 1.1
      1 # $NetBSD: t_cpuctl.sh,v 1.1 2020/06/24 09:32:41 jruoho 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="/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 	# Skip the boot processor. Disabling interrupts
     41 	# on it will hang the system (PR kern/45117).
     42 	#
     43 	while [ $ncpu -gt 1 ]; do
     44 
     45 		cpuid=$(expr $ncpu - 1)
     46 		cpuctl $1 $cpuid >/dev/null 2>&1
     47 
     48 		if [ ! $? -eq 0 ]; then
     49 			$2 $3
     50 		fi
     51 
     52 		ncpu=$(expr $ncpu - 1)
     53 	done
     54 }
     55 
     56 clean() {
     57 
     58 	i=0
     59 
     60 	while read line; do
     61 
     62 		i=$(expr $i + 1)
     63 
     64 		if [ $i -lt 3 ]; then
     65 			continue
     66 		fi
     67 
     68 		cpuid=$(echo $line | awk '{print $1}')
     69 		online=$(echo $line | awk '{print $3}')
     70 		intr=$(echo $line | awk '{print $4}')
     71 
     72 		cpuctl $online $cpuid
     73 		cpuctl $intr $cpuid
     74 
     75 	done < $tmp
     76 
     77 	rm $tmp
     78 }
     79 
     80 # ncpu.
     81 #
     82 atf_test_case ncpu
     83 ncpu_head() {
     84 	atf_require_prog cpuctl
     85 	atf_set "descr" "Test that cpuctl(8) returns the " \
     86 			"same number of CPUs as sysctl(8)"
     87 }
     88 
     89 ncpu_body() {
     90 
     91 	lst=$(cpuctl list | wc -l)
     92 	ncpu=$(expr $lst - 2)
     93 
     94 	if [ $ncpu -eq 1 ]; then
     95 		atf_pass
     96 	fi
     97 
     98 	if [ $(sysctl -n hw.ncpu) -eq $ncpu ]; then
     99 		atf_pass
    100 	fi
    101 
    102 	atf_fail "Different number of CPUs"
    103 }
    104 
    105 # err
    106 #
    107 atf_test_case err cleanup
    108 err_head() {
    109 	atf_require_prog cpuctl
    110 	atf_set "require.user" "root"
    111 	atf_set "descr" "Test invalid parameters to cpuctl(8)"
    112 }
    113 
    114 err_body() {
    115 
    116 	cpuctl list > $tmp
    117 	ncpu=$(sysctl -n hw.ncpu)
    118 
    119 	atf_check -s exit:1 -e ignore \
    120 		-o empty -x cpuctl identify -1
    121 
    122 	atf_check -s exit:1 -e ignore \
    123 		-o empty -x cpuctl offline -1
    124 
    125 	atf_check -s exit:1 -e ignore \
    126 		-o empty -x cpuctl nointr -1
    127 
    128 	atf_check -s exit:1 -e ignore \
    129 		-o empty -x cpuctl identify $(exp ncpu + 1)
    130 
    131 	atf_check -s exit:1 -e ignore \
    132 		-o empty -x cpuctl offline $(exp ncpu + 1)
    133 
    134 	atf_check -s exit:1 -e ignore \
    135 		-o empty -x cpuctl nointr $(exp ncpu + 1)
    136 }
    137 
    138 err_cleanup() {
    139 	clean
    140 }
    141 
    142 # identify
    143 #
    144 atf_test_case identify
    145 identify_head() {
    146 	atf_require_prog cpuctl
    147 	atf_set "descr" "Test that cpuctl(8) identifies at least " \
    148 			"something without segfaulting (PR bin/54220)"
    149 }
    150 
    151 identify_body() {
    152 
    153 	ncpu=$(sysctl -n hw.ncpu)
    154 
    155 	while [ $ncpu -gt 0 ]; do
    156 		cpuid=$(expr $ncpu - 1)
    157 		atf_check -s exit:0 -o not-empty -x cpuctl identify $cpuid
    158 		ncpu=$(expr $ncpu - 1)
    159 	done
    160 
    161 	atf_pass
    162 }
    163 
    164 # offline
    165 #
    166 atf_test_case offline cleanup
    167 offline_head() {
    168 	atf_require_prog cpuctl
    169 	atf_set "require.user" "root"
    170 	atf_set "descr" "Test setting CPUs offline"
    171 }
    172 
    173 offline_body() {
    174 
    175 	cpuctl list > $tmp
    176 	setcpu "offline" atf_fail "error in setting a CPU offline"
    177 
    178 	# Additional check that the boot processor cannot be
    179 	# set offline, as noted in the cpuctl(9) manual page.
    180 	#
    181 	cpuctl offline 0 >/dev/null 2>&1
    182 
    183 	if [ $? -eq 0 ]; then
    184 		$2 $3
    185 	fi
    186 }
    187 
    188 offline_cleanup() {
    189 	clean
    190 }
    191 
    192 atf_test_case offline_perm
    193 offline_perm_head() {
    194 	atf_require_prog cpuctl
    195 	atf_set "require.user" "unprivileged"
    196 	atf_set "descr" "Test setting CPUs offline as a user"
    197 }
    198 
    199 offline_perm_body() {
    200 	setcpu "offline" atf_pass
    201 }
    202 
    203 # nointr
    204 #
    205 atf_test_case nointr cleanup
    206 nointr_head() {
    207 	atf_require_prog cpuctl
    208 	atf_set "require.user" "root"
    209 	atf_set "descr" "Test disabling interrupts for CPUs"
    210 }
    211 
    212 nointr_body() {
    213 	cpuctl list > $tmp
    214 	setcpu "nointr" atf_fail "error in disabling interrupts"
    215 }
    216 
    217 nointr_cleanup() {
    218 	clean
    219 }
    220 
    221 atf_test_case nointr_perm
    222 nointr_perm_head() {
    223 	atf_require_prog cpuctl
    224 	atf_set "require.user" "unprivileged"
    225 	atf_set "descr" "Test disabling interrupts as a user"
    226 }
    227 
    228 nointr_perm_body() {
    229 	setcpu "nointr" atf_pass
    230 }
    231 
    232 atf_init_test_cases() {
    233 	atf_add_test_case ncpu
    234 	atf_add_test_case err
    235 	atf_add_test_case identify
    236 	atf_add_test_case offline
    237 	atf_add_test_case offline_perm
    238 	atf_add_test_case nointr
    239 	atf_add_test_case nointr_perm
    240 }
    241