diff options
author | Wang Nan <wangnan0@huawei.com> | 2015-11-06 08:55:35 -0500 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2015-11-06 13:57:18 -0500 |
commit | 07bc5c699a3d8fe5e26dbcd72e4103c7988055ba (patch) | |
tree | fac957d198bae637f821596f3e2d9b8d7f3474ec /tools/perf | |
parent | 45825d8ab8ef6287f5d05aea141419d8d4278852 (diff) |
perf tools: Make fetch_kernel_version() publicly available
There are 2 places in llvm-utils.c which find kernel version information
through uname. This patch extracts the uname related code into a
fetch_kernel_version() function and puts it into util.h so it can be
reused.
Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/1446818135-87310-1-git-send-email-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf')
-rw-r--r-- | tools/perf/util/llvm-utils.c | 49 | ||||
-rw-r--r-- | tools/perf/util/util.c | 30 | ||||
-rw-r--r-- | tools/perf/util/util.h | 3 |
3 files changed, 49 insertions, 33 deletions
diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c index 8ee25bea1b24..00724d496d38 100644 --- a/tools/perf/util/llvm-utils.c +++ b/tools/perf/util/llvm-utils.c | |||
@@ -4,7 +4,6 @@ | |||
4 | */ | 4 | */ |
5 | 5 | ||
6 | #include <stdio.h> | 6 | #include <stdio.h> |
7 | #include <sys/utsname.h> | ||
8 | #include "util.h" | 7 | #include "util.h" |
9 | #include "debug.h" | 8 | #include "debug.h" |
10 | #include "llvm-utils.h" | 9 | #include "llvm-utils.h" |
@@ -216,18 +215,19 @@ static int detect_kbuild_dir(char **kbuild_dir) | |||
216 | const char *suffix_dir = ""; | 215 | const char *suffix_dir = ""; |
217 | 216 | ||
218 | char *autoconf_path; | 217 | char *autoconf_path; |
219 | struct utsname utsname; | ||
220 | 218 | ||
221 | int err; | 219 | int err; |
222 | 220 | ||
223 | if (!test_dir) { | 221 | if (!test_dir) { |
224 | err = uname(&utsname); | 222 | /* _UTSNAME_LENGTH is 65 */ |
225 | if (err) { | 223 | char release[128]; |
226 | pr_warning("uname failed: %s\n", strerror(errno)); | 224 | |
225 | err = fetch_kernel_version(NULL, release, | ||
226 | sizeof(release)); | ||
227 | if (err) | ||
227 | return -EINVAL; | 228 | return -EINVAL; |
228 | } | ||
229 | 229 | ||
230 | test_dir = utsname.release; | 230 | test_dir = release; |
231 | prefix_dir = "/lib/modules/"; | 231 | prefix_dir = "/lib/modules/"; |
232 | suffix_dir = "/build"; | 232 | suffix_dir = "/build"; |
233 | } | 233 | } |
@@ -325,38 +325,18 @@ get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts) | |||
325 | pr_debug("include option is set to %s\n", *kbuild_include_opts); | 325 | pr_debug("include option is set to %s\n", *kbuild_include_opts); |
326 | } | 326 | } |
327 | 327 | ||
328 | static unsigned long | ||
329 | fetch_kernel_version(void) | ||
330 | { | ||
331 | struct utsname utsname; | ||
332 | int version, patchlevel, sublevel, err; | ||
333 | |||
334 | if (uname(&utsname)) | ||
335 | return 0; | ||
336 | |||
337 | err = sscanf(utsname.release, "%d.%d.%d", | ||
338 | &version, &patchlevel, &sublevel); | ||
339 | |||
340 | if (err != 3) { | ||
341 | pr_debug("Unablt to get kernel version from uname '%s'\n", | ||
342 | utsname.release); | ||
343 | return 0; | ||
344 | } | ||
345 | |||
346 | return (version << 16) + (patchlevel << 8) + sublevel; | ||
347 | } | ||
348 | |||
349 | int llvm__compile_bpf(const char *path, void **p_obj_buf, | 328 | int llvm__compile_bpf(const char *path, void **p_obj_buf, |
350 | size_t *p_obj_buf_sz) | 329 | size_t *p_obj_buf_sz) |
351 | { | 330 | { |
331 | size_t obj_buf_sz; | ||
332 | void *obj_buf = NULL; | ||
352 | int err, nr_cpus_avail; | 333 | int err, nr_cpus_avail; |
353 | char clang_path[PATH_MAX], nr_cpus_avail_str[64]; | 334 | unsigned int kernel_version; |
354 | char linux_version_code_str[64]; | 335 | char linux_version_code_str[64]; |
355 | const char *clang_opt = llvm_param.clang_opt; | 336 | const char *clang_opt = llvm_param.clang_opt; |
356 | const char *template = llvm_param.clang_bpf_cmd_template; | 337 | char clang_path[PATH_MAX], nr_cpus_avail_str[64]; |
357 | char *kbuild_dir = NULL, *kbuild_include_opts = NULL; | 338 | char *kbuild_dir = NULL, *kbuild_include_opts = NULL; |
358 | void *obj_buf = NULL; | 339 | const char *template = llvm_param.clang_bpf_cmd_template; |
359 | size_t obj_buf_sz; | ||
360 | 340 | ||
361 | if (!template) | 341 | if (!template) |
362 | template = CLANG_BPF_CMD_DEFAULT_TEMPLATE; | 342 | template = CLANG_BPF_CMD_DEFAULT_TEMPLATE; |
@@ -388,8 +368,11 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf, | |||
388 | snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d", | 368 | snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d", |
389 | nr_cpus_avail); | 369 | nr_cpus_avail); |
390 | 370 | ||
371 | if (fetch_kernel_version(&kernel_version, NULL, 0)) | ||
372 | kernel_version = 0; | ||
373 | |||
391 | snprintf(linux_version_code_str, sizeof(linux_version_code_str), | 374 | snprintf(linux_version_code_str, sizeof(linux_version_code_str), |
392 | "0x%lx", fetch_kernel_version()); | 375 | "0x%x", kernel_version); |
393 | 376 | ||
394 | force_set_env("NR_CPUS", nr_cpus_avail_str); | 377 | force_set_env("NR_CPUS", nr_cpus_avail_str); |
395 | force_set_env("LINUX_VERSION_CODE", linux_version_code_str); | 378 | force_set_env("LINUX_VERSION_CODE", linux_version_code_str); |
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index cd12c25e4ea4..47b1e36c7ea0 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c | |||
@@ -3,6 +3,7 @@ | |||
3 | #include "debug.h" | 3 | #include "debug.h" |
4 | #include <api/fs/fs.h> | 4 | #include <api/fs/fs.h> |
5 | #include <sys/mman.h> | 5 | #include <sys/mman.h> |
6 | #include <sys/utsname.h> | ||
6 | #ifdef HAVE_BACKTRACE_SUPPORT | 7 | #ifdef HAVE_BACKTRACE_SUPPORT |
7 | #include <execinfo.h> | 8 | #include <execinfo.h> |
8 | #endif | 9 | #endif |
@@ -665,3 +666,32 @@ bool find_process(const char *name) | |||
665 | closedir(dir); | 666 | closedir(dir); |
666 | return ret ? false : true; | 667 | return ret ? false : true; |
667 | } | 668 | } |
669 | |||
670 | int | ||
671 | fetch_kernel_version(unsigned int *puint, char *str, | ||
672 | size_t str_size) | ||
673 | { | ||
674 | struct utsname utsname; | ||
675 | int version, patchlevel, sublevel, err; | ||
676 | |||
677 | if (uname(&utsname)) | ||
678 | return -1; | ||
679 | |||
680 | if (str && str_size) { | ||
681 | strncpy(str, utsname.release, str_size); | ||
682 | str[str_size - 1] = '\0'; | ||
683 | } | ||
684 | |||
685 | err = sscanf(utsname.release, "%d.%d.%d", | ||
686 | &version, &patchlevel, &sublevel); | ||
687 | |||
688 | if (err != 3) { | ||
689 | pr_debug("Unablt to get kernel version from uname '%s'\n", | ||
690 | utsname.release); | ||
691 | return -1; | ||
692 | } | ||
693 | |||
694 | if (puint) | ||
695 | *puint = (version << 16) + (patchlevel << 8) + sublevel; | ||
696 | return 0; | ||
697 | } | ||
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 4cfb913aa9e0..2665126267dc 100644 --- a/tools/perf/util/util.h +++ b/tools/perf/util/util.h | |||
@@ -350,4 +350,7 @@ static inline char *asprintf_expr_not_in_ints(const char *var, size_t nints, int | |||
350 | 350 | ||
351 | int get_stack_size(const char *str, unsigned long *_size); | 351 | int get_stack_size(const char *str, unsigned long *_size); |
352 | 352 | ||
353 | int fetch_kernel_version(unsigned int *puint, | ||
354 | char *str, size_t str_sz); | ||
355 | |||
353 | #endif /* GIT_COMPAT_UTIL_H */ | 356 | #endif /* GIT_COMPAT_UTIL_H */ |