Xiao Guangrong | b8f46c5 | 2010-02-03 11:53:14 +0800 | [diff] [blame] | 1 | #define _FILE_OFFSET_BITS 64 |
| 2 | |
Arnaldo Carvalho de Melo | a9072bc | 2011-10-26 12:41:38 -0200 | [diff] [blame] | 3 | #include "util.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 4 | #include <sys/types.h> |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 5 | #include <byteswap.h> |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 6 | #include <unistd.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 9 | #include <linux/list.h> |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 11 | #include <sys/utsname.h> |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 12 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 13 | #include "evlist.h" |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 14 | #include "evsel.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 15 | #include "header.h" |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 16 | #include "../perf.h" |
| 17 | #include "trace-event.h" |
Arnaldo Carvalho de Melo | 301a0b0 | 2009-12-13 19:50:25 -0200 | [diff] [blame] | 18 | #include "session.h" |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 19 | #include "symbol.h" |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 20 | #include "debug.h" |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 21 | #include "cpumap.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 22 | |
Stephane Eranian | a1ac1d3 | 2010-06-17 11:39:01 +0200 | [diff] [blame] | 23 | static bool no_buildid_cache = false; |
| 24 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 25 | static int event_count; |
| 26 | static struct perf_trace_event_type *events; |
| 27 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 28 | static u32 header_argc; |
| 29 | static const char **header_argv; |
| 30 | |
| 31 | static int dsos__write_buildid_table(struct perf_header *header, int fd); |
| 32 | static int perf_session__cache_build_ids(struct perf_session *session); |
| 33 | |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 34 | int perf_header__push_event(u64 id, const char *name) |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 35 | { |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 36 | if (strlen(name) > MAX_EVENT_NAME) |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 37 | pr_warning("Event %s will be truncated\n", name); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 38 | |
| 39 | if (!events) { |
| 40 | events = malloc(sizeof(struct perf_trace_event_type)); |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 41 | if (events == NULL) |
| 42 | return -ENOMEM; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 43 | } else { |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 44 | struct perf_trace_event_type *nevents; |
| 45 | |
| 46 | nevents = realloc(events, (event_count + 1) * sizeof(*events)); |
| 47 | if (nevents == NULL) |
| 48 | return -ENOMEM; |
| 49 | events = nevents; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 50 | } |
| 51 | memset(&events[event_count], 0, sizeof(struct perf_trace_event_type)); |
| 52 | events[event_count].event_id = id; |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 53 | strncpy(events[event_count].name, name, MAX_EVENT_NAME - 1); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 54 | event_count++; |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 55 | return 0; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | char *perf_header__find_event(u64 id) |
| 59 | { |
| 60 | int i; |
| 61 | for (i = 0 ; i < event_count; i++) { |
| 62 | if (events[i].event_id == id) |
| 63 | return events[i].name; |
| 64 | } |
| 65 | return NULL; |
| 66 | } |
| 67 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 68 | static const char *__perf_magic = "PERFFILE"; |
| 69 | |
| 70 | #define PERF_MAGIC (*(u64 *)__perf_magic) |
| 71 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 72 | struct perf_file_attr { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 73 | struct perf_event_attr attr; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 74 | struct perf_file_section ids; |
| 75 | }; |
| 76 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 77 | void perf_header__set_feat(struct perf_header *header, int feat) |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 78 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 79 | set_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 80 | } |
| 81 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 82 | void perf_header__clear_feat(struct perf_header *header, int feat) |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 83 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 84 | clear_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 85 | } |
| 86 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 87 | bool perf_header__has_feat(const struct perf_header *header, int feat) |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 88 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 89 | return test_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 90 | } |
| 91 | |
Arnaldo Carvalho de Melo | 3726cc7 | 2009-11-17 01:18:12 -0200 | [diff] [blame] | 92 | static int do_write(int fd, const void *buf, size_t size) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 93 | { |
| 94 | while (size) { |
| 95 | int ret = write(fd, buf, size); |
| 96 | |
| 97 | if (ret < 0) |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 98 | return -errno; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 99 | |
| 100 | size -= ret; |
| 101 | buf += ret; |
| 102 | } |
Arnaldo Carvalho de Melo | 3726cc7 | 2009-11-17 01:18:12 -0200 | [diff] [blame] | 103 | |
| 104 | return 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Arnaldo Carvalho de Melo | f92cb24 | 2010-01-04 16:19:28 -0200 | [diff] [blame] | 107 | #define NAME_ALIGN 64 |
| 108 | |
| 109 | static int write_padded(int fd, const void *bf, size_t count, |
| 110 | size_t count_aligned) |
| 111 | { |
| 112 | static const char zero_buf[NAME_ALIGN]; |
| 113 | int err = do_write(fd, bf, count); |
| 114 | |
| 115 | if (!err) |
| 116 | err = do_write(fd, zero_buf, count_aligned - count); |
| 117 | |
| 118 | return err; |
| 119 | } |
| 120 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 121 | static int do_write_string(int fd, const char *str) |
| 122 | { |
| 123 | u32 len, olen; |
| 124 | int ret; |
| 125 | |
| 126 | olen = strlen(str) + 1; |
| 127 | len = ALIGN(olen, NAME_ALIGN); |
| 128 | |
| 129 | /* write len, incl. \0 */ |
| 130 | ret = do_write(fd, &len, sizeof(len)); |
| 131 | if (ret < 0) |
| 132 | return ret; |
| 133 | |
| 134 | return write_padded(fd, str, olen, len); |
| 135 | } |
| 136 | |
| 137 | static char *do_read_string(int fd, struct perf_header *ph) |
| 138 | { |
| 139 | ssize_t sz, ret; |
| 140 | u32 len; |
| 141 | char *buf; |
| 142 | |
| 143 | sz = read(fd, &len, sizeof(len)); |
| 144 | if (sz < (ssize_t)sizeof(len)) |
| 145 | return NULL; |
| 146 | |
| 147 | if (ph->needs_swap) |
| 148 | len = bswap_32(len); |
| 149 | |
| 150 | buf = malloc(len); |
| 151 | if (!buf) |
| 152 | return NULL; |
| 153 | |
| 154 | ret = read(fd, buf, len); |
| 155 | if (ret == (ssize_t)len) { |
| 156 | /* |
| 157 | * strings are padded by zeroes |
| 158 | * thus the actual strlen of buf |
| 159 | * may be less than len |
| 160 | */ |
| 161 | return buf; |
| 162 | } |
| 163 | |
| 164 | free(buf); |
| 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | int |
| 169 | perf_header__set_cmdline(int argc, const char **argv) |
| 170 | { |
| 171 | int i; |
| 172 | |
| 173 | header_argc = (u32)argc; |
| 174 | |
| 175 | /* do not include NULL termination */ |
| 176 | header_argv = calloc(argc, sizeof(char *)); |
| 177 | if (!header_argv) |
| 178 | return -ENOMEM; |
| 179 | |
| 180 | /* |
| 181 | * must copy argv contents because it gets moved |
| 182 | * around during option parsing |
| 183 | */ |
| 184 | for (i = 0; i < argc ; i++) |
| 185 | header_argv[i] = argv[i]; |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | static int write_trace_info(int fd, struct perf_header *h __used, |
| 191 | struct perf_evlist *evlist) |
| 192 | { |
| 193 | return read_tracing_data(fd, &evlist->entries); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | static int write_build_id(int fd, struct perf_header *h, |
| 198 | struct perf_evlist *evlist __used) |
| 199 | { |
| 200 | struct perf_session *session; |
| 201 | int err; |
| 202 | |
| 203 | session = container_of(h, struct perf_session, header); |
| 204 | |
| 205 | err = dsos__write_buildid_table(h, fd); |
| 206 | if (err < 0) { |
| 207 | pr_debug("failed to write buildid table\n"); |
| 208 | return err; |
| 209 | } |
| 210 | if (!no_buildid_cache) |
| 211 | perf_session__cache_build_ids(session); |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static int write_hostname(int fd, struct perf_header *h __used, |
| 217 | struct perf_evlist *evlist __used) |
| 218 | { |
| 219 | struct utsname uts; |
| 220 | int ret; |
| 221 | |
| 222 | ret = uname(&uts); |
| 223 | if (ret < 0) |
| 224 | return -1; |
| 225 | |
| 226 | return do_write_string(fd, uts.nodename); |
| 227 | } |
| 228 | |
| 229 | static int write_osrelease(int fd, struct perf_header *h __used, |
| 230 | struct perf_evlist *evlist __used) |
| 231 | { |
| 232 | struct utsname uts; |
| 233 | int ret; |
| 234 | |
| 235 | ret = uname(&uts); |
| 236 | if (ret < 0) |
| 237 | return -1; |
| 238 | |
| 239 | return do_write_string(fd, uts.release); |
| 240 | } |
| 241 | |
| 242 | static int write_arch(int fd, struct perf_header *h __used, |
| 243 | struct perf_evlist *evlist __used) |
| 244 | { |
| 245 | struct utsname uts; |
| 246 | int ret; |
| 247 | |
| 248 | ret = uname(&uts); |
| 249 | if (ret < 0) |
| 250 | return -1; |
| 251 | |
| 252 | return do_write_string(fd, uts.machine); |
| 253 | } |
| 254 | |
| 255 | static int write_version(int fd, struct perf_header *h __used, |
| 256 | struct perf_evlist *evlist __used) |
| 257 | { |
| 258 | return do_write_string(fd, perf_version_string); |
| 259 | } |
| 260 | |
| 261 | static int write_cpudesc(int fd, struct perf_header *h __used, |
| 262 | struct perf_evlist *evlist __used) |
| 263 | { |
| 264 | #ifndef CPUINFO_PROC |
| 265 | #define CPUINFO_PROC NULL |
| 266 | #endif |
| 267 | FILE *file; |
| 268 | char *buf = NULL; |
| 269 | char *s, *p; |
| 270 | const char *search = CPUINFO_PROC; |
| 271 | size_t len = 0; |
| 272 | int ret = -1; |
| 273 | |
| 274 | if (!search) |
| 275 | return -1; |
| 276 | |
| 277 | file = fopen("/proc/cpuinfo", "r"); |
| 278 | if (!file) |
| 279 | return -1; |
| 280 | |
| 281 | while (getline(&buf, &len, file) > 0) { |
| 282 | ret = strncmp(buf, search, strlen(search)); |
| 283 | if (!ret) |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | if (ret) |
| 288 | goto done; |
| 289 | |
| 290 | s = buf; |
| 291 | |
| 292 | p = strchr(buf, ':'); |
| 293 | if (p && *(p+1) == ' ' && *(p+2)) |
| 294 | s = p + 2; |
| 295 | p = strchr(s, '\n'); |
| 296 | if (p) |
| 297 | *p = '\0'; |
| 298 | |
| 299 | /* squash extra space characters (branding string) */ |
| 300 | p = s; |
| 301 | while (*p) { |
| 302 | if (isspace(*p)) { |
| 303 | char *r = p + 1; |
| 304 | char *q = r; |
| 305 | *p = ' '; |
| 306 | while (*q && isspace(*q)) |
| 307 | q++; |
| 308 | if (q != (p+1)) |
| 309 | while ((*r++ = *q++)); |
| 310 | } |
| 311 | p++; |
| 312 | } |
| 313 | ret = do_write_string(fd, s); |
| 314 | done: |
| 315 | free(buf); |
| 316 | fclose(file); |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | static int write_nrcpus(int fd, struct perf_header *h __used, |
| 321 | struct perf_evlist *evlist __used) |
| 322 | { |
| 323 | long nr; |
| 324 | u32 nrc, nra; |
| 325 | int ret; |
| 326 | |
| 327 | nr = sysconf(_SC_NPROCESSORS_CONF); |
| 328 | if (nr < 0) |
| 329 | return -1; |
| 330 | |
| 331 | nrc = (u32)(nr & UINT_MAX); |
| 332 | |
| 333 | nr = sysconf(_SC_NPROCESSORS_ONLN); |
| 334 | if (nr < 0) |
| 335 | return -1; |
| 336 | |
| 337 | nra = (u32)(nr & UINT_MAX); |
| 338 | |
| 339 | ret = do_write(fd, &nrc, sizeof(nrc)); |
| 340 | if (ret < 0) |
| 341 | return ret; |
| 342 | |
| 343 | return do_write(fd, &nra, sizeof(nra)); |
| 344 | } |
| 345 | |
| 346 | static int write_event_desc(int fd, struct perf_header *h __used, |
| 347 | struct perf_evlist *evlist) |
| 348 | { |
| 349 | struct perf_evsel *attr; |
| 350 | u32 nre = 0, nri, sz; |
| 351 | int ret; |
| 352 | |
| 353 | list_for_each_entry(attr, &evlist->entries, node) |
| 354 | nre++; |
| 355 | |
| 356 | /* |
| 357 | * write number of events |
| 358 | */ |
| 359 | ret = do_write(fd, &nre, sizeof(nre)); |
| 360 | if (ret < 0) |
| 361 | return ret; |
| 362 | |
| 363 | /* |
| 364 | * size of perf_event_attr struct |
| 365 | */ |
| 366 | sz = (u32)sizeof(attr->attr); |
| 367 | ret = do_write(fd, &sz, sizeof(sz)); |
| 368 | if (ret < 0) |
| 369 | return ret; |
| 370 | |
| 371 | list_for_each_entry(attr, &evlist->entries, node) { |
| 372 | |
| 373 | ret = do_write(fd, &attr->attr, sz); |
| 374 | if (ret < 0) |
| 375 | return ret; |
| 376 | /* |
| 377 | * write number of unique id per event |
| 378 | * there is one id per instance of an event |
| 379 | * |
| 380 | * copy into an nri to be independent of the |
| 381 | * type of ids, |
| 382 | */ |
| 383 | nri = attr->ids; |
| 384 | ret = do_write(fd, &nri, sizeof(nri)); |
| 385 | if (ret < 0) |
| 386 | return ret; |
| 387 | |
| 388 | /* |
| 389 | * write event string as passed on cmdline |
| 390 | */ |
Andrew Vagin | 6340cfe | 2011-11-28 12:03:29 +0300 | [diff] [blame] | 391 | ret = do_write_string(fd, event_name(attr)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 392 | if (ret < 0) |
| 393 | return ret; |
| 394 | /* |
| 395 | * write unique ids for this event |
| 396 | */ |
| 397 | ret = do_write(fd, attr->id, attr->ids * sizeof(u64)); |
| 398 | if (ret < 0) |
| 399 | return ret; |
| 400 | } |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static int write_cmdline(int fd, struct perf_header *h __used, |
| 405 | struct perf_evlist *evlist __used) |
| 406 | { |
| 407 | char buf[MAXPATHLEN]; |
| 408 | char proc[32]; |
| 409 | u32 i, n; |
| 410 | int ret; |
| 411 | |
| 412 | /* |
| 413 | * actual atual path to perf binary |
| 414 | */ |
| 415 | sprintf(proc, "/proc/%d/exe", getpid()); |
| 416 | ret = readlink(proc, buf, sizeof(buf)); |
| 417 | if (ret <= 0) |
| 418 | return -1; |
| 419 | |
| 420 | /* readlink() does not add null termination */ |
| 421 | buf[ret] = '\0'; |
| 422 | |
| 423 | /* account for binary path */ |
| 424 | n = header_argc + 1; |
| 425 | |
| 426 | ret = do_write(fd, &n, sizeof(n)); |
| 427 | if (ret < 0) |
| 428 | return ret; |
| 429 | |
| 430 | ret = do_write_string(fd, buf); |
| 431 | if (ret < 0) |
| 432 | return ret; |
| 433 | |
| 434 | for (i = 0 ; i < header_argc; i++) { |
| 435 | ret = do_write_string(fd, header_argv[i]); |
| 436 | if (ret < 0) |
| 437 | return ret; |
| 438 | } |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | #define CORE_SIB_FMT \ |
| 443 | "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list" |
| 444 | #define THRD_SIB_FMT \ |
| 445 | "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list" |
| 446 | |
| 447 | struct cpu_topo { |
| 448 | u32 core_sib; |
| 449 | u32 thread_sib; |
| 450 | char **core_siblings; |
| 451 | char **thread_siblings; |
| 452 | }; |
| 453 | |
| 454 | static int build_cpu_topo(struct cpu_topo *tp, int cpu) |
| 455 | { |
| 456 | FILE *fp; |
| 457 | char filename[MAXPATHLEN]; |
| 458 | char *buf = NULL, *p; |
| 459 | size_t len = 0; |
| 460 | u32 i = 0; |
| 461 | int ret = -1; |
| 462 | |
| 463 | sprintf(filename, CORE_SIB_FMT, cpu); |
| 464 | fp = fopen(filename, "r"); |
| 465 | if (!fp) |
| 466 | return -1; |
| 467 | |
| 468 | if (getline(&buf, &len, fp) <= 0) |
| 469 | goto done; |
| 470 | |
| 471 | fclose(fp); |
| 472 | |
| 473 | p = strchr(buf, '\n'); |
| 474 | if (p) |
| 475 | *p = '\0'; |
| 476 | |
| 477 | for (i = 0; i < tp->core_sib; i++) { |
| 478 | if (!strcmp(buf, tp->core_siblings[i])) |
| 479 | break; |
| 480 | } |
| 481 | if (i == tp->core_sib) { |
| 482 | tp->core_siblings[i] = buf; |
| 483 | tp->core_sib++; |
| 484 | buf = NULL; |
| 485 | len = 0; |
| 486 | } |
| 487 | |
| 488 | sprintf(filename, THRD_SIB_FMT, cpu); |
| 489 | fp = fopen(filename, "r"); |
| 490 | if (!fp) |
| 491 | goto done; |
| 492 | |
| 493 | if (getline(&buf, &len, fp) <= 0) |
| 494 | goto done; |
| 495 | |
| 496 | p = strchr(buf, '\n'); |
| 497 | if (p) |
| 498 | *p = '\0'; |
| 499 | |
| 500 | for (i = 0; i < tp->thread_sib; i++) { |
| 501 | if (!strcmp(buf, tp->thread_siblings[i])) |
| 502 | break; |
| 503 | } |
| 504 | if (i == tp->thread_sib) { |
| 505 | tp->thread_siblings[i] = buf; |
| 506 | tp->thread_sib++; |
| 507 | buf = NULL; |
| 508 | } |
| 509 | ret = 0; |
| 510 | done: |
| 511 | if(fp) |
| 512 | fclose(fp); |
| 513 | free(buf); |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | static void free_cpu_topo(struct cpu_topo *tp) |
| 518 | { |
| 519 | u32 i; |
| 520 | |
| 521 | if (!tp) |
| 522 | return; |
| 523 | |
| 524 | for (i = 0 ; i < tp->core_sib; i++) |
| 525 | free(tp->core_siblings[i]); |
| 526 | |
| 527 | for (i = 0 ; i < tp->thread_sib; i++) |
| 528 | free(tp->thread_siblings[i]); |
| 529 | |
| 530 | free(tp); |
| 531 | } |
| 532 | |
| 533 | static struct cpu_topo *build_cpu_topology(void) |
| 534 | { |
| 535 | struct cpu_topo *tp; |
| 536 | void *addr; |
| 537 | u32 nr, i; |
| 538 | size_t sz; |
| 539 | long ncpus; |
| 540 | int ret = -1; |
| 541 | |
| 542 | ncpus = sysconf(_SC_NPROCESSORS_CONF); |
| 543 | if (ncpus < 0) |
| 544 | return NULL; |
| 545 | |
| 546 | nr = (u32)(ncpus & UINT_MAX); |
| 547 | |
| 548 | sz = nr * sizeof(char *); |
| 549 | |
| 550 | addr = calloc(1, sizeof(*tp) + 2 * sz); |
| 551 | if (!addr) |
| 552 | return NULL; |
| 553 | |
| 554 | tp = addr; |
| 555 | |
| 556 | addr += sizeof(*tp); |
| 557 | tp->core_siblings = addr; |
| 558 | addr += sz; |
| 559 | tp->thread_siblings = addr; |
| 560 | |
| 561 | for (i = 0; i < nr; i++) { |
| 562 | ret = build_cpu_topo(tp, i); |
| 563 | if (ret < 0) |
| 564 | break; |
| 565 | } |
| 566 | if (ret) { |
| 567 | free_cpu_topo(tp); |
| 568 | tp = NULL; |
| 569 | } |
| 570 | return tp; |
| 571 | } |
| 572 | |
| 573 | static int write_cpu_topology(int fd, struct perf_header *h __used, |
| 574 | struct perf_evlist *evlist __used) |
| 575 | { |
| 576 | struct cpu_topo *tp; |
| 577 | u32 i; |
| 578 | int ret; |
| 579 | |
| 580 | tp = build_cpu_topology(); |
| 581 | if (!tp) |
| 582 | return -1; |
| 583 | |
| 584 | ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib)); |
| 585 | if (ret < 0) |
| 586 | goto done; |
| 587 | |
| 588 | for (i = 0; i < tp->core_sib; i++) { |
| 589 | ret = do_write_string(fd, tp->core_siblings[i]); |
| 590 | if (ret < 0) |
| 591 | goto done; |
| 592 | } |
| 593 | ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib)); |
| 594 | if (ret < 0) |
| 595 | goto done; |
| 596 | |
| 597 | for (i = 0; i < tp->thread_sib; i++) { |
| 598 | ret = do_write_string(fd, tp->thread_siblings[i]); |
| 599 | if (ret < 0) |
| 600 | break; |
| 601 | } |
| 602 | done: |
| 603 | free_cpu_topo(tp); |
| 604 | return ret; |
| 605 | } |
| 606 | |
| 607 | |
| 608 | |
| 609 | static int write_total_mem(int fd, struct perf_header *h __used, |
| 610 | struct perf_evlist *evlist __used) |
| 611 | { |
| 612 | char *buf = NULL; |
| 613 | FILE *fp; |
| 614 | size_t len = 0; |
| 615 | int ret = -1, n; |
| 616 | uint64_t mem; |
| 617 | |
| 618 | fp = fopen("/proc/meminfo", "r"); |
| 619 | if (!fp) |
| 620 | return -1; |
| 621 | |
| 622 | while (getline(&buf, &len, fp) > 0) { |
| 623 | ret = strncmp(buf, "MemTotal:", 9); |
| 624 | if (!ret) |
| 625 | break; |
| 626 | } |
| 627 | if (!ret) { |
| 628 | n = sscanf(buf, "%*s %"PRIu64, &mem); |
| 629 | if (n == 1) |
| 630 | ret = do_write(fd, &mem, sizeof(mem)); |
| 631 | } |
| 632 | free(buf); |
| 633 | fclose(fp); |
| 634 | return ret; |
| 635 | } |
| 636 | |
| 637 | static int write_topo_node(int fd, int node) |
| 638 | { |
| 639 | char str[MAXPATHLEN]; |
| 640 | char field[32]; |
| 641 | char *buf = NULL, *p; |
| 642 | size_t len = 0; |
| 643 | FILE *fp; |
| 644 | u64 mem_total, mem_free, mem; |
| 645 | int ret = -1; |
| 646 | |
| 647 | sprintf(str, "/sys/devices/system/node/node%d/meminfo", node); |
| 648 | fp = fopen(str, "r"); |
| 649 | if (!fp) |
| 650 | return -1; |
| 651 | |
| 652 | while (getline(&buf, &len, fp) > 0) { |
| 653 | /* skip over invalid lines */ |
| 654 | if (!strchr(buf, ':')) |
| 655 | continue; |
| 656 | if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2) |
| 657 | goto done; |
| 658 | if (!strcmp(field, "MemTotal:")) |
| 659 | mem_total = mem; |
| 660 | if (!strcmp(field, "MemFree:")) |
| 661 | mem_free = mem; |
| 662 | } |
| 663 | |
| 664 | fclose(fp); |
| 665 | |
| 666 | ret = do_write(fd, &mem_total, sizeof(u64)); |
| 667 | if (ret) |
| 668 | goto done; |
| 669 | |
| 670 | ret = do_write(fd, &mem_free, sizeof(u64)); |
| 671 | if (ret) |
| 672 | goto done; |
| 673 | |
| 674 | ret = -1; |
| 675 | sprintf(str, "/sys/devices/system/node/node%d/cpulist", node); |
| 676 | |
| 677 | fp = fopen(str, "r"); |
| 678 | if (!fp) |
| 679 | goto done; |
| 680 | |
| 681 | if (getline(&buf, &len, fp) <= 0) |
| 682 | goto done; |
| 683 | |
| 684 | p = strchr(buf, '\n'); |
| 685 | if (p) |
| 686 | *p = '\0'; |
| 687 | |
| 688 | ret = do_write_string(fd, buf); |
| 689 | done: |
| 690 | free(buf); |
| 691 | fclose(fp); |
| 692 | return ret; |
| 693 | } |
| 694 | |
| 695 | static int write_numa_topology(int fd, struct perf_header *h __used, |
| 696 | struct perf_evlist *evlist __used) |
| 697 | { |
| 698 | char *buf = NULL; |
| 699 | size_t len = 0; |
| 700 | FILE *fp; |
| 701 | struct cpu_map *node_map = NULL; |
| 702 | char *c; |
| 703 | u32 nr, i, j; |
| 704 | int ret = -1; |
| 705 | |
| 706 | fp = fopen("/sys/devices/system/node/online", "r"); |
| 707 | if (!fp) |
| 708 | return -1; |
| 709 | |
| 710 | if (getline(&buf, &len, fp) <= 0) |
| 711 | goto done; |
| 712 | |
| 713 | c = strchr(buf, '\n'); |
| 714 | if (c) |
| 715 | *c = '\0'; |
| 716 | |
| 717 | node_map = cpu_map__new(buf); |
| 718 | if (!node_map) |
| 719 | goto done; |
| 720 | |
| 721 | nr = (u32)node_map->nr; |
| 722 | |
| 723 | ret = do_write(fd, &nr, sizeof(nr)); |
| 724 | if (ret < 0) |
| 725 | goto done; |
| 726 | |
| 727 | for (i = 0; i < nr; i++) { |
| 728 | j = (u32)node_map->map[i]; |
| 729 | ret = do_write(fd, &j, sizeof(j)); |
| 730 | if (ret < 0) |
| 731 | break; |
| 732 | |
| 733 | ret = write_topo_node(fd, i); |
| 734 | if (ret < 0) |
| 735 | break; |
| 736 | } |
| 737 | done: |
| 738 | free(buf); |
| 739 | fclose(fp); |
| 740 | free(node_map); |
| 741 | return ret; |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | * default get_cpuid(): nothing gets recorded |
| 746 | * actual implementation must be in arch/$(ARCH)/util/header.c |
| 747 | */ |
| 748 | int __attribute__((weak)) get_cpuid(char *buffer __used, size_t sz __used) |
| 749 | { |
| 750 | return -1; |
| 751 | } |
| 752 | |
| 753 | static int write_cpuid(int fd, struct perf_header *h __used, |
| 754 | struct perf_evlist *evlist __used) |
| 755 | { |
| 756 | char buffer[64]; |
| 757 | int ret; |
| 758 | |
| 759 | ret = get_cpuid(buffer, sizeof(buffer)); |
| 760 | if (!ret) |
| 761 | goto write_it; |
| 762 | |
| 763 | return -1; |
| 764 | write_it: |
| 765 | return do_write_string(fd, buffer); |
| 766 | } |
| 767 | |
| 768 | static void print_hostname(struct perf_header *ph, int fd, FILE *fp) |
| 769 | { |
| 770 | char *str = do_read_string(fd, ph); |
| 771 | fprintf(fp, "# hostname : %s\n", str); |
| 772 | free(str); |
| 773 | } |
| 774 | |
| 775 | static void print_osrelease(struct perf_header *ph, int fd, FILE *fp) |
| 776 | { |
| 777 | char *str = do_read_string(fd, ph); |
| 778 | fprintf(fp, "# os release : %s\n", str); |
| 779 | free(str); |
| 780 | } |
| 781 | |
| 782 | static void print_arch(struct perf_header *ph, int fd, FILE *fp) |
| 783 | { |
| 784 | char *str = do_read_string(fd, ph); |
| 785 | fprintf(fp, "# arch : %s\n", str); |
| 786 | free(str); |
| 787 | } |
| 788 | |
| 789 | static void print_cpudesc(struct perf_header *ph, int fd, FILE *fp) |
| 790 | { |
| 791 | char *str = do_read_string(fd, ph); |
| 792 | fprintf(fp, "# cpudesc : %s\n", str); |
| 793 | free(str); |
| 794 | } |
| 795 | |
| 796 | static void print_nrcpus(struct perf_header *ph, int fd, FILE *fp) |
| 797 | { |
| 798 | ssize_t ret; |
| 799 | u32 nr; |
| 800 | |
| 801 | ret = read(fd, &nr, sizeof(nr)); |
| 802 | if (ret != (ssize_t)sizeof(nr)) |
| 803 | nr = -1; /* interpreted as error */ |
| 804 | |
| 805 | if (ph->needs_swap) |
| 806 | nr = bswap_32(nr); |
| 807 | |
| 808 | fprintf(fp, "# nrcpus online : %u\n", nr); |
| 809 | |
| 810 | ret = read(fd, &nr, sizeof(nr)); |
| 811 | if (ret != (ssize_t)sizeof(nr)) |
| 812 | nr = -1; /* interpreted as error */ |
| 813 | |
| 814 | if (ph->needs_swap) |
| 815 | nr = bswap_32(nr); |
| 816 | |
| 817 | fprintf(fp, "# nrcpus avail : %u\n", nr); |
| 818 | } |
| 819 | |
| 820 | static void print_version(struct perf_header *ph, int fd, FILE *fp) |
| 821 | { |
| 822 | char *str = do_read_string(fd, ph); |
| 823 | fprintf(fp, "# perf version : %s\n", str); |
| 824 | free(str); |
| 825 | } |
| 826 | |
| 827 | static void print_cmdline(struct perf_header *ph, int fd, FILE *fp) |
| 828 | { |
| 829 | ssize_t ret; |
| 830 | char *str; |
| 831 | u32 nr, i; |
| 832 | |
| 833 | ret = read(fd, &nr, sizeof(nr)); |
| 834 | if (ret != (ssize_t)sizeof(nr)) |
| 835 | return; |
| 836 | |
| 837 | if (ph->needs_swap) |
| 838 | nr = bswap_32(nr); |
| 839 | |
| 840 | fprintf(fp, "# cmdline : "); |
| 841 | |
| 842 | for (i = 0; i < nr; i++) { |
| 843 | str = do_read_string(fd, ph); |
| 844 | fprintf(fp, "%s ", str); |
| 845 | free(str); |
| 846 | } |
| 847 | fputc('\n', fp); |
| 848 | } |
| 849 | |
| 850 | static void print_cpu_topology(struct perf_header *ph, int fd, FILE *fp) |
| 851 | { |
| 852 | ssize_t ret; |
| 853 | u32 nr, i; |
| 854 | char *str; |
| 855 | |
| 856 | ret = read(fd, &nr, sizeof(nr)); |
| 857 | if (ret != (ssize_t)sizeof(nr)) |
| 858 | return; |
| 859 | |
| 860 | if (ph->needs_swap) |
| 861 | nr = bswap_32(nr); |
| 862 | |
| 863 | for (i = 0; i < nr; i++) { |
| 864 | str = do_read_string(fd, ph); |
| 865 | fprintf(fp, "# sibling cores : %s\n", str); |
| 866 | free(str); |
| 867 | } |
| 868 | |
| 869 | ret = read(fd, &nr, sizeof(nr)); |
| 870 | if (ret != (ssize_t)sizeof(nr)) |
| 871 | return; |
| 872 | |
| 873 | if (ph->needs_swap) |
| 874 | nr = bswap_32(nr); |
| 875 | |
| 876 | for (i = 0; i < nr; i++) { |
| 877 | str = do_read_string(fd, ph); |
| 878 | fprintf(fp, "# sibling threads : %s\n", str); |
| 879 | free(str); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | static void print_event_desc(struct perf_header *ph, int fd, FILE *fp) |
| 884 | { |
| 885 | struct perf_event_attr attr; |
| 886 | uint64_t id; |
| 887 | void *buf = NULL; |
| 888 | char *str; |
| 889 | u32 nre, sz, nr, i, j, msz; |
| 890 | int ret; |
| 891 | |
| 892 | /* number of events */ |
| 893 | ret = read(fd, &nre, sizeof(nre)); |
| 894 | if (ret != (ssize_t)sizeof(nre)) |
| 895 | goto error; |
| 896 | |
| 897 | if (ph->needs_swap) |
| 898 | nre = bswap_32(nre); |
| 899 | |
| 900 | ret = read(fd, &sz, sizeof(sz)); |
| 901 | if (ret != (ssize_t)sizeof(sz)) |
| 902 | goto error; |
| 903 | |
| 904 | if (ph->needs_swap) |
| 905 | sz = bswap_32(sz); |
| 906 | |
| 907 | /* |
| 908 | * ensure it is at least to our ABI rev |
| 909 | */ |
| 910 | if (sz < (u32)sizeof(attr)) |
| 911 | goto error; |
| 912 | |
| 913 | memset(&attr, 0, sizeof(attr)); |
| 914 | |
| 915 | /* read entire region to sync up to next field */ |
| 916 | buf = malloc(sz); |
| 917 | if (!buf) |
| 918 | goto error; |
| 919 | |
| 920 | msz = sizeof(attr); |
| 921 | if (sz < msz) |
| 922 | msz = sz; |
| 923 | |
| 924 | for (i = 0 ; i < nre; i++) { |
| 925 | |
| 926 | ret = read(fd, buf, sz); |
| 927 | if (ret != (ssize_t)sz) |
| 928 | goto error; |
| 929 | |
| 930 | if (ph->needs_swap) |
| 931 | perf_event__attr_swap(buf); |
| 932 | |
| 933 | memcpy(&attr, buf, msz); |
| 934 | |
| 935 | ret = read(fd, &nr, sizeof(nr)); |
| 936 | if (ret != (ssize_t)sizeof(nr)) |
| 937 | goto error; |
| 938 | |
| 939 | if (ph->needs_swap) |
| 940 | nr = bswap_32(nr); |
| 941 | |
| 942 | str = do_read_string(fd, ph); |
| 943 | fprintf(fp, "# event : name = %s, ", str); |
| 944 | free(str); |
| 945 | |
| 946 | fprintf(fp, "type = %d, config = 0x%"PRIx64 |
| 947 | ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64, |
| 948 | attr.type, |
| 949 | (u64)attr.config, |
| 950 | (u64)attr.config1, |
| 951 | (u64)attr.config2); |
| 952 | |
| 953 | fprintf(fp, ", excl_usr = %d, excl_kern = %d", |
| 954 | attr.exclude_user, |
| 955 | attr.exclude_kernel); |
| 956 | |
| 957 | if (nr) |
| 958 | fprintf(fp, ", id = {"); |
| 959 | |
| 960 | for (j = 0 ; j < nr; j++) { |
| 961 | ret = read(fd, &id, sizeof(id)); |
| 962 | if (ret != (ssize_t)sizeof(id)) |
| 963 | goto error; |
| 964 | |
| 965 | if (ph->needs_swap) |
| 966 | id = bswap_64(id); |
| 967 | |
| 968 | if (j) |
| 969 | fputc(',', fp); |
| 970 | |
| 971 | fprintf(fp, " %"PRIu64, id); |
| 972 | } |
| 973 | if (nr && j == nr) |
| 974 | fprintf(fp, " }"); |
| 975 | fputc('\n', fp); |
| 976 | } |
| 977 | free(buf); |
| 978 | return; |
| 979 | error: |
| 980 | fprintf(fp, "# event desc: not available or unable to read\n"); |
| 981 | } |
| 982 | |
| 983 | static void print_total_mem(struct perf_header *h __used, int fd, FILE *fp) |
| 984 | { |
| 985 | uint64_t mem; |
| 986 | ssize_t ret; |
| 987 | |
| 988 | ret = read(fd, &mem, sizeof(mem)); |
| 989 | if (ret != sizeof(mem)) |
| 990 | goto error; |
| 991 | |
| 992 | if (h->needs_swap) |
| 993 | mem = bswap_64(mem); |
| 994 | |
| 995 | fprintf(fp, "# total memory : %"PRIu64" kB\n", mem); |
| 996 | return; |
| 997 | error: |
| 998 | fprintf(fp, "# total memory : unknown\n"); |
| 999 | } |
| 1000 | |
| 1001 | static void print_numa_topology(struct perf_header *h __used, int fd, FILE *fp) |
| 1002 | { |
| 1003 | ssize_t ret; |
| 1004 | u32 nr, c, i; |
| 1005 | char *str; |
| 1006 | uint64_t mem_total, mem_free; |
| 1007 | |
| 1008 | /* nr nodes */ |
| 1009 | ret = read(fd, &nr, sizeof(nr)); |
| 1010 | if (ret != (ssize_t)sizeof(nr)) |
| 1011 | goto error; |
| 1012 | |
| 1013 | if (h->needs_swap) |
| 1014 | nr = bswap_32(nr); |
| 1015 | |
| 1016 | for (i = 0; i < nr; i++) { |
| 1017 | |
| 1018 | /* node number */ |
| 1019 | ret = read(fd, &c, sizeof(c)); |
| 1020 | if (ret != (ssize_t)sizeof(c)) |
| 1021 | goto error; |
| 1022 | |
| 1023 | if (h->needs_swap) |
| 1024 | c = bswap_32(c); |
| 1025 | |
| 1026 | ret = read(fd, &mem_total, sizeof(u64)); |
| 1027 | if (ret != sizeof(u64)) |
| 1028 | goto error; |
| 1029 | |
| 1030 | ret = read(fd, &mem_free, sizeof(u64)); |
| 1031 | if (ret != sizeof(u64)) |
| 1032 | goto error; |
| 1033 | |
| 1034 | if (h->needs_swap) { |
| 1035 | mem_total = bswap_64(mem_total); |
| 1036 | mem_free = bswap_64(mem_free); |
| 1037 | } |
| 1038 | |
| 1039 | fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB," |
| 1040 | " free = %"PRIu64" kB\n", |
| 1041 | c, |
| 1042 | mem_total, |
| 1043 | mem_free); |
| 1044 | |
| 1045 | str = do_read_string(fd, h); |
| 1046 | fprintf(fp, "# node%u cpu list : %s\n", c, str); |
| 1047 | free(str); |
| 1048 | } |
| 1049 | return; |
| 1050 | error: |
| 1051 | fprintf(fp, "# numa topology : not available\n"); |
| 1052 | } |
| 1053 | |
| 1054 | static void print_cpuid(struct perf_header *ph, int fd, FILE *fp) |
| 1055 | { |
| 1056 | char *str = do_read_string(fd, ph); |
| 1057 | fprintf(fp, "# cpuid : %s\n", str); |
| 1058 | free(str); |
| 1059 | } |
| 1060 | |
| 1061 | struct feature_ops { |
| 1062 | int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist); |
| 1063 | void (*print)(struct perf_header *h, int fd, FILE *fp); |
| 1064 | const char *name; |
| 1065 | bool full_only; |
| 1066 | }; |
| 1067 | |
Robert Richter | 8cdfa78 | 2011-12-07 10:02:56 +0100 | [diff] [blame] | 1068 | #define FEAT_OPA(n, func) \ |
| 1069 | [n] = { .name = #n, .write = write_##func, .print = print_##func } |
| 1070 | #define FEAT_OPF(n, func) \ |
| 1071 | [n] = { .name = #n, .write = write_##func, .print = print_##func, .full_only = true } |
| 1072 | |
| 1073 | /* feature_ops not implemented: */ |
| 1074 | #define print_trace_info NULL |
| 1075 | #define print_build_id NULL |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1076 | |
| 1077 | static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = { |
Robert Richter | 8cdfa78 | 2011-12-07 10:02:56 +0100 | [diff] [blame] | 1078 | FEAT_OPA(HEADER_TRACE_INFO, trace_info), |
| 1079 | FEAT_OPA(HEADER_BUILD_ID, build_id), |
| 1080 | FEAT_OPA(HEADER_HOSTNAME, hostname), |
| 1081 | FEAT_OPA(HEADER_OSRELEASE, osrelease), |
| 1082 | FEAT_OPA(HEADER_VERSION, version), |
| 1083 | FEAT_OPA(HEADER_ARCH, arch), |
| 1084 | FEAT_OPA(HEADER_NRCPUS, nrcpus), |
| 1085 | FEAT_OPA(HEADER_CPUDESC, cpudesc), |
| 1086 | FEAT_OPA(HEADER_CPUID, cpuid), |
| 1087 | FEAT_OPA(HEADER_TOTAL_MEM, total_mem), |
| 1088 | FEAT_OPA(HEADER_EVENT_DESC, event_desc), |
| 1089 | FEAT_OPA(HEADER_CMDLINE, cmdline), |
| 1090 | FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology), |
| 1091 | FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology), |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1092 | }; |
| 1093 | |
| 1094 | struct header_print_data { |
| 1095 | FILE *fp; |
| 1096 | bool full; /* extended list of headers */ |
| 1097 | }; |
| 1098 | |
| 1099 | static int perf_file_section__fprintf_info(struct perf_file_section *section, |
| 1100 | struct perf_header *ph, |
| 1101 | int feat, int fd, void *data) |
| 1102 | { |
| 1103 | struct header_print_data *hd = data; |
| 1104 | |
| 1105 | if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { |
| 1106 | pr_debug("Failed to lseek to %" PRIu64 " offset for feature " |
| 1107 | "%d, continuing...\n", section->offset, feat); |
| 1108 | return 0; |
| 1109 | } |
| 1110 | if (feat < HEADER_TRACE_INFO || feat >= HEADER_LAST_FEATURE) { |
| 1111 | pr_warning("unknown feature %d\n", feat); |
Robert Richter | f7a8a13 | 2011-12-07 10:02:51 +0100 | [diff] [blame^] | 1112 | return 0; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1113 | } |
| 1114 | if (!feat_ops[feat].print) |
| 1115 | return 0; |
| 1116 | |
| 1117 | if (!feat_ops[feat].full_only || hd->full) |
| 1118 | feat_ops[feat].print(ph, fd, hd->fp); |
| 1119 | else |
| 1120 | fprintf(hd->fp, "# %s info available, use -I to display\n", |
| 1121 | feat_ops[feat].name); |
| 1122 | |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
| 1126 | int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full) |
| 1127 | { |
| 1128 | struct header_print_data hd; |
| 1129 | struct perf_header *header = &session->header; |
| 1130 | int fd = session->fd; |
| 1131 | hd.fp = fp; |
| 1132 | hd.full = full; |
| 1133 | |
| 1134 | perf_header__process_sections(header, fd, &hd, |
| 1135 | perf_file_section__fprintf_info); |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1139 | #define dsos__for_each_with_build_id(pos, head) \ |
| 1140 | list_for_each_entry(pos, head, node) \ |
| 1141 | if (!pos->has_build_id) \ |
| 1142 | continue; \ |
| 1143 | else |
| 1144 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1145 | static int __dsos__write_buildid_table(struct list_head *head, pid_t pid, |
| 1146 | u16 misc, int fd) |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 1147 | { |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 1148 | struct dso *pos; |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 1149 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1150 | dsos__for_each_with_build_id(pos, head) { |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1151 | int err; |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 1152 | struct build_id_event b; |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 1153 | size_t len; |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 1154 | |
Arnaldo Carvalho de Melo | 6122e4e | 2010-02-03 16:52:05 -0200 | [diff] [blame] | 1155 | if (!pos->hit) |
| 1156 | continue; |
| 1157 | len = pos->long_name_len + 1; |
OGAWA Hirofumi | 7691b1e | 2009-12-06 20:10:49 +0900 | [diff] [blame] | 1158 | len = ALIGN(len, NAME_ALIGN); |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 1159 | memset(&b, 0, sizeof(b)); |
| 1160 | memcpy(&b.build_id, pos->build_id, sizeof(pos->build_id)); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1161 | b.pid = pid; |
Arnaldo Carvalho de Melo | a89e5abe | 2010-01-07 19:59:39 -0200 | [diff] [blame] | 1162 | b.header.misc = misc; |
Arnaldo Carvalho de Melo | e30a3d1 | 2009-11-18 20:20:51 -0200 | [diff] [blame] | 1163 | b.header.size = sizeof(b) + len; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1164 | err = do_write(fd, &b, sizeof(b)); |
| 1165 | if (err < 0) |
| 1166 | return err; |
Arnaldo Carvalho de Melo | f92cb24 | 2010-01-04 16:19:28 -0200 | [diff] [blame] | 1167 | err = write_padded(fd, pos->long_name, |
| 1168 | pos->long_name_len + 1, len); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1169 | if (err < 0) |
| 1170 | return err; |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 1171 | } |
Arnaldo Carvalho de Melo | 3726cc7 | 2009-11-17 01:18:12 -0200 | [diff] [blame] | 1172 | |
| 1173 | return 0; |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 1174 | } |
| 1175 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1176 | static int machine__write_buildid_table(struct machine *machine, int fd) |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1177 | { |
| 1178 | int err; |
| 1179 | u16 kmisc = PERF_RECORD_MISC_KERNEL, |
| 1180 | umisc = PERF_RECORD_MISC_USER; |
| 1181 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1182 | if (!machine__is_host(machine)) { |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1183 | kmisc = PERF_RECORD_MISC_GUEST_KERNEL; |
| 1184 | umisc = PERF_RECORD_MISC_GUEST_USER; |
| 1185 | } |
| 1186 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1187 | err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid, |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1188 | kmisc, fd); |
| 1189 | if (err == 0) |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1190 | err = __dsos__write_buildid_table(&machine->user_dsos, |
| 1191 | machine->pid, umisc, fd); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1192 | return err; |
| 1193 | } |
| 1194 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1195 | static int dsos__write_buildid_table(struct perf_header *header, int fd) |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1196 | { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1197 | struct perf_session *session = container_of(header, |
| 1198 | struct perf_session, header); |
| 1199 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1200 | int err = machine__write_buildid_table(&session->host_machine, fd); |
| 1201 | |
| 1202 | if (err) |
| 1203 | return err; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1204 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1205 | for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { |
| 1206 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1207 | err = machine__write_buildid_table(pos, fd); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1208 | if (err) |
| 1209 | break; |
| 1210 | } |
Arnaldo Carvalho de Melo | b0da954 | 2009-11-27 16:29:14 -0200 | [diff] [blame] | 1211 | return err; |
| 1212 | } |
| 1213 | |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 1214 | int build_id_cache__add_s(const char *sbuild_id, const char *debugdir, |
| 1215 | const char *name, bool is_kallsyms) |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1216 | { |
| 1217 | const size_t size = PATH_MAX; |
Han Pingtian | 4f9bae3 | 2011-07-18 11:13:14 +0800 | [diff] [blame] | 1218 | char *realname, *filename = zalloc(size), |
| 1219 | *linkname = zalloc(size), *targetname; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1220 | int len, err = -1; |
| 1221 | |
Arnaldo Carvalho de Melo | ec80fde | 2011-05-26 09:53:51 -0300 | [diff] [blame] | 1222 | if (is_kallsyms) { |
| 1223 | if (symbol_conf.kptr_restrict) { |
| 1224 | pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n"); |
| 1225 | return 0; |
| 1226 | } |
Lin Ming | 7ee235e | 2011-03-03 23:23:57 +0800 | [diff] [blame] | 1227 | realname = (char *)name; |
Arnaldo Carvalho de Melo | ec80fde | 2011-05-26 09:53:51 -0300 | [diff] [blame] | 1228 | } else |
Lin Ming | 7ee235e | 2011-03-03 23:23:57 +0800 | [diff] [blame] | 1229 | realname = realpath(name, NULL); |
| 1230 | |
Franck Bui-Huu | 68a7a77 | 2010-12-10 22:06:26 +0100 | [diff] [blame] | 1231 | if (realname == NULL || filename == NULL || linkname == NULL) |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1232 | goto out_free; |
| 1233 | |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 1234 | len = snprintf(filename, size, "%s%s%s", |
Franck Bui-Huu | 68a7a77 | 2010-12-10 22:06:26 +0100 | [diff] [blame] | 1235 | debugdir, is_kallsyms ? "/" : "", realname); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1236 | if (mkdir_p(filename, 0755)) |
| 1237 | goto out_free; |
| 1238 | |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 1239 | snprintf(filename + len, sizeof(filename) - len, "/%s", sbuild_id); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1240 | |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 1241 | if (access(filename, F_OK)) { |
| 1242 | if (is_kallsyms) { |
| 1243 | if (copyfile("/proc/kallsyms", filename)) |
| 1244 | goto out_free; |
Franck Bui-Huu | 68a7a77 | 2010-12-10 22:06:26 +0100 | [diff] [blame] | 1245 | } else if (link(realname, filename) && copyfile(name, filename)) |
Arnaldo Carvalho de Melo | 9e20144 | 2010-01-14 18:30:06 -0200 | [diff] [blame] | 1246 | goto out_free; |
| 1247 | } |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1248 | |
| 1249 | len = snprintf(linkname, size, "%s/.build-id/%.2s", |
| 1250 | debugdir, sbuild_id); |
| 1251 | |
| 1252 | if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) |
| 1253 | goto out_free; |
| 1254 | |
| 1255 | snprintf(linkname + len, size - len, "/%s", sbuild_id + 2); |
| 1256 | targetname = filename + strlen(debugdir) - 5; |
| 1257 | memcpy(targetname, "../..", 5); |
| 1258 | |
| 1259 | if (symlink(targetname, linkname) == 0) |
| 1260 | err = 0; |
| 1261 | out_free: |
Lin Ming | 7ee235e | 2011-03-03 23:23:57 +0800 | [diff] [blame] | 1262 | if (!is_kallsyms) |
| 1263 | free(realname); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1264 | free(filename); |
| 1265 | free(linkname); |
| 1266 | return err; |
| 1267 | } |
| 1268 | |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 1269 | static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size, |
| 1270 | const char *name, const char *debugdir, |
| 1271 | bool is_kallsyms) |
| 1272 | { |
| 1273 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 1274 | |
| 1275 | build_id__sprintf(build_id, build_id_size, sbuild_id); |
| 1276 | |
| 1277 | return build_id_cache__add_s(sbuild_id, debugdir, name, is_kallsyms); |
| 1278 | } |
| 1279 | |
| 1280 | int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir) |
| 1281 | { |
| 1282 | const size_t size = PATH_MAX; |
Han Pingtian | 4f9bae3 | 2011-07-18 11:13:14 +0800 | [diff] [blame] | 1283 | char *filename = zalloc(size), |
| 1284 | *linkname = zalloc(size); |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 1285 | int err = -1; |
| 1286 | |
| 1287 | if (filename == NULL || linkname == NULL) |
| 1288 | goto out_free; |
| 1289 | |
| 1290 | snprintf(linkname, size, "%s/.build-id/%.2s/%s", |
| 1291 | debugdir, sbuild_id, sbuild_id + 2); |
| 1292 | |
| 1293 | if (access(linkname, F_OK)) |
| 1294 | goto out_free; |
| 1295 | |
Thomas Jarosch | 6c3c5b2 | 2011-10-14 19:00:47 +0200 | [diff] [blame] | 1296 | if (readlink(linkname, filename, size - 1) < 0) |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 1297 | goto out_free; |
| 1298 | |
| 1299 | if (unlink(linkname)) |
| 1300 | goto out_free; |
| 1301 | |
| 1302 | /* |
| 1303 | * Since the link is relative, we must make it absolute: |
| 1304 | */ |
| 1305 | snprintf(linkname, size, "%s/.build-id/%.2s/%s", |
| 1306 | debugdir, sbuild_id, filename); |
| 1307 | |
| 1308 | if (unlink(linkname)) |
| 1309 | goto out_free; |
| 1310 | |
| 1311 | err = 0; |
| 1312 | out_free: |
| 1313 | free(filename); |
| 1314 | free(linkname); |
| 1315 | return err; |
| 1316 | } |
| 1317 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1318 | static int dso__cache_build_id(struct dso *dso, const char *debugdir) |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 1319 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1320 | bool is_kallsyms = dso->kernel && dso->long_name[0] != '/'; |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 1321 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1322 | return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), |
| 1323 | dso->long_name, debugdir, is_kallsyms); |
Arnaldo Carvalho de Melo | ef12a14 | 2010-01-20 15:28:45 -0200 | [diff] [blame] | 1324 | } |
| 1325 | |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1326 | static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir) |
| 1327 | { |
| 1328 | struct dso *pos; |
| 1329 | int err = 0; |
| 1330 | |
| 1331 | dsos__for_each_with_build_id(pos, head) |
| 1332 | if (dso__cache_build_id(pos, debugdir)) |
| 1333 | err = -1; |
| 1334 | |
| 1335 | return err; |
| 1336 | } |
| 1337 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1338 | static int machine__cache_build_ids(struct machine *machine, const char *debugdir) |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1339 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1340 | int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir); |
| 1341 | ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1342 | return ret; |
| 1343 | } |
| 1344 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1345 | static int perf_session__cache_build_ids(struct perf_session *session) |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1346 | { |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1347 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1348 | int ret; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1349 | char debugdir[PATH_MAX]; |
| 1350 | |
Stephane Eranian | 45de34b | 2010-06-01 21:25:01 +0200 | [diff] [blame] | 1351 | snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir); |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1352 | |
| 1353 | if (mkdir(debugdir, 0755) != 0 && errno != EEXIST) |
| 1354 | return -1; |
| 1355 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1356 | ret = machine__cache_build_ids(&session->host_machine, debugdir); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1357 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1358 | for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1359 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1360 | ret |= machine__cache_build_ids(pos, debugdir); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1361 | } |
| 1362 | return ret ? -1 : 0; |
| 1363 | } |
| 1364 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1365 | static bool machine__read_build_ids(struct machine *machine, bool with_hits) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1366 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1367 | bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits); |
| 1368 | ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1369 | return ret; |
| 1370 | } |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1371 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1372 | static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits) |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1373 | { |
| 1374 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1375 | bool ret = machine__read_build_ids(&session->host_machine, with_hits); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1376 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1377 | for (nd = rb_first(&session->machines); nd; nd = rb_next(nd)) { |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1378 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1379 | ret |= machine__read_build_ids(pos, with_hits); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | return ret; |
Arnaldo Carvalho de Melo | 4cf4013 | 2009-12-27 21:37:06 -0200 | [diff] [blame] | 1383 | } |
| 1384 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1385 | static int do_write_feat(int fd, struct perf_header *h, int type, |
| 1386 | struct perf_file_section **p, |
| 1387 | struct perf_evlist *evlist) |
| 1388 | { |
| 1389 | int err; |
| 1390 | int ret = 0; |
| 1391 | |
| 1392 | if (perf_header__has_feat(h, type)) { |
| 1393 | |
| 1394 | (*p)->offset = lseek(fd, 0, SEEK_CUR); |
| 1395 | |
| 1396 | err = feat_ops[type].write(fd, h, evlist); |
| 1397 | if (err < 0) { |
| 1398 | pr_debug("failed to write feature %d\n", type); |
| 1399 | |
| 1400 | /* undo anything written */ |
| 1401 | lseek(fd, (*p)->offset, SEEK_SET); |
| 1402 | |
| 1403 | return -1; |
| 1404 | } |
| 1405 | (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset; |
| 1406 | (*p)++; |
| 1407 | } |
| 1408 | return ret; |
| 1409 | } |
| 1410 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1411 | static int perf_header__adds_write(struct perf_header *header, |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 1412 | struct perf_evlist *evlist, int fd) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 1413 | { |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1414 | int nr_sections; |
Arnaldo Carvalho de Melo | f869097 | 2010-05-19 13:41:23 -0300 | [diff] [blame] | 1415 | struct perf_session *session; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1416 | struct perf_file_section *feat_sec, *p; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1417 | int sec_size; |
| 1418 | u64 sec_start; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1419 | int err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1420 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1421 | session = container_of(header, struct perf_session, header); |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 1422 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1423 | if (perf_header__has_feat(header, HEADER_BUILD_ID && |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 1424 | !perf_session__read_build_ids(session, true))) |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1425 | perf_header__clear_feat(header, HEADER_BUILD_ID); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1426 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1427 | nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1428 | if (!nr_sections) |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1429 | return 0; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1430 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1431 | feat_sec = p = calloc(sizeof(*feat_sec), nr_sections); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1432 | if (feat_sec == NULL) |
| 1433 | return -ENOMEM; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1434 | |
| 1435 | sec_size = sizeof(*feat_sec) * nr_sections; |
| 1436 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1437 | sec_start = header->data_offset + header->data_size; |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 1438 | lseek(fd, sec_start + sec_size, SEEK_SET); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 1439 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1440 | err = do_write_feat(fd, header, HEADER_TRACE_INFO, &p, evlist); |
| 1441 | if (err) |
| 1442 | goto out_free; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1443 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1444 | err = do_write_feat(fd, header, HEADER_BUILD_ID, &p, evlist); |
| 1445 | if (err) { |
| 1446 | perf_header__clear_feat(header, HEADER_BUILD_ID); |
| 1447 | goto out_free; |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 1448 | } |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 1449 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1450 | err = do_write_feat(fd, header, HEADER_HOSTNAME, &p, evlist); |
| 1451 | if (err) |
| 1452 | perf_header__clear_feat(header, HEADER_HOSTNAME); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1453 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1454 | err = do_write_feat(fd, header, HEADER_OSRELEASE, &p, evlist); |
| 1455 | if (err) |
| 1456 | perf_header__clear_feat(header, HEADER_OSRELEASE); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1457 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1458 | err = do_write_feat(fd, header, HEADER_VERSION, &p, evlist); |
| 1459 | if (err) |
| 1460 | perf_header__clear_feat(header, HEADER_VERSION); |
| 1461 | |
| 1462 | err = do_write_feat(fd, header, HEADER_ARCH, &p, evlist); |
| 1463 | if (err) |
| 1464 | perf_header__clear_feat(header, HEADER_ARCH); |
| 1465 | |
| 1466 | err = do_write_feat(fd, header, HEADER_NRCPUS, &p, evlist); |
| 1467 | if (err) |
| 1468 | perf_header__clear_feat(header, HEADER_NRCPUS); |
| 1469 | |
| 1470 | err = do_write_feat(fd, header, HEADER_CPUDESC, &p, evlist); |
| 1471 | if (err) |
| 1472 | perf_header__clear_feat(header, HEADER_CPUDESC); |
| 1473 | |
| 1474 | err = do_write_feat(fd, header, HEADER_CPUID, &p, evlist); |
| 1475 | if (err) |
| 1476 | perf_header__clear_feat(header, HEADER_CPUID); |
| 1477 | |
| 1478 | err = do_write_feat(fd, header, HEADER_TOTAL_MEM, &p, evlist); |
| 1479 | if (err) |
| 1480 | perf_header__clear_feat(header, HEADER_TOTAL_MEM); |
| 1481 | |
| 1482 | err = do_write_feat(fd, header, HEADER_CMDLINE, &p, evlist); |
| 1483 | if (err) |
| 1484 | perf_header__clear_feat(header, HEADER_CMDLINE); |
| 1485 | |
| 1486 | err = do_write_feat(fd, header, HEADER_EVENT_DESC, &p, evlist); |
| 1487 | if (err) |
| 1488 | perf_header__clear_feat(header, HEADER_EVENT_DESC); |
| 1489 | |
| 1490 | err = do_write_feat(fd, header, HEADER_CPU_TOPOLOGY, &p, evlist); |
| 1491 | if (err) |
| 1492 | perf_header__clear_feat(header, HEADER_CPU_TOPOLOGY); |
| 1493 | |
| 1494 | err = do_write_feat(fd, header, HEADER_NUMA_TOPOLOGY, &p, evlist); |
| 1495 | if (err) |
| 1496 | perf_header__clear_feat(header, HEADER_NUMA_TOPOLOGY); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1497 | |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 1498 | lseek(fd, sec_start, SEEK_SET); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1499 | /* |
| 1500 | * may write more than needed due to dropped feature, but |
| 1501 | * this is okay, reader will skip the mising entries |
| 1502 | */ |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1503 | err = do_write(fd, feat_sec, sec_size); |
| 1504 | if (err < 0) |
| 1505 | pr_debug("failed to write feature section\n"); |
| 1506 | out_free: |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1507 | free(feat_sec); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1508 | return err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1509 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 1510 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1511 | int perf_header__write_pipe(int fd) |
| 1512 | { |
| 1513 | struct perf_pipe_file_header f_header; |
| 1514 | int err; |
| 1515 | |
| 1516 | f_header = (struct perf_pipe_file_header){ |
| 1517 | .magic = PERF_MAGIC, |
| 1518 | .size = sizeof(f_header), |
| 1519 | }; |
| 1520 | |
| 1521 | err = do_write(fd, &f_header, sizeof(f_header)); |
| 1522 | if (err < 0) { |
| 1523 | pr_debug("failed to write perf pipe header\n"); |
| 1524 | return err; |
| 1525 | } |
| 1526 | |
| 1527 | return 0; |
| 1528 | } |
| 1529 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1530 | int perf_session__write_header(struct perf_session *session, |
| 1531 | struct perf_evlist *evlist, |
| 1532 | int fd, bool at_exit) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1533 | { |
| 1534 | struct perf_file_header f_header; |
| 1535 | struct perf_file_attr f_attr; |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1536 | struct perf_header *header = &session->header; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1537 | struct perf_evsel *attr, *pair = NULL; |
| 1538 | int err; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1539 | |
| 1540 | lseek(fd, sizeof(f_header), SEEK_SET); |
| 1541 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1542 | if (session->evlist != evlist) |
| 1543 | pair = list_entry(session->evlist->entries.next, struct perf_evsel, node); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1544 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1545 | list_for_each_entry(attr, &evlist->entries, node) { |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1546 | attr->id_offset = lseek(fd, 0, SEEK_CUR); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1547 | err = do_write(fd, attr->id, attr->ids * sizeof(u64)); |
| 1548 | if (err < 0) { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1549 | out_err_write: |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1550 | pr_debug("failed to write perf header\n"); |
| 1551 | return err; |
| 1552 | } |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1553 | if (session->evlist != evlist) { |
| 1554 | err = do_write(fd, pair->id, pair->ids * sizeof(u64)); |
| 1555 | if (err < 0) |
| 1556 | goto out_err_write; |
| 1557 | attr->ids += pair->ids; |
| 1558 | pair = list_entry(pair->node.next, struct perf_evsel, node); |
| 1559 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1560 | } |
| 1561 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1562 | header->attr_offset = lseek(fd, 0, SEEK_CUR); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1563 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1564 | list_for_each_entry(attr, &evlist->entries, node) { |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1565 | f_attr = (struct perf_file_attr){ |
| 1566 | .attr = attr->attr, |
| 1567 | .ids = { |
| 1568 | .offset = attr->id_offset, |
| 1569 | .size = attr->ids * sizeof(u64), |
| 1570 | } |
| 1571 | }; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1572 | err = do_write(fd, &f_attr, sizeof(f_attr)); |
| 1573 | if (err < 0) { |
| 1574 | pr_debug("failed to write perf header attribute\n"); |
| 1575 | return err; |
| 1576 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1577 | } |
| 1578 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1579 | header->event_offset = lseek(fd, 0, SEEK_CUR); |
| 1580 | header->event_size = event_count * sizeof(struct perf_trace_event_type); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1581 | if (events) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1582 | err = do_write(fd, events, header->event_size); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1583 | if (err < 0) { |
| 1584 | pr_debug("failed to write perf header events\n"); |
| 1585 | return err; |
| 1586 | } |
| 1587 | } |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 1588 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1589 | header->data_offset = lseek(fd, 0, SEEK_CUR); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1590 | |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1591 | if (at_exit) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1592 | err = perf_header__adds_write(header, evlist, fd); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1593 | if (err < 0) |
| 1594 | return err; |
| 1595 | } |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1596 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1597 | f_header = (struct perf_file_header){ |
| 1598 | .magic = PERF_MAGIC, |
| 1599 | .size = sizeof(f_header), |
| 1600 | .attr_size = sizeof(f_attr), |
| 1601 | .attrs = { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1602 | .offset = header->attr_offset, |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1603 | .size = evlist->nr_entries * sizeof(f_attr), |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1604 | }, |
| 1605 | .data = { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1606 | .offset = header->data_offset, |
| 1607 | .size = header->data_size, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1608 | }, |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 1609 | .event_types = { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1610 | .offset = header->event_offset, |
| 1611 | .size = header->event_size, |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 1612 | }, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1613 | }; |
| 1614 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1615 | memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features)); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 1616 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1617 | lseek(fd, 0, SEEK_SET); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1618 | err = do_write(fd, &f_header, sizeof(f_header)); |
| 1619 | if (err < 0) { |
| 1620 | pr_debug("failed to write perf header\n"); |
| 1621 | return err; |
| 1622 | } |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1623 | lseek(fd, header->data_offset + header->data_size, SEEK_SET); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1624 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1625 | header->frozen = 1; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 1626 | return 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1627 | } |
| 1628 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1629 | static int perf_header__getbuffer64(struct perf_header *header, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1630 | int fd, void *buf, size_t size) |
| 1631 | { |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 1632 | if (readn(fd, buf, size) <= 0) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1633 | return -1; |
| 1634 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1635 | if (header->needs_swap) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1636 | mem_bswap_64(buf, size); |
| 1637 | |
| 1638 | return 0; |
| 1639 | } |
| 1640 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1641 | int perf_header__process_sections(struct perf_header *header, int fd, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1642 | void *data, |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1643 | int (*process)(struct perf_file_section *section, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1644 | struct perf_header *ph, |
| 1645 | int feat, int fd, void *data)) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 1646 | { |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1647 | struct perf_file_section *feat_sec; |
| 1648 | int nr_sections; |
| 1649 | int sec_size; |
| 1650 | int idx = 0; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 1651 | int err = -1, feat = 1; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1652 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1653 | nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1654 | if (!nr_sections) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1655 | return 0; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1656 | |
| 1657 | feat_sec = calloc(sizeof(*feat_sec), nr_sections); |
| 1658 | if (!feat_sec) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1659 | return -1; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1660 | |
| 1661 | sec_size = sizeof(*feat_sec) * nr_sections; |
| 1662 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1663 | lseek(fd, header->data_offset + header->data_size, SEEK_SET); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1664 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1665 | if (perf_header__getbuffer64(header, fd, feat_sec, sec_size)) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 1666 | goto out_free; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1667 | |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 1668 | err = 0; |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1669 | while (idx < nr_sections && feat < HEADER_LAST_FEATURE) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1670 | if (perf_header__has_feat(header, feat)) { |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1671 | struct perf_file_section *sec = &feat_sec[idx++]; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1672 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1673 | err = process(sec, header, feat, fd, data); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1674 | if (err < 0) |
| 1675 | break; |
| 1676 | } |
| 1677 | ++feat; |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 1678 | } |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 1679 | out_free: |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 1680 | free(feat_sec); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1681 | return err; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 1682 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 1683 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1684 | int perf_file_header__read(struct perf_file_header *header, |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1685 | struct perf_header *ph, int fd) |
| 1686 | { |
| 1687 | lseek(fd, 0, SEEK_SET); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1688 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1689 | if (readn(fd, header, sizeof(*header)) <= 0 || |
| 1690 | memcmp(&header->magic, __perf_magic, sizeof(header->magic))) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1691 | return -1; |
| 1692 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1693 | if (header->attr_size != sizeof(struct perf_file_attr)) { |
| 1694 | u64 attr_size = bswap_64(header->attr_size); |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1695 | |
| 1696 | if (attr_size != sizeof(struct perf_file_attr)) |
| 1697 | return -1; |
| 1698 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1699 | mem_bswap_64(header, offsetof(struct perf_file_header, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1700 | adds_features)); |
| 1701 | ph->needs_swap = true; |
| 1702 | } |
| 1703 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1704 | if (header->size != sizeof(*header)) { |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1705 | /* Support the previous format */ |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1706 | if (header->size == offsetof(typeof(*header), adds_features)) |
| 1707 | bitmap_zero(header->adds_features, HEADER_FEAT_BITS); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1708 | else |
| 1709 | return -1; |
David Ahern | d327fa4 | 2011-10-18 17:34:01 -0600 | [diff] [blame] | 1710 | } else if (ph->needs_swap) { |
| 1711 | unsigned int i; |
| 1712 | /* |
| 1713 | * feature bitmap is declared as an array of unsigned longs -- |
| 1714 | * not good since its size can differ between the host that |
| 1715 | * generated the data file and the host analyzing the file. |
| 1716 | * |
| 1717 | * We need to handle endianness, but we don't know the size of |
| 1718 | * the unsigned long where the file was generated. Take a best |
| 1719 | * guess at determining it: try 64-bit swap first (ie., file |
| 1720 | * created on a 64-bit host), and check if the hostname feature |
| 1721 | * bit is set (this feature bit is forced on as of fbe96f2). |
| 1722 | * If the bit is not, undo the 64-bit swap and try a 32-bit |
| 1723 | * swap. If the hostname bit is still not set (e.g., older data |
| 1724 | * file), punt and fallback to the original behavior -- |
| 1725 | * clearing all feature bits and setting buildid. |
| 1726 | */ |
| 1727 | for (i = 0; i < BITS_TO_LONGS(HEADER_FEAT_BITS); ++i) |
| 1728 | header->adds_features[i] = bswap_64(header->adds_features[i]); |
| 1729 | |
| 1730 | if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { |
| 1731 | for (i = 0; i < BITS_TO_LONGS(HEADER_FEAT_BITS); ++i) { |
| 1732 | header->adds_features[i] = bswap_64(header->adds_features[i]); |
| 1733 | header->adds_features[i] = bswap_32(header->adds_features[i]); |
| 1734 | } |
| 1735 | } |
| 1736 | |
| 1737 | if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { |
| 1738 | bitmap_zero(header->adds_features, HEADER_FEAT_BITS); |
| 1739 | set_bit(HEADER_BUILD_ID, header->adds_features); |
| 1740 | } |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1741 | } |
| 1742 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1743 | memcpy(&ph->adds_features, &header->adds_features, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1744 | sizeof(ph->adds_features)); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1745 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1746 | ph->event_offset = header->event_types.offset; |
| 1747 | ph->event_size = header->event_types.size; |
| 1748 | ph->data_offset = header->data.offset; |
| 1749 | ph->data_size = header->data.size; |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1750 | return 0; |
| 1751 | } |
| 1752 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1753 | static int __event_process_build_id(struct build_id_event *bev, |
| 1754 | char *filename, |
| 1755 | struct perf_session *session) |
| 1756 | { |
| 1757 | int err = -1; |
| 1758 | struct list_head *head; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1759 | struct machine *machine; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1760 | u16 misc; |
| 1761 | struct dso *dso; |
| 1762 | enum dso_kernel_type dso_type; |
| 1763 | |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1764 | machine = perf_session__findnew_machine(session, bev->pid); |
| 1765 | if (!machine) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1766 | goto out; |
| 1767 | |
| 1768 | misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 1769 | |
| 1770 | switch (misc) { |
| 1771 | case PERF_RECORD_MISC_KERNEL: |
| 1772 | dso_type = DSO_TYPE_KERNEL; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1773 | head = &machine->kernel_dsos; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1774 | break; |
| 1775 | case PERF_RECORD_MISC_GUEST_KERNEL: |
| 1776 | dso_type = DSO_TYPE_GUEST_KERNEL; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1777 | head = &machine->kernel_dsos; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1778 | break; |
| 1779 | case PERF_RECORD_MISC_USER: |
| 1780 | case PERF_RECORD_MISC_GUEST_USER: |
| 1781 | dso_type = DSO_TYPE_USER; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 1782 | head = &machine->user_dsos; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1783 | break; |
| 1784 | default: |
| 1785 | goto out; |
| 1786 | } |
| 1787 | |
| 1788 | dso = __dsos__findnew(head, filename); |
| 1789 | if (dso != NULL) { |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 1790 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 1791 | |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1792 | dso__set_build_id(dso, &bev->build_id); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 1793 | |
| 1794 | if (filename[0] == '[') |
| 1795 | dso->kernel = dso_type; |
| 1796 | |
| 1797 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), |
| 1798 | sbuild_id); |
| 1799 | pr_debug("build id event received for %s: %s\n", |
| 1800 | dso->long_name, sbuild_id); |
| 1801 | } |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1802 | |
| 1803 | err = 0; |
| 1804 | out: |
| 1805 | return err; |
| 1806 | } |
| 1807 | |
Arnaldo Carvalho de Melo | b251148 | 2011-03-22 13:36:45 -0300 | [diff] [blame] | 1808 | static int perf_header__read_build_ids_abi_quirk(struct perf_header *header, |
| 1809 | int input, u64 offset, u64 size) |
| 1810 | { |
| 1811 | struct perf_session *session = container_of(header, struct perf_session, header); |
| 1812 | struct { |
| 1813 | struct perf_event_header header; |
| 1814 | u8 build_id[ALIGN(BUILD_ID_SIZE, sizeof(u64))]; |
| 1815 | char filename[0]; |
| 1816 | } old_bev; |
| 1817 | struct build_id_event bev; |
| 1818 | char filename[PATH_MAX]; |
| 1819 | u64 limit = offset + size; |
| 1820 | |
| 1821 | while (offset < limit) { |
| 1822 | ssize_t len; |
| 1823 | |
| 1824 | if (read(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev)) |
| 1825 | return -1; |
| 1826 | |
| 1827 | if (header->needs_swap) |
| 1828 | perf_event_header__bswap(&old_bev.header); |
| 1829 | |
| 1830 | len = old_bev.header.size - sizeof(old_bev); |
| 1831 | if (read(input, filename, len) != len) |
| 1832 | return -1; |
| 1833 | |
| 1834 | bev.header = old_bev.header; |
Jiri Olsa | f57b05e | 2011-06-01 21:43:46 +0200 | [diff] [blame] | 1835 | |
| 1836 | /* |
| 1837 | * As the pid is the missing value, we need to fill |
| 1838 | * it properly. The header.misc value give us nice hint. |
| 1839 | */ |
| 1840 | bev.pid = HOST_KERNEL_ID; |
| 1841 | if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER || |
| 1842 | bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL) |
| 1843 | bev.pid = DEFAULT_GUEST_KERNEL_ID; |
| 1844 | |
Arnaldo Carvalho de Melo | b251148 | 2011-03-22 13:36:45 -0300 | [diff] [blame] | 1845 | memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id)); |
| 1846 | __event_process_build_id(&bev, filename, session); |
| 1847 | |
| 1848 | offset += bev.header.size; |
| 1849 | } |
| 1850 | |
| 1851 | return 0; |
| 1852 | } |
| 1853 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1854 | static int perf_header__read_build_ids(struct perf_header *header, |
| 1855 | int input, u64 offset, u64 size) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1856 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1857 | struct perf_session *session = container_of(header, struct perf_session, header); |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1858 | struct build_id_event bev; |
| 1859 | char filename[PATH_MAX]; |
Arnaldo Carvalho de Melo | b251148 | 2011-03-22 13:36:45 -0300 | [diff] [blame] | 1860 | u64 limit = offset + size, orig_offset = offset; |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1861 | int err = -1; |
| 1862 | |
| 1863 | while (offset < limit) { |
| 1864 | ssize_t len; |
| 1865 | |
| 1866 | if (read(input, &bev, sizeof(bev)) != sizeof(bev)) |
| 1867 | goto out; |
| 1868 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1869 | if (header->needs_swap) |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1870 | perf_event_header__bswap(&bev.header); |
| 1871 | |
| 1872 | len = bev.header.size - sizeof(bev); |
| 1873 | if (read(input, filename, len) != len) |
| 1874 | goto out; |
Arnaldo Carvalho de Melo | b251148 | 2011-03-22 13:36:45 -0300 | [diff] [blame] | 1875 | /* |
| 1876 | * The a1645ce1 changeset: |
| 1877 | * |
| 1878 | * "perf: 'perf kvm' tool for monitoring guest performance from host" |
| 1879 | * |
| 1880 | * Added a field to struct build_id_event that broke the file |
| 1881 | * format. |
| 1882 | * |
| 1883 | * Since the kernel build-id is the first entry, process the |
| 1884 | * table using the old format if the well known |
| 1885 | * '[kernel.kallsyms]' string for the kernel build-id has the |
| 1886 | * first 4 characters chopped off (where the pid_t sits). |
| 1887 | */ |
| 1888 | if (memcmp(filename, "nel.kallsyms]", 13) == 0) { |
| 1889 | if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1) |
| 1890 | return -1; |
| 1891 | return perf_header__read_build_ids_abi_quirk(header, input, offset, size); |
| 1892 | } |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 1893 | |
| 1894 | __event_process_build_id(&bev, filename, session); |
| 1895 | |
| 1896 | offset += bev.header.size; |
| 1897 | } |
| 1898 | err = 0; |
| 1899 | out: |
| 1900 | return err; |
| 1901 | } |
| 1902 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1903 | static int perf_file_section__process(struct perf_file_section *section, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1904 | struct perf_header *ph, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1905 | int feat, int fd, void *data __used) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1906 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1907 | if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 1908 | pr_debug("Failed to lseek to %" PRIu64 " offset for feature " |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1909 | "%d, continuing...\n", section->offset, feat); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1910 | return 0; |
| 1911 | } |
| 1912 | |
| 1913 | switch (feat) { |
| 1914 | case HEADER_TRACE_INFO: |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 1915 | trace_report(fd, false); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1916 | break; |
| 1917 | |
| 1918 | case HEADER_BUILD_ID: |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1919 | if (perf_header__read_build_ids(ph, fd, section->offset, section->size)) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1920 | pr_debug("Failed to read buildids, continuing...\n"); |
| 1921 | break; |
David Ahern | 51192de | 2011-10-18 18:34:24 -0600 | [diff] [blame] | 1922 | |
| 1923 | case HEADER_HOSTNAME: |
| 1924 | case HEADER_OSRELEASE: |
| 1925 | case HEADER_VERSION: |
| 1926 | case HEADER_ARCH: |
| 1927 | case HEADER_NRCPUS: |
| 1928 | case HEADER_CPUDESC: |
| 1929 | case HEADER_CPUID: |
| 1930 | case HEADER_TOTAL_MEM: |
| 1931 | case HEADER_CMDLINE: |
| 1932 | case HEADER_EVENT_DESC: |
| 1933 | case HEADER_CPU_TOPOLOGY: |
| 1934 | case HEADER_NUMA_TOPOLOGY: |
| 1935 | break; |
| 1936 | |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 1937 | default: |
| 1938 | pr_debug("unknown feature %d, continuing...\n", feat); |
| 1939 | } |
| 1940 | |
| 1941 | return 0; |
| 1942 | } |
| 1943 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1944 | static int perf_file_header__read_pipe(struct perf_pipe_file_header *header, |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 1945 | struct perf_header *ph, int fd, |
| 1946 | bool repipe) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1947 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1948 | if (readn(fd, header, sizeof(*header)) <= 0 || |
| 1949 | memcmp(&header->magic, __perf_magic, sizeof(header->magic))) |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1950 | return -1; |
| 1951 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1952 | if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 1953 | return -1; |
| 1954 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1955 | if (header->size != sizeof(*header)) { |
| 1956 | u64 size = bswap_64(header->size); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1957 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1958 | if (size != sizeof(*header)) |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1959 | return -1; |
| 1960 | |
| 1961 | ph->needs_swap = true; |
| 1962 | } |
| 1963 | |
| 1964 | return 0; |
| 1965 | } |
| 1966 | |
| 1967 | static int perf_header__read_pipe(struct perf_session *session, int fd) |
| 1968 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1969 | struct perf_header *header = &session->header; |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1970 | struct perf_pipe_file_header f_header; |
| 1971 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1972 | if (perf_file_header__read_pipe(&f_header, header, fd, |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 1973 | session->repipe) < 0) { |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1974 | pr_debug("incompatible file format\n"); |
| 1975 | return -EINVAL; |
| 1976 | } |
| 1977 | |
| 1978 | session->fd = fd; |
| 1979 | |
| 1980 | return 0; |
| 1981 | } |
| 1982 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1983 | int perf_session__read_header(struct perf_session *session, int fd) |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1984 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1985 | struct perf_header *header = &session->header; |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 1986 | struct perf_file_header f_header; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1987 | struct perf_file_attr f_attr; |
| 1988 | u64 f_id; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 1989 | int nr_attrs, nr_ids, i, j; |
| 1990 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 1991 | session->evlist = perf_evlist__new(NULL, NULL); |
| 1992 | if (session->evlist == NULL) |
| 1993 | return -ENOMEM; |
| 1994 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 1995 | if (session->fd_pipe) |
| 1996 | return perf_header__read_pipe(session, fd); |
| 1997 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 1998 | if (perf_file_header__read(&f_header, header, fd) < 0) { |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 1999 | pr_debug("incompatible file format\n"); |
| 2000 | return -EINVAL; |
| 2001 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2002 | |
| 2003 | nr_attrs = f_header.attrs.size / sizeof(f_attr); |
| 2004 | lseek(fd, f_header.attrs.offset, SEEK_SET); |
| 2005 | |
| 2006 | for (i = 0; i < nr_attrs; i++) { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2007 | struct perf_evsel *evsel; |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 2008 | off_t tmp; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2009 | |
David Ahern | eda3913 | 2011-07-15 12:34:09 -0600 | [diff] [blame] | 2010 | if (readn(fd, &f_attr, sizeof(f_attr)) <= 0) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2011 | goto out_errno; |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2012 | |
David Ahern | eda3913 | 2011-07-15 12:34:09 -0600 | [diff] [blame] | 2013 | if (header->needs_swap) |
| 2014 | perf_event__attr_swap(&f_attr.attr); |
| 2015 | |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 2016 | tmp = lseek(fd, 0, SEEK_CUR); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2017 | evsel = perf_evsel__new(&f_attr.attr, i); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2018 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2019 | if (evsel == NULL) |
| 2020 | goto out_delete_evlist; |
| 2021 | /* |
| 2022 | * Do it before so that if perf_evsel__alloc_id fails, this |
| 2023 | * entry gets purged too at perf_evlist__delete(). |
| 2024 | */ |
| 2025 | perf_evlist__add(session->evlist, evsel); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2026 | |
| 2027 | nr_ids = f_attr.ids.size / sizeof(u64); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2028 | /* |
| 2029 | * We don't have the cpu and thread maps on the header, so |
| 2030 | * for allocating the perf_sample_id table we fake 1 cpu and |
| 2031 | * hattr->ids threads. |
| 2032 | */ |
| 2033 | if (perf_evsel__alloc_id(evsel, 1, nr_ids)) |
| 2034 | goto out_delete_evlist; |
| 2035 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2036 | lseek(fd, f_attr.ids.offset, SEEK_SET); |
| 2037 | |
| 2038 | for (j = 0; j < nr_ids; j++) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2039 | if (perf_header__getbuffer64(header, fd, &f_id, sizeof(f_id))) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2040 | goto out_errno; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2041 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2042 | perf_evlist__id_add(session->evlist, evsel, 0, j, f_id); |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 2043 | } |
Arnaldo Carvalho de Melo | 11deb1f | 2009-11-17 01:18:09 -0200 | [diff] [blame] | 2044 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2045 | lseek(fd, tmp, SEEK_SET); |
| 2046 | } |
| 2047 | |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 2048 | symbol_conf.nr_events = nr_attrs; |
| 2049 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 2050 | if (f_header.event_types.size) { |
| 2051 | lseek(fd, f_header.event_types.offset, SEEK_SET); |
| 2052 | events = malloc(f_header.event_types.size); |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 2053 | if (events == NULL) |
| 2054 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2055 | if (perf_header__getbuffer64(header, fd, events, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2056 | f_header.event_types.size)) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2057 | goto out_errno; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 2058 | event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type); |
| 2059 | } |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 2060 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2061 | perf_header__process_sections(header, fd, NULL, |
| 2062 | perf_file_section__process); |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 2063 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2064 | lseek(fd, header->data_offset, SEEK_SET); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2065 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2066 | header->frozen = 1; |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 2067 | return 0; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2068 | out_errno: |
| 2069 | return -errno; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2070 | |
| 2071 | out_delete_evlist: |
| 2072 | perf_evlist__delete(session->evlist); |
| 2073 | session->evlist = NULL; |
| 2074 | return -ENOMEM; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2075 | } |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 2076 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2077 | int perf_event__synthesize_attr(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2078 | struct perf_event_attr *attr, u16 ids, u64 *id, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2079 | perf_event__handler_t process) |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 2080 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2081 | union perf_event *ev; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2082 | size_t size; |
| 2083 | int err; |
| 2084 | |
| 2085 | size = sizeof(struct perf_event_attr); |
| 2086 | size = ALIGN(size, sizeof(u64)); |
| 2087 | size += sizeof(struct perf_event_header); |
| 2088 | size += ids * sizeof(u64); |
| 2089 | |
| 2090 | ev = malloc(size); |
| 2091 | |
Chris Samuel | ce47dc5 | 2010-11-13 13:35:06 +1100 | [diff] [blame] | 2092 | if (ev == NULL) |
| 2093 | return -ENOMEM; |
| 2094 | |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2095 | ev->attr.attr = *attr; |
| 2096 | memcpy(ev->attr.id, id, ids * sizeof(u64)); |
| 2097 | |
| 2098 | ev->attr.header.type = PERF_RECORD_HEADER_ATTR; |
| 2099 | ev->attr.header.size = size; |
| 2100 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2101 | err = process(tool, ev, NULL, NULL); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2102 | |
| 2103 | free(ev); |
| 2104 | |
| 2105 | return err; |
| 2106 | } |
| 2107 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2108 | int perf_event__synthesize_attrs(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2109 | struct perf_session *session, |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2110 | perf_event__handler_t process) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2111 | { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2112 | struct perf_evsel *attr; |
| 2113 | int err = 0; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2114 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2115 | list_for_each_entry(attr, &session->evlist->entries, node) { |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2116 | err = perf_event__synthesize_attr(tool, &attr->attr, attr->ids, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2117 | attr->id, process); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2118 | if (err) { |
| 2119 | pr_debug("failed to create perf header attribute\n"); |
| 2120 | return err; |
| 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | return err; |
| 2125 | } |
| 2126 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2127 | int perf_event__process_attr(union perf_event *event, |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2128 | struct perf_evlist **pevlist) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2129 | { |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2130 | unsigned int i, ids, n_ids; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2131 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2132 | struct perf_evlist *evlist = *pevlist; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2133 | |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2134 | if (evlist == NULL) { |
| 2135 | *pevlist = evlist = perf_evlist__new(NULL, NULL); |
| 2136 | if (evlist == NULL) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2137 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2138 | } |
| 2139 | |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2140 | evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2141 | if (evsel == NULL) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2142 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2143 | |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2144 | perf_evlist__add(evlist, evsel); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2145 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2146 | ids = event->header.size; |
| 2147 | ids -= (void *)&event->attr.id - (void *)event; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2148 | n_ids = ids / sizeof(u64); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2149 | /* |
| 2150 | * We don't have the cpu and thread maps on the header, so |
| 2151 | * for allocating the perf_sample_id table we fake 1 cpu and |
| 2152 | * hattr->ids threads. |
| 2153 | */ |
| 2154 | if (perf_evsel__alloc_id(evsel, 1, n_ids)) |
| 2155 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2156 | |
| 2157 | for (i = 0; i < n_ids; i++) { |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2158 | perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2159 | } |
| 2160 | |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2161 | return 0; |
| 2162 | } |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2163 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2164 | int perf_event__synthesize_event_type(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2165 | u64 event_id, char *name, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2166 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2167 | struct machine *machine) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2168 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2169 | union perf_event ev; |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2170 | size_t size = 0; |
| 2171 | int err = 0; |
| 2172 | |
| 2173 | memset(&ev, 0, sizeof(ev)); |
| 2174 | |
| 2175 | ev.event_type.event_type.event_id = event_id; |
| 2176 | memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME); |
| 2177 | strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1); |
| 2178 | |
| 2179 | ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE; |
| 2180 | size = strlen(name); |
| 2181 | size = ALIGN(size, sizeof(u64)); |
| 2182 | ev.event_type.header.size = sizeof(ev.event_type) - |
| 2183 | (sizeof(ev.event_type.event_type.name) - size); |
| 2184 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2185 | err = process(tool, &ev, NULL, machine); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2186 | |
| 2187 | return err; |
| 2188 | } |
| 2189 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2190 | int perf_event__synthesize_event_types(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2191 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2192 | struct machine *machine) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2193 | { |
| 2194 | struct perf_trace_event_type *type; |
| 2195 | int i, err = 0; |
| 2196 | |
| 2197 | for (i = 0; i < event_count; i++) { |
| 2198 | type = &events[i]; |
| 2199 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2200 | err = perf_event__synthesize_event_type(tool, type->event_id, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2201 | type->name, process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2202 | machine); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2203 | if (err) { |
| 2204 | pr_debug("failed to create perf header event type\n"); |
| 2205 | return err; |
| 2206 | } |
| 2207 | } |
| 2208 | |
| 2209 | return err; |
| 2210 | } |
| 2211 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2212 | int perf_event__process_event_type(struct perf_tool *tool __unused, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2213 | union perf_event *event) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2214 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2215 | if (perf_header__push_event(event->event_type.event_type.event_id, |
| 2216 | event->event_type.event_type.name) < 0) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2217 | return -ENOMEM; |
| 2218 | |
| 2219 | return 0; |
| 2220 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2221 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2222 | int perf_event__synthesize_tracing_data(struct perf_tool *tool, int fd, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2223 | struct perf_evlist *evlist, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2224 | perf_event__handler_t process) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2225 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2226 | union perf_event ev; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 2227 | struct tracing_data *tdata; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2228 | ssize_t size = 0, aligned_size = 0, padding; |
Kyle McMartin | fb7d0b3 | 2011-01-24 11:13:04 -0500 | [diff] [blame] | 2229 | int err __used = 0; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2230 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 2231 | /* |
| 2232 | * We are going to store the size of the data followed |
| 2233 | * by the data contents. Since the fd descriptor is a pipe, |
| 2234 | * we cannot seek back to store the size of the data once |
| 2235 | * we know it. Instead we: |
| 2236 | * |
| 2237 | * - write the tracing data to the temp file |
| 2238 | * - get/write the data size to pipe |
| 2239 | * - write the tracing data from the temp file |
| 2240 | * to the pipe |
| 2241 | */ |
| 2242 | tdata = tracing_data_get(&evlist->entries, fd, true); |
| 2243 | if (!tdata) |
| 2244 | return -1; |
| 2245 | |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2246 | memset(&ev, 0, sizeof(ev)); |
| 2247 | |
| 2248 | ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 2249 | size = tdata->size; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2250 | aligned_size = ALIGN(size, sizeof(u64)); |
| 2251 | padding = aligned_size - size; |
| 2252 | ev.tracing_data.header.size = sizeof(ev.tracing_data); |
| 2253 | ev.tracing_data.size = aligned_size; |
| 2254 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2255 | process(tool, &ev, NULL, NULL); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2256 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 2257 | /* |
| 2258 | * The put function will copy all the tracing data |
| 2259 | * stored in temp file to the pipe. |
| 2260 | */ |
| 2261 | tracing_data_put(tdata); |
| 2262 | |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2263 | write_padded(fd, NULL, 0, padding); |
| 2264 | |
| 2265 | return aligned_size; |
| 2266 | } |
| 2267 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2268 | int perf_event__process_tracing_data(union perf_event *event, |
| 2269 | struct perf_session *session) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2270 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2271 | ssize_t size_read, padding, size = event->tracing_data.size; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2272 | off_t offset = lseek(session->fd, 0, SEEK_CUR); |
| 2273 | char buf[BUFSIZ]; |
| 2274 | |
| 2275 | /* setup for reading amidst mmap */ |
| 2276 | lseek(session->fd, offset + sizeof(struct tracing_data_event), |
| 2277 | SEEK_SET); |
| 2278 | |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 2279 | size_read = trace_report(session->fd, session->repipe); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2280 | |
| 2281 | padding = ALIGN(size_read, sizeof(u64)) - size_read; |
| 2282 | |
| 2283 | if (read(session->fd, buf, padding) < 0) |
| 2284 | die("reading input file"); |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 2285 | if (session->repipe) { |
| 2286 | int retw = write(STDOUT_FILENO, buf, padding); |
| 2287 | if (retw <= 0 || retw != padding) |
| 2288 | die("repiping tracing data padding"); |
| 2289 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2290 | |
| 2291 | if (size_read + padding != size) |
| 2292 | die("tracing data size mismatch"); |
| 2293 | |
| 2294 | return size_read + padding; |
| 2295 | } |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2296 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2297 | int perf_event__synthesize_build_id(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2298 | struct dso *pos, u16 misc, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2299 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2300 | struct machine *machine) |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2301 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2302 | union perf_event ev; |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2303 | size_t len; |
| 2304 | int err = 0; |
| 2305 | |
| 2306 | if (!pos->hit) |
| 2307 | return err; |
| 2308 | |
| 2309 | memset(&ev, 0, sizeof(ev)); |
| 2310 | |
| 2311 | len = pos->long_name_len + 1; |
| 2312 | len = ALIGN(len, NAME_ALIGN); |
| 2313 | memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id)); |
| 2314 | ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID; |
| 2315 | ev.build_id.header.misc = misc; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 2316 | ev.build_id.pid = machine->pid; |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2317 | ev.build_id.header.size = sizeof(ev.build_id) + len; |
| 2318 | memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len); |
| 2319 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2320 | err = process(tool, &ev, NULL, machine); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2321 | |
| 2322 | return err; |
| 2323 | } |
| 2324 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2325 | int perf_event__process_build_id(struct perf_tool *tool __used, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2326 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2327 | struct perf_session *session) |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2328 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2329 | __event_process_build_id(&event->build_id, |
| 2330 | event->build_id.filename, |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2331 | session); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2332 | return 0; |
| 2333 | } |
Stephane Eranian | a1ac1d3 | 2010-06-17 11:39:01 +0200 | [diff] [blame] | 2334 | |
| 2335 | void disable_buildid_cache(void) |
| 2336 | { |
| 2337 | no_buildid_cache = true; |
| 2338 | } |