Re: [PATCH 2/4] perf tools: Add functions which can get or set perf config variables.

From: Jiri Olsa
Date: Mon May 04 2015 - 15:16:30 EST


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] ...]
>
> display key-value pairs of specific config variables
> # perf config report.queue-size report.children

[jolsa@krava perf]$ ./perf config krava.krava
krava.krava=true

?

some comments below

>
> set specific config variables
> # perf config report.queue-size=100M report.children=true
>
> Signed-off-by: Taeung Song <treeze.taeung@xxxxxxxxx>
> ---
> 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(-)

SNIP

> +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, ".");

should this check the config syntax? could be used for command line check as well

> +}
> +
> +static int collect_config(const char *var, const char *value,
> + void *cb __maybe_unused)
> +{
> + struct config_section *section_node;
> + const char *section_name, *subkey;

SNIP

> + }
> + 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;

you dont allow '-' in the key report.queue-size, I think we should support also _

also please put the name checks into separated function

> + }
> + }
> +
> + if (num_equals > 1 || num_dot > 1)
> + goto out_err;
> +
> + given_value = strchr(key, '=');
> + if (given_value == NULL || given_value == key)
> + given_value = NULL;

SNIP

> 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])

hum, so you let go in args like '=krava' ?

why dont you completely check the name (assignment string) first
and decide later about the callback

> + 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;
> + }
> + }

SNIP

> @@ -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.";

so you parse whole config, change it and write back..
hum, I dont see better way.. and I like the first line ;-)

> + 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 majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/