blob: 5f44a21955cde0a53bb94859ddf32df55da79e3d [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>
Wang Nanc339b1a2016-02-24 11:20:44 +000017#include <linux/log2.h>
Jiri Olsa9398c482014-08-11 10:50:02 +020018#include <unistd.h>
Jiri Olsa23aadb12014-10-01 18:00:26 +020019#include "callchain.h"
Namhyung Kim14cbfbe2016-01-07 20:41:53 +090020#include "strlist.h"
Jiri Olsa23aadb12014-10-01 18:00:26 +020021
Jiri Olsa347ca872016-07-04 14:16:21 +020022#define CALLCHAIN_PARAM_DEFAULT \
23 .mode = CHAIN_GRAPH_ABS, \
24 .min_percent = 0.5, \
25 .order = ORDER_CALLEE, \
26 .key = CCKEY_FUNCTION, \
27 .value = CCVAL_PERCENT, \
28
29struct callchain_param callchain_param = {
30 CALLCHAIN_PARAM_DEFAULT
31};
32
33struct callchain_param callchain_param_default = {
34 CALLCHAIN_PARAM_DEFAULT
Jiri Olsa23aadb12014-10-01 18:00:26 +020035};
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020036
Joerg Roedel1aed2672012-01-04 17:54:20 +010037/*
38 * XXX We need to find a better place for these things...
39 */
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -030040unsigned int page_size;
Don Zickus2b1b7102014-05-30 16:10:05 -040041int cacheline_size;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -030042
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -030043int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
44int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -030045
Arnaldo Carvalho de Melo0c6332e2012-12-13 16:43:04 -030046bool test_attr__enabled;
47
Joerg Roedel1aed2672012-01-04 17:54:20 +010048bool perf_host = true;
Joerg Roedelc4a7dca2012-02-10 18:05:05 +010049bool perf_guest = false;
Joerg Roedel1aed2672012-01-04 17:54:20 +010050
51void event_attr_init(struct perf_event_attr *attr)
52{
53 if (!perf_host)
54 attr->exclude_host = 1;
55 if (!perf_guest)
56 attr->exclude_guest = 1;
Stephane Eranian7e1ccd32012-02-09 16:12:38 +010057 /* to capture ABI version */
58 attr->size = sizeof(*attr);
Joerg Roedel1aed2672012-01-04 17:54:20 +010059}
60
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020061int mkdir_p(char *path, mode_t mode)
62{
63 struct stat st;
64 int err;
65 char *d = path;
66
67 if (*d != '/')
68 return -1;
69
70 if (stat(path, &st) == 0)
71 return 0;
72
73 while (*++d == '/');
74
75 while ((d = strchr(d, '/'))) {
76 *d = '\0';
77 err = stat(path, &st) && mkdir(path, mode);
78 *d++ = '/';
79 if (err)
80 return -1;
81 while (*d == '/')
82 ++d;
83 }
84 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
85}
86
Namhyung Kim0b1de0be2015-05-18 09:30:17 +090087int rm_rf(char *path)
88{
89 DIR *dir;
90 int ret = 0;
91 struct dirent *d;
92 char namebuf[PATH_MAX];
93
94 dir = opendir(path);
95 if (dir == NULL)
96 return 0;
97
98 while ((d = readdir(dir)) != NULL && !ret) {
99 struct stat statbuf;
100
101 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
102 continue;
103
104 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
105 path, d->d_name);
106
Masami Hiramatsu2a1ef032016-06-08 18:29:11 +0900107 /* We have to check symbolic link itself */
108 ret = lstat(namebuf, &statbuf);
Namhyung Kim0b1de0be2015-05-18 09:30:17 +0900109 if (ret < 0) {
110 pr_debug("stat failed: %s\n", namebuf);
111 break;
112 }
113
Masami Hiramatsu2a1ef032016-06-08 18:29:11 +0900114 if (S_ISDIR(statbuf.st_mode))
Namhyung Kim0b1de0be2015-05-18 09:30:17 +0900115 ret = rm_rf(namebuf);
Masami Hiramatsu2a1ef032016-06-08 18:29:11 +0900116 else
117 ret = unlink(namebuf);
Namhyung Kim0b1de0be2015-05-18 09:30:17 +0900118 }
119 closedir(dir);
120
121 if (ret < 0)
122 return ret;
123
124 return rmdir(path);
125}
126
Masami Hiramatsue1ce7262016-04-26 18:02:42 +0900127/* A filter which removes dot files */
128bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
129{
130 return d->d_name[0] != '.';
131}
132
133/* lsdir reads a directory and store it in strlist */
134struct strlist *lsdir(const char *name,
135 bool (*filter)(const char *, struct dirent *))
136{
137 struct strlist *list = NULL;
138 DIR *dir;
139 struct dirent *d;
140
141 dir = opendir(name);
142 if (!dir)
143 return NULL;
144
145 list = strlist__new(NULL, NULL);
146 if (!list) {
Masami Hiramatsu357a54f2016-05-11 22:51:27 +0900147 errno = ENOMEM;
Masami Hiramatsue1ce7262016-04-26 18:02:42 +0900148 goto out;
149 }
150
151 while ((d = readdir(dir)) != NULL) {
152 if (!filter || filter(name, d))
153 strlist__add(list, d->d_name);
154 }
155
156out:
157 closedir(dir);
158 return list;
159}
160
Milos Vyleteld7c72602015-06-08 16:50:16 +0200161static int slow_copyfile(const char *from, const char *to)
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200162{
Adrian Hunter9a17d722013-10-14 13:43:41 +0300163 int err = -1;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200164 char *line = NULL;
165 size_t n;
166 FILE *from_fp = fopen(from, "r"), *to_fp;
167
168 if (from_fp == NULL)
169 goto out;
170
171 to_fp = fopen(to, "w");
172 if (to_fp == NULL)
173 goto out_fclose_from;
174
175 while (getline(&line, &n, from_fp) > 0)
176 if (fputs(line, to_fp) == EOF)
177 goto out_fclose_to;
178 err = 0;
179out_fclose_to:
180 fclose(to_fp);
181 free(line);
182out_fclose_from:
183 fclose(from_fp);
184out:
185 return err;
186}
187
Namhyung Kim9c9f5a22015-05-18 09:30:18 +0900188int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
189{
190 void *ptr;
191 loff_t pgoff;
192
193 pgoff = off_in & ~(page_size - 1);
194 off_in -= pgoff;
195
196 ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
197 if (ptr == MAP_FAILED)
198 return -1;
199
200 while (size) {
201 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
202 if (ret < 0 && errno == EINTR)
203 continue;
204 if (ret <= 0)
205 break;
206
207 size -= ret;
208 off_in += ret;
209 off_out -= ret;
210 }
211 munmap(ptr, off_in + size);
212
213 return size ? -1 : 0;
214}
215
Adrian Hunter9a17d722013-10-14 13:43:41 +0300216int copyfile_mode(const char *from, const char *to, mode_t mode)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200217{
218 int fromfd, tofd;
219 struct stat st;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200220 int err = -1;
Milos Vyleteld7c72602015-06-08 16:50:16 +0200221 char *tmp = NULL, *ptr = NULL;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200222
223 if (stat(from, &st))
224 goto out;
225
Milos Vyleteld7c72602015-06-08 16:50:16 +0200226 /* extra 'x' at the end is to reserve space for '.' */
227 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
228 tmp = NULL;
229 goto out;
230 }
231 ptr = strrchr(tmp, '/');
232 if (!ptr)
233 goto out;
234 ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
235 *ptr = '.';
236
237 tofd = mkstemp(tmp);
238 if (tofd < 0)
239 goto out;
240
241 if (fchmod(tofd, mode))
242 goto out_close_to;
243
244 if (st.st_size == 0) { /* /proc? do it slowly... */
245 err = slow_copyfile(from, tmp);
246 goto out_close_to;
247 }
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200248
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200249 fromfd = open(from, O_RDONLY);
250 if (fromfd < 0)
Milos Vyleteld7c72602015-06-08 16:50:16 +0200251 goto out_close_to;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200252
Namhyung Kim9c9f5a22015-05-18 09:30:18 +0900253 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200254
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200255 close(fromfd);
Milos Vyleteld7c72602015-06-08 16:50:16 +0200256out_close_to:
257 close(tofd);
258 if (!err)
259 err = link(tmp, to);
260 unlink(tmp);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200261out:
Milos Vyleteld7c72602015-06-08 16:50:16 +0200262 free(tmp);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200263 return err;
264}
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300265
Adrian Hunter9a17d722013-10-14 13:43:41 +0300266int copyfile(const char *from, const char *to)
267{
268 return copyfile_mode(from, to, 0755);
269}
270
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300271unsigned long convert_unit(unsigned long value, char *unit)
272{
273 *unit = ' ';
274
275 if (value > 1000) {
276 value /= 1000;
277 *unit = 'K';
278 }
279
280 if (value > 1000) {
281 value /= 1000;
282 *unit = 'M';
283 }
284
285 if (value > 1000) {
286 value /= 1000;
287 *unit = 'G';
288 }
289
290 return value;
291}
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200292
Jiri Olsabc3a5022013-11-28 11:30:16 +0100293static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200294{
295 void *buf_start = buf;
Jiri Olsa838d1452013-11-28 11:30:15 +0100296 size_t left = n;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200297
Jiri Olsa838d1452013-11-28 11:30:15 +0100298 while (left) {
Jiri Olsabc3a5022013-11-28 11:30:16 +0100299 ssize_t ret = is_read ? read(fd, buf, left) :
300 write(fd, buf, left);
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200301
Namhyung Kime148c762014-04-24 22:27:32 +0900302 if (ret < 0 && errno == EINTR)
303 continue;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200304 if (ret <= 0)
305 return ret;
306
Jiri Olsa838d1452013-11-28 11:30:15 +0100307 left -= ret;
308 buf += ret;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200309 }
310
Jiri Olsa838d1452013-11-28 11:30:15 +0100311 BUG_ON((size_t)(buf - buf_start) != n);
312 return n;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200313}
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -0300314
Jiri Olsabc3a5022013-11-28 11:30:16 +0100315/*
316 * Read exactly 'n' bytes or return an error.
317 */
318ssize_t readn(int fd, void *buf, size_t n)
319{
320 return ion(true, fd, buf, n);
321}
322
323/*
324 * Write exactly 'n' bytes or return an error.
325 */
326ssize_t writen(int fd, void *buf, size_t n)
327{
328 return ion(false, fd, buf, n);
329}
330
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -0300331size_t hex_width(u64 v)
332{
333 size_t n = 1;
334
335 while ((v >>= 4))
336 ++n;
337
338 return n;
339}
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -0300340
Jiri Olsab2aff5f2012-10-27 23:18:30 +0200341static int hex(char ch)
342{
343 if ((ch >= '0') && (ch <= '9'))
344 return ch - '0';
345 if ((ch >= 'a') && (ch <= 'f'))
346 return ch - 'a' + 10;
347 if ((ch >= 'A') && (ch <= 'F'))
348 return ch - 'A' + 10;
349 return -1;
350}
351
352/*
353 * While we find nice hex chars, build a long_val.
354 * Return number of chars processed.
355 */
356int hex2u64(const char *ptr, u64 *long_val)
357{
358 const char *p = ptr;
359 *long_val = 0;
360
361 while (*p) {
362 const int hex_val = hex(*p);
363
364 if (hex_val < 0)
365 break;
366
367 *long_val = (*long_val << 4) | hex_val;
368 p++;
369 }
370
371 return p - ptr;
372}
373
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -0300374/* Obtain a backtrace and print it to stdout. */
Ingo Molnar89fe8082013-09-30 12:07:11 +0200375#ifdef HAVE_BACKTRACE_SUPPORT
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -0300376void dump_stack(void)
377{
378 void *array[16];
379 size_t size = backtrace(array, ARRAY_SIZE(array));
380 char **strings = backtrace_symbols(array, size);
381 size_t i;
382
383 printf("Obtained %zd stack frames.\n", size);
384
385 for (i = 0; i < size; i++)
386 printf("%s\n", strings[i]);
387
388 free(strings);
389}
Irina Tirdeac9f08be2012-09-08 03:43:23 +0300390#else
391void dump_stack(void) {}
392#endif
David Ahern2c803e52013-01-14 10:48:01 -0700393
Arnaldo Carvalho de Melo07c1a0d2015-02-24 15:34:23 -0300394void sighandler_dump_stack(int sig)
395{
396 psignal(sig, "perf");
397 dump_stack();
Arnaldo Carvalho de Melo9daddf62015-12-11 16:43:57 -0300398 signal(sig, SIG_DFL);
399 raise(sig);
Arnaldo Carvalho de Melo07c1a0d2015-02-24 15:34:23 -0300400}
401
Namhyung Kim3b47abe2013-06-04 10:50:29 +0900402int parse_nsec_time(const char *str, u64 *ptime)
403{
404 u64 time_sec, time_nsec;
405 char *end;
406
407 time_sec = strtoul(str, &end, 10);
408 if (*end != '.' && *end != '\0')
409 return -1;
410
411 if (*end == '.') {
412 int i;
413 char nsec_buf[10];
414
415 if (strlen(++end) > 9)
416 return -1;
417
418 strncpy(nsec_buf, end, 9);
419 nsec_buf[9] = '\0';
420
421 /* make it nsec precision */
422 for (i = strlen(nsec_buf); i < 9; i++)
423 nsec_buf[i] = '0';
424
425 time_nsec = strtoul(nsec_buf, &end, 10);
426 if (*end != '\0')
427 return -1;
428 } else
429 time_nsec = 0;
430
431 *ptime = time_sec * NSEC_PER_SEC + time_nsec;
432 return 0;
433}
Jiri Olsa27050f52013-09-01 12:36:13 +0200434
435unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
436{
437 struct parse_tag *i = tags;
438
439 while (i->tag) {
440 char *s;
441
442 s = strchr(str, i->tag);
443 if (s) {
444 unsigned long int value;
445 char *endptr;
446
447 value = strtoul(str, &endptr, 10);
448 if (s != endptr)
449 break;
450
Adrian Hunter56921be2013-10-22 10:34:17 +0300451 if (value > ULONG_MAX / i->mult)
452 break;
Jiri Olsa27050f52013-09-01 12:36:13 +0200453 value *= i->mult;
454 return value;
455 }
456 i++;
457 }
458
459 return (unsigned long) -1;
460}
Arnaldo Carvalho de Melo97a07f12013-10-17 16:33:43 -0300461
Kan Liang076a30c2015-08-06 15:44:52 -0400462int get_stack_size(const char *str, unsigned long *_size)
463{
464 char *endptr;
465 unsigned long size;
466 unsigned long max_size = round_down(USHRT_MAX, sizeof(u64));
467
468 size = strtoul(str, &endptr, 0);
469
470 do {
471 if (*endptr)
472 break;
473
474 size = round_up(size, sizeof(u64));
475 if (!size || size > max_size)
476 break;
477
478 *_size = size;
479 return 0;
480
481 } while (0);
482
483 pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
484 max_size, str);
485 return -1;
486}
487
488int parse_callchain_record(const char *arg, struct callchain_param *param)
489{
490 char *tok, *name, *saveptr = NULL;
491 char *buf;
492 int ret = -1;
493
494 /* We need buffer that we know we can write to. */
495 buf = malloc(strlen(arg) + 1);
496 if (!buf)
497 return -ENOMEM;
498
499 strcpy(buf, arg);
500
501 tok = strtok_r((char *)buf, ",", &saveptr);
502 name = tok ? : (char *)buf;
503
504 do {
505 /* Framepointer style */
506 if (!strncmp(name, "fp", sizeof("fp"))) {
507 if (!strtok_r(NULL, ",", &saveptr)) {
508 param->record_mode = CALLCHAIN_FP;
509 ret = 0;
510 } else
511 pr_err("callchain: No more arguments "
512 "needed for --call-graph fp\n");
513 break;
514
Kan Liang076a30c2015-08-06 15:44:52 -0400515 /* Dwarf style */
516 } else if (!strncmp(name, "dwarf", sizeof("dwarf"))) {
517 const unsigned long default_stack_dump_size = 8192;
518
519 ret = 0;
520 param->record_mode = CALLCHAIN_DWARF;
521 param->dump_size = default_stack_dump_size;
522
523 tok = strtok_r(NULL, ",", &saveptr);
524 if (tok) {
525 unsigned long size = 0;
526
527 ret = get_stack_size(tok, &size);
528 param->dump_size = size;
529 }
Kan Liang076a30c2015-08-06 15:44:52 -0400530 } else if (!strncmp(name, "lbr", sizeof("lbr"))) {
531 if (!strtok_r(NULL, ",", &saveptr)) {
532 param->record_mode = CALLCHAIN_LBR;
533 ret = 0;
534 } else
535 pr_err("callchain: No more arguments "
536 "needed for --call-graph lbr\n");
537 break;
538 } else {
539 pr_err("callchain: Unknown --call-graph option "
540 "value: %s\n", arg);
541 break;
542 }
543
544 } while (0);
545
546 free(buf);
547 return ret;
548}
549
Dongsheng Yange1a2b172013-12-06 17:25:51 -0500550const char *get_filename_for_perf_kvm(void)
551{
552 const char *filename;
553
554 if (perf_host && !perf_guest)
555 filename = strdup("perf.data.host");
556 else if (!perf_host && perf_guest)
557 filename = strdup("perf.data.guest");
558 else
559 filename = strdup("perf.data.kvm");
560
561 return filename;
562}
Adrian Hunter1a472452013-12-11 14:36:23 +0200563
564int perf_event_paranoid(void)
565{
Adrian Hunter1a472452013-12-11 14:36:23 +0200566 int value;
567
Arnaldo Carvalho de Meloce273092014-12-11 13:37:59 -0300568 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
Adrian Hunter1a472452013-12-11 14:36:23 +0200569 return INT_MAX;
570
571 return value;
572}
Adrian Hunter71db07b2013-12-11 14:36:32 +0200573
574void mem_bswap_32(void *src, int byte_size)
575{
576 u32 *m = src;
577 while (byte_size > 0) {
578 *m = bswap_32(*m);
579 byte_size -= sizeof(u32);
580 ++m;
581 }
582}
583
584void mem_bswap_64(void *src, int byte_size)
585{
586 u64 *m = src;
587
588 while (byte_size > 0) {
589 *m = bswap_64(*m);
590 byte_size -= sizeof(u64);
591 ++m;
592 }
593}
Jiri Olsa63914ac2014-08-01 17:46:54 +0200594
595bool find_process(const char *name)
596{
597 size_t len = strlen(name);
598 DIR *dir;
599 struct dirent *d;
600 int ret = -1;
601
602 dir = opendir(procfs__mountpoint());
603 if (!dir)
Peter Senna Tschudinbf644562015-09-17 12:08:53 +0200604 return false;
Jiri Olsa63914ac2014-08-01 17:46:54 +0200605
606 /* Walk through the directory. */
607 while (ret && (d = readdir(dir)) != NULL) {
608 char path[PATH_MAX];
609 char *data;
610 size_t size;
611
612 if ((d->d_type != DT_DIR) ||
613 !strcmp(".", d->d_name) ||
614 !strcmp("..", d->d_name))
615 continue;
616
617 scnprintf(path, sizeof(path), "%s/%s/comm",
618 procfs__mountpoint(), d->d_name);
619
620 if (filename__read_str(path, &data, &size))
621 continue;
622
623 ret = strncmp(name, data, len);
624 free(data);
625 }
626
627 closedir(dir);
628 return ret ? false : true;
629}
Wang Nan07bc5c62015-11-06 13:55:35 +0000630
631int
632fetch_kernel_version(unsigned int *puint, char *str,
633 size_t str_size)
634{
635 struct utsname utsname;
636 int version, patchlevel, sublevel, err;
637
638 if (uname(&utsname))
639 return -1;
640
641 if (str && str_size) {
642 strncpy(str, utsname.release, str_size);
643 str[str_size - 1] = '\0';
644 }
645
646 err = sscanf(utsname.release, "%d.%d.%d",
647 &version, &patchlevel, &sublevel);
648
649 if (err != 3) {
650 pr_debug("Unablt to get kernel version from uname '%s'\n",
651 utsname.release);
652 return -1;
653 }
654
655 if (puint)
656 *puint = (version << 16) + (patchlevel << 8) + sublevel;
657 return 0;
658}
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900659
660const char *perf_tip(const char *dirpath)
661{
662 struct strlist *tips;
663 struct str_node *node;
664 char *tip = NULL;
665 struct strlist_config conf = {
Namhyung Kim34b7b0f2016-01-09 19:16:29 +0900666 .dirname = dirpath,
667 .file_only = true,
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900668 };
669
670 tips = strlist__new("tips.txt", &conf);
Namhyung Kim34b7b0f2016-01-09 19:16:29 +0900671 if (tips == NULL)
672 return errno == ENOENT ? NULL : "Tip: get more memory! ;-p";
673
674 if (strlist__nr_entries(tips) == 0)
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900675 goto out;
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900676
677 node = strlist__entry(tips, random() % strlist__nr_entries(tips));
678 if (asprintf(&tip, "Tip: %s", node->s) < 0)
679 tip = (char *)"Tip: get more memory! ;-)";
680
681out:
682 strlist__delete(tips);
683
684 return tip;
685}
Jiri Olsa40356722016-01-20 12:56:32 +0100686
687bool is_regular_file(const char *file)
688{
689 struct stat st;
690
691 if (stat(file, &st))
692 return false;
693
694 return S_ISREG(st.st_mode);
695}
Wang Nan37b20152016-01-25 09:56:13 +0000696
697int fetch_current_timestamp(char *buf, size_t sz)
698{
699 struct timeval tv;
700 struct tm tm;
701 char dt[32];
702
703 if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm))
704 return -1;
705
706 if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm))
707 return -1;
708
709 scnprintf(buf, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000);
710
711 return 0;
712}
Wang Nanc339b1a2016-02-24 11:20:44 +0000713
714void print_binary(unsigned char *data, size_t len,
715 size_t bytes_per_line, print_binary_t printer,
716 void *extra)
717{
718 size_t i, j, mask;
719
720 if (!printer)
721 return;
722
723 bytes_per_line = roundup_pow_of_two(bytes_per_line);
724 mask = bytes_per_line - 1;
725
726 printer(BINARY_PRINT_DATA_BEGIN, 0, extra);
727 for (i = 0; i < len; i++) {
728 if ((i & mask) == 0) {
729 printer(BINARY_PRINT_LINE_BEGIN, -1, extra);
730 printer(BINARY_PRINT_ADDR, i, extra);
731 }
732
733 printer(BINARY_PRINT_NUM_DATA, data[i], extra);
734
735 if (((i & mask) == mask) || i == len - 1) {
736 for (j = 0; j < mask-(i & mask); j++)
737 printer(BINARY_PRINT_NUM_PAD, -1, extra);
738
739 printer(BINARY_PRINT_SEP, i, extra);
740 for (j = i & ~mask; j <= i; j++)
741 printer(BINARY_PRINT_CHAR_DATA, data[j], extra);
742 for (j = 0; j < mask-(i & mask); j++)
743 printer(BINARY_PRINT_CHAR_PAD, i, extra);
744 printer(BINARY_PRINT_LINE_END, -1, extra);
745 }
746 }
747 printer(BINARY_PRINT_DATA_END, -1, extra);
748}