xref: /aosp_15_r20/external/ltp/testcases/kernel/controllers/cgroup_fj/cgroup_fj_stress.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2009 FUJITSU LIMITED
4# Copyright (c) 2015 Cedric Hnyda <[email protected]>
5# Copyright (c) 2015-2016 Cyril Hrubis <[email protected]>
6# Author: Shi Weihua <[email protected]>
7
8TCID="cgroup_fj_stress"
9TST_CNT=1
10TST_TESTFUNC=do_test
11TST_SETUP=setup
12TST_CLEANUP=cleanup
13TST_POS_ARGS=4
14
15subsystem="$1"
16subgroup_num="$2"
17subgroup_depth="$3"
18attach_operation="$4"
19
20usage_and_exit()
21{
22    echo "usage of cgroup_fj_stress.sh: "
23    echo "  ./cgroup_fj_stress.sh subsystem subgroup_num subgroup_depth attach_operation"
24    echo "    subgroup_num"
25    echo "      number of subgroups created in group"
26    echo "    subgroup_depth"
27    echo "      depth of the created tree"
28    echo "    attach_operation"
29    echo "      none - do not attach anything"
30    echo "      one  - move one processe around"
31    echo "      each - attach process to each subgroup"
32    echo "example: ./cgroup_fj_stress.sh cpuset 1 1 one"
33    echo
34    tst_brk TBROK "$1"
35}
36
37build_subgroups()
38{
39    local cur_path="$1"
40    local cur_depth="$2"
41    local i
42
43    if [ "$cur_depth" -gt "$subgroup_depth" ]; then
44        return
45    fi
46
47    create_subgroup "$cur_path"
48
49    # We can only attach processes to the leaves of the tree in cgroup v2 which
50    # means we need to enable the controllers everywhere inbetween.
51    if [ "$cgroup_version" = "2" ] && [ "$cur_depth" -ne "$subgroup_depth" ]; then
52        ROD echo "+$subsystem" \> "$cur_path/cgroup.subtree_control"
53    fi
54    count=$((count+1))
55
56    for i in $(seq 1 $subgroup_num); do
57         build_subgroups "$cur_path/$i" $((cur_depth+1))
58    done
59}
60
61attach_task()
62{
63    local cur_path="$1"
64    local cur_depth="$2"
65    local ppid="$3"
66    local i
67
68    if [ "$cur_depth" -gt "$subgroup_depth" ]; then
69        return
70    fi
71
72    if [ -z "$ppid" ]; then
73        cgroup_fj_proc&
74        pid=$!
75        collected_pids="$collected_pids $pid"
76    else
77        pid="$ppid"
78    fi
79
80    if [ "$cgroup_version" = "2" ] && [ $cur_depth -eq $subgroup_depth ] || [ "$cgroup_version" = "1" ]; then
81        if ! attach_and_check "$pid" "$cur_path"; then
82            fail=1
83        fi
84    fi
85
86    for i in $(seq 1 $subgroup_num); do
87         local new_path="$cur_path/$i"
88         attach_task "$new_path" $((cur_depth+1)) "$ppid"
89    done
90
91    if [ -n "$ppid" ]; then
92        if [ "$cgroup_version" = "2" ] && [ $cur_depth -eq $subgroup_depth ] || [ "$cgroup_version" = "1" ]; then
93            if ! attach_and_check "$pid" "$cur_path"; then
94                fail=1
95            fi
96        fi
97    fi
98}
99
100setup()
101{
102    export TMPFILE=./tmp_tasks.$$
103    count=0
104    collected_pids=
105
106    case $subgroup_num in
107        ''|*[!0-9]*) usage_and_exit "Number of subgroups must be possitive integer";;
108        *) ;;
109    esac
110
111    case $subgroup_depth in
112        ''|*[!0-9]*) usage_and_exit "Depth of the subgroup tree must be possitive integer";;
113        *) ;;
114    esac
115
116    case $attach_operation in
117        'none'|'one'|'each');;
118        *) usage_and_exit "Invalid attach operation: $attach_operation";;
119    esac
120
121    common_setup
122}
123
124cleanup()
125{
126    common_cleanup
127}
128
129do_test()
130{
131    tst_res TINFO "Creating subgroups ..."
132
133    build_subgroups "$start_path" 0
134
135    tst_res TINFO "... mkdired $count times"
136
137    case $attach_operation in
138    "one" )
139        cgroup_fj_proc &
140        pid=$!
141
142        tst_res TINFO "Moving one task around"
143        attach_task "$start_path" 0 "$pid"
144        ROD kill -9 "$pid"
145        wait "$pid"
146        ;;
147    "each" )
148        tst_res TINFO "Attaching task to each subgroup"
149        attach_task "$start_path" 0
150        for pid in $collected_pids; do
151            ROD kill -9 "$pid"
152            wait "$pid"
153        done
154        ;;
155    *  )
156        ;;
157    esac
158
159    if [ -n "$fail" ]; then
160        tst_res TFAIL "Attaching tasks failed!"
161    else
162        tst_res TPASS "All done!"
163    fi
164}
165
166. cgroup_fj_common.sh
167tst_run
168