[PATCH v7 5/7] perf config: Add 'set' feature

From: Taeung Song
Date: Sun Oct 04 2015 - 04:41:06 EST


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

Signed-off-by: Taeung Song <treeze.taeung@xxxxxxxxx>
---
tools/perf/Documentation/perf-config.txt | 2 +-
tools/perf/builtin-config.c | 122 ++++++++++++++++++++++++++-----
tools/perf/util/cache.h | 4 +-
tools/perf/util/config.c | 27 +++++++
4 files changed, 133 insertions(+), 22 deletions(-)

diff --git a/tools/perf/Documentation/perf-config.txt b/tools/perf/Documentation/perf-config.txt
index 7b83406..f24926e 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 731056c..f034c19 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
};

@@ -453,7 +453,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 __maybe_unused)
{
int i;
struct config_section *section = NULL;
@@ -481,6 +483,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 __maybe_unused)
{
@@ -526,8 +561,10 @@ static int collect_current_config(const char *var, const char *value,
return 0;
}

-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)
{
char *section_name;
char *name;
@@ -554,15 +591,31 @@ static int perf_configset_with_option(configset_fn_t fn, struct list_head *secti

section_name = strsep(&key, ".");
name = strsep(&key, ".");
+ if (!value) {
+ /* do nothing */
+ } else if (!strcmp(value, "=")) {
+ pr_err("The config variable does not contain a value: %s.%s\n",
+ section_name, name);
+ return -1;
+ } else {
+ value++;
+ name = strsep(&name, "=");
+ if (name[0] == '\0') {
+ pr_err("invalid key: %s\n", var);
+ return -1;
+ }
+ }
free(key);

- return fn(sections, section_name, name);
+ return fn(sections, config_file_name, section_name, name, value);
}

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 *config_file_name;
const char *system_config = perf_etc_perfconfig();
char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));

@@ -579,18 +632,29 @@ int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
return -1;
}

- INIT_LIST_HEAD(&sections);
-
- if (use_system_config || (!use_system_config && !use_user_config))
- perf_config_from_file(collect_current_config, system_config, &sections);
-
- if (use_user_config || (!use_system_config && !use_user_config))
- perf_config_from_file(collect_current_config, user_config, &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) {
+ sections = &system_sections;
+ config_file_name = system_config;
+ } else if (use_user_config) {
+ sections = &user_sections;
+ config_file_name = user_config;
+ } else {
+ INIT_LIST_HEAD(&all_sections);
+ sections = &all_sections;
+ config_file_name = user_config;
+ perf_config_from_file(collect_current_config, system_config, &all_sections);
+ perf_config_from_file(collect_current_config, user_config, &all_sections);
+ }

switch (actions) {
case ACTION_LIST_ALL:
if (argc == 0) {
- ret = show_all_config(&sections);
+ ret = show_all_config(sections);
break;
}
case ACTION_LIST:
@@ -603,12 +667,30 @@ int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
return -1;
}
default:
- if (argc)
- for (i = 0; argv[i]; i++)
- ret = perf_configset_with_option(show_spec_config, &sections,
- argv[i]);
- else
- ret = show_config(&sections);
+
+ 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_file_name,
+ argv[i], value);
+ }
+ }
+ } else
+ ret = show_config(sections);
}

return ret;
diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h
index e9bb75e..0723cdf 100644
--- a/tools/perf/util/cache.h
+++ b/tools/perf/util/cache.h
@@ -33,7 +33,9 @@ struct config_section {
};

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);
diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c
index 23fa8c5..ad539d9 100644
--- a/tools/perf/util/config.c
+++ b/tools/perf/util/config.c
@@ -506,6 +506,33 @@ 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) {
+ pr_err("Error: %s: Can't open this file\n", file_name);
+ 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").
--
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/