Skip to contentSkip to conversation

perf tools: Add 'perf-config' command

PATCH In review Taeung Song 2 participants 4 messages Apr 4, 2015 – Apr 7, 2015 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 perf tools: Add 'perf-config' command

The perf configuration file contain many variables which can make
the perf command's action more effective and more skilful.
But looking through state of configuration is difficult and
there's no knowing what kind of other variables except variables in perfconfig.example exist. So This patch adds 'perf-config' command with '--list' option and a document for it.

---
 tools/perf/Build                            |   1 +
 tools/perf/Documentation/perf-config.txt    | 418 ++++++++++++++++++++++++++++
 tools/perf/Documentation/perfconfig.example |  65 ++++-
 tools/perf/builtin-config.c                 |  67 +++++
 tools/perf/builtin.h                        |   1 +
 tools/perf/command-list.txt                 |   1 +
 tools/perf/perf.c                           |   1 +
 7 files changed, 543 insertions(+), 11 deletions(-)
 create mode 100644 tools/perf/Documentation/perf-config.txt
 create mode 100644 tools/perf/builtin-config.c
tools/perf/Build +1 -0
index b77370e..3c1f437 100644
@@ -1,5 +1,6 @@
 perf-y += builtin-bench.o
 perf-y += builtin-annotate.o
+perf-y += builtin-config.o
 perf-y += builtin-diff.o
 perf-y += builtin-evlist.o
 perf-y += builtin-help.o
tools/perf/Documentation/perf-config.txt new file +418 -0

418 lines not on this page · raw message

tools/perf/Documentation/perfconfig.example +54 -11

83 lines not on this page · raw message

tools/perf/builtin-config.c new file +67 -0

67 lines not on this page · raw message

tools/perf/builtin.h +1 -0
index 3688ad2..3f871b5 100644
@@ -17,6 +17,7 @@ extern int cmd_annotate(int argc, const char **argv, const char *prefix);
 extern int cmd_bench(int argc, const char **argv, const char *prefix);
 extern int cmd_buildid_cache(int argc, const char **argv, const char *prefix);
 extern int cmd_buildid_list(int argc, const char **argv, const char *prefix);
+extern int cmd_config(int argc, const char **argv, const char *prefix);
 extern int cmd_diff(int argc, const char **argv, const char *prefix);
 extern int cmd_evlist(int argc, const char **argv, const char *prefix);
 extern int cmd_help(int argc, const char **argv, const char *prefix);
tools/perf/command-list.txt +1 -0
index 00fcaf8..acc3ea7 100644
@@ -9,6 +9,7 @@ perf-buildid-cache		mainporcelain common
 perf-buildid-list		mainporcelain common
 perf-data			mainporcelain common
 perf-diff			mainporcelain common
+perf-config			mainporcelain common
 perf-evlist			mainporcelain common
 perf-inject			mainporcelain common
 perf-kmem			mainporcelain common
