1 1.7 martin # $NetBSD: t_cpuctl.sh,v 1.7 2025/06/02 19:04:25 martin Exp $ 2 1.1 jruoho # 3 1.1 jruoho # Copyright (c) 2020 The NetBSD Foundation, Inc. 4 1.1 jruoho # All rights reserved. 5 1.1 jruoho # 6 1.1 jruoho # This code is derived from software contributed to The NetBSD Foundation 7 1.1 jruoho # by Jukka Ruohonen. 8 1.1 jruoho # 9 1.1 jruoho # Redistribution and use in source and binary forms, with or without 10 1.1 jruoho # modification, are permitted provided that the following conditions 11 1.1 jruoho # are met: 12 1.1 jruoho # 1. Redistributions of source code must retain the above copyright 13 1.1 jruoho # notice, this list of conditions and the following disclaimer. 14 1.1 jruoho # 2. Redistributions in binary form must reproduce the above copyright 15 1.1 jruoho # notice, this list of conditions and the following disclaimer in the 16 1.1 jruoho # documentation and/or other materials provided with the distribution. 17 1.1 jruoho # 18 1.1 jruoho # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 1.1 jruoho # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 1.1 jruoho # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 1.1 jruoho # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 1.1 jruoho # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 1.1 jruoho # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 1.1 jruoho # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 1.1 jruoho # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 1.1 jruoho # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 1.1 jruoho # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 1.1 jruoho # POSSIBILITY OF SUCH DAMAGE. 29 1.1 jruoho # 30 1.7 martin tmp="./cpuctl.txt" 31 1.1 jruoho 32 1.1 jruoho setcpu() { 33 1.1 jruoho 34 1.1 jruoho ncpu=$(sysctl -n hw.ncpu) 35 1.1 jruoho 36 1.1 jruoho if [ $ncpu -eq 1 ]; then 37 1.1 jruoho atf_pass 38 1.1 jruoho fi 39 1.1 jruoho 40 1.1 jruoho while [ $ncpu -gt 1 ]; do 41 1.1 jruoho 42 1.5 martin cpuid=$(( $ncpu - 1 )) 43 1.1 jruoho cpuctl $1 $cpuid >/dev/null 2>&1 44 1.1 jruoho 45 1.1 jruoho if [ ! $? -eq 0 ]; then 46 1.1 jruoho $2 $3 47 1.1 jruoho fi 48 1.1 jruoho 49 1.5 martin ncpu=$(( $ncpu - 1 )) 50 1.1 jruoho done 51 1.4 jruoho 52 1.4 jruoho # Additional check that interrupts cannot be 53 1.4 jruoho # disabled for the primary CPU (PR kern/45117). 54 1.4 jruoho # 55 1.4 jruoho cpuctl nointr 0 >/dev/null 2>&1 56 1.4 jruoho 57 1.4 jruoho if [ $? -eq 0 ]; then 58 1.4 jruoho $2 $3 59 1.4 jruoho fi 60 1.1 jruoho } 61 1.1 jruoho 62 1.1 jruoho clean() { 63 1.1 jruoho 64 1.7 martin [ -r $tmp ] || return 0 65 1.7 martin 66 1.1 jruoho i=0 67 1.1 jruoho 68 1.1 jruoho while read line; do 69 1.1 jruoho 70 1.5 martin i=$(( $i + 1 )) 71 1.1 jruoho 72 1.1 jruoho if [ $i -lt 3 ]; then 73 1.1 jruoho continue 74 1.1 jruoho fi 75 1.1 jruoho 76 1.1 jruoho cpuid=$(echo $line | awk '{print $1}') 77 1.1 jruoho online=$(echo $line | awk '{print $3}') 78 1.1 jruoho intr=$(echo $line | awk '{print $4}') 79 1.1 jruoho 80 1.1 jruoho cpuctl $online $cpuid 81 1.1 jruoho cpuctl $intr $cpuid 82 1.1 jruoho 83 1.1 jruoho done < $tmp 84 1.1 jruoho 85 1.1 jruoho rm $tmp 86 1.1 jruoho } 87 1.1 jruoho 88 1.1 jruoho # ncpu. 89 1.1 jruoho # 90 1.1 jruoho atf_test_case ncpu 91 1.1 jruoho ncpu_head() { 92 1.1 jruoho atf_require_prog cpuctl 93 1.1 jruoho atf_set "descr" "Test that cpuctl(8) returns the " \ 94 1.1 jruoho "same number of CPUs as sysctl(8)" 95 1.1 jruoho } 96 1.1 jruoho 97 1.1 jruoho ncpu_body() { 98 1.1 jruoho 99 1.1 jruoho lst=$(cpuctl list | wc -l) 100 1.5 martin ncpu=$(( $lst - 2 )) 101 1.1 jruoho 102 1.1 jruoho if [ $ncpu -eq 1 ]; then 103 1.1 jruoho atf_pass 104 1.1 jruoho fi 105 1.1 jruoho 106 1.1 jruoho if [ $(sysctl -n hw.ncpu) -eq $ncpu ]; then 107 1.1 jruoho atf_pass 108 1.1 jruoho fi 109 1.1 jruoho 110 1.1 jruoho atf_fail "Different number of CPUs" 111 1.1 jruoho } 112 1.1 jruoho 113 1.1 jruoho # err 114 1.1 jruoho # 115 1.1 jruoho atf_test_case err cleanup 116 1.1 jruoho err_head() { 117 1.1 jruoho atf_require_prog cpuctl 118 1.1 jruoho atf_set "require.user" "root" 119 1.1 jruoho atf_set "descr" "Test invalid parameters to cpuctl(8)" 120 1.1 jruoho } 121 1.1 jruoho 122 1.1 jruoho err_body() { 123 1.1 jruoho 124 1.1 jruoho cpuctl list > $tmp 125 1.1 jruoho ncpu=$(sysctl -n hw.ncpu) 126 1.1 jruoho 127 1.1 jruoho atf_check -s exit:1 -e ignore \ 128 1.1 jruoho -o empty -x cpuctl identify -1 129 1.1 jruoho 130 1.1 jruoho atf_check -s exit:1 -e ignore \ 131 1.1 jruoho -o empty -x cpuctl offline -1 132 1.1 jruoho 133 1.1 jruoho atf_check -s exit:1 -e ignore \ 134 1.1 jruoho -o empty -x cpuctl nointr -1 135 1.1 jruoho 136 1.1 jruoho atf_check -s exit:1 -e ignore \ 137 1.5 martin -o empty -x cpuctl identify $(( $ncpu + 1 )) 138 1.1 jruoho 139 1.1 jruoho atf_check -s exit:1 -e ignore \ 140 1.5 martin -o empty -x cpuctl offline $(( $ncpu + 1 )) 141 1.1 jruoho 142 1.1 jruoho atf_check -s exit:1 -e ignore \ 143 1.5 martin -o empty -x cpuctl nointr $(( $ncpu + 1 )) 144 1.1 jruoho } 145 1.1 jruoho 146 1.1 jruoho err_cleanup() { 147 1.1 jruoho clean 148 1.1 jruoho } 149 1.1 jruoho 150 1.1 jruoho # identify 151 1.1 jruoho # 152 1.1 jruoho atf_test_case identify 153 1.1 jruoho identify_head() { 154 1.1 jruoho atf_require_prog cpuctl 155 1.1 jruoho atf_set "descr" "Test that cpuctl(8) identifies at least " \ 156 1.1 jruoho "something without segfaulting (PR bin/54220)" 157 1.1 jruoho } 158 1.1 jruoho 159 1.1 jruoho identify_body() { 160 1.1 jruoho 161 1.1 jruoho ncpu=$(sysctl -n hw.ncpu) 162 1.1 jruoho 163 1.1 jruoho while [ $ncpu -gt 0 ]; do 164 1.5 martin cpuid=$(( $ncpu - 1 )) 165 1.1 jruoho atf_check -s exit:0 -o not-empty -x cpuctl identify $cpuid 166 1.5 martin ncpu=$(( $ncpu - 1 )) 167 1.1 jruoho done 168 1.1 jruoho 169 1.1 jruoho atf_pass 170 1.1 jruoho } 171 1.1 jruoho 172 1.6 mrg # 173 1.6 mrg # check_cpuctl_ok - only run some tests if 174 1.6 mrg # is set ATF_USR_SBIN_CPUCTL_OFFLINE_ENABLE. 175 1.6 mrg check_cpuctl_ok() { 176 1.6 mrg if [ -z "$ATF_USR_SBIN_CPUCTL_OFFLINE_ENABLE" ]; then 177 1.6 mrg return 1 178 1.6 mrg fi 179 1.6 mrg return 0 180 1.6 mrg } 181 1.6 mrg 182 1.1 jruoho # offline 183 1.1 jruoho # 184 1.1 jruoho atf_test_case offline cleanup 185 1.1 jruoho offline_head() { 186 1.1 jruoho atf_require_prog cpuctl 187 1.1 jruoho atf_set "require.user" "root" 188 1.1 jruoho atf_set "descr" "Test setting CPUs offline" 189 1.1 jruoho } 190 1.1 jruoho 191 1.1 jruoho offline_body() { 192 1.1 jruoho 193 1.6 mrg if ! check_cpuctl_ok; then 194 1.6 mrg atf_skip \ 195 1.6 mrg "test sometimes hangs or upsets machine" 196 1.6 mrg fi 197 1.6 mrg 198 1.1 jruoho cpuctl list > $tmp 199 1.1 jruoho setcpu "offline" atf_fail "error in setting a CPU offline" 200 1.1 jruoho 201 1.1 jruoho # Additional check that the boot processor cannot be 202 1.2 jruoho # set offline, as noted in the cpuctl(8) manual page. 203 1.1 jruoho # 204 1.1 jruoho cpuctl offline 0 >/dev/null 2>&1 205 1.1 jruoho 206 1.1 jruoho if [ $? -eq 0 ]; then 207 1.1 jruoho $2 $3 208 1.1 jruoho fi 209 1.1 jruoho } 210 1.1 jruoho 211 1.1 jruoho offline_cleanup() { 212 1.1 jruoho clean 213 1.1 jruoho } 214 1.1 jruoho 215 1.1 jruoho atf_test_case offline_perm 216 1.1 jruoho offline_perm_head() { 217 1.1 jruoho atf_require_prog cpuctl 218 1.1 jruoho atf_set "require.user" "unprivileged" 219 1.1 jruoho atf_set "descr" "Test setting CPUs offline as a user" 220 1.1 jruoho } 221 1.1 jruoho 222 1.1 jruoho offline_perm_body() { 223 1.1 jruoho setcpu "offline" atf_pass 224 1.1 jruoho } 225 1.1 jruoho 226 1.1 jruoho # nointr 227 1.1 jruoho # 228 1.1 jruoho atf_test_case nointr cleanup 229 1.1 jruoho nointr_head() { 230 1.1 jruoho atf_require_prog cpuctl 231 1.1 jruoho atf_set "require.user" "root" 232 1.1 jruoho atf_set "descr" "Test disabling interrupts for CPUs" 233 1.1 jruoho } 234 1.1 jruoho 235 1.1 jruoho nointr_body() { 236 1.6 mrg 237 1.6 mrg if ! check_cpuctl_ok; then 238 1.6 mrg atf_skip \ 239 1.6 mrg "test sometimes hangs or upsets machine" 240 1.6 mrg fi 241 1.6 mrg 242 1.1 jruoho cpuctl list > $tmp 243 1.1 jruoho setcpu "nointr" atf_fail "error in disabling interrupts" 244 1.1 jruoho } 245 1.1 jruoho 246 1.1 jruoho nointr_cleanup() { 247 1.1 jruoho clean 248 1.1 jruoho } 249 1.1 jruoho 250 1.1 jruoho atf_test_case nointr_perm 251 1.1 jruoho nointr_perm_head() { 252 1.1 jruoho atf_require_prog cpuctl 253 1.1 jruoho atf_set "require.user" "unprivileged" 254 1.1 jruoho atf_set "descr" "Test disabling interrupts as a user" 255 1.1 jruoho } 256 1.1 jruoho 257 1.1 jruoho nointr_perm_body() { 258 1.1 jruoho setcpu "nointr" atf_pass 259 1.1 jruoho } 260 1.1 jruoho 261 1.1 jruoho atf_init_test_cases() { 262 1.1 jruoho atf_add_test_case ncpu 263 1.1 jruoho atf_add_test_case err 264 1.1 jruoho atf_add_test_case identify 265 1.1 jruoho atf_add_test_case offline 266 1.1 jruoho atf_add_test_case offline_perm 267 1.1 jruoho atf_add_test_case nointr 268 1.1 jruoho atf_add_test_case nointr_perm 269 1.1 jruoho } 270