1f08c3bdfSopenharmony_ci#!/bin/sh
2f08c3bdfSopenharmony_ci#
3f08c3bdfSopenharmony_ci# Test Case 3
4f08c3bdfSopenharmony_ci#
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ciexport TCID="cpuhotplug03"
7f08c3bdfSopenharmony_ciexport TST_TOTAL=1
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci# Includes:
10f08c3bdfSopenharmony_ci. test.sh
11f08c3bdfSopenharmony_ci. cpuhotplug_testsuite.sh
12f08c3bdfSopenharmony_ci. cpuhotplug_hotplug.sh
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_cicat <<EOF
15f08c3bdfSopenharmony_ciName:   $TCID
16f08c3bdfSopenharmony_ciDate:   `date`
17f08c3bdfSopenharmony_ciDesc:   Do tasks get scheduled to a newly on-lined CPU?
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ciEOF
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ciusage()
22f08c3bdfSopenharmony_ci{
23f08c3bdfSopenharmony_ci	cat << EOF
24f08c3bdfSopenharmony_ci	usage: $0 -c cpu -l loop
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_ci	OPTIONS
27f08c3bdfSopenharmony_ci		-c  cpu which is specified for testing
28f08c3bdfSopenharmony_ci		-l  number of cycle test
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ciEOF
31f08c3bdfSopenharmony_ci	exit 1
32f08c3bdfSopenharmony_ci}
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci# do_clean()
35f08c3bdfSopenharmony_ci#
36f08c3bdfSopenharmony_ci#  Callback to be executed when script exits from a user interrupt
37f08c3bdfSopenharmony_ci#  or regular program termination
38f08c3bdfSopenharmony_ci#
39f08c3bdfSopenharmony_cido_clean()
40f08c3bdfSopenharmony_ci{
41f08c3bdfSopenharmony_ci	# Kill all the processes we started up and get rid of their pid files
42f08c3bdfSopenharmony_ci	if [ -e "/var/run/hotplug4_$$.pid" ]; then
43f08c3bdfSopenharmony_ci		for i in `cat /var/run/hotplug4_$$.pid`; do
44f08c3bdfSopenharmony_ci			kill_pid $i
45f08c3bdfSopenharmony_ci		done
46f08c3bdfSopenharmony_ci		rm /var/run/hotplug4_$$.pid
47f08c3bdfSopenharmony_ci	fi
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	# Restore CPU states
50f08c3bdfSopenharmony_ci	set_all_cpu_states "$cpu_states"
51f08c3bdfSopenharmony_ci}
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ciwhile getopts c:l: OPTION; do
54f08c3bdfSopenharmony_cicase $OPTION in
55f08c3bdfSopenharmony_ci	c)
56f08c3bdfSopenharmony_ci		CPU_TO_TEST=$OPTARG;;
57f08c3bdfSopenharmony_ci	l)
58f08c3bdfSopenharmony_ci		HOTPLUG03_LOOPS=$OPTARG;;
59f08c3bdfSopenharmony_ci	?)
60f08c3bdfSopenharmony_ci		usage;;
61f08c3bdfSopenharmony_ci	esac
62f08c3bdfSopenharmony_cidone
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ciLOOP_COUNT=1
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ciif tst_virt_hyperv; then
67f08c3bdfSopenharmony_ci	tst_brkm TCONF "Microsoft Hyper-V detected, no support for CPU hotplug"
68f08c3bdfSopenharmony_cifi
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_cicpus_num=$(get_present_cpus_num)
71f08c3bdfSopenharmony_ciif [ $cpus_num -lt 2 ]; then
72f08c3bdfSopenharmony_ci	tst_brkm TCONF "system doesn't have required CPU hotplug support"
73f08c3bdfSopenharmony_cifi
74f08c3bdfSopenharmony_ci
75f08c3bdfSopenharmony_ciif [ -z $CPU_TO_TEST ]; then
76f08c3bdfSopenharmony_ci	tst_brkm TBROK "usage: ${0##*} <CPU to online>"
77f08c3bdfSopenharmony_cifi
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci# Validate the specified CPU is available
80f08c3bdfSopenharmony_ciif ! cpu_is_valid "${CPU_TO_TEST}" ; then
81f08c3bdfSopenharmony_ci	tst_brkm TCONF "cpu${CPU_TO_TEST} doesn't support hotplug"
82f08c3bdfSopenharmony_cifi
83f08c3bdfSopenharmony_ci
84f08c3bdfSopenharmony_ciTST_CLEANUP=do_clean
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_cicpu_states=$(get_all_cpu_states)
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ciuntil [ $LOOP_COUNT -gt $HOTPLUG03_LOOPS ]; do
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	# Turns on all CPUs
91f08c3bdfSopenharmony_ci	for i in $( get_hotplug_cpus ); do
92f08c3bdfSopenharmony_ci            if ! cpu_is_online $i; then
93f08c3bdfSopenharmony_ci				if ! online_cpu $i; then
94f08c3bdfSopenharmony_ci                    tst_brkm TBROK "Could not online cpu $i"
95f08c3bdfSopenharmony_ci                fi
96f08c3bdfSopenharmony_ci            fi
97f08c3bdfSopenharmony_ci	done
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_ci	if ! offline_cpu ${CPU_TO_TEST} ; then
100f08c3bdfSopenharmony_ci		tst_resm TBROK "CPU${CPU_TO_TEST} cannot be offlined"
101f08c3bdfSopenharmony_ci	fi
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci	# Start up a number of processes equal to twice the number of
104f08c3bdfSopenharmony_ci	# CPUs we have.  This is to help ensure we've got enough processes
105f08c3bdfSopenharmony_ci	# that at least one will migrate to the new CPU.  Store the PIDs
106f08c3bdfSopenharmony_ci	# so we can kill them later.
107f08c3bdfSopenharmony_ci	number_of_procs=$((cpus_num*2))
108f08c3bdfSopenharmony_ci	until [ $number_of_procs -eq 0 ]; do
109f08c3bdfSopenharmony_ci		cpuhotplug_do_spin_loop > /dev/null 2>&1 &
110f08c3bdfSopenharmony_ci		echo $! >> /var/run/hotplug4_$$.pid
111f08c3bdfSopenharmony_ci		number_of_procs=$((number_of_procs-1))
112f08c3bdfSopenharmony_ci	done
113f08c3bdfSopenharmony_ci
114f08c3bdfSopenharmony_ci	ps aux | head -n 1
115f08c3bdfSopenharmony_ci	ps aux | grep cpuhotplug_do_spin_loop
116f08c3bdfSopenharmony_ci
117f08c3bdfSopenharmony_ci	# Online the CPU
118f08c3bdfSopenharmony_ci	tst_resm TINFO "Onlining CPU ${CPU_TO_TEST}"
119f08c3bdfSopenharmony_ci	if ! online_cpu ${CPU_TO_TEST}; then
120f08c3bdfSopenharmony_ci		tst_brkm TBROK "CPU${CPU_TO_TEST} cannot be onlined"
121f08c3bdfSopenharmony_ci	fi
122f08c3bdfSopenharmony_ci
123f08c3bdfSopenharmony_ci	sleep 1
124f08c3bdfSopenharmony_ci
125f08c3bdfSopenharmony_ci	# Verify at least one process has migrated to the new CPU
126f08c3bdfSopenharmony_ci	# Since procps v3.3.15, we need to accurately select command name
127f08c3bdfSopenharmony_ci	# by -C option, because procps cannot trucate normal command name
128f08c3bdfSopenharmony_ci	# to 15 characters by default).
129f08c3bdfSopenharmony_ci	ps -o psr -o command --no-headers -C cpuhotplug_do_s
130f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
131f08c3bdfSopenharmony_ci		tst_brkm TBROK "No cpuhotplug_do_spin_loop processes \
132f08c3bdfSopenharmony_ci			found on any processor"
133f08c3bdfSopenharmony_ci	fi
134f08c3bdfSopenharmony_ci	NUM=`ps -o psr -o command --no-headers -C cpuhotplug_do_s \
135f08c3bdfSopenharmony_ci		| sed -e "s/^ *//" | cut -d' ' -f 1 | grep "^${CPU_TO_TEST}$" \
136f08c3bdfSopenharmony_ci		| wc -l`
137f08c3bdfSopenharmony_ci	if [ $NUM -lt 1 ]; then
138f08c3bdfSopenharmony_ci		tst_resm TFAIL "No cpuhotplug_do_spin_loop processes found on \
139f08c3bdfSopenharmony_ci			CPU${CPU_TO_TEST}"
140f08c3bdfSopenharmony_ci		tst_exit
141f08c3bdfSopenharmony_ci	fi
142f08c3bdfSopenharmony_ci
143f08c3bdfSopenharmony_ci	do_clean
144f08c3bdfSopenharmony_ci
145f08c3bdfSopenharmony_ci	LOOP_COUNT=$((LOOP_COUNT+1))
146f08c3bdfSopenharmony_cidone
147f08c3bdfSopenharmony_ci
148f08c3bdfSopenharmony_citst_resm TPASS "$NUM cpuhotplug_do_spin_loop processes found on \
149f08c3bdfSopenharmony_ci	CPU${CPU_TO_TEST}"
150f08c3bdfSopenharmony_ci
151f08c3bdfSopenharmony_citst_exit
152