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