From: Wang Nan <wangnan0@huawei.com>
To: <acme@kernel.org>, <namhyung@kernel.org>
Cc: <lizefan@huawei.com>, <pi3orama@163.com>,
<linux-kernel@vger.kernel.org>, <jolsa@kernel.org>,
Wang Nan <wangnan0@huawei.com>,
Arnaldo Carvalho de Melo <acme@redhat.com>
Subject: [PATCH v2 3/7] perf tools: Make fetch_kernel_version() public available
Date: Fri, 6 Nov 2015 13:49:39 +0000 [thread overview]
Message-ID: <1446817783-86722-4-git-send-email-wangnan0@huawei.com> (raw)
In-Reply-To: <1446817783-86722-1-git-send-email-wangnan0@huawei.com>
There are 2 places in llvm-utils.c which find kernel version
information through uname. This patch extracts uname related code
into one fetch_kernel_version() function and put it into util.h
so other part of code can use it.
Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
---
tools/perf/util/llvm-utils.c | 49 +++++++++++++++-----------------------------
tools/perf/util/util.c | 31 ++++++++++++++++++++++++++++
tools/perf/util/util.h | 3 +++
3 files changed, 50 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 <stdio.h>
-#include <sys/utsname.h>
#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..c0ee81c 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -3,6 +3,7 @@
#include "debug.h"
#include <api/fs/fs.h>
#include <sys/mman.h>
+#include <sys/utsname.h>
#ifdef HAVE_BACKTRACE_SUPPORT
#include <execinfo.h>
#endif
@@ -665,3 +666,33 @@ 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 */
--
1.8.3.4
next prev parent reply other threads:[~2015-11-06 13:51 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-11-06 13:49 [PATCH v2 0/7] perf tools: improve BPF related error delivering and testing Wang Nan
2015-11-06 13:49 ` [PATCH v2 1/7] bpf tools: Improve libbpf error reporting Wang Nan
2015-11-06 14:06 ` Arnaldo Carvalho de Melo
2015-11-08 7:32 ` [tip:perf/urgent] " tip-bot for Wang Nan
2015-11-06 13:49 ` [PATCH v2 2/7] bpf tools: Add new API bpf_object__get_kversion() Wang Nan
2015-11-08 7:32 ` [tip:perf/urgent] bpf tools: Add new API bpf_object__get_kversion () tip-bot for Wang Nan
2015-11-06 13:49 ` Wang Nan [this message]
2015-11-06 13:55 ` [PATCH v2 3/7 fix] perf tools: Make fetch_kernel_version() public available Wang Nan
2015-11-08 7:33 ` [tip:perf/urgent] perf tools: Make fetch_kernel_version() publicly available tip-bot for Wang Nan
2015-11-06 13:49 ` [PATCH v2 4/7] perf tools: Improve BPF related error messages output Wang Nan
2015-11-06 13:58 ` [PATCH v2 4/7 fix] " Wang Nan
2015-11-08 7:33 ` [tip:perf/urgent] perf bpf: Improve BPF related error messages tip-bot for Wang Nan
2015-11-06 13:49 ` [PATCH v2 5/7] perf test: Enforce LLVM test: update basic BPF test program Wang Nan
2015-11-08 7:34 ` [tip:perf/urgent] perf test: Enhance the " tip-bot for Wang Nan
2015-11-06 13:49 ` [PATCH v2 6/7] perf test: Enforce LLVM test: add kbuild test Wang Nan
2015-11-08 7:34 ` [tip:perf/urgent] perf test: Enhance the LLVM tests: " tip-bot for Wang Nan
2015-11-06 13:49 ` [PATCH v2 7/7] perf test: Add 'perf test BPF' Wang Nan
2015-11-08 7:34 ` [tip:perf/urgent] " tip-bot for Wang Nan
2015-11-06 19:20 ` [PATCH v2 0/7] perf tools: improve BPF related error delivering and testing Arnaldo Carvalho de Melo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1446817783-86722-4-git-send-email-wangnan0@huawei.com \
--to=wangnan0@huawei.com \
--cc=acme@kernel.org \
--cc=acme@redhat.com \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lizefan@huawei.com \
--cc=namhyung@kernel.org \
--cc=pi3orama@163.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®