xref: /aosp_15_r20/external/ltp/testcases/kernel/controllers/cgroup_fj/cgroup_fj_common.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
8for arg; do
9    TCID="${TCID}_$arg"
10done
11
12TST_NEEDS_CMDS="rmdir killall"
13TST_NEEDS_ROOT=1
14TST_NEEDS_TMPDIR=1
15
16attach_and_check()
17{
18    local pid="$1"
19    local path="$2"
20    local task
21    shift
22
23    tst_res TINFO "Attaching task $pid to $path"
24
25    ROD echo "$pid" \> "$path/$task_list"
26
27    for task in $(cat "$path/$task_list"); do
28        if [ "$task" -ne "$pid" ]; then
29            tst_res TINFO "Unexpected pid $task in $path/$task_list, expected $pid"
30            return 1
31        fi
32    done
33
34    return 0
35}
36
37create_subgroup()
38{
39    path="$1"
40
41    [ ! -d "$path" ] && ROD mkdir "$path"
42
43    # cpuset.cpus and cpuset.mems must be initialized with suitable value
44    # before any pids are attached.
45    # Only needs to be done for cgroup v1 as sets are inherited from parents
46    # by default in cgroup v2.
47    if [ "$cgroup_version" = "1" ] && [ "$subsystem" = "cpuset" ]; then
48        if [ -e "$mount_point/cpus" ]; then
49            ROD cat "$mount_point/cpus" \> "$path/cpus"
50            ROD cat "$mount_point/mems" \> "$path/mems"
51        else
52            ROD cat "$mount_point/cpuset.cpus" \> "$path/cpuset.cpus"
53            ROD cat "$mount_point/cpuset.mems" \> "$path/cpuset.mems"
54        fi
55    fi
56}
57
58common_setup()
59{
60    cgroup_require "$subsystem"
61    mount_point=$(cgroup_get_mountpoint "$subsystem")
62    start_path=$(cgroup_get_test_path "$subsystem")
63    cgroup_version=$(cgroup_get_version "$subsystem")
64    task_list=$(cgroup_get_task_list "$subsystem")
65
66    [ "$cgroup_version" = "2" ] && ROD echo "+$subsystem" \> "$start_path/cgroup.subtree_control"
67    tst_res TINFO "test starts with cgroup version $cgroup_version"
68}
69
70common_cleanup()
71{
72    killall -9 cgroup_fj_proc >/dev/null 2>&1
73
74    tst_res TINFO "Removing all ltp subgroups..."
75
76    [ -d "$start_path" ] && find "$start_path" -depth -type d -exec rmdir '{}' \;
77
78    cgroup_cleanup
79}
80
81. cgroup_lib.sh
82