Skip to content

Commit c7ac241

Browse files
Taeungacmel
authored andcommitted
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>
1 parent a7636d9 commit c7ac241

4 files changed

Lines changed: 42 additions & 6 deletions

File tree

‎tools/perf/Documentation/perf-config.txt‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ perf-config - Get and set variables in a configuration file.
88
SYNOPSIS
99
--------
1010
[verse]
11-
'perf config' -l | --list
11+
'perf config' [<file-option>] -l | --list
1212

1313
DESCRIPTION
1414
-----------
@@ -21,6 +21,14 @@ OPTIONS
2121
--list::
2222
Show current config variables, name and value, for all sections.
2323

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+
2432
CONFIGURATION FILE
2533
------------------
2634

@@ -30,6 +38,10 @@ The '$HOME/.perfconfig' file is used to store a per-user configuration.
3038
The file '$(sysconfdir)/perfconfig' can be used to
3139
store a system-wide default configuration.
3240

41+
When reading or writing, the values are read from the system and user
42+
configuration files by default, and options '--system' and '--user'
43+
can be used to tell the command to read from or write to only that location.
44+
3345
Syntax
3446
~~~~~~
3547

‎tools/perf/builtin-config.c‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
#include "util/util.h"
1414
#include "util/debug.h"
1515

16+
static bool use_system_config, use_user_config;
17+
1618
static const char * const config_usage[] = {
17-
"perf config [options]",
19+
"perf config [<file-option>] [options]",
1820
NULL
1921
};
2022

@@ -25,6 +27,8 @@ enum actions {
2527
static struct option config_options[] = {
2628
OPT_SET_UINT('l', "list", &actions,
2729
"show current config variables", ACTION_LIST),
30+
OPT_BOOLEAN(0, "system", &use_system_config, "use system config file"),
31+
OPT_BOOLEAN(0, "user", &use_user_config, "use user config file"),
2832
OPT_END()
2933
};
3034

@@ -42,20 +46,37 @@ static int show_config(const char *key, const char *value,
4246
int cmd_config(int argc, const char **argv, const char *prefix __maybe_unused)
4347
{
4448
int ret = 0;
49+
char *user_config = mkpath("%s/.perfconfig", getenv("HOME"));
4550

4651
argc = parse_options(argc, argv, config_options, config_usage,
4752
PARSE_OPT_STOP_AT_NON_OPTION);
4853

54+
if (use_system_config && use_user_config) {
55+
pr_err("Error: only one config file at a time\n");
56+
parse_options_usage(config_usage, config_options, "user", 0);
57+
parse_options_usage(NULL, config_options, "system", 0);
58+
return -1;
59+
}
60+
61+
if (use_system_config)
62+
config_exclusive_filename = perf_etc_perfconfig();
63+
else if (use_user_config)
64+
config_exclusive_filename = user_config;
65+
4966
switch (actions) {
5067
case ACTION_LIST:
5168
if (argc) {
5269
pr_err("Error: takes no arguments\n");
5370
parse_options_usage(config_usage, config_options, "l", 1);
5471
} else {
5572
ret = perf_config(show_config, NULL);
56-
if (ret < 0)
73+
if (ret < 0) {
74+
const char * config_filename = config_exclusive_filename;
75+
if (!config_exclusive_filename)
76+
config_filename = user_config;
5777
pr_err("Nothing configured, "
58-
"please check your ~/.perfconfig file\n");
78+
"please check your %s \n", config_filename);
79+
}
5980
}
6081
break;
6182
default:

‎tools/perf/util/cache.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#define PERF_TRACEFS_ENVIRONMENT "PERF_TRACEFS_DIR"
2424
#define PERF_PAGER_ENVIRONMENT "PERF_PAGER"
2525

26+
extern const char *config_exclusive_filename;
27+
2628
typedef int (*config_fn_t)(const char *, const char *, void *);
2729
extern int perf_default_config(const char *, const char *, void *);
2830
extern int perf_config(config_fn_t fn, void *);
@@ -31,6 +33,7 @@ extern u64 perf_config_u64(const char *, const char *);
3133
extern int perf_config_bool(const char *, const char *);
3234
extern int config_error_nonbool(const char *);
3335
extern const char *perf_config_dirname(const char *, const char *);
36+
extern const char *perf_etc_perfconfig(void);
3437

3538
char *alias_lookup(const char *alias);
3639
int split_cmdline(char *cmdline, const char ***argv);

‎tools/perf/util/config.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static const char *config_file_name;
2626
static int config_linenr;
2727
static int config_file_eof;
2828

29-
static const char *config_exclusive_filename;
29+
const char *config_exclusive_filename;
3030

3131
static int get_next_char(void)
3232
{
@@ -434,7 +434,7 @@ static int perf_config_from_file(config_fn_t fn, const char *filename, void *dat
434434
return ret;
435435
}
436436

437-
static const char *perf_etc_perfconfig(void)
437+
const char *perf_etc_perfconfig(void)
438438
{
439439
static const char *system_wide;
440440
if (!system_wide)

0 commit comments

Comments
 (0)