liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/bin/sh
##############################################################################
#                                                                            #
# Copyright (c) International Business Machines  Corp., 2007                 #
#               Sivakumar Chinnaiah, Sivakumar.C@in.ibm.com                  #
# Copyright (c) Linux Test Project, 2016                                     #
#                                                                            #
# This program is free software: you can redistribute it and/or modify       #
# it under the terms of the GNU General Public License as published by       #
# the Free Software Foundation, either version 3 of the License, or          #
# (at your option) any later version.                                        #
#                                                                            #
# This program is distributed in the hope that it will be useful,            #
# but WITHOUT ANY WARRANTY; without even the implied warranty of             #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              #
# GNU General Public License for more details.                               #
#                                                                            #
# You should have received a copy of the GNU General Public License          #
# along with this program. If not, see <http://www.gnu.org/licenses/>.       #
#                                                                            #
##############################################################################
#                                                                            #
# Description:  Test Basic functionality of numactl command.                 #
#               Test #1: Verifies cpunodebind and membind                    #
#               Test #2: Verifies preferred node bind for memory allocation  #
#               Test #3: Verifies share memory allocation on preferred node  #
#               Test #4: Verifies memory interleave on all nodes             #
#               Test #5: Verifies share memory interleave on all nodes       #
#               Test #6: Verifies physcpubind                                #
#               Test #7: Verifies localalloc                                 #
#               Test #8: Verifies memhog                                     #
#               Test #9: Verifies numa_node_size api                         #
#               Test #10:Verifies Migratepages                               #
#               Test #11:Verifies hugepage alloacted on specified node       #
#               Test #12:Verifies THP memory allocated on preferred node     #
#                                                                            #
##############################################################################
 
TST_CNT=12
TST_SETUP=setup
TST_TESTFUNC=test
TST_NEEDS_TMPDIR=1
TST_NEEDS_ROOT=1
TST_NEEDS_CMDS="numactl numastat awk"
 
. tst_test.sh
 
#
# Extracts the value of given numa node from the `numastat -p` output.
#
# $1 - Pid number.
# $2 - Node number.
#
extract_numastat_p()
{
   local pid=$1
   local node=$(($2 + 2))
 
   echo $(numastat -p $pid |awk '/^Total/ {print $'$node'}')
}
 
check_for_support_numa()
{
   local pid=$1
 
   local state=$(awk '{print $3}' /proc/$pid/stat)
 
   if [ $state = 'T' ]; then
       return 0
   fi
 
   return 1
}
 
setup()
{
   export MB=$((1024*1024))
   export PAGE_SIZE=$(tst_getconf PAGESIZE)
   export HPAGE_SIZE=$(awk '/Hugepagesize:/ {print $2}' /proc/meminfo)
 
   total_nodes=0
 
   nodes_list=$(numactl --show | grep nodebind | cut -d ':' -f 2)
   for node in $nodes_list; do
       total_nodes=$((total_nodes+1))
   done
 
   tst_res TINFO "The system contains $total_nodes nodes: $nodes_list"
   if [ $total_nodes -le 1 ]; then
       tst_brk TCONF "your machine does not support numa policy
       or your machine is not a NUMA machine"
   fi
}
 
# Verification of memory allocated on a node
test1()
{
   Mem_curr=0
 
   for node in $nodes_list; do
       numactl --cpunodebind=$node --membind=$node support_numa alloc_1MB &
       pid=$!
 
       TST_RETRY_FUNC "check_for_support_numa $pid" 0
 
       Mem_curr=$(echo "$(extract_numastat_p $pid $node) * $MB" |bc)
       if [ $(echo "$Mem_curr < $MB" | bc) -eq 1 ]; then
           tst_res TFAIL \
               "NUMA memory allocated in node$node is less than expected"
           kill -CONT $pid >/dev/null 2>&1
           return
       fi
 
       kill -CONT $pid >/dev/null 2>&1
   done
 
   tst_res TPASS "NUMA local node and memory affinity"
}
 
