xref: /aosp_15_r20/external/ltp/testcases/kernel/numa/numa01.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker#!/bin/sh
2*49cdfc7eSAndroid Build Coastguard Worker# SPDX-License-Identifier: GPL-2.0-or-later
3*49cdfc7eSAndroid Build Coastguard Worker# Copyright (c) International Business Machines Corp., 2007
4*49cdfc7eSAndroid Build Coastguard Worker# Copyright (c) Linux Test Project, 2016-2020
5*49cdfc7eSAndroid Build Coastguard Worker# Author: Sivakumar Chinnaiah <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker#
7*49cdfc7eSAndroid Build Coastguard Worker# Test Basic functionality of numactl command.
8*49cdfc7eSAndroid Build Coastguard Worker# Test #1: Verifies cpunodebind and membind
9*49cdfc7eSAndroid Build Coastguard Worker# Test #2: Verifies preferred node bind for memory allocation
10*49cdfc7eSAndroid Build Coastguard Worker# Test #3: Verifies memory interleave on all nodes
11*49cdfc7eSAndroid Build Coastguard Worker# Test #4: Verifies physcpubind
12*49cdfc7eSAndroid Build Coastguard Worker# Test #5: Verifies localalloc
13*49cdfc7eSAndroid Build Coastguard Worker# Test #6: Verifies memhog
14*49cdfc7eSAndroid Build Coastguard Worker# Test #7: Verifies numa_node_size api
15*49cdfc7eSAndroid Build Coastguard Worker# Test #8: Verifies hugepage alloacted on specified node
16*49cdfc7eSAndroid Build Coastguard Worker# Test #9: Verifies THP memory allocated on preferred node
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard WorkerTST_CNT=9
19*49cdfc7eSAndroid Build Coastguard WorkerTST_SETUP=setup
20*49cdfc7eSAndroid Build Coastguard WorkerTST_TESTFUNC=test
21*49cdfc7eSAndroid Build Coastguard WorkerTST_NEEDS_TMPDIR=1
22*49cdfc7eSAndroid Build Coastguard WorkerTST_NEEDS_ROOT=1
23*49cdfc7eSAndroid Build Coastguard WorkerTST_NEEDS_CMDS="awk bc numactl numastat"
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker# Awk the field matching the node value for numastat
26*49cdfc7eSAndroid Build Coastguard Worker# $1 - Pid number
27*49cdfc7eSAndroid Build Coastguard Worker# $2 - Node number
28*49cdfc7eSAndroid Build Coastguard Workerget_node_index()
29*49cdfc7eSAndroid Build Coastguard Worker{
30*49cdfc7eSAndroid Build Coastguard Worker       local pid=$1
31*49cdfc7eSAndroid Build Coastguard Worker       local nid="Node $2"
32*49cdfc7eSAndroid Build Coastguard Worker       echo $(numastat -p $pid | sed '3q;d' | awk -F '[[:space:]][[:space:]]+' \
33*49cdfc7eSAndroid Build Coastguard Worker               -v node="$nid" '{ for (i = 1; i <= NF; ++i) if($i==node) print i; exit }')
34*49cdfc7eSAndroid Build Coastguard Worker}
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker# Convert the value of given NUMA node from the `numastat -p` output,
37*49cdfc7eSAndroid Build Coastguard Worker# multiply by size.
38*49cdfc7eSAndroid Build Coastguard Worker# $1 - Pid number
39*49cdfc7eSAndroid Build Coastguard Worker# $2 - Node number
40*49cdfc7eSAndroid Build Coastguard Worker# $3 - Size for multiplication (e.g. 1024, $MB)
41*49cdfc7eSAndroid Build Coastguard Workerget_mem_cur()
42*49cdfc7eSAndroid Build Coastguard Worker{
43*49cdfc7eSAndroid Build Coastguard Worker	local pid=$1
44*49cdfc7eSAndroid Build Coastguard Worker	local index=$(echo "$(get_node_index $pid $2)")
45*49cdfc7eSAndroid Build Coastguard Worker	local size=$3
46*49cdfc7eSAndroid Build Coastguard Worker	local numstat=$(numastat -p $pid |awk '/^Total/ {print $'$index'}')
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker	if [ -z "$numstat" ]; then
49*49cdfc7eSAndroid Build Coastguard Worker		echo 0
50*49cdfc7eSAndroid Build Coastguard Worker		return
51*49cdfc7eSAndroid Build Coastguard Worker	fi
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker	echo $(echo "$numstat * $size" | bc)
54*49cdfc7eSAndroid Build Coastguard Worker}
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Workercheck_for_support_numa()
57*49cdfc7eSAndroid Build Coastguard Worker{
58*49cdfc7eSAndroid Build Coastguard Worker	local pid=$1
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker	local state=$(awk '{print $3}' /proc/$pid/stat)
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Worker	if [ $state = 'T' ]; then
63*49cdfc7eSAndroid Build Coastguard Worker		return 0
64*49cdfc7eSAndroid Build Coastguard Worker	fi
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker	return 1
67*49cdfc7eSAndroid Build Coastguard Worker}
68*49cdfc7eSAndroid Build Coastguard Worker
69*49cdfc7eSAndroid Build Coastguard Workersetup()
70*49cdfc7eSAndroid Build Coastguard Worker{
71*49cdfc7eSAndroid Build Coastguard Worker	export MB=$((1024*1024))
72*49cdfc7eSAndroid Build Coastguard Worker	export PAGE_SIZE=$(tst_getconf PAGESIZE)
73*49cdfc7eSAndroid Build Coastguard Worker	export HPAGE_SIZE=$(awk '/Hugepagesize:/ {print $2}' /proc/meminfo)
74*49cdfc7eSAndroid Build Coastguard Worker
75*49cdfc7eSAndroid Build Coastguard Worker	total_nodes=0
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker	nodes_list=$(numactl --show | grep nodebind | cut -d ':' -f 2)
78*49cdfc7eSAndroid Build Coastguard Worker	for node in $nodes_list; do
79*49cdfc7eSAndroid Build Coastguard Worker		total_nodes=$((total_nodes+1))
80*49cdfc7eSAndroid Build Coastguard Worker	done
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker	tst_res TINFO "The system contains $total_nodes nodes: $nodes_list"
83*49cdfc7eSAndroid Build Coastguard Worker
84*49cdfc7eSAndroid Build Coastguard Worker	if [ $total_nodes -le 1 ]; then
85*49cdfc7eSAndroid Build Coastguard Worker		tst_brk TCONF "SUT does not support NUMA policy or not a NUMA machine"
86*49cdfc7eSAndroid Build Coastguard Worker	fi
87*49cdfc7eSAndroid Build Coastguard Worker}
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker# Verification of memory allocated on a node
90*49cdfc7eSAndroid Build Coastguard Workertest1()
91*49cdfc7eSAndroid Build Coastguard Worker{
92*49cdfc7eSAndroid Build Coastguard Worker	local mem_curr
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker	for node in $nodes_list; do
95*49cdfc7eSAndroid Build Coastguard Worker		numactl --cpunodebind=$node --membind=$node support_numa alloc_1MB &
96*49cdfc7eSAndroid Build Coastguard Worker		pid=$!
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker		TST_RETRY_FUNC "check_for_support_numa $pid" 0
99*49cdfc7eSAndroid Build Coastguard Worker
100*49cdfc7eSAndroid Build Coastguard Worker		mem_curr=$(get_mem_cur $pid $node $MB)
101*49cdfc7eSAndroid Build Coastguard Worker		if [ $(echo "$mem_curr < $MB" | bc) -eq 1 ]; then
102*49cdfc7eSAndroid Build Coastguard Worker			tst_res TFAIL \
103*49cdfc7eSAndroid Build Coastguard Worker				"NUMA memory allocated in node$node is less than expected"
104*49cdfc7eSAndroid Build Coastguard Worker			kill -CONT $pid >/dev/null 2>&1
105*49cdfc7eSAndroid Build Coastguard Worker			return
106*49cdfc7eSAndroid Build Coastguard Worker		fi
107*49cdfc7eSAndroid Build Coastguard Worker
108*49cdfc7eSAndroid Build Coastguard Worker		kill -CONT $pid >/dev/null 2>&1
109*49cdfc7eSAndroid Build Coastguard Worker	done
110*49cdfc7eSAndroid Build Coastguard Worker
111*49cdfc7eSAndroid Build Coastguard Worker	tst_res TPASS "NUMA local node and memory affinity"
112*49cdfc7eSAndroid Build Coastguard Worker}
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker# Verification of memory allocated on preferred node
115*49cdfc7eSAndroid Build Coastguard Workertest2()
116*49cdfc7eSAndroid Build Coastguard Worker{
117*49cdfc7eSAndroid Build Coastguard Worker	local mem_curr
118*49cdfc7eSAndroid Build Coastguard Worker	local cnt=1
119*49cdfc7eSAndroid Build Coastguard Worker
120*49cdfc7eSAndroid Build Coastguard Worker	for node in $nodes_list; do
121*49cdfc7eSAndroid Build Coastguard Worker
122*49cdfc7eSAndroid Build Coastguard Worker		if [ $cnt -eq $total_nodes ]; then   #wrap up for last node
123*49cdfc7eSAndroid Build Coastguard Worker			Preferred_node=$(echo $nodes_list | cut -d ' ' -f 1)
124*49cdfc7eSAndroid Build Coastguard Worker		else
125*49cdfc7eSAndroid Build Coastguard Worker			# always next node is preferred node
126*49cdfc7eSAndroid Build Coastguard Worker			Preferred_node=$(echo $nodes_list | cut -d ' ' -f $((cnt+1)))
127*49cdfc7eSAndroid Build Coastguard Worker		fi
128*49cdfc7eSAndroid Build Coastguard Worker
129*49cdfc7eSAndroid Build Coastguard Worker		numactl --cpunodebind=$node --preferred=$Preferred_node support_numa alloc_1MB &
130*49cdfc7eSAndroid Build Coastguard Worker		pid=$!
131*49cdfc7eSAndroid Build Coastguard Worker
132*49cdfc7eSAndroid Build Coastguard Worker		TST_RETRY_FUNC "check_for_support_numa $pid" 0
133*49cdfc7eSAndroid Build Coastguard Worker
134*49cdfc7eSAndroid Build Coastguard Worker		mem_curr=$(get_mem_cur $pid $Preferred_node $MB)
135*49cdfc7eSAndroid Build Coastguard Worker		if [ $(echo "$mem_curr < $MB" |bc ) -eq 1 ]; then
136*49cdfc7eSAndroid Build Coastguard Worker			tst_res TFAIL \
137*49cdfc7eSAndroid Build Coastguard Worker				"NUMA memory allocated in node$Preferred_node is less than expected"
138*49cdfc7eSAndroid Build Coastguard Worker			kill -CONT $pid >/dev/null 2>&1
139*49cdfc7eSAndroid Build Coastguard Worker			return
140*49cdfc7eSAndroid Build Coastguard Worker		fi
141*49cdfc7eSAndroid Build Coastguard Worker
142*49cdfc7eSAndroid Build Coastguard Worker		cnt=$((cnt+1))
143*49cdfc7eSAndroid Build Coastguard Worker		kill -CONT $pid >/dev/null 2>&1
144*49cdfc7eSAndroid Build Coastguard Worker	done
145*49cdfc7eSAndroid Build Coastguard Worker
146*49cdfc7eSAndroid Build Coastguard Worker	tst_res TPASS "NUMA preferred node policy"
147*49cdfc7eSAndroid Build Coastguard Worker}
148*49cdfc7eSAndroid Build Coastguard Worker
149*49cdfc7eSAndroid Build Coastguard Worker# Verification of memory interleaved on all nodes
150*49cdfc7eSAndroid Build Coastguard Workertest3()
151*49cdfc7eSAndroid Build Coastguard Worker{
152*49cdfc7eSAndroid Build Coastguard Worker	local mem_curr
153*49cdfc7eSAndroid Build Coastguard Worker	# Memory will be allocated using round robin on nodes.
154*49cdfc7eSAndroid Build Coastguard Worker	Exp_incr=$(echo "$MB / $total_nodes" |bc)
155*49cdfc7eSAndroid Build Coastguard Worker
156*49cdfc7eSAndroid Build Coastguard Worker	numactl --interleave=all support_numa alloc_1MB &
157*49cdfc7eSAndroid Build Coastguard Worker	pid=$!
158*49cdfc7eSAndroid Build Coastguard Worker
159*49cdfc7eSAndroid Build Coastguard Worker	TST_RETRY_FUNC "check_for_support_numa $pid" 0
160*49cdfc7eSAndroid Build Coastguard Worker
161*49cdfc7eSAndroid Build Coastguard Worker	for node in $nodes_list; do
162*49cdfc7eSAndroid Build Coastguard Worker		mem_curr=$(get_mem_cur $pid $node $MB)
163*49cdfc7eSAndroid Build Coastguard Worker
164*49cdfc7eSAndroid Build Coastguard Worker		if [ $(echo "$mem_curr < $Exp_incr" |bc ) -eq 1 ]; then
165*49cdfc7eSAndroid Build Coastguard Worker			tst_res TFAIL \
166*49cdfc7eSAndroid Build Coastguard Worker				"NUMA interleave memory allocated in node$node is less than expected"
167*49cdfc7eSAndroid Build Coastguard Worker			kill -CONT $pid >/dev/null 2>&1
168*49cdfc7eSAndroid Build Coastguard Worker			return
169*49cdfc7eSAndroid Build Coastguard Worker		fi
170*49cdfc7eSAndroid Build Coastguard Worker	done
171*49cdfc7eSAndroid Build Coastguard Worker
172*49cdfc7eSAndroid Build Coastguard Worker	kill -CONT $pid >/dev/null 2>&1
173*49cdfc7eSAndroid Build Coastguard Worker	tst_res TPASS "NUMA interleave policy"
174*49cdfc7eSAndroid Build Coastguard Worker}
175*49cdfc7eSAndroid Build Coastguard Worker
176*49cdfc7eSAndroid Build Coastguard Worker# Verification of physical cpu bind
177*49cdfc7eSAndroid Build Coastguard Workertest4()
178*49cdfc7eSAndroid Build Coastguard Worker{
179*49cdfc7eSAndroid Build Coastguard Worker	no_of_cpus=0	#no. of cpu's exist
180*49cdfc7eSAndroid Build Coastguard Worker	run_on_cpu=0
181*49cdfc7eSAndroid Build Coastguard Worker	running_on_cpu=0
182*49cdfc7eSAndroid Build Coastguard Worker
183*49cdfc7eSAndroid Build Coastguard Worker	no_of_cpus=$(tst_ncpus)
184*49cdfc7eSAndroid Build Coastguard Worker	# not sure whether cpu's can't be in odd number
185*49cdfc7eSAndroid Build Coastguard Worker	run_on_cpu=$(($((no_of_cpus+1))/2))
186*49cdfc7eSAndroid Build Coastguard Worker	numactl --all --physcpubind=$run_on_cpu support_numa pause & #just waits for sigint
187*49cdfc7eSAndroid Build Coastguard Worker	pid=$!
188*49cdfc7eSAndroid Build Coastguard Worker	var=`awk '{ print $2 }' /proc/$pid/stat`
189*49cdfc7eSAndroid Build Coastguard Worker	while [ $var = '(numactl)' ]; do
190*49cdfc7eSAndroid Build Coastguard Worker		var=`awk '{ print $2 }' /proc/$pid/stat`
191*49cdfc7eSAndroid Build Coastguard Worker		tst_sleep 100ms
192*49cdfc7eSAndroid Build Coastguard Worker	done
193*49cdfc7eSAndroid Build Coastguard Worker	# Warning !! 39 represents cpu number, on which process pid is currently running and
194*49cdfc7eSAndroid Build Coastguard Worker	# this may change if Some more fields are added in the middle, may be in future
195*49cdfc7eSAndroid Build Coastguard Worker	running_on_cpu=$(awk '{ print $39; }' /proc/$pid/stat)
196*49cdfc7eSAndroid Build Coastguard Worker	if [ $running_on_cpu -ne $run_on_cpu ]; then
197*49cdfc7eSAndroid Build Coastguard Worker		tst_res TFAIL \
198*49cdfc7eSAndroid Build Coastguard Worker			"Process running on cpu$running_on_cpu but expected to run on cpu$run_on_cpu"
199*49cdfc7eSAndroid Build Coastguard Worker		ROD kill -INT $pid
200*49cdfc7eSAndroid Build Coastguard Worker		return
201*49cdfc7eSAndroid Build Coastguard Worker	fi
202*49cdfc7eSAndroid Build Coastguard Worker
203*49cdfc7eSAndroid Build Coastguard Worker	ROD kill -INT $pid
204*49cdfc7eSAndroid Build Coastguard Worker
205*49cdfc7eSAndroid Build Coastguard Worker	tst_res TPASS "NUMA phycpubind policy"
206*49cdfc7eSAndroid Build Coastguard Worker}
207*49cdfc7eSAndroid Build Coastguard Worker
208*49cdfc7eSAndroid Build Coastguard Worker# Verification of local node allocation
209*49cdfc7eSAndroid Build Coastguard Workertest5()
210*49cdfc7eSAndroid Build Coastguard Worker{
211*49cdfc7eSAndroid Build Coastguard Worker	local mem_curr
212*49cdfc7eSAndroid Build Coastguard Worker
213*49cdfc7eSAndroid Build Coastguard Worker	for node in $nodes_list; do
214*49cdfc7eSAndroid Build Coastguard Worker		numactl --cpunodebind=$node --localalloc support_numa alloc_1MB &
215*49cdfc7eSAndroid Build Coastguard Worker		pid=$!
216*49cdfc7eSAndroid Build Coastguard Worker
217*49cdfc7eSAndroid Build Coastguard Worker		TST_RETRY_FUNC "check_for_support_numa $pid" 0
218*49cdfc7eSAndroid Build Coastguard Worker
219*49cdfc7eSAndroid Build Coastguard Worker		mem_curr=$(get_mem_cur $pid $node $MB)
220*49cdfc7eSAndroid Build Coastguard Worker		if [ $(echo "$mem_curr < $MB" |bc ) -eq 1 ]; then
221*49cdfc7eSAndroid Build Coastguard Worker			tst_res TFAIL \
222*49cdfc7eSAndroid Build Coastguard Worker				"NUMA localnode memory allocated in node$node is less than expected"
223*49cdfc7eSAndroid Build Coastguard Worker			kill -CONT $pid >/dev/null 2>&1
224*49cdfc7eSAndroid Build Coastguard Worker			return
225*49cdfc7eSAndroid Build Coastguard Worker		fi
226*49cdfc7eSAndroid Build Coastguard Worker
227*49cdfc7eSAndroid Build Coastguard Worker		kill -CONT $pid >/dev/null 2>&1
228*49cdfc7eSAndroid Build Coastguard Worker	done
229*49cdfc7eSAndroid Build Coastguard Worker
230*49cdfc7eSAndroid Build Coastguard Worker	tst_res TPASS "NUMA local node allocation"
231*49cdfc7eSAndroid Build Coastguard Worker}
232*49cdfc7eSAndroid Build Coastguard Worker
233*49cdfc7eSAndroid Build Coastguard Workercheck_ltp_numa_test8_log()
234*49cdfc7eSAndroid Build Coastguard Worker{
235*49cdfc7eSAndroid Build Coastguard Worker	grep -m1 -q '.' ltp_numa_test8.log
236*49cdfc7eSAndroid Build Coastguard Worker}
237*49cdfc7eSAndroid Build Coastguard Worker
238*49cdfc7eSAndroid Build Coastguard Worker# Verification of memhog with interleave policy
239*49cdfc7eSAndroid Build Coastguard Workertest6()
240*49cdfc7eSAndroid Build Coastguard Worker{
241*49cdfc7eSAndroid Build Coastguard Worker	local mem_curr
242*49cdfc7eSAndroid Build Coastguard Worker	# Memory will be allocated using round robin on nodes.
243*49cdfc7eSAndroid Build Coastguard Worker	Exp_incr=$(echo "$MB / $total_nodes" |bc)
244*49cdfc7eSAndroid Build Coastguard Worker
245*49cdfc7eSAndroid Build Coastguard Worker	numactl --interleave=all memhog -r1000000 1MB >ltp_numa_test8.log 2>&1 &
246*49cdfc7eSAndroid Build Coastguard Worker	pid=$!
247*49cdfc7eSAndroid Build Coastguard Worker
248*49cdfc7eSAndroid Build Coastguard Worker	TST_RETRY_FUNC "check_ltp_numa_test8_log" 0
249*49cdfc7eSAndroid Build Coastguard Worker
250*49cdfc7eSAndroid Build Coastguard Worker	for node in $nodes_list; do
251*49cdfc7eSAndroid Build Coastguard Worker		mem_curr=$(get_mem_cur $pid $node $MB)
252*49cdfc7eSAndroid Build Coastguard Worker
253*49cdfc7eSAndroid Build Coastguard Worker		if [ $(echo "$mem_curr < $Exp_incr" |bc ) -eq 1 ]; then
254*49cdfc7eSAndroid Build Coastguard Worker			tst_res TFAIL \
255*49cdfc7eSAndroid Build Coastguard Worker				"NUMA interleave memhog in node$node is less than expected"
256*49cdfc7eSAndroid Build Coastguard Worker			kill -KILL $pid >/dev/null 2>&1
257*49cdfc7eSAndroid Build Coastguard Worker			return
258*49cdfc7eSAndroid Build Coastguard Worker		fi
259*49cdfc7eSAndroid Build Coastguard Worker	done
260*49cdfc7eSAndroid Build Coastguard Worker
261*49cdfc7eSAndroid Build Coastguard Worker	kill -KILL $pid >/dev/null 2>&1
262*49cdfc7eSAndroid Build Coastguard Worker	tst_res TPASS "NUMA MEMHOG policy"
263*49cdfc7eSAndroid Build Coastguard Worker}
264*49cdfc7eSAndroid Build Coastguard Worker
265*49cdfc7eSAndroid Build Coastguard Worker# Function:     hardware cheking with numa_node_size api
266*49cdfc7eSAndroid Build Coastguard Worker#
267*49cdfc7eSAndroid Build Coastguard Worker# Description:  - Returns the size of available nodes if success.
268*49cdfc7eSAndroid Build Coastguard Worker#
269*49cdfc7eSAndroid Build Coastguard Worker# Input:        - o/p of numactl --hardware command which is expected in the format
270*49cdfc7eSAndroid Build Coastguard Worker#                 shown below
271*49cdfc7eSAndroid Build Coastguard Worker#               available: 2 nodes (0-1)
272*49cdfc7eSAndroid Build Coastguard Worker#               node 0 size: 7808 MB
273*49cdfc7eSAndroid Build Coastguard Worker#               node 0 free: 7457 MB
274*49cdfc7eSAndroid Build Coastguard Worker#               node 1 size: 5807 MB
275*49cdfc7eSAndroid Build Coastguard Worker#               node 1 free: 5731 MB
276*49cdfc7eSAndroid Build Coastguard Worker#               node distances:
277*49cdfc7eSAndroid Build Coastguard Worker#               node   0   1
278*49cdfc7eSAndroid Build Coastguard Worker#                 0:  10  20
279*49cdfc7eSAndroid Build Coastguard Worker#                 1:  20  10
280*49cdfc7eSAndroid Build Coastguard Worker#
281*49cdfc7eSAndroid Build Coastguard Workertest7()
282*49cdfc7eSAndroid Build Coastguard Worker{
283*49cdfc7eSAndroid Build Coastguard Worker	RC=0
284*49cdfc7eSAndroid Build Coastguard Worker
285*49cdfc7eSAndroid Build Coastguard Worker	numactl --hardware > gavail_nodes
286*49cdfc7eSAndroid Build Coastguard Worker	RC=$(awk '{ if ( NR == 1 ) {print $1;} }' gavail_nodes)
287*49cdfc7eSAndroid Build Coastguard Worker	if [ $RC = "available:" ]; then
288*49cdfc7eSAndroid Build Coastguard Worker		RC=$(awk '{ if ( NR == 1 ) {print $3;} }' gavail_nodes)
289*49cdfc7eSAndroid Build Coastguard Worker		if [ $RC = "nodes" ]; then
290*49cdfc7eSAndroid Build Coastguard Worker			RC=$(awk '{ if ( NR == 1 ) {print $2;} }' gavail_nodes)
291*49cdfc7eSAndroid Build Coastguard Worker			tst_res TPASS "NUMA policy on lib NUMA_NODE_SIZE API"
292*49cdfc7eSAndroid Build Coastguard Worker		else
293*49cdfc7eSAndroid Build Coastguard Worker			tst_res TFAIL "Failed with NUMA policy"
294*49cdfc7eSAndroid Build Coastguard Worker		fi
295*49cdfc7eSAndroid Build Coastguard Worker	else
296*49cdfc7eSAndroid Build Coastguard Worker		tst_res TFAIL "Failed with NUMA policy"
297*49cdfc7eSAndroid Build Coastguard Worker	fi
298*49cdfc7eSAndroid Build Coastguard Worker}
299*49cdfc7eSAndroid Build Coastguard Worker
300*49cdfc7eSAndroid Build Coastguard Worker# Verification of hugepage memory allocated on a node
301*49cdfc7eSAndroid Build Coastguard Workertest8()
302*49cdfc7eSAndroid Build Coastguard Worker{
303*49cdfc7eSAndroid Build Coastguard Worker	Mem_huge=0
304*49cdfc7eSAndroid Build Coastguard Worker	Sys_node=/sys/devices/system/node
305*49cdfc7eSAndroid Build Coastguard Worker
306*49cdfc7eSAndroid Build Coastguard Worker	if [ ! -d "/sys/kernel/mm/hugepages/" ]; then
307*49cdfc7eSAndroid Build Coastguard Worker		tst_res TCONF "hugepage is not supported"
308*49cdfc7eSAndroid Build Coastguard Worker		return
309*49cdfc7eSAndroid Build Coastguard Worker	fi
310*49cdfc7eSAndroid Build Coastguard Worker
311*49cdfc7eSAndroid Build Coastguard Worker	for node in $nodes_list; do
312*49cdfc7eSAndroid Build Coastguard Worker		Ori_hpgs=$(cat ${Sys_node}/node${node}/hugepages/hugepages-${HPAGE_SIZE}kB/nr_hugepages)
313*49cdfc7eSAndroid Build Coastguard Worker		New_hpgs=$((Ori_hpgs + 1))
314*49cdfc7eSAndroid Build Coastguard Worker		echo $New_hpgs >${Sys_node}/node${node}/hugepages/hugepages-${HPAGE_SIZE}kB/nr_hugepages
315*49cdfc7eSAndroid Build Coastguard Worker
316*49cdfc7eSAndroid Build Coastguard Worker		Chk_hpgs=$(cat ${Sys_node}/node${node}/hugepages/hugepages-${HPAGE_SIZE}kB/nr_hugepages)
317*49cdfc7eSAndroid Build Coastguard Worker		if [ "$Chk_hpgs" -ne "$New_hpgs" ]; then
318*49cdfc7eSAndroid Build Coastguard Worker			tst_res TCONF "hugepage is not enough to test"
319*49cdfc7eSAndroid Build Coastguard Worker			return
320*49cdfc7eSAndroid Build Coastguard Worker		fi
321*49cdfc7eSAndroid Build Coastguard Worker
322*49cdfc7eSAndroid Build Coastguard Worker		numactl --cpunodebind=$node --membind=$node support_numa alloc_1huge_page &
323*49cdfc7eSAndroid Build Coastguard Worker		pid=$!
324*49cdfc7eSAndroid Build Coastguard Worker		TST_RETRY_FUNC "check_for_support_numa $pid" 0
325*49cdfc7eSAndroid Build Coastguard Worker
326*49cdfc7eSAndroid Build Coastguard Worker		local index=$(echo "$(get_node_index $pid $node)")
327*49cdfc7eSAndroid Build Coastguard Worker		Mem_huge=$(echo $(numastat -p $pid |awk '/^Huge/ {print $'$index'}'))
328*49cdfc7eSAndroid Build Coastguard Worker		Mem_huge=$((${Mem_huge%.*} * 1024))
329*49cdfc7eSAndroid Build Coastguard Worker
330*49cdfc7eSAndroid Build Coastguard Worker		if [ "$Mem_huge" -lt "$HPAGE_SIZE" ]; then
331*49cdfc7eSAndroid Build Coastguard Worker			tst_res TFAIL \
332*49cdfc7eSAndroid Build Coastguard Worker				"NUMA memory allocated in node$node is less than expected"
333*49cdfc7eSAndroid Build Coastguard Worker			kill -CONT $pid >/dev/null 2>&1
334*49cdfc7eSAndroid Build Coastguard Worker			echo $Ori_hpgs >${Sys_node}/node${node}/hugepages/hugepages-${HPAGE_SIZE}kB/nr_hugepages
335*49cdfc7eSAndroid Build Coastguard Worker			return
336*49cdfc7eSAndroid Build Coastguard Worker		fi
337*49cdfc7eSAndroid Build Coastguard Worker
338*49cdfc7eSAndroid Build Coastguard Worker		kill -CONT $pid >/dev/null 2>&1
339*49cdfc7eSAndroid Build Coastguard Worker		echo $Ori_hpgs >${Sys_node}/node${node}/hugepages/hugepages-${HPAGE_SIZE}kB/nr_hugepages
340*49cdfc7eSAndroid Build Coastguard Worker	done
341*49cdfc7eSAndroid Build Coastguard Worker
342*49cdfc7eSAndroid Build Coastguard Worker	tst_res TPASS "NUMA local node hugepage memory allocated"
343*49cdfc7eSAndroid Build Coastguard Worker}
344*49cdfc7eSAndroid Build Coastguard Worker
345*49cdfc7eSAndroid Build Coastguard Worker# Verification of THP memory allocated on preferred node
346*49cdfc7eSAndroid Build Coastguard Workertest9()
347*49cdfc7eSAndroid Build Coastguard Worker{
348*49cdfc7eSAndroid Build Coastguard Worker	local mem_curr
349*49cdfc7eSAndroid Build Coastguard Worker	local cnt=1
350*49cdfc7eSAndroid Build Coastguard Worker
351*49cdfc7eSAndroid Build Coastguard Worker	if ! grep -q '\[always\]' /sys/kernel/mm/transparent_hugepage/enabled; then
352*49cdfc7eSAndroid Build Coastguard Worker		tst_res TCONF "THP is not supported/enabled"
353*49cdfc7eSAndroid Build Coastguard Worker		return
354*49cdfc7eSAndroid Build Coastguard Worker	fi
355*49cdfc7eSAndroid Build Coastguard Worker
356*49cdfc7eSAndroid Build Coastguard Worker	for node in $nodes_list; do
357*49cdfc7eSAndroid Build Coastguard Worker		if [ $cnt -eq $total_nodes ]; then   #wrap up for last node
358*49cdfc7eSAndroid Build Coastguard Worker			Preferred_node=$(echo $nodes_list | cut -d ' ' -f 1)
359*49cdfc7eSAndroid Build Coastguard Worker		else
360*49cdfc7eSAndroid Build Coastguard Worker			# always next node is preferred node
361*49cdfc7eSAndroid Build Coastguard Worker			Preferred_node=$(echo $nodes_list | cut -d ' ' -f $((cnt+1)))
362*49cdfc7eSAndroid Build Coastguard Worker		fi
363*49cdfc7eSAndroid Build Coastguard Worker
364*49cdfc7eSAndroid Build Coastguard Worker		numactl --cpunodebind=$node --preferred=$Preferred_node support_numa alloc_2HPSZ_THP &
365*49cdfc7eSAndroid Build Coastguard Worker		pid=$!
366*49cdfc7eSAndroid Build Coastguard Worker
367*49cdfc7eSAndroid Build Coastguard Worker		TST_RETRY_FUNC "check_for_support_numa $pid" 0
368*49cdfc7eSAndroid Build Coastguard Worker
369*49cdfc7eSAndroid Build Coastguard Worker		mem_curr=$(get_mem_cur $pid $Preferred_node 1024)
370*49cdfc7eSAndroid Build Coastguard Worker		if [ $(echo "$mem_curr < $HPAGE_SIZE * 2" |bc ) -eq 1 ]; then
371*49cdfc7eSAndroid Build Coastguard Worker			tst_res TFAIL \
372*49cdfc7eSAndroid Build Coastguard Worker				"NUMA memory allocated in node$Preferred_node is less than expected"
373*49cdfc7eSAndroid Build Coastguard Worker			kill -CONT $pid >/dev/null 2>&1
374*49cdfc7eSAndroid Build Coastguard Worker			return
375*49cdfc7eSAndroid Build Coastguard Worker		fi
376*49cdfc7eSAndroid Build Coastguard Worker
377*49cdfc7eSAndroid Build Coastguard Worker		cnt=$((cnt+1))
378*49cdfc7eSAndroid Build Coastguard Worker		kill -CONT $pid >/dev/null 2>&1
379*49cdfc7eSAndroid Build Coastguard Worker	done
380*49cdfc7eSAndroid Build Coastguard Worker
381*49cdfc7eSAndroid Build Coastguard Worker	tst_res TPASS "NUMA preferred node policy verified with THP enabled"
382*49cdfc7eSAndroid Build Coastguard Worker}
383*49cdfc7eSAndroid Build Coastguard Worker
384*49cdfc7eSAndroid Build Coastguard Worker. tst_test.sh
385*49cdfc7eSAndroid Build Coastguard Workertst_run
386