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