xref: /aosp_15_r20/external/ltp/testcases/kernel/hotplug/cpu_hotplug/functional/cpuhotplug05.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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	if ! online_cpu ${CPU_TO_TEST}; then
40		tst_brkm TBROK "CPU${CPU_TO_TEST} cannot be onlined"
41	fi
42}
43
44get_field()
45{
46	echo "$1" | awk "{print \$$2}"
47}
48
49while getopts c:l:d: OPTION; do
50	case $OPTION in
51	c)
52		CPU_TO_TEST=$OPTARG;;
53	l)
54		HOTPLUG05_LOOPS=$OPTARG;;
55	d)
56		TMP=$OPTARG;;
57	?)
58		usage;;
59	esac
60done
61
62LOOP_COUNT=1
63
64tst_require_cmds sar
65
66if tst_virt_hyperv; then
67	tst_brkm TCONF "Microsoft Hyper-V detected, no support for CPU hotplug"
68fi
69
70if [ $(get_present_cpus_num) -lt 2 ]; then
71	tst_brkm TCONF "system doesn't have required CPU hotplug support"
72fi
73
74if [ -z "$CPU_TO_TEST" ]; then
75	tst_brkm TBROK "usage: ${0##*} <CPU to offline>"
76fi
77
78# Validate the specified CPU is available
79if ! cpu_is_valid "${CPU_TO_TEST}" ; then
80	tst_brkm TCONF "cpu${CPU_TO_TEST} doesn't support hotplug"
81fi
82
83# Check that the specified CPU is offline; if not, offline it
84if cpu_is_online "${CPU_TO_TEST}" ; then
85	if ! offline_cpu ${CPU_TO_TEST} ; then
86		tst_brkm TBROK "CPU${CPU_TO_TEST} cannot be offlined"
87	fi
88fi
89
90TST_CLEANUP=do_clean
91
92LOG_FILE="$TMP/log_$$"
93
94until [ $LOOP_COUNT -gt $HOTPLUG05_LOOPS ]; do
95
96	# Start up SAR and give it a couple cycles to run
97	sar 1 0 >/dev/null 2>&1 &
98	sleep 2
99	# "sar 1 0" is supported before 'sysstat-8.1.4(include sar)',
100	# after that use "sar 1" instead of. Use 'ps -C sar' to check.
101	if ps -C sar >/dev/null 2>&1; then
102		pkill sar
103		sar -P "$CPU_TO_TEST" 1 0 > "$LOG_FILE" &
104	else
105		sar -P "$CPU_TO_TEST" 1 > "$LOG_FILE" &
106	fi
107	sleep 2
108	SAR_PID=$!
109
110	# Since the CPU is offline, SAR should display all the 6 fields
111	# of CPU statistics as '0.00'
112	offline_status=$(tail -n 1 "$LOG_FILE")
113	if [ -z "$offline_status" ]; then
114		tst_brkm TBROK "SAR output file is empty"
115	fi
116
117	cpu_field=$(get_field "$offline_status" "2")
118	if [ "${cpu_field}" = "CPU" ]; then
119		# Since sysstat-11.7.1, sar/sadf didn't display offline CPU
120		tst_resm TINFO "SAR didn't display offline CPU"
121	else
122		for i in $(seq 3 8); do
123			field=$(get_field "$offline_status" "$i")
124			if [ "$field" != "0.00" ]; then
125				tst_brkm TBROK "Field $i is '$field', '0.00' expected"
126			fi
127		done
128	fi
129
130	# Online the CPU
131	if ! online_cpu ${CPU_TO_TEST}; then
132		tst_brkm TBROK "CPU${CPU_TO_TEST} cannot be onlined"
133	fi
134
135	sleep 2
136
137	# Check that SAR registered the change in CPU online/offline states
138	online_status=$(tail -n 1 "$LOG_FILE")
139	check_passed=0
140	for i in $(seq 3 8); do
141		field_online=$(get_field "$online_status" "$i")
142
143		if [ "$field_online" != "0.00" ]; then
144			check_passed=1
145			break
146		fi
147	done
148
149	if [ $check_passed -eq 0 ]; then
150		tst_resm TFAIL "No change in the CPU statistics"
151		tst_exit
152	fi
153
154	if ! offline_cpu ${CPU_TO_TEST}; then
155		tst_brkm TBROK "CPU${CPU_TO_TEST} cannot be offlined"
156	fi
157
158	kill_pid ${SAR_PID}
159
160	LOOP_COUNT=$((LOOP_COUNT+1))
161
162done
163
164tst_resm TPASS "SAR updated statistics after the CPU was turned on."
165
166tst_exit
167