• 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: cpuisolation03.sh
15#
16# Description: check CPU lightweight isolation Power up and down 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    touch isolated_cpu1.txt
29    touch active_cpu1.txt
30    touch isolated_cpu2.txt
31    touch active_cpu2.txt
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
41    tst_res TINFO "Start to check CPU isolation Power up and down function."
42    sh create_process.sh 40
43    sleep 5
44
45    echo 2 > /sys/devices/system/cpu/cpu0/core_ctl/max_cpus
46    check_isolation
47    check_movement
48    isolated_cpu_online
49    active_cpu_online
50    echo "ret=$ret"
51    if [ $ret -eq 0 ]; then
52        tst_res TPASS "CPU isolation Power up and down function success."
53    else
54        tst_res TFAIL "CPU isolation Power up and down function failed!"
55    fi
56
57    echo 4 > /sys/devices/system/cpu/cpu0/core_ctl/max_cpus
58}
59
60check_isolation()
61{
62    cpu_total=$(cat /proc/cpuinfo | grep "processor" | wc -l)
63    cpu_total=$(( ${cpu_total} - 1 ))
64    active_num=0
65    isolated_num=0
66    for i in $(seq 0 ${cpu_total}); do
67        line=$(( $i + 1))
68        cpu_isolated_state=$(cat $global_state | grep 'Isolated:'  \
69        | sed -n "${line}p" | awk -F ':' '{print$2}')
70        if [ $cpu_isolated_state -eq 0 ]; then
71            tst_res TINFO "cpu$i is active."
72            active_num=$(( $active_num + 1 ))
73            echo $i >> active_cpu1.txt
74        else
75            tst_res TINFO "cpu$i is isolated."
76            isolated_num=$(( $isolated_num + 1 ))
77            echo $i >> isolated_cpu1.txt
78        fi
79    done
80    if [[ $active_num -eq 2 ]] && [[ $isolated_num -eq 2 ]];then
81        tst_res TPASS "two cpus is active,and two cpus is isolated."
82    else
83        tst_res TFAIL "the cpus state error."
84        ((ret++))
85    fi
86
87    cpu_num=$(cat isolated_cpu1.txt | sed -n "1p" )
88    echo 0 > /sys/devices/system/cpu/cpu${cpu_num}/online
89    cpu_number=$(( $cpu_num + 1 ))
90    cpu_online_state=$(cat $global_state | grep 'Online:'  \
91    | sed -n "${cpu_number}p" | awk -F ':' '{print$2}')
92    if [ ${cpu_online_state} -eq 0 ];then
93        tst_res TPASS "cpu${cpu_num} is offline."
94    else
95        tst_res TFAIL "cpu${cpu_num} is online."
96        ((ret++))
97    fi
98
99    cpu_num_isolated_state=$(cat $global_state | grep 'Isolated:'  \
100    | sed -n "${cpu_number}p" | awk -F ':' '{print$2}')
101    if [ $cpu_num_isolated_state -eq 0 ]; then
102        tst_res TPASS "cpu${cpu_num} isolated state was cleaned."
103    else
104        tst_res TFAIL "cpu${cpu_num} isolated state was not cleaned."
105        ((ret++))
106    fi
107}
108
109check_movement()
110{
111    cpu_pid=0
112    for pid in $(cat taskpid.txt); do
113        if [ $(sed -n '/^cpu#${cpu_num}/,/cpu#${cpu_number}$/p' $proc_sd  \
114        | awk -F " " '{print $3}' | grep -w "$pid") ];then
115            cpu_pid=$(($cpu_pid + 1))
116        fi
117    done
118
119    if [ $cpu_pid -eq 0 ]; then
120        tst_res TPASS "cpu$cpu_num process migrated."
121    else
122        tst_res TFAIL "cpu$cpu_num process is not migrated."
123        ((ret++))
124    fi
125}
126
127isolated_cpu_online()
128{
129    echo 1 > /sys/devices/system/cpu/cpu${cpu_num}/online
130    cpu_online_state1=$(cat $global_state | grep 'Online:'  \
131    | sed -n "${cpu_number}p" | awk -F ':' '{print$2}')
132    if [ ${cpu_online_state1} -eq 1 ];then
133        tst_res TPASS "cpu${cpu_num} is online."
134    else
135        tst_res TFAIL "cpu${cpu_num} is offline."
136        ((ret++))
137    fi
138
139    cpu_num_isolated_state1=$(cat $global_state | grep 'Isolated:'  \
140    | sed -n "${cpu_number}p" | awk -F ':' '{print$2}')
141    if [ $cpu_num_isolated_state1 -eq 0 ]; then
142        tst_res TPASS "cpu${cpu_num} is active."
143    else
144        tst_res TFAIL "cpu${cpu_num} is isolated."
145        ((ret++))
146    fi
147
148    for i in $(cat active_cpu1.txt); do
149        line1=$(( $i + 1))
150        cpu_isolated_state1=$(cat $global_state | grep 'Isolated:'  \
151        | sed -n "${line1}p" | awk -F ':' '{print$2}')
152        if [ $cpu_isolated_state1 -eq 1 ]; then
153            isolated_num1=$(( $isolated_num1 + 1 ))
154        fi
155    done
156
157    if [ ${isolated_num1} -eq 1 ];then
158        tst_res TPASS "A active cpu is isolated."
159    else
160        tst_res TFAIL "the cpus state error."
161        ((ret++))
162    fi
163}
164
165active_cpu_online()
166{
167    active_num2=0
168    isolated_num2=0
169    for i in $(seq 0 3); do
170        line2=$(( $i + 1))
171        cpu_isolated_state2=$(cat $global_state | grep 'Isolated:'  \
172        | sed -n "${line2}p" | awk -F ':' '{print$2}')
173        if [ $cpu_isolated_state2 -eq 0 ]; then
174            tst_res TINFO "cpu$i is active."
175            active_num2=$(( $active_num2 + 1 ))
176            echo $active_num2
177            echo $i >> active_cpu2.txt
178        fi
179
180        if [ $cpu_isolated_state2 -eq 1 ]; then
181            tst_res TINFO "cpu$i is isolated."
182            isolated_num2=$(( $isolated_num2 + 1 ))
183            echo $isolated_num2
184            echo $i >> isolated_cpu2.txt
185        fi
186    done
187    if [[ $active_num2 -eq 2 ]] && [[ $isolated_num2 -eq 2 ]];then
188        tst_res TPASS "two cpus is active,and two cpus is isolated."
189    else
190        tst_res TFAIL "the cpus state error."
191        ((ret++))
192    fi
193    cpu_num1=$(cat active_cpu2.txt | sed -n "1p" )
194
195    echo 0 > /sys/devices/system/cpu/cpu${cpu_num1}/online
196    cpu_number1=$(( $cpu_num1 + 1 ))
197    cpu_online_state2=$(cat $global_state | grep 'Online:'  \
198    | sed -n "${cpu_number1}p" | awk -F ':' '{print$2}')
199    if [ ${cpu_online_state2} -eq 0 ];then
200        tst_res TPASS "cpu${cpu_num1} is offline."
201    else
202        tst_res TFAIL "cpu${cpu_num1} is online."
203        ((ret++))
204    fi
205
206    echo 1 > /sys/devices/system/cpu/cpu${cpu_num1}/online
207    cpu_online_state3=$(cat $global_state | grep 'Online:'  \
208    | sed -n "${cpu_number1}p" | awk -F ':' '{print$2}')
209    if [ ${cpu_online_state3} -eq 1 ];then
210        tst_res TPASS "cpu${cpu_num1} is online."
211    else
212        tst_res TFAIL "cpu${cpu_num1} is offline."
213        ((ret++))
214    fi
215
216    for i in $(cat isolated_cpu2.txt); do
217        line3=$(( $i + 1 ))
218        cpu_num_isolated_state2=$(cat $global_state | grep 'Isolated:'  \
219        | sed -n "${line3}p" | awk -F ':' '{print$2}')
220        isolated_num3=$(( $isolated_num3 + 1 ))
221    done
222    if [ $isolated_num3 -eq 2 ]; then
223        tst_res TPASS "isolated number does not change."
224    else
225        tst_res TFAIL "isolated number had been changed."
226    fi
227}
228
229do_clean()
230{
231    ps -ef | grep "create_process" | grep -v "grep" | cut -c 9-18  \
232    | xargs kill -9
233    echo "y" | rm isolated_cpu1.txt
234    echo "y" | rm active_cpu1.txt
235    echo "y" | rm isolated_cpu2.txt
236    echo "y" | rm active_cpu2.txt
237}
238
239do_setup
240do_test
241do_clean
242tst_exit
243