3 # core_pinning_auto.sh -- script to test vpp (debug-build) core-pinning
4 # -- in bare-metal, containers (docker, lxc)
6 DOCKER_CONTAINER_NAME="vpp_core_pinning"
8 CONTAINER_CPU_RANGE="4-7"
13 if [ ! -d "../../../vpp" ]; then
14 echo "VPP workspace path invalid"
15 echo "Please execute script from vpp/test/scripts folder.."
18 WS_ROOT="$(dirname $(readlink -e "../../../vpp"))/$(basename "../../../vpp")"
20 # Get available CPU count on host machine
21 host_cpulist=$(cat /sys/devices/system/cpu/online)
22 startcpu="${host_cpulist%-*}"
23 endcpu="${host_cpulist#*\-}"
24 cpucount="$(($endcpu - $startcpu + 1))"
25 if [ $cpucount -lt 8 ]
27 echo "Current host machine has $cpucount CPUs"
28 echo "A minimum of 8 CPUs is required to run testcases, exiting.."
31 # Check that container 'vpp_core_pinning' does not already exist
32 count=$(docker ps -a | grep -c "$DOCKER_CONTAINER_NAME")
35 echo "Error: docker container $DOCKER_CONTAINER_NAME already exists"
36 echo "Remove it using 'docker stop/docker rm', then re-run test"
39 # Check that there is no vpp instance currently running on the machine
40 count=$(pgrep vpp | wc -l)
43 echo "Error: a vpp instance is currently running on this machine"
44 echo "Please stop the running instance, then re-run test"
47 mkdir -p $VPP_SOCK_PATH
49 # Function to parse main core
52 main_core_parsed=$main_core_args
53 if [ $main_core_args = "auto" ];
56 if [ -n "$SKIP_CORE" ]
58 main_core_parsed=$(($main_core_parsed + $SKIP_CORE))
60 if [ -n "$CONTAINER_RESTRAIN_CPUSET" ]
62 main_core_parsed=(${container_cpus[ $main_core_parsed ]})
65 echo $main_core_parsed
68 # Function to parse n workers range to an array
69 # e.g. "4" is parsed to ('0','1','2','3')
73 main_core_increment="0"
74 skip_core_increment="0"
75 if [ -n "$SKIP_CORE" ]
77 skip_core_increment=$(($SKIP_CORE))
80 for ((i=0;i<$workers_n_args;i++)); do
82 if [ -n "$CONTAINER_RESTRAIN_CPUSET" ]
84 if [ $(( ${container_cpus[ $(($i + $skip_core_increment)) ]})) -eq $(("$parsed_main_core")) ]
86 main_core_increment=$(($main_core_increment + 1))
88 workers_n_parsed+=" ${container_cpus[ $(($i + $main_core_increment + $skip_core_increment)) ]}"
90 if [ $(( $skip_core_increment + $i)) -eq $(("$parsed_main_core")) ]
92 main_core_increment=$(($main_core_increment + 1))
94 workers_n_parsed+=" $(($i + $main_core_increment + $skip_core_increment))"
97 echo $workers_n_parsed
100 # Function to parse corelist range to an array
101 # e.g. "0,3-5,7" is parsed to ('0','3','4','5','7')
104 corelist_args=$(echo $corelist_args | grep -Po '[0-9]+-[0-9]+|[0-9]+')
106 for corelist_elt in ${corelist_args[@]};do
107 if [ $(echo $corelist_elt | grep -Po '[0-9]+-[0-9]+') ]
109 startcpu="${corelist_elt%-*}"
110 endcpu="${corelist_elt#*\-}"
111 cpucount="$(($endcpu - $startcpu))"
112 for ((i=0;i<=$cpucount;i++)); do
113 corelist_parsed+=" $(($i+$startcpu))"
115 elif [ $(echo $corelist_elt | grep -Po '[0-9]+') ]
117 corelist_parsed+=" ${corelist_elt}"
120 echo $corelist_parsed
122 # Test VPP core pinning configuration
123 test_pinning_conf () {
124 VPP_CPU_EXTRA_OPTIONS=""
125 if [ -n "$CORELIST_WORKERS" ];
127 VPP_CPU_EXTRA_OPTIONS=" corelist-workers ${CORELIST_WORKERS}"
129 if [ -n "$WORKERS_AUTO" ];
131 VPP_CPU_EXTRA_OPTIONS=" workers ${WORKERS_AUTO}"
133 if [ -n "$SKIP_CORE" ];
135 VPP_CPU_EXTRA_OPTIONS="${VPP_CPU_EXTRA_OPTIONS} skip-cores ${SKIP_CORE}"
137 echo "TEST - conf 'cpu {main-core ${MAIN_CORE} ${VPP_CPU_EXTRA_OPTIONS}}'"
138 if [ -z "$CONTAINER_RESTRAIN_CPUSET" ];
140 VPP_CONTAINER_CPUSET=""
141 echo "(Running vpp in container with full host cpuset $host_cpulist)"
143 VPP_CONTAINER_CPUSET="--cpuset-cpus $CONTAINER_CPU_RANGE"
144 echo "(Running vpp in container with limited cpuset $CONTAINER_CPU_RANGE)"
146 (docker run -d ${VPP_CONTAINER_CPUSET} --name="$DOCKER_CONTAINER_NAME" \
147 -e LD_LIBRARY_PATH="/vpp/build-root/build-vpp_debug-native/vpp/lib/x86_64-linux-gnu/" -v $VPP_SOCK_PATH:$VPP_SOCK_PATH \
148 -v $WS_ROOT:/vpp ubuntu:22.04 sh -c "/vpp/build-root/build-vpp_debug-native/vpp/bin/vpp unix {interactive \
149 nodaemon cli-listen $VPP_SOCK_PATH/cli.sock} cpu {main-core ${MAIN_CORE} ${VPP_CPU_EXTRA_OPTIONS} } plugins \
150 { plugin dpdk_plugin.so {disable } }" > /dev/null )
151 sleep 3 # wait for VPP to initialize socket
152 # Change access permissions on vpp cli socket
153 # docker exec -it "$DOCKER_CONTAINER_NAME" /bin/bash -c "chmod 777 $VPP_SOCK_PATH/cli.sock" > /dev/null
154 # check if vppctl can connect to vpp container instance
155 $WS_ROOT/build-root/build-vpp_debug-native/vpp/bin/vppctl -s $VPP_SOCK_PATH/cli.sock show threads 1> /dev/null
156 # get CPUs vpp instance in container is running on
157 taskset_vpp_cpus=($( taskset --all-tasks -pc $(pgrep vpp) | grep -e ".$" -o))
159 # parse list of user requested CPUs for vpp
161 parsed_main_core=$(parse_maincore ${MAIN_CORE})
162 requested_cpus+=($parsed_main_core)
163 if [ -n "$CORELIST_WORKERS" ];
165 requested_cpus+=($(parse_corelist ${CORELIST_WORKERS}))
167 if [ -n "$WORKERS_AUTO" ];
169 requested_cpus+=($(parse_workers_n ${WORKERS_AUTO}))
172 # parse list of expected CPUs used by vpp
173 expected_cpu_mapping=()
174 expected_cpu_mapping=("${requested_cpus[@]}")
175 echo "CPUs requested by user: [${requested_cpus[@]}]"
176 echo "--------------------"
177 echo "Expected CPU Mapping: [${expected_cpu_mapping[@]}]"
178 echo "VPP pinning (taskset): [${taskset_vpp_cpus[@]}]"
179 #check if expected CPU mapping matches CPUs vpp instance in container is running on
181 for index in ${!taskset_vpp_cpus[@]}; do
182 if [ ${taskset_vpp_cpus[$index]} -ne ${expected_cpu_mapping[ $index ]} ]
187 if [ $rc -eq 0 ] && [ -z "$failure_cond" ]
189 echo "Test Successful"
190 TEST_SUCCESS=$(($TEST_SUCCESS+1))
193 TEST_FAIL=$(($TEST_FAIL+1))
195 echo "=============================================="
197 # Stop & destroy container instance
198 docker stop $DOCKER_CONTAINER_NAME &> /dev/null
199 docker rm -f $DOCKER_CONTAINER_NAME &> /dev/null
201 test_invalid_conf () {
202 if [ -n "$CORELIST_WORKERS" ];
204 VPP_CPU_EXTRA_OPTIONS=" corelist-workers ${CORELIST_WORKERS}"
206 if [ -n "$WORKERS_AUTO" ];
208 VPP_CPU_EXTRA_OPTIONS=" workers ${WORKERS_AUTO}"
210 if [ -n "$SKIP_CORE" ];
212 VPP_CPU_EXTRA_OPTIONS="${VPP_CPU_EXTRA_OPTIONS} skip-cores ${SKIP_CORE}"
214 echo "TEST - conf 'cpu {main-core ${MAIN_CORE} ${VPP_CPU_EXTRA_OPTIONS}}'"
215 if [ -z "$CONTAINER_RESTRAIN_CPUSET" ];
217 VPP_CONTAINER_CPUSET=""
218 echo "(Running vpp in container with full host cpuset $host_cpulist)"
220 VPP_CONTAINER_CPUSET="--cpuset-cpus $CONTAINER_CPU_RANGE"
221 echo "(Running vpp in container with limited cpuset $CONTAINER_CPU_RANGE)"
223 (docker run -d --cpuset-cpus $CONTAINER_CPU_RANGE --name="$DOCKER_CONTAINER_NAME" \
224 -e LD_LIBRARY_PATH="/vpp/build-root/build-vpp_debug-native/vpp/lib/x86_64-linux-gnu/" -v $VPP_SOCK_PATH:$VPP_SOCK_PATH \
225 -v $WS_ROOT:/vpp ubuntu:22.04 sh -c "/vpp/build-root/build-vpp_debug-native/vpp/bin/vpp unix {interactive \
226 nodaemon cli-listen $VPP_SOCK_PATH/cli.sock} cpu {main-core ${MAIN_CORE} ${VPP_CPU_EXTRA_OPTIONS}} plugins \
227 { plugin dpdk_plugin.so {disable } }" > /dev/null)
228 sleep 3 # wait for vpp to initialize socket
229 # check if vpp launched with invalid configuration
230 taskset --all-tasks -pc $(pgrep vpp) &> /dev/null
235 echo "OK... VPP did not launch with invalid configuration"
236 TEST_SUCCESS=$(($TEST_SUCCESS+1))
239 echo "Failure... VPP launched with wrong configuration"
240 TEST_FAIL=$(($TEST_FAIL+1))
242 echo "=============================================="
244 # Stop & destroy container instance
245 docker stop $DOCKER_CONTAINER_NAME &> /dev/null
246 docker rm -f $DOCKER_CONTAINER_NAME &> /dev/null
249 container_cpus=($(parse_corelist ${CONTAINER_CPU_RANGE}))
250 echo "TESTING VALID CORE PINNING CONFIGURATIONS"
254 CONTAINER_RESTRAIN_CPUSET=""
255 CORELIST_WORKERS="1-3"
260 CONTAINER_RESTRAIN_CPUSET=""
261 CORELIST_WORKERS="0,2-3"
266 CONTAINER_RESTRAIN_CPUSET=""
267 CORELIST_WORKERS="0-2"
272 CONTAINER_RESTRAIN_CPUSET=""
278 CONTAINER_RESTRAIN_CPUSET="t"
284 CONTAINER_RESTRAIN_CPUSET="t"
290 CONTAINER_RESTRAIN_CPUSET="t"
294 echo "TESTING NON-VALID CORE PINNING CONFIGURATIONS"
298 CONTAINER_RESTRAIN_CPUSET="t"
299 CORELIST_WORKERS="1-3"
304 CONTAINER_RESTRAIN_CPUSET="t"
310 CONTAINER_RESTRAIN_CPUSET="t"
316 CONTAINER_RESTRAIN_CPUSET="t"
321 echo "========================"
323 echo "SUCCESS: $TEST_SUCCESS"
324 echo "FAILURE: $TEST_FAIL"
325 echo "========================"