# Verification of memory allocated on preferred node
test2()
{
   Mem_curr=0
 
   COUNTER=1
   for node in $nodes_list; do
 
       if [ $COUNTER -eq $total_nodes ]; then   #wrap up for last node
           Preferred_node=$(echo $nodes_list | cut -d ' ' -f 1)
       else
           # always next node is preferred node
           Preferred_node=$(echo $nodes_list | cut -d ' ' -f $((COUNTER+1)))
       fi
 
       numactl --cpunodebind=$node --preferred=$Preferred_node support_numa alloc_1MB &
       pid=$!
 
       TST_RETRY_FUNC "check_for_support_numa $pid" 0
 
       Mem_curr=$(echo "$(extract_numastat_p $pid $Preferred_node) * $MB" |bc)
       if [ $(echo "$Mem_curr < $MB" |bc ) -eq 1 ]; then
           tst_res TFAIL \
               "NUMA memory allocated in node$Preferred_node is less than expected"
           kill -CONT $pid >/dev/null 2>&1
           return
       fi
 
       COUNTER=$((COUNTER+1))
       kill -CONT $pid >/dev/null 2>&1
   done
 
   tst_res TPASS "NUMA preferred node policy"
}
 
# Verification of share memory allocated on preferred node
test3()
{
   Mem_curr=0
   COUNTER=1
 
   for node in $nodes_list; do
 
       if [ $COUNTER -eq $total_nodes ]   #wrap up for last node
       then
           Preferred_node=$(echo $nodes_list | cut -d ' ' -f 1)
       else
           # always next node is preferred node
           Preferred_node=$(echo $nodes_list | cut -d ' ' -f $((COUNTER+1)))
       fi
 
       numactl --cpunodebind=$node --preferred=$Preferred_node support_numa alloc_1MB_shared &
       pid=$!
 
       TST_RETRY_FUNC "check_for_support_numa $pid" 0
 
       Mem_curr=$(echo "$(extract_numastat_p $pid $Preferred_node) * $MB" |bc)
       if [ $(echo "$Mem_curr < $MB" |bc ) -eq 1 ]; then
           tst_res TFAIL \
               "NUMA share memory allocated in node$Preferred_node is less than expected"
           kill -CONT $pid >/dev/null 2>&1
           return
       fi
 
       COUNTER=$((COUNTER+1))
       kill -CONT $pid >/dev/null 2>&1
   done
 
   tst_res TPASS "NUMA share memory allocated in preferred node"
}
 
# Verification of memory interleaved on all nodes
test4()
{
   Mem_curr=0
   # Memory will be allocated using round robin on nodes.
   Exp_incr=$(echo "$MB / $total_nodes" |bc)
 
   numactl --interleave=all support_numa alloc_1MB &
   pid=$!
 
   TST_RETRY_FUNC "check_for_support_numa $pid" 0
 
   for node in $nodes_list; do
       Mem_curr=$(echo "$(extract_numastat_p $pid $node) * $MB" |bc)
 
       if [ $(echo "$Mem_curr < $Exp_incr" |bc ) -eq 1 ]; then
           tst_res TFAIL \
               "NUMA interleave memory allocated in node$node is less than expected"
           kill -CONT $pid >/dev/null 2>&1
           return
       fi
   done
 
   kill -CONT $pid >/dev/null 2>&1
   tst_res TPASS "NUMA interleave policy"
}
 
# Verification of shared memory interleaved on all nodes
test5()
{
   Mem_curr=0
   # Memory will be allocated using round robin on nodes.
   Exp_incr=$(echo "$MB / $total_nodes" |bc)
 
   numactl --interleave=all support_numa alloc_1MB_shared &
   pid=$!
 
   TST_RETRY_FUNC "check_for_support_numa $pid" 0
 
   for node in $nodes_list; do
       Mem_curr=$(echo "$(extract_numastat_p $pid $node) * $MB" |bc)
 
       if [ $(echo "$Mem_curr < $Exp_incr" |bc ) -eq 1 ]; then
           tst_res TFAIL \
               "NUMA interleave share memory allocated in node$node is less than expected"
           kill -CONT $pid >/dev/null 2>&1
           return
       fi
   done
 
   kill -CONT $pid >/dev/null 2>&1
 
   tst_res TPASS "NUMA interleave policy on shared memory"
}
 
