Skip to contentSkip to conversation

perf config: Add '--system' and '--global' options to be able to select which config file to be used.

PATCH Superseded Taeung Song 1 participant 1 message Jul 12, 2015 perf events

v3 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 5/5 perf config: Add '--system' and '--global' options to be able to select which config file to be used.

Which config file is used is decided at only perf_config(). That's why a config file to be used can't be selected among system and global config file when perf-config commands get or set it. So add '--system' and '--global' options to select which config file to be used when getting, setting or etc.

---
 tools/perf/Documentation/perf-config.txt | 20 ++++++++--
 tools/perf/builtin-config.c              | 24 +++++++++--
 tools/perf/util/cache.h                  |  5 ++-
 tools/perf/util/config.c                 | 68 +++++++++++++++++++-------------
 4 files changed, 80 insertions(+), 37 deletions(-)
tools/perf/Documentation/perf-config.txt +16 -4
index a543e1b..b798112 100644
@@ -8,13 +8,13 @@ perf-config - Get and set variables in a configuration file.
 SYNOPSIS
 --------
 [verse]
-'perf config' [section.name[=value] ...]
+'perf config' [<file-option>] [section.name[=value] ...]
 or
-'perf config' -l | --list
+'perf config' [<file-option>] -l | --list
 or
-'perf config' -a | --list-all
+'perf config' [<file-option>] -a | --list-all
 or
-'perf config' -r | --remove [section.name ...]
+'perf config' [<file-option>] -r | --remove [section.name ...]
 
 DESCRIPTION
 -----------
@@ -35,6 +35,14 @@ OPTIONS
 --remove::
 	Remove specific config variables.
 
+--global::
+	For writing and reading options: write to global
+	'$HOME/.perfconfig' file or read it.
+
+--system::
+	For writing and reading options: write to system-wide
+	'$(sysconfdir)/perfconfig' or read it.
+
 CONFIGURATION FILE
 ------------------
 
@@ -47,6 +55,10 @@ store a system-wide default configuration.
 The variables are divided into sections. In each section, the variables
 can are composed of a key and value.
 
+When reading or writing, the values are read from the system and  global
+configuration files by default, and options '--system' and '--global'
+can be used to tell the command to read from or write to only that location.
+
 Syntax
 ~~~~~~
 
tools/perf/builtin-config.c +20 -4
index 857e121..d8fa566 100644
@@ -14,6 +14,8 @@
 #include "util/debug.h"
 
 static int actions;
+static bool use_system_config, use_global_config;
+static const char *config_file_name;
 
 static const char * const config_usage[] = {
 	"perf config [options] [section.name[=value] ...]",
@@ -25,6 +27,9 @@ static const char * const config_usage[] = {
 #define ACTION_REMOVE (1<<2)
 
 static const struct option config_options[] = {
+	OPT_GROUP("Config file location"),
+	OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"),
+	OPT_BOOLEAN(0, "global", &use_global_config, "use global config file"),
 	OPT_GROUP("Action"),
 	OPT_BIT('l', "list", &actions, "show current config variables", ACTION_LIST),
 	OPT_BIT('a', "list-all", &actions,
@@ -508,7 +513,7 @@ static int set_config(const char *section_name, const char *name, char *value)
 		}
 	}
 
-	return perf_configset_write_in_full();
+	return perf_configset_write_in_full(config_file_name);
 }
 
 static int collect_current_config(const char *var, const char *value,
@@ -635,13 +640,24 @@ int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
 	else
 		has_option = false;
 
+	if (use_system_config && use_global_config) {
+		pr_err("Error: only one config file at a time.");
+		usage_with_options(config_usage, config_options);
+		return -1;
+	} else if (use_global_config || (!use_system_config && !use_global_config)) {
+		config_file_name = perf_user_perfconfig(getenv("HOME"));
+		if (!config_file_name)
+			return -1;
+	} else if (use_system_config)
+		config_file_name = perf_etc_perfconfig();
+
 	INIT_LIST_HEAD(&sections);
-	perf_config(collect_current_config, NULL);
+	perf_config_from_file(collect_current_config, config_file_name, NULL);
 
 	switch (actions) {
 	case ACTION_LIST:
 		if (argc == 0)
-			ret = perf_config(show_config, NULL);
+			ret = perf_config_from_file(show_config, config_file_name, NULL);
 		else
 			goto out_err;
 		goto out;
@@ -665,7 +681,7 @@ int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
 		goto out;
 	default:
 		if (!has_option && argc == 0) {
-			ret = perf_config(show_config, NULL);
+			ret = perf_config_from_file(show_config, config_file_name, NULL);
 			goto out;
 		} else if (argc > 0) {
 			for (i = 0; argv[i]; i++) {
tools/perf/util/cache.h +4 -1
index 36d7839..079a3846 100644
@@ -35,15 +35,18 @@ struct config_section {
 struct list_head sections;
 
 typedef int (*configset_fn_t)(const char *, const char *, char*);
-extern int perf_configset_write_in_full(void);
+extern int perf_configset_write_in_full(const char *file_name);
 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 *);
+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 *);
 extern int config_error_nonbool(const char *);
 extern const char *perf_config_dirname(const char *, const char *);
+extern const char *perf_etc_perfconfig(void);
+extern const char *perf_user_perfconfig(const char *home);
 
 /* pager.c */
 extern void setup_pager(void);
tools/perf/util/config.c +40 -28

113 lines not on this page · raw message

Signature · 1 line
1.9.1