aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/llvm-utils.c
diff options
context:
space:
mode:
authorWang Nan <wangnan0@huawei.com>2016-11-26 02:03:30 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2016-12-05 13:51:42 -0500
commit2bd42de0e196f89994cbae1990d2c7c5a9b6a529 (patch)
tree38f5ca69a0cd1f64eb89715d6a1933eb5c63ee6c /tools/perf/util/llvm-utils.c
parent8ad85e9e6fdaf996bf3ff60303ea00e696bcdd36 (diff)
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>
Diffstat (limited to 'tools/perf/util/llvm-utils.c')
-rw-r--r--tools/perf/util/llvm-utils.c76
1 files changed, 58 insertions, 18 deletions
diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c
index 27b6f303720a..b23ff44cf214 100644
--- a/tools/perf/util/llvm-utils.c
+++ b/tools/perf/util/llvm-utils.c
@@ -7,6 +7,7 @@
7#include <limits.h> 7#include <limits.h>
8#include <stdio.h> 8#include <stdio.h>
9#include <stdlib.h> 9#include <stdlib.h>
10#include <linux/err.h>
10#include "debug.h" 11#include "debug.h"
11#include "llvm-utils.h" 12#include "llvm-utils.h"
12#include "config.h" 13#include "config.h"
@@ -282,9 +283,10 @@ static const char *kinc_fetch_script =
282"rm -rf $TMPDIR\n" 283"rm -rf $TMPDIR\n"
283"exit $RET\n"; 284"exit $RET\n";
284 285
285static inline void 286void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
286get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
287{ 287{
288 static char *saved_kbuild_dir;
289 static char *saved_kbuild_include_opts;
288 int err; 290 int err;
289 291
290 if (!kbuild_dir || !kbuild_include_opts) 292 if (!kbuild_dir || !kbuild_include_opts)
@@ -293,10 +295,28 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
293 *kbuild_dir = NULL; 295 *kbuild_dir = NULL;
294 *kbuild_include_opts = NULL; 296 *kbuild_include_opts = NULL;
295 297
298 if (saved_kbuild_dir && saved_kbuild_include_opts &&
299 !IS_ERR(saved_kbuild_dir) && !IS_ERR(saved_kbuild_include_opts)) {
300 *kbuild_dir = strdup(saved_kbuild_dir);
301 *kbuild_include_opts = strdup(saved_kbuild_include_opts);
302
303 if (*kbuild_dir && *kbuild_include_opts)
304 return;
305
306 zfree(kbuild_dir);
307 zfree(kbuild_include_opts);
308 /*
309 * Don't fall through: it may breaks saved_kbuild_dir and
310 * saved_kbuild_include_opts if detect them again when
311 * memory is low.
312 */
313 return;
314 }
315
296 if (llvm_param.kbuild_dir && !llvm_param.kbuild_dir[0]) { 316 if (llvm_param.kbuild_dir && !llvm_param.kbuild_dir[0]) {
297 pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n"); 317 pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n");
298 pr_debug("Skip kbuild options detection.\n"); 318 pr_debug("Skip kbuild options detection.\n");
299 return; 319 goto errout;
300 } 320 }
301 321
302 err = detect_kbuild_dir(kbuild_dir); 322 err = detect_kbuild_dir(kbuild_dir);
@@ -306,7 +326,7 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
306"Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n" 326"Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n"
307" \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n" 327" \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n"
308" \tdetection.\n\n"); 328" \tdetection.\n\n");
309 return; 329 goto errout;
310 } 330 }
311 331
312 pr_debug("Kernel build dir is set to %s\n", *kbuild_dir); 332 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)
325 345
326 free(*kbuild_dir); 346 free(*kbuild_dir);
327 *kbuild_dir = NULL; 347 *kbuild_dir = NULL;
328 return; 348 goto errout;
329 } 349 }
330 350
331 pr_debug("include option is set to %s\n", *kbuild_include_opts); 351 pr_debug("include option is set to %s\n", *kbuild_include_opts);
352
353 saved_kbuild_dir = strdup(*kbuild_dir);
354 saved_kbuild_include_opts = strdup(*kbuild_include_opts);
355
356 if (!saved_kbuild_dir || !saved_kbuild_include_opts) {
357 zfree(&saved_kbuild_dir);
358 zfree(&saved_kbuild_include_opts);
359 }
360 return;
361errout:
362 saved_kbuild_dir = ERR_PTR(-EINVAL);
363 saved_kbuild_include_opts = ERR_PTR(-EINVAL);
332} 364}
333 365
334static void 366int llvm__get_nr_cpus(void)
335dump_obj(const char *path, void *obj_buf, size_t size) 367{
368 static int nr_cpus_avail = 0;
369 char serr[STRERR_BUFSIZE];
370
371 if (nr_cpus_avail > 0)
372 return nr_cpus_avail;
373
374 nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
375 if (nr_cpus_avail <= 0) {
376 pr_err(
377"WARNING:\tunable to get available CPUs in this system: %s\n"
378" \tUse 128 instead.\n", str_error_r(errno, serr, sizeof(serr)));
379 nr_cpus_avail = 128;
380 }
381 return nr_cpus_avail;
382}
383
384void llvm__dump_obj(const char *path, void *obj_buf, size_t size)
336{ 385{
337 char *obj_path = strdup(path); 386 char *obj_path = strdup(path);
338 FILE *fp; 387 FILE *fp;
@@ -406,15 +455,9 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
406 * This is an optional work. Even it fail we can continue our 455 * This is an optional work. Even it fail we can continue our
407 * work. Needn't to check error return. 456 * work. Needn't to check error return.
408 */ 457 */
409 get_kbuild_opts(&kbuild_dir, &kbuild_include_opts); 458 llvm__get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
410 459
411 nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF); 460 nr_cpus_avail = llvm__get_nr_cpus();
412 if (nr_cpus_avail <= 0) {
413 pr_err(
414"WARNING:\tunable to get available CPUs in this system: %s\n"
415" \tUse 128 instead.\n", str_error_r(errno, serr, sizeof(serr)));
416 nr_cpus_avail = 128;
417 }
418 snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d", 461 snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
419 nr_cpus_avail); 462 nr_cpus_avail);
420 463
@@ -453,9 +496,6 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
453 free(kbuild_dir); 496 free(kbuild_dir);
454 free(kbuild_include_opts); 497 free(kbuild_include_opts);
455 498
456 if (llvm_param.dump_obj)
457 dump_obj(path, obj_buf, obj_buf_sz);
458
459 if (!p_obj_buf) 499 if (!p_obj_buf)
460 free(obj_buf); 500 free(obj_buf);
461 else 501 else