10 echo $msg must be run as root
>&2
14 SYSFS
=`mount -t sysfs | head -1 | awk '{ print $3 }'`
16 if [ ! -d "$SYSFS" ]; then
17 echo $msg sysfs is not mounted
>&2
21 if ! ls $SYSFS/devices
/system
/memory
/memory
* > /dev
/null
2>&1; then
22 echo $msg memory hotplug is not supported
>&2
26 if ! grep -q 1 $SYSFS/devices
/system
/memory
/memory
*/removable
; then
27 echo $msg no hot-pluggable memory
>&2
33 # list all hot-pluggable memory
39 for memory
in $SYSFS/devices
/system
/memory
/memory
*; do
40 if grep -q 1 $memory/removable
&&
41 grep -q $state $memory/state
; then
42 echo ${memory##/*/memory}
47 hotpluggable_offline_memory
()
49 hotpluggable_memory offline
52 hotpluggable_online_memory
()
54 hotpluggable_memory online
59 grep -q online
$SYSFS/devices
/system
/memory
/memory
$1/state
64 grep -q offline
$SYSFS/devices
/system
/memory
/memory
$1/state
69 echo online
> $SYSFS/devices
/system
/memory
/memory
$1/state
74 echo offline
> $SYSFS/devices
/system
/memory
/memory
$1/state
77 online_memory_expect_success
()
81 if ! online_memory
$memory; then
82 echo $FUNCNAME $memory: unexpected fail
>&2
84 elif ! memory_is_online
$memory; then
85 echo $FUNCNAME $memory: unexpected offline
>&2
91 online_memory_expect_fail
()
95 if online_memory
$memory 2> /dev
/null
; then
96 echo $FUNCNAME $memory: unexpected success
>&2
98 elif ! memory_is_offline
$memory; then
99 echo $FUNCNAME $memory: unexpected online
>&2
105 offline_memory_expect_success
()
109 if ! offline_memory
$memory; then
110 echo $FUNCNAME $memory: unexpected fail
>&2
112 elif ! memory_is_offline
$memory; then
113 echo $FUNCNAME $memory: unexpected offline
>&2
119 offline_memory_expect_fail
()
123 if offline_memory
$memory 2> /dev
/null
; then
124 echo $FUNCNAME $memory: unexpected success
>&2
126 elif ! memory_is_online
$memory; then
127 echo $FUNCNAME $memory: unexpected offline
>&2
138 while getopts e
:hp
:r
: opt
; do
144 echo "Usage $0 [ -e errno ] [ -p notifier-priority ] [ -r percent-of-memory-to-offline ]"
152 if [ "$ratio" -gt 100 ] ||
[ "$ratio" -lt 0 ]; then
153 echo "The percentage should be an integer within 0~100 range"
160 if ! [ "$error" -ge -4095 -a "$error" -lt 0 ]; then
161 echo "error code must be -4095 <= errno < 0" >&2
167 echo "Test scope: $ratio% hotplug memory"
170 # Online all hot-pluggable memory
172 hotpluggable_num
=`hotpluggable_offline_memory | wc -l`
173 echo -e "\t online all hot-pluggable memory in offline state:"
174 if [ "$hotpluggable_num" -gt 0 ]; then
175 for memory
in `hotpluggable_offline_memory`; do
176 echo "offline->online memory$memory"
177 if ! online_memory_expect_success
$memory; then
182 echo -e "\t\t SKIPPED - no hot-pluggable memory in offline state"
186 # Offline $ratio percent of hot-pluggable memory
188 hotpluggable_num
=`hotpluggable_online_memory | wc -l`
189 target
=`echo "a=$hotpluggable_num*$ratio; if ( a%100 ) a/100+1 else a/100" | bc`
190 echo -e "\t offline $ratio% hot-pluggable memory in online state"
191 echo -e "\t trying to offline $target out of $hotpluggable_num memory block(s):"
192 for memory
in `hotpluggable_online_memory`; do
193 if [ "$target" -gt 0 ]; then
194 echo "online->offline memory$memory"
195 if offline_memory_expect_success
$memory; then
196 target
=$
(($target - 1))
200 if [ "$target" -gt 0 ]; then
202 echo -e "\t\t FAILED - unable to offline some memory blocks, device busy?"
206 # Online all hot-pluggable memory again
208 hotpluggable_num
=`hotpluggable_offline_memory | wc -l`
209 echo -e "\t online all hot-pluggable memory in offline state:"
210 if [ "$hotpluggable_num" -gt 0 ]; then
211 for memory
in `hotpluggable_offline_memory`; do
212 echo "offline->online memory$memory"
213 if ! online_memory_expect_success
$memory; then
218 echo -e "\t\t SKIPPED - no hot-pluggable memory in offline state"
222 # Test with memory notifier error injection
225 DEBUGFS
=`mount -t debugfs | head -1 | awk '{ print $3 }'`
226 NOTIFIER_ERR_INJECT_DIR
=$DEBUGFS/notifier-error-inject
/memory
230 msg
="skip extra tests:"
232 /sbin
/modprobe
-q -r memory-notifier-error-inject
233 /sbin
/modprobe
-q memory-notifier-error-inject priority
=$priority
235 if [ ! -d "$DEBUGFS" ]; then
236 echo $msg debugfs is not mounted
>&2
240 if [ ! -d $NOTIFIER_ERR_INJECT_DIR ]; then
241 echo $msg memory-notifier-error-inject module is not available
>&2
246 echo -e "\t Test with memory notifier error injection"
250 # Offline $ratio percent of hot-pluggable memory
252 echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions
/MEM_GOING_OFFLINE
/error
253 for memory
in `hotpluggable_online_memory`; do
254 if [ $
((RANDOM
% 100)) -lt $ratio ]; then
255 offline_memory_expect_success
$memory
260 # Test memory hot-add error handling (offline => online)
262 echo $error > $NOTIFIER_ERR_INJECT_DIR/actions
/MEM_GOING_ONLINE
/error
263 for memory
in `hotpluggable_offline_memory`; do
264 online_memory_expect_fail
$memory
268 # Online all hot-pluggable memory
270 echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions
/MEM_GOING_ONLINE
/error
271 for memory
in `hotpluggable_offline_memory`; do
272 online_memory_expect_success
$memory
276 # Test memory hot-remove error handling (online => offline)
278 echo $error > $NOTIFIER_ERR_INJECT_DIR/actions
/MEM_GOING_OFFLINE
/error
279 for memory
in `hotpluggable_online_memory`; do
280 offline_memory_expect_fail
$memory
283 echo 0 > $NOTIFIER_ERR_INJECT_DIR/actions
/MEM_GOING_OFFLINE
/error
284 /sbin
/modprobe
-q -r memory-notifier-error-inject