1f08c3bdfSopenharmony_ci#!/bin/sh
2f08c3bdfSopenharmony_ci#
3f08c3bdfSopenharmony_ci# Test Case 5 - sar
4f08c3bdfSopenharmony_ci#
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ciexport TCID="cpuhotplug05"
7f08c3bdfSopenharmony_ciexport TST_TOTAL=1
8f08c3bdfSopenharmony_ciexport LC_TIME="POSIX"
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_ci# Includes:
11f08c3bdfSopenharmony_ci. test.sh
12f08c3bdfSopenharmony_ci. cpuhotplug_testsuite.sh
13f08c3bdfSopenharmony_ci. cpuhotplug_hotplug.sh
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_cicat <<EOF
16f08c3bdfSopenharmony_ciName:   $TCID
17f08c3bdfSopenharmony_ciDate:   `date`
18f08c3bdfSopenharmony_ciDesc:   Does sar behave properly during CPU hotplug events?
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ciEOF
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ciusage()
23f08c3bdfSopenharmony_ci{
24f08c3bdfSopenharmony_ci	cat << EOF
25f08c3bdfSopenharmony_ci	usage: $0 -c cpu -l loop -d directory
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci	OPTIONS
28f08c3bdfSopenharmony_ci		-c  cpu which is specified for testing
29f08c3bdfSopenharmony_ci		-l  number of cycle test
30f08c3bdfSopenharmony_ci		-d  directory used to lay file
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_ciEOF
33f08c3bdfSopenharmony_ci	exit 1
34f08c3bdfSopenharmony_ci}
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cido_clean()
37f08c3bdfSopenharmony_ci{
38f08c3bdfSopenharmony_ci	pid_is_valid ${SAR_PID} && kill_pid ${SAR_PID}
39f08c3bdfSopenharmony_ci	if ! online_cpu ${CPU_TO_TEST}; then
40f08c3bdfSopenharmony_ci		tst_brkm TBROK "CPU${CPU_TO_TEST} cannot be onlined"
41f08c3bdfSopenharmony_ci	fi
42f08c3bdfSopenharmony_ci}
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ciget_field()
45f08c3bdfSopenharmony_ci{
46f08c3bdfSopenharmony_ci	echo "$1" | awk "{print \$$2}"
47f08c3bdfSopenharmony_ci}
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ciwhile getopts c:l:d: OPTION; do
50f08c3bdfSopenharmony_ci	case $OPTION in
51f08c3bdfSopenharmony_ci	c)
52f08c3bdfSopenharmony_ci		CPU_TO_TEST=$OPTARG;;
53f08c3bdfSopenharmony_ci	l)
54f08c3bdfSopenharmony_ci		HOTPLUG05_LOOPS=$OPTARG;;
55f08c3bdfSopenharmony_ci	d)
56f08c3bdfSopenharmony_ci		TMP=$OPTARG;;
57f08c3bdfSopenharmony_ci	?)
58f08c3bdfSopenharmony_ci		usage;;
59f08c3bdfSopenharmony_ci	esac
60f08c3bdfSopenharmony_cidone
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ciLOOP_COUNT=1
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_citst_require_cmds sar
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_ciif [ $(get_present_cpus_num) -lt 2 ]; then
71f08c3bdfSopenharmony_ci	tst_brkm TCONF "system doesn't have required CPU hotplug support"
72f08c3bdfSopenharmony_cifi
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ciif [ -z "$CPU_TO_TEST" ]; then
75f08c3bdfSopenharmony_ci	tst_brkm TBROK "usage: ${0##*} <CPU to offline>"
76f08c3bdfSopenharmony_cifi
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci# Validate the specified CPU is available
79f08c3bdfSopenharmony_ciif ! cpu_is_valid "${CPU_TO_TEST}" ; then
80f08c3bdfSopenharmony_ci	tst_brkm TCONF "cpu${CPU_TO_TEST} doesn't support hotplug"
81f08c3bdfSopenharmony_cifi
82f08c3bdfSopenharmony_ci
83f08c3bdfSopenharmony_ci# Check that the specified CPU is offline; if not, offline it
84f08c3bdfSopenharmony_ciif cpu_is_online "${CPU_TO_TEST}" ; then
85f08c3bdfSopenharmony_ci	if ! offline_cpu ${CPU_TO_TEST} ; then
86f08c3bdfSopenharmony_ci		tst_brkm TBROK "CPU${CPU_TO_TEST} cannot be offlined"
87f08c3bdfSopenharmony_ci	fi
88f08c3bdfSopenharmony_cifi
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ciTST_CLEANUP=do_clean
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ciLOG_FILE="$TMP/log_$$"
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ciuntil [ $LOOP_COUNT -gt $HOTPLUG05_LOOPS ]; do
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	# Start up SAR and give it a couple cycles to run
97f08c3bdfSopenharmony_ci	sar 1 0 >/dev/null 2>&1 &
98f08c3bdfSopenharmony_ci	sleep 2
99f08c3bdfSopenharmony_ci	# "sar 1 0" is supported before 'sysstat-8.1.4(include sar)',
100f08c3bdfSopenharmony_ci	# after that use "sar 1" instead of. Use 'ps -C sar' to check.
101f08c3bdfSopenharmony_ci	if ps -C sar >/dev/null 2>&1; then
102f08c3bdfSopenharmony_ci		pkill sar
103f08c3bdfSopenharmony_ci		sar -P "$CPU_TO_TEST" 1 0 > "$LOG_FILE" &
104f08c3bdfSopenharmony_ci	else
105f08c3bdfSopenharmony_ci		sar -P "$CPU_TO_TEST" 1 > "$LOG_FILE" &
106f08c3bdfSopenharmony_ci	fi
107f08c3bdfSopenharmony_ci	sleep 2
108f08c3bdfSopenharmony_ci	SAR_PID=$!
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_ci	# Since the CPU is offline, SAR should display all the 6 fields
111f08c3bdfSopenharmony_ci	# of CPU statistics as '0.00'
112f08c3bdfSopenharmony_ci	offline_status=$(tail -n 1 "$LOG_FILE")
113f08c3bdfSopenharmony_ci	if [ -z "$offline_status" ]; then
114f08c3bdfSopenharmony_ci		tst_brkm TBROK "SAR output file is empty"
115f08c3bdfSopenharmony_ci	fi
116f08c3bdfSopenharmony_ci
117f08c3bdfSopenharmony_ci	cpu_field=$(get_field "$offline_status" "2")
118f08c3bdfSopenharmony_ci	if [ "${cpu_field}" = "CPU" ]; then
119f08c3bdfSopenharmony_ci		# Since sysstat-11.7.1, sar/sadf didn't display offline CPU
120f08c3bdfSopenharmony_ci		tst_resm TINFO "SAR didn't display offline CPU"
121f08c3bdfSopenharmony_ci	else
122f08c3bdfSopenharmony_ci		for i in $(seq 3 8); do
123f08c3bdfSopenharmony_ci			field=$(get_field "$offline_status" "$i")
124f08c3bdfSopenharmony_ci			if [ "$field" != "0.00" ]; then
125f08c3bdfSopenharmony_ci				tst_brkm TBROK "Field $i is '$field', '0.00' expected"
126f08c3bdfSopenharmony_ci			fi
127f08c3bdfSopenharmony_ci		done
128f08c3bdfSopenharmony_ci	fi
129f08c3bdfSopenharmony_ci
130f08c3bdfSopenharmony_ci	# Online the CPU
131f08c3bdfSopenharmony_ci	if ! online_cpu ${CPU_TO_TEST}; then
132f08c3bdfSopenharmony_ci		tst_brkm TBROK "CPU${CPU_TO_TEST} cannot be onlined"
133f08c3bdfSopenharmony_ci	fi
134f08c3bdfSopenharmony_ci
135f08c3bdfSopenharmony_ci	sleep 2
136f08c3bdfSopenharmony_ci
137f08c3bdfSopenharmony_ci	# Check that SAR registered the change in CPU online/offline states
138f08c3bdfSopenharmony_ci	online_status=$(tail -n 1 "$LOG_FILE")
139f08c3bdfSopenharmony_ci	check_passed=0
140f08c3bdfSopenharmony_ci	for i in $(seq 3 8); do
141f08c3bdfSopenharmony_ci		field_online=$(get_field "$online_status" "$i")
142f08c3bdfSopenharmony_ci
143f08c3bdfSopenharmony_ci		if [ "$field_online" != "0.00" ]; then
144f08c3bdfSopenharmony_ci			check_passed=1
145f08c3bdfSopenharmony_ci			break
146f08c3bdfSopenharmony_ci		fi
147f08c3bdfSopenharmony_ci	done
148f08c3bdfSopenharmony_ci
149f08c3bdfSopenharmony_ci	if [ $check_passed -eq 0 ]; then
150f08c3bdfSopenharmony_ci		tst_resm TFAIL "No change in the CPU statistics"
151f08c3bdfSopenharmony_ci		tst_exit
152f08c3bdfSopenharmony_ci	fi
153f08c3bdfSopenharmony_ci
154f08c3bdfSopenharmony_ci	if ! offline_cpu ${CPU_TO_TEST}; then
155f08c3bdfSopenharmony_ci		tst_brkm TBROK "CPU${CPU_TO_TEST} cannot be offlined"
156f08c3bdfSopenharmony_ci	fi
157f08c3bdfSopenharmony_ci
158f08c3bdfSopenharmony_ci	kill_pid ${SAR_PID}
159f08c3bdfSopenharmony_ci
160f08c3bdfSopenharmony_ci	LOOP_COUNT=$((LOOP_COUNT+1))
161f08c3bdfSopenharmony_ci
162f08c3bdfSopenharmony_cidone
163f08c3bdfSopenharmony_ci
164f08c3bdfSopenharmony_citst_resm TPASS "SAR updated statistics after the CPU was turned on."
165f08c3bdfSopenharmony_ci
166f08c3bdfSopenharmony_citst_exit
167