arm64: dts: h3ulcb: Drop superfluous status update for frequency override
[linux-2.6/btrfs-unstable.git] / tools / perf / util / llvm-utils.c
blob824356488ce6a8c7d9ecda1748456b262f0a7114
1 /*
2 * Copyright (C) 2015, Wang Nan <wangnan0@huawei.com>
3 * Copyright (C) 2015, Huawei Inc.
4 */
6 #include <errno.h>
7 #include <limits.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <linux/err.h>
11 #include "debug.h"
12 #include "llvm-utils.h"
13 #include "config.h"
14 #include "util.h"
16 #define CLANG_BPF_CMD_DEFAULT_TEMPLATE \
17 "$CLANG_EXEC -D__KERNEL__ -D__NR_CPUS__=$NR_CPUS "\
18 "-DLINUX_VERSION_CODE=$LINUX_VERSION_CODE " \
19 "$CLANG_OPTIONS $KERNEL_INC_OPTIONS " \
20 "-Wno-unused-value -Wno-pointer-sign " \
21 "-working-directory $WORKING_DIR " \
22 "-c \"$CLANG_SOURCE\" -target bpf -O2 -o -"
24 struct llvm_param llvm_param = {
25 .clang_path = "clang",
26 .clang_bpf_cmd_template = CLANG_BPF_CMD_DEFAULT_TEMPLATE,
27 .clang_opt = NULL,
28 .kbuild_dir = NULL,
29 .kbuild_opts = NULL,
30 .user_set_param = false,
33 int perf_llvm_config(const char *var, const char *value)
35 if (prefixcmp(var, "llvm."))
36 return 0;
37 var += sizeof("llvm.") - 1;
39 if (!strcmp(var, "clang-path"))
40 llvm_param.clang_path = strdup(value);
41 else if (!strcmp(var, "clang-bpf-cmd-template"))
42 llvm_param.clang_bpf_cmd_template = strdup(value);
43 else if (!strcmp(var, "clang-opt"))
44 llvm_param.clang_opt = strdup(value);
45 else if (!strcmp(var, "kbuild-dir"))
46 llvm_param.kbuild_dir = strdup(value);
47 else if (!strcmp(var, "kbuild-opts"))
48 llvm_param.kbuild_opts = strdup(value);
49 else if (!strcmp(var, "dump-obj"))
50 llvm_param.dump_obj = !!perf_config_bool(var, value);
51 else {
52 pr_debug("Invalid LLVM config option: %s\n", value);
53 return -1;
55 llvm_param.user_set_param = true;
56 return 0;
59 static int
60 search_program(const char *def, const char *name,
61 char *output)
63 char *env, *path, *tmp = NULL;
64 char buf[PATH_MAX];
65 int ret;
67 output[0] = '\0';
68 if (def && def[0] != '\0') {
69 if (def[0] == '/') {
70 if (access(def, F_OK) == 0) {
71 strlcpy(output, def, PATH_MAX);
72 return 0;
74 } else if (def[0] != '\0')
75 name = def;
78 env = getenv("PATH");
79 if (!env)
80 return -1;
81 env = strdup(env);
82 if (!env)
83 return -1;
85 ret = -ENOENT;
86 path = strtok_r(env, ":", &tmp);
87 while (path) {
88 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
89 if (access(buf, F_OK) == 0) {
90 strlcpy(output, buf, PATH_MAX);
91 ret = 0;
92 break;
94 path = strtok_r(NULL, ":", &tmp);
97 free(env);
98 return ret;
101 #define READ_SIZE 4096
102 static int
103 read_from_pipe(const char *cmd, void **p_buf, size_t *p_read_sz)
105 int err = 0;
106 void *buf = NULL;
107 FILE *file = NULL;
108 size_t read_sz = 0, buf_sz = 0;
109 char serr[STRERR_BUFSIZE];
111 file = popen(cmd, "r");
112 if (!file) {
113 pr_err("ERROR: unable to popen cmd: %s\n",
114 str_error_r(errno, serr, sizeof(serr)));
115 return -EINVAL;
118 while (!feof(file) && !ferror(file)) {
120 * Make buf_sz always have obe byte extra space so we
121 * can put '\0' there.
123 if (buf_sz - read_sz < READ_SIZE + 1) {
124 void *new_buf;
126 buf_sz = read_sz + READ_SIZE + 1;
127 new_buf = realloc(buf, buf_sz);
129 if (!new_buf) {
130 pr_err("ERROR: failed to realloc memory\n");
131 err = -ENOMEM;
132 goto errout;
135 buf = new_buf;
137 read_sz += fread(buf + read_sz, 1, READ_SIZE, file);
140 if (buf_sz - read_sz < 1) {
141 pr_err("ERROR: internal error\n");
142 err = -EINVAL;
143 goto errout;
146 if (ferror(file)) {
147 pr_err("ERROR: error occurred when reading from pipe: %s\n",
148 str_error_r(errno, serr, sizeof(serr)));
149 err = -EIO;
150 goto errout;
153 err = WEXITSTATUS(pclose(file));
154 file = NULL;
155 if (err) {
156 err = -EINVAL;
157 goto errout;
161 * If buf is string, give it terminal '\0' to make our life
162 * easier. If buf is not string, that '\0' is out of space
163 * indicated by read_sz so caller won't even notice it.
165 ((char *)buf)[read_sz] = '\0';
167 if (!p_buf)
168 free(buf);
169 else
170 *p_buf = buf;
172 if (p_read_sz)
173 *p_read_sz = read_sz;
174 return 0;
176 errout:
177 if (file)
178 pclose(file);
179 free(buf);
180 if (p_buf)
181 *p_buf = NULL;
182 if (p_read_sz)
183 *p_read_sz = 0;
184 return err;
187 static inline void
188 force_set_env(const char *var, const char *value)
190 if (value) {
191 setenv(var, value, 1);
192 pr_debug("set env: %s=%s\n", var, value);
193 } else {
194 unsetenv(var);
195 pr_debug("unset env: %s\n", var);
199 static void
200 version_notice(void)
202 pr_err(
203 " \tLLVM 3.7 or newer is required. Which can be found from http://llvm.org\n"
204 " \tYou may want to try git trunk:\n"
205 " \t\tgit clone http://llvm.org/git/llvm.git\n"
206 " \t\t and\n"
207 " \t\tgit clone http://llvm.org/git/clang.git\n\n"
208 " \tOr fetch the latest clang/llvm 3.7 from pre-built llvm packages for\n"
209 " \tdebian/ubuntu:\n"
210 " \t\thttp://llvm.org/apt\n\n"
211 " \tIf you are using old version of clang, change 'clang-bpf-cmd-template'\n"
212 " \toption in [llvm] section of ~/.perfconfig to:\n\n"
213 " \t \"$CLANG_EXEC $CLANG_OPTIONS $KERNEL_INC_OPTIONS \\\n"
214 " \t -working-directory $WORKING_DIR -c $CLANG_SOURCE \\\n"
215 " \t -emit-llvm -o - | /path/to/llc -march=bpf -filetype=obj -o -\"\n"
216 " \t(Replace /path/to/llc with path to your llc)\n\n"
220 static int detect_kbuild_dir(char **kbuild_dir)
222 const char *test_dir = llvm_param.kbuild_dir;
223 const char *prefix_dir = "";
224 const char *suffix_dir = "";
226 char *autoconf_path;
228 int err;
230 if (!test_dir) {
231 /* _UTSNAME_LENGTH is 65 */
232 char release[128];
234 err = fetch_kernel_version(NULL, release,
235 sizeof(release));
236 if (err)
237 return -EINVAL;
239 test_dir = release;
240 prefix_dir = "/lib/modules/";
241 suffix_dir = "/build";
244 err = asprintf(&autoconf_path, "%s%s%s/include/generated/autoconf.h",
245 prefix_dir, test_dir, suffix_dir);
246 if (err < 0)
247 return -ENOMEM;
249 if (access(autoconf_path, R_OK) == 0) {
250 free(autoconf_path);
252 err = asprintf(kbuild_dir, "%s%s%s", prefix_dir, test_dir,
253 suffix_dir);
254 if (err < 0)
255 return -ENOMEM;
256 return 0;
258 free(autoconf_path);
259 return -ENOENT;
262 static const char *kinc_fetch_script =
263 "#!/usr/bin/env sh\n"
264 "if ! test -d \"$KBUILD_DIR\"\n"
265 "then\n"
266 " exit -1\n"
267 "fi\n"
268 "if ! test -f \"$KBUILD_DIR/include/generated/autoconf.h\"\n"
269 "then\n"
270 " exit -1\n"
271 "fi\n"
272 "TMPDIR=`mktemp -d`\n"
273 "if test -z \"$TMPDIR\"\n"
274 "then\n"
275 " exit -1\n"
276 "fi\n"
277 "cat << EOF > $TMPDIR/Makefile\n"
278 "obj-y := dummy.o\n"
279 "\\$(obj)/%.o: \\$(src)/%.c\n"
280 "\t@echo -n \"\\$(NOSTDINC_FLAGS) \\$(LINUXINCLUDE) \\$(EXTRA_CFLAGS)\"\n"
281 "EOF\n"
282 "touch $TMPDIR/dummy.c\n"
283 "make -s -C $KBUILD_DIR M=$TMPDIR $KBUILD_OPTS dummy.o 2>/dev/null\n"
284 "RET=$?\n"
285 "rm -rf $TMPDIR\n"
286 "exit $RET\n";
288 void llvm__get_kbuild_opts(char **kbuild_dir, char **kbuild_include_opts)
290 static char *saved_kbuild_dir;
291 static char *saved_kbuild_include_opts;
292 int err;
294 if (!kbuild_dir || !kbuild_include_opts)
295 return;
297 *kbuild_dir = NULL;
298 *kbuild_include_opts = NULL;
300 if (saved_kbuild_dir && saved_kbuild_include_opts &&
301 !IS_ERR(saved_kbuild_dir) && !IS_ERR(saved_kbuild_include_opts)) {
302 *kbuild_dir = strdup(saved_kbuild_dir);
303 *kbuild_include_opts = strdup(saved_kbuild_include_opts);
305 if (*kbuild_dir && *kbuild_include_opts)
306 return;
308 zfree(kbuild_dir);
309 zfree(kbuild_include_opts);
311 * Don't fall through: it may breaks saved_kbuild_dir and
312 * saved_kbuild_include_opts if detect them again when
313 * memory is low.
315 return;
318 if (llvm_param.kbuild_dir && !llvm_param.kbuild_dir[0]) {
319 pr_debug("[llvm.kbuild-dir] is set to \"\" deliberately.\n");
320 pr_debug("Skip kbuild options detection.\n");
321 goto errout;
324 err = detect_kbuild_dir(kbuild_dir);
325 if (err) {
326 pr_warning(
327 "WARNING:\tunable to get correct kernel building directory.\n"
328 "Hint:\tSet correct kbuild directory using 'kbuild-dir' option in [llvm]\n"
329 " \tsection of ~/.perfconfig or set it to \"\" to suppress kbuild\n"
330 " \tdetection.\n\n");
331 goto errout;
334 pr_debug("Kernel build dir is set to %s\n", *kbuild_dir);
335 force_set_env("KBUILD_DIR", *kbuild_dir);
336 force_set_env("KBUILD_OPTS", llvm_param.kbuild_opts);
337 err = read_from_pipe(kinc_fetch_script,
338 (void **)kbuild_include_opts,
339 NULL);
340 if (err) {
341 pr_warning(
342 "WARNING:\tunable to get kernel include directories from '%s'\n"
343 "Hint:\tTry set clang include options using 'clang-bpf-cmd-template'\n"
344 " \toption in [llvm] section of ~/.perfconfig and set 'kbuild-dir'\n"
345 " \toption in [llvm] to \"\" to suppress this detection.\n\n",
346 *kbuild_dir);
348 free(*kbuild_dir);
349 *kbuild_dir = NULL;
350 goto errout;
353 pr_debug("include option is set to %s\n", *kbuild_include_opts);
355 saved_kbuild_dir = strdup(*kbuild_dir);
356 saved_kbuild_include_opts = strdup(*kbuild_include_opts);
358 if (!saved_kbuild_dir || !saved_kbuild_include_opts) {
359 zfree(&saved_kbuild_dir);
360 zfree(&saved_kbuild_include_opts);
362 return;
363 errout:
364 saved_kbuild_dir = ERR_PTR(-EINVAL);
365 saved_kbuild_include_opts = ERR_PTR(-EINVAL);
368 int llvm__get_nr_cpus(void)
370 static int nr_cpus_avail = 0;
371 char serr[STRERR_BUFSIZE];
373 if (nr_cpus_avail > 0)
374 return nr_cpus_avail;
376 nr_cpus_avail = sysconf(_SC_NPROCESSORS_CONF);
377 if (nr_cpus_avail <= 0) {
378 pr_err(
379 "WARNING:\tunable to get available CPUs in this system: %s\n"
380 " \tUse 128 instead.\n", str_error_r(errno, serr, sizeof(serr)));
381 nr_cpus_avail = 128;
383 return nr_cpus_avail;
386 void llvm__dump_obj(const char *path, void *obj_buf, size_t size)
388 char *obj_path = strdup(path);
389 FILE *fp;
390 char *p;
392 if (!obj_path) {
393 pr_warning("WARNING: Not enough memory, skip object dumping\n");
394 return;
397 p = strrchr(obj_path, '.');
398 if (!p || (strcmp(p, ".c") != 0)) {
399 pr_warning("WARNING: invalid llvm source path: '%s', skip object dumping\n",
400 obj_path);
401 goto out;
404 p[1] = 'o';
405 fp = fopen(obj_path, "wb");
406 if (!fp) {
407 pr_warning("WARNING: failed to open '%s': %s, skip object dumping\n",
408 obj_path, strerror(errno));
409 goto out;
412 pr_info("LLVM: dumping %s\n", obj_path);
413 if (fwrite(obj_buf, size, 1, fp) != 1)
414 pr_warning("WARNING: failed to write to file '%s': %s, skip object dumping\n",
415 obj_path, strerror(errno));
416 fclose(fp);
417 out:
418 free(obj_path);
421 int llvm__compile_bpf(const char *path, void **p_obj_buf,
422 size_t *p_obj_buf_sz)
424 size_t obj_buf_sz;
425 void *obj_buf = NULL;
426 int err, nr_cpus_avail;
427 unsigned int kernel_version;
428 char linux_version_code_str[64];
429 const char *clang_opt = llvm_param.clang_opt;
430 char clang_path[PATH_MAX], abspath[PATH_MAX], nr_cpus_avail_str[64];
431 char serr[STRERR_BUFSIZE];
432 char *kbuild_dir = NULL, *kbuild_include_opts = NULL;
433 const char *template = llvm_param.clang_bpf_cmd_template;
435 if (path[0] != '-' && realpath(path, abspath) == NULL) {
436 err = errno;
437 pr_err("ERROR: problems with path %s: %s\n",
438 path, str_error_r(err, serr, sizeof(serr)));
439 return -err;
442 if (!template)
443 template = CLANG_BPF_CMD_DEFAULT_TEMPLATE;
445 err = search_program(llvm_param.clang_path,
446 "clang", clang_path);
447 if (err) {
448 pr_err(
449 "ERROR:\tunable to find clang.\n"
450 "Hint:\tTry to install latest clang/llvm to support BPF. Check your $PATH\n"
451 " \tand 'clang-path' option in [llvm] section of ~/.perfconfig.\n");
452 version_notice();
453 return -ENOENT;
457 * This is an optional work. Even it fail we can continue our
458 * work. Needn't to check error return.
460 llvm__get_kbuild_opts(&kbuild_dir, &kbuild_include_opts);
462 nr_cpus_avail = llvm__get_nr_cpus();
463 snprintf(nr_cpus_avail_str, sizeof(nr_cpus_avail_str), "%d",
464 nr_cpus_avail);
466 if (fetch_kernel_version(&kernel_version, NULL, 0))
467 kernel_version = 0;
469 snprintf(linux_version_code_str, sizeof(linux_version_code_str),
470 "0x%x", kernel_version);
472 force_set_env("NR_CPUS", nr_cpus_avail_str);
473 force_set_env("LINUX_VERSION_CODE", linux_version_code_str);
474 force_set_env("CLANG_EXEC", clang_path);
475 force_set_env("CLANG_OPTIONS", clang_opt);
476 force_set_env("KERNEL_INC_OPTIONS", kbuild_include_opts);
477 force_set_env("WORKING_DIR", kbuild_dir ? : ".");
480 * Since we may reset clang's working dir, path of source file
481 * should be transferred into absolute path, except we want
482 * stdin to be source file (testing).
484 force_set_env("CLANG_SOURCE",
485 (path[0] == '-') ? path : abspath);
487 pr_debug("llvm compiling command template: %s\n", template);
488 err = read_from_pipe(template, &obj_buf, &obj_buf_sz);
489 if (err) {
490 pr_err("ERROR:\tunable to compile %s\n", path);
491 pr_err("Hint:\tCheck error message shown above.\n");
492 pr_err("Hint:\tYou can also pre-compile it into .o using:\n");
493 pr_err(" \t\tclang -target bpf -O2 -c %s\n", path);
494 pr_err(" \twith proper -I and -D options.\n");
495 goto errout;
498 free(kbuild_dir);
499 free(kbuild_include_opts);
501 if (!p_obj_buf)
502 free(obj_buf);
503 else
504 *p_obj_buf = obj_buf;
506 if (p_obj_buf_sz)
507 *p_obj_buf_sz = obj_buf_sz;
508 return 0;
509 errout:
510 free(kbuild_dir);
511 free(kbuild_include_opts);
512 free(obj_buf);
513 if (p_obj_buf)
514 *p_obj_buf = NULL;
515 if (p_obj_buf_sz)
516 *p_obj_buf_sz = 0;
517 return err;
520 int llvm__search_clang(void)
522 char clang_path[PATH_MAX];
524 return search_program(llvm_param.clang_path, "clang", clang_path);