Re: [PATCH] perf tools: Add file-handle feature test

From: Arnaldo Carvalho de Melo
Date: Thu Apr 02 2020 - 11:37:55 EST


Em Thu, Apr 02, 2020 at 10:52:49AM +0900, Namhyung Kim escreveu:
> The file handle (FHANDLE) support is configurable so some systems might
> not have it. So add a config feature item to check it on build time
> and reject cgroup tracking based on that.

Ok, I'll break this patch in two, add the feature test first, then fold
the usage of HAVE_FILE_HANDLE with the patch that uses it, so that we
keep the codebase bisectable,

Thanks!

- Arnaldo

> Reported-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
> Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx>
> ---
> tools/build/Makefile.feature | 3 ++-
> tools/build/feature/Makefile | 6 +++++-
> tools/build/feature/test-file-handle.c | 14 ++++++++++++++
> tools/perf/Makefile.config | 4 ++++
> tools/perf/builtin-record.c | 8 +++++++-
> tools/perf/builtin-top.c | 8 +++++++-
> tools/perf/util/synthetic-events.c | 9 +++++++++
> 7 files changed, 48 insertions(+), 4 deletions(-)
> create mode 100644 tools/build/feature/test-file-handle.c
>
> diff --git a/tools/build/Makefile.feature b/tools/build/Makefile.feature
> index 574c2e0b9d20..3e0c019ef297 100644
> --- a/tools/build/Makefile.feature
> +++ b/tools/build/Makefile.feature
> @@ -72,7 +72,8 @@ FEATURE_TESTS_BASIC := \
> setns \
> libaio \
> libzstd \
> - disassembler-four-args
> + disassembler-four-args \
> + file-handle
>
> # FEATURE_TESTS_BASIC + FEATURE_TESTS_EXTRA is the complete list
> # of all feature tests
> diff --git a/tools/build/feature/Makefile b/tools/build/feature/Makefile
> index 7ac0d8088565..621f528f7822 100644
> --- a/tools/build/feature/Makefile
> +++ b/tools/build/feature/Makefile
> @@ -67,7 +67,8 @@ FILES= \
> test-llvm.bin \
> test-llvm-version.bin \
> test-libaio.bin \
> - test-libzstd.bin
> + test-libzstd.bin \
> + test-file-handle.bin
>
> FILES := $(addprefix $(OUTPUT),$(FILES))
>
> @@ -321,6 +322,9 @@ FLAGS_PERL_EMBED=$(PERL_EMBED_CCOPTS) $(PERL_EMBED_LDOPTS)
> $(OUTPUT)test-libzstd.bin:
> $(BUILD) -lzstd
>
> +$(OUTPUT)test-file-handle.bin:
> + $(BUILD)
> +
> ###############################
>
> clean:
> diff --git a/tools/build/feature/test-file-handle.c b/tools/build/feature/test-file-handle.c
> new file mode 100644
> index 000000000000..5f8c16f8784f
> --- /dev/null
> +++ b/tools/build/feature/test-file-handle.c
> @@ -0,0 +1,14 @@
> +#define _GNU_SOURCE
> +#include <sys/types.h>
> +#include <sys/stat.h>
> +#include <fcntl.h>
> +
> +int main(void)
> +{
> + struct file_handle fh;
> + int mount_id;
> +
> + name_to_handle_at(AT_FDCWD, "/", &fh, &mount_id, 0);
> + return 0;
> +}
> +
> diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config
> index 80e55e796be9..eb95c0c0a169 100644
> --- a/tools/perf/Makefile.config
> +++ b/tools/perf/Makefile.config
> @@ -348,6 +348,10 @@ ifeq ($(feature-gettid), 1)
> CFLAGS += -DHAVE_GETTID
> endif
>
> +ifeq ($(feature-file-handle), 1)
> + CFLAGS += -DHAVE_FILE_HANDLE
> +endif
> +
> ifdef NO_LIBELF
> NO_DWARF := 1
> NO_DEMANGLE := 1
> diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
> index 7d7912e121d6..1ab349abe904 100644
> --- a/tools/perf/builtin-record.c
> +++ b/tools/perf/builtin-record.c
> @@ -1433,8 +1433,14 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
> if (rec->opts.record_namespaces)
> tool->namespace_events = true;
>
> - if (rec->opts.record_cgroup)
> + if (rec->opts.record_cgroup) {
> +#ifdef HAVE_FILE_HANDLE
> tool->cgroup_events = true;
> +#else
> + pr_err("cgroup tracking is not supported\n");
> + return -1;
> +#endif
> + }
>
> if (rec->opts.auxtrace_snapshot_mode || rec->switch_output.enabled) {
> signal(SIGUSR2, snapshot_sig_handler);
> diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
> index 56b2dd0db88e..02ea2cf2a3d9 100644
> --- a/tools/perf/builtin-top.c
> +++ b/tools/perf/builtin-top.c
> @@ -1246,8 +1246,14 @@ static int __cmd_top(struct perf_top *top)
>
> if (opts->record_namespaces)
> top->tool.namespace_events = true;
> - if (opts->record_cgroup)
> + if (opts->record_cgroup) {
> +#ifdef HAVE_FILE_HANDLE
> top->tool.cgroup_events = true;
> +#else
> + pr_err("cgroup tracking is not supported.\n");
> + return -1;
> +#endif
> + }
>
> ret = perf_event__synthesize_bpf_events(top->session, perf_event__process,
> &top->session->machines.host,
> diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic-events.c
> index 24975470ed5c..f96e84956d84 100644
> --- a/tools/perf/util/synthetic-events.c
> +++ b/tools/perf/util/synthetic-events.c
> @@ -415,6 +415,7 @@ int perf_event__synthesize_mmap_events(struct perf_tool *tool,
> return rc;
> }
>
> +#ifdef HAVE_FILE_HANDLE
> static int perf_event__synthesize_cgroup(struct perf_tool *tool,
> union perf_event *event,
> char *path, size_t mount_len,
> @@ -526,6 +527,14 @@ int perf_event__synthesize_cgroups(struct perf_tool *tool,
>
> return 0;
> }
> +#else
> +int perf_event__synthesize_cgroups(struct perf_tool *tool,
> + perf_event__handler_t process,
> + struct machine *machine)
> +{
> + return -1;
> +}
> +#endif
>
> int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t process,
> struct machine *machine)
> --
> 2.26.0.rc2.310.g2932bb562d-goog
>

--

- Arnaldo