[PATCH v3 10/17] perf: Provide subcmd configuration at runtime

From: Josh Poimboeuf
Date: Sun Dec 13 2015 - 23:19:19 EST


Create init functions for exec_cmd.c, parse-option.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.

Signed-off-by: Josh Poimboeuf <jpoimboe@xxxxxxxxxx>
---
tools/perf/Build | 5 ++++-
tools/perf/perf.c | 7 ++++++-
tools/perf/util/Build | 1 -
tools/perf/util/cache.h | 1 +
tools/perf/util/exec_cmd.c | 28 +++++++++++++++++++++-------
tools/perf/util/exec_cmd.h | 3 +++
tools/perf/util/pager.c | 9 ++++++++-
tools/perf/util/pager.h | 2 ++
tools/perf/util/parse-options.c | 10 +++++++++-
tools/perf/util/parse-options.h | 2 ++
10 files changed, 56 insertions(+), 12 deletions(-)

diff --git a/tools/perf/Build b/tools/perf/Build
index 2a41217..00c4b8c 100644
--- a/tools/perf/Build
+++ b/tools/perf/Build
@@ -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/
diff --git a/tools/perf/perf.c b/tools/perf/perf.c
index 59ea48c..4d94c80 100644
--- a/tools/perf/perf.c
+++ b/tools/perf/perf.c
@@ -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,11 @@ 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);
+ parse_options_init("perf");
+ 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);
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 99b3dae..470515b 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -113,7 +113,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)
diff --git a/tools/perf/util/cache.h b/tools/perf/util/cache.h
index 9ca4a58..4b9d7da 100644
--- a/tools/perf/util/cache.h
+++ b/tools/perf/util/cache.h
@@ -19,6 +19,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 *);
diff --git a/tools/perf/util/exec_cmd.c b/tools/perf/util/exec_cmd.c
index 1099e92..84f9dc5 100644
--- a/tools/perf/util/exec_cmd.c
+++ b/tools/perf/util/exec_cmd.c
@@ -5,19 +5,33 @@
#include <string.h>

#define MAX_ARGS 32
+#define UNDEFINED "UNDEFINED"

static const char *argv_exec_path;
static const char *argv0_path;

+static const char *cfg_exec_name = UNDEFINED;
+static const char *cfg_prefix = UNDEFINED;
+static const char *cfg_exec_path = UNDEFINED;
+static const char *cfg_exec_path_env = UNDEFINED;
+
+void exec_cmd_init(const char *exec_name, const char *prefix,
+ const char *exec_path, const char *exec_path_env)
+{
+ cfg_exec_name = exec_name;
+ cfg_prefix = prefix;
+ cfg_exec_path = exec_path;
+ cfg_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", cfg_prefix, path);
path = strbuf_detach(&d, NULL);
return (char *)path;
}
@@ -47,7 +61,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(cfg_exec_path_env, exec_path, 1);
}


@@ -59,11 +73,11 @@ char *perf_exec_path(void)
if (argv_exec_path)
return strdup(argv_exec_path);

- env = getenv(EXEC_PATH_ENVIRONMENT);
+ env = getenv(cfg_exec_path_env);
if (env && *env)
return strdup(env);

- return system_path(PERF_EXEC_PATH);
+ return system_path(cfg_exec_path);
}

static void add_path(struct strbuf *out, const char *path)
@@ -107,7 +121,7 @@ static const char **prepare_perf_cmd(const char **argv)
; /* just counting */
nargv = malloc(sizeof(*nargv) * (argc + 2));

- nargv[0] = "perf";
+ nargv[0] = cfg_exec_name;
for (argc = 0; argv[argc]; argc++)
nargv[argc + 1] = argv[argc];
nargv[argc + 1] = NULL;
@@ -118,7 +132,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(cfg_exec_name, (char **)nargv);

free(nargv);
return -1;
diff --git a/tools/perf/util/exec_cmd.h b/tools/perf/util/exec_cmd.h
index 48b4175..fd4434e 100644
--- a/tools/perf/util/exec_cmd.h
+++ b/tools/perf/util/exec_cmd.h
@@ -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);
diff --git a/tools/perf/util/pager.c b/tools/perf/util/pager.c
index 7dcbef6..f6153a1 100644
--- a/tools/perf/util/pager.c
+++ b/tools/perf/util/pager.c
@@ -9,6 +9,13 @@

static int spawned_pager;

+static const char *cfg_pager_env = "UNDEFINED";
+
+void pager_init(const char *pager_env)
+{
+ cfg_pager_env = pager_env;
+}
+
static void pager_preexec(void)
{
/*
@@ -46,7 +53,7 @@ static void wait_for_pager_signal(int signo)

void setup_pager(void)
{
- const char *pager = getenv("PERF_PAGER");
+ const char *pager = getenv(cfg_pager_env);

if (!isatty(1))
return;
diff --git a/tools/perf/util/pager.h b/tools/perf/util/pager.h
index 2794a83..d6a591a 100644
--- a/tools/perf/util/pager.h
+++ b/tools/perf/util/pager.h
@@ -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);

diff --git a/tools/perf/util/parse-options.c b/tools/perf/util/parse-options.c
index de3290b..00a03a9 100644
--- a/tools/perf/util/parse-options.c
+++ b/tools/perf/util/parse-options.c
@@ -9,6 +9,13 @@

static struct strbuf error_buf = STRBUF_INIT;

+static const char *cfg_exec_name = "UNDEFINED";
+
+void parse_options_init(const char *exec_name)
+{
+ cfg_exec_name = exec_name;
+}
+
static int opterror(const struct option *opt, const char *reason, int flags)
{
if (flags & OPT_SHORT)
@@ -505,7 +512,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>] {",
+ cfg_exec_name, argv[0]);
for (int i = 0; subcommands[i]; i++) {
if (i)
strbuf_addstr(&buf, "|");
diff --git a/tools/perf/util/parse-options.h b/tools/perf/util/parse-options.h
index a8e407b..2c7ee90 100644
--- a/tools/perf/util/parse-options.h
+++ b/tools/perf/util/parse-options.h
@@ -4,6 +4,8 @@
#include <linux/kernel.h>
#include <stdbool.h>

+extern void parse_options_init(const char *exec_name);
+
enum parse_opt_type {
/* special types */
OPTION_END,
--
2.4.3

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/