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