public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jiri Olsa <jolsa@redhat.com>
To: Taeung Song <treeze.taeung@gmail.com>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>,
	linux-kernel@vger.kernel.org, namhyung@kernel.org
Subject: Re: [PATCH 2/4] perf tools: Add functions which can get or set perf config variables.
Date: Mon, 4 May 2015 21:16:15 +0200	[thread overview]
Message-ID: <20150504191615.GA11223@krava> (raw)
In-Reply-To: <1430116466-14427-2-git-send-email-treeze.taeung@gmail.com>

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@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(-)

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
> 

  parent reply	other threads:[~2015-05-04 19:16 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-27  6:34 [PATCH 1/4] perf tools: Add 'perf-config' command Taeung Song
2015-04-27  6:34 ` [PATCH 2/4] perf tools: Add functions which can get or set perf config variables Taeung Song
2015-05-04  8:41   ` Jiri Olsa
2015-05-04 19:16   ` Jiri Olsa [this message]
2015-05-11  2:15     ` taeung
2015-04-27  6:34 ` [PATCH 3/4] perf tools: Add a option 'all' to perf-config Taeung Song
2015-05-04  8:56   ` Jiri Olsa
2015-05-04 19:16   ` Jiri Olsa
2015-05-04 19:16   ` Jiri Olsa
2015-05-04 19:16   ` Jiri Olsa
2015-04-27  6:34 ` [PATCH 4/4] perf tools: Add a option 'remove' " Taeung Song
2015-04-27 22:17 ` [PATCH 1/4] perf tools: Add 'perf-config' command Arnaldo Carvalho de Melo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150504191615.GA11223@krava \
    --to=jolsa@redhat.com \
    --cc=acme@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=namhyung@kernel.org \
    --cc=treeze.taeung@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox