# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-# Also initializes the HOTPLUGGABLE_CPUS array.
+# Also initializes the ALL_CPUS and HOTPLUGGABLE_CPUS arrays.
_have_cpu_hotplug() {
+ ALL_CPUS=()
HOTPLUGGABLE_CPUS=()
CPUS_ONLINE_SAVED=()
local cpu_dir cpu
for cpu_dir in /sys/devices/system/cpu/cpu+([0-9]); do
+ ALL_CPUS+=("$cpu")
if [[ -w ${cpu_dir}/online ]]; then
cpu="${cpu_dir#/sys/devices/system/cpu/cpu}"
HOTPLUGGABLE_CPUS+=("$cpu")
fi
done
- if [[ ${#HOTPLUGGABLE_CPUS[@]} -eq 0 ]]; then
+ if [[ ${#ALL_CPUS[@]} -eq 1 || ${#HOTPLUGGABLE_CPUS[@]} -eq 0 ]]; then
SKIP_REASON="CPU hotplugging is not supported"
return 1
fi
_online_cpu() {
RESTORE_CPUS_ONLINE=1
- echo 1 >"/sys/devices/system/cpu/cpu$1/online"
+ echo 1 > "/sys/devices/system/cpu/cpu$1/online"
}
_offline_cpu() {
RESTORE_CPUS_ONLINE=1
- echo 0 >"/sys/devices/system/cpu/cpu$1/online"
+ echo 0 > "/sys/devices/system/cpu/cpu$1/online"
}
# start fio job
_run_fio_rand_io --filename="$TEST_DEV" --size="$size" &
- # while job is running, hotplug CPUs randomly
- while kill -0 $! 2>/dev/null; do
- idx=$((RANDOM % ${#HOTPLUGGABLE_CPUS[@]}))
- if (( RANDOM % 2 )); then
- _online_cpu "${HOTPLUGGABLE_CPUS[$idx]}"
+ local online_cpus=()
+ local offline_cpus=()
+ local offlining=1
+ local max_offline=${#HOTPLUGGABLE_CPUS[@]}
+ if [[ ${#HOTPLUGGABLE_CPUS[@]} -eq ${#ALL_CPUS[@]} ]]; then
+ (( max_offline-- ))
+ fi
+ for cpu in "${HOTPLUGGABLE_CPUS[@]}"; do
+ if (( "$(cat "/sys/devices/system/cpu/cpu${cpu}/online")" )); then
+ online_cpus+=("$cpu")
+ else
+ offline_cpus+=("$cpu")
+ fi
+ done
+
+ # while job is running, hotplug CPUs
+ while kill -0 $! 2> /dev/null; do
+ if (( offlining && ${#offline_cpus[@]} == max_offline )); then
+ offlining=0
+ elif (( ! offlining && ${#online_cpus[@]} == ${#HOTPLUGGABLE_CPUS[@]} )); then
+ offlining=1
+ fi
+
+ if (( offlining )); then
+ idx=$((RANDOM % ${#online_cpus[@]}))
+ echo "offlining $idx (online=${online_cpus[@]} offline=${offline_cpus[@]})" >> /dev/kmsg
+ _offline_cpu "${online_cpus[$idx]}"
+ offline_cpus+=("${online_cpus[$idx]}")
+ unset online_cpus[$idx]
+ online_cpus=("${online_cpus[@]}")
else
- _offline_cpu "${HOTPLUGGABLE_CPUS[$idx]}"
+ idx=$((RANDOM % ${#offline_cpus[@]}))
+ echo "onlining $idx (online=${online_cpus[@]} offline=${offline_cpus[@]})" >> /dev/kmsg
+ _online_cpu "${offline_cpus[$idx]}"
+ online_cpus+=("${offline_cpus[$idx]}")
+ unset offline_cpus[$idx]
+ offline_cpus=("${offline_cpus[@]}")
fi
+
sleep .2
done