• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: cpuisolation01.sh
15#
16# Description: check rw nodes test about CPU lightweight isolation
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    echo 0 > /sys/devices/system/cpu/cpu0/core_ctl/min_cpus
29    echo 4 > /sys/devices/system/cpu/cpu0/core_ctl/max_cpus
30}
31
32write_invalid()
33{
34    local max_value=$1
35    local min_value=$2
36    local mode=$3
37
38    min_cpus_value=$(cat $min_cpus)
39    max_cpus_value=$(cat $max_cpus)
40    min_cpus_value_backup=$(cat $min_cpus)
41    max_cpus_value_backup=$(cat $max_cpus)
42
43    echo $max_value > $max_cpus
44    ret_max=$?
45    echo $min_value > $min_cpus
46    ret_min=$?
47    min_cpus_value=$(cat $min_cpus)
48    max_cpus_value=$(cat $max_cpus)
49
50    # mode 1 is to inject a single invalid value
51    if [ $mode -eq 1 ]; then
52        if [ $ret_min -eq 1 ]; then
53            check_value
54        elif [ $min_cpus_value -eq 4 ]; then
55            check_value
56        elif [ $ret_max -eq 1 ]; then
57            check_value
58        elif [[ $min_cpus_value -eq 0 ]] && [[ $max_cpus_value -eq 4 ]]; then
59            check_value
60        else
61            tst_res TFAIL "writing single MAX VALUE $max_value or  \
62            MIN VALUE $min_value failed."
63            ((ret++))
64        fi
65    fi
66
67    # mode 2 is to inject both invalid values
68    if [ $mode -eq 2 ]; then
69        if [[ $ret_min -eq 1 ]] && [[ $ret_max -eq 1 ]]; then
70            check_value
71        elif [[ $ret_min -eq 1 ]] && [[ $max_cpus_value -eq 4 ]]; then
72            check_value
73        elif [[ $ret_max -eq 1 ]] && [[ $min_cpus_value -eq 4 ]]; then
74            check_value
75        elif [[ $ret_max -ne 1 ]] && [[ $max_cpus_value -eq 4 ]] &&  \
76        [[ $min_cpus_value -eq 4 ]]; then
77            check_value
78        else
79            tst_res TFAIL "writing both MAX VALUE $max_value and  \
80            MIN VALUE $min_value failed."
81            ((ret++))
82        fi
83    fi
84
85    echo ${min_cpus_value_backup} > $min_cpus
86    echo ${max_cpus_value_backup} > $max_cpus
87}
88
89check_value()
90{
91    if [ $min_cpus_value -ge 0 ] && [ $min_cpus_value -le $max_cpus_value ]\
92           && [ $max_cpus_value -le 4 ]; then
93        tst_res TPASS "writing MAX VALUE $max_value and MIN VALUE $min_value , \
94        value is normal."
95    else
96        tst_res TFAIL "writing MAX VALUE $max_value and MIN VALUE $min_value , \
97        value is abnormal."
98        ((ret++))
99    fi
100}
101
102do_test()
103{
104    local ret=0
105    local dir_name=/sys/devices/system/cpu/cpu0/core_ctl
106    local min_cpus=${dir_name}/min_cpus
107    local max_cpus=${dir_name}/max_cpus
108
109    active_cpus=${dir_name}/active_cpus
110    enable=${dir_name}/enable
111    global_state=${dir_name}/global_state
112    need_cpus=${dir_name}/need_cpus
113
114    tst_res TINFO "Start to check rw nodes test about CPU lightweight isolation."
115    if [[ -e "${active_cpus}" && -e "${enable}" && -e "${global_state}" \
116          && -e "${max_cpus}" && -e "${min_cpus}" && -e "${need_cpus}" ]]; then
117        tst_res TPASS "/sys/devices/system/cpu/cpu0/core_ctl/ node normal."
118    else
119        tst_res TFAIL "/sys/devices/system/cpu/cpu0/core_ctl/ node abnormal."
120        ((ret++))
121    fi
122
123    write_invalid 4 -3 1
124    write_invalid 4 999 1
125    write_invalid -3 0 1
126    write_invalid 999 0 1
127    write_invalid -3 -3 2
128    write_invalid 999 -3 2
129    write_invalid -3 999 2
130    write_invalid 999 999 2
131
132    cpu_total=$(cat /proc/cpuinfo | grep "processor"| wc -l)
133    cpu_total=$(( $cpu_total - 1 ))
134    for i in $(seq 0 $cpu_total); do
135        cpu_name=$(cat $global_state | grep "CPU${i}")
136        echo $cpu_name
137        if [ "$cpu_name"x = "CPU${i}"x ]; then
138            tst_res TPASS "cpu$i name correct."
139        else
140            tst_res TFAIL "cpu$i name incorrect."
141            ((ret++))
142        fi
143
144        line=$(( $i + 1))
145        cpu_online_state=$(cat $global_state | grep 'Online:' | \
146                         sed -n "${line}p" | awk -F ':' '{print$2}')
147        if [ $cpu_online_state -eq 1 ]; then
148            tst_res TPASS "cpu$i online."
149        else
150            tst_res TFAIL"cpu$i offline."
151            ((ret++))
152        fi
153
154        cpu_isolated_state=$(cat $global_state | grep 'Isolated:' | \
155                           sed -n "${line}p" | awk -F ':' '{print$2}')
156        if [ $cpu_isolated_state -eq 0 ]; then
157            tst_res TPASS "cpu$i isolated : 0."
158        else
159            tst_res TFAIL "cpu$i isolated : 1."
160            ((ret++))
161        fi
162
163        cpu_is_busy_state=$(cat $global_state | grep 'Is busy:' | \
164                          sed -n "${line}p" | awk -F ':' '{print$2}')
165        if [ $cpu_is_busy_state -eq 1 ]; then
166            tst_res TPASS "cpu$i is_busy : 1."
167        else
168            tst_res TFAIL "cpu$i is_busy : 0."
169            ((ret++))
170        fi
171    done
172
173    if [ $ret -eq 0 ];then
174        tst_res TPASS "check rw nodes test about CPU isolation successfully."
175        exit 0
176    else
177        tst_res TFAIL "check rw nodes test about CPU isolation failed!"
178        exit 1
179    fi
180}
181
182do_clean()
183{
184
185}
186
187do_setup
188do_test
189do_clean
190tst_exit