Re: [RFC PATCH 04/22] perf tools: Add new 'perf bpf' command.

Namhyung Kim
2015-05-11T06:38:16Z

Hi,

On Thu, Apr 30, 2015 at 10:52:27AM +0000, Wang Nan wrote:
> Adding new 'perf bpf' command to provide eBPF program loading and
> management support.
>
> Signed-off-by: Wang Nan <wangnan0@huawei.com>
> ---
> tools/perf/Build | 1 +
> tools/perf/Documentation/perf-bpf.txt | 18 ++++++++++
> tools/perf/builtin-bpf.c | 63 +++++++++++++++++++++++++++++++++++
> tools/perf/builtin.h | 1 +
> tools/perf/perf.c | 1 +
> tools/perf/util/Build | 1 +
> tools/perf/util/bpf-loader.c | 35 +++++++++++++++++++
> tools/perf/util/bpf-loader.h | 21 ++++++++++++
> 8 files changed, 141 insertions(+)
> create mode 100644 tools/perf/Documentation/perf-bpf.txt
> create mode 100644 tools/perf/builtin-bpf.c
> create mode 100644 tools/perf/util/bpf-loader.c
> create mode 100644 tools/perf/util/bpf-loader.h
>
> diff --git a/tools/perf/Build b/tools/perf/Build
> index b77370e..c69f0c1 100644
> --- a/tools/perf/Build
> +++ b/tools/perf/Build
> @@ -19,6 +19,7 @@ perf-y += builtin-kvm.o
> perf-y += builtin-inject.o
> perf-y += builtin-mem.o
> perf-y += builtin-data.o
> +perf-y += builtin-bpf.o
>
> perf-$(CONFIG_AUDIT) += builtin-trace.o
> perf-$(CONFIG_LIBELF) += builtin-probe.o
> diff --git a/tools/perf/Documentation/perf-bpf.txt b/tools/perf/Documentation/perf-bpf.txt
> new file mode 100644
> index 0000000..634d588
> --- /dev/null
> +++ b/tools/perf/Documentation/perf-bpf.txt
> @@ -0,0 +1,18 @@
> +perf-bpf(1)
> +==============
> +
> +NAME
> +----
> +perf-bpf - loads eBPF programs into kernel.
> +
> +SYNOPSIS
> +--------
> +[verse]
> +'perf bpf' [<common options>] <bpfprogram.o>",
> +
> +DESCRIPTION
> +-----------
> +Loading eBPF programs into kernel.
> +
> +OPTIONS
> +-------
> diff --git a/tools/perf/builtin-bpf.c b/tools/perf/builtin-bpf.c
> new file mode 100644
> index 0000000..0fc7a82
> --- /dev/null
> +++ b/tools/perf/builtin-bpf.c
> @@ -0,0 +1,63 @@
> +/*
> + * buildin-bpf.c

s/buildin/builtn/

> + *
> + * Buildin bpf command: Load bpf and attach bpf programs onto kprobes.

ditto.

> + */
> +#include "builtin.h"
> +#include "perf.h"
> +#include "debug.h"
> +#include "parse-options.h"
> +#include "bpf-loader.h"
> +
> +static const char *bpf_usage[] = {
> + "perf bpf [<options>] <bpfobj>",
> + NULL
> +};
> +
> +static void print_usage(void)
> +{
> + printf("Usage:\n");
> + printf("\t%s\n\n", bpf_usage[0]);
> +}

Why not using usage_with_options() for this?

Thanks,
Namhyung

> +
> +struct option __bpf_options[] = {
> + OPT_INCR('v', "verbose", &verbose, "be more verbose"),
> + OPT_END()
> +};
> +
> +struct option *bpf_options = __bpf_options;
> +
> +int cmd_bpf(int argc, const char **argv,
> + const char *prefix __maybe_unused)
> +{
> + int err;
> + const char **pfn;
> +
> + if (argc < 2)
> + goto usage;
> +
> + argc = parse_options(argc, argv, bpf_options, bpf_usage,
> + PARSE_OPT_STOP_AT_NON_OPTION);
> + if (argc < 1)
> + goto usage;
> +
> + pfn = argv;
> + while (*pfn != NULL) {
> + const char *fn = *pfn++;
> +
> + err = bpf__load(fn);
> + if (err) {
> + pr_err("bpf: load bpf program from %s: result: %d\n",
> + fn, err);
> + break;
> + }
> + }
> +
> + if (!err)
> + bpf__run();
> + return err;
> +usage:
> + print_usage();
> + return -1;
> +}
> +