Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758519AbbKHHdl (ORCPT ); Sun, 8 Nov 2015 02:33:41 -0500 Received: from terminus.zytor.com ([198.137.202.10]:46109 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754854AbbKHHdh (ORCPT ); Sun, 8 Nov 2015 02:33:37 -0500 Date: Sat, 7 Nov 2015 23:33:19 -0800 From: tip-bot for Wang Nan Message-ID: Cc: tglx@linutronix.de, wangnan0@huawei.com, acme@redhat.com, mingo@kernel.org, linux-kernel@vger.kernel.org, lizefan@huawei.com, namhyung@kernel.org, jolsa@kernel.org, hpa@zytor.com Reply-To: lizefan@huawei.com, namhyung@kernel.org, jolsa@kernel.org, hpa@zytor.com, wangnan0@huawei.com, tglx@linutronix.de, acme@redhat.com, mingo@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <1446818135-87310-1-git-send-email-wangnan0@huawei.com> References: <1446818135-87310-1-git-send-email-wangnan0@huawei.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/urgent] perf tools: Make fetch_kernel_version() publicly available Git-Commit-ID: 07bc5c699a3d8fe5e26dbcd72e4103c7988055ba 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: 5466 Lines: 184 Commit-ID: 07bc5c699a3d8fe5e26dbcd72e4103c7988055ba Gitweb: http://git.kernel.org/tip/07bc5c699a3d8fe5e26dbcd72e4103c7988055ba Author: Wang Nan AuthorDate: Fri, 6 Nov 2015 13:55:35 +0000 Committer: Arnaldo Carvalho de Melo CommitDate: Fri, 6 Nov 2015 15:57:18 -0300 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 Cc: Jiri Olsa Cc: Namhyung Kim Cc: Zefan Li 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 --- tools/perf/util/llvm-utils.c | 49 +++++++++++++++----------------------------- tools/perf/util/util.c | 30 +++++++++++++++++++++++++++ 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 8ee25be..00724d4 100644 --- a/tools/perf/util/llvm-utils.c +++ b/tools/perf/util/llvm-utils.c @@ -4,7 +4,6 @@ */ #include -#include #include "util.h" #include "debug.h" #include "llvm-utils.h" @@ -216,18 +215,19 @@ static int detect_kbuild_dir(char **kbuild_dir) const char *suffix_dir = ""; char *autoconf_path; - struct utsname utsname; int err; if (!test_dir) { - err = uname(&utsname); - if (err) { - pr_warning("uname failed: %s\n", strerror(errno)); + /* _UTSNAME_LENGTH is 65 */ + char release[128]; + + err = fetch_kernel_version(NULL, release, + sizeof(release)); + if (err) return -EINVAL; - } - test_dir = utsname.release; + test_dir = release; prefix_dir = "/lib/modules/"; suffix_dir = "/build"; } @@ -325,38 +325,18 @@ 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) { + size_t obj_buf_sz; + void *obj_buf = NULL; int err, nr_cpus_avail; - char clang_path[PATH_MAX], nr_cpus_avail_str[64]; + unsigned int kernel_version; char linux_version_code_str[64]; const char *clang_opt = llvm_param.clang_opt; - const char *template = llvm_param.clang_bpf_cmd_template; + char clang_path[PATH_MAX], nr_cpus_avail_str[64]; char *kbuild_dir = NULL, *kbuild_include_opts = NULL; - void *obj_buf = NULL; - size_t obj_buf_sz; + const char *template = llvm_param.clang_bpf_cmd_template; if (!template) template = CLANG_BPF_CMD_DEFAULT_TEMPLATE; @@ -388,8 +368,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); + if (fetch_kernel_version(&kernel_version, NULL, 0)) + kernel_version = 0; + snprintf(linux_version_code_str, sizeof(linux_version_code_str), - "0x%lx", fetch_kernel_version()); + "0x%x", kernel_version); force_set_env("NR_CPUS", nr_cpus_avail_str); 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 cd12c25..47b1e36 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c @@ -3,6 +3,7 @@ #include "debug.h" #include #include +#include #ifdef HAVE_BACKTRACE_SUPPORT #include #endif @@ -665,3 +666,32 @@ bool find_process(const char *name) closedir(dir); return ret ? false : true; } + +int +fetch_kernel_version(unsigned int *puint, char *str, + size_t str_size) +{ + struct utsname utsname; + int version, patchlevel, sublevel, err; + + if (uname(&utsname)) + return -1; + + if (str && str_size) { + strncpy(str, utsname.release, str_size); + str[str_size - 1] = '\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 -1; + } + + if (puint) + *puint = (version << 16) + (patchlevel << 8) + sublevel; + return 0; +} diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h index 4cfb913..2665126 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 int get_stack_size(const char *str, unsigned long *_size); +int fetch_kernel_version(unsigned int *puint, + char *str, size_t str_sz); + #endif /* GIT_COMPAT_UTIL_H */ -- 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/