]> git.karo-electronics.de Git - karo-tx-linux.git/blob - tools/perf/util/util.c
b9716bc6e8fd64f1715b684aab0297f681fff48f
[karo-tx-linux.git] / tools / perf / util / util.c
1 #include "../perf.h"
2 #include "util.h"
3 #include "debug.h"
4 #include <api/fs/fs.h>
5 #include <sys/mman.h>
6 #include <sys/utsname.h>
7 #ifdef HAVE_BACKTRACE_SUPPORT
8 #include <execinfo.h>
9 #endif
10 #include <dirent.h>
11 #include <inttypes.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <errno.h>
16 #include <limits.h>
17 #include <byteswap.h>
18 #include <linux/kernel.h>
19 #include <linux/log2.h>
20 #include <linux/time64.h>
21 #include <unistd.h>
22 #include "callchain.h"
23 #include "strlist.h"
24
25 #define CALLCHAIN_PARAM_DEFAULT                 \
26         .mode           = CHAIN_GRAPH_ABS,      \
27         .min_percent    = 0.5,                  \
28         .order          = ORDER_CALLEE,         \
29         .key            = CCKEY_FUNCTION,       \
30         .value          = CCVAL_PERCENT,        \
31
32 struct callchain_param callchain_param = {
33         CALLCHAIN_PARAM_DEFAULT
34 };
35
36 struct callchain_param callchain_param_default = {
37         CALLCHAIN_PARAM_DEFAULT
38 };
39
40 /*
41  * XXX We need to find a better place for these things...
42  */
43 unsigned int page_size;
44 int cacheline_size;
45
46 int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
47 int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
48
49 bool test_attr__enabled;
50
51 bool perf_host  = true;
52 bool perf_guest = false;
53
54 void event_attr_init(struct perf_event_attr *attr)
55 {
56         if (!perf_host)
57                 attr->exclude_host  = 1;
58         if (!perf_guest)
59                 attr->exclude_guest = 1;
60         /* to capture ABI version */
61         attr->size = sizeof(*attr);
62 }
63
64 int mkdir_p(char *path, mode_t mode)
65 {
66         struct stat st;
67         int err;
68         char *d = path;
69
70         if (*d != '/')
71                 return -1;
72
73         if (stat(path, &st) == 0)
74                 return 0;
75
76         while (*++d == '/');
77
78         while ((d = strchr(d, '/'))) {
79                 *d = '\0';
80                 err = stat(path, &st) && mkdir(path, mode);
81                 *d++ = '/';
82                 if (err)
83                         return -1;
84                 while (*d == '/')
85                         ++d;
86         }
87         return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
88 }
89
90 int rm_rf(const char *path)
91 {
92         DIR *dir;
93         int ret = 0;
94         struct dirent *d;
95         char namebuf[PATH_MAX];
96
97         dir = opendir(path);
98         if (dir == NULL)
99                 return 0;
100
101         while ((d = readdir(dir)) != NULL && !ret) {
102                 struct stat statbuf;
103
104                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
105                         continue;
106
107                 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
108                           path, d->d_name);
109
110                 /* We have to check symbolic link itself */
111                 ret = lstat(namebuf, &statbuf);
112                 if (ret < 0) {
113                         pr_debug("stat failed: %s\n", namebuf);
114                         break;
115                 }
116
117                 if (S_ISDIR(statbuf.st_mode))
118                         ret = rm_rf(namebuf);
119                 else
120                         ret = unlink(namebuf);
121         }
122         closedir(dir);
123
124         if (ret < 0)
125                 return ret;
126
127         return rmdir(path);
128 }
129
130 /* A filter which removes dot files */
131 bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
132 {
133         return d->d_name[0] != '.';
134 }
135
136 /* lsdir reads a directory and store it in strlist */
137 struct strlist *lsdir(const char *name,
138                       bool (*filter)(const char *, struct dirent *))
139 {
140         struct strlist *list = NULL;
141         DIR *dir;
142         struct dirent *d;
143
144         dir = opendir(name);
145         if (!dir)
146                 return NULL;
147
148         list = strlist__new(NULL, NULL);
149         if (!list) {
150                 errno = ENOMEM;
151                 goto out;
152         }
153
154         while ((d = readdir(dir)) != NULL) {
155                 if (!filter || filter(name, d))
156                         strlist__add(list, d->d_name);
157         }
158
159 out:
160         closedir(dir);
161         return list;
162 }
163
164 static int slow_copyfile(const char *from, const char *to)
165 {
166         int err = -1;
167         char *line = NULL;
168         size_t n;
169         FILE *from_fp = fopen(from, "r"), *to_fp;
170
171         if (from_fp == NULL)
172                 goto out;
173
174         to_fp = fopen(to, "w");
175         if (to_fp == NULL)
176                 goto out_fclose_from;
177
178         while (getline(&line, &n, from_fp) > 0)
179                 if (fputs(line, to_fp) == EOF)
180                         goto out_fclose_to;
181         err = 0;
182 out_fclose_to:
183         fclose(to_fp);
184         free(line);
185 out_fclose_from:
186         fclose(from_fp);
187 out:
188         return err;
189 }
190
191 int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
192 {
193         void *ptr;
194         loff_t pgoff;
195
196         pgoff = off_in & ~(page_size - 1);
197         off_in -= pgoff;
198
199         ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
200         if (ptr == MAP_FAILED)
201                 return -1;
202
203         while (size) {
204                 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
205                 if (ret < 0 && errno == EINTR)
206                         continue;
207                 if (ret <= 0)
208                         break;
209
210                 size -= ret;
211                 off_in += ret;
212                 off_out -= ret;
213         }
214         munmap(ptr, off_in + size);
215
216         return size ? -1 : 0;
217 }
218
219 int copyfile_mode(const char *from, const char *to, mode_t mode)
220 {
221         int fromfd, tofd;
222         struct stat st;
223         int err = -1;
224         char *tmp = NULL, *ptr = NULL;
225
226         if (stat(from, &st))
227                 goto out;
228
229         /* extra 'x' at the end is to reserve space for '.' */
230         if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
231                 tmp = NULL;
232                 goto out;
233         }
234         ptr = strrchr(tmp, '/');
235         if (!ptr)
236                 goto out;
237         ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
238         *ptr = '.';
239
240         tofd = mkstemp(tmp);
241         if (tofd < 0)
242                 goto out;
243
244         if (fchmod(tofd, mode))
245                 goto out_close_to;
246
247         if (st.st_size == 0) { /* /proc? do it slowly... */
248                 err = slow_copyfile(from, tmp);
249                 goto out_close_to;
250         }
251
252         fromfd = open(from, O_RDONLY);
253         if (fromfd < 0)
254                 goto out_close_to;
255
256         err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
257
258         close(fromfd);
259 out_close_to:
260         close(tofd);
261         if (!err)
262                 err = link(tmp, to);
263         unlink(tmp);
264 out:
265         free(tmp);
266         return err;
267 }
268
269 int copyfile(const char *from, const char *to)
270 {
271         return copyfile_mode(from, to, 0755);
272 }
273
274 unsigned long convert_unit(unsigned long value, char *unit)
275 {
276         *unit = ' ';
277
278         if (value > 1000) {
279                 value /= 1000;
280                 *unit = 'K';
281         }
282
283         if (value > 1000) {
284                 value /= 1000;
285                 *unit = 'M';
286         }
287
288         if (value > 1000) {
289                 value /= 1000;
290                 *unit = 'G';
291         }
292
293         return value;
294 }
295
296 static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
297 {
298         void *buf_start = buf;
299         size_t left = n;
300
301         while (left) {
302                 ssize_t ret = is_read ? read(fd, buf, left) :
303                                         write(fd, buf, left);
304
305                 if (ret < 0 && errno == EINTR)
306                         continue;
307                 if (ret <= 0)
308                         return ret;
309
310                 left -= ret;
311                 buf  += ret;
312         }
313
314         BUG_ON((size_t)(buf - buf_start) != n);
315         return n;
316 }
317
318 /*
319  * Read exactly 'n' bytes or return an error.
320  */
321 ssize_t readn(int fd, void *buf, size_t n)
322 {
323         return ion(true, fd, buf, n);
324 }
325
326 /*
327  * Write exactly 'n' bytes or return an error.
328  */
329 ssize_t writen(int fd, void *buf, size_t n)
330 {
331         return ion(false, fd, buf, n);
332 }
333
334 size_t hex_width(u64 v)
335 {
336         size_t n = 1;
337
338         while ((v >>= 4))
339                 ++n;
340
341         return n;
342 }
343
344 static int hex(char ch)
345 {
346         if ((ch >= '0') && (ch <= '9'))
347                 return ch - '0';
348         if ((ch >= 'a') && (ch <= 'f'))
349                 return ch - 'a' + 10;
350         if ((ch >= 'A') && (ch <= 'F'))
351                 return ch - 'A' + 10;
352         return -1;
353 }
354
355 /*
356  * While we find nice hex chars, build a long_val.
357  * Return number of chars processed.
358  */
359 int hex2u64(const char *ptr, u64 *long_val)
360 {
361         const char *p = ptr;
362         *long_val = 0;
363
364         while (*p) {
365                 const int hex_val = hex(*p);
366
367                 if (hex_val < 0)
368                         break;
369
370                 *long_val = (*long_val << 4) | hex_val;
371                 p++;
372         }
373
374         return p - ptr;
375 }
376
377 /* Obtain a backtrace and print it to stdout. */
378 #ifdef HAVE_BACKTRACE_SUPPORT
379 void dump_stack(void)
380 {
381         void *array[16];
382         size_t size = backtrace(array, ARRAY_SIZE(array));
383         char **strings = backtrace_symbols(array, size);
384         size_t i;
385
386         printf("Obtained %zd stack frames.\n", size);
387
388         for (i = 0; i < size; i++)
389                 printf("%s\n", strings[i]);
390
391         free(strings);
392 }
393 #else
394 void dump_stack(void) {}
395 #endif
396
397 void sighandler_dump_stack(int sig)
398 {
399         psignal(sig, "perf");
400         dump_stack();
401         signal(sig, SIG_DFL);
402         raise(sig);
403 }
404
405 int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz)
406 {
407         u64  sec = timestamp / NSEC_PER_SEC;
408         u64 usec = (timestamp % NSEC_PER_SEC) / NSEC_PER_USEC;
409
410         return scnprintf(buf, sz, "%"PRIu64".%06"PRIu64, sec, usec);
411 }
412
413 unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
414 {
415         struct parse_tag *i = tags;
416
417         while (i->tag) {
418                 char *s;
419
420                 s = strchr(str, i->tag);
421                 if (s) {
422                         unsigned long int value;
423                         char *endptr;
424
425                         value = strtoul(str, &endptr, 10);
426                         if (s != endptr)
427                                 break;
428
429                         if (value > ULONG_MAX / i->mult)
430                                 break;
431                         value *= i->mult;
432                         return value;
433                 }
434                 i++;
435         }
436
437         return (unsigned long) -1;
438 }
439
440 int get_stack_size(const char *str, unsigned long *_size)
441 {
442         char *endptr;
443         unsigned long size;
444         unsigned long max_size = round_down(USHRT_MAX, sizeof(u64));
445
446         size = strtoul(str, &endptr, 0);
447
448         do {
449                 if (*endptr)
450                         break;
451
452                 size = round_up(size, sizeof(u64));
453                 if (!size || size > max_size)
454                         break;
455
456                 *_size = size;
457                 return 0;
458
459         } while (0);
460
461         pr_err("callchain: Incorrect stack dump size (max %ld): %s\n",
462                max_size, str);
463         return -1;
464 }
465
466 int parse_callchain_record(const char *arg, struct callchain_param *param)
467 {
468         char *tok, *name, *saveptr = NULL;
469         char *buf;
470         int ret = -1;
471
472         /* We need buffer that we know we can write to. */
473         buf = malloc(strlen(arg) + 1);
474         if (!buf)
475                 return -ENOMEM;
476
477         strcpy(buf, arg);
478
479         tok = strtok_r((char *)buf, ",", &saveptr);
480         name = tok ? : (char *)buf;
481
482         do {
483                 /* Framepointer style */
484                 if (!strncmp(name, "fp", sizeof("fp"))) {
485                         if (!strtok_r(NULL, ",", &saveptr)) {
486                                 param->record_mode = CALLCHAIN_FP;
487                                 ret = 0;
488                         } else
489                                 pr_err("callchain: No more arguments "
490                                        "needed for --call-graph fp\n");
491                         break;
492
493                 /* Dwarf style */
494                 } else if (!strncmp(name, "dwarf", sizeof("dwarf"))) {
495                         const unsigned long default_stack_dump_size = 8192;
496
497                         ret = 0;
498                         param->record_mode = CALLCHAIN_DWARF;
499                         param->dump_size = default_stack_dump_size;
500
501                         tok = strtok_r(NULL, ",", &saveptr);
502                         if (tok) {
503                                 unsigned long size = 0;
504
505                                 ret = get_stack_size(tok, &size);
506                                 param->dump_size = size;
507                         }
508                 } else if (!strncmp(name, "lbr", sizeof("lbr"))) {
509                         if (!strtok_r(NULL, ",", &saveptr)) {
510                                 param->record_mode = CALLCHAIN_LBR;
511                                 ret = 0;
512                         } else
513                                 pr_err("callchain: No more arguments "
514                                         "needed for --call-graph lbr\n");
515                         break;
516                 } else {
517                         pr_err("callchain: Unknown --call-graph option "
518                                "value: %s\n", arg);
519                         break;
520                 }
521
522         } while (0);
523
524         free(buf);
525         return ret;
526 }
527
528 const char *get_filename_for_perf_kvm(void)
529 {
530         const char *filename;
531
532         if (perf_host && !perf_guest)
533                 filename = strdup("perf.data.host");
534         else if (!perf_host && perf_guest)
535                 filename = strdup("perf.data.guest");
536         else
537                 filename = strdup("perf.data.kvm");
538
539         return filename;
540 }
541
542 int perf_event_paranoid(void)
543 {
544         int value;
545
546         if (sysctl__read_int("kernel/perf_event_paranoid", &value))
547                 return INT_MAX;
548
549         return value;
550 }
551
552 void mem_bswap_32(void *src, int byte_size)
553 {
554         u32 *m = src;
555         while (byte_size > 0) {
556                 *m = bswap_32(*m);
557                 byte_size -= sizeof(u32);
558                 ++m;
559         }
560 }
561
562 void mem_bswap_64(void *src, int byte_size)
563 {
564         u64 *m = src;
565
566         while (byte_size > 0) {
567                 *m = bswap_64(*m);
568                 byte_size -= sizeof(u64);
569                 ++m;
570         }
571 }
572
573 bool find_process(const char *name)
574 {
575         size_t len = strlen(name);
576         DIR *dir;
577         struct dirent *d;
578         int ret = -1;
579
580         dir = opendir(procfs__mountpoint());
581         if (!dir)
582                 return false;
583
584         /* Walk through the directory. */
585         while (ret && (d = readdir(dir)) != NULL) {
586                 char path[PATH_MAX];
587                 char *data;
588                 size_t size;
589
590                 if ((d->d_type != DT_DIR) ||
591                      !strcmp(".", d->d_name) ||
592                      !strcmp("..", d->d_name))
593                         continue;
594
595                 scnprintf(path, sizeof(path), "%s/%s/comm",
596                           procfs__mountpoint(), d->d_name);
597
598                 if (filename__read_str(path, &data, &size))
599                         continue;
600
601                 ret = strncmp(name, data, len);
602                 free(data);
603         }
604
605         closedir(dir);
606         return ret ? false : true;
607 }
608
609 static int
610 fetch_ubuntu_kernel_version(unsigned int *puint)
611 {
612         ssize_t len;
613         size_t line_len = 0;
614         char *ptr, *line = NULL;
615         int version, patchlevel, sublevel, err;
616         FILE *vsig = fopen("/proc/version_signature", "r");
617
618         if (!vsig) {
619                 pr_debug("Open /proc/version_signature failed: %s\n",
620                          strerror(errno));
621                 return -1;
622         }
623
624         len = getline(&line, &line_len, vsig);
625         fclose(vsig);
626         err = -1;
627         if (len <= 0) {
628                 pr_debug("Reading from /proc/version_signature failed: %s\n",
629                          strerror(errno));
630                 goto errout;
631         }
632
633         ptr = strrchr(line, ' ');
634         if (!ptr) {
635                 pr_debug("Parsing /proc/version_signature failed: %s\n", line);
636                 goto errout;
637         }
638
639         err = sscanf(ptr + 1, "%d.%d.%d",
640                      &version, &patchlevel, &sublevel);
641         if (err != 3) {
642                 pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
643                          line);
644                 goto errout;
645         }
646
647         if (puint)
648                 *puint = (version << 16) + (patchlevel << 8) + sublevel;
649         err = 0;
650 errout:
651         free(line);
652         return err;
653 }
654
655 int
656 fetch_kernel_version(unsigned int *puint, char *str,
657                      size_t str_size)
658 {
659         struct utsname utsname;
660         int version, patchlevel, sublevel, err;
661         bool int_ver_ready = false;
662
663         if (access("/proc/version_signature", R_OK) == 0)
664                 if (!fetch_ubuntu_kernel_version(puint))
665                         int_ver_ready = true;
666
667         if (uname(&utsname))
668                 return -1;
669
670         if (str && str_size) {
671                 strncpy(str, utsname.release, str_size);
672                 str[str_size - 1] = '\0';
673         }
674
675         err = sscanf(utsname.release, "%d.%d.%d",
676                      &version, &patchlevel, &sublevel);
677
678         if (err != 3) {
679                 pr_debug("Unable to get kernel version from uname '%s'\n",
680                          utsname.release);
681                 return -1;
682         }
683
684         if (puint && !int_ver_ready)
685                 *puint = (version << 16) + (patchlevel << 8) + sublevel;
686         return 0;
687 }
688
689 const char *perf_tip(const char *dirpath)
690 {
691         struct strlist *tips;
692         struct str_node *node;
693         char *tip = NULL;
694         struct strlist_config conf = {
695                 .dirname = dirpath,
696                 .file_only = true,
697         };
698
699         tips = strlist__new("tips.txt", &conf);
700         if (tips == NULL)
701                 return errno == ENOENT ? NULL :
702                         "Tip: check path of tips.txt or get more memory! ;-p";
703
704         if (strlist__nr_entries(tips) == 0)
705                 goto out;
706
707         node = strlist__entry(tips, random() % strlist__nr_entries(tips));
708         if (asprintf(&tip, "Tip: %s", node->s) < 0)
709                 tip = (char *)"Tip: get more memory! ;-)";
710
711 out:
712         strlist__delete(tips);
713
714         return tip;
715 }
716
717 int fetch_current_timestamp(char *buf, size_t sz)
718 {
719         struct timeval tv;
720         struct tm tm;
721         char dt[32];
722
723         if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm))
724                 return -1;
725
726         if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm))
727                 return -1;
728
729         scnprintf(buf, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000);
730
731         return 0;
732 }
733
734 int unit_number__scnprintf(char *buf, size_t size, u64 n)
735 {
736         char unit[4] = "BKMG";
737         int i = 0;
738
739         while (((n / 1024) > 1) && (i < 3)) {
740                 n /= 1024;
741                 i++;
742         }
743
744         return scnprintf(buf, size, "%" PRIu64 "%c", n, unit[i]);
745 }