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