From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S966541AbcIZHaj (ORCPT ); Mon, 26 Sep 2016 03:30:39 -0400 Received: from szxga02-in.huawei.com ([119.145.14.65]:15875 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1161079AbcIZH2V (ORCPT ); Mon, 26 Sep 2016 03:28:21 -0400 From: Wang Nan To: , CC: , , , Wang Nan , Arnaldo Carvalho de Melo , Alexei Starovoitov , He Kuang , Jiri Olsa Subject: [PATCH v2 00/18] perf clang: Support compiling BPF script on the fly Date: Mon, 26 Sep 2016 07:26:54 +0000 Message-ID: <1474874832-134786-1-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A0B0201.57E8CDF0.0102,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2013-06-18 04:22:30, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 382935c1cdaba6f6192a777c3937289a Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 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/linux-kernel@vger.kernel.org/msg1238358.html (should be http://lkml.kernel.org/g/1474635001-153850-1-git-send-email-wangnan0@huawei.com 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 Cc: Arnaldo Carvalho de Melo Cc: Alexei Starovoitov Cc: He Kuang Cc: Jiri Olsa -- 1.8.3.4