diff mbox series

[07/16] testcases/lib: Implement tst_cgctl binary

Message ID 631e84014f8c9ad23cc634f6de8770998833286a.1642601554.git.luke.nowakowskikrijger@canonical.com
State Superseded
Headers show
Series Expand Cgroup lib and modify controller tests | expand

Commit Message

Luke Nowakowski-Krijger Jan. 19, 2022, 2:44 p.m. UTC
Implement 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.

Signed-off-by: Luke Nowakowski-Krijger <luke.nowakowskikrijger@canonical.com>
---
 testcases/lib/Makefile    |  2 +-
 testcases/lib/tst_cgctl.c | 69 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+), 1 deletion(-)
 create mode 100644 testcases/lib/tst_cgctl.c

Comments

Li Wang Jan. 24, 2022, 7:54 a.m. UTC | #1
On Wed, Jan 19, 2022 at 10:44 PM Luke Nowakowski-Krijger <
luke.nowakowskikrijger@canonical.com> wrote:
>
> Implement 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.
>
> Signed-off-by: Luke Nowakowski-Krijger <
luke.nowakowskikrijger@canonical.com>
> ---
>  testcases/lib/Makefile    |  2 +-
>  testcases/lib/tst_cgctl.c | 69 +++++++++++++++++++++++++++++++++++++++
>  2 files changed, 70 insertions(+), 1 deletion(-)
>  create mode 100644 testcases/lib/tst_cgctl.c
>
> diff --git a/testcases/lib/Makefile b/testcases/lib/Makefile
> index f2de0c832..f4f8c8524 100644
> --- a/testcases/lib/Makefile
> +++ b/testcases/lib/Makefile
> @@ -12,6 +12,6 @@ MAKE_TARGETS          := tst_sleep tst_random
tst_checkpoint tst_rod tst_kvcmp\
>                            tst_device tst_net_iface_prefix
tst_net_ip_prefix tst_net_vars\
>                            tst_getconf tst_supported_fs tst_check_drivers
tst_get_unused_port\
>                            tst_get_median tst_hexdump tst_get_free_pids
tst_timeout_kill\
> -                          tst_check_kconfigs
> +                          tst_check_kconfigs tst_cgctl
>
>  include $(top_srcdir)/include/mk/generic_leaf_target.mk
> diff --git a/testcases/lib/tst_cgctl.c b/testcases/lib/tst_cgctl.c
> new file mode 100644
> index 000000000..a6cf88f41
> --- /dev/null
> +++ b/testcases/lib/tst_cgctl.c
> @@ -0,0 +1,69 @@

We need to add SPDX-License-Identifier and copyright for this file.

And better to use Tabs (8 characters) instead of 4 spaces for the code
indentation:).
See:
https://www.kernel.org/doc/html/latest/process/coding-style.html#indentation

> +#include <stdio.h>
> +#include <string.h>
> +#include <stdlib.h>
> +#include <stdint.h>
> +#include <unistd.h>
> +#include "tst_cgroup.h"
> +
> +static int cgctl_require(const char *ctrl, int test_pid)
> +{
> +    struct tst_cgroup_opts opts;
> +
> +    memset(&opts, 0, sizeof(opts));
> +    opts.test_pid = test_pid;
> +
> +    tst_cgroup_require(ctrl, &opts);
> +    tst_cgroup_print_config();
> +
> +    return 0;
> +}
> +
> +static int cgctl_cleanup(const char *config)
> +{
> +    tst_cgroup_scan();
> +    tst_cgroup_load_config(config);
> +    tst_cgroup_cleanup();
> +
> +    return 0;
> +}
> +
> +static int cgctl_print(void)
> +{
> +    tst_cgroup_scan();
> +    tst_cgroup_print_config();
> +
> +    return 0;
> +}
> +
> +static int cgctl_process_cmd(int argc, char *argv[])
> +{
> +    int test_pid;
> +    const char *cmd_name = argv[1];
> +
> +    if (!strcmp(cmd_name, "require")) {
> +        test_pid = atoi(argv[3]);
> +        if (!test_pid) {
> +            fprintf(stderr, "tst_cgctl: Invalid test_pid '%s' given\n",
> +                    argv[3]);
> +            return 1;
> +        }
> +        return cgctl_require(argv[2], test_pid);
> +    } else if (!strcmp(cmd_name, "cleanup")) {
> +        return cgctl_cleanup(argv[2]);
> +    } else if (!strcmp(cmd_name, "print")) {
> +        return cgctl_print();
> +    }
> +
> +    fprintf(stderr, "tst_cgctl: Unknown command '%s' given\n", cmd_name);
> +    return 1;
> +}
> +
> +int main(int argc, char *argv[])
> +{
> +    if (argc < 2 || argc > 4) {
> +        fprintf(stderr, "tst_cgctl: Invalid number of arguements given");
> +        return 1;
> +    }

It'd be great to have a help() function to print the usage.
Something maybe looks like:

Usage: ./tst_cgctl  require|print|cleanup  ...

  # cgroup_state=$(./tst_cgctl require "$ctrl" "$pid")
  # echo "$cgroup_state"  # to print detailed controllers
  # tst_cgctl cleanup "$cgroup_state"


> +
> +    return cgctl_process_cmd(argc, argv);
> +}
> --
> 2.32.0
>


