Expand Cgroup shell library
cover Expand Cgroup shell library
This patchset aims to expand the cgroup_lib shell library to simplify and centralize the whole mounting and cleanup process that, with all the different versions and mounting schemes, can get confusing and rather redundant.
So the aim here is to centralize all the functionality to the already existing cgroup C API which handles a lot of the corner cases and complexity while still getting to use it from a shell environment which some developers probably prefer to write their tests in. This also seems important to make it easier to reconfigure the current tests to work under cgroup v2 controllers, as well as for developers to write tests for cgroup v2 controllers for which there doesen't seem to be very much coverage at the moment.
Luke Nowakowski-Krijger (6):
API/cgroup: Modify tst_cgroup_print_config for easier parsing and
consumption
API/cgroup: Add cgroup_find_root helper function API/cgroup: Add option for specific pid to tst_cgroup_opts API/cgroup: Implement tst_cgroup_load_config()
tools: Implement tst_cgctl binary utility
controllers: Expand cgroup_lib shell library
include/tst_cgroup.h | 7 +- lib/tst_cgroup.c | 165 ++++++++++++++++++++- testcases/kernel/controllers/cgroup_lib.sh | 129 ++++++++++++++-- tools/cgroup/Makefile | 7 + tools/cgroup/tst_cgctl.c | 69 +++++++++ 5 files changed, 355 insertions(+), 22 deletions(-) create mode 100644 tools/cgroup/Makefile create mode 100644 tools/cgroup/tst_cgctl.c
Signature · 5 lines
2.32.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 1/6 API/cgroup: Modify tst_cgroup_print_config for easier parsing and consumption
Prepare tst_cgroup_print_config to be more easily parsed and consumed by shell scripts and other programs.
Also add any detected root information as well as the relevant state associated with the roots that would be needed by the test to properly cleanup.
--- lib/tst_cgroup.c | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-)
lib/tst_cgroup.c +34 -4
@@ -300,12 +300,27 @@ opendir: O_PATH | O_DIRECTORY); } +#define CONFIG_LTPDIR_KEY "Created_Ltp_Dir" +#define CONFIG_MOUNTROOT_KEY "Mounted_Root" +#define CONFIG_DRAINDIR_KEY "Created_Drain_Dir" +#define CONFIG_TESTID_KEY "Test_Id" +#define CONFIG_CTRL_REQUIRED "Required" +#define CONFIG_CTRL_HEADER "Detected Controllers:" +#define CONFIG_ROOT_HEADER "Detected Roots:" + +/* Prints out the minimal internal state of the test to be consumed +* by tst_cgroup_load_config(). +* +* The config keeps track of the minimal state needed for tst_cgroup_cleanup +* to cleanup after a test and does not keep track of the creation of +* test cgroups that might be created through tst_cgroup_group_mk(). +*/ void tst_cgroup_print_config (void ) {struct cgroup_root *root;const struct cgroup_ctrl *ctrl; -tst_res (TINFO ,"Detected Controllers:" ); +printf ("%s\n" ,CONFIG_CTRL_HEADER );for_each_ctrl (ctrl) { root = ctrl->ctrl_root;
@@ -313,11 +328,26 @@ void tst_cgroup_print_config(void)if (!root)continue ; -tst_res (TINFO ,"\t%.10s %s @ %s:%s" , +printf ("%s %s @ %s %s\n" , ctrl->ctrl_name, - root->no_cpuset_prefix ?"[noprefix]" :"" , root->ver ==TST_CGROUP_V1 ?"V1" :"V2" , - root->mnt_path); + root->mnt_path, + ctrl->we_require_it ?CONFIG_CTRL_REQUIRED :"" ); + } + +printf ("%s\n" ,CONFIG_ROOT_HEADER ); + +for_each_root (root) { +printf ("%s %s=%s %s=%s %s=%s %s=%s\n" , + root->mnt_path, +CONFIG_MOUNTROOT_KEY , + root->we_mounted_it ?"yes" :"no" , +CONFIG_LTPDIR_KEY , + root->ltp_dir.we_created_it ?"yes" :"no" , +CONFIG_DRAINDIR_KEY , + root->drain_dir.we_created_it ?"yes" :"no" , +CONFIG_TESTID_KEY , + root->test_dir.dir_name ? root->test_dir.dir_name :"NULL" ); } }
Signature · 5 lines
2.32.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 2/6 API/cgroup: Add cgroup_find_root helper function
In reply to Luke Nowakowski-KrijgerAdd a helper function similar to cgroup_find_ctrl to make matching paths to roots more convenient.
--- lib/tst_cgroup.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
lib/tst_cgroup.c +13 -0
@@ -365,6 +365,19 @@ static struct cgroup_ctrl *cgroup_find_ctrl(const char *const ctrl_name)return ctrl; } +static struct cgroup_root *cgroup_find_root (const char *const mnt_path) +{ +struct cgroup_root *root = roots; + +while (root->ver &&strcmp (root->mnt_path, mnt_path)) + root++; + +if (!root->ver) + root =NULL ; + +return root; +} +/* Determine if a mounted cgroup hierarchy is unique and record it if so. * * For CGroups V2 this is very simple as there is only one
Signature · 5 lines
2.32.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 2/6] API/cgroup: Add cgroup_find_root helper function
Quoted 19 lines · Luke Nowakowski-Krijger
Add a helper function similar to
cgroup_find_ctrlto make matching paths to roots more convenient.Signed-off-by Luke Nowakowski-Krijger <[email protected]>--- lib/tst_cgroup.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)lib/tst_cgroup.c +2
-0@@ -365,6 +365,19 @@ static struct cgroup_ctrl *cgroup_find_ctrl(const char *const ctrl_name)return ctrl; } +static struct cgroup_root *cgroup_find_root (const char *const mnt_path) +{
lib/tst_cgroup.c static struct cgroup_ctrl *cgroup_find_ctrl(const char *const ctrl_name) · line 379
Quoted 10 lines
lib/tst_cgroup.c +10
-0+struct cgroup_root *root = roots; + +while (root->ver &&strcmp (root->mnt_path, mnt_path)) + root++; + +if (!root->ver) + root =NULL ; + +return root; +}
I suggest using for_each_root() macro here, otherwise as I commented it can't parse root in V1.
static struct cgroup_root *cgroup_find_root(const char *const mnt_path)
{
struct cgroup_root *root;
for_each_root(root) {
if (!strcmp(root->mnt_path, mnt_path))
return root;
}
return NULL;
}Quoted 11 lines
lib/tst_cgroup.c +1
-1+/* Determine if a mounted cgroup hierarchy is unique and record it if so. * * For CGroups V2 this is very simple as there is only one -- 2.32.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 6 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 3/6 API/cgroup: Add option for specific pid to tst_cgroup_opts
In reply to Luke Nowakowski-KrijgerAdd an option that would allow to create a test directory with a specified pid, as opposed to the calling processes pid.
--- include/tst_cgroup.h | 3 +++ lib/tst_cgroup.c | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-)
include/tst_cgroup.h +3 -0
@@ -98,6 +98,9 @@ struct tst_cgroup_opts {* only indicates that we should mount V1 controllers if * nothing is present. By default we try to mount V2 first. */ int only_mount_v1:1; +/* Pass in a specific pid to create and identify the test +* directory as opposed to the default pid of the calling process. */ +int test_pid; };/* A Control Group in LTP's aggregated hierarchy */
lib/tst_cgroup.c +5 -1
@@ -713,7 +713,11 @@ mkdirs:cgroup_dir_mk (&root->ltp_dir, cgroup_ltp_drain_dir, &root->drain_dir); -sprintf (cgroup_test_dir,"test-%d" ,getpid ()); +if (options->test_pid) +sprintf (cgroup_test_dir,"test-%d" , options->test_pid); +else +sprintf (cgroup_test_dir,"test-%d" ,getpid ()); +cgroup_dir_mk (&root->ltp_dir, cgroup_test_dir, &root->test_dir); }
Signature · 5 lines
2.32.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 4/6 API/cgroup: Implement tst_cgroup_load_config()
In reply to Luke Nowakowski-KrijgerImplement tst_cgroup_load_config which consumes the state given by tst_cgroup_print_config() to update the internal test state to reflect the given config.
This allows for programs using the cgroup C API to load and reload state, allowing functionality such as calling tst_cgroup_require and tst_cgroup_cleanup to function properly between programs or between invocations of a binary using the C API.
--- include/tst_cgroup.h | 4 +- lib/tst_cgroup.c | 108 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 1 deletion(-)
include/tst_cgroup.h +3 -1
@@ -112,7 +112,9 @@ struct tst_cgroup_group;void tst_cgroup_scan (void );/* Print the config detected by tst_cgroup_scan */ void tst_cgroup_print_config (void ); - +/* Load the config printed by tst_cgroup_print_config() to update the +* the internal state of the test to the given config */ +void tst_cgroup_load_config (const char *const config);/* Ensure the specified controller is available in the test's default * CGroup, mounting/enabling it if necessary */ void tst_cgroup_require(const char *const ctrl_name,
lib/tst_cgroup.c +108 -0
114 lines not on this page · raw message
Signature · 5 lines
2.32.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 4/6] API/cgroup: Implement tst_cgroup_load_config()
Quoted 20 lines · Luke Nowakowski-Krijger
lib/tst_cgroup.c +20
-0+static int parse_root_config (char *config_entry) +{ +char *key; +char *value; +struct cgroup_root *root; + +for (key =strtok (config_entry," " ); key; key =strtok (NULL ," " )) { +if (!(value =strchr (key,'=' ))) { +if (!(root =cgroup_find_root (key))) +tst_brk (TBROK ,"Could not find root from config. Roots changing between calls?" ); + +continue ; + } + + *value ='\0' ; + value = value +1 ; + +if (!strcmp (key,CONFIG_MOUNTROOT_KEY ) && !strcmp (value,"yes" )) { + root->we_mounted_it =1 ; +
lib/tst_cgroup.c static struct cgroup_root *cgroup_find_root(const char *const mnt_path) · line 432
Quoted 10 lines
lib/tst_cgroup.c +9
-0+ }else if (!strcmp (key,CONFIG_LTPDIR_KEY ) && !root->ltp_dir.dir_name) { +cgroup_dir_mk (&root->mnt_dir, cgroup_ltp_dir, &root->ltp_dir); +if (!strcmp (value,"yes" )) + root->ltp_dir.we_created_it =1 ; + + }else if (!strcmp (key,CONFIG_DRAINDIR_KEY ) && !root->drain_dir.dir_name) { +cgroup_dir_mk (&root->ltp_dir, cgroup_ltp_drain_dir, &root->drain_dir); +if (!strcmp (value,"yes" )) + root->ltp_dir.we_created_it =1 ;
I think that parsing the CONFIG_DRAINDIR_KEY from '$_cgroup_state' is superfluous. Because from the tst_cgroup_cleanup, if root->ltp_dir.we_created_it
is 1 then both of the two directories will be removed, so just using CONFIG_LTPDIR_KE
to track the status is enough.
And maybe it is not necessary to print "Created_Drain_Dir=xx" in tst_cgroup_print_config at all.
Then, the code snippet could be as:
} else if (!strcmp(key, CONFIG_LTPDIR_KEY) &&
!root->ltp_dir.dir_name) {
cgroup_dir_mk(&root->mnt_dir, cgroup_ltp_dir,
&root->ltp_dir);
cgroup_dir_mk(&root->ltp_dir,
cgroup_ltp_drain_dir, &root->drain_dir);
if (!strcmp(value, "yes"))
root->ltp_dir.we_created_it = 1;lib/tst_cgroup.c static struct cgroup_root *cgroup_find_root(const char *const mnt_path) · line 442
Quoted 10 lines
lib/tst_cgroup.c +10
-0+ + }else if (!strcmp (key,CONFIG_TESTID_KEY ) &&strcmp (value,"NULL" ) && + !root->test_dir.dir_name) { +cgroup_dir_mk (&root->ltp_dir, value, &root->test_dir); + root->test_dir.we_created_it =1 ; + } + } + +return 0 ; +}
--
Regards,
Li Wang
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 4/6] API/cgroup: Implement tst_cgroup_load_config()
Hi Li,
Quoted 40 lines · Li Wang
lib/tst_cgroup.c static struct cgroup_root *cgroup_find_root(const char *const mnt_path)
Quoted 7 lines · Luke Nowakowski-Krijger
lib/tst_cgroup.c +7
-0+static int parse_root_config(char *config_entry) +{ + char *key; + char *value; + struct cgroup_root *root; + + for (key = strtok(config_entry, " "); key; key = strtok(NULL, "")) {
lib/tst_cgroup.c static struct cgroup_root *cgroup_find_root(const char *const mnt_path)
Quoted 3 lines
lib/tst_cgroup.c +3
-0+if (!(value =strchr (key,'=' ))) { +if (!(root =cgroup_find_root (key))) + tst_brk(TBROK, "Could not find root fromconfig. Roots changing between calls?");
lib/tst_cgroup.c static struct cgroup_root *cgroup_find_root(const char *const mnt_path)
Quoted 8 lines
lib/tst_cgroup.c +8
-0+ +continue ; + } + + *value ='\0' ; + value = value +1 ; + + if (!strcmp(key, CONFIG_MOUNTROOT_KEY) && !strcmp(value,"yes")) {
lib/tst_cgroup.c static struct cgroup_root *cgroup_find_root(const char *const mnt_path)
Quoted 4 lines
lib/tst_cgroup.c +3
-0+ root->we_mounted_it =1 ; + + } else if (!strcmp(key, CONFIG_LTPDIR_KEY) &&!root->ltp_dir.dir_name) {
Quoted 1 line
+ cgroup_dir_mk(&root->mnt_dir,
cgroup_ltp_dir,
&root->ltp_dir);lib/tst_cgroup.c static struct cgroup_root *cgroup_find_root(const char *const mnt_path)
Quoted 4 lines
lib/tst_cgroup.c +4
-0+if (!strcmp (value,"yes" )) + root->ltp_dir.we_created_it =1 ; + + } else if (!strcmp(key, CONFIG_DRAINDIR_KEY) &&!root->drain_dir.dir_name) {
Quoted 1 line
+ cgroup_dir_mk(&root->ltp_dir,
cgroup_ltp_drain_dir,&root->drain_dir);Review
Quoted 2 lines
(quoted diff) +2
-0+ if (!strcmp(value, "yes")) + root->ltp_dir.we_created_it = 1;
Quoted 8 lines
I think that parsing the
CONFIG_DRAINDIR_KEYfrom '$_cgroup_state' is superfluous. Because from thetst_cgroup_cleanup, ifroot->ltp_dir.we_created_it
is 1 then both of the two directories will be removed, so just usingCONFIG_LTPDIR_KE
to track the status is enough.
This is definitely true and I think it makes it a lot simpler. I will add this in the next version.
And maybe it is not necessary to print "Created_Drain_Dir=xx" in
Quoted 19 lines
tst_cgroup_print_configat all.Then, the code snippet could be as:
} else if (!strcmp(key, CONFIG_LTPDIR_KEY) && !root->ltp_dir.dir_name) { cgroup_dir_mk(&root->mnt_dir, cgroup_ltp_dir, &root->ltp_dir); cgroup_dir_mk(&root->ltp_dir, cgroup_ltp_drain_dir, &root->drain_dir); if (!strcmp(value, "yes")) root->ltp_dir.we_created_it = 1;Review
Quoted 2 lines
(quoted diff) +2
-0+ + } else if (!strcmp(key, CONFIG_TESTID_KEY) &&
strcmp(value, "NULL")&&Review
Quoted 2 lines
(quoted diff) +2
-0+ !root->test_dir.dir_name) { + cgroup_dir_mk(&root->ltp_dir, value,
Quoted 11 lines
&root->test_dir);lib/tst_cgroup.c static struct cgroup_root *cgroup_find_root(const char *const mnt_path) · line 442
Quoted 6 lines
lib/tst_cgroup.c +6
-0+ root->test_dir.we_created_it =1 ; + } + } + +return 0 ; +}
I'll be posting the changes along with all the modified tests probably later today or early next week, and I will definitely need help reviewing that :)
Quoted 6 lines
--
Regards,
Li Wang
Thanks as always,
- Luke
Signature · 2 lines
-- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 5/6 tools: Implement tst_cgctl binary utility
In reply to Luke Nowakowski-KrijgerImplement a binary utility that creates an interface to make calls to the cgroup C API.
This will effectively allow shell scripts to make calls to the cgroup C api.
--- tools/cgroup/Makefile | 7 ++++ tools/cgroup/tst_cgctl.c | 69 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 tools/cgroup/Makefile create mode 100644 tools/cgroup/tst_cgctl.c
tools/cgroup/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +top_srcdir ?= ../.. + +include $(top_srcdir)/include/mk/testcases.mk + +MAKE_TARGETS := tst_cgctl + +include $(top_srcdir)/include/mk/generic_leaf_target.mk
tools/cgroup/tst_cgctl.c new file +69 -0
70 lines not on this page · raw message
Signature · 5 lines
2.32.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 5/6] tools: Implement tst_cgctl binary utility
Quoted 2 lines · Luke Nowakowski-Krijger
tools/cgroup/Makefile | 7 ++++ tools/cgroup/tst_cgctl.c | 69 ++++++++++++++++++++++++++++++++++++++++
Looks like putting this tst_cgctl.c in testcase/lib/ will be better, we have no necessary to create it under a bit far directory, and that tool/ is more generic for LTP, but this process is only for shell tests.
tools/cgroup/tst_cgctl.c line 24
Quoted 4 lines
tools/cgroup/tst_cgctl.c new file +4
-0+static int cgctl_cleanup (const char *config) +{ +tst_cgroup_scan (); +tst_cgroup_load_config (config);
This tst_cgroup_load_config() does not work as expected. From my manual check, the ltp and drain dir have been created but it prints " Created_Ltp_Dir=no Created_Drain_Dir=no" strings.
...
Detected Roots:/sys/fs/cgroup/memory Mounted_Root=no Created_Ltp_Dir=no Created_Drain_Dir=no Test_Id=test-1801
...
Quoted 1 line
+
tst_cgroup_cleanup();
This does not work as expected too, but the problem should exist in previous patches. Anyway, I will look into the details tomorrow.
# ./tst_cgctl cleanup "$_cgroup_state" tst_cgroup.c:414: TBROK: Could not find root from config. Roots changing between calls?
tools/cgroup/tst_cgctl.c line 19
Quoted 3 lines
tools/cgroup/tst_cgctl.c new file +3
-0+ +return 0 ; +}
--
Regards,
Li Wang
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 5/6] tools: Implement tst_cgctl binary utility
Hi Li,
Quoted 11 lines · Li Wang
Quoted 2 lines · Luke Nowakowski-Krijger
tools/cgroup/Makefile | 7 ++++ tools/cgroup/tst_cgctl.c | 69 ++++++++++++++++++++++++++++++++++++++++Looks like putting this
tst_cgctl.cin testcase/lib/ will be better, we have no necessary to create it under a bit far directory, and that tool/ is more generic for LTP, but this process is only for shell tests.
Yeah I will move it over here. I was not sure exactly where it should go.
Quoted 9 lines
+static int
cgctl_cleanup(const char *config)tools/cgroup/tst_cgctl.c line 24
Quoted 3 lines
tools/cgroup/tst_cgctl.c new file +3
-0+{ +tst_cgroup_scan (); +tst_cgroup_load_config (config);This
tst_cgroup_load_config()does not work as expected. From my manual check, the ltp and drain dir have been created but it prints " Created_Ltp_Dir=no Created_Drain_Dir=no" strings.
Quoted 8 lines
...
Detected Roots:/sys/fs/cgroup/memoryMounted_Root=no Created_Ltp_Dir=no Created_Drain_Dir=no Test_Id=test-1801
...
Hm, I'm not sure what this could be. Are you sure when you were reading the printed info it was in the same invocation as when it was being created? Because the tst_cgroup_print_config is just pretty directly printing out the state of the cgroup framework.
Quoted 10 lines
+
tst_cgroup_cleanup();This does not work as expected too, but the problem should exist in previous patches. Anyway, I will look into the details tomorrow.
# ./tst_cgctl cleanup "$_cgroup_state" tst_cgroup.c:414: TBROK: Could not find root from config. Roots changing between calls?
Was "/sys/fs/cgroup/cpu,cpuacct" one of the roots that was printed out? Because if so the way I have it now it would not be able to parse that. I will look into fixing this.
Quoted 10 lines
tools/cgroup/tst_cgctl.c line 19
Quoted 3 lines
tools/cgroup/tst_cgctl.c new file +3
-0+ +return 0 ; +}--
Regards,
Li Wang
Thanks again for review,
- Luke
Signature · 2 lines
-- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 5/6] tools: Implement tst_cgctl binary utility
Hi Luke,
Quoted 17 lines
Quoted 9 lines
tools/cgroup/tst_cgctl.c line 24
Quoted 4 lines
tools/cgroup/tst_cgctl.c new file +4
-0+static int cgctl_cleanup (const char *config) +{ +tst_cgroup_scan (); +tst_cgroup_load_config (config);This
tst_cgroup_load_config()does not work as expected. From my manual check, the ltp and drain dir have been created but it prints " Created_Ltp_Dir=no Created_Drain_Dir=no" strings.Quoted 7 lines
...
Detected Roots:/sys/fs/cgroup/memoryMounted_Root=no Created_Ltp_Dir=no Created_Drain_Dir=no Test_Id=test-1801
...
Quoted 1 line
Hm, I'm not sure what this could be. Are you sure when you were reading the printed info it was in the same invocation as when it was being created? Because the
tst_cgroup_print_configis just pretty directly printing out the state of the cgroup framework.
I have got the reason, because I manually run Cgroup test but did not allow it to do cleanup so leaves the ltp/ and drain/ dir there, so it will not be recorded as "yes" in Created_Ltp_dir next time, that behavior is correct.
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
+
tst_cgroup_cleanup();This does not work as expected too, but the problem should exist in previous patches. Anyway, I will look into the details tomorrow.
Quoted 6 lines
Quoted 4 lines
# ./tst_cgctl cleanup "$_cgroup_state" tst_cgroup.c:414: TBROK: Could not find root from config. Roots changing between calls?Was "
/sys/fs/cgroup/cpu,cpuacct" one of the roots that was printed out? Because if so the way I have it now it would not be able to parse that. I will look into fixing this.
Yes, it was, I just cut the message to show the problem.
Detected Controllers:
memory V1 @ /sys/fs/cgroup/memory Required
cpu V1 @ /sys/fs/cgroup/cpu,cpuacct
cpuset V1 @ /sys/fs/cgroup/cpuset
Detected Roots:/sys/fs/cgroup/memory Mounted_Root=no Created_Ltp_Dir=no Created_Drain_Dir=no Test_Id=test-5091 /sys/fs/cgroup/cpu,cpuacct Mounted_Root=no Created_Ltp_Dir=no Created_Drain_Dir=no Test_Id=NULL /sys/fs/cgroup/cpuset Mounted_Root=no Created_Ltp_Dir=no Created_Drain_Dir=no Test_Id=NULL
From my test, the progress works well on Cgroup V2, but now fails on parsing the root config in V1. I suspect there is a bug in patch 4/6, but still checking why the "roots" is NULL at that moment.
Signature · 6 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp
PATCH 6/6 controllers: Expand cgroup_lib shell library
In reply to Luke Nowakowski-KrijgerExpand the cgroup_lib library by using the tools/cgroup/tst_cgctl binary utility to make calls to the Cgroup C API to simplify and centralize the mounting and cleanup process of Cgroups.
--- testcases/kernel/controllers/cgroup_lib.sh | 129 ++++++++++++++++++--- 1 file changed, 113 insertions(+), 16 deletions(-)
testcases/kernel/controllers/cgroup_lib.sh +113 -16
@@ -5,22 +5,7 @@ . tst_test.sh -# Find mountpoint to given subsystem -# get_cgroup_mountpoint SUBSYSTEM -# RETURN: 0 if mountpoint found, otherwise 1 -get_cgroup_mountpoint() -{ - local subsystem=$1 - local mntpoint - - [ $# -eq 0 ] && tst_brk TBROK "get_cgroup_mountpoint: subsystem not defined" - - mntpoint=$(grep cgroup /proc/mounts | grep -w $subsystem | awk '{ print $2 }') - [ -z "$mntpoint" ] && return 1 - - echo $mntpoint - return 0 -} +_cgroup_state= # Check if given subsystem is supported and enabled # is_cgroup_subsystem_available_and_enabled SUBSYSTEM
@@ -37,3 +22,115 @@ is_cgroup_subsystem_available_and_enabled() return 1 } + +# Find mountpoint of the given controller +# USAGE: cgroup_get_mountpoint CONTROLLER +# RETURNS: Prints the mountpoint of the given controller +# Must call cgroup_require before calling +cgroup_get_mountpoint() +{ + local ctrl=$1 + local mountpoint + + [ $# -eq 0 ] && tst_brk TBROK "cgroup_get_mountpoint: controller not defined" + [ "$_cgroup_state" = "" ] && tst_brk TBROK "cgroup_get_mountpoint: No previous state found. Forgot to call cgroup_require?" + + mountpoint=$(echo "$_cgroup_state" | grep -w "$ctrl" | awk '{ print $4 }') + echo "$mountpoint" + + return 0 +} + +# Get the test path of a given controller that has been created by the cgroup C API +# USAGE: cgroup_get_test_path CONTROLLER +# RETURNS: Prints the path to the test direcory +# Must call cgroup_require before calling +cgroup_get_test_path() +{ + local ctrl="$1" + local mountpoint + local test_path + + [ $# -eq 0 ] && tst_brk TBROK "cgroup_get_test_path: controller not defined" + [ "$_cgroup_state" = "" ] && tst_brk TBROK "cgroup_get_test_path: No previous state found. Forgot to call cgroup_require?" + + mountpoint=$(cgroup_get_mountpoint "$ctrl") + + test_path="$mountpoint/ltp/test-$$" + + [ ! -d "$test_path" ] && tst_brk TBROK "cgroup_get_test_path: No test path found. Forgot to call cgroup_require?" + + echo "$test_path" + + return 0 +} + +# Gets the cgroup version of the given controller +# USAGE: cgroup_get_version CONTROLLER +# RETURNS: "V1" if version 1 and "V2" if version 2 +# Must call cgroup_require before calling +cgroup_get_version() +{ + local ctrl="$1" + + [ $# -eq 0 ] && tst_brk TBROK "cgroup_get_version: controller not defined" + [ "$_cgroup_state" = "" ] && tst_brk TBROK "cgroup_get_version: No previous state found. Forgot to call cgroup_require?" + + version=$(echo "$_cgroup_state" | grep -w "$ctrl" | awk '{ print $2 }') + [ "$version" = "" ] && tst_brk TBROK "cgroup_get_version: Could not find controller $ctrl" + + echo "$version" + + return 0 +} + +# Cleans up any setup done by calling cgroup_require. +# USAGE: cgroup_cleanup +# Can be safely called even when no setup has been done +cgroup_cleanup() +{ + [ "$_cgroup_state" = "" ] && return 0 + + tst_cgctl cleanup "$_cgroup_state" + + return 0 +} + +# Get the task list of the given controller +# USAGE: cgroup_get_task_list CONTROLLER +# RETURNS: prints out "cgroup.procs" if version 2 otherwise "tasks" +# Must call cgroup_require before calling +cgroup_get_task_list() +{ + local ctrl="$1" + local version + + version=$(cgroup_get_version "$ctrl") + + if [ "$version" = "V2" ]; then + echo "cgroup.procs" + else + echo "tasks" + fi + + return 0 +} + +# Mounts and configures the given controller +# USAGE: cgroup_require CONTROLLER +cgroup_require() +{ + local ctrl="$1" + + [ $# -eq 0 ] && tst_brk TBROK "cgroup_require: controller not defined" + + if ! is_cgroup_subsystem_available_and_enabled "$ctrl"; then + tst_brk TBROK "cgroup_require: Controller not available or not enabled" + fi + + _cgroup_state=$(tst_cgctl require "$ctrl" $$) + + [ "$_cgroup_state" = "" ] && tst_brk TBROK "cgroup_require: No state was set after call. Controller '$ctrl' maybe does not exist?" + + return 0 +}
Signature · 5 lines
2.32.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 6/6] controllers: Expand cgroup_lib shell library
Quoted 15 lines · Luke Nowakowski-Krijger
Expand the
cgroup_liblibrary by using thetools/cgroup/tst_cgctlbinary utility to make calls to the Cgroup C API to simplify and centralize the mounting and cleanup process of Cgroups.Signed-off-by Luke Nowakowski-Krijger <[email protected]>--- testcases/kernel/controllers/cgroup_lib.sh | 129 ++++++++++++++++++--- 1 file changed, 113 insertions(+), 16 deletions(-)testcases/kernel/controllers/cgroup_lib.sh +0
-0
testcases/kernel/controllers/cgroup_lib.sh line 11
Quoted 7 lines
testcases/kernel/controllers/cgroup_lib.sh +0
-4@@ -5,22 +5,7 @@ . tst_test.sh -# Find mountpoint to given subsystem -# get_cgroup_mountpoint SUBSYSTEM -# RETURN: 0 if mountpoint found, otherwise 1 -get_cgroup_mountpoint()
As we have renamed this function to cgroup_get_mountpoint(), so the invoke part in test cases should be updated too. Maybe you're going to complete that in following up patches, but as a completed patch set, we'd better keep the code not broken in running.
cgroup/cgroup_regression_test.sh:
cpu_subsys_path=$(get_cgroup_mountpoint "cpu")cgroup/cgroup_regression_test.sh: local
subsys_path=$(get_cgroup_mountpoint $subsys)cpuset/cpuset_regression_test.sh:
root_cpuset_dir=$(get_cgroup_mountpoint cpuset)
Btw, it would be great you can write simple test demos to verify the new shell API works well.
Just like what people done at: ltp/lib/newlib_tests/shell/*
Quoted 78 lines
testcases/kernel/controllers/cgroup_lib.sh +59
-12-{ - local subsystem=$1 - local mntpoint - - [ $# -eq 0 ] && tst_brk TBROK "get_cgroup_mountpoint: subsystem not defined" - - mntpoint=$(grep cgroup /proc/mounts | grep -w $subsystem | awk '{ print $2 }') - [ -z "$mntpoint" ] && return 1 - - echo $mntpoint - return 0 -} +_cgroup_state= # Check if given subsystem is supported and enabled # is_cgroup_subsystem_available_and_enabled SUBSYSTEM@@ -37,3 +22,115 @@ is_cgroup_subsystem_available_and_enabled() return 1 } + +# Find mountpoint of the given controller +# USAGE: cgroup_get_mountpoint CONTROLLER +# RETURNS: Prints the mountpoint of the given controller +# Must call cgroup_require before calling +cgroup_get_mountpoint() +{ + local ctrl=$1 + local mountpoint + + [ $# -eq 0 ] && tst_brk TBROK "cgroup_get_mountpoint: controller not defined" + [ "$_cgroup_state" = "" ] && tst_brk TBROK "cgroup_get_mountpoint: No previous state found. Forgot to call cgroup_require?" + + mountpoint=$(echo "$_cgroup_state" | grep -w "$ctrl" | awk '{ print $4 }') + echo "$mountpoint" + + return 0 +} + +# Get the test path of a given controller that has been created by the cgroup C API +# USAGE: cgroup_get_test_path CONTROLLER +# RETURNS: Prints the path to the test direcory +# Must call cgroup_require before calling +cgroup_get_test_path() +{ + local ctrl="$1" + local mountpoint + local test_path + + [ $# -eq 0 ] && tst_brk TBROK "cgroup_get_test_path: controller not defined" + [ "$_cgroup_state" = "" ] && tst_brk TBROK "cgroup_get_test_path: No previous state found. Forgot to call cgroup_require?" + + mountpoint=$(cgroup_get_mountpoint "$ctrl") + + test_path="$mountpoint/ltp/test-$$" + + [ ! -d "$test_path" ] && tst_brk TBROK "cgroup_get_test_path: No test path found. Forgot to call cgroup_require?" + + echo "$test_path" + + return 0 +} + +# Gets the cgroup version of the given controller +# USAGE: cgroup_get_version CONTROLLER +# RETURNS: "V1" if version 1 and "V2" if version 2 +# Must call cgroup_require before calling +cgroup_get_version() +{ + local ctrl="$1" + + [ $# -eq 0 ] && tst_brk TBROK "cgroup_get_version: controller not defined" + [ "$_cgroup_state" = "" ] && tst_brk TBROK "cgroup_get_version: No previous state found. Forgot to call cgroup_require?" + + version=$(echo "$_cgroup_state" | grep -w "$ctrl" | awk '{ print $2 }') + [ "$version" = "" ] && tst_brk TBROK "cgroup_get_version: Could not find controller $ctrl" + + echo "$version"
testcases/kernel/controllers/cgroup_lib.sh is_cgroup_subsystem_available_and_enabled() · line 92
Quoted 10 lines
testcases/kernel/controllers/cgroup_lib.sh +10
-0+ + return 0 +} + +# Cleans up any setup done by calling cgroup_require. +# USAGE: cgroup_cleanup +# Can be safely called even when no setup has been done +cgroup_cleanup() +{ + [ "$_cgroup_state" = "" ] && return 0
?> +
testcases/kernel/controllers/cgroup_lib.sh is_cgroup_subsystem_available_and_enabled() · line 94
Quoted 1 line
testcases/kernel/controllers/cgroup_lib.sh +1
-0+ tst_cgctl cleanup "$_cgroup_state"
I don't understand what exactly the $_cgroup_state do here, isn't that just 0 or 1 which returned by 'tst_cgctl require "$ctrl" $$' ? Maybe you can add brief comments on this variable.
Quoted 21 lines
testcases/kernel/controllers/cgroup_lib.sh +21
-0+ + return 0 +} + +# Get the task list of the given controller +# USAGE: cgroup_get_task_list CONTROLLER +# RETURNS: prints out "cgroup.procs" if version 2 otherwise "tasks" +# Must call cgroup_require before calling +cgroup_get_task_list() +{ + local ctrl="$1" + local version + + version=$(cgroup_get_version "$ctrl") + + if [ "$version" = "V2" ]; then + echo "cgroup.procs" + else + echo "tasks" + fi +
testcases/kernel/controllers/cgroup_lib.sh is_cgroup_subsystem_available_and_enabled() · line 125
Quoted 10 lines
testcases/kernel/controllers/cgroup_lib.sh +10
-0+ return 0 +} + +# Mounts and configures the given controller +# USAGE: cgroup_require CONTROLLER +cgroup_require() +{ + local ctrl="$1" + + [ $# -eq 0 ] && tst_brk TBROK "cgroup_require: controller not defined"
We have already do parameter "$1" checking inis_cgroup_subsystem_avalibale_and_enbale,
so here looks reductant.
And, I think we don't need to invoke that independent function at any other place
if we put that in this cgroup_require() function. It might be good to delete is_cgroup_subsystem_avalibale_and_enbale and move the process to here.
testcases/kernel/controllers/cgroup_lib.sh is_cgroup_subsystem_available_and_enabled() · line 128
Quoted 3 lines
testcases/kernel/controllers/cgroup_lib.sh +3
-0+ + if ! is_cgroup_subsystem_available_and_enabled "$ctrl"; then + tst_brk TBROK "cgroup_require: Controller not available or not enabled"
Maybe using TCONF here is more elegent.
Quoted 15 lines
testcases/kernel/controllers/cgroup_lib.sh +8
-1+ fi + + _cgroup_state=$(tst_cgctl require "$ctrl" $$) + + [ "$_cgroup_state" = "" ] && tst_brk TBROK "cgroup_require: No state was set after call. Controller '$ctrl' maybe does not exist?" + + return 0 +} --2.32.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 6 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 6/6] controllers: Expand cgroup_lib shell library
Quoted 8 lines
testcases/kernel/controllers/cgroup_lib.sh is_cgroup_subsystem_available_and_enabled() · line 92
Quoted 3 lines
testcases/kernel/controllers/cgroup_lib.sh +3
-0+cgroup_cleanup() +{ + [ "$_cgroup_state" = "" ] && return 0?> +
Quoted 1 line
+
tst_cgctlcleanup "$_cgroup_state"I don't understand what exactly the $_cgroup_state do here, isn't that just 0 or 1 which returned by '
tst_cgctlrequire "$ctrl" $$' ?
Ah, I got it, that comes from a string printed by tst_cgroup_print_config. (Sorry I was read these codes from top to down.)
Signature · 6 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 6/6] controllers: Expand cgroup_lib shell library
In reply to Luke Nowakowski-KrijgerQuoted 44 lines
testcases/kernel/controllers/cgroup_lib.sh +44
-0+# Find mountpoint of the given controller +# USAGE: cgroup_get_mountpoint CONTROLLER +# RETURNS: Prints the mountpoint of the given controller +# Must call cgroup_require before calling +cgroup_get_mountpoint() +{ + local ctrl=$1 + local mountpoint + + [ $# -eq 0 ] && tst_brk TBROK "cgroup_get_mountpoint: controller not defined" + [ "$_cgroup_state" = "" ] && tst_brk TBROK "cgroup_get_mountpoint: No previous state found. Forgot to call cgroup_require?" + + mountpoint=$(echo "$_cgroup_state" | grep -w "$ctrl" | awk '{ print $4 }') + echo "$mountpoint" + + return 0 +} + +# Get the test path of a given controller that has been created by the cgroup C API +# USAGE: cgroup_get_test_path CONTROLLER +# RETURNS: Prints the path to the test direcory +# Must call cgroup_require before calling +cgroup_get_test_path() +{ + local ctrl="$1" + local mountpoint + local test_path + + [ $# -eq 0 ] && tst_brk TBROK "cgroup_get_test_path: controller not defined" + [ "$_cgroup_state" = "" ] && tst_brk TBROK "cgroup_get_test_path: No previous state found. Forgot to call cgroup_require?" + + mountpoint=$(cgroup_get_mountpoint "$ctrl") + + test_path="$mountpoint/ltp/test-$$" + + [ ! -d "$test_path" ] && tst_brk TBROK "cgroup_get_test_path: No test path found. Forgot to call cgroup_require?" + + echo "$test_path" + + return 0 +} + +# Gets the cgroup version of the given controller +# USAGE: cgroup_get_version CONTROLLER
testcases/kernel/controllers/cgroup_lib.sh is_cgroup_subsystem_available_and_enabled() · line 79
Quoted 10 lines
testcases/kernel/controllers/cgroup_lib.sh +10
-0+# RETURNS: "V1" if version 1 and "V2" if version 2 +# Must call cgroup_require before calling +cgroup_get_version() +{ + local ctrl="$1" + + [ $# -eq 0 ] && tst_brk TBROK "cgroup_get_version: controller not defined" + [ "$_cgroup_state" = "" ] && tst_brk TBROK "cgroup_get_version: No previous state found. Forgot to call cgroup_require?" + + version=$(echo "$_cgroup_state" | grep -w "$ctrl" | awk '{ print $2 }')
This won't work on my x86_64 KVM platform, maybe we need: grep -w "^$ctrl".
# echo $$ 1801
# ./tst_cgctl require memory 1801 Detected Controllers: memory V1 @ /sys/fs/cgroup/memory Required cpu V1 @ /sys/fs/cgroup/cpu,cpuacct cpuset V1 @ /sys/fs/cgroup/cpuset Detected Roots: /sys/fs/cgroup/memory Mounted_Root=no Created_Ltp_Dir=no Created_Drain_Dir=no Test_Id=test-1801 /sys/fs/cgroup/cpu,cpuacct Mounted_Root=no Created_Ltp_Dir=no Created_Drain_Dir=no Test_Id=NULL /sys/fs/cgroup/cpuset Mounted_Root=no Created_Ltp_Dir=no Created_Drain_Dir=no Test_Id=NULL
# _cgroup_state=$(./tst_cgctl require memory 1801)
# echo "$_cgroup_state" | grep -w "memory" | awk '{ print $2 }'
V1
Mounted_Root=no# echo "$_cgroup_state" | grep -w "memory" | awk '{ print $4 }'
/sys/fs/cgroup/memory
Created_Drain_Dir=no# ./tst_cgctl cleanup "$_cgroup_state" tst_cgroup.c:414: TBROK: Could not find root from config. Roots changing between calls?
Quoted 7 lines
testcases/kernel/controllers/cgroup_lib.sh +7
-0+ [ "$version" = "" ] && tst_brk TBROK "cgroup_get_version: Could not find controller $ctrl" + + echo "$version" + + return 0 +} +
Signature · 6 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp
Re: [LTP] [PATCH 6/6] controllers: Expand cgroup_lib shell library
Hi Li,
Quoted 100 lines · Li Wang
testcases/kernel/controllers/cgroup_lib.sh is_cgroup_subsystem_available_and_enabled()
Quoted 10 lines
testcases/kernel/controllers/cgroup_lib.sh +10
-0+# Find mountpoint of the given controller +# USAGE: cgroup_get_mountpoint CONTROLLER +# RETURNS: Prints the mountpoint of the given controller +# Must call cgroup_require before calling +cgroup_get_mountpoint() +{ + local ctrl=$1 + local mountpoint + + [ $# -eq 0 ] && tst_brk TBROK "cgroup_get_mountpoint: controllernot defined"
Quoted 1 line
+ [ "$_cgroup_state" = "" ] &&
tst_brkTBROK"
cgroup_get_mountpoint: No previous state found. Forgot to callcgroup_require?"Review
Quoted 2 lines
(quoted diff) +2
-0+ + mountpoint=$(echo "$_cgroup_state" | grep -w "$ctrl" | awk '{print $4 }')
testcases/kernel/controllers/cgroup_lib.sh is_cgroup_subsystem_available_and_enabled()
Quoted 6 lines
testcases/kernel/controllers/cgroup_lib.sh +6
-0+ echo "$mountpoint" + + return 0 +} + +# Get the test path of a given controller that has been created by thecgroup C API
testcases/kernel/controllers/cgroup_lib.sh is_cgroup_subsystem_available_and_enabled()
Quoted 10 lines
testcases/kernel/controllers/cgroup_lib.sh +10
-0+# USAGE: cgroup_get_test_path CONTROLLER +# RETURNS: Prints the path to the test direcory +# Must call cgroup_require before calling +cgroup_get_test_path() +{ + local ctrl="$1" + local mountpoint + local test_path + + [ $# -eq 0 ] && tst_brk TBROK "cgroup_get_test_path: controllernot defined"
Quoted 1 line
+ [ "$_cgroup_state" = "" ] &&
tst_brkTBROK"
cgroup_get_test_path: No previous state found. Forgot to callcgroup_require?"testcases/kernel/controllers/cgroup_lib.sh is_cgroup_subsystem_available_and_enabled()
Quoted 6 lines
testcases/kernel/controllers/cgroup_lib.sh +6
-0+ + mountpoint=$(cgroup_get_mountpoint "$ctrl") + + test_path="$mountpoint/ltp/test-$$" + + [ ! -d "$test_path" ] && tst_brk TBROK "cgroup_get_test_path: Notest path found. Forgot to call
cgroup_require?"Quoted 5 lines
testcases/kernel/controllers/cgroup_lib.sh +5
-0+ + echo "$test_path" + + return 0 +}testcases/kernel/controllers/cgroup_lib.sh is_cgroup_subsystem_available_and_enabled()
Quoted 10 lines
testcases/kernel/controllers/cgroup_lib.sh +10
-0+ +# Gets the cgroup version of the given controller +# USAGE: cgroup_get_version CONTROLLER +# RETURNS: "V1" if version 1 and "V2" if version 2 +# Must call cgroup_require before calling +cgroup_get_version() +{ + local ctrl="$1" + + [ $# -eq 0 ] && tst_brk TBROK "cgroup_get_version: controllernot defined"
Quoted 1 line
+ [ "$_cgroup_state" = "" ] &&
tst_brkTBROK "cgroup_get_version:No previous state found. Forgot to call
cgroup_require?"Review
Quoted 2 lines
(quoted diff) +2
-0+ + version=$(echo "$_cgroup_state" | grep -w "$ctrl" | awk '{ print$2 }')
This won't work on my
x86_64KVM platform, maybe we need: grep -w "^$ctrl".# echo $$ 1801# ./tst_cgctl require memory 1801 Detected Controllers: memory V1 @ /sys/fs/cgroup/memory Required cpu V1 @ /sys/fs/cgroup/cpu,cpuacct cpuset V1 @ /sys/fs/cgroup/cpuset Detected Roots: /sys/fs/cgroup/memory Mounted_Root=no Created_Ltp_Dir=no Created_Drain_Dir=no Test_Id=test-1801 /sys/fs/cgroup/cpu,cpuacct Mounted_Root=no Created_Ltp_Dir=no Created_Drain_Dir=no Test_Id=NULL /sys/fs/cgroup/cpuset Mounted_Root=no Created_Ltp_Dir=no Created_Drain_Dir=no Test_Id=NULL# _cgroup_state=$(./tst_cgctl require memory 1801)
# echo "$_cgroup_state" | grep -w "memory" | awk '{ print $2 }' V1 Mounted_Root=no# echo "$_cgroup_state" | grep -w "memory" | awk '{ print $4 }' /sys/fs/cgroup/memory Created_Drain_Dir=no# ./tst_cgctl cleanup "$_cgroup_state" tst_cgroup.c:414: TBROK: Could not find root from config. Roots changing between calls?
Quoted 10 lines
Quoted 1 line
+ [ "$version" = "" ] &&
tst_brkTBROK "cgroup_get_version: Couldnot find controller $ctrl"
testcases/kernel/controllers/cgroup_lib.sh is_cgroup_subsystem_available_and_enabled() · line 86
Quoted 6 lines
testcases/kernel/controllers/cgroup_lib.sh +6
-0+ + echo "$version" + + return 0 +} +
Ah I see that the grepping goes awry when there is already a V1 controller which has the controller name in the mount path. Thank you for checking this, I will fix this in the next version.
Quoted 5 lines
--
Regards,
Li Wang
Thanks for the review,
- Luke
Signature · 2 lines
-- Mailing list info: https://lists.linux.it/listinfo/ltp