Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758331AbbKHHag (ORCPT ); Sun, 8 Nov 2015 02:30:36 -0500 Received: from terminus.zytor.com ([198.137.202.10]:45999 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754668AbbKHHae (ORCPT ); Sun, 8 Nov 2015 02:30:34 -0500 Date: Sat, 7 Nov 2015 23:30:08 -0800 From: tip-bot for Wang Nan Message-ID: Cc: mingo@kernel.org, wangnan0@huawei.com, hpa@zytor.com, acme@redhat.com, lizefan@huawei.com, tglx@linutronix.de, linux-kernel@vger.kernel.org, namhyung@kernel.org, ast@kernel.org Reply-To: hpa@zytor.com, wangnan0@huawei.com, mingo@kernel.org, linux-kernel@vger.kernel.org, ast@kernel.org, namhyung@kernel.org, tglx@linutronix.de, lizefan@huawei.com, acme@redhat.com In-Reply-To: <1446636007-239722-3-git-send-email-wangnan0@huawei.com> References: <1446636007-239722-3-git-send-email-wangnan0@huawei.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/urgent] perf llvm: Pass LINUX_VERSION_CODE to BPF program when compiling Git-Commit-ID: 4a4f66a1a7031675745812729ade94ad1caf1db6 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4689 Lines: 134 Commit-ID: 4a4f66a1a7031675745812729ade94ad1caf1db6 Gitweb: http://git.kernel.org/tip/4a4f66a1a7031675745812729ade94ad1caf1db6 Author: Wang Nan AuthorDate: Wed, 4 Nov 2015 11:20:05 +0000 Committer: Arnaldo Carvalho de Melo CommitDate: Thu, 5 Nov 2015 12:47:50 -0300 perf llvm: Pass LINUX_VERSION_CODE to BPF program when compiling Arnaldo suggests to make LINUX_VERSION_CODE works like __func__ and __FILE__ so user don't need to care setting right linux version too much. In this patch, perf llvm transfers LINUX_VERSION_CODE macro through clang cmdline. [1] http://lkml.kernel.org/r/20151029223744.GK2923@kernel.org Committer notes: Before, forgetting to update the version: # uname -r 4.3.0-rc1+ # cat bpf.c __attribute__((section("fork=_do_fork"), used)) int fork(void *ctx) { return 1; } char _license[] __attribute__((section("license"), used)) = "GPL"; int _version __attribute__((section("version"), used)) = 0x40200; # # perf record -e bpf.c sleep 1 event syntax error: 'bpf.c' \___ Invalid argument: Are you root and runing a CONFIG_BPF_SYSCALL kernel? (add -v to see detail) Run 'perf list' for a list of valid events Usage: perf record [] [] or: perf record [] -- [] -e, --event event selector. use 'perf list' to list available events # After: # grep version bpf.c int _version __attribute__((section("version"), used)) = LINUX_VERSION_CODE; # perf record -e bpf.c sleep 1 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.017 MB perf.data ] # perf evlist -v perf_bpf_probe:fork: type: 2, size: 112, config: 0x5ee, { sample_period, sample_freq }: 1, sample_type: IP|TID|TIME|CPU|PERIOD|RAW, disabled: 1, inherit: 1, mmap: 1, comm: 1, enable_on_exec: 1, task: 1, sample_id_all: 1, exclude_guest: 1, mmap2: 1, comm_exec: 1 # Suggested-and-Tested-by: Arnaldo Carvalho de Melo Signed-off-by: Wang Nan Cc: Alexei Starovoitov Cc: Namhyung Kim Cc: Zefan Li Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/1446636007-239722-3-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/llvm-utils.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c index 80eecef..8ee25be 100644 --- a/tools/perf/util/llvm-utils.c +++ b/tools/perf/util/llvm-utils.c @@ -12,6 +12,7 @@ #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \ "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\ + "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \ "$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \ "-Wno-unused-value -Wno-pointer-sign " \ "-working-directory $WORKING_DIR " \ @@ -324,11 +325,33 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts) pr_debug("include option is set to %s\n", *kbuild_include_opts); } +static unsigned long +fetch_kernel_version(void) +{ + struct utsname utsname; + int version, patchlevel, sublevel, err; + + if (uname(&utsname)) + return 0; + + err = sscanf(utsname.release, "%d.%d.%d", + &version, &patchlevel, &sublevel); + + if (err != 3) { + pr_debug("Unablt to get kernel version from uname '%s'\n", + utsname.release); + return 0; + } + + return (version << 16) + (patchlevel << 8) + sublevel; +} + int llvm__compile_bpf(const char *path, void **p_obj_buf, size_t *p_obj_buf_sz) { int err, nr_cpus_avail; char clang_path[PATH_MAX], nr_cpus_avail_str[64]; + char linux_version_code_str[64]; const char *clang_opt = llvm_param.clang_opt; const char *template = llvm_param.clang_bpf_cmd_template; char *kbuild_dir = NULL, *kbuild_include_opts = NULL; @@ -365,7 +388,11 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf, snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d", nr_cpus_avail); + snprintf(linux_version_code_str, sizeof(linux_version_code_str), + "0x%lx", fetch_kernel_version()); + force_set_env("NR_CPUS", nr_cpus_avail_str); + force_set_env("LINUX_VERSION_CODE", linux_version_code_str); force_set_env("CLANG_EXEC", clang_path); force_set_env("CLANG_OPTIONS", clang_opt); force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts); -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/