# Verification of physical cpu bind
test6()
{
   no_of_cpus=0    #no. of cpu's exist
   run_on_cpu=0
   running_on_cpu=0
 
   no_of_cpus=$(tst_ncpus)
   # not sure whether cpu's can't be in odd number
   run_on_cpu=$(($((no_of_cpus+1))/2))
   numactl --physcpubind=$run_on_cpu support_numa pause & #just waits for sigint
   pid=$!
   var=`awk '{ print $2 }' /proc/$pid/stat`
   while [ $var = '(numactl)' ]; do
       var=`awk '{ print $2 }' /proc/$pid/stat`
       tst_sleep 100ms
   done
   # Warning !! 39 represents cpu number, on which process pid is currently running and
   # this may change if Some more fields are added in the middle, may be in future
   running_on_cpu=$(awk '{ print $39; }' /proc/$pid/stat)
   if [ $running_on_cpu -ne $run_on_cpu ]; then
       tst_res TFAIL \
           "Process running on cpu$running_on_cpu but expected to run on cpu$run_on_cpu"
       ROD kill -INT $pid
       return
   fi
 
   ROD kill -INT $pid
 
   tst_res TPASS "NUMA phycpubind policy"
}
 
# Verification of local node allocation
test7()
{
   Mem_curr=0
 
   for node in $nodes_list; do
       numactl --cpunodebind=$node --localalloc support_numa alloc_1MB &
       pid=$!
 
       TST_RETRY_FUNC "check_for_support_numa $pid" 0
 
       Mem_curr=$(echo "$(extract_numastat_p $pid $node) * $MB" |bc)
       if [ $(echo "$Mem_curr < $MB" |bc ) -eq 1 ]; then
           tst_res TFAIL \
               "NUMA localnode memory allocated in node$node is less than expected"
           kill -CONT $pid >/dev/null 2>&1
           return
       fi
 
       kill -CONT $pid >/dev/null 2>&1
   done
 
   tst_res TPASS "NUMA local node allocation"
}
 
check_ltp_numa_test8_log()
{
   grep -m1 -q '.' ltp_numa_test8.log
}
 
# Verification of memhog with interleave policy
test8()
{
   Mem_curr=0
   # Memory will be allocated using round robin on nodes.
   Exp_incr=$(echo "$MB / $total_nodes" |bc)
 
   numactl --interleave=all memhog -r1000000 1MB >ltp_numa_test8.log 2>&1 &
   pid=$!
 
   TST_RETRY_FUNC "check_ltp_numa_test8_log" 0
 
   for node in $nodes_list; do
       Mem_curr=$(echo "$(extract_numastat_p $pid $node) * $MB" |bc)
 
       if [ $(echo "$Mem_curr < $Exp_incr" |bc ) -eq 1 ]; then
           tst_res TFAIL \
               "NUMA interleave memhog in node$node is less than expected"
           kill -KILL $pid >/dev/null 2>&1
           return
       fi
   done
 
   kill -KILL $pid >/dev/null 2>&1
   tst_res TPASS "NUMA MEMHOG policy"
}
 
# Function:     hardware cheking with numa_node_size api
#
# Description:  - Returns the size of available nodes if success.
#
# Input:        - o/p of numactl --hardware command which is expected in the format
#                 shown below
#               available: 2 nodes (0-1)
#               node 0 size: 7808 MB
#               node 0 free: 7457 MB
#               node 1 size: 5807 MB
#               node 1 free: 5731 MB
#               node distances:
#               node   0   1
#                 0:  10  20
#                 1:  20  10
#
test9()
{
   RC=0
 
   numactl --hardware > gavail_nodes
   RC=$(awk '{ if ( NR == 1 ) {print $1;} }' gavail_nodes)
   if [ $RC = "available:" ]; then
       RC=$(awk '{ if ( NR == 1 ) {print $3;} }' gavail_nodes)
       if [ $RC = "nodes" ]; then
           RC=$(awk '{ if ( NR == 1 ) {print $2;} }' gavail_nodes)
           tst_res TPASS "NUMA policy on lib NUMA_NODE_SIZE API"
       else
           tst_res TFAIL "Failed with numa policy"
       fi
   else
       tst_res TFAIL "Failed with numa policy"
   fi
}
 