--
Regards,
Li Wang
diff mbox series

Patch

diff --git a/testcases/lib/Makefile b/testcases/lib/Makefile
index f2de0c832..f4f8c8524 100644
--- a/testcases/lib/Makefile
+++ b/testcases/lib/Makefile
@@ -12,6 +12,6 @@  MAKE_TARGETS		:= tst_sleep tst_random tst_checkpoint tst_rod tst_kvcmp\
 			   tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars\
 			   tst_getconf tst_supported_fs tst_check_drivers tst_get_unused_port\
 			   tst_get_median tst_hexdump tst_get_free_pids tst_timeout_kill\
-			   tst_check_kconfigs
+			   tst_check_kconfigs tst_cgctl
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/lib/tst_cgctl.c b/testcases/lib/tst_cgctl.c
new file mode 100644
index 000000000..a6cf88f41
--- /dev/null
+++ b/testcases/lib/tst_cgctl.c
@@ -0,0 +1,69 @@ 
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <unistd.h>
+#include "tst_cgroup.h"
+
+static int cgctl_require(const char *ctrl, int test_pid)
+{
+    struct tst_cgroup_opts opts;
+
+    memset(&opts, 0, sizeof(opts));
+    opts.test_pid = test_pid;
+
+    tst_cgroup_require(ctrl, &opts);
+    tst_cgroup_print_config();
+
+    return 0;
+}
+
+static int cgctl_cleanup(const char *config)
+{
+    tst_cgroup_scan();
+    tst_cgroup_load_config(config);
+    tst_cgroup_cleanup();
+
+    return 0;
+}
+
+static int cgctl_print(void)
+{
+    tst_cgroup_scan();
+    tst_cgroup_print_config();
+
+    return 0;
+}
+
+static int cgctl_process_cmd(int argc, char *argv[])
+{
+    int test_pid;
+    const char *cmd_name = argv[1];
+
+    if (!strcmp(cmd_name, "require")) {
+        test_pid = atoi(argv[3]);
+        if (!test_pid) {
+            fprintf(stderr, "tst_cgctl: Invalid test_pid '%s' given\n",
+                    argv[3]);
+            return 1;
+        }
+        return cgctl_require(argv[2], test_pid);
+    } else if (!strcmp(cmd_name, "cleanup")) {
+        return cgctl_cleanup(argv[2]);
+    } else if (!strcmp(cmd_name, "print")) {
+        return cgctl_print();
+    }
+
+    fprintf(stderr, "tst_cgctl: Unknown command '%s' given\n", cmd_name);
+    return 1;
+}
+
+int main(int argc, char *argv[])
+{
+    if (argc < 2 || argc > 4) {
+        fprintf(stderr, "tst_cgctl: Invalid number of arguements given");
+        return 1;
+    }
+
+    return cgctl_process_cmd(argc, argv);
+}