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" |
Frederic Weisbecker | 69e3f52 | 2010-01-16 14:21:15 +0100 | [diff] [blame] | 3 | #include <sys/mman.h> |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 4 | #ifdef HAVE_BACKTRACE_SUPPORT |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 5 | #include <execinfo.h> |
Irina Tirdea | c9f08be | 2012-09-08 03:43:23 +0300 | [diff] [blame] | 6 | #endif |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame^] | 9 | #include <linux/kernel.h> |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 10 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 11 | /* |
| 12 | * XXX We need to find a better place for these things... |
| 13 | */ |
Arnaldo Carvalho de Melo | 0c1fe6b | 2012-10-06 14:57:10 -0300 | [diff] [blame] | 14 | unsigned int page_size; |
| 15 | |
Arnaldo Carvalho de Melo | 0c6332e | 2012-12-13 16:43:04 -0300 | [diff] [blame] | 16 | bool test_attr__enabled; |
| 17 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 18 | bool perf_host = true; |
Joerg Roedel | c4a7dca | 2012-02-10 18:05:05 +0100 | [diff] [blame] | 19 | bool perf_guest = false; |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 20 | |
Borislav Petkov | 1355915 | 2013-02-20 16:32:31 +0100 | [diff] [blame] | 21 | char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events"; |
| 22 | |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 23 | void event_attr_init(struct perf_event_attr *attr) |
| 24 | { |
| 25 | if (!perf_host) |
| 26 | attr->exclude_host = 1; |
| 27 | if (!perf_guest) |
| 28 | attr->exclude_guest = 1; |
Stephane Eranian | 7e1ccd3 | 2012-02-09 16:12:38 +0100 | [diff] [blame] | 29 | /* to capture ABI version */ |
| 30 | attr->size = sizeof(*attr); |
Joerg Roedel | 1aed267 | 2012-01-04 17:54:20 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 33 | int mkdir_p(char *path, mode_t mode) |
| 34 | { |
| 35 | struct stat st; |
| 36 | int err; |
| 37 | char *d = path; |
| 38 | |
| 39 | if (*d != '/') |
| 40 | return -1; |
| 41 | |
| 42 | if (stat(path, &st) == 0) |
| 43 | return 0; |
| 44 | |
| 45 | while (*++d == '/'); |
| 46 | |
| 47 | while ((d = strchr(d, '/'))) { |
| 48 | *d = '\0'; |
| 49 | err = stat(path, &st) && mkdir(path, mode); |
| 50 | *d++ = '/'; |
| 51 | if (err) |
| 52 | return -1; |
| 53 | while (*d == '/') |
| 54 | ++d; |
| 55 | } |
| 56 | return (stat(path, &st) && mkdir(path, mode)) ? -1 : 0; |
| 57 | } |
| 58 | |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 59 | 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] | 60 | { |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 61 | int err = -1; |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 62 | char *line = NULL; |
| 63 | size_t n; |
| 64 | FILE *from_fp = fopen(from, "r"), *to_fp; |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 65 | mode_t old_umask; |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 66 | |
| 67 | if (from_fp == NULL) |
| 68 | goto out; |
| 69 | |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 70 | old_umask = umask(mode ^ 0777); |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 71 | to_fp = fopen(to, "w"); |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 72 | umask(old_umask); |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 73 | if (to_fp == NULL) |
| 74 | goto out_fclose_from; |
| 75 | |
| 76 | while (getline(&line, &n, from_fp) > 0) |
| 77 | if (fputs(line, to_fp) == EOF) |
| 78 | goto out_fclose_to; |
| 79 | err = 0; |
| 80 | out_fclose_to: |
| 81 | fclose(to_fp); |
| 82 | free(line); |
| 83 | out_fclose_from: |
| 84 | fclose(from_fp); |
| 85 | out: |
| 86 | return err; |
| 87 | } |
| 88 | |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 89 | 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] | 90 | { |
| 91 | int fromfd, tofd; |
| 92 | struct stat st; |
| 93 | void *addr; |
| 94 | int err = -1; |
| 95 | |
| 96 | if (stat(from, &st)) |
| 97 | goto out; |
| 98 | |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 99 | if (st.st_size == 0) /* /proc? do it slowly... */ |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 100 | return slow_copyfile(from, to, mode); |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 101 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 102 | fromfd = open(from, O_RDONLY); |
| 103 | if (fromfd < 0) |
| 104 | goto out; |
| 105 | |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 106 | tofd = creat(to, mode); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 107 | if (tofd < 0) |
| 108 | goto out_close_from; |
| 109 | |
| 110 | addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fromfd, 0); |
| 111 | if (addr == MAP_FAILED) |
| 112 | goto out_close_to; |
| 113 | |
| 114 | if (write(tofd, addr, st.st_size) == st.st_size) |
| 115 | err = 0; |
| 116 | |
| 117 | munmap(addr, st.st_size); |
| 118 | out_close_to: |
| 119 | close(tofd); |
| 120 | if (err) |
| 121 | unlink(to); |
| 122 | out_close_from: |
| 123 | close(fromfd); |
| 124 | out: |
| 125 | return err; |
| 126 | } |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 127 | |
Adrian Hunter | 9a17d72 | 2013-10-14 13:43:41 +0300 | [diff] [blame] | 128 | int copyfile(const char *from, const char *to) |
| 129 | { |
| 130 | return copyfile_mode(from, to, 0755); |
| 131 | } |
| 132 | |
Arnaldo Carvalho de Melo | c82ee82 | 2010-05-14 14:19:35 -0300 | [diff] [blame] | 133 | unsigned long convert_unit(unsigned long value, char *unit) |
| 134 | { |
| 135 | *unit = ' '; |
| 136 | |
| 137 | if (value > 1000) { |
| 138 | value /= 1000; |
| 139 | *unit = 'K'; |
| 140 | } |
| 141 | |
| 142 | if (value > 1000) { |
| 143 | value /= 1000; |
| 144 | *unit = 'M'; |
| 145 | } |
| 146 | |
| 147 | if (value > 1000) { |
| 148 | value /= 1000; |
| 149 | *unit = 'G'; |
| 150 | } |
| 151 | |
| 152 | return value; |
| 153 | } |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 154 | |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame^] | 155 | /* |
| 156 | * Read exactly 'n' bytes or return an error. |
| 157 | */ |
Jiri Olsa | 727ebd5 | 2013-11-28 11:30:14 +0100 | [diff] [blame] | 158 | ssize_t readn(int fd, void *buf, size_t n) |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 159 | { |
| 160 | void *buf_start = buf; |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame^] | 161 | size_t left = n; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 162 | |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame^] | 163 | while (left) { |
| 164 | ssize_t ret = read(fd, buf, left); |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 165 | |
| 166 | if (ret <= 0) |
| 167 | return ret; |
| 168 | |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame^] | 169 | left -= ret; |
| 170 | buf += ret; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 171 | } |
| 172 | |
Jiri Olsa | 838d145 | 2013-11-28 11:30:15 +0100 | [diff] [blame^] | 173 | BUG_ON((size_t)(buf - buf_start) != n); |
| 174 | return n; |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 175 | } |
Arnaldo Carvalho de Melo | 61e04b3 | 2012-04-19 13:15:24 -0300 | [diff] [blame] | 176 | |
| 177 | size_t hex_width(u64 v) |
| 178 | { |
| 179 | size_t n = 1; |
| 180 | |
| 181 | while ((v >>= 4)) |
| 182 | ++n; |
| 183 | |
| 184 | return n; |
| 185 | } |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 186 | |
Jiri Olsa | b2aff5f | 2012-10-27 23:18:30 +0200 | [diff] [blame] | 187 | static int hex(char ch) |
| 188 | { |
| 189 | if ((ch >= '0') && (ch <= '9')) |
| 190 | return ch - '0'; |
| 191 | if ((ch >= 'a') && (ch <= 'f')) |
| 192 | return ch - 'a' + 10; |
| 193 | if ((ch >= 'A') && (ch <= 'F')) |
| 194 | return ch - 'A' + 10; |
| 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * While we find nice hex chars, build a long_val. |
| 200 | * Return number of chars processed. |
| 201 | */ |
| 202 | int hex2u64(const char *ptr, u64 *long_val) |
| 203 | { |
| 204 | const char *p = ptr; |
| 205 | *long_val = 0; |
| 206 | |
| 207 | while (*p) { |
| 208 | const int hex_val = hex(*p); |
| 209 | |
| 210 | if (hex_val < 0) |
| 211 | break; |
| 212 | |
| 213 | *long_val = (*long_val << 4) | hex_val; |
| 214 | p++; |
| 215 | } |
| 216 | |
| 217 | return p - ptr; |
| 218 | } |
| 219 | |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 220 | /* Obtain a backtrace and print it to stdout. */ |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 221 | #ifdef HAVE_BACKTRACE_SUPPORT |
Arnaldo Carvalho de Melo | dc4552b | 2012-08-07 23:32:05 -0300 | [diff] [blame] | 222 | void dump_stack(void) |
| 223 | { |
| 224 | void *array[16]; |
| 225 | size_t size = backtrace(array, ARRAY_SIZE(array)); |
| 226 | char **strings = backtrace_symbols(array, size); |
| 227 | size_t i; |
| 228 | |
| 229 | printf("Obtained %zd stack frames.\n", size); |
| 230 | |
| 231 | for (i = 0; i < size; i++) |
| 232 | printf("%s\n", strings[i]); |
| 233 | |
| 234 | free(strings); |
| 235 | } |
Irina Tirdea | c9f08be | 2012-09-08 03:43:23 +0300 | [diff] [blame] | 236 | #else |
| 237 | void dump_stack(void) {} |
| 238 | #endif |
David Ahern | 2c803e5 | 2013-01-14 10:48:01 -0700 | [diff] [blame] | 239 | |
| 240 | void get_term_dimensions(struct winsize *ws) |
| 241 | { |
| 242 | char *s = getenv("LINES"); |
| 243 | |
| 244 | if (s != NULL) { |
| 245 | ws->ws_row = atoi(s); |
| 246 | s = getenv("COLUMNS"); |
| 247 | if (s != NULL) { |
| 248 | ws->ws_col = atoi(s); |
| 249 | if (ws->ws_row && ws->ws_col) |
| 250 | return; |
| 251 | } |
| 252 | } |
| 253 | #ifdef TIOCGWINSZ |
| 254 | if (ioctl(1, TIOCGWINSZ, ws) == 0 && |
| 255 | ws->ws_row && ws->ws_col) |
| 256 | return; |
| 257 | #endif |
| 258 | ws->ws_row = 25; |
| 259 | ws->ws_col = 80; |
| 260 | } |
Borislav Petkov | 1355915 | 2013-02-20 16:32:31 +0100 | [diff] [blame] | 261 | |
| 262 | static void set_tracing_events_path(const char *mountpoint) |
| 263 | { |
| 264 | snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s", |
| 265 | mountpoint, "tracing/events"); |
| 266 | } |
| 267 | |
| 268 | const char *perf_debugfs_mount(const char *mountpoint) |
| 269 | { |
| 270 | const char *mnt; |
| 271 | |
| 272 | mnt = debugfs_mount(mountpoint); |
| 273 | if (!mnt) |
| 274 | return NULL; |
| 275 | |
| 276 | set_tracing_events_path(mnt); |
| 277 | |
| 278 | return mnt; |
| 279 | } |
| 280 | |
| 281 | void perf_debugfs_set_path(const char *mntpt) |
| 282 | { |
| 283 | snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt); |
| 284 | set_tracing_events_path(mntpt); |
| 285 | } |
Namhyung Kim | 167aedc | 2013-06-26 16:14:04 +0900 | [diff] [blame] | 286 | |
| 287 | static const char *find_debugfs(void) |
| 288 | { |
| 289 | const char *path = perf_debugfs_mount(NULL); |
| 290 | |
| 291 | if (!path) |
| 292 | fprintf(stderr, "Your kernel does not support the debugfs filesystem"); |
| 293 | |
| 294 | return path; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Finds the path to the debugfs/tracing |
| 299 | * Allocates the string and stores it. |
| 300 | */ |
| 301 | const char *find_tracing_dir(void) |
| 302 | { |
| 303 | static char *tracing; |
| 304 | static int tracing_found; |
| 305 | const char *debugfs; |
| 306 | |
| 307 | if (tracing_found) |
| 308 | return tracing; |
| 309 | |
| 310 | debugfs = find_debugfs(); |
| 311 | if (!debugfs) |
| 312 | return NULL; |
| 313 | |
| 314 | tracing = malloc(strlen(debugfs) + 9); |
| 315 | if (!tracing) |
| 316 | return NULL; |
| 317 | |
| 318 | sprintf(tracing, "%s/tracing", debugfs); |
| 319 | |
| 320 | tracing_found = 1; |
| 321 | return tracing; |
| 322 | } |
| 323 | |
| 324 | char *get_tracing_file(const char *name) |
| 325 | { |
| 326 | const char *tracing; |
| 327 | char *file; |
| 328 | |
| 329 | tracing = find_tracing_dir(); |
| 330 | if (!tracing) |
| 331 | return NULL; |
| 332 | |
| 333 | file = malloc(strlen(tracing) + strlen(name) + 2); |
| 334 | if (!file) |
| 335 | return NULL; |
| 336 | |
| 337 | sprintf(file, "%s/%s", tracing, name); |
| 338 | return file; |
| 339 | } |
| 340 | |
| 341 | void put_tracing_file(char *file) |
| 342 | { |
| 343 | free(file); |
| 344 | } |
Namhyung Kim | 3b47abe | 2013-06-04 10:50:29 +0900 | [diff] [blame] | 345 | |
| 346 | int parse_nsec_time(const char *str, u64 *ptime) |
| 347 | { |
| 348 | u64 time_sec, time_nsec; |
| 349 | char *end; |
| 350 | |
| 351 | time_sec = strtoul(str, &end, 10); |
| 352 | if (*end != '.' && *end != '\0') |
| 353 | return -1; |
| 354 | |
| 355 | if (*end == '.') { |
| 356 | int i; |
| 357 | char nsec_buf[10]; |
| 358 | |
| 359 | if (strlen(++end) > 9) |
| 360 | return -1; |
| 361 | |
| 362 | strncpy(nsec_buf, end, 9); |
| 363 | nsec_buf[9] = '\0'; |
| 364 | |
| 365 | /* make it nsec precision */ |
| 366 | for (i = strlen(nsec_buf); i < 9; i++) |
| 367 | nsec_buf[i] = '0'; |
| 368 | |
| 369 | time_nsec = strtoul(nsec_buf, &end, 10); |
| 370 | if (*end != '\0') |
| 371 | return -1; |
| 372 | } else |
| 373 | time_nsec = 0; |
| 374 | |
| 375 | *ptime = time_sec * NSEC_PER_SEC + time_nsec; |
| 376 | return 0; |
| 377 | } |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 378 | |
| 379 | unsigned long parse_tag_value(const char *str, struct parse_tag *tags) |
| 380 | { |
| 381 | struct parse_tag *i = tags; |
| 382 | |
| 383 | while (i->tag) { |
| 384 | char *s; |
| 385 | |
| 386 | s = strchr(str, i->tag); |
| 387 | if (s) { |
| 388 | unsigned long int value; |
| 389 | char *endptr; |
| 390 | |
| 391 | value = strtoul(str, &endptr, 10); |
| 392 | if (s != endptr) |
| 393 | break; |
| 394 | |
Adrian Hunter | 56921be | 2013-10-22 10:34:17 +0300 | [diff] [blame] | 395 | if (value > ULONG_MAX / i->mult) |
| 396 | break; |
Jiri Olsa | 27050f5 | 2013-09-01 12:36:13 +0200 | [diff] [blame] | 397 | value *= i->mult; |
| 398 | return value; |
| 399 | } |
| 400 | i++; |
| 401 | } |
| 402 | |
| 403 | return (unsigned long) -1; |
| 404 | } |
Arnaldo Carvalho de Melo | 97a07f1 | 2013-10-17 16:33:43 -0300 | [diff] [blame] | 405 | |
| 406 | int filename__read_int(const char *filename, int *value) |
| 407 | { |
| 408 | char line[64]; |
| 409 | int fd = open(filename, O_RDONLY), err = -1; |
| 410 | |
| 411 | if (fd < 0) |
| 412 | return -1; |
| 413 | |
| 414 | if (read(fd, line, sizeof(line)) > 0) { |
| 415 | *value = atoi(line); |
| 416 | err = 0; |
| 417 | } |
| 418 | |
| 419 | close(fd); |
| 420 | return err; |
| 421 | } |