# Verification of migratepages
test10()
{
   Mem_curr=0
   COUNTER=1
 
   for node in $nodes_list; do
 
       if [ $COUNTER -eq $total_nodes ]; then
           Preferred_node=$(echo $nodes_list | cut -d ' ' -f 1)
       else
           Preferred_node=$(echo $nodes_list | cut -d ' ' -f $((COUNTER+1)))
       fi
 
       numactl --preferred=$node support_numa alloc_1MB &
       pid=$!
 
       TST_RETRY_FUNC "check_for_support_numa $pid" 0
 
       migratepages $pid $node $Preferred_node
 
       Mem_curr=$(echo "$(extract_numastat_p $pid $Preferred_node) * $MB" |bc)
       if [ $(echo "$Mem_curr < $MB" |bc ) -eq 1 ]; then
           tst_res TFAIL \
               "NUMA migratepages is not working fine"
           kill -CONT $pid >/dev/null 2>&1
           return
       fi
 
       COUNTER=$((COUNTER+1))
       kill -CONT $pid >/dev/null 2>&1
   done
 
   tst_res TPASS "NUMA MIGRATEPAGES policy"
}
 
# Verification of hugepage memory allocated on a node
test11()
{
   Mem_huge=0
   Sys_node=/sys/devices/system/node
 
   if [ ! -d "/sys/kernel/mm/hugepages/" ]; then
       tst_res TCONF "hugepage is not supported"
       return
   fi
 
   for node in $nodes_list; do
       Ori_hpgs=$(cat ${Sys_node}/node${node}/hugepages/hugepages-${HPAGE_SIZE}kB/nr_hugepages)
       New_hpgs=$((Ori_hpgs + 1))
       echo $New_hpgs >${Sys_node}/node${node}/hugepages/hugepages-${HPAGE_SIZE}kB/nr_hugepages
 
       Chk_hpgs=$(cat ${Sys_node}/node${node}/hugepages/hugepages-${HPAGE_SIZE}kB/nr_hugepages)
       if [ "$Chk_hpgs" -ne "$New_hpgs" ]; then
           tst_res TCONF "hugepage is not enough to test"
           return
       fi
 
       numactl --cpunodebind=$node --membind=$node support_numa alloc_1huge_page &
       pid=$!
       TST_RETRY_FUNC "check_for_support_numa $pid" 0
 
       Mem_huge=$(echo $(numastat -p $pid |awk '/^Huge/ {print $'$((node+2))'}'))
       Mem_huge=$((${Mem_huge%.*} * 1024))
 
       if [ "$Mem_huge" -lt "$HPAGE_SIZE" ]; then
           tst_res TFAIL \
               "NUMA memory allocated in node$node is less than expected"
           kill -CONT $pid >/dev/null 2>&1
           echo $Ori_hpgs >${Sys_node}/node${node}/hugepages/hugepages-${HPAGE_SIZE}kB/nr_hugepages
           return
       fi
 
       kill -CONT $pid >/dev/null 2>&1
       echo $Ori_hpgs >${Sys_node}/node${node}/hugepages/hugepages-${HPAGE_SIZE}kB/nr_hugepages
   done
 
   tst_res TPASS "NUMA local node hugepage memory allocated"
}
 
# Verification of THP memory allocated on preferred node
test12()
{
   Mem_curr=0
 
   if ! grep -q '\[always\]' /sys/kernel/mm/transparent_hugepage/enabled; then
       tst_res TCONF "THP is not supported/enabled"
       return
   fi
 
   COUNTER=1
   for node in $nodes_list; do
 
       if [ $COUNTER -eq $total_nodes ]; then   #wrap up for last node
           Preferred_node=$(echo $nodes_list | cut -d ' ' -f 1)
       else
           # always next node is preferred node
           Preferred_node=$(echo $nodes_list | cut -d ' ' -f $((COUNTER+1)))
       fi
 
       numactl --cpunodebind=$node --preferred=$Preferred_node support_numa alloc_2HPSZ_THP &
       pid=$!
 
       TST_RETRY_FUNC "check_for_support_numa $pid" 0
 
       Mem_curr=$(echo "$(extract_numastat_p $pid $Preferred_node) * 1024" |bc)
       if [ $(echo "$Mem_curr < $HPAGE_SIZE * 2" |bc ) -eq 1 ]; then
           tst_res TFAIL \
               "NUMA memory allocated in node$Preferred_node is less than expected"
           kill -CONT $pid >/dev/null 2>&1
           return
       fi
 
       COUNTER=$((COUNTER+1))
       kill -CONT $pid >/dev/null 2>&1
   done
 
   tst_res TPASS "NUMA preferred node policy verified with THP enabled"
}
 
tst_run