1f08c3bdfSopenharmony_ci#!/bin/sh
2f08c3bdfSopenharmony_ci#
3f08c3bdfSopenharmony_ci# Test Case 6 - top
4f08c3bdfSopenharmony_ci#
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ciexport TCID="cpuhotplug06"
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:   Does top work properly when CPU hotplug events occur?
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_cido_clean()
35f08c3bdfSopenharmony_ci{
36f08c3bdfSopenharmony_ci	pid_is_valid ${TOP_PID} && kill_pid ${TOP_PID}
37f08c3bdfSopenharmony_ci}
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ciwhile getopts c:l: OPTION; do
40f08c3bdfSopenharmony_ci	case $OPTION in
41f08c3bdfSopenharmony_ci	c)
42f08c3bdfSopenharmony_ci		CPU_TO_TEST=$OPTARG;;
43f08c3bdfSopenharmony_ci	l)
44f08c3bdfSopenharmony_ci		HOTPLUG06_LOOPS=$OPTARG;;
45f08c3bdfSopenharmony_ci	?)
46f08c3bdfSopenharmony_ci		usage;;
47f08c3bdfSopenharmony_ci	esac
48f08c3bdfSopenharmony_cidone
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ciLOOP_COUNT=1
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_ciif tst_virt_hyperv; then
53f08c3bdfSopenharmony_ci	tst_brkm TCONF "Microsoft Hyper-V detected, no support for CPU hotplug"
54f08c3bdfSopenharmony_cifi
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ciif top -v | grep -q htop; then
57f08c3bdfSopenharmony_ci	tst_brkm TCONF "htop is used instead of top (workaround: alias top='/path/to/real/top')"
58f08c3bdfSopenharmony_cifi
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ciif [ $(get_present_cpus_num) -lt 2 ]; then
61f08c3bdfSopenharmony_ci	tst_brkm TCONF "system doesn't have required CPU hotplug support"
62f08c3bdfSopenharmony_cifi
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ciif [ -z "$CPU_TO_TEST" ]; then
65f08c3bdfSopenharmony_ci	tst_brkm TBROK "Usage: ${0##*/} <CPU to offline>"
66f08c3bdfSopenharmony_cifi
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci# Verify that the specified CPU is available
69f08c3bdfSopenharmony_ciif ! cpu_is_valid "${CPU_TO_TEST}" ; then
70f08c3bdfSopenharmony_ci	tst_brkm TCONF "cpu${CPU_TO_TEST} doesn't support hotplug"
71f08c3bdfSopenharmony_cifi
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_ci# Check that the specified CPU is online; if not, online it
74f08c3bdfSopenharmony_ciif ! cpu_is_online "${CPU_TO_TEST}" ; then
75f08c3bdfSopenharmony_ci	if ! online_cpu ${CPU_TO_TEST}; then
76f08c3bdfSopenharmony_ci		tst_brkm TBROK "CPU${CPU_TO_TEST} cannot be onlined"
77f08c3bdfSopenharmony_ci	fi
78f08c3bdfSopenharmony_cifi
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ciTST_CLEANUP=do_clean
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ciuntil [ $LOOP_COUNT -gt $HOTPLUG06_LOOPS ]; do
83f08c3bdfSopenharmony_ci	# Start up top and give it a little time to run
84f08c3bdfSopenharmony_ci	top -b -d 00.10 > /dev/null 2>&1 &
85f08c3bdfSopenharmony_ci	TOP_PID=$!
86f08c3bdfSopenharmony_ci	sleep 1
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci	# Now offline the CPU
89f08c3bdfSopenharmony_ci	if ! offline_cpu ${CPU_TO_TEST} ; then
90f08c3bdfSopenharmony_ci		tst_brkm TBROK "CPU${CPU_TO_TEST} cannot be offlined"
91f08c3bdfSopenharmony_ci	fi
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci	# Wait a little time for top to notice the CPU is gone
94f08c3bdfSopenharmony_ci	sleep 1
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	# Check that top hasn't crashed
97f08c3bdfSopenharmony_ci	if ! pid_is_valid ${TOP_PID} ; then
98f08c3bdfSopenharmony_ci		tst_resm TFAIL "PID ${TOP_PID} no longer running"
99f08c3bdfSopenharmony_ci		tst_exit
100f08c3bdfSopenharmony_ci	fi
101f08c3bdfSopenharmony_ci
102f08c3bdfSopenharmony_ci	if ! online_cpu ${CPU_TO_TEST}; then
103f08c3bdfSopenharmony_ci		tst_brkm TBROK "CPU${CPU_TO_TEST} cannot be onlined"
104f08c3bdfSopenharmony_ci	fi
105f08c3bdfSopenharmony_ci
106f08c3bdfSopenharmony_ci	kill_pid ${TOP_PID}
107f08c3bdfSopenharmony_ci
108f08c3bdfSopenharmony_ci	LOOP_COUNT=$((LOOP_COUNT+1))
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_cidone
111f08c3bdfSopenharmony_ci
112f08c3bdfSopenharmony_citst_resm TPASS "PID ${TOP_PID} still running."
113f08c3bdfSopenharmony_ci
114f08c3bdfSopenharmony_citst_exit
115