On Fri, Jun 19, 2009 at 05:08:13PM +0200, Ivana Varekova wrote:
> - This patch change the system of generating cgroup  to
> 1/ create one src cgroup
> 2/ for each input cgroup name cgroup_copy_cgroup
>    the name-value data from original one
> This method is better because the name-values pairs should not be
> parsed several times. And this method will be used in new coption
> --copy-from too (the src group will be find in option)
> 
> move the part in which src is generated to separate function
> 
> Signed-off-by: Ivana Varekova <[email protected]>
> ---
> 
>  src/tools/cgset.c |  117 
> ++++++++++++++++++++++++++++++++++-------------------
>  1 files changed, 74 insertions(+), 43 deletions(-)
> 
> diff --git a/src/tools/cgset.c b/src/tools/cgset.c
> index be234be..071ffac 100644
> --- a/src/tools/cgset.c
> +++ b/src/tools/cgset.c
> @@ -8,20 +8,75 @@
> 
>  #include "tools-common.h"
> 
> +struct cgroup *copy_name_value_from_rules(int nv_number,
> +                     struct control_value *name_value)

I like this function. This is very useful and should go into
wrapper.c with some minor changes. Exchange the order of arguments
passed (generally it is the array followed by the size) and pass in a
name for the cgroup as well. (And a better name :))

> +{
> +     struct cgroup *src_cgroup;
> +     struct cgroup_controller *cgc;
> +     char con[FILENAME_MAX];
> +
> +     int ret;
> +     int i;
> +
> +     /* create source cgroup */
> +     src_cgroup = cgroup_new_cgroup("tmp");
> +     if (!src_cgroup) {
> +             fprintf(stderr, "can't create cgroup: %s\n",
> +                     cgroup_strerror(ECGFAIL));
> +             goto scgroup_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, "wrong -r  parameter (%s=%s)\n",
> +                             name_value[i].name, name_value[i].value);
> +                     goto scgroup_err;
> +             }
> +
> +             strncpy(con, name_value[i].name, FILENAME_MAX);
> +             strtok(con, ".");
> +
> +             /* add relevant controller */
> +             cgc = cgroup_add_controller(src_cgroup, con);
> +             if (!cgc) {
> +                     fprintf(stderr, "controller %s can't be add\n",
> +                                     con);
> +                     goto scgroup_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, "name-value pair %s=%s can't be set\n",
> +                             name_value[i].name, name_value[i].value);
> +                     goto scgroup_err;
> +             }
> +     }
> +
> +     return src_cgroup;
> +scgroup_err:
> +     cgroup_free(&src_cgroup);
> +     return NULL;
> +}
> +
> +
>  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;
> 
> +     char src_cg_path[FILENAME_MAX];
> +     struct cgroup *src_cgroup;
>       struct cgroup *cgroup;
> -     struct cgroup_controller *cgc;
> 
>       /* no parametr on input */
>       if (argc < 2) {
> @@ -107,6 +162,10 @@ int main(int argc, char *argv[])
>               goto err;
>       }
> 
> +     src_cgroup = copy_name_value_from_rules(nv_number, name_value);
> +     if (src_cgroup == NULL)
> +             goto err;
> +
>       while (optind < argc) {
> 
>               /* create new cgroup */
> @@ -115,49 +174,18 @@ int main(int argc, char *argv[])
>                       ret = ECGFAIL;
>                       fprintf(stderr, "%s: can't add new cgroup: %s\n",
>                               argv[0], cgroup_strerror(ret));
> -                     ret = -1;
> -                     goto err;
> +                     goto cgroup_free_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;
> -                     }
> +             /* copy the values from the source cgroup to new one */
> +             ret = cgroup_copy_cgroup(cgroup, src_cgroup);
> +             if (ret != 0) {
> +                     fprintf(stderr, "%s: cgroup %s error: %s \n",
> +                             argv[0], src_cg_path, cgroup_strerror(ret));
> +                     goto cgroup_free_err;
>               }
> 
> -             /* modify cgroup */
> +             /* modify cgroup based on values of the new one */
>               ret = cgroup_modify_cgroup(cgroup);
>               if (ret) {
>                       fprintf(stderr, "%s: "
> @@ -169,9 +197,12 @@ int main(int argc, char *argv[])
>               optind++;
>               cgroup_free(&cgroup);
>       }
> +
>  cgroup_free_err:
> -     if (ret)
> +     if (cgroup)
>               cgroup_free(&cgroup);
> +     cgroup_free(&src_cgroup);
> +
>  err:
>       free(name_value);
>       return ret;
-- 
regards,
Dhaval

------------------------------------------------------------------------------
_______________________________________________
Libcg-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libcg-devel

Reply via email to