blob: 524bd3b9d98d426f0aaedce737091630a7b649d9 [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 Melofd20e812017-04-17 15:23:08 -030010#include <inttypes.h>
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -030011#include <stdio.h>
12#include <stdlib.h>
Jiri Olsacef82c92013-12-03 14:09:22 +010013#include <string.h>
14#include <errno.h>
Adrian Hunter1a472452013-12-11 14:36:23 +020015#include <limits.h>
Adrian Hunter71db07b2013-12-11 14:36:32 +020016#include <byteswap.h>
Jiri Olsa838d1452013-11-28 11:30:15 +010017#include <linux/kernel.h>
Wang Nanc339b1a2016-02-24 11:20:44 +000018#include <linux/log2.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030019#include <linux/time64.h>
Jiri Olsa9398c482014-08-11 10:50:02 +020020#include <unistd.h>
Jiri Olsa23aadb12014-10-01 18:00:26 +020021#include "callchain.h"
Namhyung Kim14cbfbe2016-01-07 20:41:53 +090022#include "strlist.h"
Jiri Olsa23aadb12014-10-01 18:00:26 +020023
Jiri Olsa347ca872016-07-04 14:16:21 +020024#define CALLCHAIN_PARAM_DEFAULT \
25 .mode = CHAIN_GRAPH_ABS, \
26 .min_percent = 0.5, \
27 .order = ORDER_CALLEE, \
28 .key = CCKEY_FUNCTION, \
29 .value = CCVAL_PERCENT, \
30
31struct callchain_param callchain_param = {
32 CALLCHAIN_PARAM_DEFAULT
33};
34
35struct callchain_param callchain_param_default = {
36 CALLCHAIN_PARAM_DEFAULT
Jiri Olsa23aadb12014-10-01 18:00:26 +020037};
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020038
Joerg Roedel1aed2672012-01-04 17:54:20 +010039/*
40 * XXX We need to find a better place for these things...
41 */
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -030042unsigned int page_size;
Don Zickus2b1b7102014-05-30 16:10:05 -040043int cacheline_size;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -030044
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -030045int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
46int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -030047
Arnaldo Carvalho de Melo0c6332e2012-12-13 16:43:04 -030048bool test_attr__enabled;
49
Joerg Roedel1aed2672012-01-04 17:54:20 +010050bool perf_host = true;
Joerg Roedelc4a7dca2012-02-10 18:05:05 +010051bool perf_guest = false;
Joerg Roedel1aed2672012-01-04 17:54:20 +010052
53void event_attr_init(struct perf_event_attr *attr)
54{
55 if (!perf_host)
56 attr->exclude_host = 1;
57 if (!perf_guest)
58 attr->exclude_guest = 1;
Stephane Eranian7e1ccd32012-02-09 16:12:38 +010059 /* to capture ABI version */
60 attr->size = sizeof(*attr);
Joerg Roedel1aed2672012-01-04 17:54:20 +010061}
62
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020063int mkdir_p(char *path, mode_t mode)
64{
65 struct stat st;
66 int err;
67 char *d = path;
68
69 if (*d != '/')
70 return -1;
71
72 if (stat(path, &st) == 0)
73 return 0;
74
75 while (*++d == '/');
76
77 while ((d = strchr(d, '/'))) {
78 *d = '\0';
79 err = stat(path, &st) && mkdir(path, mode);
80 *d++ = '/';
81 if (err)
82 return -1;
83 while (*d == '/')
84 ++d;
85 }
86 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
87}
88
Joe Stringer9a9c7332017-01-26 13:19:59 -080089int rm_rf(const char *path)
Namhyung Kim0b1de0be2015-05-18 09:30:17 +090090{
91 DIR *dir;
92 int ret = 0;
93 struct dirent *d;
94 char namebuf[PATH_MAX];
95
96 dir = opendir(path);
97 if (dir == NULL)
98 return 0;
99
100 while ((d = readdir(dir)) != NULL && !ret) {
101 struct stat statbuf;
102
103 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
104 continue;
105
106 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
107 path, d->d_name);
108
Masami Hiramatsu2a1ef032016-06-08 18:29:11 +0900109 /* We have to check symbolic link itself */
110 ret = lstat(namebuf, &statbuf);
Namhyung Kim0b1de0be2015-05-18 09:30:17 +0900111 if (ret < 0) {
112 pr_debug("stat failed: %s\n", namebuf);
113 break;
114 }
115
Masami Hiramatsu2a1ef032016-06-08 18:29:11 +0900116 if (S_ISDIR(statbuf.st_mode))
Namhyung Kim0b1de0be2015-05-18 09:30:17 +0900117 ret = rm_rf(namebuf);
Masami Hiramatsu2a1ef032016-06-08 18:29:11 +0900118 else
119 ret = unlink(namebuf);
Namhyung Kim0b1de0be2015-05-18 09:30:17 +0900120 }
121 closedir(dir);
122
123 if (ret < 0)
124 return ret;
125
126 return rmdir(path);
127}
128
Masami Hiramatsue1ce7262016-04-26 18:02:42 +0900129/* A filter which removes dot files */
130bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
131{
132 return d->d_name[0] != '.';
133}
134
135/* lsdir reads a directory and store it in strlist */
136struct strlist *lsdir(const char *name,
137 bool (*filter)(const char *, struct dirent *))
138{
139 struct strlist *list = NULL;
140 DIR *dir;
141 struct dirent *d;
142
143 dir = opendir(name);
144 if (!dir)
145 return NULL;
146
147 list = strlist__new(NULL, NULL);
148 if (!list) {
Masami Hiramatsu357a54f2016-05-11 22:51:27 +0900149 errno = ENOMEM;
Masami Hiramatsue1ce7262016-04-26 18:02:42 +0900150 goto out;
151 }
152
153 while ((d = readdir(dir)) != NULL) {
154 if (!filter || filter(name, d))
155 strlist__add(list, d->d_name);
156 }
157
158out:
159 closedir(dir);
160 return list;
161}
162
Milos Vyleteld7c72602015-06-08 16:50:16 +0200163static int slow_copyfile(const char *from, const char *to)
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200164{
Adrian Hunter9a17d722013-10-14 13:43:41 +0300165 int err = -1;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200166 char *line = NULL;
167 size_t n;
168 FILE *from_fp = fopen(from, "r"), *to_fp;
169
170 if (from_fp == NULL)
171 goto out;
172
173 to_fp = fopen(to, "w");
174 if (to_fp == NULL)
175 goto out_fclose_from;
176
177 while (getline(&line, &n, from_fp) > 0)
178 if (fputs(line, to_fp) == EOF)
179 goto out_fclose_to;
180 err = 0;
181out_fclose_to:
182 fclose(to_fp);
183 free(line);
184out_fclose_from:
185 fclose(from_fp);
186out:
187 return err;
188}
189
Namhyung Kim9c9f5a22015-05-18 09:30:18 +0900190int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
191{
192 void *ptr;
193 loff_t pgoff;
194
195 pgoff = off_in & ~(page_size - 1);
196 off_in -= pgoff;
197
198 ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
199 if (ptr == MAP_FAILED)
200 return -1;
201
202 while (size) {
203 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
204 if (ret < 0 && errno == EINTR)
205 continue;
206 if (ret <= 0)
207 break;
208
209 size -= ret;
210 off_in += ret;
211 off_out -= ret;
212 }
213 munmap(ptr, off_in + size);
214
215 return size ? -1 : 0;
216}
217
Adrian Hunter9a17d722013-10-14 13:43:41 +0300218int copyfile_mode(const char *from, const char *to, mode_t mode)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200219{
220 int fromfd, tofd;
221 struct stat st;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200222 int err = -1;
Milos Vyleteld7c72602015-06-08 16:50:16 +0200223 char *tmp = NULL, *ptr = NULL;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200224
225 if (stat(from, &st))
226 goto out;
227
Milos Vyleteld7c72602015-06-08 16:50:16 +0200228 /* extra 'x' at the end is to reserve space for '.' */
229 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
230 tmp = NULL;
231 goto out;
232 }
233 ptr = strrchr(tmp, '/');
234 if (!ptr)
235 goto out;
236 ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
237 *ptr = '.';
238
239 tofd = mkstemp(tmp);
240 if (tofd < 0)
241 goto out;
242
243 if (fchmod(tofd, mode))
244 goto out_close_to;
245
246 if (st.st_size == 0) { /* /proc? do it slowly... */
247 err = slow_copyfile(from, tmp);
248 goto out_close_to;
249 }
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200250
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200251 fromfd = open(from, O_RDONLY);
252 if (fromfd < 0)
Milos Vyleteld7c72602015-06-08 16:50:16 +0200253 goto out_close_to;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200254
Namhyung Kim9c9f5a22015-05-18 09:30:18 +0900255 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200256
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200257 close(fromfd);
Milos Vyleteld7c72602015-06-08 16:50:16 +0200258out_close_to:
259 close(tofd);
260 if (!err)
261 err = link(tmp, to);
262 unlink(tmp);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200263out:
Milos Vyleteld7c72602015-06-08 16:50:16 +0200264 free(tmp);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200265 return err;
266}
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300267
Adrian Hunter9a17d722013-10-14 13:43:41 +0300268int copyfile(const char *from, const char *to)
269{
270 return copyfile_mode(from, to, 0755);
271}
272
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300273unsigned long convert_unit(unsigned long value, char *unit)
274{
275 *unit = ' ';
276
277 if (value > 1000) {
278 value /= 1000;
279 *unit = 'K';
280 }
281
282 if (value > 1000) {
283 value /= 1000;
284 *unit = 'M';
285 }
286
287 if (value > 1000) {
288 value /= 1000;
289 *unit = 'G';
290 }
291
292 return value;
293}
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200294
Jiri Olsabc3a5022013-11-28 11:30:16 +0100295static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200296{
297 void *buf_start = buf;
Jiri Olsa838d1452013-11-28 11:30:15 +0100298 size_t left = n;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200299
Jiri Olsa838d1452013-11-28 11:30:15 +0100300 while (left) {
Jiri Olsabc3a5022013-11-28 11:30:16 +0100301 ssize_t ret = is_read ? read(fd, buf, left) :
302 write(fd, buf, left);
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200303
Namhyung Kime148c762014-04-24 22:27:32 +0900304 if (ret < 0 && errno == EINTR)
305 continue;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200306 if (ret <= 0)
307 return ret;
308
Jiri Olsa838d1452013-11-28 11:30:15 +0100309 left -= ret;
310 buf += ret;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200311 }
312
Jiri Olsa838d1452013-11-28 11:30:15 +0100313 BUG_ON((size_t)(buf - buf_start) != n);
314 return n;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200315}
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -0300316
Jiri Olsabc3a5022013-11-28 11:30:16 +0100317/*
318 * Read exactly 'n' bytes or return an error.
319 */
320ssize_t readn(int fd, void *buf, size_t n)
321{
322 return ion(true, fd, buf, n);
323}
324
325/*
326 * Write exactly 'n' bytes or return an error.
327 */
328ssize_t writen(int fd, void *buf, size_t n)
329{
330 return ion(false, fd, buf, n);
331}
332
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -0300333size_t hex_width(u64 v)
334{
335 size_t n = 1;
336
337 while ((v >>= 4))
338 ++n;
339
340 return n;
341}
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -0300342
Jiri Olsab2aff5f2012-10-27 23:18:30 +0200343static int hex(char ch)
344{
345 if ((ch >= '0') && (ch <= '9'))
346 return ch - '0';
347 if ((ch >= 'a') && (ch <= 'f'))
348 return ch - 'a' + 10;
349 if ((ch >= 'A') && (ch <= 'F'))
350 return ch - 'A' + 10;
351 return -1;
352}
353
354/*
355 * While we find nice hex chars, build a long_val.
356 * Return number of chars processed.
357 */
358int hex2u64(const char *ptr, u64 *long_val)
359{
360 const char *p = ptr;
361 *long_val = 0;
362
363 while (*p) {
364 const int hex_val = hex(*p);
365
366 if (hex_val < 0)
367 break;
368
369 *long_val = (*long_val << 4) | hex_val;
370 p++;
371 }
372
373 return p - ptr;
374}
375
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -0300376/* Obtain a backtrace and print it to stdout. */
Ingo Molnar89fe8082013-09-30 12:07:11 +0200377#ifdef HAVE_BACKTRACE_SUPPORT
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -0300378void dump_stack(void)
379{
380 void *array[16];
381 size_t size = backtrace(array, ARRAY_SIZE(array));
382 char **strings = backtrace_symbols(array, size);
383 size_t i;
384
385 printf("Obtained %zd stack frames.\n", size);
386
387 for (i = 0; i < size; i++)
388 printf("%s\n", strings[i]);
389
390 free(strings);
391}
Irina Tirdeac9f08be2012-09-08 03:43:23 +0300392#else
393void dump_stack(void) {}
394#endif
David Ahern2c803e52013-01-14 10:48:01 -0700395
Arnaldo Carvalho de Melo07c1a0d2015-02-24 15:34:23 -0300396void sighandler_dump_stack(int sig)
397{
398 psignal(sig, "perf");
399 dump_stack();
Arnaldo Carvalho de Melo9daddf62015-12-11 16:43:57 -0300400 signal(sig, SIG_DFL);
401 raise(sig);
Arnaldo Carvalho de Melo07c1a0d2015-02-24 15:34:23 -0300402}
403
Namhyung Kim99620a52016-10-24 11:02:45 +0900404int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz)
405{
406 u64 sec = timestamp / NSEC_PER_SEC;
407 u64 usec = (timestamp % NSEC_PER_SEC) / NSEC_PER_USEC;
408
409 return scnprintf(buf, sz, "%"PRIu64".%06"PRIu64, sec, usec);
410}
411
Jiri Olsa27050f52013-09-01 12:36:13 +0200412unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
413{
414 struct parse_tag *i = tags;
415
416 while (i->tag) {
417 char *s;
418
419 s = strchr(str, i->tag);
420 if (s) {
421 unsigned long int value;
422 char *endptr;
423
424 value = strtoul(str, &endptr, 10);
425 if (s != endptr)
426 break;
427
Adrian Hunter56921be2013-10-22 10:34:17 +0300428 if (value > ULONG_MAX / i->mult)
429 break;
Jiri Olsa27050f52013-09-01 12:36:13 +0200430 value *= i->mult;
431 return value;
432 }
433 i++;
434 }
435
436 return (unsigned long) -1;
437}
Arnaldo Carvalho de Melo97a07f12013-10-17 16:33:43 -0300438
Kan Liang076a30c2015-08-06 15:44:52 -0400439int get_stack_size(const char *str, unsigned long *_size)
440{
441 char *endptr;
442 unsigned long size;
443 unsigned long max_size = round_down(USHRT_MAX, sizeof(u64));
444
445 size = strtoul(str, &endptr, 0);
446
447 do {
448 if (*endptr)
449 break;
450
451 size = round_up(size, sizeof(u64));
452 if (!size || size > max_size)
453 break;
454
455 *_size = size;
456 return 0;
457
458 } while (0);
459
460 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
461 max_size, str);
462 return -1;
463}
464
465int parse_callchain_record(const char *arg, struct callchain_param *param)
466{
467 char *tok, *name, *saveptr = NULL;
468 char *buf;
469 int ret = -1;
470
471 /* We need buffer that we know we can write to. */
472 buf = malloc(strlen(arg) + 1);
473 if (!buf)
474 return -ENOMEM;
475
476 strcpy(buf, arg);
477
478 tok = strtok_r((char *)buf, ",", &saveptr);
479 name = tok ? : (char *)buf;
480
481 do {
482 /* Framepointer style */
483 if (!strncmp(name, "fp", sizeof("fp"))) {
484 if (!strtok_r(NULL, ",", &saveptr)) {
485 param->record_mode = CALLCHAIN_FP;
486 ret = 0;
487 } else
488 pr_err("callchain: No more arguments "
489 "needed for --call-graph fp\n");
490 break;
491
Kan Liang076a30c2015-08-06 15:44:52 -0400492 /* Dwarf style */
493 } else if (!strncmp(name, "dwarf", sizeof("dwarf"))) {
494 const unsigned long default_stack_dump_size = 8192;
495
496 ret = 0;
497 param->record_mode = CALLCHAIN_DWARF;
498 param->dump_size = default_stack_dump_size;
499
500 tok = strtok_r(NULL, ",", &saveptr);
501 if (tok) {
502 unsigned long size = 0;
503
504 ret = get_stack_size(tok, &size);
505 param->dump_size = size;
506 }
Kan Liang076a30c2015-08-06 15:44:52 -0400507 } else if (!strncmp(name, "lbr", sizeof("lbr"))) {
508 if (!strtok_r(NULL, ",", &saveptr)) {
509 param->record_mode = CALLCHAIN_LBR;
510 ret = 0;
511 } else
512 pr_err("callchain: No more arguments "
513 "needed for --call-graph lbr\n");
514 break;
515 } else {
516 pr_err("callchain: Unknown --call-graph option "
517 "value: %s\n", arg);
518 break;
519 }
520
521 } while (0);
522
523 free(buf);
524 return ret;
525}
526
Dongsheng Yange1a2b172013-12-06 17:25:51 -0500527const char *get_filename_for_perf_kvm(void)
528{
529 const char *filename;
530
531 if (perf_host && !perf_guest)
532 filename = strdup("perf.data.host");
533 else if (!perf_host && perf_guest)
534 filename = strdup("perf.data.guest");
535 else
536 filename = strdup("perf.data.kvm");
537
538 return filename;
539}
Adrian Hunter1a472452013-12-11 14:36:23 +0200540
541int perf_event_paranoid(void)
542{
Adrian Hunter1a472452013-12-11 14:36:23 +0200543 int value;
544
Arnaldo Carvalho de Meloce273092014-12-11 13:37:59 -0300545 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
Adrian Hunter1a472452013-12-11 14:36:23 +0200546 return INT_MAX;
547
548 return value;
549}
Adrian Hunter71db07b2013-12-11 14:36:32 +0200550
551void mem_bswap_32(void *src, int byte_size)
552{
553 u32 *m = src;
554 while (byte_size > 0) {
555 *m = bswap_32(*m);
556 byte_size -= sizeof(u32);
557 ++m;
558 }
559}
560
561void mem_bswap_64(void *src, int byte_size)
562{
563 u64 *m = src;
564
565 while (byte_size > 0) {
566 *m = bswap_64(*m);
567 byte_size -= sizeof(u64);
568 ++m;
569 }
570}
Jiri Olsa63914ac2014-08-01 17:46:54 +0200571
572bool find_process(const char *name)
573{
574 size_t len = strlen(name);
575 DIR *dir;
576 struct dirent *d;
577 int ret = -1;
578
579 dir = opendir(procfs__mountpoint());
580 if (!dir)
Peter Senna Tschudinbf644562015-09-17 12:08:53 +0200581 return false;
Jiri Olsa63914ac2014-08-01 17:46:54 +0200582
583 /* Walk through the directory. */
584 while (ret && (d = readdir(dir)) != NULL) {
585 char path[PATH_MAX];
586 char *data;
587 size_t size;
588
589 if ((d->d_type != DT_DIR) ||
590 !strcmp(".", d->d_name) ||
591 !strcmp("..", d->d_name))
592 continue;
593
594 scnprintf(path, sizeof(path), "%s/%s/comm",
595 procfs__mountpoint(), d->d_name);
596
597 if (filename__read_str(path, &data, &size))
598 continue;
599
600 ret = strncmp(name, data, len);
601 free(data);
602 }
603
604 closedir(dir);
605 return ret ? false : true;
606}
Wang Nan07bc5c62015-11-06 13:55:35 +0000607
Wang Nand18acd12016-11-15 04:05:44 +0000608static int
609fetch_ubuntu_kernel_version(unsigned int *puint)
610{
611 ssize_t len;
612 size_t line_len = 0;
613 char *ptr, *line = NULL;
614 int version, patchlevel, sublevel, err;
615 FILE *vsig = fopen("/proc/version_signature", "r");
616
617 if (!vsig) {
618 pr_debug("Open /proc/version_signature failed: %s\n",
619 strerror(errno));
620 return -1;
621 }
622
623 len = getline(&line, &line_len, vsig);
624 fclose(vsig);
625 err = -1;
626 if (len <= 0) {
627 pr_debug("Reading from /proc/version_signature failed: %s\n",
628 strerror(errno));
629 goto errout;
630 }
631
632 ptr = strrchr(line, ' ');
633 if (!ptr) {
634 pr_debug("Parsing /proc/version_signature failed: %s\n", line);
635 goto errout;
636 }
637
638 err = sscanf(ptr + 1, "%d.%d.%d",
639 &version, &patchlevel, &sublevel);
640 if (err != 3) {
641 pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
642 line);
643 goto errout;
644 }
645
646 if (puint)
647 *puint = (version << 16) + (patchlevel << 8) + sublevel;
648 err = 0;
649errout:
650 free(line);
651 return err;
652}
653
Wang Nan07bc5c62015-11-06 13:55:35 +0000654int
655fetch_kernel_version(unsigned int *puint, char *str,
656 size_t str_size)
657{
658 struct utsname utsname;
659 int version, patchlevel, sublevel, err;
Wang Nand18acd12016-11-15 04:05:44 +0000660 bool int_ver_ready = false;
661
662 if (access("/proc/version_signature", R_OK) == 0)
663 if (!fetch_ubuntu_kernel_version(puint))
664 int_ver_ready = true;
Wang Nan07bc5c62015-11-06 13:55:35 +0000665
666 if (uname(&utsname))
667 return -1;
668
669 if (str && str_size) {
670 strncpy(str, utsname.release, str_size);
671 str[str_size - 1] = '\0';
672 }
673
674 err = sscanf(utsname.release, "%d.%d.%d",
675 &version, &patchlevel, &sublevel);
676
677 if (err != 3) {
Wang Nand18acd12016-11-15 04:05:44 +0000678 pr_debug("Unable to get kernel version from uname '%s'\n",
Wang Nan07bc5c62015-11-06 13:55:35 +0000679 utsname.release);
680 return -1;
681 }
682
Wang Nand18acd12016-11-15 04:05:44 +0000683 if (puint && !int_ver_ready)
Wang Nan07bc5c62015-11-06 13:55:35 +0000684 *puint = (version << 16) + (patchlevel << 8) + sublevel;
685 return 0;
686}
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900687
688const char *perf_tip(const char *dirpath)
689{
690 struct strlist *tips;
691 struct str_node *node;
692 char *tip = NULL;
693 struct strlist_config conf = {
Namhyung Kim34b7b0f2016-01-09 19:16:29 +0900694 .dirname = dirpath,
695 .file_only = true,
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900696 };
697
698 tips = strlist__new("tips.txt", &conf);
Namhyung Kim34b7b0f2016-01-09 19:16:29 +0900699 if (tips == NULL)
David Carrillo-Cisneros570eda02017-04-11 23:49:16 -0700700 return errno == ENOENT ? NULL :
701 "Tip: check path of tips.txt or get more memory! ;-p";
Namhyung Kim34b7b0f2016-01-09 19:16:29 +0900702
703 if (strlist__nr_entries(tips) == 0)
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900704 goto out;
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900705
706 node = strlist__entry(tips, random() % strlist__nr_entries(tips));
707 if (asprintf(&tip, "Tip: %s", node->s) < 0)
708 tip = (char *)"Tip: get more memory! ;-)";
709
710out:
711 strlist__delete(tips);
712
713 return tip;
714}
Jiri Olsa40356722016-01-20 12:56:32 +0100715
716bool is_regular_file(const char *file)
717{
718 struct stat st;
719
720 if (stat(file, &st))
721 return false;
722
723 return S_ISREG(st.st_mode);
724}
Wang Nan37b20152016-01-25 09:56:13 +0000725
726int fetch_current_timestamp(char *buf, size_t sz)
727{
728 struct timeval tv;
729 struct tm tm;
730 char dt[32];
731
732 if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm))
733 return -1;
734
735 if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm))
736 return -1;
737
738 scnprintf(buf, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000);
739
740 return 0;
741}
Wang Nanc339b1a2016-02-24 11:20:44 +0000742
743void print_binary(unsigned char *data, size_t len,
744 size_t bytes_per_line, print_binary_t printer,
745 void *extra)
746{
747 size_t i, j, mask;
748
749 if (!printer)
750 return;
751
752 bytes_per_line = roundup_pow_of_two(bytes_per_line);
753 mask = bytes_per_line - 1;
754
755 printer(BINARY_PRINT_DATA_BEGIN, 0, extra);
756 for (i = 0; i < len; i++) {
757 if ((i & mask) == 0) {
758 printer(BINARY_PRINT_LINE_BEGIN, -1, extra);
759 printer(BINARY_PRINT_ADDR, i, extra);
760 }
761
762 printer(BINARY_PRINT_NUM_DATA, data[i], extra);
763
764 if (((i & mask) == mask) || i == len - 1) {
765 for (j = 0; j < mask-(i & mask); j++)
766 printer(BINARY_PRINT_NUM_PAD, -1, extra);
767
768 printer(BINARY_PRINT_SEP, i, extra);
769 for (j = i & ~mask; j <= i; j++)
770 printer(BINARY_PRINT_CHAR_DATA, data[j], extra);
771 for (j = 0; j < mask-(i & mask); j++)
772 printer(BINARY_PRINT_CHAR_PAD, i, extra);
773 printer(BINARY_PRINT_LINE_END, -1, extra);
774 }
775 }
776 printer(BINARY_PRINT_DATA_END, -1, extra);
777}
Jiri Olsaaccaed22016-07-16 18:11:19 +0200778
779int is_printable_array(char *p, unsigned int len)
780{
781 unsigned int i;
782
783 if (!p || !len || p[len - 1] != 0)
784 return 0;
785
786 len--;
787
788 for (i = 0; i < len; i++) {
789 if (!isprint(p[i]) && !isspace(p[i]))
790 return 0;
791 }
792 return 1;
793}
Jiri Olsa98081432017-01-09 10:51:55 +0100794
795int unit_number__scnprintf(char *buf, size_t size, u64 n)
796{
797 char unit[4] = "BKMG";
798 int i = 0;
799
800 while (((n / 1024) > 1) && (i < 3)) {
801 n /= 1024;
802 i++;
803 }
804
805 return scnprintf(buf, size, "%" PRIu64 "%c", n, unit[i]);
806}