On Mon, Apr 27, 2015 at 03:34:24PM +0900, Taeung Song wrote:
> This patch consists of functions
> which can get, set specific config variables.
> For the syntax examples,
> 
>    perf config [options] [section.subkey[=value] ...]

not sure this gets fixed later on, but after applying
up to this patch I've got following error:

> 
>    display key-value pairs of specific config variables
>    # perf config report.queue-size report.children

[jolsa@krava perf]$ ./perf config report.queue-size
invalid key: report.queue-size[jolsa@krava perf]$ 

> 
>    set specific config variables
>    # perf config report.queue-size=100M report.children=true

[jolsa@krava perf]$ ./perf config report.queue-size=50M
invalid key: report.queue-size=50M[jolsa@krava perf]$ 

jirka

> 
> Signed-off-by: Taeung Song <treeze.tae...@gmail.com>
> ---
>  tools/perf/Documentation/perf-config.txt |   2 +
>  tools/perf/builtin-config.c              | 276 
> ++++++++++++++++++++++++++++++-
>  tools/perf/util/cache.h                  |  17 ++
>  tools/perf/util/config.c                 |  30 +++-
>  4 files changed, 320 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/perf/Documentation/perf-config.txt 
> b/tools/perf/Documentation/perf-config.txt
> index 489c2c6..b2b3321 100644
> --- a/tools/perf/Documentation/perf-config.txt
> +++ b/tools/perf/Documentation/perf-config.txt
> @@ -8,6 +8,8 @@ perf-config - Get and set variables in configuration file.
>  SYNOPSIS
>  --------
>  [verse]
> +'perf config' [section.subkey[=value] ...]
> +or
>  'perf config' -l | --list
>  
>  DESCRIPTION
> diff --git a/tools/perf/builtin-config.c b/tools/perf/builtin-config.c
> index 9e4ba72..99292f6 100644
> --- a/tools/perf/builtin-config.c
> +++ b/tools/perf/builtin-config.c
> @@ -15,10 +15,14 @@
>  
>  static struct {
>       bool list_action;
> +     bool get_action;
> +     bool set_action;
>  } params;
>  
> +struct list_head *sections;
> +
>  static const char * const config_usage[] = {
> -     "perf config [options]",
> +     "perf config [options] [section.subkey[=value] ...]",
>       NULL
>  };
>  static const struct option config_options[] = {
> @@ -27,6 +31,74 @@ static const struct option config_options[] = {
>       OPT_END()
>  };
>  
> +static struct config_section *find_config_section(const char *section_name)
> +{
> +     struct config_section *section_node;
> +     list_for_each_entry(section_node, sections, list)
> +             if (!strcmp(section_node->name, section_name))
> +                     return section_node;
> +
> +     return NULL;
> +}
> +
> +static struct config_element *find_config_element(const char *subkey,
> +                                               struct config_section 
> *section_node)
> +{
> +     struct config_element *element_node;
> +
> +     list_for_each_entry(element_node, &section_node->element_head, list)
> +             if (!strcmp(element_node->subkey, subkey))
> +                     return element_node;
> +
> +     return NULL;
> +}
> +
> +static struct config_section *init_config_section(const char *section_name)
> +{
> +     struct config_section *section_node;
> +
> +     section_node = zalloc(sizeof(*section_node));
> +     if (!section_node)
> +             return NULL;
> +
> +     INIT_LIST_HEAD(&section_node->element_head);
> +     section_node->name = strdup(section_name);
> +     if (!section_node->name) {
> +             pr_err("%s: strdup failed\n", __func__);
> +             free(section_node);
> +             return NULL;
> +     }
> +
> +     return section_node;
> +}
> +
> +static int add_config_element(struct list_head *head,
> +                           const char *subkey, const char *value)
> +{
> +     struct config_element *element_node;
> +     element_node = zalloc(sizeof(*element_node));
> +     element_node->subkey = strdup(subkey);
> +     if (!element_node->subkey) {
> +             pr_err("%s: strdup failed\n", __func__);
> +             goto out_free;
> +     }
> +     if (value) {
> +             element_node->value = strdup(value);
> +             if (!element_node->value) {
> +                     pr_err("%s: strdup failed\n", __func__);
> +                     goto out_free;
> +             }
> +     } else
> +             element_node->value = NULL;
> +
> +     list_add_tail(&element_node->list, head);
> +     return 0;
> +
> +out_free:
> +     free(element_node);
> +     return -1;
> +}
> +
>  static int show_config(const char *key, const char *value,
>                      void *cb __maybe_unused)
>  {
> @@ -38,12 +110,211 @@ static int show_config(const char *key, const char 
> *value,
>       return 0;
>  }
>  
> -int cmd_config(int argc, const char **argv, const char *prefix 
> __maybe_unused)
> +static void find_config(struct config_section **section_node, struct 
> config_element **element_node,
> +                     const char *given_section_name, const char 
> *given_subkey)
> +{
> +     *section_node = find_config_section(given_section_name);
> +
> +     if (*section_node != NULL)
> +             *element_node = find_config_element(given_subkey, 
> *section_node);
> +     else
> +             *element_node = NULL;
> +}
> +
> +static int show_spec_config(const char *section_name, const char *subkey,
> +                         const char *value __maybe_unused)
> +{
> +     struct config_section *section_node = NULL;
> +     struct config_element *element_node = NULL;
> +     char key[BUFSIZ];
> +
> +     find_config(&section_node, &element_node, section_name, subkey);
> +
> +     if (section_node && element_node) {
> +             scnprintf(key, sizeof(key), "%s.%s", section_node->name, 
> element_node->subkey);
> +             show_config(key, element_node->value, NULL);
> +     } else
> +             pr_err("Error: Failed to find the variable.\n");
> +
> +     return 0;
> +}
> +
> +static int set_config(const char *section_name, const char *subkey,
> +                   const char *value)
> +{
> +     struct config_section *section_node = NULL;
> +     struct config_element *element_node = NULL;
> +
> +     find_config(&section_node, &element_node, section_name, subkey);
> +
> +     /* if there isn't existent section, add a new section */
> +     if (!section_node) {
> +             section_node = init_config_section(section_name);
> +             if (!section_node)
> +                     return -1;
> +             list_add_tail(&section_node->list, sections);
> +     }
> +     /* if nothing to replace, add a new element which contains key-value 
> pair. */
> +     if (!element_node)
> +             return add_config_element(&section_node->element_head, subkey, 
> value);
> +     else
> +             element_node->value = (char *)value;
> +
> +     return 0;
> +}
> +
> +static int set_spec_config(const char *section_name, const char *subkey,
> +                        const char *value)
>  {
>       int ret = 0;
> +     ret += set_config(section_name, subkey, value);
> +     ret += perf_configset_write_in_full();
> +
> +     return ret;
> +}
> +
> +static void parse_key(const char *var, const char **section_name, const char 
> **subkey)
> +{
> +     char *key = strdup(var);
> +
> +     if (!key)
> +             die("%s: strdup failed\n", __func__);
> +
> +     *section_name = strsep(&key, ".");
> +     *subkey = strsep(&key, ".");
> +}
> +
> +static int collect_config(const char *var, const char *value,
> +                       void *cb __maybe_unused)
> +{
> +     struct config_section *section_node;
> +     const char *section_name, *subkey;
> +
> +     parse_key(var, &section_name, &subkey);
> +
> +     section_node = find_config_section(section_name);
> +     if (!section_node) {
> +             /* Add a new section */
> +             section_node = init_config_section(section_name);
> +             if (!section_node)
> +                     return -1;
> +             list_add_tail(&section_node->list, sections);
> +     }
> +
> +     return add_config_element(&section_node->element_head, subkey, value);
> +}
> +
> +static int perf_configset_with_option(configset_fn_t fn, const char *var)
> +{
> +     int i, num_dot = 0, num_equals = 0;
> +     static char *given_section_name;
> +     static char *given_subkey;
> +     static char *given_value;
> +     const char *last_dot;
> +     char *key = strdup(var);
> +
> +     if (!key) {
> +             pr_err("%s: strdup failed\n", __func__);
> +             return -1;
> +     }
> +     last_dot = strrchr(key, '.');
> +     /*
> +      * Since "key" actually contains the section name and the real
> +      * key name separated by a dot, we have to know where the dot is.
> +      */
> +     if (last_dot == NULL || last_dot == key) {
> +             pr_err("The config variable does not contain a section: %s\n", 
> key);
> +             return -1;
> +     }
> +     if (!last_dot[1]) {
> +             pr_err("The config varible does not contain variable name: 
> %s\n", key);
> +             return -1;
> +     }
> +     for (i = 0; key[i]; i++) {
> +             if (i == 0 && !isalpha(key[i++]))
> +                     goto out_err;
> +
> +             switch (key[i]) {
> +             case '.':
> +                     num_dot += 1;
> +                     if (!isalpha(key[++i]))
> +                             goto out_err;
> +                     break;
> +             case '=':
> +                     num_equals += 1;
> +                     break;
> +             default:
> +                     if (!isalpha(key[i]) && !isalnum(key[i]))
> +                             goto out_err;
> +             }
> +     }
> +
> +     if (num_equals > 1 || num_dot > 1)
> +             goto out_err;
> +
> +     given_value = strchr(key, '=');
> +     if (given_value == NULL || given_value == key)
> +             given_value = NULL;
> +     else {
> +             if (!given_value[1]) {
> +                     pr_err("The config variable does not contain a value: 
> %s\n", key);
> +                     return -1;
> +             } else
> +                     given_value++;
> +     }
> +     given_section_name = strsep(&key, ".");
> +     given_subkey = strsep(&key, ".");
> +
> +     if (given_value)
> +             given_subkey = strsep(&given_subkey, "=");
> +
> +     if (!sections) {
> +             sections = zalloc(sizeof(*sections));
> +             if (!sections)
> +                     return -1;
> +
> +             INIT_LIST_HEAD(sections);
> +             perf_config(collect_config, NULL);
> +     }
> +
> +     return fn(given_section_name, given_subkey, given_value);
> +
> +out_err:
> +     pr_err("invalid key: %s", var);
> +     return -1;
> +}
> +
> +int cmd_config(int argc, const char **argv, const char *prefix 
> __maybe_unused)
> +{
> +     int i, ret = 0;
> +     int origin_argc = argc - 1;
> +     char *value;
> +     bool is_option;
>  
>       argc = parse_options(argc, argv, config_options, config_usage,
>                            PARSE_OPT_STOP_AT_NON_OPTION);
> +     if (origin_argc > argc)
> +             is_option = true;
> +     else
> +             is_option = false;
> +
> +     if (!is_option && argc >= 0) {
> +             switch (argc) {
> +             case 0:
> +                     break;
> +             default:
> +                     for (i = 0; argv[i]; i++) {
> +                             value = strrchr(argv[i], '=');
> +                             if (value == NULL || value == argv[i])
> +                                     ret = 
> perf_configset_with_option(show_spec_config, argv[i]);
> +                             else
> +                                     ret = 
> perf_configset_with_option(set_spec_config, argv[i]);
> +                             if (ret < 0)
> +                                     break;
> +                     }
> +                     goto out;
> +             }
> +     }
>  
>       if (params.list_action && argc == 0)
>               ret = perf_config(show_config, NULL);
> @@ -52,5 +323,6 @@ int cmd_config(int argc, const char **argv, const char 
> *prefix __maybe_unused)
>               usage_with_options(config_usage, config_options);
>       }
>  
> +out:
>       return ret;
>  }
> diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h
> index fbcca21..d333753 100644
> --- a/tools/perf/util/cache.h
> +++ b/tools/perf/util/cache.h
> @@ -1,6 +1,7 @@
>  #ifndef __PERF_CACHE_H
>  #define __PERF_CACHE_H
>  
> +#include <linux/list.h>
>  #include <stdbool.h>
>  #include "util.h"
>  #include "strbuf.h"
> @@ -19,6 +20,22 @@
>  #define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR"
>  #define PERF_TRACEFS_ENVIRONMENT "PERF_TRACEFS_DIR"
>  
> +struct config_element {
> +     char *subkey;
> +     char *value;
> +     struct list_head list;
> +};
> +
> +struct config_section {
> +     char *name;
> +     struct list_head element_head;
> +     struct list_head list;
> +};
> +
> +extern struct list_head *sections;
> +
> +typedef int (*configset_fn_t)(const char *, const char *, const char*);
> +extern int perf_configset_write_in_full(void);
>  typedef int (*config_fn_t)(const char *, const char *, void *);
>  extern int perf_default_config(const char *, const char *, void *);
>  extern int perf_config(config_fn_t fn, void *);
> diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
> index e18f653..71294ef 100644
> --- a/tools/perf/util/config.c
> +++ b/tools/perf/util/config.c
> @@ -21,7 +21,7 @@
>  char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */
>  
>  static FILE *config_file;
> -static const char *config_file_name;
> +static char *config_file_name;
>  static int config_linenr;
>  static int config_file_eof;
>  
> @@ -420,12 +420,11 @@ static int perf_config_from_file(config_fn_t fn, const 
> char *filename, void *dat
>       ret = -1;
>       if (f) {
>               config_file = f;
> -             config_file_name = filename;
> +             config_file_name = strdup(filename);
>               config_linenr = 1;
>               config_file_eof = 0;
>               ret = perf_parse_file(fn, data);
>               fclose(f);
> -             config_file_name = NULL;
>       }
>       return ret;
>  }
> @@ -502,6 +501,31 @@ out:
>       return ret;
>  }
>  
> +int perf_configset_write_in_full(void)
> +{
> +     struct config_section *section_node;
> +     struct config_element *element_node;
> +     const char *first_line = "# this file is auto-generated.";
> +     FILE *fp = fopen(config_file_name, "w");
> +
> +     if (!fp)
> +             return -1;
> +
> +     fprintf(fp, "%s\n", first_line);
> +     /* overwrite configvariables */
> +     list_for_each_entry(section_node, sections, list) {
> +             fprintf(fp, "[%s]\n", section_node->name);
> +             list_for_each_entry(element_node, &section_node->element_head, 
> list) {
> +                     if (element_node->value)
> +                             fprintf(fp, "\t%s = %s\n",
> +                                     element_node->subkey, 
> element_node->value);
> +             }
> +     }
> +     fclose(fp);
> +
> +     return 0;
> +}
> +
>  /*
>   * Call this to report error for your variable that should not
>   * get a boolean value (i.e. "[my] var" means "true").
> -- 
> 1.9.1
> 
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to