blob: 6450c75a6f5b40bc28e9fe5f98cb98960cec1d10 [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>
Arnaldo Carvalho de Melo7a8ef4c2017-04-19 20:57:47 -03006#include <sys/stat.h>
Wang Nan07bc5c62015-11-06 13:55:35 +00007#include <sys/utsname.h>
Arnaldo Carvalho de Melo76b31a22017-04-18 12:26:44 -03008#include <dirent.h>
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03009#include <inttypes.h>
Arnaldo Carvalho de Melo9607ad32017-04-19 15:49:18 -030010#include <signal.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>
Namhyung Kim14cbfbe2016-01-07 20:41:53 +090021#include "strlist.h"
Jiri Olsa23aadb12014-10-01 18:00:26 +020022
Joerg Roedel1aed2672012-01-04 17:54:20 +010023/*
24 * XXX We need to find a better place for these things...
25 */
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -030026unsigned int page_size;
Don Zickus2b1b7102014-05-30 16:10:05 -040027int cacheline_size;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -030028
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -030029int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
30int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -030031
Arnaldo Carvalho de Melo0c6332e2012-12-13 16:43:04 -030032bool test_attr__enabled;
33
Joerg Roedel1aed2672012-01-04 17:54:20 +010034bool perf_host = true;
Joerg Roedelc4a7dca2012-02-10 18:05:05 +010035bool perf_guest = false;
Joerg Roedel1aed2672012-01-04 17:54:20 +010036
37void event_attr_init(struct perf_event_attr *attr)
38{
39 if (!perf_host)
40 attr->exclude_host = 1;
41 if (!perf_guest)
42 attr->exclude_guest = 1;
Stephane Eranian7e1ccd32012-02-09 16:12:38 +010043 /* to capture ABI version */
44 attr->size = sizeof(*attr);
Joerg Roedel1aed2672012-01-04 17:54:20 +010045}
46
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020047int mkdir_p(char *path, mode_t mode)
48{
49 struct stat st;
50 int err;
51 char *d = path;
52
53 if (*d != '/')
54 return -1;
55
56 if (stat(path, &st) == 0)
57 return 0;
58
59 while (*++d == '/');
60
61 while ((d = strchr(d, '/'))) {
62 *d = '\0';
63 err = stat(path, &st) && mkdir(path, mode);
64 *d++ = '/';
65 if (err)
66 return -1;
67 while (*d == '/')
68 ++d;
69 }
70 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
71}
72
Joe Stringer9a9c7332017-01-26 13:19:59 -080073int rm_rf(const char *path)
Namhyung Kim0b1de0be2015-05-18 09:30:17 +090074{
75 DIR *dir;
76 int ret = 0;
77 struct dirent *d;
78 char namebuf[PATH_MAX];
79
80 dir = opendir(path);
81 if (dir == NULL)
82 return 0;
83
84 while ((d = readdir(dir)) != NULL && !ret) {
85 struct stat statbuf;
86
87 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
88 continue;
89
90 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
91 path, d->d_name);
92
Masami Hiramatsu2a1ef032016-06-08 18:29:11 +090093 /* We have to check symbolic link itself */
94 ret = lstat(namebuf, &statbuf);
Namhyung Kim0b1de0be2015-05-18 09:30:17 +090095 if (ret < 0) {
96 pr_debug("stat failed: %s\n", namebuf);
97 break;
98 }
99
Masami Hiramatsu2a1ef032016-06-08 18:29:11 +0900100 if (S_ISDIR(statbuf.st_mode))
Namhyung Kim0b1de0be2015-05-18 09:30:17 +0900101 ret = rm_rf(namebuf);
Masami Hiramatsu2a1ef032016-06-08 18:29:11 +0900102 else
103 ret = unlink(namebuf);
Namhyung Kim0b1de0be2015-05-18 09:30:17 +0900104 }
105 closedir(dir);
106
107 if (ret < 0)
108 return ret;
109
110 return rmdir(path);
111}
112
Masami Hiramatsue1ce7262016-04-26 18:02:42 +0900113/* A filter which removes dot files */
114bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
115{
116 return d->d_name[0] != '.';
117}
118
119/* lsdir reads a directory and store it in strlist */
120struct strlist *lsdir(const char *name,
121 bool (*filter)(const char *, struct dirent *))
122{
123 struct strlist *list = NULL;
124 DIR *dir;
125 struct dirent *d;
126
127 dir = opendir(name);
128 if (!dir)
129 return NULL;
130
131 list = strlist__new(NULL, NULL);
132 if (!list) {
Masami Hiramatsu357a54f2016-05-11 22:51:27 +0900133 errno = ENOMEM;
Masami Hiramatsue1ce7262016-04-26 18:02:42 +0900134 goto out;
135 }
136
137 while ((d = readdir(dir)) != NULL) {
138 if (!filter || filter(name, d))
139 strlist__add(list, d->d_name);
140 }
141
142out:
143 closedir(dir);
144 return list;
145}
146
Milos Vyleteld7c72602015-06-08 16:50:16 +0200147static int slow_copyfile(const char *from, const char *to)
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200148{
Adrian Hunter9a17d722013-10-14 13:43:41 +0300149 int err = -1;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200150 char *line = NULL;
151 size_t n;
152 FILE *from_fp = fopen(from, "r"), *to_fp;
153
154 if (from_fp == NULL)
155 goto out;
156
157 to_fp = fopen(to, "w");
158 if (to_fp == NULL)
159 goto out_fclose_from;
160
161 while (getline(&line, &n, from_fp) > 0)
162 if (fputs(line, to_fp) == EOF)
163 goto out_fclose_to;
164 err = 0;
165out_fclose_to:
166 fclose(to_fp);
167 free(line);
168out_fclose_from:
169 fclose(from_fp);
170out:
171 return err;
172}
173
Namhyung Kim9c9f5a22015-05-18 09:30:18 +0900174int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
175{
176 void *ptr;
177 loff_t pgoff;
178
179 pgoff = off_in & ~(page_size - 1);
180 off_in -= pgoff;
181
182 ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
183 if (ptr == MAP_FAILED)
184 return -1;
185
186 while (size) {
187 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
188 if (ret < 0 && errno == EINTR)
189 continue;
190 if (ret <= 0)
191 break;
192
193 size -= ret;
194 off_in += ret;
195 off_out -= ret;
196 }
197 munmap(ptr, off_in + size);
198
199 return size ? -1 : 0;
200}
201
Adrian Hunter9a17d722013-10-14 13:43:41 +0300202int copyfile_mode(const char *from, const char *to, mode_t mode)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200203{
204 int fromfd, tofd;
205 struct stat st;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200206 int err = -1;
Milos Vyleteld7c72602015-06-08 16:50:16 +0200207 char *tmp = NULL, *ptr = NULL;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200208
209 if (stat(from, &st))
210 goto out;
211
Milos Vyleteld7c72602015-06-08 16:50:16 +0200212 /* extra 'x' at the end is to reserve space for '.' */
213 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
214 tmp = NULL;
215 goto out;
216 }
217 ptr = strrchr(tmp, '/');
218 if (!ptr)
219 goto out;
220 ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
221 *ptr = '.';
222
223 tofd = mkstemp(tmp);
224 if (tofd < 0)
225 goto out;
226
227 if (fchmod(tofd, mode))
228 goto out_close_to;
229
230 if (st.st_size == 0) { /* /proc? do it slowly... */
231 err = slow_copyfile(from, tmp);
232 goto out_close_to;
233 }
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200234
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200235 fromfd = open(from, O_RDONLY);
236 if (fromfd < 0)
Milos Vyleteld7c72602015-06-08 16:50:16 +0200237 goto out_close_to;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200238
Namhyung Kim9c9f5a22015-05-18 09:30:18 +0900239 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200240
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200241 close(fromfd);
Milos Vyleteld7c72602015-06-08 16:50:16 +0200242out_close_to:
243 close(tofd);
244 if (!err)
245 err = link(tmp, to);
246 unlink(tmp);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200247out:
Milos Vyleteld7c72602015-06-08 16:50:16 +0200248 free(tmp);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200249 return err;
250}
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300251
Adrian Hunter9a17d722013-10-14 13:43:41 +0300252int copyfile(const char *from, const char *to)
253{
254 return copyfile_mode(from, to, 0755);
255}
256
Jiri Olsabc3a5022013-11-28 11:30:16 +0100257static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200258{
259 void *buf_start = buf;
Jiri Olsa838d1452013-11-28 11:30:15 +0100260 size_t left = n;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200261
Jiri Olsa838d1452013-11-28 11:30:15 +0100262 while (left) {
Jiri Olsabc3a5022013-11-28 11:30:16 +0100263 ssize_t ret = is_read ? read(fd, buf, left) :
264 write(fd, buf, left);
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200265
Namhyung Kime148c762014-04-24 22:27:32 +0900266 if (ret < 0 && errno == EINTR)
267 continue;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200268 if (ret <= 0)
269 return ret;
270
Jiri Olsa838d1452013-11-28 11:30:15 +0100271 left -= ret;
272 buf += ret;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200273 }
274
Jiri Olsa838d1452013-11-28 11:30:15 +0100275 BUG_ON((size_t)(buf - buf_start) != n);
276 return n;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200277}
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -0300278
Jiri Olsabc3a5022013-11-28 11:30:16 +0100279/*
280 * Read exactly 'n' bytes or return an error.
281 */
282ssize_t readn(int fd, void *buf, size_t n)
283{
284 return ion(true, fd, buf, n);
285}
286
287/*
288 * Write exactly 'n' bytes or return an error.
289 */
290ssize_t writen(int fd, void *buf, size_t n)
291{
292 return ion(false, fd, buf, n);
293}
294
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -0300295size_t hex_width(u64 v)
296{
297 size_t n = 1;
298
299 while ((v >>= 4))
300 ++n;
301
302 return n;
303}
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -0300304
Jiri Olsab2aff5f2012-10-27 23:18:30 +0200305static int hex(char ch)
306{
307 if ((ch >= '0') && (ch <= '9'))
308 return ch - '0';
309 if ((ch >= 'a') && (ch <= 'f'))
310 return ch - 'a' + 10;
311 if ((ch >= 'A') && (ch <= 'F'))
312 return ch - 'A' + 10;
313 return -1;
314}
315
316/*
317 * While we find nice hex chars, build a long_val.
318 * Return number of chars processed.
319 */
320int hex2u64(const char *ptr, u64 *long_val)
321{
322 const char *p = ptr;
323 *long_val = 0;
324
325 while (*p) {
326 const int hex_val = hex(*p);
327
328 if (hex_val < 0)
329 break;
330
331 *long_val = (*long_val << 4) | hex_val;
332 p++;
333 }
334
335 return p - ptr;
336}
337
Jiri Olsa27050f52013-09-01 12:36:13 +0200338unsigned long parse_tag_value(const char *str, struct parse_tag *tags)
339{
340 struct parse_tag *i = tags;
341
342 while (i->tag) {
343 char *s;
344
345 s = strchr(str, i->tag);
346 if (s) {
347 unsigned long int value;
348 char *endptr;
349
350 value = strtoul(str, &endptr, 10);
351 if (s != endptr)
352 break;
353
Adrian Hunter56921be2013-10-22 10:34:17 +0300354 if (value > ULONG_MAX / i->mult)
355 break;
Jiri Olsa27050f52013-09-01 12:36:13 +0200356 value *= i->mult;
357 return value;
358 }
359 i++;
360 }
361
362 return (unsigned long) -1;
363}
Arnaldo Carvalho de Melo97a07f12013-10-17 16:33:43 -0300364
Adrian Hunter1a472452013-12-11 14:36:23 +0200365int perf_event_paranoid(void)
366{
Adrian Hunter1a472452013-12-11 14:36:23 +0200367 int value;
368
Arnaldo Carvalho de Meloce273092014-12-11 13:37:59 -0300369 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
Adrian Hunter1a472452013-12-11 14:36:23 +0200370 return INT_MAX;
371
372 return value;
373}
Adrian Hunter71db07b2013-12-11 14:36:32 +0200374
375void mem_bswap_32(void *src, int byte_size)
376{
377 u32 *m = src;
378 while (byte_size > 0) {
379 *m = bswap_32(*m);
380 byte_size -= sizeof(u32);
381 ++m;
382 }
383}
384
385void mem_bswap_64(void *src, int byte_size)
386{
387 u64 *m = src;
388
389 while (byte_size > 0) {
390 *m = bswap_64(*m);
391 byte_size -= sizeof(u64);
392 ++m;
393 }
394}
Jiri Olsa63914ac2014-08-01 17:46:54 +0200395
396bool find_process(const char *name)
397{
398 size_t len = strlen(name);
399 DIR *dir;
400 struct dirent *d;
401 int ret = -1;
402
403 dir = opendir(procfs__mountpoint());
404 if (!dir)
Peter Senna Tschudinbf644562015-09-17 12:08:53 +0200405 return false;
Jiri Olsa63914ac2014-08-01 17:46:54 +0200406
407 /* Walk through the directory. */
408 while (ret && (d = readdir(dir)) != NULL) {
409 char path[PATH_MAX];
410 char *data;
411 size_t size;
412
413 if ((d->d_type != DT_DIR) ||
414 !strcmp(".", d->d_name) ||
415 !strcmp("..", d->d_name))
416 continue;
417
418 scnprintf(path, sizeof(path), "%s/%s/comm",
419 procfs__mountpoint(), d->d_name);
420
421 if (filename__read_str(path, &data, &size))
422 continue;
423
424 ret = strncmp(name, data, len);
425 free(data);
426 }
427
428 closedir(dir);
429 return ret ? false : true;
430}
Wang Nan07bc5c62015-11-06 13:55:35 +0000431
Wang Nand18acd12016-11-15 04:05:44 +0000432static int
433fetch_ubuntu_kernel_version(unsigned int *puint)
434{
435 ssize_t len;
436 size_t line_len = 0;
437 char *ptr, *line = NULL;
438 int version, patchlevel, sublevel, err;
439 FILE *vsig = fopen("/proc/version_signature", "r");
440
441 if (!vsig) {
442 pr_debug("Open /proc/version_signature failed: %s\n",
443 strerror(errno));
444 return -1;
445 }
446
447 len = getline(&line, &line_len, vsig);
448 fclose(vsig);
449 err = -1;
450 if (len <= 0) {
451 pr_debug("Reading from /proc/version_signature failed: %s\n",
452 strerror(errno));
453 goto errout;
454 }
455
456 ptr = strrchr(line, ' ');
457 if (!ptr) {
458 pr_debug("Parsing /proc/version_signature failed: %s\n", line);
459 goto errout;
460 }
461
462 err = sscanf(ptr + 1, "%d.%d.%d",
463 &version, &patchlevel, &sublevel);
464 if (err != 3) {
465 pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
466 line);
467 goto errout;
468 }
469
470 if (puint)
471 *puint = (version << 16) + (patchlevel << 8) + sublevel;
472 err = 0;
473errout:
474 free(line);
475 return err;
476}
477
Wang Nan07bc5c62015-11-06 13:55:35 +0000478int
479fetch_kernel_version(unsigned int *puint, char *str,
480 size_t str_size)
481{
482 struct utsname utsname;
483 int version, patchlevel, sublevel, err;
Wang Nand18acd12016-11-15 04:05:44 +0000484 bool int_ver_ready = false;
485
486 if (access("/proc/version_signature", R_OK) == 0)
487 if (!fetch_ubuntu_kernel_version(puint))
488 int_ver_ready = true;
Wang Nan07bc5c62015-11-06 13:55:35 +0000489
490 if (uname(&utsname))
491 return -1;
492
493 if (str && str_size) {
494 strncpy(str, utsname.release, str_size);
495 str[str_size - 1] = '\0';
496 }
497
498 err = sscanf(utsname.release, "%d.%d.%d",
499 &version, &patchlevel, &sublevel);
500
501 if (err != 3) {
Wang Nand18acd12016-11-15 04:05:44 +0000502 pr_debug("Unable to get kernel version from uname '%s'\n",
Wang Nan07bc5c62015-11-06 13:55:35 +0000503 utsname.release);
504 return -1;
505 }
506
Wang Nand18acd12016-11-15 04:05:44 +0000507 if (puint && !int_ver_ready)
Wang Nan07bc5c62015-11-06 13:55:35 +0000508 *puint = (version << 16) + (patchlevel << 8) + sublevel;
509 return 0;
510}
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900511
512const char *perf_tip(const char *dirpath)
513{
514 struct strlist *tips;
515 struct str_node *node;
516 char *tip = NULL;
517 struct strlist_config conf = {
Namhyung Kim34b7b0f2016-01-09 19:16:29 +0900518 .dirname = dirpath,
519 .file_only = true,
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900520 };
521
522 tips = strlist__new("tips.txt", &conf);
Namhyung Kim34b7b0f2016-01-09 19:16:29 +0900523 if (tips == NULL)
David Carrillo-Cisneros570eda02017-04-11 23:49:16 -0700524 return errno == ENOENT ? NULL :
525 "Tip: check path of tips.txt or get more memory! ;-p";
Namhyung Kim34b7b0f2016-01-09 19:16:29 +0900526
527 if (strlist__nr_entries(tips) == 0)
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900528 goto out;
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900529
530 node = strlist__entry(tips, random() % strlist__nr_entries(tips));
531 if (asprintf(&tip, "Tip: %s", node->s) < 0)
532 tip = (char *)"Tip: get more memory! ;-)";
533
534out:
535 strlist__delete(tips);
536
537 return tip;
538}