aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Poimboeuf <jpoimboe@redhat.com>2015-12-15 10:39:35 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2015-12-16 14:18:16 -0500
commit096d35585b4fce7d3ee9b8b34314f39f49491ab1 (patch)
tree943117cf4968b21676cea2ba7d1c65d0d74370e2
parent24a88bdd05d5de32f3a56a4dcc5070c97d4a514f (diff)
perf tools: Provide subcmd configuration at runtime
Create init functions for exec_cmd.c and pager.c. This allows their configuration to be specified at runtime so they can be split out into a separate library which can be used by other programs. Their configuration is stored in a shared subcmd_config struct. Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/21f5f6b38da72c985a8dcfa185700d03e7eecd1d.1450193761.git.jpoimboe@redhat.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
-rw-r--r--tools/perf/Build5
-rw-r--r--tools/perf/perf.c6
-rw-r--r--tools/perf/util/Build2
-rw-r--r--tools/perf/util/cache.h1
-rw-r--r--tools/perf/util/exec_cmd.c23
-rw-r--r--tools/perf/util/exec_cmd.h3
-rw-r--r--tools/perf/util/pager.c8
-rw-r--r--tools/perf/util/pager.h2
-rw-r--r--tools/perf/util/parse-options.c4
-rw-r--r--tools/perf/util/subcmd-config.c11
-rw-r--r--tools/perf/util/subcmd-config.h14
11 files changed, 67 insertions, 12 deletions
diff --git a/tools/perf/Build b/tools/perf/Build
index 2a41217e9d88..00c4b8c3d8ca 100644
--- a/tools/perf/Build
+++ b/tools/perf/Build
@@ -36,7 +36,10 @@ paths += -DPERF_MAN_PATH="BUILD_STR($(mandir_SQ))"
36 36
37CFLAGS_builtin-help.o += $(paths) 37CFLAGS_builtin-help.o += $(paths)
38CFLAGS_builtin-timechart.o += $(paths) 38CFLAGS_builtin-timechart.o += $(paths)
39CFLAGS_perf.o += -DPERF_HTML_PATH="BUILD_STR($(htmldir_SQ))" -include $(OUTPUT)PERF-VERSION-FILE 39CFLAGS_perf.o += -DPERF_HTML_PATH="BUILD_STR($(htmldir_SQ))" \
40 -DPERF_EXEC_PATH="BUILD_STR($(perfexecdir_SQ))" \
41 -DPREFIX="BUILD_STR($(prefix_SQ))" \
42 -include $(OUTPUT)PERF-VERSION-FILE
40CFLAGS_builtin-trace.o += -DSTRACE_GROUPS_DIR="BUILD_STR($(STRACE_GROUPS_DIR_SQ))" 43CFLAGS_builtin-trace.o += -DSTRACE_GROUPS_DIR="BUILD_STR($(STRACE_GROUPS_DIR_SQ))"
41 44
42libperf-y += util/ 45libperf-y += util/
diff --git a/tools/perf/perf.c b/tools/perf/perf.c
index 59ea48c7e26c..783a3310a9d8 100644
--- a/tools/perf/perf.c
+++ b/tools/perf/perf.c
@@ -119,7 +119,7 @@ static void commit_pager_choice(void)
119{ 119{
120 switch (use_pager) { 120 switch (use_pager) {
121 case 0: 121 case 0:
122 setenv("PERF_PAGER", "cat", 1); 122 setenv(PERF_PAGER_ENVIRONMENT, "cat", 1);
123 break; 123 break;
124 case 1: 124 case 1:
125 /* setup_pager(); */ 125 /* setup_pager(); */
@@ -530,6 +530,10 @@ int main(int argc, const char **argv)
530 const char *cmd; 530 const char *cmd;
531 char sbuf[STRERR_BUFSIZE]; 531 char sbuf[STRERR_BUFSIZE];
532 532
533 /* libsubcmd init */
534 exec_cmd_init("perf", PREFIX, PERF_EXEC_PATH, EXEC_PATH_ENVIRONMENT);
535 pager_init(PERF_PAGER_ENVIRONMENT);
536
533 /* The page_size is placed in util object. */ 537 /* The page_size is placed in util object. */
534 page_size = sysconf(_SC_PAGE_SIZE); 538 page_size = sysconf(_SC_PAGE_SIZE);
535 cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE); 539 cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 99b3dae57806..196beefa16a9 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -88,6 +88,7 @@ libperf-y += parse-branch-options.o
88libperf-y += parse-regs-options.o 88libperf-y += parse-regs-options.o
89libperf-y += term.o 89libperf-y += term.o
90libperf-y += help-unknown-cmd.o 90libperf-y += help-unknown-cmd.o
91libperf-y += subcmd-config.o
91 92
92libperf-$(CONFIG_LIBBPF) += bpf-loader.o 93libperf-$(CONFIG_LIBBPF) += bpf-loader.o
93libperf-$(CONFIG_BPF_PROLOGUE) += bpf-prologue.o 94libperf-$(CONFIG_BPF_PROLOGUE) += bpf-prologue.o
@@ -113,7 +114,6 @@ libperf-$(CONFIG_ZLIB) += zlib.o
113libperf-$(CONFIG_LZMA) += lzma.o 114libperf-$(CONFIG_LZMA) += lzma.o
114 115
115CFLAGS_config.o += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))" 116CFLAGS_config.o += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
116CFLAGS_exec_cmd.o += -DPERF_EXEC_PATH="BUILD_STR($(perfexecdir_SQ))" -DPREFIX="BUILD_STR($(prefix_SQ))"
117 117
118$(OUTPUT)util/parse-events-flex.c: util/parse-events.l $(OUTPUT)util/parse-events-bison.c 118$(OUTPUT)util/parse-events-flex.c: util/parse-events.l $(OUTPUT)util/parse-events-bison.c
119 $(call rule_mkdir) 119 $(call rule_mkdir)
diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h
index d723ecb9b959..fc6a745d2ec6 100644
--- a/tools/perf/util/cache.h
+++ b/tools/perf/util/cache.h
@@ -21,6 +21,7 @@
21#define DEFAULT_PERF_DIR_ENVIRONMENT ".perf" 21#define DEFAULT_PERF_DIR_ENVIRONMENT ".perf"
22#define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR" 22#define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR"
23#define PERF_TRACEFS_ENVIRONMENT "PERF_TRACEFS_DIR" 23#define PERF_TRACEFS_ENVIRONMENT "PERF_TRACEFS_DIR"
24#define PERF_PAGER_ENVIRONMENT "PERF_PAGER"
24 25
25typedef int (*config_fn_t)(const char *, const char *, void *); 26typedef int (*config_fn_t)(const char *, const char *, void *);
26extern int perf_default_config(const char *, const char *, void *); 27extern int perf_default_config(const char *, const char *, void *);
diff --git a/tools/perf/util/exec_cmd.c b/tools/perf/util/exec_cmd.c
index 1099e92f5ee1..b935e4ce62a2 100644
--- a/tools/perf/util/exec_cmd.c
+++ b/tools/perf/util/exec_cmd.c
@@ -1,6 +1,7 @@
1#include "cache.h" 1#include "cache.h"
2#include "exec_cmd.h" 2#include "exec_cmd.h"
3#include "quote.h" 3#include "quote.h"
4#include "subcmd-config.h"
4 5
5#include <string.h> 6#include <string.h>
6 7
@@ -9,15 +10,23 @@
9static const char *argv_exec_path; 10static const char *argv_exec_path;
10static const char *argv0_path; 11static const char *argv0_path;
11 12
13void exec_cmd_init(const char *exec_name, const char *prefix,
14 const char *exec_path, const char *exec_path_env)
15{
16 subcmd_config.exec_name = exec_name;
17 subcmd_config.prefix = prefix;
18 subcmd_config.exec_path = exec_path;
19 subcmd_config.exec_path_env = exec_path_env;
20}
21
12char *system_path(const char *path) 22char *system_path(const char *path)
13{ 23{
14 static const char *prefix = PREFIX;
15 struct strbuf d = STRBUF_INIT; 24 struct strbuf d = STRBUF_INIT;
16 25
17 if (is_absolute_path(path)) 26 if (is_absolute_path(path))
18 return strdup(path); 27 return strdup(path);
19 28
20 strbuf_addf(&d, "%s/%s", prefix, path); 29 strbuf_addf(&d, "%s/%s", subcmd_config.prefix, path);
21 path = strbuf_detach(&d, NULL); 30 path = strbuf_detach(&d, NULL);
22 return (char *)path; 31 return (char *)path;
23} 32}
@@ -47,7 +56,7 @@ void perf_set_argv_exec_path(const char *exec_path)
47 /* 56 /*
48 * Propagate this setting to external programs. 57 * Propagate this setting to external programs.
49 */ 58 */
50 setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1); 59 setenv(subcmd_config.exec_path_env, exec_path, 1);
51} 60}
52 61
53 62
@@ -59,11 +68,11 @@ char *perf_exec_path(void)
59 if (argv_exec_path) 68 if (argv_exec_path)
60 return strdup(argv_exec_path); 69 return strdup(argv_exec_path);
61 70
62 env = getenv(EXEC_PATH_ENVIRONMENT); 71 env = getenv(subcmd_config.exec_path_env);
63 if (env && *env) 72 if (env && *env)
64 return strdup(env); 73 return strdup(env);
65 74
66 return system_path(PERF_EXEC_PATH); 75 return system_path(subcmd_config.exec_path);
67} 76}
68 77
69static void add_path(struct strbuf *out, const char *path) 78static void add_path(struct strbuf *out, const char *path)
@@ -107,7 +116,7 @@ static const char **prepare_perf_cmd(const char **argv)
107 ; /* just counting */ 116 ; /* just counting */
108 nargv = malloc(sizeof(*nargv) * (argc + 2)); 117 nargv = malloc(sizeof(*nargv) * (argc + 2));
109 118
110 nargv[0] = "perf"; 119 nargv[0] = subcmd_config.exec_name;
111 for (argc = 0; argv[argc]; argc++) 120 for (argc = 0; argv[argc]; argc++)
112 nargv[argc + 1] = argv[argc]; 121 nargv[argc + 1] = argv[argc];
113 nargv[argc + 1] = NULL; 122 nargv[argc + 1] = NULL;
@@ -118,7 +127,7 @@ int execv_perf_cmd(const char **argv) {
118 const char **nargv = prepare_perf_cmd(argv); 127 const char **nargv = prepare_perf_cmd(argv);
119 128
120 /* execvp() can only ever return if it fails */ 129 /* execvp() can only ever return if it fails */
121 execvp("perf", (char **)nargv); 130 execvp(subcmd_config.exec_name, (char **)nargv);
122 131
123 free(nargv); 132 free(nargv);
124 return -1; 133 return -1;
diff --git a/tools/perf/util/exec_cmd.h b/tools/perf/util/exec_cmd.h
index 48b4175f1e11..fd4434e48d21 100644
--- a/tools/perf/util/exec_cmd.h
+++ b/tools/perf/util/exec_cmd.h
@@ -1,6 +1,9 @@
1#ifndef __PERF_EXEC_CMD_H 1#ifndef __PERF_EXEC_CMD_H
2#define __PERF_EXEC_CMD_H 2#define __PERF_EXEC_CMD_H
3 3
4extern void exec_cmd_init(const char *exec_name, const char *prefix,
5 const char *exec_path, const char *exec_path_env);
6
4extern void perf_set_argv_exec_path(const char *exec_path); 7extern void perf_set_argv_exec_path(const char *exec_path);
5extern const char *perf_extract_argv0_path(const char *path); 8extern const char *perf_extract_argv0_path(const char *path);
6extern void setup_path(void); 9extern void setup_path(void);
diff --git a/tools/perf/util/pager.c b/tools/perf/util/pager.c
index 7dcbef64b609..d5ef62eaa413 100644
--- a/tools/perf/util/pager.c
+++ b/tools/perf/util/pager.c
@@ -1,6 +1,7 @@
1#include "cache.h" 1#include "cache.h"
2#include "run-command.h" 2#include "run-command.h"
3#include "sigchain.h" 3#include "sigchain.h"
4#include "subcmd-config.h"
4 5
5/* 6/*
6 * This is split up from the rest of git so that we can do 7 * This is split up from the rest of git so that we can do
@@ -9,6 +10,11 @@
9 10
10static int spawned_pager; 11static int spawned_pager;
11 12
13void pager_init(const char *pager_env)
14{
15 subcmd_config.pager_env = pager_env;
16}
17
12static void pager_preexec(void) 18static void pager_preexec(void)
13{ 19{
14 /* 20 /*
@@ -46,7 +52,7 @@ static void wait_for_pager_signal(int signo)
46 52
47void setup_pager(void) 53void setup_pager(void)
48{ 54{
49 const char *pager = getenv("PERF_PAGER"); 55 const char *pager = getenv(subcmd_config.pager_env);
50 56
51 if (!isatty(1)) 57 if (!isatty(1))
52 return; 58 return;
diff --git a/tools/perf/util/pager.h b/tools/perf/util/pager.h
index 2794a83b7e85..d6a591a4c017 100644
--- a/tools/perf/util/pager.h
+++ b/tools/perf/util/pager.h
@@ -1,6 +1,8 @@
1#ifndef __PERF_PAGER_H 1#ifndef __PERF_PAGER_H
2#define __PERF_PAGER_H 2#define __PERF_PAGER_H
3 3
4extern void pager_init(const char *pager_env);
5
4extern void setup_pager(void); 6extern void setup_pager(void);
5extern int pager_in_use(void); 7extern int pager_in_use(void);
6 8
diff --git a/tools/perf/util/parse-options.c b/tools/perf/util/parse-options.c
index 0ad1384783dd..da4ba21cad21 100644
--- a/tools/perf/util/parse-options.c
+++ b/tools/perf/util/parse-options.c
@@ -2,6 +2,7 @@
2#include "parse-options.h" 2#include "parse-options.h"
3#include "cache.h" 3#include "cache.h"
4#include "header.h" 4#include "header.h"
5#include "subcmd-config.h"
5#include <linux/string.h> 6#include <linux/string.h>
6 7
7#define OPT_SHORT 1 8#define OPT_SHORT 1
@@ -577,7 +578,8 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
577 if (subcommands && !usagestr[0]) { 578 if (subcommands && !usagestr[0]) {
578 struct strbuf buf = STRBUF_INIT; 579 struct strbuf buf = STRBUF_INIT;
579 580
580 strbuf_addf(&buf, "perf %s [<options>] {", argv[0]); 581 strbuf_addf(&buf, "%s %s [<options>] {",
582 subcmd_config.exec_name, argv[0]);
581 for (int i = 0; subcommands[i]; i++) { 583 for (int i = 0; subcommands[i]; i++) {
582 if (i) 584 if (i)
583 strbuf_addstr(&buf, "|"); 585 strbuf_addstr(&buf, "|");
diff --git a/tools/perf/util/subcmd-config.c b/tools/perf/util/subcmd-config.c
new file mode 100644
index 000000000000..d017c728bd1b
--- /dev/null
+++ b/tools/perf/util/subcmd-config.c
@@ -0,0 +1,11 @@
1#include "subcmd-config.h"
2
3#define UNDEFINED "SUBCMD_HAS_NOT_BEEN_INITIALIZED"
4
5struct subcmd_config subcmd_config = {
6 .exec_name = UNDEFINED,
7 .prefix = UNDEFINED,
8 .exec_path = UNDEFINED,
9 .exec_path_env = UNDEFINED,
10 .pager_env = UNDEFINED,
11};
diff --git a/tools/perf/util/subcmd-config.h b/tools/perf/util/subcmd-config.h
new file mode 100644
index 000000000000..cc8514030b5c
--- /dev/null
+++ b/tools/perf/util/subcmd-config.h
@@ -0,0 +1,14 @@
1#ifndef __PERF_SUBCMD_CONFIG_H
2#define __PERF_SUBCMD_CONFIG_H
3
4struct subcmd_config {
5 const char *exec_name;
6 const char *prefix;
7 const char *exec_path;
8 const char *exec_path_env;
9 const char *pager_env;
10};
11
12extern struct subcmd_config subcmd_config;
13
14#endif /* __PERF_SUBCMD_CONFIG_H */