Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf tools: Introduce llvm config options

This patch introduces [llvm] config section with 5 options. Following
patches will use then to config llvm dynamica compiling.

'llvm-utils.[ch]' is introduced in this patch for holding all
llvm/clang related stuffs.

Example:

[llvm]
# Path to clang. If omit, search it from $PATH.
clang-path = "/path/to/clang"

# Cmdline template. Following line shows its default value.
# Environment variable is used to passing options.
#
# *NOTE*: -D__KERNEL__ MUST appears before $CLANG_OPTIONS,
# so user have a chance to use -U__KERNEL__ in $CLANG_OPTIONS
# to cancel it.
clang-bpf-cmd-template = "$CLANG_EXEC -D__KERNEL__ $CLANG_OPTIONS \
$KERNEL_INC_OPTIONS -Wno-unused-value \
-Wno-pointer-sign -working-directory \
$WORKING_DIR -c $CLANG_SOURCE -target \
bpf -O2 -o -"

# Options passed to clang, will be passed to cmdline by
# $CLANG_OPTIONS.
clang-opt = "-Wno-unused-value -Wno-pointer-sign"

# kbuild directory. If not set, use /lib/modules/`uname -r`/build.
# If set to "" deliberately, skip kernel header auto-detector.
kbuild-dir = "/path/to/kernel/build"

# Options passed to 'make' when detecting kernel header options.
kbuild-opts = "ARCH=x86_64"

Signed-off-by: Wang Nan <wangnan0@huawei.com>
Acked-by: Alexei Starovoitov <ast@plumgrid.com>
Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: David Ahern <dsahern@gmail.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kaixu Xia <xiakaixu@huawei.com>
Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1437477214-149684-1-git-send-email-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Wang Nan and committed by
Arnaldo Carvalho de Melo
aa61fd05 9a208eff

+86
+1
tools/perf/util/Build
··· 14 14 libperf-y += help.o 15 15 libperf-y += kallsyms.o 16 16 libperf-y += levenshtein.o 17 + libperf-y += llvm-utils.o 17 18 libperf-y += parse-options.o 18 19 libperf-y += parse-events.o 19 20 libperf-y += path.o
+4
tools/perf/util/config.c
··· 12 12 #include "cache.h" 13 13 #include "exec_cmd.h" 14 14 #include "util/hist.h" /* perf_hist_config */ 15 + #include "util/llvm-utils.h" /* perf_llvm_config */ 15 16 16 17 #define MAXNAME (256) 17 18 ··· 408 407 409 408 if (!prefixcmp(var, "call-graph.")) 410 409 return perf_callchain_config(var, value); 410 + 411 + if (!prefixcmp(var, "llvm.")) 412 + return perf_llvm_config(var, value); 411 413 412 414 /* Add other config variables here. */ 413 415 return 0;
+45
tools/perf/util/llvm-utils.c
··· 1 + /* 2 + * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com> 3 + * Copyright (C) 2015, Huawei Inc. 4 + */ 5 + 6 + #include <stdio.h> 7 + #include "util.h" 8 + #include "debug.h" 9 + #include "llvm-utils.h" 10 + #include "cache.h" 11 + 12 + #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \ 13 + "$CLANG_EXEC -D__KERNEL__ $CLANG_OPTIONS " \ 14 + "$KERNEL_INC_OPTIONS -Wno-unused-value " \ 15 + "-Wno-pointer-sign -working-directory " \ 16 + "$WORKING_DIR -c \"$CLANG_SOURCE\" -target bpf -O2 -o -" 17 + 18 + struct llvm_param llvm_param = { 19 + .clang_path = "clang", 20 + .clang_bpf_cmd_template = CLANG_BPF_CMD_DEFAULT_TEMPLATE, 21 + .clang_opt = NULL, 22 + .kbuild_dir = NULL, 23 + .kbuild_opts = NULL, 24 + }; 25 + 26 + int perf_llvm_config(const char *var, const char *value) 27 + { 28 + if (prefixcmp(var, "llvm.")) 29 + return 0; 30 + var += sizeof("llvm.") - 1; 31 + 32 + if (!strcmp(var, "clang-path")) 33 + llvm_param.clang_path = strdup(value); 34 + else if (!strcmp(var, "clang-bpf-cmd-template")) 35 + llvm_param.clang_bpf_cmd_template = strdup(value); 36 + else if (!strcmp(var, "clang-opt")) 37 + llvm_param.clang_opt = strdup(value); 38 + else if (!strcmp(var, "kbuild-dir")) 39 + llvm_param.kbuild_dir = strdup(value); 40 + else if (!strcmp(var, "kbuild-opts")) 41 + llvm_param.kbuild_opts = strdup(value); 42 + else 43 + return -1; 44 + return 0; 45 + }
+36
tools/perf/util/llvm-utils.h
··· 1 + /* 2 + * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com> 3 + * Copyright (C) 2015, Huawei Inc. 4 + */ 5 + #ifndef __LLVM_UTILS_H 6 + #define __LLVM_UTILS_H 7 + 8 + #include "debug.h" 9 + 10 + struct llvm_param { 11 + /* Path of clang executable */ 12 + const char *clang_path; 13 + /* 14 + * Template of clang bpf compiling. 5 env variables 15 + * can be used: 16 + * $CLANG_EXEC: Path to clang. 17 + * $CLANG_OPTIONS: Extra options to clang. 18 + * $KERNEL_INC_OPTIONS: Kernel include directories. 19 + * $WORKING_DIR: Kernel source directory. 20 + * $CLANG_SOURCE: Source file to be compiled. 21 + */ 22 + const char *clang_bpf_cmd_template; 23 + /* Will be filled in $CLANG_OPTIONS */ 24 + const char *clang_opt; 25 + /* Where to find kbuild system */ 26 + const char *kbuild_dir; 27 + /* 28 + * Arguments passed to make, like 'ARCH=arm' if doing cross 29 + * compiling. Should not be used for dynamic compiling. 30 + */ 31 + const char *kbuild_opts; 32 + }; 33 + 34 + extern struct llvm_param llvm_param; 35 + extern int perf_llvm_config(const char *var, const char *value); 36 + #endif