Skip to content

perf clang: Support compiling BPF script on the fly

Wang Nan lkml 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest18 patches 2 repliesIn review
  2. v114 patches 5 repliesSuperseded

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/18tools build: Support compiling C++ source filetools/build/Build.include tools/build/Makefile.build+8 -00 replies
2/18perf tools: Add feature detection for g++tools/build/Makefile.feature tools/build/feature/Makefile tools/build/feature/test-cxx.cpp+25 -20 replies
3/18perf tools: Add feature detection for LLVMtools/build/feature/Makefile tools/build/feature/test-llvm.cpp+16 -00 replies
4/18perf tools: Add feature detection for clangtools/build/feature/Makefile tools/build/feature/test-clang.cpp+31 -00 replies
5/18perf build: Add clang and llvm compile and linking supporttools/perf/Makefile.config tools/perf/Makefile.perf tools/perf/tests/make+59 -10 replies
6/18perf clang: Add builtin clang support ant test casetools/perf/tests/Build tools/perf/tests/builtin-test.c tools/perf/tests/clang.c +7 more+218 -00 replies
7/18perf clang: Use real file system for #includetools/perf/util/c++/clang.cpp tools/perf/util/c++/clang.h+35 -120 replies
8/18perf clang: Allow passing CFLAGS to builtin clangtools/perf/util/c++/clang-test.cpp tools/perf/util/c++/clang.cpp tools/perf/util/c++/clang.h+22 -120 replies
9/18perf clang: Update test case to use real BPF scripttools/perf/Makefile.config tools/perf/tests/llvm-cxx.h tools/perf/util/c++/clang-test.cpp +1 more+68 -150 replies
10/18perf clang: Support compile IR to BPF object and add testcasetools/perf/tests/clang.c tools/perf/util/c++/clang-c.h tools/perf/util/c++/clang-test.cpp +2 more+80 -70 replies
11/18perf tools: Extract helpers in llvm-utils.ctools/perf/util/llvm-utils.c tools/perf/util/llvm-utils.h+62 -130 replies
12/18perf bpf: Compile BPF script use builtin clang supporttools/perf/util/bpf-loader.c tools/perf/util/c++/clang-c.h tools/perf/util/c++/clang.cpp+66 -40 replies
13/18perf clang: Pass full path to builtin clangtools/perf/util/c++/clang.cpp+6 -20 replies
14/18perf clang: Pass CFLAGS to builtin clangtools/perf/util/c++/clang.cpp tools/perf/util/llvm-utils-cxx.h tools/perf/util/llvm-utils.h+116 -50 replies
15/18perf clang: Link BPF functions declaration into perftools/perf/util/c++/Build tools/perf/util/c++/bpf-funcs-str.c tools/perf/util/c++/clang-bpf-includes.h+227 -00 replies
16/18perf clang: Declare BPF functions for BPF scripts automaticallytools/perf/tests/bpf-script-example.c tools/perf/tests/bpf-script-test-prologue.c tools/perf/tests/bpf-script-test-relocation.c +1 more+55 -150 replies
17/18perf clang: Include helpers to BPF scriptstools/perf/tests/bpf-script-example.c tools/perf/tests/bpf-script-test-kbuild.c tools/perf/tests/bpf-script-test-prologue.c +5 more+24 -00 replies
18/18perf clang: Define PERF_BUILTIN_CLANG for builtin clang compilingtools/perf/util/c++/clang.cpp+1 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v2 yet.

Read the v2 thread

Cover letter [PATCH v2 00/18] perf clang: Support compiling BPF script on the fly

This patch add builtin clang, allow perf compile BPF scripts on the fly. This is the first step to implement what I announced at LinuxCon 2016 NA:

http://events.linuxfoundation.org/sites/events/files/slides/Performance%20Monitoring%20and%20Analysis%20Using%20perf%20and%20BPF_1.pdf

Compare with v1:
1. Fix API usage so can be built at Ubuntu 16.04 (with llvm-dev and libclang-dev installed)
2. Introduce default include files so BPF script writer doesn't need define many BPF functions by their own.

Test:

 # cat ./test_bpf_output.c
 /************************ BEGIN **************************/
 #include <uapi/linux/bpf.h>
 struct bpf_map_def SEC("maps") __bpf_stdout__ = {
        .type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
        .key_size = sizeof(int),
        .value_size = sizeof(u32),
        .max_entries = __NR_CPUS__,
 };

static inline int __attribute__((always_inline)) func(void *ctx, int type)

 {
	char output_str[] = "Raise a BPF event!";

        bpf_perf_event_output(ctx, &__bpf_stdout__, bpf_get_smp_processor_id(),
			  &output_str, sizeof(output_str));
        return 0;
 }
 SEC("func_begin=sys_nanosleep")
 int func_begin(void *ctx) {return func(ctx, 1);}
 SEC("func_end=sys_nanosleep%return")
 int func_end(void *ctx) { return func(ctx, 2);}
 char _license[] SEC("license") = "GPL";
 int _version SEC("version") = LINUX_VERSION_CODE;
 /************************* END ***************************/
 # perf trace --event ./test_bpf_output.c usleep 10
 ...
 0.449 ( 0.002 ms): usleep/827 getuid(                                                               ) = 0
 0.482 ( 0.006 ms): usleep/827 nanosleep(rqtp: 0x7ffecc22fa50                                        ) ...
 0.482 (         ): __bpf_stdout__:Raise a BPF event!..)
 0.555 (         ): __bpf_stdout__:Raise a BPF event!..)
 0.557 ( 0.081 ms): usleep/827  ... [continued]: nanosleep() = 0
 0.562 ( 0.000 ms): usleep/827 exit_group(                                                           )

