drm/atomic-helper: document drm_atomic_helper_check() restrictions
[drm/drm-misc.git] / tools / testing / selftests / cpu-hotplug / cpu-on-off-test.sh
blobd5dc7e0dc726351a739bc487ee72bae9bd6cf090
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
4 SYSFS=
5 # Kselftest framework requirement - SKIP code is 4.
6 ksft_skip=4
7 retval=0
9 prerequisite()
11 msg="skip all tests:"
13 if [ $UID != 0 ]; then
14 echo $msg must be run as root >&2
15 exit $ksft_skip
18 taskset -p 01 $$
20 SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
22 if [ ! -d "$SYSFS" ]; then
23 echo $msg sysfs is not mounted >&2
24 exit $ksft_skip
27 if ! ls $SYSFS/devices/system/cpu/cpu* > /dev/null 2>&1; then
28 echo $msg cpu hotplug is not supported >&2
29 exit $ksft_skip
32 echo "CPU online/offline summary:"
33 online_cpus=`cat $SYSFS/devices/system/cpu/online`
34 online_max=${online_cpus##*-}
36 if [[ "$online_cpus" = "$online_max" ]]; then
37 echo "$msg: since there is only one cpu: $online_cpus"
38 exit $ksft_skip
41 present_cpus=`cat $SYSFS/devices/system/cpu/present`
42 present_max=${present_cpus##*-}
43 echo "present_cpus = $present_cpus present_max = $present_max"
45 echo -e "\t Cpus in online state: $online_cpus"
47 offline_cpus=`cat $SYSFS/devices/system/cpu/offline`
48 if [[ "a$offline_cpus" = "a" ]]; then
49 offline_cpus=0
50 else
51 offline_max=${offline_cpus##*-}
53 echo -e "\t Cpus in offline state: $offline_cpus"
57 # list all hot-pluggable CPUs
59 hotpluggable_cpus()
61 local state=${1:-.\*}
63 for cpu in $SYSFS/devices/system/cpu/cpu*; do
64 if [ -f $cpu/online ] && grep -q $state $cpu/online; then
65 echo ${cpu##/*/cpu}
67 done
70 hotplaggable_offline_cpus()
72 hotpluggable_cpus 0
75 hotpluggable_online_cpus()
77 hotpluggable_cpus 1
80 cpu_is_online()
82 grep -q 1 $SYSFS/devices/system/cpu/cpu$1/online
85 cpu_is_offline()
87 grep -q 0 $SYSFS/devices/system/cpu/cpu$1/online
90 online_cpu()
92 echo 1 > $SYSFS/devices/system/cpu/cpu$1/online
95 offline_cpu()
97 echo 0 > $SYSFS/devices/system/cpu/cpu$1/online
100 online_cpu_expect_success()
102 local cpu=$1
104 if ! online_cpu $cpu; then
105 echo $FUNCNAME $cpu: unexpected fail >&2
106 retval=1
107 elif ! cpu_is_online $cpu; then
108 echo $FUNCNAME $cpu: unexpected offline >&2
109 retval=1
113 online_cpu_expect_fail()
115 local cpu=$1
117 if online_cpu $cpu 2> /dev/null; then
118 echo $FUNCNAME $cpu: unexpected success >&2
119 retval=1
120 elif ! cpu_is_offline $cpu; then
121 echo $FUNCNAME $cpu: unexpected online >&2
122 retval=1
126 offline_cpu_expect_success()
128 local cpu=$1
130 if ! offline_cpu $cpu; then
131 echo $FUNCNAME $cpu: unexpected fail >&2
132 retval=1
133 elif ! cpu_is_offline $cpu; then
134 echo $FUNCNAME $cpu: unexpected offline >&2
135 retval=1
139 offline_cpu_expect_fail()
141 local cpu=$1
143 if offline_cpu $cpu 2> /dev/null; then
144 echo $FUNCNAME $cpu: unexpected success >&2
145 retval=1
146 elif ! cpu_is_online $cpu; then
147 echo $FUNCNAME $cpu: unexpected offline >&2
148 retval=1
152 online_all_hot_pluggable_cpus()
154 for cpu in `hotplaggable_offline_cpus`; do
155 online_cpu_expect_success $cpu
156 done
159 offline_all_hot_pluggable_cpus()
161 local reserve_cpu=$online_max
162 for cpu in `hotpluggable_online_cpus`; do
163 # Reserve one cpu oneline at least.
164 if [ $cpu -eq $reserve_cpu ];then
165 continue
167 offline_cpu_expect_success $cpu
168 done
171 allcpus=0
172 online_cpus=0
173 online_max=0
174 offline_cpus=0
175 offline_max=0
176 present_cpus=0
177 present_max=0
179 while getopts ah opt; do
180 case $opt in
182 allcpus=1
185 echo "Usage $0 [ -a ]"
186 echo -e "\t default offline one cpu"
187 echo -e "\t run with -a option to offline all cpus"
188 exit
190 esac
191 done
193 prerequisite
196 # Safe test (default) - offline and online one cpu
198 if [ $allcpus -eq 0 ]; then
199 echo "Limited scope test: one hotplug cpu"
200 echo -e "\t (leaves cpu in the original state):"
201 echo -e "\t online to offline to online: cpu $online_max"
202 offline_cpu_expect_success $online_max
203 online_cpu_expect_success $online_max
205 if [[ $offline_cpus -gt 0 ]]; then
206 echo -e "\t online to offline to online: cpu $present_max"
207 online_cpu_expect_success $present_max
208 offline_cpu_expect_success $present_max
209 online_cpu $present_max
211 exit $retval
212 else
213 echo "Full scope test: all hotplug cpus"
214 echo -e "\t online all offline cpus"
215 echo -e "\t offline all online cpus"
216 echo -e "\t online all offline cpus"
219 online_all_hot_pluggable_cpus
221 offline_all_hot_pluggable_cpus
223 online_all_hot_pluggable_cpus
225 exit $retval