Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf config: Add '--system' and '--user' options to select which config file is used

The '--system' option means $(sysconfdir)/perfconfig and '--user' means
$HOME/.perfconfig. If none is used, both system and user config file are
read. E.g.:

# perf config [<file-option>] [options]

With an specific config file:

# perf config --user | --system

or both user and system config file:

# perf config

Signed-off-by: Taeung Song <treeze.taeung@gmail.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: http://lkml.kernel.org/r/1455126685-32367-2-git-send-email-treeze.taeung@gmail.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Taeung Song and committed by
Arnaldo Carvalho de Melo
c7ac2417 a7636d9e

+42 -6
+13 -1
tools/perf/Documentation/perf-config.txt
··· 8 8 SYNOPSIS 9 9 -------- 10 10 [verse] 11 - 'perf config' -l | --list 11 + 'perf config' [<file-option>] -l | --list 12 12 13 13 DESCRIPTION 14 14 ----------- ··· 21 21 --list:: 22 22 Show current config variables, name and value, for all sections. 23 23 24 + --user:: 25 + For writing and reading options: write to user 26 + '$HOME/.perfconfig' file or read it. 27 + 28 + --system:: 29 + For writing and reading options: write to system-wide 30 + '$(sysconfdir)/perfconfig' or read it. 31 + 24 32 CONFIGURATION FILE 25 33 ------------------ 26 34 ··· 37 29 The '$HOME/.perfconfig' file is used to store a per-user configuration. 38 30 The file '$(sysconfdir)/perfconfig' can be used to 39 31 store a system-wide default configuration. 32 + 33 + When reading or writing, the values are read from the system and user 34 + configuration files by default, and options '--system' and '--user' 35 + can be used to tell the command to read from or write to only that location. 40 36 41 37 Syntax 42 38 ~~~~~~
+24 -3
tools/perf/builtin-config.c
··· 13 13 #include "util/util.h" 14 14 #include "util/debug.h" 15 15 16 + static bool use_system_config, use_user_config; 17 + 16 18 static const char * const config_usage[] = { 17 - "perf config [options]", 19 + "perf config [<file-option>] [options]", 18 20 NULL 19 21 }; 20 22 ··· 27 25 static struct option config_options[] = { 28 26 OPT_SET_UINT('l', "list", &actions, 29 27 "show current config variables", ACTION_LIST), 28 + OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"), 29 + OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"), 30 30 OPT_END() 31 31 }; 32 32 ··· 46 42 int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused) 47 43 { 48 44 int ret = 0; 45 + char *user_config = mkpath("%s/.perfconfig", getenv("HOME")); 49 46 50 47 argc = parse_options(argc, argv, config_options, config_usage, 51 48 PARSE_OPT_STOP_AT_NON_OPTION); 49 + 50 + if (use_system_config && use_user_config) { 51 + pr_err("Error: only one config file at a time\n"); 52 + parse_options_usage(config_usage, config_options, "user", 0); 53 + parse_options_usage(NULL, config_options, "system", 0); 54 + return -1; 55 + } 56 + 57 + if (use_system_config) 58 + config_exclusive_filename = perf_etc_perfconfig(); 59 + else if (use_user_config) 60 + config_exclusive_filename = user_config; 52 61 53 62 switch (actions) { 54 63 case ACTION_LIST: ··· 70 53 parse_options_usage(config_usage, config_options, "l", 1); 71 54 } else { 72 55 ret = perf_config(show_config, NULL); 73 - if (ret < 0) 56 + if (ret < 0) { 57 + const char * config_filename = config_exclusive_filename; 58 + if (!config_exclusive_filename) 59 + config_filename = user_config; 74 60 pr_err("Nothing configured, " 75 - "please check your ~/.perfconfig file\n"); 61 + "please check your %s \n", config_filename); 62 + } 76 63 } 77 64 break; 78 65 default:
+3
tools/perf/util/cache.h
··· 23 23 #define PERF_TRACEFS_ENVIRONMENT "PERF_TRACEFS_DIR" 24 24 #define PERF_PAGER_ENVIRONMENT "PERF_PAGER" 25 25 26 + extern const char *config_exclusive_filename; 27 + 26 28 typedef int (*config_fn_t)(const char *, const char *, void *); 27 29 extern int perf_default_config(const char *, const char *, void *); 28 30 extern int perf_config(config_fn_t fn, void *); ··· 33 31 extern int perf_config_bool(const char *, const char *); 34 32 extern int config_error_nonbool(const char *); 35 33 extern const char *perf_config_dirname(const char *, const char *); 34 + extern const char *perf_etc_perfconfig(void); 36 35 37 36 char *alias_lookup(const char *alias); 38 37 int split_cmdline(char *cmdline, const char ***argv);
+2 -2
tools/perf/util/config.c
··· 26 26 static int config_linenr; 27 27 static int config_file_eof; 28 28 29 - static const char *config_exclusive_filename; 29 + const char *config_exclusive_filename; 30 30 31 31 static int get_next_char(void) 32 32 { ··· 434 434 return ret; 435 435 } 436 436 437 - static const char *perf_etc_perfconfig(void) 437 + const char *perf_etc_perfconfig(void) 438 438 { 439 439 static const char *system_wide; 440 440 if (!system_wide)