tools/perf/perf.c +1 -0
index b857fcb..604fa4a 100644
@@ -37,6 +37,7 @@ struct cmd_struct {
 static struct cmd_struct commands[] = {
 	{ "buildid-cache", cmd_buildid_cache, 0 },
 	{ "buildid-list", cmd_buildid_list, 0 },
+	{ "config",	cmd_config,	0 },
 	{ "diff",	cmd_diff,	0 },
 	{ "evlist",	cmd_evlist,	0 },
 	{ "help",	cmd_help,	0 },
Signature · 1 line
1.9.1
#1 lore raw

PATCH 2/2 perf tools: Add options 'get', 'set' and 'unset' to 'perf-config'.

Add 'get' option that can show value of a specific config variable and 'set' and 'unset' option that can create or replace a config variable.

---
 tools/perf/builtin-config.c | 198 ++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/cache.h     |  18 +++-
 tools/perf/util/config.c    |  35 +++++++-
 3 files changed, 247 insertions(+), 4 deletions(-)
tools/perf/builtin-config.c +198 -0
index 494bd73..b0885ff 100644
@@ -15,14 +15,25 @@
 
 static struct {
 	bool list_action;
+	bool get_action;
+	bool set_action;
+	bool unset_action;
 } params;
 
+LIST_HEAD(sections);
+static char *section_name_;
+static char *subkey_;
+static char *value_;
+
 static const char * const config_usage[] = {
 	"perf config [options]",
 	NULL
 };
 static const struct option config_options[] = {
 	OPT_BOOLEAN('l', "list", &params.list_action, "list up current configurations"),
+	OPT_BOOLEAN(0, "get", &params.get_action, "get value: section.subkey"),
+	OPT_BOOLEAN(0, "set", &params.set_action, "create or replace a variable: section.subkey value"),
+	OPT_BOOLEAN(0, "unset", &params.unset_action, "remove a variable: section.subkey"),
 	OPT_END()
 };
 
@@ -34,6 +45,72 @@ static void check_argc(int argc, int min, int max)
 	usage_with_options(config_usage, config_options);
 }
 
+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;
+	LIST_HEAD(element_head);
+
+	section_node = zalloc(sizeof(*section_node));
+	if (!section_node)
+		return NULL;
+
+	INIT_LIST_HEAD(&section_node->element_head);
+	list_splice(&element_head, &section_node->element_head);
+	section_node->name = strdup(section_name);
+	if (!section_node->name) {
+		pr_err("%s: strdup failed\n", __func__);
+		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__);
+		return -1;
+	}
+	if (value) {
+		element_node->value = strdup(value);
+		if (!element_node->value) {
+			pr_err("%s: strdup failed\n", __func__);
+			return -1;
+		}
+	} else
+		element_node->value = NULL;
+
+	list_add_tail(&element_node->list, head);
+
+	return 0;
+}
+
 static int show_config(const char *key, const char *value,
 			   void *cb __maybe_unused)
 {
@@ -45,6 +122,112 @@ static int show_config(const char *key, const char *value,
 	return 0;
 }
 
+static int show_spec_config(struct config_section *section_node
+			    , struct config_element *element_node)
+{
+	char key[BUFSIZ];
+
+	if (section_node && element_node) {
+		sprintf(key, "%s.%s", section_node->name, element_node->subkey);
+		show_config(key, element_node->value, NULL);
+	}
+
+	return 0;
+}
+
+static int set_config(struct config_section *section_node, struct config_element *element_node)
+{
+	if (!value_) {
+		/* value == NULL means unset */
+		if (section_node && element_node)
+			element_node->value = NULL;
+		else /* do nothing */
+			return 0;
+	} else {
+		/* 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)
+			add_config_element(&section_node->element_head, subkey_, value_);
+		else
+			element_node->value = strdup(value_);
+	}
+
+	perf_configset_write_in_full();
+
+	return 0;
+}
+
+static int collect_config(const char *var, const char *value
+			  , void *cb __maybe_unused)
+{
+	struct config_section *section_node;
+	char *section_name, *subkey;
+	char *key = strdup(var);
+
+	if (!key) {
+		pr_err("%s: strdup failed\n", __func__);
+		return -1;
+	}
+	section_name = strsep(&key, ".");
+	subkey = strsep(&key, ".");
+	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);
+	}
+
+	add_config_element(&section_node->element_head, subkey, value);
+
+	return 0;
+}
+
+static int perf_configset_with_option(configset_fn_t fn, const char *var, const char *value)
+{
+	struct config_section *section_node = NULL;
+	struct config_element *element_node = NULL;
+	char *key = strdup(var);
+	const char *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", key);
+		return -1;
+	}
+	if (!last_dot[1]) {
+		pr_err("The config varible does not contain variable name: %s", key);
+		return -1;
+	}
+
+	perf_config(collect_config, NULL);
+	section_name_ = strsep(&key, ".");
+	subkey_ = strsep(&key, ".");
+	if (value)
+		value_ = strdup(value);
+	else
+		value_ = NULL;
+
+	section_node = find_config_section(section_name_);
+
+	if (section_node != NULL)
+		element_node = find_config_element(subkey_, section_node);
+
+	fn(section_node, element_node);
+
+	return 0;
+}
+
 int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
 {
 	int ret = 0;
@@ -63,5 +246,20 @@ int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
 		ret = perf_config(show_config, NULL);
 	}
 
+	else if (params.get_action) {
+		check_argc(argc, 1, 1);
+		ret = perf_configset_with_option(show_spec_config, argv[0], argv[1]);
+	}
+
+	else if (params.set_action) {
+		check_argc(argc, 2, 2);
+		ret = perf_configset_with_option(set_config, argv[0], argv[1]);
+	}
+
+	else if (params.unset_action) {
+		check_argc(argc, 1, 1);
+		ret = perf_configset_with_option(set_config, argv[0], argv[1]);
+	}
+
 	return ret;
 }
tools/perf/util/cache.h +17 -1
index fbcca21..c06b08d 100644
@@ -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)(struct config_section *, struct config_element *);
+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 *);
@@ -27,7 +44,6 @@ extern u64 perf_config_u64(const char *, const char *);
 extern int perf_config_bool(const char *, const char *);
 extern int config_error_nonbool(const char *);
 extern const char *perf_config_dirname(const char *, const char *);
-
 /* pager.c */
 extern void setup_pager(void);
 extern const char *pager_program;
tools/perf/util/config.c +32 -3
index e18f653..ffcbe96 100644
@@ -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,36 @@ out:
 	return ret;
 }
 
+int perf_configset_write_in_full(void)
+{
+	struct config_section *section_node;
+	struct config_element *element_node;
+	char section[BUFSIZ];
+	char key_value_pair[BUFSIZ];
+	const char *first_line = "# this file is auto-generated.\n";
+	FILE *f = fopen(config_file_name, "w");
+
+	if (!f)
+		return -1;
+
+	fwrite(first_line, strlen(first_line), 1, f);
+	/* overwrite configvariables */
+	list_for_each_entry(section_node, &sections, list) {
+		sprintf(section, "[%s]\n", section_node->name);
+		fwrite(section, strlen(section), 1, f);
+		list_for_each_entry(element_node, &section_node->element_head, list) {
+			if (element_node->value) {
+				sprintf(key_value_pair, "\t%s = %s\n"
+					, element_node->subkey, element_node->value);
+				fwrite(key_value_pair, strlen(key_value_pair), 1, f);
+			}
+		}
+	}
+	fclose(f);
+
+	return 0;
+}
+
 /*
  * Call this to report error for your variable that should not
  * get a boolean value (i.e. "[my] var" means "true").
Signature · 1 line
1.9.1
#2 lore raw

Re: [PATCH 2/2] perf tools: Add options 'get', 'set' and 'unset' to 'perf-config'.

Quoted 2 lines · Taeung Song

Add 'get' option that can show value of a specific config variable and 'set' and 'unset' option that can create or replace a config variable.

vould you please update the changelog with the new syntax examples? also Documentation/perf-config.txt wasn't updated

anyway.. any chance we could use syntax here that would follow the sysctl style?

sysctl [options] [variable[=value]] [...]
sysctl -p [file or regexp] [...]

# display current config
perf config
# display current config plus all keys with default values
perf config -a
# display key value:
perf config report.queue
# set key value:
perf config report.queue=100M
# remove key (not in sysctl)
perf config -r report.queue

jirka

#3 lore raw
In reply to Taeung Song

On Sun, Apr 05, 2015 at 02:41:27AM +0900, Taeung Song wrote:

SNIP

tools/perf/builtin-config.c line 29

Quoted 9 lines
tools/perf/builtin-config.c new file +9 -0
+	"perf config [options]",
+	NULL
+};
+static const struct option config_options[] = {
+	OPT_BOOLEAN('l', "list", &params.list_action, "list up current configurations"),
+	OPT_END()
+};
+
+static void check_argc(int argc, int min, int max)

seems like all callers have min == max, so there's no need for 2 params

jirka