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