blob: 28c9f335006c962a5df924e30d9a45687775c201 [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>
Jiri Olsa838d1452013-11-28 11:30:15 +010016#include <linux/kernel.h>
Wang Nanc339b1a2016-02-24 11:20:44 +000017#include <linux/log2.h>
Arnaldo Carvalho de Melobd48c632016-08-05 15:40:30 -030018#include <linux/time64.h>
Jiri Olsa9398c482014-08-11 10:50:02 +020019#include <unistd.h>
Namhyung Kim14cbfbe2016-01-07 20:41:53 +090020#include "strlist.h"
Jiri Olsa23aadb12014-10-01 18:00:26 +020021
Joerg Roedel1aed2672012-01-04 17:54:20 +010022/*
23 * XXX We need to find a better place for these things...
24 */
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -030025unsigned int page_size;
Don Zickus2b1b7102014-05-30 16:10:05 -040026int cacheline_size;
Arnaldo Carvalho de Melo0c1fe6b2012-10-06 14:57:10 -030027
Arnaldo Carvalho de Meloa29d5c92016-05-16 21:16:54 -030028int sysctl_perf_event_max_stack = PERF_MAX_STACK_DEPTH;
29int sysctl_perf_event_max_contexts_per_stack = PERF_MAX_CONTEXTS_PER_STACK;
Arnaldo Carvalho de Melo4cb93442016-04-27 10:16:24 -030030
Arnaldo Carvalho de Melo0c6332e2012-12-13 16:43:04 -030031bool test_attr__enabled;
32
Joerg Roedel1aed2672012-01-04 17:54:20 +010033bool perf_host = true;
Joerg Roedelc4a7dca2012-02-10 18:05:05 +010034bool perf_guest = false;
Joerg Roedel1aed2672012-01-04 17:54:20 +010035
36void event_attr_init(struct perf_event_attr *attr)
37{
38 if (!perf_host)
39 attr->exclude_host = 1;
40 if (!perf_guest)
41 attr->exclude_guest = 1;
Stephane Eranian7e1ccd32012-02-09 16:12:38 +010042 /* to capture ABI version */
43 attr->size = sizeof(*attr);
Joerg Roedel1aed2672012-01-04 17:54:20 +010044}
45
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -020046int mkdir_p(char *path, mode_t mode)
47{
48 struct stat st;
49 int err;
50 char *d = path;
51
52 if (*d != '/')
53 return -1;
54
55 if (stat(path, &st) == 0)
56 return 0;
57
58 while (*++d == '/');
59
60 while ((d = strchr(d, '/'))) {
61 *d = '\0';
62 err = stat(path, &st) && mkdir(path, mode);
63 *d++ = '/';
64 if (err)
65 return -1;
66 while (*d == '/')
67 ++d;
68 }
69 return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0;
70}
71
Joe Stringer9a9c7332017-01-26 13:19:59 -080072int rm_rf(const char *path)
Namhyung Kim0b1de0be2015-05-18 09:30:17 +090073{
74 DIR *dir;
75 int ret = 0;
76 struct dirent *d;
77 char namebuf[PATH_MAX];
78
79 dir = opendir(path);
80 if (dir == NULL)
81 return 0;
82
83 while ((d = readdir(dir)) != NULL && !ret) {
84 struct stat statbuf;
85
86 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
87 continue;
88
89 scnprintf(namebuf, sizeof(namebuf), "%s/%s",
90 path, d->d_name);
91
Masami Hiramatsu2a1ef032016-06-08 18:29:11 +090092 /* We have to check symbolic link itself */
93 ret = lstat(namebuf, &statbuf);
Namhyung Kim0b1de0be2015-05-18 09:30:17 +090094 if (ret < 0) {
95 pr_debug("stat failed: %s\n", namebuf);
96 break;
97 }
98
Masami Hiramatsu2a1ef032016-06-08 18:29:11 +090099 if (S_ISDIR(statbuf.st_mode))
Namhyung Kim0b1de0be2015-05-18 09:30:17 +0900100 ret = rm_rf(namebuf);
Masami Hiramatsu2a1ef032016-06-08 18:29:11 +0900101 else
102 ret = unlink(namebuf);
Namhyung Kim0b1de0be2015-05-18 09:30:17 +0900103 }
104 closedir(dir);
105
106 if (ret < 0)
107 return ret;
108
109 return rmdir(path);
110}
111
Masami Hiramatsue1ce7262016-04-26 18:02:42 +0900112/* A filter which removes dot files */
113bool lsdir_no_dot_filter(const char *name __maybe_unused, struct dirent *d)
114{
115 return d->d_name[0] != '.';
116}
117
118/* lsdir reads a directory and store it in strlist */
119struct strlist *lsdir(const char *name,
120 bool (*filter)(const char *, struct dirent *))
121{
122 struct strlist *list = NULL;
123 DIR *dir;
124 struct dirent *d;
125
126 dir = opendir(name);
127 if (!dir)
128 return NULL;
129
130 list = strlist__new(NULL, NULL);
131 if (!list) {
Masami Hiramatsu357a54f2016-05-11 22:51:27 +0900132 errno = ENOMEM;
Masami Hiramatsue1ce7262016-04-26 18:02:42 +0900133 goto out;
134 }
135
136 while ((d = readdir(dir)) != NULL) {
137 if (!filter || filter(name, d))
138 strlist__add(list, d->d_name);
139 }
140
141out:
142 closedir(dir);
143 return list;
144}
145
Milos Vyleteld7c72602015-06-08 16:50:16 +0200146static int slow_copyfile(const char *from, const char *to)
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200147{
Adrian Hunter9a17d722013-10-14 13:43:41 +0300148 int err = -1;
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200149 char *line = NULL;
150 size_t n;
151 FILE *from_fp = fopen(from, "r"), *to_fp;
152
153 if (from_fp == NULL)
154 goto out;
155
156 to_fp = fopen(to, "w");
157 if (to_fp == NULL)
158 goto out_fclose_from;
159
160 while (getline(&line, &n, from_fp) > 0)
161 if (fputs(line, to_fp) == EOF)
162 goto out_fclose_to;
163 err = 0;
164out_fclose_to:
165 fclose(to_fp);
166 free(line);
167out_fclose_from:
168 fclose(from_fp);
169out:
170 return err;
171}
172
Namhyung Kim9c9f5a22015-05-18 09:30:18 +0900173int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size)
174{
175 void *ptr;
176 loff_t pgoff;
177
178 pgoff = off_in & ~(page_size - 1);
179 off_in -= pgoff;
180
181 ptr = mmap(NULL, off_in + size, PROT_READ, MAP_PRIVATE, ifd, pgoff);
182 if (ptr == MAP_FAILED)
183 return -1;
184
185 while (size) {
186 ssize_t ret = pwrite(ofd, ptr + off_in, size, off_out);
187 if (ret < 0 && errno == EINTR)
188 continue;
189 if (ret <= 0)
190 break;
191
192 size -= ret;
193 off_in += ret;
194 off_out -= ret;
195 }
196 munmap(ptr, off_in + size);
197
198 return size ? -1 : 0;
199}
200
Adrian Hunter9a17d722013-10-14 13:43:41 +0300201int copyfile_mode(const char *from, const char *to, mode_t mode)
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200202{
203 int fromfd, tofd;
204 struct stat st;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200205 int err = -1;
Milos Vyleteld7c72602015-06-08 16:50:16 +0200206 char *tmp = NULL, *ptr = NULL;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200207
208 if (stat(from, &st))
209 goto out;
210
Milos Vyleteld7c72602015-06-08 16:50:16 +0200211 /* extra 'x' at the end is to reserve space for '.' */
212 if (asprintf(&tmp, "%s.XXXXXXx", to) < 0) {
213 tmp = NULL;
214 goto out;
215 }
216 ptr = strrchr(tmp, '/');
217 if (!ptr)
218 goto out;
219 ptr = memmove(ptr + 1, ptr, strlen(ptr) - 1);
220 *ptr = '.';
221
222 tofd = mkstemp(tmp);
223 if (tofd < 0)
224 goto out;
225
226 if (fchmod(tofd, mode))
227 goto out_close_to;
228
229 if (st.st_size == 0) { /* /proc? do it slowly... */
230 err = slow_copyfile(from, tmp);
231 goto out_close_to;
232 }
Arnaldo Carvalho de Melo9e201442010-01-14 18:30:06 -0200233
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200234 fromfd = open(from, O_RDONLY);
235 if (fromfd < 0)
Milos Vyleteld7c72602015-06-08 16:50:16 +0200236 goto out_close_to;
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200237
Namhyung Kim9c9f5a22015-05-18 09:30:18 +0900238 err = copyfile_offset(fromfd, 0, tofd, 0, st.st_size);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200239
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200240 close(fromfd);
Milos Vyleteld7c72602015-06-08 16:50:16 +0200241out_close_to:
242 close(tofd);
243 if (!err)
244 err = link(tmp, to);
245 unlink(tmp);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200246out:
Milos Vyleteld7c72602015-06-08 16:50:16 +0200247 free(tmp);
Arnaldo Carvalho de Melo4cf40132009-12-27 21:37:06 -0200248 return err;
249}
Arnaldo Carvalho de Meloc82ee822010-05-14 14:19:35 -0300250
Adrian Hunter9a17d722013-10-14 13:43:41 +0300251int copyfile(const char *from, const char *to)
252{
253 return copyfile_mode(from, to, 0755);
254}
255
Jiri Olsabc3a5022013-11-28 11:30:16 +0100256static ssize_t ion(bool is_read, int fd, void *buf, size_t n)
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200257{
258 void *buf_start = buf;
Jiri Olsa838d1452013-11-28 11:30:15 +0100259 size_t left = n;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200260
Jiri Olsa838d1452013-11-28 11:30:15 +0100261 while (left) {
Jiri Olsabc3a5022013-11-28 11:30:16 +0100262 ssize_t ret = is_read ? read(fd, buf, left) :
263 write(fd, buf, left);
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200264
Namhyung Kime148c762014-04-24 22:27:32 +0900265 if (ret < 0 && errno == EINTR)
266 continue;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200267 if (ret <= 0)
268 return ret;
269
Jiri Olsa838d1452013-11-28 11:30:15 +0100270 left -= ret;
271 buf += ret;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200272 }
273
Jiri Olsa838d1452013-11-28 11:30:15 +0100274 BUG_ON((size_t)(buf - buf_start) != n);
275 return n;
Arnaldo Carvalho de Melo1e7972c2011-01-03 16:50:55 -0200276}
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -0300277
Jiri Olsabc3a5022013-11-28 11:30:16 +0100278/*
279 * Read exactly 'n' bytes or return an error.
280 */
281ssize_t readn(int fd, void *buf, size_t n)
282{
283 return ion(true, fd, buf, n);
284}
285
286/*
287 * Write exactly 'n' bytes or return an error.
288 */
289ssize_t writen(int fd, void *buf, size_t n)
290{
291 return ion(false, fd, buf, n);
292}
293
Arnaldo Carvalho de Melo61e04b32012-04-19 13:15:24 -0300294size_t hex_width(u64 v)
295{
296 size_t n = 1;
297
298 while ((v >>= 4))
299 ++n;
300
301 return n;
302}
Arnaldo Carvalho de Melodc4552b2012-08-07 23:32:05 -0300303
Jiri Olsab2aff5f2012-10-27 23:18:30 +0200304static int hex(char ch)
305{
306 if ((ch >= '0') && (ch <= '9'))
307 return ch - '0';
308 if ((ch >= 'a') && (ch <= 'f'))
309 return ch - 'a' + 10;
310 if ((ch >= 'A') && (ch <= 'F'))
311 return ch - 'A' + 10;
312 return -1;
313}
314
315/*
316 * While we find nice hex chars, build a long_val.
317 * Return number of chars processed.
318 */
319int hex2u64(const char *ptr, u64 *long_val)
320{
321 const char *p = ptr;
322 *long_val = 0;
323
324 while (*p) {
325 const int hex_val = hex(*p);
326
327 if (hex_val < 0)
328 break;
329
330 *long_val = (*long_val << 4) | hex_val;
331 p++;
332 }
333
334 return p - ptr;
335}
336
Adrian Hunter1a472452013-12-11 14:36:23 +0200337int perf_event_paranoid(void)
338{
Adrian Hunter1a472452013-12-11 14:36:23 +0200339 int value;
340
Arnaldo Carvalho de Meloce273092014-12-11 13:37:59 -0300341 if (sysctl__read_int("kernel/perf_event_paranoid", &value))
Adrian Hunter1a472452013-12-11 14:36:23 +0200342 return INT_MAX;
343
344 return value;
345}
Adrian Hunter71db07b2013-12-11 14:36:32 +0200346
Jiri Olsa63914ac2014-08-01 17:46:54 +0200347bool find_process(const char *name)
348{
349 size_t len = strlen(name);
350 DIR *dir;
351 struct dirent *d;
352 int ret = -1;
353
354 dir = opendir(procfs__mountpoint());
355 if (!dir)
Peter Senna Tschudinbf644562015-09-17 12:08:53 +0200356 return false;
Jiri Olsa63914ac2014-08-01 17:46:54 +0200357
358 /* Walk through the directory. */
359 while (ret && (d = readdir(dir)) != NULL) {
360 char path[PATH_MAX];
361 char *data;
362 size_t size;
363
364 if ((d->d_type != DT_DIR) ||
365 !strcmp(".", d->d_name) ||
366 !strcmp("..", d->d_name))
367 continue;
368
369 scnprintf(path, sizeof(path), "%s/%s/comm",
370 procfs__mountpoint(), d->d_name);
371
372 if (filename__read_str(path, &data, &size))
373 continue;
374
375 ret = strncmp(name, data, len);
376 free(data);
377 }
378
379 closedir(dir);
380 return ret ? false : true;
381}
Wang Nan07bc5c62015-11-06 13:55:35 +0000382
Wang Nand18acd12016-11-15 04:05:44 +0000383static int
384fetch_ubuntu_kernel_version(unsigned int *puint)
385{
386 ssize_t len;
387 size_t line_len = 0;
388 char *ptr, *line = NULL;
389 int version, patchlevel, sublevel, err;
390 FILE *vsig = fopen("/proc/version_signature", "r");
391
392 if (!vsig) {
393 pr_debug("Open /proc/version_signature failed: %s\n",
394 strerror(errno));
395 return -1;
396 }
397
398 len = getline(&line, &line_len, vsig);
399 fclose(vsig);
400 err = -1;
401 if (len <= 0) {
402 pr_debug("Reading from /proc/version_signature failed: %s\n",
403 strerror(errno));
404 goto errout;
405 }
406
407 ptr = strrchr(line, ' ');
408 if (!ptr) {
409 pr_debug("Parsing /proc/version_signature failed: %s\n", line);
410 goto errout;
411 }
412
413 err = sscanf(ptr + 1, "%d.%d.%d",
414 &version, &patchlevel, &sublevel);
415 if (err != 3) {
416 pr_debug("Unable to get kernel version from /proc/version_signature '%s'\n",
417 line);
418 goto errout;
419 }
420
421 if (puint)
422 *puint = (version << 16) + (patchlevel << 8) + sublevel;
423 err = 0;
424errout:
425 free(line);
426 return err;
427}
428
Wang Nan07bc5c62015-11-06 13:55:35 +0000429int
430fetch_kernel_version(unsigned int *puint, char *str,
431 size_t str_size)
432{
433 struct utsname utsname;
434 int version, patchlevel, sublevel, err;
Wang Nand18acd12016-11-15 04:05:44 +0000435 bool int_ver_ready = false;
436
437 if (access("/proc/version_signature", R_OK) == 0)
438 if (!fetch_ubuntu_kernel_version(puint))
439 int_ver_ready = true;
Wang Nan07bc5c62015-11-06 13:55:35 +0000440
441 if (uname(&utsname))
442 return -1;
443
444 if (str && str_size) {
445 strncpy(str, utsname.release, str_size);
446 str[str_size - 1] = '\0';
447 }
448
449 err = sscanf(utsname.release, "%d.%d.%d",
450 &version, &patchlevel, &sublevel);
451
452 if (err != 3) {
Wang Nand18acd12016-11-15 04:05:44 +0000453 pr_debug("Unable to get kernel version from uname '%s'\n",
Wang Nan07bc5c62015-11-06 13:55:35 +0000454 utsname.release);
455 return -1;
456 }
457
Wang Nand18acd12016-11-15 04:05:44 +0000458 if (puint && !int_ver_ready)
Wang Nan07bc5c62015-11-06 13:55:35 +0000459 *puint = (version << 16) + (patchlevel << 8) + sublevel;
460 return 0;
461}
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900462
463const char *perf_tip(const char *dirpath)
464{
465 struct strlist *tips;
466 struct str_node *node;
467 char *tip = NULL;
468 struct strlist_config conf = {
Namhyung Kim34b7b0f2016-01-09 19:16:29 +0900469 .dirname = dirpath,
470 .file_only = true,
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900471 };
472
473 tips = strlist__new("tips.txt", &conf);
Namhyung Kim34b7b0f2016-01-09 19:16:29 +0900474 if (tips == NULL)
David Carrillo-Cisneros570eda02017-04-11 23:49:16 -0700475 return errno == ENOENT ? NULL :
476 "Tip: check path of tips.txt or get more memory! ;-p";
Namhyung Kim34b7b0f2016-01-09 19:16:29 +0900477
478 if (strlist__nr_entries(tips) == 0)
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900479 goto out;
Namhyung Kim14cbfbe2016-01-07 20:41:53 +0900480
481 node = strlist__entry(tips, random() % strlist__nr_entries(tips));
482 if (asprintf(&tip, "Tip: %s", node->s) < 0)
483 tip = (char *)"Tip: get more memory! ;-)";
484
485out:
486 strlist__delete(tips);
487
488 return tip;
489}