Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 1 | #include "../perf.h" |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 2 | #include "util.h" |
Jiri Olsa | 84f5d36 | 2014-07-14 23:46:48 +0200 | [diff] [blame] | 3 | #include "debug.h" |
Borislav Petkov | cd0cfad | 2013-12-09 17:14:24 +0100 | [diff] [blame] | 4 | #include <api/fs/fs.h> |
Frederic Weisbecker | 69e3f52 | 2010-01-16 14:21:15 +0100 | [diff] [blame] | 5 | #include <sys/mman.h> |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 6 | #ifdef HAVE_BACKTRACE_SUPPORT |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 7 | #include <execinfo.h> |
Irina Tirdea | c9f08be | 2012-09-08 03:43:23 +0300 | [diff] [blame] | 8 | #endif |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
Jiri Olsa | cef82c9 | 2013-12-03 14:09:22 +0100 | [diff] [blame] | 11 | #include <string.h> |
| 12 | #include <errno.h> |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 13 | #include <limits.h> |
Adrian Hunter | 71db07b | 2013-12-11 14:36:32 +0200 | [diff] [blame] | 14 | #include <byteswap.h> |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 15 | #include <linux/kernel.h> |
Jiri Olsa | 9398c48 | 2014-08-11 10:50:02 +0200 | [diff] [blame] | 16 | #include <unistd.h> |
Jiri Olsa | 23aadb1 | 2014-10-01 18:00:26 +0200 | [diff] [blame] | 17 | #include "callchain.h" |
| 18 | |
| 19 | struct callchain_param callchain_param = { |
| 20 | .mode = CHAIN_GRAPH_REL, |
| 21 | .min_percent = 0.5, |
| 22 | .order = ORDER_CALLEE, |
| 23 | .key = CCKEY_FUNCTION |
| 24 | }; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 25 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 26 | /* |
| 27 | * XXX We need to find a better place for these things... |
| 28 | */ |
Arnaldo Carvalho de Melo | 0c1fe6b | 2012-10-06 14:57:10 -0300 | [diff] [blame] | 29 | unsigned int page_size; |
Don Zickus | 2b1b710 | 2014-05-30 16:10:05 -0400 | [diff] [blame] | 30 | int cacheline_size; |
Arnaldo Carvalho de Melo | 0c1fe6b | 2012-10-06 14:57:10 -0300 | [diff] [blame] | 31 | |
Arnaldo Carvalho de Melo | 0c6332e | 2012-12-13 16:43:04 -0300 | [diff] [blame] | 32 | bool test_attr__enabled; |
| 33 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 34 | bool perf_host = true; |
Joerg Roedel | c4a7dca | 2012-02-10 18:05:05 +0100 | [diff] [blame] | 35 | bool perf_guest = false; |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 36 | |
Borislav Petkov | 1355915 | 2013-02-20 16:32:31 +0100 | [diff] [blame] | 37 | char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events"; |
| 38 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 39 | void event_attr_init(struct perf_event_attr *attr) |
| 40 | { |
| 41 | if (!perf_host) |
| 42 | attr->exclude_host = 1; |
| 43 | if (!perf_guest) |
| 44 | attr->exclude_guest = 1; |
Stephane Eranian | 7e1ccd3 | 2012-02-09 16:12:38 +0100 | [diff] [blame] | 45 | /* to capture ABI version */ |
| 46 | attr->size = sizeof(*attr); |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 49 | int mkdir_p(char *path, mode_t mode) |
| 50 | { |
| 51 | struct stat st; |
| 52 | int err; |
| 53 | char *d = path; |
| 54 | |
| 55 | if (*d != '/') |
| 56 | return -1; |
| 57 | |
| 58 | if (stat(path, &st) == 0) |
| 59 | return 0; |
| 60 | |
| 61 | while (*++d == '/'); |
| 62 | |
| 63 | while ((d = strchr(d, '/'))) { |
| 64 | *d = '\0'; |
| 65 | err = stat(path, &st) && mkdir(path, mode); |
| 66 | *d++ = '/'; |
| 67 | if (err) |
| 68 | return -1; |
| 69 | while (*d == '/') |
| 70 | ++d; |
| 71 | } |
| 72 | return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0; |
| 73 | } |
| 74 | |
Namhyung Kim | 0b1de0be | 2015-05-18 09:30:17 +0900 | [diff] [blame^] | 75 | int rm_rf(char *path) |
| 76 | { |
| 77 | DIR *dir; |
| 78 | int ret = 0; |
| 79 | struct dirent *d; |
| 80 | char namebuf[PATH_MAX]; |
| 81 | |
| 82 | dir = opendir(path); |
| 83 | if (dir == NULL) |
| 84 | return 0; |
| 85 | |
| 86 | while ((d = readdir(dir)) != NULL && !ret) { |
| 87 | struct stat statbuf; |
| 88 | |
| 89 | if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) |
| 90 | continue; |
| 91 | |
| 92 | scnprintf(namebuf, sizeof(namebuf), "%s/%s", |
| 93 | path, d->d_name); |
| 94 | |
| 95 | ret = stat(namebuf, &statbuf); |
| 96 | if (ret < 0) { |
| 97 | pr_debug("stat failed: %s\n", namebuf); |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | if (S_ISREG(statbuf.st_mode)) |
| 102 | ret = unlink(namebuf); |
| 103 | else if (S_ISDIR(statbuf.st_mode)) |
| 104 | ret = rm_rf(namebuf); |
| 105 | else { |
| 106 | pr_debug("unknown file: %s\n", namebuf); |
| 107 | ret = -1; |
| 108 | } |
| 109 | } |
| 110 | closedir(dir); |
| 111 | |
| 112 | if (ret < 0) |
| 113 | return ret; |
| 114 | |
| 115 | return rmdir(path); |
| 116 | } |
| 117 | |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 118 | static int slow_copyfile(const char *from, const char *to, mode_t mode) |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 119 | { |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 120 | int err = -1; |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 121 | char *line = NULL; |
| 122 | size_t n; |
| 123 | FILE *from_fp = fopen(from, "r"), *to_fp; |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 124 | mode_t old_umask; |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 125 | |
| 126 | if (from_fp == NULL) |
| 127 | goto out; |
| 128 | |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 129 | old_umask = umask(mode ^ 0777); |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 130 | to_fp = fopen(to, "w"); |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 131 | umask(old_umask); |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 132 | if (to_fp == NULL) |
| 133 | goto out_fclose_from; |
| 134 | |
| 135 | while (getline(&line, &n, from_fp) > 0) |
| 136 | if (fputs(line, to_fp) == EOF) |
| 137 | goto out_fclose_to; |
| 138 | err = 0; |
| 139 | out_fclose_to: |
| 140 | fclose(to_fp); |
| 141 | free(line); |
| 142 | out_fclose_from: |
| 143 | fclose(from_fp); |
| 144 | out: |
| 145 | return err; |
| 146 | } |
| 147 | |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 148 | int copyfile_mode(const char *from, const char *to, mode_t mode) |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 149 | { |
| 150 | int fromfd, tofd; |
| 151 | struct stat st; |
| 152 | void *addr; |
| 153 | int err = -1; |
| 154 | |
| 155 | if (stat(from, &st)) |
| 156 | goto out; |
| 157 | |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 158 | if (st.st_size == 0) /* /proc? do it slowly... */ |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 159 | return slow_copyfile(from, to, mode); |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 160 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 161 | fromfd = open(from, O_RDONLY); |
| 162 | if (fromfd < 0) |
| 163 | goto out; |
| 164 | |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 165 | tofd = creat(to, mode); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 166 | if (tofd < 0) |
| 167 | goto out_close_from; |
| 168 | |
| 169 | addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0); |
| 170 | if (addr == MAP_FAILED) |
| 171 | goto out_close_to; |
| 172 | |
| 173 | if (write(tofd, addr, st.st_size) == st.st_size) |
| 174 | err = 0; |
| 175 | |
| 176 | munmap(addr, st.st_size); |
| 177 | out_close_to: |
| 178 | close(tofd); |
| 179 | if (err) |
| 180 | unlink(to); |
| 181 | out_close_from: |
| 182 | close(fromfd); |
| 183 | out: |
| 184 | return err; |
| 185 | } |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 186 | |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 187 | int copyfile(const char *from, const char *to) |
| 188 | { |
| 189 | return copyfile_mode(from, to, 0755); |
| 190 | } |
| 191 | |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 192 | unsigned long convert_unit(unsigned long value, char *unit) |
| 193 | { |
| 194 | *unit = ' '; |
| 195 | |
| 196 | if (value > 1000) { |
| 197 | value /= 1000; |
| 198 | *unit = 'K'; |
| 199 | } |
| 200 | |
| 201 | if (value > 1000) { |
| 202 | value /= 1000; |
| 203 | *unit = 'M'; |
| 204 | } |
| 205 | |
| 206 | if (value > 1000) { |
| 207 | value /= 1000; |
| 208 | *unit = 'G'; |
| 209 | } |
| 210 | |
| 211 | return value; |
| 212 | } |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 213 | |
Jiri Olsa | bc3a502 | 2013-11-28 11:30:16 +0100 | [diff] [blame] | 214 | static ssize_t ion(bool is_read, int fd, void *buf, size_t n) |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 215 | { |
| 216 | void *buf_start = buf; |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 217 | size_t left = n; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 218 | |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 219 | while (left) { |
Jiri Olsa | bc3a502 | 2013-11-28 11:30:16 +0100 | [diff] [blame] | 220 | ssize_t ret = is_read ? read(fd, buf, left) : |
| 221 | write(fd, buf, left); |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 222 | |
Namhyung Kim | e148c76 | 2014-04-24 22:27:32 +0900 | [diff] [blame] | 223 | if (ret < 0 && errno == EINTR) |
| 224 | continue; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 225 | if (ret <= 0) |
| 226 | return ret; |
| 227 | |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 228 | left -= ret; |
| 229 | buf += ret; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 230 | } |
| 231 | |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame] | 232 | BUG_ON((size_t)(buf - buf_start) != n); |
| 233 | return n; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 234 | } |
Arnaldo Carvalho de Melo | 61e04b3 | 2012-04-19 13:15:24 -0300 | [diff] [blame] | 235 | |
Jiri Olsa | bc3a502 | 2013-11-28 11:30:16 +0100 | [diff] [blame] | 236 | /* |
| 237 | * Read exactly 'n' bytes or return an error. |
| 238 | */ |
| 239 | ssize_t readn(int fd, void *buf, size_t n) |
| 240 | { |
| 241 | return ion(true, fd, buf, n); |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Write exactly 'n' bytes or return an error. |
| 246 | */ |
| 247 | ssize_t writen(int fd, void *buf, size_t n) |
| 248 | { |
| 249 | return ion(false, fd, buf, n); |
| 250 | } |
| 251 | |
Arnaldo Carvalho de Melo | 61e04b3 | 2012-04-19 13:15:24 -0300 | [diff] [blame] | 252 | size_t hex_width(u64 v) |
| 253 | { |
| 254 | size_t n = 1; |
| 255 | |
| 256 | while ((v >>= 4)) |
| 257 | ++n; |
| 258 | |
| 259 | return n; |
| 260 | } |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 261 | |
Jiri Olsa | b2aff5f | 2012-10-27 23:18:30 +0200 | [diff] [blame] | 262 | static int hex(char ch) |
| 263 | { |
| 264 | if ((ch >= '0') && (ch <= '9')) |
| 265 | return ch - '0'; |
| 266 | if ((ch >= 'a') && (ch <= 'f')) |
| 267 | return ch - 'a' + 10; |
| 268 | if ((ch >= 'A') && (ch <= 'F')) |
| 269 | return ch - 'A' + 10; |
| 270 | return -1; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * While we find nice hex chars, build a long_val. |
| 275 | * Return number of chars processed. |
| 276 | */ |
| 277 | int hex2u64(const char *ptr, u64 *long_val) |
| 278 | { |
| 279 | const char *p = ptr; |
| 280 | *long_val = 0; |
| 281 | |
| 282 | while (*p) { |
| 283 | const int hex_val = hex(*p); |
| 284 | |
| 285 | if (hex_val < 0) |
| 286 | break; |
| 287 | |
| 288 | *long_val = (*long_val << 4) | hex_val; |
| 289 | p++; |
| 290 | } |
| 291 | |
| 292 | return p - ptr; |
| 293 | } |
| 294 | |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 295 | /* Obtain a backtrace and print it to stdout. */ |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 296 | #ifdef HAVE_BACKTRACE_SUPPORT |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 297 | void dump_stack(void) |
| 298 | { |
| 299 | void *array[16]; |
| 300 | size_t size = backtrace(array, ARRAY_SIZE(array)); |
| 301 | char **strings = backtrace_symbols(array, size); |
| 302 | size_t i; |
| 303 | |
| 304 | printf("Obtained %zd stack frames.\n", size); |
| 305 | |
| 306 | for (i = 0; i < size; i++) |
| 307 | printf("%s\n", strings[i]); |
| 308 | |
| 309 | free(strings); |
| 310 | } |
Irina Tirdea | c9f08be | 2012-09-08 03:43:23 +0300 | [diff] [blame] | 311 | #else |
| 312 | void dump_stack(void) {} |
| 313 | #endif |
David Ahern | 2c803e5 | 2013-01-14 10:48:01 -0700 | [diff] [blame] | 314 | |
Arnaldo Carvalho de Melo | 07c1a0d | 2015-02-24 15:34:23 -0300 | [diff] [blame] | 315 | void sighandler_dump_stack(int sig) |
| 316 | { |
| 317 | psignal(sig, "perf"); |
| 318 | dump_stack(); |
| 319 | exit(sig); |
| 320 | } |
| 321 | |
David Ahern | 2c803e5 | 2013-01-14 10:48:01 -0700 | [diff] [blame] | 322 | void get_term_dimensions(struct winsize *ws) |
| 323 | { |
| 324 | char *s = getenv("LINES"); |
| 325 | |
| 326 | if (s != NULL) { |
| 327 | ws->ws_row = atoi(s); |
| 328 | s = getenv("COLUMNS"); |
| 329 | if (s != NULL) { |
| 330 | ws->ws_col = atoi(s); |
| 331 | if (ws->ws_row && ws->ws_col) |
| 332 | return; |
| 333 | } |
| 334 | } |
| 335 | #ifdef TIOCGWINSZ |
| 336 | if (ioctl(1, TIOCGWINSZ, ws) == 0 && |
| 337 | ws->ws_row && ws->ws_col) |
| 338 | return; |
| 339 | #endif |
| 340 | ws->ws_row = 25; |
| 341 | ws->ws_col = 80; |
| 342 | } |
Borislav Petkov | 1355915 | 2013-02-20 16:32:31 +0100 | [diff] [blame] | 343 | |
Jiri Olsa | 9398c48 | 2014-08-11 10:50:02 +0200 | [diff] [blame] | 344 | void set_term_quiet_input(struct termios *old) |
| 345 | { |
| 346 | struct termios tc; |
| 347 | |
| 348 | tcgetattr(0, old); |
| 349 | tc = *old; |
| 350 | tc.c_lflag &= ~(ICANON | ECHO); |
| 351 | tc.c_cc[VMIN] = 0; |
| 352 | tc.c_cc[VTIME] = 0; |
| 353 | tcsetattr(0, TCSANOW, &tc); |
| 354 | } |
| 355 | |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 356 | static void set_tracing_events_path(const char *tracing, const char *mountpoint) |
Borislav Petkov | 1355915 | 2013-02-20 16:32:31 +0100 | [diff] [blame] | 357 | { |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 358 | snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s%s", |
| 359 | mountpoint, tracing, "events"); |
Borislav Petkov | 1355915 | 2013-02-20 16:32:31 +0100 | [diff] [blame] | 360 | } |
| 361 | |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 362 | static const char *__perf_tracefs_mount(const char *mountpoint) |
| 363 | { |
| 364 | const char *mnt; |
| 365 | |
| 366 | mnt = tracefs_mount(mountpoint); |
| 367 | if (!mnt) |
| 368 | return NULL; |
| 369 | |
| 370 | set_tracing_events_path("", mnt); |
| 371 | |
| 372 | return mnt; |
| 373 | } |
| 374 | |
| 375 | static const char *__perf_debugfs_mount(const char *mountpoint) |
Borislav Petkov | 1355915 | 2013-02-20 16:32:31 +0100 | [diff] [blame] | 376 | { |
| 377 | const char *mnt; |
| 378 | |
| 379 | mnt = debugfs_mount(mountpoint); |
| 380 | if (!mnt) |
| 381 | return NULL; |
| 382 | |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 383 | set_tracing_events_path("tracing/", mnt); |
| 384 | |
| 385 | return mnt; |
| 386 | } |
| 387 | |
| 388 | const char *perf_debugfs_mount(const char *mountpoint) |
| 389 | { |
| 390 | const char *mnt; |
| 391 | |
| 392 | mnt = __perf_tracefs_mount(mountpoint); |
| 393 | if (mnt) |
| 394 | return mnt; |
| 395 | |
| 396 | mnt = __perf_debugfs_mount(mountpoint); |
Borislav Petkov | 1355915 | 2013-02-20 16:32:31 +0100 | [diff] [blame] | 397 | |
| 398 | return mnt; |
| 399 | } |
| 400 | |
| 401 | void perf_debugfs_set_path(const char *mntpt) |
| 402 | { |
| 403 | snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt); |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 404 | set_tracing_events_path("tracing/", mntpt); |
| 405 | } |
| 406 | |
| 407 | static const char *find_tracefs(void) |
| 408 | { |
| 409 | const char *path = __perf_tracefs_mount(NULL); |
| 410 | |
| 411 | return path; |
Borislav Petkov | 1355915 | 2013-02-20 16:32:31 +0100 | [diff] [blame] | 412 | } |
Namhyung Kim | 167aedc | 2013-06-26 16:14:04 +0900 | [diff] [blame] | 413 | |
| 414 | static const char *find_debugfs(void) |
| 415 | { |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 416 | const char *path = __perf_debugfs_mount(NULL); |
Namhyung Kim | 167aedc | 2013-06-26 16:14:04 +0900 | [diff] [blame] | 417 | |
| 418 | if (!path) |
| 419 | fprintf(stderr, "Your kernel does not support the debugfs filesystem"); |
| 420 | |
| 421 | return path; |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * Finds the path to the debugfs/tracing |
| 426 | * Allocates the string and stores it. |
| 427 | */ |
| 428 | const char *find_tracing_dir(void) |
| 429 | { |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 430 | const char *tracing_dir = ""; |
Namhyung Kim | 167aedc | 2013-06-26 16:14:04 +0900 | [diff] [blame] | 431 | static char *tracing; |
| 432 | static int tracing_found; |
| 433 | const char *debugfs; |
| 434 | |
| 435 | if (tracing_found) |
| 436 | return tracing; |
| 437 | |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 438 | debugfs = find_tracefs(); |
| 439 | if (!debugfs) { |
| 440 | tracing_dir = "/tracing"; |
| 441 | debugfs = find_debugfs(); |
| 442 | if (!debugfs) |
| 443 | return NULL; |
| 444 | } |
Namhyung Kim | 167aedc | 2013-06-26 16:14:04 +0900 | [diff] [blame] | 445 | |
Steven Rostedt (Red Hat) | 23773ca | 2015-02-02 14:35:07 -0500 | [diff] [blame] | 446 | if (asprintf(&tracing, "%s%s", debugfs, tracing_dir) < 0) |
Namhyung Kim | 167aedc | 2013-06-26 16:14:04 +0900 | [diff] [blame] | 447 | return NULL; |
| 448 | |
Namhyung Kim | 167aedc | 2013-06-26 16:14:04 +0900 | [diff] [blame] | 449 | tracing_found = 1; |
| 450 | return tracing; |
| 451 | } |
| 452 | |
| 453 | char *get_tracing_file(const char *name) |
| 454 | { |
| 455 | const char *tracing; |
| 456 | char *file; |
| 457 | |
| 458 | tracing = find_tracing_dir(); |
| 459 | if (!tracing) |
| 460 | return NULL; |
| 461 | |
Andy Shevchenko | d400a68 | 2014-07-04 14:43:48 +0300 | [diff] [blame] | 462 | if (asprintf(&file, "%s/%s", tracing, name) < 0) |
Namhyung Kim | 167aedc | 2013-06-26 16:14:04 +0900 | [diff] [blame] | 463 | return NULL; |
| 464 | |
Namhyung Kim | 167aedc | 2013-06-26 16:14:04 +0900 | [diff] [blame] | 465 | return file; |
| 466 | } |
| 467 | |
| 468 | void put_tracing_file(char *file) |
| 469 | { |
| 470 | free(file); |
| 471 | } |
Namhyung Kim | 3b47abe | 2013-06-04 10:50:29 +0900 | [diff] [blame] | 472 | |
| 473 | int parse_nsec_time(const char *str, u64 *ptime) |
| 474 | { |
| 475 | u64 time_sec, time_nsec; |
| 476 | char *end; |
| 477 | |
| 478 | time_sec = strtoul(str, &end, 10); |
| 479 | if (*end != '.' && *end != '\0') |
| 480 | return -1; |
| 481 | |
| 482 | if (*end == '.') { |
| 483 | int i; |
| 484 | char nsec_buf[10]; |
| 485 | |
| 486 | if (strlen(++end) > 9) |
| 487 | return -1; |
| 488 | |
| 489 | strncpy(nsec_buf, end, 9); |
| 490 | nsec_buf[9] = '\0'; |
| 491 | |
| 492 | /* make it nsec precision */ |
| 493 | for (i = strlen(nsec_buf); i < 9; i++) |
| 494 | nsec_buf[i] = '0'; |
| 495 | |
| 496 | time_nsec = strtoul(nsec_buf, &end, 10); |
| 497 | if (*end != '\0') |
| 498 | return -1; |
| 499 | } else |
| 500 | time_nsec = 0; |
| 501 | |
| 502 | *ptime = time_sec * NSEC_PER_SEC + time_nsec; |
| 503 | return 0; |
| 504 | } |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 505 | |
| 506 | unsigned long parse_tag_value(const char *str, struct parse_tag *tags) |
| 507 | { |
| 508 | struct parse_tag *i = tags; |
| 509 | |
| 510 | while (i->tag) { |
| 511 | char *s; |
| 512 | |
| 513 | s = strchr(str, i->tag); |
| 514 | if (s) { |
| 515 | unsigned long int value; |
| 516 | char *endptr; |
| 517 | |
| 518 | value = strtoul(str, &endptr, 10); |
| 519 | if (s != endptr) |
| 520 | break; |
| 521 | |
Adrian Hunter | 56921be | 2013-10-22 10:34:17 +0300 | [diff] [blame] | 522 | if (value > ULONG_MAX / i->mult) |
| 523 | break; |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 524 | value *= i->mult; |
| 525 | return value; |
| 526 | } |
| 527 | i++; |
| 528 | } |
| 529 | |
| 530 | return (unsigned long) -1; |
| 531 | } |
Arnaldo Carvalho de Melo | 97a07f1 | 2013-10-17 16:33:43 -0300 | [diff] [blame] | 532 | |
Jiri Olsa | cef82c9 | 2013-12-03 14:09:22 +0100 | [diff] [blame] | 533 | int filename__read_str(const char *filename, char **buf, size_t *sizep) |
| 534 | { |
| 535 | size_t size = 0, alloc_size = 0; |
| 536 | void *bf = NULL, *nbf; |
| 537 | int fd, n, err = 0; |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 538 | char sbuf[STRERR_BUFSIZE]; |
Jiri Olsa | cef82c9 | 2013-12-03 14:09:22 +0100 | [diff] [blame] | 539 | |
| 540 | fd = open(filename, O_RDONLY); |
| 541 | if (fd < 0) |
| 542 | return -errno; |
| 543 | |
| 544 | do { |
| 545 | if (size == alloc_size) { |
| 546 | alloc_size += BUFSIZ; |
| 547 | nbf = realloc(bf, alloc_size); |
| 548 | if (!nbf) { |
| 549 | err = -ENOMEM; |
| 550 | break; |
| 551 | } |
| 552 | |
| 553 | bf = nbf; |
| 554 | } |
| 555 | |
| 556 | n = read(fd, bf + size, alloc_size - size); |
| 557 | if (n < 0) { |
| 558 | if (size) { |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 559 | pr_warning("read failed %d: %s\n", errno, |
| 560 | strerror_r(errno, sbuf, sizeof(sbuf))); |
Jiri Olsa | cef82c9 | 2013-12-03 14:09:22 +0100 | [diff] [blame] | 561 | err = 0; |
| 562 | } else |
| 563 | err = -errno; |
| 564 | |
| 565 | break; |
| 566 | } |
| 567 | |
| 568 | size += n; |
| 569 | } while (n > 0); |
| 570 | |
| 571 | if (!err) { |
| 572 | *sizep = size; |
| 573 | *buf = bf; |
| 574 | } else |
| 575 | free(bf); |
| 576 | |
| 577 | close(fd); |
| 578 | return err; |
| 579 | } |
Dongsheng Yang | e1a2b17 | 2013-12-06 17:25:51 -0500 | [diff] [blame] | 580 | |
| 581 | const char *get_filename_for_perf_kvm(void) |
| 582 | { |
| 583 | const char *filename; |
| 584 | |
| 585 | if (perf_host && !perf_guest) |
| 586 | filename = strdup("perf.data.host"); |
| 587 | else if (!perf_host && perf_guest) |
| 588 | filename = strdup("perf.data.guest"); |
| 589 | else |
| 590 | filename = strdup("perf.data.kvm"); |
| 591 | |
| 592 | return filename; |
| 593 | } |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 594 | |
| 595 | int perf_event_paranoid(void) |
| 596 | { |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 597 | int value; |
| 598 | |
Arnaldo Carvalho de Melo | ce27309 | 2014-12-11 13:37:59 -0300 | [diff] [blame] | 599 | if (sysctl__read_int("kernel/perf_event_paranoid", &value)) |
Adrian Hunter | 1a47245 | 2013-12-11 14:36:23 +0200 | [diff] [blame] | 600 | return INT_MAX; |
| 601 | |
| 602 | return value; |
| 603 | } |
Adrian Hunter | 71db07b | 2013-12-11 14:36:32 +0200 | [diff] [blame] | 604 | |
| 605 | void mem_bswap_32(void *src, int byte_size) |
| 606 | { |
| 607 | u32 *m = src; |
| 608 | while (byte_size > 0) { |
| 609 | *m = bswap_32(*m); |
| 610 | byte_size -= sizeof(u32); |
| 611 | ++m; |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | void mem_bswap_64(void *src, int byte_size) |
| 616 | { |
| 617 | u64 *m = src; |
| 618 | |
| 619 | while (byte_size > 0) { |
| 620 | *m = bswap_64(*m); |
| 621 | byte_size -= sizeof(u64); |
| 622 | ++m; |
| 623 | } |
| 624 | } |
Jiri Olsa | 63914ac | 2014-08-01 17:46:54 +0200 | [diff] [blame] | 625 | |
| 626 | bool find_process(const char *name) |
| 627 | { |
| 628 | size_t len = strlen(name); |
| 629 | DIR *dir; |
| 630 | struct dirent *d; |
| 631 | int ret = -1; |
| 632 | |
| 633 | dir = opendir(procfs__mountpoint()); |
| 634 | if (!dir) |
| 635 | return -1; |
| 636 | |
| 637 | /* Walk through the directory. */ |
| 638 | while (ret && (d = readdir(dir)) != NULL) { |
| 639 | char path[PATH_MAX]; |
| 640 | char *data; |
| 641 | size_t size; |
| 642 | |
| 643 | if ((d->d_type != DT_DIR) || |
| 644 | !strcmp(".", d->d_name) || |
| 645 | !strcmp("..", d->d_name)) |
| 646 | continue; |
| 647 | |
| 648 | scnprintf(path, sizeof(path), "%s/%s/comm", |
| 649 | procfs__mountpoint(), d->d_name); |
| 650 | |
| 651 | if (filename__read_str(path, &data, &size)) |
| 652 | continue; |
| 653 | |
| 654 | ret = strncmp(name, data, len); |
| 655 | free(data); |
| 656 | } |
| 657 | |
| 658 | closedir(dir); |
| 659 | return ret ? false : true; |
| 660 | } |