blob: ead9509835d23e2aee29b9f3548613973a5937fb [file] [log] [blame]
Joerg Roedel1aed2672012-01-04 17:54:20 +01001#include "../perf.h"
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -02002#include "util.h"
Jiri Olsa84f5d362014-07-14 23:46:48 +02003#include "debug.h"
Borislav Petkovcd0cfad2013-12-09 17:14:24 +01004#include <api/fs/fs.h>
Frederic Weisbecker69e3f522010-01-16 14:21:15 +01005#include <sys/mman.h>
Wang Nan07bc5c62015-11-06 13:55:35 +00006#include <sys/utsname.h>
Ingo Molnar89fe8082013-09-30 12:07:11 +02007#ifdef HAVE_BACKTRACE_SUPPORT
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -03008#include <execinfo.h>
Irina Tirdeac9f08be2012-09-08 03:43:23 +03009#endif
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -030010#include <stdio.h>
11#include <stdlib.h>
Jiri Olsacef82c92013-12-03 14:09:22 +010012#include <string.h>
13#include <errno.h>
Adrian Hunter1a472452013-12-11 14:36:23 +020014#include <limits.h>
Adrian Hunter71db07b2013-12-11 14:36:32 +020015#include <byteswap.h>
Jiri Olsa838d1452013-11-28 11:30:15 +010016#include <linux/kernel.h>
Jiri Olsa9398c482014-08-11 10:50:02 +020017#include <unistd.h>
Jiri Olsa23aadb12014-10-01 18:00:26 +020018#include "callchain.h"
Namhyung Kim14cbfbe2016-01-07 20:41:53 +090019#include "strlist.h"
Jiri Olsa23aadb12014-10-01 18:00:26 +020020
21struct callchain_param callchain_param = {
Arnaldo Carvalho de Melodef02db2015-10-05 17:05:35 -030022 .mode = CHAIN_GRAPH_ABS,
Jiri Olsa23aadb12014-10-01 18:00:26 +020023 .min_percent = 0.5,
Namhyung Kim792aeaf2015-10-22 16:45:46 +090024 .order = ORDER_CALLEE,
Namhyung Kimf2af0082015-11-09 14:45:41 +090025 .key = CCKEY_FUNCTION,
26 .value = CCVAL_PERCENT,
Jiri Olsa23aadb12014-10-01 18:00:26 +020027};
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020028
Joerg Roedel1aed2672012-01-04 17:54:20 +010029/*
30 * XXX We need to find a better place for these things...
31 */
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -030032unsigned int page_size;
Don Zickus2b1b7102014-05-30 16:10:05 -040033int cacheline_size;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -030034
Arnaldo Carvalho de Melo0c6332e2012-12-13 16:43:04 -030035bool test_attr__enabled;
36
Joerg Roedel1aed2672012-01-04 17:54:20 +010037bool perf_host = true;
Joerg Roedelc4a7dca2012-02-10 18:05:05 +010038bool perf_guest = false;
Joerg Roedel1aed2672012-01-04 17:54:20 +010039
40void event_attr_init(struct perf_event_attr *attr)
41{
42 if (!perf_host)
43 attr->exclude_host = 1;
44 if (!perf_guest)
45 attr->exclude_guest = 1;
Stephane Eranian7e1ccd32012-02-09 16:12:38 +010046 /* to capture ABI version */
47 attr->size = sizeof(*attr);
Joerg Roedel1aed2672012-01-04 17:54:20 +010048}
49
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020050int mkdir_p(char *path, mode_t mode)
51{
52 struct stat st;
53 int err;
54 char *d = path;
55
56 if (*d != '/')
57 return -1;
58
59 if (stat(path, &st) == 0)
60 return 0;
61
62 while (*++d == '/');
63
64 while ((d = strchr(d, '/'))) {
65 *d = '\0';
66 err = stat(path, &st) && mkdir(path, mode);
67 *d++ = '/';
68 if (err)
69 return -1;
70 while (*d == '/')
71 ++d;
72 }
73 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
74}
75
Namhyung Kim0b1de0be2015-05-18 09:30:17 +090076int rm_rf(char *path)
77{
78 DIR *dir;
79 int ret = 0;
80 struct dirent *d;
81 char namebuf[PATH_MAX];
82
83 dir = opendir(path);
84 if (dir == NULL)
85 return 0;
86
87 while ((d = readdir(dir)) != NULL && !ret) {
88 struct stat statbuf;
89
90 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
91 continue;
92
93 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
94 path, d->d_name);
95
96 ret = stat(namebuf, &statbuf);
97 if (ret < 0) {
98 pr_debug("stat failed: %s\n", namebuf);
99 break;
100 }
101
102 if (S_ISREG(statbuf.st_mode))
103 ret = unlink(namebuf);
104 else if (S_ISDIR(statbuf.st_mode))
105 ret = rm_rf(namebuf);
106 else {
107 pr_debug("unknown file: %s\n", namebuf);
108 ret = -1;
109 }
110 }
111 closedir(dir);
112
113 if (ret < 0)
114 return ret;
115
116 return rmdir(path);
117}
118
Milos Vyleteld7c72602015-06-08 16:50:16 +0200119static int slow_copyfile(const char *from, const char *to)
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200120{
Adrian Hunter9a17d722013-10-14 13:43:41 +0300121 int err = -1;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200122 char *line = NULL;
123 size_t n;
124 FILE *from_fp = fopen(from, "r"), *to_fp;
125
126 if (from_fp == NULL)
127 goto out;
128
129 to_fp = fopen(to, "w");
130 if (to_fp == NULL)
131 goto out_fclose_from;
132
133 while (getline(&line, &n, from_fp) > 0)
134 if (fputs(line, to_fp) == EOF)
135 goto out_fclose_to;
136 err = 0;
137out_fclose_to:
138 fclose(to_fp);
139 free(line);
140out_fclose_from:
141 fclose(from_fp);
142out:
143 return err;
144}
145
Namhyung Kim9c9f5a22015-05-18 09:30:18 +0900146int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
147{
148 void *ptr;
149 loff_t pgoff;
150
151 pgoff = off_in & ~(page_size - 1);
152 off_in -= pgoff;
153
154 ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
155 if (ptr == MAP_FAILED)
156 return -1;
157
158 while (size) {
159 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
160 if (ret < 0 && errno == EINTR)
161 continue;
162 if (ret <= 0)
163 break;
164
165 size -= ret;
166 off_in += ret;
167 off_out -= ret;
168 }
169 munmap(ptr, off_in + size);
170
171 return size ? -1 : 0;
172}
173
Adrian Hunter9a17d722013-10-14 13:43:41 +0300174int copyfile_mode(const char *from, const char *to, mode_t mode)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200175{
176 int fromfd, tofd;
177 struct stat st;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200178 int err = -1;
Milos Vyleteld7c72602015-06-08 16:50:16 +0200179 char *tmp = NULL, *ptr = NULL;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200180
181 if (stat(from, &st))
182 goto out;
183
Milos Vyleteld7c72602015-06-08 16:50:16 +0200184 /* extra 'x' at the end is to reserve space for '.' */
185 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
186 tmp = NULL;
187 goto out;
188 }
189 ptr = strrchr(tmp, '/');
190 if (!ptr)
191 goto out;
192 ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
193 *ptr = '.';
194
195 tofd = mkstemp(tmp);
196 if (tofd < 0)
197 goto out;
198
199 if (fchmod(tofd, mode))
200 goto out_close_to;
201
202 if (st.st_size == 0) { /* /proc? do it slowly... */
203 err = slow_copyfile(from, tmp);
204 goto out_close_to;
205 }
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200206
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200207 fromfd = open(from, O_RDONLY);
208 if (fromfd < 0)
Milos Vyleteld7c72602015-06-08 16:50:16 +0200209 goto out_close_to;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200210
Namhyung Kim9c9f5a22015-05-18 09:30:18 +0900211 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200212
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200213 close(fromfd);
Milos Vyleteld7c72602015-06-08 16:50:16 +0200214out_close_to:
215 close(tofd);
216 if (!err)
217 err = link(tmp, to);
218 unlink(tmp);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200219out:
Milos Vyleteld7c72602015-06-08 16:50:16 +0200220 free(tmp);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200221 return err;
222}
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300223
Adrian Hunter9a17d722013-10-14 13:43:41 +0300224int copyfile(const char *from, const char *to)
225{
226 return copyfile_mode(from, to, 0755);
227}
228
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300229unsigned long convert_unit(unsigned long value, char *unit)
230{
231 *unit = ' ';
232
233 if (value > 1000) {
234 value /= 1000;
235 *unit = 'K';
236 }
237
238 if (value > 1000) {
239 value /= 1000;
240 *unit = 'M';
241 }
242
243 if (value > 1000) {
244 value /= 1000;
245 *unit = 'G';
246 }
247
248 return value;
249}
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200250
Jiri Olsabc3a5022013-11-28 11:30:16 +0100251static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200252{
253 void *buf_start = buf;
Jiri Olsa838d1452013-11-28 11:30:15 +0100254 size_t left = n;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200255
Jiri Olsa838d1452013-11-28 11:30:15 +0100256 while (left) {
Jiri Olsabc3a5022013-11-28 11:30:16 +0100257 ssize_t ret = is_read ? read(fd, buf, left) :
258 write(fd, buf, left);
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200259
Namhyung Kime148c762014-04-24 22:27:32 +0900260 if (ret < 0 && errno == EINTR)
261 continue;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200262 if (ret <= 0)
263 return ret;
264
Jiri Olsa838d1452013-11-28 11:30:15 +0100265 left -= ret;
266 buf += ret;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200267 }
268
Jiri Olsa838d1452013-11-28 11:30:15 +0100269 BUG_ON((size_t)(buf - buf_start) != n);
270 return n;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200271}
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -0300272
Jiri Olsabc3a5022013-11-28 11:30:16 +0100273/*
274 * Read exactly 'n' bytes or return an error.
275 */
276ssize_t readn(int fd, void *buf, size_t n)
277{
278 return ion(true, fd, buf, n);
279}
280
281/*
282 * Write exactly 'n' bytes or return an error.
283 */
284ssize_t writen(int fd, void *buf, size_t n)
285{
286 return ion(false, fd, buf, n);
287}
288
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -0300289size_t hex_width(u64 v)
290{
291 size_t n = 1;
292
293 while ((v >>= 4))
294 ++n;
295
296 return n;
297}
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -0300298
Jiri Olsab2aff5f2012-10-27 23:18:30 +0200299static int hex(char ch)
300{
301 if ((ch >= '0') && (ch <= '9'))
302 return ch - '0';
303 if ((ch >= 'a') && (ch <= 'f'))
304 return ch - 'a' + 10;
305 if ((ch >= 'A') && (ch <= 'F'))
306 return ch - 'A' + 10;
307 return -1;
308}
309
310/*
311 * While we find nice hex chars, build a long_val.
312 * Return number of chars processed.
313 */
314int hex2u64(const char *ptr, u64 *long_val)
315{
316 const char *p = ptr;
317 *long_val = 0;
318
319 while (*p) {
320 const int hex_val = hex(*p);
321
322 if (hex_val < 0)
323 break;
324
325 *long_val = (*long_val << 4) | hex_val;
326 p++;
327 }
328
329 return p - ptr;
330}
331
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -0300332/* Obtain a backtrace and print it to stdout. */
Ingo Molnar89fe8082013-09-30 12:07:11 +0200333#ifdef HAVE_BACKTRACE_SUPPORT
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -0300334void dump_stack(void)
335{
336 void *array[16];
337 size_t size = backtrace(array, ARRAY_SIZE(array));
338 char **strings = backtrace_symbols(array, size);
339 size_t i;
340
341 printf("Obtained %zd stack frames.\n", size);
342
343 for (i = 0; i < size; i++)
344 printf("%s\n", strings[i]);
345
346 free(strings);
347}
Irina Tirdeac9f08be2012-09-08 03:43:23 +0300348#else
349void dump_stack(void) {}
350#endif
David Ahern2c803e52013-01-14 10:48:01 -0700351
Arnaldo Carvalho de Melo07c1a0d2015-02-24 15:34:23 -0300352void sighandler_dump_stack(int sig)
353{
354 psignal(sig, "perf");
355 dump_stack();
Arnaldo Carvalho de Melo9daddf62015-12-11 16:43:57 -0300356 signal(sig, SIG_DFL);
357 raise(sig);
Arnaldo Carvalho de Melo07c1a0d2015-02-24 15:34:23 -0300358}
359
Namhyung Kim3b47abe2013-06-04 10:50:29 +0900360int parse_nsec_time(const char *str, u64 *ptime)
361{
362 u64 time_sec, time_nsec;
363 char *end;
364
365 time_sec = strtoul(str, &end, 10);
366 if (*end != '.' && *end != '\0')
367 return -1;
368
369 if (*end == '.') {
370 int i;
371 char nsec_buf[10];
372
373 if (strlen(++end) > 9)
374 return -1;
375
376 strncpy(nsec_buf, end, 9);
377 nsec_buf[9] = '\0';
378
379 /* make it nsec precision */
380 for (i = strlen(nsec_buf); i < 9; i++)
381 nsec_buf[i] = '0';
382
383 time_nsec = strtoul(nsec_buf, &end, 10);
384 if (*end != '\0')
385 return -1;
386 } else
387 time_nsec = 0;
388
389 *ptime = time_sec * NSEC_PER_SEC + time_nsec;
390 return 0;
391}
Jiri Olsa27050f52013-09-01 12:36:13 +0200392
393unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
394{
395 struct parse_tag *i = tags;
396
397 while (i->tag) {
398 char *s;
399
400 s = strchr(str, i->tag);
401 if (s) {
402 unsigned long int value;
403 char *endptr;
404
405 value = strtoul(str, &endptr, 10);
406 if (s != endptr)
407 break;
408
Adrian Hunter56921be2013-10-22 10:34:17 +0300409 if (value > ULONG_MAX / i->mult)
410 break;
Jiri Olsa27050f52013-09-01 12:36:13 +0200411 value *= i->mult;
412 return value;
413 }
414 i++;
415 }
416
417 return (unsigned long) -1;
418}
Arnaldo Carvalho de Melo97a07f12013-10-17 16:33:43 -0300419
Kan Liang076a30c2015-08-06 15:44:52 -0400420int get_stack_size(const char *str, unsigned long *_size)
421{
422 char *endptr;
423 unsigned long size;
424 unsigned long max_size = round_down(USHRT_MAX, sizeof(u64));
425
426 size = strtoul(str, &endptr, 0);
427
428 do {
429 if (*endptr)
430 break;
431
432 size = round_up(size, sizeof(u64));
433 if (!size || size > max_size)
434 break;
435
436 *_size = size;
437 return 0;
438
439 } while (0);
440
441 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
442 max_size, str);
443 return -1;
444}
445
446int parse_callchain_record(const char *arg, struct callchain_param *param)
447{
448 char *tok, *name, *saveptr = NULL;
449 char *buf;
450 int ret = -1;
451
452 /* We need buffer that we know we can write to. */
453 buf = malloc(strlen(arg) + 1);
454 if (!buf)
455 return -ENOMEM;
456
457 strcpy(buf, arg);
458
459 tok = strtok_r((char *)buf, ",", &saveptr);
460 name = tok ? : (char *)buf;
461
462 do {
463 /* Framepointer style */
464 if (!strncmp(name, "fp", sizeof("fp"))) {
465 if (!strtok_r(NULL, ",", &saveptr)) {
466 param->record_mode = CALLCHAIN_FP;
467 ret = 0;
468 } else
469 pr_err("callchain: No more arguments "
470 "needed for --call-graph fp\n");
471 break;
472
473#ifdef HAVE_DWARF_UNWIND_SUPPORT
474 /* Dwarf style */
475 } else if (!strncmp(name, "dwarf", sizeof("dwarf"))) {
476 const unsigned long default_stack_dump_size = 8192;
477
478 ret = 0;
479 param->record_mode = CALLCHAIN_DWARF;
480 param->dump_size = default_stack_dump_size;
481
482 tok = strtok_r(NULL, ",", &saveptr);
483 if (tok) {
484 unsigned long size = 0;
485
486 ret = get_stack_size(tok, &size);
487 param->dump_size = size;
488 }
489#endif /* HAVE_DWARF_UNWIND_SUPPORT */
490 } else if (!strncmp(name, "lbr", sizeof("lbr"))) {
491 if (!strtok_r(NULL, ",", &saveptr)) {
492 param->record_mode = CALLCHAIN_LBR;
493 ret = 0;
494 } else
495 pr_err("callchain: No more arguments "
496 "needed for --call-graph lbr\n");
497 break;
498 } else {
499 pr_err("callchain: Unknown --call-graph option "
500 "value: %s\n", arg);
501 break;
502 }
503
504 } while (0);
505
506 free(buf);
507 return ret;
508}
509
Jiri Olsacef82c92013-12-03 14:09:22 +0100510int filename__read_str(const char *filename, char **buf, size_t *sizep)
511{
512 size_t size = 0, alloc_size = 0;
513 void *bf = NULL, *nbf;
514 int fd, n, err = 0;
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000515 char sbuf[STRERR_BUFSIZE];
Jiri Olsacef82c92013-12-03 14:09:22 +0100516
517 fd = open(filename, O_RDONLY);
518 if (fd < 0)
519 return -errno;
520
521 do {
522 if (size == alloc_size) {
523 alloc_size += BUFSIZ;
524 nbf = realloc(bf, alloc_size);
525 if (!nbf) {
526 err = -ENOMEM;
527 break;
528 }
529
530 bf = nbf;
531 }
532
533 n = read(fd, bf + size, alloc_size - size);
534 if (n < 0) {
535 if (size) {
Masami Hiramatsu6e81c742014-08-14 02:22:36 +0000536 pr_warning("read failed %d: %s\n", errno,
537 strerror_r(errno, sbuf, sizeof(sbuf)));
Jiri Olsacef82c92013-12-03 14:09:22 +0100538 err = 0;
539 } else
540 err = -errno;
541
542 break;
543 }
544
545 size += n;
546 } while (n > 0);
547
548 if (!err) {
549 *sizep = size;
550 *buf = bf;
551 } else
552 free(bf);
553
554 close(fd);
555 return err;
556}
Dongsheng Yange1a2b172013-12-06 17:25:51 -0500557
558const char *get_filename_for_perf_kvm(void)
559{
560 const char *filename;
561
562 if (perf_host && !perf_guest)
563 filename = strdup("perf.data.host");
564 else if (!perf_host && perf_guest)
565 filename = strdup("perf.data.guest");
566 else
567 filename = strdup("perf.data.kvm");
568
569 return filename;
570}
Adrian Hunter1a472452013-12-11 14:36:23 +0200571
572int perf_event_paranoid(void)
573{
Adrian Hunter1a472452013-12-11 14:36:23 +0200574 int value;
575
Arnaldo Carvalho de Meloce273092014-12-11 13:37:59 -0300576 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
Adrian Hunter1a472452013-12-11 14:36:23 +0200577 return INT_MAX;
578
579 return value;
580}
Adrian Hunter71db07b2013-12-11 14:36:32 +0200581
582void mem_bswap_32(void *src, int byte_size)
583{
584 u32 *m = src;
585 while (byte_size > 0) {
586 *m = bswap_32(*m);
587 byte_size -= sizeof(u32);
588 ++m;
589 }
590}
591
592void mem_bswap_64(void *src, int byte_size)
593{
594 u64 *m = src;
595
596 while (byte_size > 0) {
597 *m = bswap_64(*m);
598 byte_size -= sizeof(u64);
599 ++m;
600 }
601}
Jiri Olsa63914ac2014-08-01 17:46:54 +0200602
603bool find_process(const char *name)
604{
605 size_t len = strlen(name);
606 DIR *dir;
607 struct dirent *d;
608 int ret = -1;
609
610 dir = opendir(procfs__mountpoint());
611 if (!dir)
Peter Senna Tschudinbf644562015-09-17 12:08:53 +0200612 return false;
Jiri Olsa63914ac2014-08-01 17:46:54 +0200613
614 /* Walk through the directory. */
615 while (ret && (d = readdir(dir)) != NULL) {
616 char path[PATH_MAX];
617 char *data;
618 size_t size;
619
620 if ((d->d_type != DT_DIR) ||
621 !strcmp(".", d->d_name) ||
622 !strcmp("..", d->d_name))
623 continue;
624
625 scnprintf(path, sizeof(path), "%s/%s/comm",
626 procfs__mountpoint(), d->d_name);
627
628 if (filename__read_str(path, &data, &size))
629 continue;
630
631 ret = strncmp(name, data, len);
632 free(data);
633 }
634
635 closedir(dir);
636 return ret ? false : true;
637}
Wang Nan07bc5c62015-11-06 13:55:35 +0000638
639int
640fetch_kernel_version(unsigned int *puint, char *str,
641 size_t str_size)
642{
643 struct utsname utsname;
644 int version, patchlevel, sublevel, err;
645
646 if (uname(&utsname))
647 return -1;
648
649 if (str && str_size) {
650 strncpy(str, utsname.release, str_size);
651 str[str_size - 1] = '\0';
652 }
653
654 err = sscanf(utsname.release, "%d.%d.%d",
655 &version, &patchlevel, &sublevel);
656
657 if (err != 3) {
658 pr_debug("Unablt to get kernel version from uname '%s'\n",
659 utsname.release);
660 return -1;
661 }
662
663 if (puint)
664 *puint = (version << 16) + (patchlevel << 8) + sublevel;
665 return 0;
666}
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900667
668const char *perf_tip(const char *dirpath)
669{
670 struct strlist *tips;
671 struct str_node *node;
672 char *tip = NULL;
673 struct strlist_config conf = {
Namhyung Kim34b7b0f2016-01-09 19:16:29 +0900674 .dirname = dirpath,
675 .file_only = true,
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900676 };
677
678 tips = strlist__new("tips.txt", &conf);
Namhyung Kim34b7b0f2016-01-09 19:16:29 +0900679 if (tips == NULL)
680 return errno == ENOENT ? NULL : "Tip: get more memory! ;-p";
681
682 if (strlist__nr_entries(tips) == 0)
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900683 goto out;
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900684
685 node = strlist__entry(tips, random() % strlist__nr_entries(tips));
686 if (asprintf(&tip, "Tip: %s", node->s) < 0)
687 tip = (char *)"Tip: get more memory! ;-)";
688
689out:
690 strlist__delete(tips);
691
692 return tip;
693}