]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
adds new option -g to cgget command
authorIvana Hutarova Varekova <varekova@redhat.com>
Mon, 8 Feb 2010 12:32:18 +0000 (13:32 +0100)
committerBalbir Singh <balbir@linux.vnet.ibm.com>
Wed, 10 Feb 2010 11:13:37 +0000 (16:43 +0530)
adds new option -g to cgget command
* the option has one argument - controller
* cgget -g devices / returns the values of all variables of controller
devices
* the option could be used multiple times

EXAMLES:

$ ./cgget -n -g cpu -r cpuset.mems /
cpu.rt_period_us=1000000
cpu.rt_runtime_us=950000
cpu.shares=1024
cpuset.mems=0

Signed-off-by: Ivana Hutarova Varekova <varekova@redhat.com>
Signed-off-by: Balbir Singh <balbir@linux.vnet.ibm.com>
doc/man/cgget.1
src/tools/cgget.c

index 485d5620c8f24deeb4cb8b780459d6e737de2ef9..67f6befa951838c20372c680ccddf59f48220233 100644 (file)
@@ -7,7 +7,8 @@
 cgget \- print parameter(s) of given group(s)
 
 .SH SYNOPSIS
-\fBcgget\fR [\fB-n\fR] [\fB-v\fR] [\fB-r\fR <\fIname\fR>] <\fBcgroup_path\fR> ...
+\fBcgget\fR [\fB-n\fR] [\fB-v\fR] [\fB-r\fR <\fIname\fR>]
+[\fB-g\fR <\fIcontroller\fR>] <\fBcgroup_path\fR> ...
 
 .SH DESCRIPTION
 The command prints the parameter(s) of input cgroup(s).
@@ -22,6 +23,11 @@ This parameter can be used multiple times.
 defines parameter to display.
 This option can be used multiple times.
 
+.TP
+.B -g <contoller>
+defines controller which values should be displaied.
+This option can be used multiple times.
+
 .TP
 .B -n
 do not print headers, i.e. name of groups.
@@ -52,6 +58,12 @@ $ cgget -nv -r cpuset.cpus -r cpuset.mems first second
 0
 0
 0
+
+$ cgget -n -g cpu /
+cpu.rt_period_us=1000000
+cpu.rt_runtime_us=950000
+cpu.shares=1024
+
 .fi
 
 .SH SEE ALSO
index 13b77640d63587d02b483a7126829477716c6f83..d3c22de0029c6382b2e080f1673ffc6dbc77eb3e 100644 (file)
@@ -19,12 +19,36 @@ void usage(int status, char *program_name)
                        " try %s -h' for more information.\n",
                        program_name);
        else {
-               printf("Usage: %s [-nv] -r <name> [-r <name>] ... <path> ...\n",
-                       program_name);
+               printf("Usage: %s [-nv] [-r <name>] [-g <controller>]..."\
+                       "<path> ...\n", program_name);
        }
 }
 
