1#!/bin/bash
2
3TMPDIR=/tmp
4
5PASS=0
6FAIL=1
7NOSUPPORT=2
8MISSING_FILE=3
9UNTESTED=4
10YES=0
11
12cleanup() {
13	if [ -f ${1} ] ; then
14		rm -f ${1}
15	fi
16}
17
18check_kervel_arch() {
19	# Checking required kernel version and architecture
20	tst_kvercmp 2 6 21; rc=$?
21	if [ $rc -ne 1 -a $rc -ne 2 ] ; then
22		tst_brkm TCONF "Kernel version not supported; not " \
23			"running testcases"
24	else
25		case "$(uname -m)" in
26		i[4-6]86|x86_64)
27			;;
28		*)
29			tst_brkm TCONF "Arch not supported; not running " \
30				"testcases"
31			;;
32		esac
33	fi
34}
35
36check_config_options() {
37	if ( ! ${3} "${1}" ${2} | grep -v "#" > /dev/null ) ; then
38		tst_brkm TCONF "NOSUPPORT: current system dosen't support ${1}"
39	fi
40}
41
42get_topology() {
43	declare -a cpus
44	declare -a phyid
45
46	total_cpus=`tst_ncpus`
47	(( total_cpus-=1 ))
48	for cpu in $(seq 0 "${total_cpus}" )
49	do
50		cpus[$cpu]=cpu${cpu}
51		phyid[$cpu]=$(cat \
52		/sys/devices/system/cpu/cpu${cpu}/topology/physical_package_id)
53	done
54	j=0
55	while [ "${j}" -lt "${total_cpus}" ]
56	do
57		(( k = $j + 1 ))
58		if [ ${phyid[$j]} -eq ${phyid[$k]} ] ; then
59			echo "${cpus[$j]} -P ${cpus[$k]}" | sed -e "s/cpu//g"
60		fi
61		(( j+=1 ))
62	done
63}
64
65check_cpufreq() {
66	total_cpus=`tst_ncpus`
67	(( total_cpus-=1 ))
68
69	for cpu in $(seq 0 "${total_cpus}" )
70	do
71		if [ ! -d /sys/devices/system/cpu/cpu${cpu}/cpufreq ] ; then
72			tst_brkm TCONF "NOSUPPORT: cpufreq support not " \
73				"found please check Kernel configuration " \
74				"or BIOS settings"
75		fi
76	done
77}
78
79get_supporting_freq() {
80	cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_available_frequencies \
81		| uniq
82}
83
84get_supporting_govr() {
85	cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_available_governors \
86		| uniq
87}
88
89is_hyper_threaded() {
90	siblings=`grep siblings /proc/cpuinfo | uniq | cut -f2 -d':'`
91	cpu_cores=`grep "cpu cores" /proc/cpuinfo | uniq | cut -f2 -d':'`
92	[ $siblings -gt $cpu_cores ]; echo $?
93}
94
95check_input() {
96	validity_check=${2:-valid}
97	testfile=$3
98	if [ "${validity_check}" = "invalid" ] ; then
99		PASS="Testcase FAIL - Able to execute"
100		FAIL="Testcase PASS - Unable to execute"
101	else
102		PASS="Testcase PASS"
103		FAIL="Testcase FAIL"
104	fi
105	RC=0
106	for input in ${1}
107	do
108		echo ${input} > ${test_file} 2>/dev/null
109		return_value=$?
110		output=$(cat ${test_file})
111		if [ "${return_value}" = "0" -a "${input}" = "${output}" ] ; then
112			echo "${0}: ${PASS}: echo ${input} > ${test_file}"
113			if [ "${validity_check}" = "invalid" ] ; then
114				RC=1
115			fi
116		else
117			echo "${0}: ${FAIL}: echo ${input} > ${test_file}"
118			if [ "${validity_check}" = "valid" ] ; then
119				RC=1
120			fi
121		fi
122	done
123	return $RC
124}
125
126is_multi_socket() {
127	no_of_sockets=`cat \
128		/sys/devices/system/cpu/cpu?/topology/physical_package_id \
129		| uniq | wc -l`
130	[ $no_of_sockets -gt 1 ] ; echo $?
131}
132
133is_multi_core() {
134	siblings=`grep siblings /proc/cpuinfo | uniq | cut -f2 -d':'`
135	cpu_cores=`grep "cpu cores" /proc/cpuinfo | uniq | cut -f2 -d':'`
136	if [ $siblings -eq $cpu_cores ]; then
137		[ $cpu_cores -gt 1 ]; echo $?
138	else
139		: $(( num_of_cpus = siblings / cpu_cores ))
140		[ $num_of_cpus -gt 1 ]; echo $?
141	fi
142}
143
144is_dual_core() {
145	siblings=`grep siblings /proc/cpuinfo | uniq | cut -f2 -d':'`
146	cpu_cores=`grep "cpu cores" /proc/cpuinfo | uniq \
147			| cut -f2 -d':'`
148        if [ $siblings -eq $cpu_cores ]; then
149                [ $cpu_cores -eq 2 ]; echo $?
150        else
151                : $(( num_of_cpus = siblings / cpu_cores ))
152                [ $num_of_cpus -eq 2 ]; echo $?
153        fi
154}
155
156get_kernel_version() {
157	# Get kernel minor version
158	export kernel_version=`uname -r | awk -F. '{print $1"."$2"."$3}' \
159		| cut -f1 -d'-'`
160}
161
162get_valid_input() {
163	kernel_version=$1
164	case "$kernel_version" in
165	'2.6.26' | '2.6.27' | '2.6.28')
166			export valid_input="0 1" ;;
167		*) export valid_input="0 1 2" ;;
168	esac
169}
170
171analyze_result_hyperthreaded() {
172	sched_mc=$1
173    pass_count=$2
174    sched_smt=$3
175	PASS="Test PASS"
176	FAIL="Test FAIL"
177
178	RC=0
179	case "$sched_mc" in
180	0)
181		case "$sched_smt" in
182		0)
183			if [ $pass_count -lt 5 ]; then
184				echo "${PASS}: cpu consolidation failed for" \
185					"sched_mc=$sched_mc & sched_smt=$sched_smt"
186			else
187				RC=1
188				echo "${FAIL}: cpu consolidation passed for" \
189					"sched_mc=$sched_mc & sched_smt=$sched_smt"
190			fi
191			;;
192		*)
193			if [ $pass_count -lt 5 ]; then
194				RC=1
195				echo "${FAIL}: cpu consolidation for" \
196					"sched_mc=$sched_mc & sched_smt=$sched_smt"
197			else
198				echo "${PASS}: cpu consolidation for" \
199					"sched_mc=$sched_mc & sched_smt=$sched_smt"
200			fi
201			;;
202		esac ;;
203	*)
204		if [ $pass_count -lt 5 ]; then
205			RC=1
206			echo "${FAIL}: cpu consolidation for" \
207				"sched_mc=$sched_mc & sched_smt=$sched_smt"
208		else
209			echo "${PASS}: cpu consolidation for" \
210				"sched_mc=$sched_mc & sched_smt=$sched_smt"
211		fi
212		;;
213	esac
214	return $RC
215}
216
217analyze_package_consolidation_result() {
218	sched_mc=$1
219    pass_count=$2
220
221	if [ $# -gt 2 ]
222	then
223		sched_smt=$3
224	else
225		sched_smt=-1
226	fi
227
228	PASS="Test PASS"
229	FAIL="Test FAIL"
230
231	RC=0
232	if [ $hyper_threaded -eq $YES -a $sched_smt -gt -1 ]; then
233		analyze_result_hyperthreaded $sched_mc $pass_count $sched_smt
234	else
235		case "$sched_mc" in
236	    0)
237    	    if [ $pass_count -lt 5 ]; then
238				echo "${PASS}: cpu consolidation failed for" \
239					"sched_mc=$sched_mc"
240        	else
241				RC=1
242				echo "${FAIL}: cpu consolidation passed for" \
243					"sched_mc=$sched_mc"
244			fi
245			;;
246    	*)
247			if [ $pass_count -lt 5 ]; then
248				RC=1
249				echo "${FAIL}: consolidation at package level" \
250					"failed for sched_mc=$sched_mc"
251			else
252				echo "${PASS}: consolidation at package level" \
253					"passed for sched_mc=$sched_mc"
254			fi
255        	;;
256    	esac
257	fi
258	return $RC
259}
260
261analyze_core_consolidation_result() {
262	sched_smt=$1
263	pass_count=$2
264	PASS="Test PASS"
265	FAIL="Test FAIL"
266
267	RC=0
268	case "$sched_smt" in
269	0)
270		if [ $pass_count -lt 5 ]; then
271			echo "${PASS}: consolidation at core level failed" \
272				"when sched_smt=$sched_smt"
273		else
274			RC=1
275			echo "${FAIL}: consolidation at core level passed for" \
276				"sched_smt=$sched_smt"
277		fi ;;
278	*)
279		if [ $pass_count -lt 5 ]; then
280			RC=1
281			echo "${FAIL}: consolidation at core level failed for" \
282				"sched_smt=$sched_smt"
283		else
284			echo "${PASS}: consolidation at core level passed for" \
285				"sched_smt=$sched_smt"
286		fi ;;
287	esac
288	return $RC
289}
290
291analyze_sched_domain_result(){
292	sched_mc=$1
293	result=$2
294	sched_smt=$3
295	PASS="Test PASS"
296	FAIL="Test FAIL"
297
298	RC=0
299	if [ $hyper_threaded -eq $YES ]; then
300		if [ $sched_smt ]; then
301			if [ "$result" = 0 ];then
302				echo "${PASS}: sched domain test for" \
303					"sched_mc=$sched_mc & sched_smt=$sched_smt"
304			else
305				RC=1
306				echo "${FAIL}: sched domain test for" \
307					"sched_mc=$sched_mc & sched_smt=$sched_smt"
308			fi
309		else
310			if [ "$result" = 0 ];then
311				echo "${PASS}: sched domain test for" \
312					"sched_mc=$sched_mc"
313			else
314				RC=1
315				echo "${FAIL}: sched domain test for" \
316					"sched_mc=$sched_mc"
317			fi
318		fi
319	else
320		if [ "$result" = 0 ];then
321			echo "${PASS}: sched domain test for sched_mc=$sched_mc"
322		else
323			RC=1
324			echo "${FAIL}: sched domain test for sched_mc=$sched_mc"
325		fi
326	fi
327	return $RC
328}
329