diff options
author | Ivana Hutarova Varekova <varekova@redhat.com> | 2010-02-08 13:32:18 +0100 |
---|---|---|
committer | Balbir Singh <balbir@linux.vnet.ibm.com> | 2010-02-10 16:43:37 +0530 |
commit | 82d4499dd8626cfad839683e9abd9dc7e45ed7a1 (patch) | |
tree | ca509bb97f3b8b732840068a6e612cef550084df | |
parent | 3b85cd32d20b674325114f045cf972fb07a29079 (diff) | |
download | libcg-82d4499dd8626cfad839683e9abd9dc7e45ed7a1.tar.gz libcg-82d4499dd8626cfad839683e9abd9dc7e45ed7a1.tar.xz libcg-82d4499dd8626cfad839683e9abd9dc7e45ed7a1.zip |
adds new option -g to cgget command
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>
-rw-r--r-- | doc/man/cgget.1 | 14 | ||||
-rw-r--r-- | src/tools/cgget.c | 194 |
2 files changed, 165 insertions, 43 deletions
diff --git a/doc/man/cgget.1 b/doc/man/cgget.1 index 485d562..67f6bef 100644 --- a/doc/man/cgget.1 +++ b/doc/man/cgget.1 @@ -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). @@ -23,6 +24,11 @@ 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 diff --git a/src/tools/cgget.c b/src/tools/cgget.c index 13b7764..d3c22de 100644 --- a/src/tools/cgget.c +++ b/src/tools/cgget.c @@ -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; } |