-int display_values(char **names, int count, const char* group_name,
+int display_one_record(char *name, struct cgroup_controller *group_controller,
+       const char *program_name, int mode)
+{
+       int ret;
+       char *value = NULL;
+
+       ret = cgroup_get_value_string(group_controller, name, &value);
+       if (ret != 0) {
+               fprintf(stderr, "%s: cannot read parameter '%s' "\
+                       "from group '%s': %s\n", program_name, name,
+                       group_controller->name, cgroup_strerror(ret));
+               return ret;
+       }
+
+       if (mode & MODE_SHOW_NAMES)
+               printf("%s=", name);
+
+       printf("%s\n", value);
+
+       free(value);
+       return ret;
+}
+
+
+int display_name_values(char **names, int count, const char* group_name,
                const char *program_name, int mode)
 {
        int i;
@@ -32,10 +56,6 @@ int display_values(char **names, int count, const char* group_name,
        struct cgroup *group = NULL;
        int ret = 0;
        char *controller = NULL, *parameter = NULL;
-       char *value = NULL;
-
-       if (mode & MODE_SHOW_HEADERS)
-               printf("%s:\n", group_name);
 
        group = cgroup_new_cgroup(group_name);
        if (group == NULL) {
@@ -79,22 +99,11 @@ int display_values(char **names, int count, const char* group_name,
                        goto err;
                }
 
-               /*
-                * Finally read the parameter value.
-                */
-               ret = cgroup_get_value_string(group_controller, names[i],
-                               &value);
-               if (ret != 0) {
-                       fprintf(stderr, "%s: cannot read parameter '%s.%s' "\
-                                       "from group '%s': %s\n", program_name,
-                                       controller, parameter, group_name,
-                                       cgroup_strerror(ret));
+               /* Finally read the parameter value.*/
+               ret = display_one_record(names[i], group_controller,
+                       program_name, mode);
+               if (ret != 0)
                        goto err;
-               }
-               if (mode & MODE_SHOW_NAMES)
-                       printf("%s.%s=", controller, parameter);
-               printf("%s\n", value);
-               free(value);
        }
 err:
        if (controller)
@@ -104,14 +113,95 @@ err:
        return ret;
 }
 
+int display_controller_values(char **controllers, int count,
+               const char *group_name, const char *program_name, int mode)
+{
+       struct cgroup *group = NULL;
+       struct cgroup_controller *group_controller = NULL;
+       char *name;
+       int i, j;
+       int name_count;
+       int ret = 0;
+       int result = 0;
+
+       /* initialize group_name variable */
+       group = cgroup_new_cgroup(group_name);
+       if (group == NULL) {
+               fprintf(stderr, "%s:cannot create group '%s'\n",
+                       program_name, group_name);
+               return -1;
+       }
+
+       ret = cgroup_get_cgroup(group);
+       if (ret != 0) {
+               fprintf(stderr, "%s: cannot read group '%s': %s\n",
+                       program_name, group_name, cgroup_strerror(ret));
+       }
+
+       /* for all wanted controllers */
+       for (j = 0; j < count; j++) {
+
+               /* read the controller group data */
+               group_controller = cgroup_get_controller(group, controllers[j]);
+               if (group_controller == NULL) {
+                       fprintf(stderr, "%s: cannot find controller "\
+                               "'%s' in group '%s'\n", program_name,
+                               controllers[j], group_name);
+                       result = -1;
+               }
+
+               /* for each variable of given group print the statistic */
+               name_count = cgroup_get_value_name_count(group_controller);
+               for (i = 0; i < name_count; i++) {
+                       name = cgroup_get_value_name(group_controller, i);
+                       if (name != NULL) {
+                               ret = display_one_record(name, group_controller,
+                                       program_name, mode);
+                               if (ret)
+                                       return ret;
+                       }
+               }
+       }
+
+       cgroup_free(&group);
+       return result;
+
+}
+
+int add_record_to_buffer(int *p_number,
+       int *p_max, char ***p_records, char *new_rec)
+{
+
+       if (*p_number >= *p_max) {
+               *p_max += CG_NV_MAX;
+               *p_records = (char **) realloc(*p_records,
+                       *p_max * sizeof(char *));
+               if (!(*p_records)) {
+                       fprintf(stderr, "not enough memory\n");
+                       return -1;
+               }
+       }
+
+       (*p_records)[*p_number] = new_rec;
+       (*p_number)++;
+
+       return 0;
+}
+
 int main(int argc, char *argv[])
 {
        int ret = 0;
+       int result = 0;
        int c, i;
 
        char **names = NULL;
        int n_number = 0;
        int n_max = 0;
+
+       char **controllers = NULL;
+       int c_number = 0;
+       int c_max = 0;
+
        int mode = MODE_SHOW_NAMES | MODE_SHOW_HEADERS;
 
        /* No parameter on input? */
@@ -121,11 +211,11 @@ int main(int argc, char *argv[])
        }
 
        /* Parse arguments. */
-       while ((c = getopt(argc, argv, "r:hnv")) != -1) {
+       while ((c = getopt(argc, argv, "r:hnvg:")) != -1) {
                switch (c) {
                case 'h':
                        usage(0, argv[0]);
-                       ret = 0;
+                       result = 0;
                        goto err;
                        break;
 
@@ -141,24 +231,25 @@ int main(int argc, char *argv[])
 
                case 'r':
                        /* Add name to buffer. */
-                       if (n_number >= n_max) {
-                               n_max += CG_NV_MAX;
-                               names = (char **) realloc(names,
-                                       n_max * sizeof(char *));
-                               if (!names) {
-                                       fprintf(stderr, "%s: "
-                                               "not enough memory\n", argv[0]);
-                                       ret = -1;
-                                       goto err;
-                               }
+                       ret = add_record_to_buffer(
+                               &n_number, &n_max, &names, optarg);
+                       if (ret) {
+                               result = ret;
+                               goto err;
+                       }
+                       break;
+               case 'g':
+                       /* for each controller add all variables to list */
+                       ret = add_record_to_buffer(&c_number,
+                               &c_max, &controllers, optarg);
+                       if (ret) {
+                               result = ret;
+                               goto err;
                        }
-
-                       names[n_number] = optarg;
-                       n_number++;
                        break;
                default:
                        usage(1, argv[0]);
-                       ret = 1;
+                       result = -1;
                        goto err;
                        break;
                }
@@ -166,7 +257,7 @@ int main(int argc, char *argv[])
 
        if (!argv[optind]) {
                fprintf(stderr, "%s: no cgroup specified\n", argv[0]);
-               ret = -1;
+               result = -1;
                goto err;
        }
 
@@ -175,14 +266,33 @@ int main(int argc, char *argv[])
        if (ret) {
                fprintf(stderr, "%s: libcgroup initialization failed: %s\n",
                        argv[0], cgroup_strerror(ret));
+               result = ret;
+               goto err;
+       }
+
+       if (!argv[optind]) {
+               fprintf(stderr, "%s: no cgroup specified\n", argv[0]);
+               result = -1;
                goto err;
        }
 
        /* Parse control groups and print them .*/
        for (i = optind; i < argc; i++) {
-               ret = display_values(names, n_number, argv[i], argv[0], mode);
+
+               /* display the directory if needed */
+               if (mode & MODE_SHOW_HEADERS)
+                       printf("%s:\n", argv[i]);
+
+               ret = display_name_values(names,
+                       n_number, argv[i], argv[0], mode);
                if (ret)
-                       goto err;
+                       result = ret;
+
+               ret = display_controller_values(controllers,
+                       c_number, argv[i], argv[0], mode);
+               if (ret)
+                       result = ret;
+
                /* Separate each group with empty line. */
                if (mode & MODE_SHOW_HEADERS && i != argc-1)
                        printf("\n");
@@ -190,5 +300,5 @@ int main(int argc, char *argv[])
 
 err:
        free(names);
-       return ret;
+       return result;
 }