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