All of lore.kernel.org
 help / color / mirror / Atom feed
From: Taeung Song <treeze.taeung@gmail.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: linux-kernel@vger.kernel.org, Jiri Olsa <jolsa@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Ingo Molnar <mingo@kernel.org>,
	Taeung Song <treeze.taeung@gmail.com>
Subject: [PATCH v15 7/9] perf config: Add 'set' feature
Date: Thu, 11 Feb 2016 02:51:23 +0900	[thread overview]
Message-ID: <1455126685-32367-8-git-send-email-treeze.taeung@gmail.com> (raw)
In-Reply-To: <1455126685-32367-1-git-send-email-treeze.taeung@gmail.com>

This patch consists of functions
which can set specific config variables.
For the syntax examples,

    perf config [<file-option>] [options] [section.name[=value] ...]

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

Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Signed-off-by: Taeung Song <treeze.taeung@gmail.com>
---
 tools/perf/Documentation/perf-config.txt |   2 +-
 tools/perf/builtin-config.c              | 122 +++++++++++++++++++++++++------
 tools/perf/util/cache.h                  |   5 +-
 tools/perf/util/config.c                 |  27 ++++++-
 4 files changed, 131 insertions(+), 25 deletions(-)

diff --git a/tools/perf/Documentation/perf-config.txt b/tools/perf/Documentation/perf-config.txt
index c4923da..0e0482c 100644
--- a/tools/perf/Documentation/perf-config.txt
+++ b/tools/perf/Documentation/perf-config.txt
@@ -8,7 +8,7 @@ perf-config - Get and set variables in a configuration file.
 SYNOPSIS
 --------
 [verse]
-'perf config' [<file-option>] [section.name ...]
+'perf config' [<file-option>] [section.name[=value] ...]
 or
 'perf config' [<file-option>] -l | --list
 or
diff --git a/tools/perf/builtin-config.c b/tools/perf/builtin-config.c
index ad7fb79..ad50cf5 100644
--- a/tools/perf/builtin-config.c
+++ b/tools/perf/builtin-config.c
@@ -16,7 +16,7 @@
 static bool use_system_config, use_user_config;
 
 static const char * const config_usage[] = {
-	"perf config [<file-option>] [options] [section.name ...]",
+	"perf config [<file-option>] [options] [section.name[=value] ...]",
 	NULL
 };
 