v1 can be found at:
https://www.mail-archive.com/[email protected]/msg1238358.html
(should be http://lkml.kernel.org/g/[email protected]
but the link is broken)

Wang Nan (18):
tools build: Support compiling C++ source file
perf tools: Add feature detection for g++
perf tools: Add feature detection for LLVM
perf tools: Add feature detection for clang
perf build: Add clang and llvm compile and linking support perf clang: Add builtin clang support ant test case perf clang: Use real file system for #include
perf clang: Allow passing CFLAGS to builtin clang
perf clang: Update test case to use real BPF script perf clang: Support compile IR to BPF object and add testcase perf tools: Extract helpers in llvm-utils.c
perf bpf: Compile BPF script use builtin clang support perf clang: Pass full path to builtin clang
perf clang: Pass CFLAGS to builtin clang
perf clang: Link BPF functions declaration into perf perf clang: Declare BPF functions for BPF scripts automatically perf clang: Include helpers to BPF scripts
perf clang: Define PERF_BUILTIN_CLANG for builtin clang compiling

 tools/build/Build.include                     |   1 +
 tools/build/Makefile.build                    |   7 +
 tools/build/Makefile.feature                  |   2 +-
 tools/build/feature/Makefile                  |  28 ++-
 tools/build/feature/test-clang.cpp            |  21 ++
 tools/build/feature/test-cxx.cpp              |  15 ++
 tools/build/feature/test-llvm.cpp             |   8 +
 tools/perf/Makefile.config                    |  62 ++++-
 tools/perf/Makefile.perf                      |  23 +-
 tools/perf/tests/Build                        |   1 +
 tools/perf/tests/bpf-script-example.c         |  20 +-
 tools/perf/tests/bpf-script-test-kbuild.c     |   2 +
 tools/perf/tests/bpf-script-test-prologue.c   |   4 +
 tools/perf/tests/bpf-script-test-relocation.c |  20 +-
 tools/perf/tests/builtin-test.c               |   9 +
 tools/perf/tests/clang.c                      |  46 ++++
 tools/perf/tests/llvm-cxx.h                   |  13 +
 tools/perf/tests/make                         |   2 +
 tools/perf/tests/tests.h                      |   3 +
 tools/perf/util/Build                         |   2 +
 tools/perf/util/bpf-loader.c                  |  15 +-
 tools/perf/util/c++/Build                     |   4 +
 tools/perf/util/c++/bpf-funcs-str.c           | 214 +++++++++++++++++
 tools/perf/util/c++/bpf-helper-str.c          |  13 +
 tools/perf/util/c++/clang-bpf-includes.h      |  13 +
 tools/perf/util/c++/clang-c.h                 |  43 ++++
 tools/perf/util/c++/clang-test.cpp            |  62 +++++
 tools/perf/util/c++/clang.cpp                 | 329 ++++++++++++++++++++++++++
 tools/perf/util/c++/clang.h                   |  26 ++
 tools/perf/util/llvm-utils-cxx.h              |  14 ++
 tools/perf/util/llvm-utils.c                  |  70 +++++-
 tools/perf/util/llvm-utils.h                  |   7 +-
 tools/perf/util/util-cxx.h                    |  26 ++
 33 files changed, 1078 insertions(+), 47 deletions(-)
 create mode 100644 tools/build/feature/test-clang.cpp
 create mode 100644 tools/build/feature/test-cxx.cpp
 create mode 100644 tools/build/feature/test-llvm.cpp
 create mode 100644 tools/perf/tests/clang.c
 create mode 100644 tools/perf/tests/llvm-cxx.h
 create mode 100644 tools/perf/util/c++/Build
 create mode 100644 tools/perf/util/c++/bpf-funcs-str.c
 create mode 100644 tools/perf/util/c++/bpf-helper-str.c
 create mode 100644 tools/perf/util/c++/clang-bpf-includes.h
 create mode 100644 tools/perf/util/c++/clang-c.h
 create mode 100644 tools/perf/util/c++/clang-test.cpp
 create mode 100644 tools/perf/util/c++/clang.cpp
 create mode 100644 tools/perf/util/c++/clang.h
 create mode 100644 tools/perf/util/llvm-utils-cxx.h
 create mode 100644 tools/perf/util/util-cxx.h
Cc Wang Nan <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Alexei Starovoitov <[email protected]>
Cc He Kuang <[email protected]>
Cc Jiri Olsa <[email protected]>
Signature · 1 line
1.8.3.4