1#!/bin/sh 2################################################################################ 3# 4# Copyright (C) 2022 Huawei Device Co., Ltd. 5# SPDX-License-Identifier: GPL-2.0 6# 7# Unless required by applicable law or agreed to in writing, software 8# distributed under the License is distributed on an "AS IS" BASIS, 9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10# See the License for the specific language governing permissions and 11# limitations under the License. 12# 13################################################################################ 14# File: cpuisolation02.sh 15# 16# Description: check CPU lightweight isolation basic function 17# 18# Authors: liudanning - liudanning@h-partners.com 19# 20# History: Mar 24 2022 - init scripts 21# 22################################################################################ 23 24source tst_oh.sh 25 26do_setup() 27{ 28 29} 30 31do_test() 32{ 33 ret=0 34 dir_name=/sys/devices/system/cpu/cpu0/core_ctl 35 global_state=${dir_name}/global_state 36 proc_sd=/proc/sched_debug 37 cpu_total=$(cat /proc/cpuinfo | grep "processor"| wc -l) 38 cpu_total=$(( $cpu_total - 1 )) 39 40 for i in $(seq 0 $cpu_total); do 41 touch cpu${i}_taskpid.txt 42 done 43 44 tst_res TINFO "Start to check CPU lightweight isolation basic function." 45 sh create_process.sh 40 46 sleep 20 47 48 echo "y" | rm cpu_log.txt 49 sleep 1 50 cat $proc_sd > cpu_log.txt 51 cpu_num0=0 52 cpu_num1=0 53 cpu_num2=0 54 cpu_num3=0 55 # check sh distributed on each CPU 56 for pid in $(cat taskpid.txt); do 57 echo $pid 58 if [ $(sed -n '/^cpu#0/,/cpu#1$/p' cpu_log.txt | awk -F " " '{print $3}' \ 59 | awk '!arr[$0]++' | grep -w "$pid") ]; then 60 cpu_num0=$(($cpu_num0 + 1)) 61 echo $pid >> cpu0_taskpid.txt 62 elif [ $(sed -n '/^cpu#1/,/cpu#2$/p' cpu_log.txt | awk -F " " '{print $3}' \ 63 | awk '!arr[$0]++' | grep -w "$pid") ]; then 64 cpu_num1=$(($cpu_num1 + 1)) 65 echo $pid >> cpu1_taskpid.txt 66 elif [ $(sed -n '/^cpu#2/,/cpu#3$/p' cpu_log.txt | awk -F " " '{print $3}' \ 67 | awk '!arr[$0]++' | grep -w "$pid") ]; then 68 cpu_num2=$(($cpu_num2 + 1)) 69 echo $pid >> cpu2_taskpid.txt 70 elif [ $(sed -n '/^cpu#3/,$p' cpu_log.txt | awk -F " " '{print $3}' \ 71 | awk '!arr[$0]++' | grep -w "$pid") ]; then 72 cpu_num3=$(($cpu_num3 + 1)) 73 echo $pid >> cpu3_taskpid.txt 74 fi 75 done 76 77 echo 2 > /sys/devices/system/cpu/cpu0/core_ctl/max_cpus 78 sleep 5 79 # both CPUs' isolated is set to 1 and NR isolated is set to 2 80 count=0 81 cpu_total=$(cat /proc/cpuinfo | grep "processor"| wc -l) 82 cpu_total=$(( $cpu_total -1 )) 83 for i in $(seq 0 $cpu_total); do 84 line=$(( $i + 1 )) 85 cpu_isolated=$(cat $global_state | grep 'Isolated:' \ 86 | sed -n "${line}p" | awk -F ':' '{print$2}') 87 nr_isolated=$(cat $global_state | grep 'Nr isolated CPUs:' \ 88 | sed -n "${line}p" | awk -F ':' '{print$2}') 89 if [[ $cpu_isolated -eq 1 && $nr_isolated -eq 2 ]]; then 90 tst_res TINFO "cpu$i Isolated: 1,and Nr isolated CPUs: 2." 91 count=$(( $count + 1 )) 92 check_migration 93 fi 94 done 95 96 if [ $count -eq 2 ]; then 97 tst_res TPASS "two Isolated set to 1,and two Nr isolated CPUs set to 2." 98 else 99 tst_res TFAIL "Isolated not set to 1 ,or Nr isolated CPUs not set to 2." 100 ((ret++)) 101 fi 102 echo "ret=$ret" 103 if [ $ret -eq 0 ]; then 104 tst_res TPASS "CPU lightweight isolation basic function test success." 105 else 106 tst_res TFAIL "CPU lightweight isolation basic function test failed!" 107 fi 108 109 echo 4 > /sys/devices/system/cpu/cpu0/core_ctl/max_cpus 110} 111 112check_migration() 113{ 114 res_3_pid=0 115 res_012_pid=0 116 # when the isolation CPU is 0/1/2, check the migration 117 if [[ $i -eq 0 || $i -eq 1 || $i -eq 2 ]];then 118 cpu_taskpid=cpu${i}_taskpid.txt 119 echo "cputaskpid$i:::::::" 120 cat cpu${i}_taskpid.txt 121 for pid1 in $(cat $cpu_taskpid); do 122 i_cpu=$(( $i + 1 )) 123 sed -n '/^cpu#$i/,/cpu#${i_cpu}$/p' $proc_sd \ 124 | awk -F " " '{print $3}' | grep -w "$pid1" 125 res_012_pid=$(($res_012_pid + $?)) 126 done 127 echo "cpu$i: $res_012_pid" 128 if [ $res_012_pid -eq $(eval echo '$'cpu_num"$i") ];then 129 tst_res TPASS "cpu$i process migrated." 130 else 131 tst_res TFAIL "cpu$i process is not migrated." 132 ((ret++)) 133 fi 134 res_012_pid=0 135 # when the isolation CPU is 3, check the migration 136 else 137 cat cpu3_taskpid.txt 138 for pid2 in $(cat cpu3_taskpid.txt); do 139 tail -n+$(sed -n -e "/cpu#3/=" $proc_sd) $proc_sd \ 140 | awk -F " " '{print $3}' | grep -w "$pid2" 141 res_3_pid=$(($res_3_pid + $?)) 142 done 143 if [ $res_3_pid -eq $cpu_num3 ];then 144 tst_res TPASS "cpu3 process migrated." 145 else 146 tst_res TFAIL "cpu3 process is not migrated." 147 ((ret++)) 148 fi 149 fi 150} 151 152do_clean() 153{ 154 ps -ef | grep "create_process" | grep -v "grep" | cut -c 9-18 \ 155 | xargs kill -9 156 echo "y" | rm cpu0_taskpid.txt 157 echo "y" | rm cpu1_taskpid.txt 158 echo "y" | rm cpu2_taskpid.txt 159 echo "y" | rm cpu3_taskpid.txt 160} 161 162do_setup 163do_test 164do_clean 165tst_exit