@@ -405,7 +405,9 @@ static int show_all_config(struct list_head *sections)
 }
 
 static int show_spec_config(struct list_head *sections,
-			    const char *section_name, const char *name)
+			    const char *config_file_name __maybe_unused,
+			    const char *section_name, const char *name,
+			    char *value)
 {
 	int i;
 	struct config_section *section = NULL;
@@ -418,7 +420,7 @@ static int show_spec_config(struct list_head *sections,
 
 		if (!strcmp(config->section, section_name) &&
 		    !strcmp(config->name, name)) {
-			char *value = get_value(config);
+			value = get_value(config);
 
 			if (verbose)
 				printf("# %s\n", config->desc);
@@ -442,6 +444,39 @@ static int show_spec_config(struct list_head *sections,
 	return -1;
 }
 
+static int set_config(struct list_head *sections, const char *config_file_name,
+		      const char *section_name, const char *name, char *value)
+{
+	struct config_section *section = NULL;
+	struct config_element *element = NULL;
+
+	find_config(sections, &section, &element, section_name, name);
+	if (value != NULL) {
+		value = strdup(value);
+		if (!value) {
+			pr_err("%s: strdup failed\n", __func__);
+			return -1;
+		}
+
+		/* if there isn't existent section, add a new section */
+		if (!section) {
+			section = init_section(section_name);
+			if (!section)
+				return -1;
+			list_add_tail(&section->list, sections);
+		}
+		/* if nothing to replace, add a new element which contains key-value pair. */
+		if (!element) {
+			add_element(&section->element_head, name, value);
+		} else {
+			free(element->value);
+			element->value = value;
+		}
+	}
+
+	return perf_configset_write_in_full(sections, config_file_name);
+}
+
 static int collect_current_config(const char *var, const char *value,
 				  void *spec_sections)
 {
@@ -491,8 +526,10 @@ out_err:
 	return ret;
 }
 
-static int perf_configset_with_option(configset_fn_t fn, struct list_head *sections,
-				      const char *var)
+static int perf_configset_with_option(configset_fn_t fn,
+				      struct list_head *sections,
+				      const char *config_file_name,
+				      const char *var, char *value)
 {
 	int ret = -1;
 	char *ptr, *key;
@@ -519,10 +556,24 @@ static int perf_configset_with_option(configset_fn_t fn, struct list_head *secti
 	}
 
 	section_name = strsep(&ptr, ".");
-	name = ptr;
-	fn(sections, section_name, name);
-	ret = 0;
+	name = strsep(&ptr, "=");
+	if (!value) {
+		/* do nothing */
+	} else if (!strcmp(value, "=")) {
+		pr_err("The config variable does not contain a value: %s.%s\n",
+		       section_name, name);
+		goto out_err;
+	} else {
+		value++;
+		name = strsep(&name, "=");
+		if (name[0] == '\0') {
+			pr_err("invalid key: %s\n", var);
+			goto out_err;
+		}
+	}
 
+	fn(sections, config_file_name, section_name, name, value);
+	ret = 0;
 out_err:
 	free(key);
 	return ret;
@@ -561,7 +612,9 @@ static int show_config(struct list_head *sections)
 int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
 {
 	int i, ret = 0;
-	struct list_head sections;
+	struct list_head *sections;
+	struct list_head all_sections, user_sections, system_sections;
+	const char *system_config = perf_etc_perfconfig();
 	char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
 
 	set_option_flag(config_options, 'l', "list", PARSE_OPT_EXCLUSIVE);
@@ -577,14 +630,22 @@ int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
 		return -1;
 	}
 
-	INIT_LIST_HEAD(&sections);
+	INIT_LIST_HEAD(&user_sections);
+	INIT_LIST_HEAD(&system_sections);
+	perf_config_from_file(collect_current_config, user_config,  &user_sections);
+	perf_config_from_file(collect_current_config, system_config,  &system_sections);
 
-	if (use_system_config)
-		config_exclusive_filename = perf_etc_perfconfig();
-	else if (use_user_config)
+	if (use_system_config) {
+		sections = &system_sections;
+		config_exclusive_filename = system_config;
+	} else if (use_user_config) {
+		sections = &user_sections;
 		config_exclusive_filename = user_config;
-
-	perf_config(collect_current_config, &sections);
+	} else {
+		sections = &all_sections;
+		INIT_LIST_HEAD(&all_sections);
+		perf_config(collect_current_config, &all_sections);
+	}
 
 	switch (actions) {
 	case ACTION_SKEL:
@@ -595,7 +656,7 @@ int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
 		break;
 	case ACTION_LIST_ALL:
 		if (argc == 0) {
-			ret = show_all_config(&sections);
+			ret = show_all_config(sections);
 			break;
 		}
 	case ACTION_LIST:
@@ -606,7 +667,7 @@ int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
 			else
 				parse_options_usage(config_usage, config_options, "l", 1);
 		} else {
-			ret = show_config(&sections);
+			ret = show_config(sections);
 			if (ret < 0) {
 				const char * config_filename = config_exclusive_filename;
 				if (!config_exclusive_filename)
@@ -617,11 +678,28 @@ int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
 		}
 		break;
 	default:
-		if (argc)
-			for (i = 0; argv[i]; i++)
-				ret = perf_configset_with_option(show_spec_config, &sections,
-								 argv[i]);
-		else
+		if (argc) {
+			for (i = 0; argv[i]; i++) {
+				char *value = strchr(argv[i], '=');
+
+				if (value == NULL)
+					ret = perf_configset_with_option(show_spec_config,
+									 sections, NULL,
+									 argv[i], value);
+				else {
+					if (!use_system_config && !use_user_config)
+						ret = perf_configset_with_option(set_config,
+										 &user_sections,
+										 user_config,
+										 argv[i], value);
+					else
+						ret = perf_configset_with_option(set_config,
+										 sections,
+										 config_exclusive_filename,
+										 argv[i], value);
+				}
+			}
+		} else
 			usage_with_options(config_usage, config_options);
 	}
 
diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h
index d5dcc85..9614afd 100644
--- a/tools/perf/util/cache.h
+++ b/tools/perf/util/cache.h
@@ -39,9 +39,12 @@ struct config_section {
 extern const char *config_exclusive_filename;
 
 typedef int (*config_fn_t)(const char *, const char *, void *);
-typedef int (*configset_fn_t)(struct list_head *, const char *, const char *);
+typedef int (*configset_fn_t)(struct list_head *, const char *,
+			      const char *, const char *, char *);
+extern int perf_configset_write_in_full(struct list_head *sections, const char *file_name);
 extern int perf_default_config(const char *, const char *, void *);
 extern int perf_config(config_fn_t fn, void *);
+extern int perf_config_from_file(config_fn_t fn, const char *filename, void *data);
 extern int perf_config_int(const char *, const char *);
 extern u64 perf_config_u64(const char *, const char *);
 extern int perf_config_bool(const char *, const char *);
diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
index 4e72763..34eff22 100644
--- a/tools/perf/util/config.c
+++ b/tools/perf/util/config.c
@@ -416,7 +416,7 @@ int perf_default_config(const char *var, const char *value,
 	return 0;
 }
 
-static int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
+int perf_config_from_file(config_fn_t fn, const char *filename, void *data)
 {
 	int ret;
 	FILE *f = fopen(filename, "r");
@@ -506,6 +506,31 @@ out:
 	return ret;
 }
 
+int perf_configset_write_in_full(struct list_head *sections, const char *file_name)
+{
+	struct config_section *section;
+	struct config_element *element;
+	const char *first_line = "# this file is auto-generated.";
+	FILE *fp = fopen(file_name, "w");
+
+	if (!fp)
+		return -1;
+
+	fprintf(fp, "%s\n", first_line);
+	/* overwrite configvariables */
+	list_for_each_entry(section, sections, list) {
+		fprintf(fp, "[%s]\n", section->name);
+		list_for_each_entry(element, &section->element_head, list) {
+			if (element->value)
+				fprintf(fp, "\t%s = %s\n",
+					element->name, element->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").
-- 
2.5.0

  parent reply	other threads:[~2016-02-10 17:51 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-10 17:51 [PATCHSET v15 0/9] perf config: Add several options into perf-config Taeung Song
2016-02-10 17:51 ` [PATCH v15 1/9] perf config: Add '--system' and '--user' options to select which config file is used Taeung Song
2016-02-16  7:51   ` [tip:perf/core] " tip-bot for Taeung Song
2016-02-10 17:51 ` [PATCH v15 2/9] perf config: Collect configs to handle config variables Taeung Song
2016-02-10 17:51 ` [PATCH v15 3/9] perf config: Add 'list-all' option to perf-config Taeung Song
2016-02-10 17:51 ` [PATCH v15 4/9] perf config: Add a option 'skel' " Taeung Song
2016-02-10 17:51 ` [PATCH v15 5/9] perf config: Add --verbose option for showing config description Taeung Song
2016-02-10 17:51 ` [PATCH v15 6/9] perf config: Add 'get' functionality Taeung Song
2016-02-10 17:51 ` Taeung Song [this message]
2016-02-10 17:51 ` [PATCH v15 8/9] perf config: normalize a value depending on default type of it Taeung Song
2016-02-10 17:51 ` [PATCH v15 9/9] perf config: Add a option 'remove' to perf-config Taeung Song

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=1455126685-32367-8-git-send-email-treeze.taeung@gmail.com \
    --to=treeze.taeung@gmail.com \
    --cc=acme@kernel.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.