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