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