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