On Thu, Jun 04, 2009 at 04:43:38PM +0200, Ivana Varekova wrote:
> This patch adds cgset tool, which sets parameters of controllers
> for given cgroup based on input name-variable pairs
> - the syntax is:
> 
> cgset -r <name=value> <relative path to cgroup>
> 
> -------------------------------------------------
> EXAMPLES:
> $ cgcreate -g cpuset:test1
> $ cgset -r cpuset.cpus=1 test
> $ cat ./test/cpuset.cpus
> 1
> 
> $ cgcreate -g cpuset:test1 -g cpuset:test2
> $ cgset -r cpuset.cpus=0 test1 test2
> $ cat /mnt/cgroups/cpuset/test1/cpuset.cpus
> 0
> $ cat /mnt/cgroups/cpuset/test2/cpuset.cpus
> 0
> 
> 

Hi Ivana,

A question. What's  the use of cgset? How it is better than plain echo?

Thanks
Vivek

> 
> 
> 
> 
> Signed-off-by: Ivana Varekova <[email protected]>
> ---
> 
>  src/tools/Makefile.am |    2 -
>  src/tools/cgset.c     |  178 
> +++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 179 insertions(+), 1 deletions(-)
>  create mode 100644 src/tools/cgset.c
> 
> diff --git a/src/tools/Makefile.am b/src/tools/Makefile.am
> index 73f9130..2b4c9e7 100644
> --- a/src/tools/Makefile.am
> +++ b/src/tools/Makefile.am
> @@ -3,7 +3,7 @@ LDADD = $(top_srcdir)/src/.libs/libcgroup.la
>  
>  if WITH_TOOLS
>  
> -bin_PROGRAMS = cgexec cgclassify cgcreate
> +bin_PROGRAMS = cgexec cgclassify cgcreate cgset
>  
>  sbin_PROGRAMS = cgconfigparser
>  
> diff --git a/src/tools/cgset.c b/src/tools/cgset.c
> new file mode 100644
> index 0000000..be234be
> --- /dev/null
> +++ b/src/tools/cgset.c
> @@ -0,0 +1,178 @@
> +#include <libcgroup.h>
> +#include <libcgroup-internal.h>
> +
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <unistd.h>
> +
> +#include "tools-common.h"
> +
> +int main(int argc, char *argv[])
> +{
> +     int ret = 0;
> +     int i;
> +     char c;
> +
> +     char *buf;
> +     char con[FILENAME_MAX];
> +     struct control_value *name_value = NULL;
> +     int nv_number = 0;
> +     int nv_max = 0;
> +
> +     struct cgroup *cgroup;
> +     struct cgroup_controller *cgc;
> +
> +     /* no parametr on input */
> +     if (argc < 2) {
> +             fprintf(stderr, "Usage is %s -r <name=value> "
> +                     "<relative path to cgroup>\n", argv[0]);
> +             return -1;
> +     }
> +
> +     /* parse arguments */
> +     while ((c = getopt(argc, argv, "r:")) > 0) {
> +             switch (c) {
> +             case 'r':
> +                     /* add name-value pair to buffer
> +                             (= name_value variable) */
> +                     if (nv_number >= nv_max) {
> +                             nv_max += CG_NV_MAX;
> +                             name_value = (struct control_value *)
> +                                     realloc(name_value,
> +                                     nv_max * sizeof(struct control_value));
> +                             if (!name_value) {
> +                                     fprintf(stderr, "cgset: "
> +                                             "not enough memory\n");
> +                                     ret = -1;
> +                                     goto err;
> +                             }
> +                     }
> +
> +                     /* parse optarg value */
> +                     /* there is necessary to input the tuple n=v */
> +                     buf = strtok(optarg, "=");
> +                     if (buf == NULL) {
> +                             fprintf(stderr, "%s: "
> +                                     "wrong parameter of option -r: %s\n",
> +                                     argv[0], optarg);
> +                             ret = -1;
> +                             goto err;
> +                     }
> +
> +                     strncpy(name_value[nv_number].name, buf, FILENAME_MAX);
> +                     name_value[nv_number].name[FILENAME_MAX-1] = '\0';
> +
> +                     buf = strtok(NULL, "=");
> +                     if (buf == NULL) {
> +                             fprintf(stderr, "%s: "
> +                                     "wrong parameter of option -r: %s\n",
> +                                     argv[0], optarg);
> +                             ret = -1;
> +                             goto err;
> +                     }
> +
> +                     strncpy(name_value[nv_number].value, buf, CG_VALUE_MAX);
> +                     name_value[nv_number].value[CG_VALUE_MAX-1] = '\0';
> +
> +                     nv_number++;
> +                     break;
> +             default:
> +                     fprintf(stderr, "%s: invalid command line option\n",
> +                             argv[0]);
> +                     ret = -1;
> +                     goto err;
> +                     break;
> +             }
> +     }
> +
> +     /* no cgroup name */
> +     if (!argv[optind]) {
> +             fprintf(stderr, "%s: no cgroup specified\n", argv[0]);
> +             ret = -1;
> +             goto err;
> +     }
> +
> +     if (nv_number == 0) {
> +             fprintf(stderr, "%s: no name-value pair was set\n", argv[0]);
> +             ret = -1;
> +             goto err;
> +     }
> +
> +     /* initialize libcgroup */
> +     ret = cgroup_init();
> +     if (ret) {
> +             fprintf(stderr, "%s: libcgroup initialization failed: %s\n",
> +                     argv[0], cgroup_strerror(ret));
> +             goto err;
> +     }
> +
> +     while (optind < argc) {
> +
> +             /* create new cgroup */
> +             cgroup = cgroup_new_cgroup(argv[optind]);
> +             if (!cgroup) {
> +                     ret = ECGFAIL;
> +                     fprintf(stderr, "%s: can't add new cgroup: %s\n",
> +                             argv[0], cgroup_strerror(ret));
> +                     ret = -1;
> +                     goto err;
> +             }
> +
> +             /* add pairs name-value to
> +                relevant controllers of this cgroup */
> +             for (i = 0; i < nv_number; i++) {
> +
> +                     if ((strchr(name_value[i].name, '.')) == NULL) {
> +                             fprintf(stderr, "%s: "
> +                                     "wrong -r  parameter (%s=%s)\n",
> +                                     argv[0], name_value[i].name,
> +                                     name_value[i].value);
> +                             ret = -1;
> +                             goto cgroup_free_err;
> +                     }
> +
> +                     strncpy(con, name_value[i].name, FILENAME_MAX);
> +                     strtok(con, ".");
> +
> +                     /* add relevant controller */
> +                     cgc = cgroup_add_controller(cgroup, con);
> +                     if (!cgc) {
> +                             fprintf(stderr, "%s: "
> +                                     "controller %s can't be add\n",
> +                                     argv[0], con);
> +                             goto cgroup_free_err;
> +                     }
> +
> +                     /* add name-value pair to this controller */
> +                     ret = cgroup_add_value_string(cgc,
> +                             name_value[i].name, name_value[i].value);
> +                     if (ret) {
> +                             fprintf(stderr, "%s: "
> +                                     "name-value pair %s=%s "
> +                                     "can't be set\n",
> +                                     argv[0], name_value[i].name,
> +                                     name_value[i].value);
> +                             goto cgroup_free_err;
> +                     }
> +             }
> +
> +             /* modify cgroup */
> +             ret = cgroup_modify_cgroup(cgroup);
> +             if (ret) {
> +                     fprintf(stderr, "%s: "
> +                             "the group can't be modified\n",
> +                             argv[0]);
> +                     goto cgroup_free_err;
> +             }
> +
> +             optind++;
> +             cgroup_free(&cgroup);
> +     }
> +cgroup_free_err:
> +     if (ret)
> +             cgroup_free(&cgroup);
> +err:
> +     free(name_value);
> +     return ret;
> +}
> 
> 
> ------------------------------------------------------------------------------
> OpenSolaris 2009.06 is a cutting edge operating system for enterprises 
> looking to deploy the next generation of Solaris that includes the latest 
> innovations from Sun and the OpenSource community. Download a copy and 
> enjoy capabilities such as Networking, Storage and Virtualization. 
> Go to: http://p.sf.net/sfu/opensolaris-get
> _______________________________________________
> Libcg-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/libcg-devel

------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing 
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Libcg-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to