perf llvm: Extract helpers in llvm-utils.c
The following commits will use builtin clang to compile BPF scripts. llvm__get_kbuild_opts() and llvm__get_nr_cpus() are extracted to help building '-DKERNEL_VERSION_CODE' and '-D__NR_CPUS__' macros. Doing object dumping in bpf loader, so further builtin clang compiling needn't consider it. Signed-off-by: Wang Nan <wangnan0@huawei.com> Cc: Alexei Starovoitov <ast@fb.com> Cc: He Kuang <hekuang@huawei.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Joe Stringer <joe@ovn.org> Cc: Zefan Li <lizefan@huawei.com> Cc: pi3orama@163.com Link: http://lkml.kernel.org/r/20161126070354.141764-7-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
parent
8ad85e9e6f
commit
2bd42de0e1
|
@ -90,6 +90,10 @@ struct bpf_object *bpf__prepare_load(const char *filename, bool source)
|
|||
if (err)
|
||||
return ERR_PTR(-BPF_LOADER_ERRNO__COMPILE);
|
||||
obj = bpf_object__open_buffer(obj_buf, obj_buf_sz, filename);
|
||||
|
||||
if (!IS_ERR(obj) && llvm_param.dump_obj)
|
||||
llvm__dump_obj(filename, obj_buf, obj_buf_sz);
|
||||
|
||||
free(obj_buf);
|
||||
} else
|
||||
obj = bpf_object__open(filename);
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <linux/err.h>
|
||||
#include "debug.h"
|
||||
#include "llvm-utils.h"
|
||||
#include "config.h"
|
||||
|
@ -282,9 +283,10 @@ static const char *kinc_fetch_script =
|
|||
"rm -rf $TMPDIR\n"
|
||||
"exit $RET\n";
|
||||
|
||||
static inline void
|
||||
get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
|
||||
void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
|
||||
{
|
||||
static char *saved_kbuild_dir;
|
||||
static char *saved_kbuild_include_opts;
|
||||
int err;
|
||||
|
||||
if (!kbuild_dir || !kbuild_include_opts)
|
||||
|
@ -293,10 +295,28 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
|
|||
*kbuild_dir = NULL;
|
||||
*kbuild_include_opts = NULL;
|
||||
|
||||
if (saved_kbuild_dir && saved_kbuild_include_opts &&
|
||||
!IS_ERR(saved_kbuild_dir) && !IS_ERR(saved_kbuild_include_opts)) {
|
||||
*kbuild_dir = strdup(saved_kbuild_dir);
|
||||
*kbuild_include_opts = strdup(saved_kbuild_include_opts);
|
||||
|
||||
if (*kbuild_dir && *kbuild_include_opts)
|
||||
return;
|
||||
|
||||
zfree(kbuild_dir);
|
||||
zfree(kbuild_include_opts);
|
||||
/*
|
||||
* Don't fall through: it may breaks saved_kbuild_dir and
|
||||
* saved_kbuild_include_opts if detect them again when
|
||||
* memory is low.
|
||||
*/
|
||||
return;
|
||||
}
|
||||
|
||||
if (llvm_param.kbuild_dir && !llvm_param.kbuild_dir[0]) {
|
||||
pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n");
|
||||
pr_debug("Skip kbuild options detection.\n");
|
||||
return;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
err = detect_kbuild_dir(kbuild_dir);
|
||||
|
@ -306,7 +326,7 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
|
|||
"Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n"
|
||||
" \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n"
|
||||
" \tdetection.\n\n");
|
||||
return;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
pr_debug("Kernel build dir is set to %s\n", *kbuild_dir);
|
||||
|
@ -325,14 +345,43 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
|
|||
|
||||
free(*kbuild_dir);
|
||||
*kbuild_dir = NULL;
|
||||
return;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
pr_debug("include option is set to %s\n", *kbuild_include_opts);
|
||||
|
||||
saved_kbuild_dir = strdup(*kbuild_dir);
|
||||
saved_kbuild_include_opts = strdup(*kbuild_include_opts);
|
||||
|
||||
if (!saved_kbuild_dir || !saved_kbuild_include_opts) {
|
||||
zfree(&saved_kbuild_dir);
|
||||
zfree(&saved_kbuild_include_opts);
|
||||
}
|
||||
return;
|
||||
errout:
|
||||
saved_kbuild_dir = ERR_PTR(-EINVAL);
|
||||
saved_kbuild_include_opts = ERR_PTR(-EINVAL);
|
||||
}
|
||||
|
||||
static void
|
||||
dump_obj(const char *path, void *obj_buf, size_t size)
|
||||
int llvm__get_nr_cpus(void)
|
||||
{
|
||||
static int nr_cpus_avail = 0;
|
||||
char serr[STRERR_BUFSIZE];
|
||||
|
||||
if (nr_cpus_avail > 0)
|
||||
return nr_cpus_avail;
|
||||
|
||||
nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
|
||||
if (nr_cpus_avail <= 0) {
|
||||
pr_err(
|
||||
"WARNING:\tunable to get available CPUs in this system: %s\n"
|
||||
" \tUse 128 instead.\n", str_error_r(errno, serr, sizeof(serr)));
|
||||
nr_cpus_avail = 128;
|
||||
}
|
||||
return nr_cpus_avail;
|
||||
}
|
||||
|
||||
void llvm__dump_obj(const char *path, void *obj_buf, size_t size)
|
||||
{
|
||||
char *obj_path = strdup(path);
|
||||
FILE *fp;
|
||||
|
@ -406,15 +455,9 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
|
|||
* This is an optional work. Even it fail we can continue our
|
||||
* work. Needn't to check error return.
|
||||
*/
|
||||
get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
|
||||
llvm__get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
|
||||
|
||||
nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
|
||||
if (nr_cpus_avail <= 0) {
|
||||
pr_err(
|
||||
"WARNING:\tunable to get available CPUs in this system: %s\n"
|
||||
" \tUse 128 instead.\n", str_error_r(errno, serr, sizeof(serr)));
|
||||
nr_cpus_avail = 128;
|
||||
}
|
||||
nr_cpus_avail = llvm__get_nr_cpus();
|
||||
snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
|
||||
nr_cpus_avail);
|
||||
|
||||
|
@ -453,9 +496,6 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
|
|||
free(kbuild_dir);
|
||||
free(kbuild_include_opts);
|
||||
|
||||
if (llvm_param.dump_obj)
|
||||
dump_obj(path, obj_buf, obj_buf_sz);
|
||||
|
||||
if (!p_obj_buf)
|
||||
free(obj_buf);
|
||||
else
|
||||
|
|
|
@ -50,4 +50,10 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf, size_t *p_obj_buf_sz);
|
|||
|
||||
/* This function is for test__llvm() use only */
|
||||
int llvm__search_clang(void);
|
||||
|
||||
/* Following functions are reused by builtin clang support */
|
||||
void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts);
|
||||
int llvm__get_nr_cpus(void);
|
||||
|
||||
void llvm__dump_obj(const char *path, void *obj_buf, size_t size);
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue