Linux 3.11-rc3
[cris-mirror.git] / tools / testing / selftests / memory-hotplug / on-off-test.sh
bloba2816f631542134ddeae828e7ad688d1c2653fe3
1 #!/bin/bash
3 SYSFS=
5 prerequisite()
7 msg="skip all tests:"
9 if [ $UID != 0 ]; then
10 echo $msg must be run as root >&2
11 exit 0
14 SYSFS=`mount -t sysfs | head -1 | awk '{ print $3 }'`
16 if [ ! -d "$SYSFS" ]; then
17 echo $msg sysfs is not mounted >&2
18 exit 0
21 if ! ls $SYSFS/devices/system/memory/memory* > /dev/null 2>&1; then
22 echo $msg memory hotplug is not supported >&2
23 exit 0
28 # list all hot-pluggable memory
30 hotpluggable_memory()
32 local state=${1:-.\*}
34 for memory in $SYSFS/devices/system/memory/memory*; do
35 if grep -q 1 $memory/removable &&
36 grep -q $state $memory/state; then
37 echo ${memory##/*/memory}
39 done
42 hotplaggable_offline_memory()
44 hotpluggable_memory offline
47 hotpluggable_online_memory()
49 hotpluggable_memory online
52 memory_is_online()
54 grep -q online $SYSFS/devices/system/memory/memory$1/state
57 memory_is_offline()
59 grep -q offline $SYSFS/devices/system/memory/memory$1/state
62 online_memory()
64 echo online > $SYSFS/devices/system/memory/memory$1/state
67 offline_memory()
69 echo offline > $SYSFS/devices/system/memory/memory$1/state
72 online_memory_expect_success()
74 local memory=$1
76 if ! online_memory $memory; then
77 echo $FUNCNAME $memory: unexpected fail >&2
78 elif ! memory_is_online $memory; then
79 echo $FUNCNAME $memory: unexpected offline >&2
83 online_memory_expect_fail()
85 local memory=$1
87 if online_memory $memory 2> /dev/null; then
88 echo $FUNCNAME $memory: unexpected success >&2
89 elif ! memory_is_offline $memory; then
90 echo $FUNCNAME $memory: unexpected online >&2
94 offline_memory_expect_success()
96 local memory=$1
98 if ! offline_memory $memory; then
99 echo $FUNCNAME $memory: unexpected fail >&2
100 elif ! memory_is_offline $memory; then
101 echo $FUNCNAME $memory: unexpected offline >&2
105 offline_memory_expect_fail()
107 local memory=$1
109 if offline_memory $memory 2> /dev/null; then
110 echo $FUNCNAME $memory: unexpected success >&2
111 elif ! memory_is_online $memory; then
112 echo $FUNCNAME $memory: unexpected offline >&2
116 error=-12
117 priority=0
118 ratio=10
120 while getopts e:hp:r: opt; do
121 case $opt in
123 error=$OPTARG
126 echo "Usage $0 [ -e errno ] [ -p notifier-priority ] [ -r percent-of-memory-to-offline ]"
127 exit
130 priority=$OPTARG
133 ratio=$OPTARG
135 esac
136 done
138 if ! [ "$error" -ge -4095 -a "$error" -lt 0 ]; then
139 echo "error code must be -4095 <= errno < 0" >&2
140 exit 1
143 prerequisite
146 # Online all hot-pluggable memory
148 for memory in `hotplaggable_offline_memory`; do
149 online_memory_expect_success $memory
150 done
153 # Offline $ratio percent of hot-pluggable memory
155 for memory in `hotpluggable_online_memory`; do
156 if [ $((RANDOM % 100)) -lt $ratio ]; then
157 offline_memory_expect_success $memory
159 done
162 # Online all hot-pluggable memory again
164 for memory in `hotplaggable_offline_memory`; do
165 online_memory_expect_success $memory
166 done
169 # Test with memory notifier error injection
172 DEBUGFS=`mount -t debugfs | head -1 | awk '{ print $3 }'`
173 NOTIFIER_ERR_INJECT_DIR=$DEBUGFS/notifier-error-inject/memory
175 prerequisite_extra()
177 msg="skip extra tests:"
179 /sbin/modprobe -q -r memory-notifier-error-inject
180 /sbin/modprobe -q memory-notifier-error-inject priority=$priority
182 if [ ! -d "$DEBUGFS" ]; then
183 echo $msg debugfs is not mounted >&2
184 exit 0
187 if [ ! -d $NOTIFIER_ERR_INJECT_DIR ]; then
188 echo $msg memory-notifier-error-inject module is not available >&2
189 exit 0
193 prerequisite_extra
196 # Offline $ratio percent of hot-pluggable memory
198 echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error
199 for memory in `hotpluggable_online_memory`; do
200 if [ $((RANDOM % 100)) -lt $ratio ]; then
201 offline_memory_expect_success $memory
203 done
206 # Test memory hot-add error handling (offline => online)
208 echo $error > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_ONLINE/error
209 for memory in `hotplaggable_offline_memory`; do
210 online_memory_expect_fail $memory
211 done
214 # Online all hot-pluggable memory
216 echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_ONLINE/error
217 for memory in `hotplaggable_offline_memory`; do
218 online_memory_expect_success $memory
219 done
222 # Test memory hot-remove error handling (online => offline)
224 echo $error > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error
225 for memory in `hotpluggable_online_memory`; do
226 offline_memory_expect_fail $memory
227 done
229 echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions/MEM_GOING_OFFLINE/error
230 /sbin/modprobe -q -r memory-notifier-error-inject