xref: /aosp_15_r20/external/ltp/testcases/kernel/controllers/cgroup_lib.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker#!/bin/sh
2*49cdfc7eSAndroid Build Coastguard Worker# SPDX-License-Identifier: GPL-2.0-or-later
3*49cdfc7eSAndroid Build Coastguard Worker# Copyright (c) 2019-2022 Petr Vorel <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker# Copyright (c) 2018-2019 ARM Ltd. All Rights Reserved.
5*49cdfc7eSAndroid Build Coastguard Worker# Copyright (c) 2022 Canonical Ltd.
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker_cgroup_state=
8*49cdfc7eSAndroid Build Coastguard Worker
9*49cdfc7eSAndroid Build Coastguard Worker# Find mountpoint of the given controller
10*49cdfc7eSAndroid Build Coastguard Worker# USAGE: cgroup_get_mountpoint CONTROLLER
11*49cdfc7eSAndroid Build Coastguard Worker# RETURNS: Prints the mountpoint of the given controller
12*49cdfc7eSAndroid Build Coastguard Worker# Must call cgroup_require before calling
13*49cdfc7eSAndroid Build Coastguard Workercgroup_get_mountpoint()
14*49cdfc7eSAndroid Build Coastguard Worker{
15*49cdfc7eSAndroid Build Coastguard Worker	local ctrl="$1"
16*49cdfc7eSAndroid Build Coastguard Worker	local mountpoint
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker	[ "$ctrl" ] || tst_brk TBROK "cgroup_get_mountpoint: controller not defined"
19*49cdfc7eSAndroid Build Coastguard Worker	[ "$_cgroup_state" ] || tst_brk TBROK "cgroup_get_mountpoint: No previous state found. Forgot to call cgroup_require?"
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker	mountpoint=$(echo "$_cgroup_state" | grep -w "^$ctrl" | awk '{ print $4 }')
22*49cdfc7eSAndroid Build Coastguard Worker	echo "$mountpoint"
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker	return 0
25*49cdfc7eSAndroid Build Coastguard Worker}
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker# Get the test path of a given controller that has been created by the cgroup C API
28*49cdfc7eSAndroid Build Coastguard Worker# USAGE: cgroup_get_test_path CONTROLLER
29*49cdfc7eSAndroid Build Coastguard Worker# RETURNS: Prints the path to the test direcory
30*49cdfc7eSAndroid Build Coastguard Worker# Must call cgroup_require before calling
31*49cdfc7eSAndroid Build Coastguard Workercgroup_get_test_path()
32*49cdfc7eSAndroid Build Coastguard Worker{
33*49cdfc7eSAndroid Build Coastguard Worker	local ctrl="$1"
34*49cdfc7eSAndroid Build Coastguard Worker	local mountpoint
35*49cdfc7eSAndroid Build Coastguard Worker	local test_path
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker	[ "$ctrl" ] || tst_brk TBROK "cgroup_get_test_path: controller not defined"
38*49cdfc7eSAndroid Build Coastguard Worker	[ "$_cgroup_state" ] || tst_brk TBROK "cgroup_get_test_path: No previous state found. Forgot to call cgroup_require?"
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker	mountpoint=$(cgroup_get_mountpoint "$ctrl")
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker	test_path="$mountpoint/ltp/test-$$"
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker	[ ! -e "$test_path" ] && tst_brk TBROK "cgroup_get_test_path: No test path found. Forgot to call cgroup_require?"
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker	echo "$test_path"
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker	return 0
49*49cdfc7eSAndroid Build Coastguard Worker}
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker# Gets the cgroup version of the given controller
52*49cdfc7eSAndroid Build Coastguard Worker# USAGE: cgroup_get_version CONTROLLER
53*49cdfc7eSAndroid Build Coastguard Worker# RETURNS: "1" if version 1 and "2" if version 2
54*49cdfc7eSAndroid Build Coastguard Worker# Must call cgroup_require before calling
55*49cdfc7eSAndroid Build Coastguard Workercgroup_get_version()
56*49cdfc7eSAndroid Build Coastguard Worker{
57*49cdfc7eSAndroid Build Coastguard Worker	local ctrl="$1"
58*49cdfc7eSAndroid Build Coastguard Worker	local version
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker	[ "$ctrl" ] || tst_brk TBROK "cgroup_get_version: controller not defined"
61*49cdfc7eSAndroid Build Coastguard Worker	[ "$_cgroup_state" ] || tst_brk TBROK "cgroup_get_version: No previous state found. Forgot to call cgroup_require?"
62*49cdfc7eSAndroid Build Coastguard Worker
63*49cdfc7eSAndroid Build Coastguard Worker	version=$(echo "$_cgroup_state" | grep -w "^$ctrl" | awk '{ print $2 }')
64*49cdfc7eSAndroid Build Coastguard Worker	[  "$version" ] || tst_brk TBROK "cgroup_get_version: Could not find controller $ctrl"
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker	echo "$version"
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker	return 0
69*49cdfc7eSAndroid Build Coastguard Worker}
70*49cdfc7eSAndroid Build Coastguard Worker
71*49cdfc7eSAndroid Build Coastguard Worker# Cleans up any setup done by calling cgroup_require.
72*49cdfc7eSAndroid Build Coastguard Worker# USAGE: cgroup_cleanup
73*49cdfc7eSAndroid Build Coastguard Worker# Can be safely called even when no setup has been done
74*49cdfc7eSAndroid Build Coastguard Workercgroup_cleanup()
75*49cdfc7eSAndroid Build Coastguard Worker{
76*49cdfc7eSAndroid Build Coastguard Worker	[ "$_cgroup_state" ] || return 0
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker	ROD tst_cgctl cleanup "$_cgroup_state"
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker	_cgroup_state=
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker	return 0
83*49cdfc7eSAndroid Build Coastguard Worker}
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker# Get the task list of the given controller
86*49cdfc7eSAndroid Build Coastguard Worker# USAGE: cgroup_get_task_list CONTROLLER
87*49cdfc7eSAndroid Build Coastguard Worker# RETURNS: prints out "cgroup.procs" if version 2 otherwise "tasks"
88*49cdfc7eSAndroid Build Coastguard Worker# Must call cgroup_require before calling
89*49cdfc7eSAndroid Build Coastguard Workercgroup_get_task_list()
90*49cdfc7eSAndroid Build Coastguard Worker{
91*49cdfc7eSAndroid Build Coastguard Worker	local ctrl="$1"
92*49cdfc7eSAndroid Build Coastguard Worker	local version
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker	[ "$ctrl" ] || tst_brk TBROK "cgroup_get_task_list: controller not defined"
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker	version=$(cgroup_get_version "$ctrl")
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker	if [ "$version" = "2" ]; then
99*49cdfc7eSAndroid Build Coastguard Worker		echo "cgroup.procs"
100*49cdfc7eSAndroid Build Coastguard Worker	else
101*49cdfc7eSAndroid Build Coastguard Worker		echo "tasks"
102*49cdfc7eSAndroid Build Coastguard Worker	fi
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker	return 0
105*49cdfc7eSAndroid Build Coastguard Worker}
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker# Mounts and configures the given controller
108*49cdfc7eSAndroid Build Coastguard Worker# USAGE: cgroup_require CONTROLLER
109*49cdfc7eSAndroid Build Coastguard Workercgroup_require()
110*49cdfc7eSAndroid Build Coastguard Worker{
111*49cdfc7eSAndroid Build Coastguard Worker	local ctrl="$1"
112*49cdfc7eSAndroid Build Coastguard Worker	local ret
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker	[ "$ctrl" ] || tst_brk TBROK "cgroup_require: controller not defined"
115*49cdfc7eSAndroid Build Coastguard Worker
116*49cdfc7eSAndroid Build Coastguard Worker	[ ! -f /proc/cgroups ] && tst_brk TCONF "Kernel does not support control groups"
117*49cdfc7eSAndroid Build Coastguard Worker
118*49cdfc7eSAndroid Build Coastguard Worker	_cgroup_state=$(tst_cgctl require "$ctrl" $$)
119*49cdfc7eSAndroid Build Coastguard Worker	ret=$?
120*49cdfc7eSAndroid Build Coastguard Worker
121*49cdfc7eSAndroid Build Coastguard Worker	if [ $ret -eq 32 ]; then
122*49cdfc7eSAndroid Build Coastguard Worker		tst_brk TCONF "'tst_cgctl require' exited. Controller is probably not available?"
123*49cdfc7eSAndroid Build Coastguard Worker		return $ret
124*49cdfc7eSAndroid Build Coastguard Worker	fi
125*49cdfc7eSAndroid Build Coastguard Worker
126*49cdfc7eSAndroid Build Coastguard Worker	if [ $ret -ne 0 ]; then
127*49cdfc7eSAndroid Build Coastguard Worker		tst_brk TBROK "'tst_cgctl require' exited"
128*49cdfc7eSAndroid Build Coastguard Worker		return $ret
129*49cdfc7eSAndroid Build Coastguard Worker	fi
130*49cdfc7eSAndroid Build Coastguard Worker
131*49cdfc7eSAndroid Build Coastguard Worker	[ "$_cgroup_state" ] || tst_brk TBROK "cgroup_require: No state was set after call to tst_cgctl require?"
132*49cdfc7eSAndroid Build Coastguard Worker
133*49cdfc7eSAndroid Build Coastguard Worker	return 0
134*49cdfc7eSAndroid Build Coastguard Worker}
135*49cdfc7eSAndroid Build Coastguard Worker
136*49cdfc7eSAndroid Build Coastguard Worker. tst_test.sh
137