lib: rewrite cgroup_find_ctrl with using for_each_ctrl
PATCH 1/1 lib: rewrite cgroup_find_ctrl with using for_each_ctrl
It is safe to start from controllers[0] to traverse each of the controller whatever V2 or V1, then we can make use of it in the cgroup_find_ctrl() function.
--- lib/tst_cgroup.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
lib/tst_cgroup.c +7 -8
@@ -214,7 +214,7 @@ static const char *cgroup_v2_ltp_mount = "unified";#define for_each_v1_root (r) \ for ((r) = roots + 1; (r)->ver; (r)++)#define for_each_ctrl (ctrl) \ - for ((ctrl) = controllers + 1; (ctrl)->ctrl_name; (ctrl)++) + for ((ctrl) = controllers; (ctrl)->ctrl_name; (ctrl)++)/* In all cases except one, this only loops once. *
@@ -325,15 +325,14 @@ void tst_cgroup_print_config(void) __attribute__ ((nonnull, warn_unused_result))static struct cgroup_ctrl *cgroup_find_ctrl (const char *const ctrl_name) { -struct cgroup_ctrl *ctrl = controllers; - -while (ctrl->ctrl_name &&strcmp (ctrl_name, ctrl->ctrl_name)) - ctrl++; +struct cgroup_ctrl *ctrl; -if (!ctrl->ctrl_name) - ctrl =NULL ; +for_each_ctrl (ctrl) { +if (!strcmp (ctrl_name, ctrl->ctrl_name)) +return ctrl; + } -return ctrl; +return NULL ; }/* Determine if a mounted cgroup hierarchy is unique and record it if so.
Signature · 5 lines
2.31.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hello Li,
Quoted 3 lines · Li Wang
It is safe to start from controllers[0] to traverse each of the controller whatever V2 or V1, then we can make use of it in the
cgroup_find_ctrl()function.
Right, it seems we never set ctrl_root on "cgroup" nor is it added to ctrl_field. So it will be skipped in other loops. This might not be what people expect, but I'm not sure what to do about that.
Quoted 31 lines
Signed-off-by Li Wang <[email protected]>--- lib/tst_cgroup.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)lib/tst_cgroup.c +2
-7@@ -214,7 +214,7 @@ static const char *cgroup_v2_ltp_mount = "unified";#define for_each_v1_root (r) \ for ((r) = roots + 1; (r)->ver; (r)++)#define for_each_ctrl (ctrl) \ - for ((ctrl) = controllers + 1; (ctrl)->ctrl_name; (ctrl)++) + for ((ctrl) = controllers; (ctrl)->ctrl_name; (ctrl)++)/* In all cases except one, this only loops once. * @@ -325,15 +325,14 @@ void tst_cgroup_print_config(void) __attribute__ ((nonnull, warn_unused_result))static struct cgroup_ctrl *cgroup_find_ctrl (const char *const ctrl_name) { -struct cgroup_ctrl *ctrl = controllers; - -while (ctrl->ctrl_name &&strcmp (ctrl_name, ctrl->ctrl_name)) - ctrl++; +struct cgroup_ctrl *ctrl; -if (!ctrl->ctrl_name) - ctrl =NULL ;
lib/tst_cgroup.c void tst_cgroup_print_config(void) · line 338
Quoted 10 lines
lib/tst_cgroup.c +5
-1+for_each_ctrl (ctrl) { +if (!strcmp (ctrl_name, ctrl->ctrl_name)) +return ctrl; + } -return ctrl; +return NULL ; }/* Determine if a mounted cgroup hierarchy is unique and record it if so.
Nice simplification!
Signature · 5 lines
Thank you, Richard. -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 7 lines · Richard Palethorpe
Quoted 3 lines
It is safe to start from controllers[0] to traverse each of the controller whatever V2 or V1, then we can make use of it in the
cgroup_find_ctrl()function.Right, it seems we never set
ctrl_rooton "cgroup" nor is it added toctrl_field. So it will be skipped in other loops. This might not be what people expect, but I'm not sure what to do about that.
Yes, but that's fine. It's because of the difference between V1 and V2.
Quoted 6 lines
Quoted 2 lines
/* Determine if a mounted cgroup hierarchy is unique and record it if so.
Nice simplification!
Reviewed-by Richard Palethorpe <[email protected]>
Thanks for the review, I will merge this after the new release.
Signature · 6 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp
Hi Li,
LGTM.
Kind regards,
Petr
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
Hi all,
Patch merged, thanks!
Quoted 9 lines · Petr Vorel
Hi Li,
LGTM.
Reviewed-by Petr Vorel <[email protected]>Kind regards,
Petr
Signature · 6 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp