perf tools: Provide subcmd configuration at runtime
authorJosh Poimboeuf <jpoimboe@redhat.com>
Tue, 15 Dec 2015 15:39:35 +0000 (09:39 -0600)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 16 Dec 2015 19:18:16 +0000 (16:18 -0300)
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>
tools/perf/Build
tools/perf/perf.c
tools/perf/util/Build
tools/perf/util/cache.h
tools/perf/util/exec_cmd.c
tools/perf/util/exec_cmd.h
tools/perf/util/pager.c
tools/perf/util/pager.h
tools/perf/util/parse-options.c
tools/perf/util/subcmd-config.c [new file with mode: 0644]
tools/perf/util/subcmd-config.h [new file with mode: 0644]

index 2a41217e9d88633358a478cbe8b81f2484ee3b16..00c4b8c3d8cab95ad3ff7df2159c634de0bb12f2 100644 (file)
@@ -36,7 +36,10 @@ paths += -DPERF_MAN_PATH="BUILD_STR($(mandir_SQ))"
 
 CFLAGS_builtin-help.o      += $(paths)
 CFLAGS_builtin-timechart.o += $(paths)
-CFLAGS_perf.o              += -DPERF_HTML_PATH="BUILD_STR($(htmldir_SQ))" -include $(OUTPUT)PERF-VERSION-FILE
+CFLAGS_perf.o              += -DPERF_HTML_PATH="BUILD_STR($(htmldir_SQ))"      \
+                             -DPERF_EXEC_PATH="BUILD_STR($(perfexecdir_SQ))"   \
+                             -DPREFIX="BUILD_STR($(prefix_SQ))"                \
+                             -include $(OUTPUT)PERF-VERSION-FILE
 CFLAGS_builtin-trace.o    += -DSTRACE_GROUPS_DIR="BUILD_STR($(STRACE_GROUPS_DIR_SQ))"
 
 libperf-y += util/
index 59ea48c7e26c46968e802badd3a9dd9f1a5aecae..783a3310a9d85dc2a6842fb5aab5afbd84a452e4 100644 (file)
@@ -119,7 +119,7 @@ static void commit_pager_choice(void)
 {
        switch (use_pager) {
        case 0:
-               setenv("PERF_PAGER", "cat", 1);
+               setenv(PERF_PAGER_ENVIRONMENT, "cat", 1);
                break;
        case 1:
                /* setup_pager(); */
@@ -530,6 +530,10 @@ int main(int argc, const char **argv)
        const char *cmd;
        char sbuf[STRERR_BUFSIZE];
 
+       /* libsubcmd init */
+       exec_cmd_init("perf", PREFIX, PERF_EXEC_PATH, EXEC_PATH_ENVIRONMENT);
+       pager_init(PERF_PAGER_ENVIRONMENT);
+
        /* The page_size is placed in util object. */
        page_size = sysconf(_SC_PAGE_SIZE);
        cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
index 99b3dae57806b7ee6520c129c0d2c091f88a4095..196beefa16a9732210447048b440624d81f71a5b 100644 (file)
@@ -88,6 +88,7 @@ libperf-y += parse-branch-options.o
 libperf-y += parse-regs-options.o
 libperf-y += term.o
 libperf-y += help-unknown-cmd.o
+libperf-y += subcmd-config.o
 
 libperf-$(CONFIG_LIBBPF) += bpf-loader.o
 libperf-$(CONFIG_BPF_PROLOGUE) += bpf-prologue.o
@@ -113,7 +114,6 @@ libperf-$(CONFIG_ZLIB) += zlib.o
 libperf-$(CONFIG_LZMA) += lzma.o
 
 CFLAGS_config.o   += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
-CFLAGS_exec_cmd.o += -DPERF_EXEC_PATH="BUILD_STR($(perfexecdir_SQ))" -DPREFIX="BUILD_STR($(prefix_SQ))"
 
 $(OUTPUT)util/parse-events-flex.c: util/parse-events.l $(OUTPUT)util/parse-events-bison.c
        $(call rule_mkdir)
index d723ecb9b9590aa8ff279d2af94f1e7ffe36b9d0..fc6a745d2ec6815693ebc1583403065f025577c7 100644 (file)
@@ -21,6 +21,7 @@
 #define DEFAULT_PERF_DIR_ENVIRONMENT ".perf"
 #define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR"
 #define PERF_TRACEFS_ENVIRONMENT "PERF_TRACEFS_DIR"
+#define PERF_PAGER_ENVIRONMENT "PERF_PAGER"
 
 typedef int (*config_fn_t)(const char *, const char *, void *);
 extern int perf_default_config(const char *, const char *, void *);
index 1099e92f5ee1881c1519211a0ff3d98c39585c3c..b935e4ce62a232566df223a63992461a2e8bc54a 100644 (file)
@@ -1,6 +1,7 @@
 #include "cache.h"
 #include "exec_cmd.h"
 #include "quote.h"
+#include "subcmd-config.h"
 
 #include <string.h>
 
 static const char *argv_exec_path;
 static const char *argv0_path;
 
+void exec_cmd_init(const char *exec_name, const char *prefix,
+                  const char *exec_path, const char *exec_path_env)
+{
+       subcmd_config.exec_name         = exec_name;
+       subcmd_config.prefix            = prefix;
+       subcmd_config.exec_path         = exec_path;
+       subcmd_config.exec_path_env     = exec_path_env;
+}
+
 char *system_path(const char *path)
 {
-       static const char *prefix = PREFIX;
        struct strbuf d = STRBUF_INIT;
 
        if (is_absolute_path(path))
                return strdup(path);
 
-       strbuf_addf(&d, "%s/%s", prefix, path);
+       strbuf_addf(&d, "%s/%s", subcmd_config.prefix, path);
        path = strbuf_detach(&d, NULL);
        return (char *)path;
 }
@@ -47,7 +56,7 @@ void perf_set_argv_exec_path(const char *exec_path)
        /*
         * Propagate this setting to external programs.
         */
-       setenv(EXEC_PATH_ENVIRONMENT, exec_path, 1);
+       setenv(subcmd_config.exec_path_env, exec_path, 1);
 }
 
 
@@ -59,11 +68,11 @@ char *perf_exec_path(void)
        if (argv_exec_path)
                return strdup(argv_exec_path);
 
-       env = getenv(EXEC_PATH_ENVIRONMENT);
+       env = getenv(subcmd_config.exec_path_env);
        if (env && *env)
                return strdup(env);
 
-       return system_path(PERF_EXEC_PATH);
+       return system_path(subcmd_config.exec_path);
 }
 
 static void add_path(struct strbuf *out, const char *path)
@@ -107,7 +116,7 @@ static const char **prepare_perf_cmd(const char **argv)
                ; /* just counting */
        nargv = malloc(sizeof(*nargv) * (argc + 2));
 
-       nargv[0] = "perf";
+       nargv[0] = subcmd_config.exec_name;
        for (argc = 0; argv[argc]; argc++)
                nargv[argc + 1] = argv[argc];
        nargv[argc + 1] = NULL;
@@ -118,7 +127,7 @@ int execv_perf_cmd(const char **argv) {
        const char **nargv = prepare_perf_cmd(argv);
 
        /* execvp() can only ever return if it fails */
-       execvp("perf", (char **)nargv);
+       execvp(subcmd_config.exec_name, (char **)nargv);
 
        free(nargv);
        return -1;
index 48b4175f1e110900d239ad44648f98b1c83c7d4d..fd4434e48d2161d7c5aa82296c329164a73e396f 100644 (file)
@@ -1,6 +1,9 @@
 #ifndef __PERF_EXEC_CMD_H
 #define __PERF_EXEC_CMD_H
 
+extern void exec_cmd_init(const char *exec_name, const char *prefix,
+                         const char *exec_path, const char *exec_path_env);
+
 extern void perf_set_argv_exec_path(const char *exec_path);
 extern const char *perf_extract_argv0_path(const char *path);
 extern void setup_path(void);
index 7dcbef64b6092b7c3dc38daefbeba01e71f237d7..d5ef62eaa413f21846499e7127a896c9943006f2 100644 (file)
@@ -1,6 +1,7 @@
 #include "cache.h"
 #include "run-command.h"
 #include "sigchain.h"
+#include "subcmd-config.h"
 
 /*
  * This is split up from the rest of git so that we can do
@@ -9,6 +10,11 @@
 
 static int spawned_pager;
 
+void pager_init(const char *pager_env)
+{
+       subcmd_config.pager_env = pager_env;
+}
+
 static void pager_preexec(void)
 {
        /*
@@ -46,7 +52,7 @@ static void wait_for_pager_signal(int signo)
 
 void setup_pager(void)
 {
-       const char *pager = getenv("PERF_PAGER");
+       const char *pager = getenv(subcmd_config.pager_env);
 
        if (!isatty(1))
                return;
index 2794a83b7e851eaec8100479e5ebfffabc2cdaa5..d6a591a4c0178d6f6f35f33d8e146a461bc4bb48 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef __PERF_PAGER_H
 #define __PERF_PAGER_H
 
+extern void pager_init(const char *pager_env);
+
 extern void setup_pager(void);
 extern int pager_in_use(void);
 
index 0ad1384783dd5926b6f8add3a53c6ca8bffb892d..da4ba21cad212de48bcab566db9680b89cd1856a 100644 (file)
@@ -2,6 +2,7 @@
 #include "parse-options.h"
 #include "cache.h"
 #include "header.h"
+#include "subcmd-config.h"
 #include <linux/string.h>
 
 #define OPT_SHORT 1
@@ -577,7 +578,8 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
        if (subcommands && !usagestr[0]) {
                struct strbuf buf = STRBUF_INIT;
 
-               strbuf_addf(&buf, "perf %s [<options>] {", argv[0]);
+               strbuf_addf(&buf, "%s %s [<options>] {",
+                           subcmd_config.exec_name, argv[0]);
                for (int i = 0; subcommands[i]; i++) {
                        if (i)
                                strbuf_addstr(&buf, "|");
diff --git a/tools/perf/util/subcmd-config.c b/tools/perf/util/subcmd-config.c
new file mode 100644 (file)
index 0000000..d017c72
--- /dev/null
@@ -0,0 +1,11 @@
+#include "subcmd-config.h"
+
+#define UNDEFINED "SUBCMD_HAS_NOT_BEEN_INITIALIZED"
+
+struct subcmd_config subcmd_config = {
+       .exec_name      = UNDEFINED,
+       .prefix         = UNDEFINED,
+       .exec_path      = UNDEFINED,
+       .exec_path_env  = UNDEFINED,
+       .pager_env      = UNDEFINED,
+};
diff --git a/tools/perf/util/subcmd-config.h b/tools/perf/util/subcmd-config.h
new file mode 100644 (file)
index 0000000..cc85140
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef __PERF_SUBCMD_CONFIG_H
+#define __PERF_SUBCMD_CONFIG_H
+
+struct subcmd_config {
+       const char *exec_name;
+       const char *prefix;
+       const char *exec_path;
+       const char *exec_path_env;
+       const char *pager_env;
+};
+
+extern struct subcmd_config subcmd_config;
+
+#endif /* __PERF_SUBCMD_CONFIG_H */