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> |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 12 | #include <sys/utsname.h> |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 13 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 14 | #include "evlist.h" |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 15 | #include "evsel.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 16 | #include "header.h" |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 17 | #include "../perf.h" |
| 18 | #include "trace-event.h" |
Arnaldo Carvalho de Melo | 301a0b0 | 2009-12-13 19:50:25 -0200 | [diff] [blame] | 19 | #include "session.h" |
Frederic Weisbecker | 8671dab | 2009-11-11 04:51:03 +0100 | [diff] [blame] | 20 | #include "symbol.h" |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 21 | #include "debug.h" |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 22 | #include "cpumap.h" |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 23 | #include "pmu.h" |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 24 | #include "vdso.h" |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 25 | #include "strbuf.h" |
Jiri Olsa | ebb296c | 2012-10-27 23:18:28 +0200 | [diff] [blame] | 26 | #include "build-id.h" |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 27 | |
Stephane Eranian | a1ac1d3 | 2010-06-17 11:39:01 +0200 | [diff] [blame] | 28 | static bool no_buildid_cache = false; |
| 29 | |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 30 | static int trace_event_count; |
| 31 | static struct perf_trace_event_type *trace_events; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 32 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 33 | static u32 header_argc; |
| 34 | static const char **header_argv; |
| 35 | |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 36 | int perf_header__push_event(u64 id, const char *name) |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 37 | { |
Ulrich Drepper | f3054c778 | 2012-04-23 09:04:29 -0400 | [diff] [blame] | 38 | struct perf_trace_event_type *nevents; |
| 39 | |
Arjan van de Ven | ec60a3f | 2009-09-19 13:36:30 +0200 | [diff] [blame] | 40 | if (strlen(name) > MAX_EVENT_NAME) |
Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 41 | pr_warning("Event %s will be truncated\n", name); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 42 | |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 43 | nevents = realloc(trace_events, (trace_event_count + 1) * sizeof(*trace_events)); |
Ulrich Drepper | f3054c778 | 2012-04-23 09:04:29 -0400 | [diff] [blame] | 44 | if (nevents == NULL) |
| 45 | return -ENOMEM; |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 46 | trace_events = nevents; |
Arnaldo Carvalho de Melo | ae99fb2 | 2009-12-28 22:48:33 -0200 | [diff] [blame] | 47 | |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 48 | memset(&trace_events[trace_event_count], 0, sizeof(struct perf_trace_event_type)); |
| 49 | trace_events[trace_event_count].event_id = id; |
| 50 | strncpy(trace_events[trace_event_count].name, name, MAX_EVENT_NAME - 1); |
| 51 | trace_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; |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 58 | for (i = 0 ; i < trace_event_count; i++) { |
| 59 | if (trace_events[i].event_id == id) |
| 60 | return trace_events[i].name; |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 61 | } |
| 62 | return NULL; |
| 63 | } |
| 64 | |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 65 | /* |
| 66 | * magic2 = "PERFILE2" |
| 67 | * must be a numerical value to let the endianness |
| 68 | * determine the memory layout. That way we are able |
| 69 | * to detect endianness when reading the perf.data file |
| 70 | * back. |
| 71 | * |
| 72 | * we check for legacy (PERFFILE) format. |
| 73 | */ |
| 74 | static const char *__perf_magic1 = "PERFFILE"; |
| 75 | static const u64 __perf_magic2 = 0x32454c4946524550ULL; |
| 76 | static const u64 __perf_magic2_sw = 0x50455246494c4532ULL; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 77 | |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 78 | #define PERF_MAGIC __perf_magic2 |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 79 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 80 | struct perf_file_attr { |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 81 | struct perf_event_attr attr; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 82 | struct perf_file_section ids; |
| 83 | }; |
| 84 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 85 | 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] | 86 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 87 | set_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 88 | } |
| 89 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 90 | 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] | 91 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 92 | clear_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | baa2f6c | 2010-11-26 19:39:15 -0200 | [diff] [blame] | 93 | } |
| 94 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 95 | 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] | 96 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 97 | return test_bit(feat, header->adds_features); |
Arnaldo Carvalho de Melo | 8d06367 | 2009-11-04 18:50:43 -0200 | [diff] [blame] | 98 | } |
| 99 | |
Arnaldo Carvalho de Melo | 3726cc7 | 2009-11-17 01:18:12 -0200 | [diff] [blame] | 100 | static int do_write(int fd, const void *buf, size_t size) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 101 | { |
| 102 | while (size) { |
| 103 | int ret = write(fd, buf, size); |
| 104 | |
| 105 | if (ret < 0) |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 106 | return -errno; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 107 | |
| 108 | size -= ret; |
| 109 | buf += ret; |
| 110 | } |
Arnaldo Carvalho de Melo | 3726cc7 | 2009-11-17 01:18:12 -0200 | [diff] [blame] | 111 | |
| 112 | return 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Arnaldo Carvalho de Melo | f92cb24 | 2010-01-04 16:19:28 -0200 | [diff] [blame] | 115 | #define NAME_ALIGN 64 |
| 116 | |
| 117 | static int write_padded(int fd, const void *bf, size_t count, |
| 118 | size_t count_aligned) |
| 119 | { |
| 120 | static const char zero_buf[NAME_ALIGN]; |
| 121 | int err = do_write(fd, bf, count); |
| 122 | |
| 123 | if (!err) |
| 124 | err = do_write(fd, zero_buf, count_aligned - count); |
| 125 | |
| 126 | return err; |
| 127 | } |
| 128 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 129 | static int do_write_string(int fd, const char *str) |
| 130 | { |
| 131 | u32 len, olen; |
| 132 | int ret; |
| 133 | |
| 134 | olen = strlen(str) + 1; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 135 | len = PERF_ALIGN(olen, NAME_ALIGN); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 136 | |
| 137 | /* write len, incl. \0 */ |
| 138 | ret = do_write(fd, &len, sizeof(len)); |
| 139 | if (ret < 0) |
| 140 | return ret; |
| 141 | |
| 142 | return write_padded(fd, str, olen, len); |
| 143 | } |
| 144 | |
| 145 | static char *do_read_string(int fd, struct perf_header *ph) |
| 146 | { |
| 147 | ssize_t sz, ret; |
| 148 | u32 len; |
| 149 | char *buf; |
| 150 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 151 | sz = readn(fd, &len, sizeof(len)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 152 | if (sz < (ssize_t)sizeof(len)) |
| 153 | return NULL; |
| 154 | |
| 155 | if (ph->needs_swap) |
| 156 | len = bswap_32(len); |
| 157 | |
| 158 | buf = malloc(len); |
| 159 | if (!buf) |
| 160 | return NULL; |
| 161 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 162 | ret = readn(fd, buf, len); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 163 | if (ret == (ssize_t)len) { |
| 164 | /* |
| 165 | * strings are padded by zeroes |
| 166 | * thus the actual strlen of buf |
| 167 | * may be less than len |
| 168 | */ |
| 169 | return buf; |
| 170 | } |
| 171 | |
| 172 | free(buf); |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | int |
| 177 | perf_header__set_cmdline(int argc, const char **argv) |
| 178 | { |
| 179 | int i; |
| 180 | |
David Ahern | 56e6f60 | 2012-07-29 20:53:51 -0600 | [diff] [blame] | 181 | /* |
| 182 | * If header_argv has already been set, do not override it. |
| 183 | * This allows a command to set the cmdline, parse args and |
| 184 | * then call another builtin function that implements a |
| 185 | * command -- e.g, cmd_kvm calling cmd_record. |
| 186 | */ |
| 187 | if (header_argv) |
| 188 | return 0; |
| 189 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 190 | header_argc = (u32)argc; |
| 191 | |
| 192 | /* do not include NULL termination */ |
| 193 | header_argv = calloc(argc, sizeof(char *)); |
| 194 | if (!header_argv) |
| 195 | return -ENOMEM; |
| 196 | |
| 197 | /* |
| 198 | * must copy argv contents because it gets moved |
| 199 | * around during option parsing |
| 200 | */ |
| 201 | for (i = 0; i < argc ; i++) |
| 202 | header_argv[i] = argv[i]; |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 207 | #define dsos__for_each_with_build_id(pos, head) \ |
| 208 | list_for_each_entry(pos, head, node) \ |
| 209 | if (!pos->has_build_id) \ |
| 210 | continue; \ |
| 211 | else |
| 212 | |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 213 | static int write_buildid(char *name, size_t name_len, u8 *build_id, |
| 214 | pid_t pid, u16 misc, int fd) |
| 215 | { |
| 216 | int err; |
| 217 | struct build_id_event b; |
| 218 | size_t len; |
| 219 | |
| 220 | len = name_len + 1; |
| 221 | len = PERF_ALIGN(len, NAME_ALIGN); |
| 222 | |
| 223 | memset(&b, 0, sizeof(b)); |
| 224 | memcpy(&b.build_id, build_id, BUILD_ID_SIZE); |
| 225 | b.pid = pid; |
| 226 | b.header.misc = misc; |
| 227 | b.header.size = sizeof(b) + len; |
| 228 | |
| 229 | err = do_write(fd, &b, sizeof(b)); |
| 230 | if (err < 0) |
| 231 | return err; |
| 232 | |
| 233 | return write_padded(fd, name, name_len + 1, len); |
| 234 | } |
| 235 | |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 236 | static int __dsos__write_buildid_table(struct list_head *head, pid_t pid, |
| 237 | u16 misc, int fd) |
| 238 | { |
| 239 | struct dso *pos; |
| 240 | |
| 241 | dsos__for_each_with_build_id(pos, head) { |
| 242 | int err; |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 243 | char *name; |
| 244 | size_t name_len; |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 245 | |
| 246 | if (!pos->hit) |
| 247 | continue; |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 248 | |
| 249 | if (is_vdso_map(pos->short_name)) { |
| 250 | name = (char *) VDSO__MAP_NAME; |
| 251 | name_len = sizeof(VDSO__MAP_NAME) + 1; |
| 252 | } else { |
| 253 | name = pos->long_name; |
| 254 | name_len = pos->long_name_len + 1; |
| 255 | } |
| 256 | |
| 257 | err = write_buildid(name, name_len, pos->build_id, |
| 258 | pid, misc, fd); |
| 259 | if (err) |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 260 | return err; |
| 261 | } |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | static int machine__write_buildid_table(struct machine *machine, int fd) |
| 267 | { |
| 268 | int err; |
| 269 | u16 kmisc = PERF_RECORD_MISC_KERNEL, |
| 270 | umisc = PERF_RECORD_MISC_USER; |
| 271 | |
| 272 | if (!machine__is_host(machine)) { |
| 273 | kmisc = PERF_RECORD_MISC_GUEST_KERNEL; |
| 274 | umisc = PERF_RECORD_MISC_GUEST_USER; |
| 275 | } |
| 276 | |
| 277 | err = __dsos__write_buildid_table(&machine->kernel_dsos, machine->pid, |
| 278 | kmisc, fd); |
| 279 | if (err == 0) |
| 280 | err = __dsos__write_buildid_table(&machine->user_dsos, |
| 281 | machine->pid, umisc, fd); |
| 282 | return err; |
| 283 | } |
| 284 | |
| 285 | static int dsos__write_buildid_table(struct perf_header *header, int fd) |
| 286 | { |
| 287 | struct perf_session *session = container_of(header, |
| 288 | struct perf_session, header); |
| 289 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 290 | int err = machine__write_buildid_table(&session->machines.host, fd); |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 291 | |
| 292 | if (err) |
| 293 | return err; |
| 294 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 295 | for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 296 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 297 | err = machine__write_buildid_table(pos, fd); |
| 298 | if (err) |
| 299 | break; |
| 300 | } |
| 301 | return err; |
| 302 | } |
| 303 | |
| 304 | int build_id_cache__add_s(const char *sbuild_id, const char *debugdir, |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 305 | const char *name, bool is_kallsyms, bool is_vdso) |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 306 | { |
| 307 | const size_t size = PATH_MAX; |
| 308 | char *realname, *filename = zalloc(size), |
| 309 | *linkname = zalloc(size), *targetname; |
| 310 | int len, err = -1; |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 311 | bool slash = is_kallsyms || is_vdso; |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 312 | |
| 313 | if (is_kallsyms) { |
| 314 | if (symbol_conf.kptr_restrict) { |
| 315 | pr_debug("Not caching a kptr_restrict'ed /proc/kallsyms\n"); |
Thomas Jarosch | fdae637 | 2013-01-25 11:21:39 +0100 | [diff] [blame^] | 316 | err = 0; |
| 317 | goto out_free; |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 318 | } |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 319 | realname = (char *) name; |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 320 | } else |
| 321 | realname = realpath(name, NULL); |
| 322 | |
| 323 | if (realname == NULL || filename == NULL || linkname == NULL) |
| 324 | goto out_free; |
| 325 | |
Arnaldo Carvalho de Melo | e7f01d1 | 2012-03-14 12:29:29 -0300 | [diff] [blame] | 326 | len = scnprintf(filename, size, "%s%s%s", |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 327 | debugdir, slash ? "/" : "", |
| 328 | is_vdso ? VDSO__MAP_NAME : realname); |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 329 | if (mkdir_p(filename, 0755)) |
| 330 | goto out_free; |
| 331 | |
Namhyung Kim | afda0f9 | 2012-05-01 23:19:36 +0900 | [diff] [blame] | 332 | snprintf(filename + len, size - len, "/%s", sbuild_id); |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 333 | |
| 334 | if (access(filename, F_OK)) { |
| 335 | if (is_kallsyms) { |
| 336 | if (copyfile("/proc/kallsyms", filename)) |
| 337 | goto out_free; |
| 338 | } else if (link(realname, filename) && copyfile(name, filename)) |
| 339 | goto out_free; |
| 340 | } |
| 341 | |
Arnaldo Carvalho de Melo | e7f01d1 | 2012-03-14 12:29:29 -0300 | [diff] [blame] | 342 | len = scnprintf(linkname, size, "%s/.build-id/%.2s", |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 343 | debugdir, sbuild_id); |
| 344 | |
| 345 | if (access(linkname, X_OK) && mkdir_p(linkname, 0755)) |
| 346 | goto out_free; |
| 347 | |
| 348 | snprintf(linkname + len, size - len, "/%s", sbuild_id + 2); |
| 349 | targetname = filename + strlen(debugdir) - 5; |
| 350 | memcpy(targetname, "../..", 5); |
| 351 | |
| 352 | if (symlink(targetname, linkname) == 0) |
| 353 | err = 0; |
| 354 | out_free: |
| 355 | if (!is_kallsyms) |
| 356 | free(realname); |
| 357 | free(filename); |
| 358 | free(linkname); |
| 359 | return err; |
| 360 | } |
| 361 | |
| 362 | static int build_id_cache__add_b(const u8 *build_id, size_t build_id_size, |
| 363 | const char *name, const char *debugdir, |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 364 | bool is_kallsyms, bool is_vdso) |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 365 | { |
| 366 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 367 | |
| 368 | build_id__sprintf(build_id, build_id_size, sbuild_id); |
| 369 | |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 370 | return build_id_cache__add_s(sbuild_id, debugdir, name, |
| 371 | is_kallsyms, is_vdso); |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | int build_id_cache__remove_s(const char *sbuild_id, const char *debugdir) |
| 375 | { |
| 376 | const size_t size = PATH_MAX; |
| 377 | char *filename = zalloc(size), |
| 378 | *linkname = zalloc(size); |
| 379 | int err = -1; |
| 380 | |
| 381 | if (filename == NULL || linkname == NULL) |
| 382 | goto out_free; |
| 383 | |
| 384 | snprintf(linkname, size, "%s/.build-id/%.2s/%s", |
| 385 | debugdir, sbuild_id, sbuild_id + 2); |
| 386 | |
| 387 | if (access(linkname, F_OK)) |
| 388 | goto out_free; |
| 389 | |
| 390 | if (readlink(linkname, filename, size - 1) < 0) |
| 391 | goto out_free; |
| 392 | |
| 393 | if (unlink(linkname)) |
| 394 | goto out_free; |
| 395 | |
| 396 | /* |
| 397 | * Since the link is relative, we must make it absolute: |
| 398 | */ |
| 399 | snprintf(linkname, size, "%s/.build-id/%.2s/%s", |
| 400 | debugdir, sbuild_id, filename); |
| 401 | |
| 402 | if (unlink(linkname)) |
| 403 | goto out_free; |
| 404 | |
| 405 | err = 0; |
| 406 | out_free: |
| 407 | free(filename); |
| 408 | free(linkname); |
| 409 | return err; |
| 410 | } |
| 411 | |
| 412 | static int dso__cache_build_id(struct dso *dso, const char *debugdir) |
| 413 | { |
| 414 | bool is_kallsyms = dso->kernel && dso->long_name[0] != '/'; |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 415 | bool is_vdso = is_vdso_map(dso->short_name); |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 416 | |
| 417 | return build_id_cache__add_b(dso->build_id, sizeof(dso->build_id), |
Jiri Olsa | 7dbf4dc | 2012-09-10 18:50:19 +0200 | [diff] [blame] | 418 | dso->long_name, debugdir, |
| 419 | is_kallsyms, is_vdso); |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static int __dsos__cache_build_ids(struct list_head *head, const char *debugdir) |
| 423 | { |
| 424 | struct dso *pos; |
| 425 | int err = 0; |
| 426 | |
| 427 | dsos__for_each_with_build_id(pos, head) |
| 428 | if (dso__cache_build_id(pos, debugdir)) |
| 429 | err = -1; |
| 430 | |
| 431 | return err; |
| 432 | } |
| 433 | |
| 434 | static int machine__cache_build_ids(struct machine *machine, const char *debugdir) |
| 435 | { |
| 436 | int ret = __dsos__cache_build_ids(&machine->kernel_dsos, debugdir); |
| 437 | ret |= __dsos__cache_build_ids(&machine->user_dsos, debugdir); |
| 438 | return ret; |
| 439 | } |
| 440 | |
| 441 | static int perf_session__cache_build_ids(struct perf_session *session) |
| 442 | { |
| 443 | struct rb_node *nd; |
| 444 | int ret; |
| 445 | char debugdir[PATH_MAX]; |
| 446 | |
| 447 | snprintf(debugdir, sizeof(debugdir), "%s", buildid_dir); |
| 448 | |
| 449 | if (mkdir(debugdir, 0755) != 0 && errno != EEXIST) |
| 450 | return -1; |
| 451 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 452 | ret = machine__cache_build_ids(&session->machines.host, debugdir); |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 453 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 454 | for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 455 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 456 | ret |= machine__cache_build_ids(pos, debugdir); |
| 457 | } |
| 458 | return ret ? -1 : 0; |
| 459 | } |
| 460 | |
| 461 | static bool machine__read_build_ids(struct machine *machine, bool with_hits) |
| 462 | { |
| 463 | bool ret = __dsos__read_build_ids(&machine->kernel_dsos, with_hits); |
| 464 | ret |= __dsos__read_build_ids(&machine->user_dsos, with_hits); |
| 465 | return ret; |
| 466 | } |
| 467 | |
| 468 | static bool perf_session__read_build_ids(struct perf_session *session, bool with_hits) |
| 469 | { |
| 470 | struct rb_node *nd; |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 471 | bool ret = machine__read_build_ids(&session->machines.host, with_hits); |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 472 | |
Arnaldo Carvalho de Melo | 876650e | 2012-12-18 19:15:48 -0300 | [diff] [blame] | 473 | for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) { |
Robert Richter | 1b54950 | 2011-12-07 10:02:53 +0100 | [diff] [blame] | 474 | struct machine *pos = rb_entry(nd, struct machine, rb_node); |
| 475 | ret |= machine__read_build_ids(pos, with_hits); |
| 476 | } |
| 477 | |
| 478 | return ret; |
| 479 | } |
| 480 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 481 | static int write_tracing_data(int fd, struct perf_header *h __maybe_unused, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 482 | struct perf_evlist *evlist) |
| 483 | { |
| 484 | return read_tracing_data(fd, &evlist->entries); |
| 485 | } |
| 486 | |
| 487 | |
| 488 | static int write_build_id(int fd, struct perf_header *h, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 489 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 490 | { |
| 491 | struct perf_session *session; |
| 492 | int err; |
| 493 | |
| 494 | session = container_of(h, struct perf_session, header); |
| 495 | |
Robert Richter | e20960c | 2011-12-07 10:02:55 +0100 | [diff] [blame] | 496 | if (!perf_session__read_build_ids(session, true)) |
| 497 | return -1; |
| 498 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 499 | err = dsos__write_buildid_table(h, fd); |
| 500 | if (err < 0) { |
| 501 | pr_debug("failed to write buildid table\n"); |
| 502 | return err; |
| 503 | } |
| 504 | if (!no_buildid_cache) |
| 505 | perf_session__cache_build_ids(session); |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 510 | static int write_hostname(int fd, struct perf_header *h __maybe_unused, |
| 511 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 512 | { |
| 513 | struct utsname uts; |
| 514 | int ret; |
| 515 | |
| 516 | ret = uname(&uts); |
| 517 | if (ret < 0) |
| 518 | return -1; |
| 519 | |
| 520 | return do_write_string(fd, uts.nodename); |
| 521 | } |
| 522 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 523 | static int write_osrelease(int fd, struct perf_header *h __maybe_unused, |
| 524 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 525 | { |
| 526 | struct utsname uts; |
| 527 | int ret; |
| 528 | |
| 529 | ret = uname(&uts); |
| 530 | if (ret < 0) |
| 531 | return -1; |
| 532 | |
| 533 | return do_write_string(fd, uts.release); |
| 534 | } |
| 535 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 536 | static int write_arch(int fd, struct perf_header *h __maybe_unused, |
| 537 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 538 | { |
| 539 | struct utsname uts; |
| 540 | int ret; |
| 541 | |
| 542 | ret = uname(&uts); |
| 543 | if (ret < 0) |
| 544 | return -1; |
| 545 | |
| 546 | return do_write_string(fd, uts.machine); |
| 547 | } |
| 548 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 549 | static int write_version(int fd, struct perf_header *h __maybe_unused, |
| 550 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 551 | { |
| 552 | return do_write_string(fd, perf_version_string); |
| 553 | } |
| 554 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 555 | static int write_cpudesc(int fd, struct perf_header *h __maybe_unused, |
| 556 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 557 | { |
| 558 | #ifndef CPUINFO_PROC |
| 559 | #define CPUINFO_PROC NULL |
| 560 | #endif |
| 561 | FILE *file; |
| 562 | char *buf = NULL; |
| 563 | char *s, *p; |
| 564 | const char *search = CPUINFO_PROC; |
| 565 | size_t len = 0; |
| 566 | int ret = -1; |
| 567 | |
| 568 | if (!search) |
| 569 | return -1; |
| 570 | |
| 571 | file = fopen("/proc/cpuinfo", "r"); |
| 572 | if (!file) |
| 573 | return -1; |
| 574 | |
| 575 | while (getline(&buf, &len, file) > 0) { |
| 576 | ret = strncmp(buf, search, strlen(search)); |
| 577 | if (!ret) |
| 578 | break; |
| 579 | } |
| 580 | |
| 581 | if (ret) |
| 582 | goto done; |
| 583 | |
| 584 | s = buf; |
| 585 | |
| 586 | p = strchr(buf, ':'); |
| 587 | if (p && *(p+1) == ' ' && *(p+2)) |
| 588 | s = p + 2; |
| 589 | p = strchr(s, '\n'); |
| 590 | if (p) |
| 591 | *p = '\0'; |
| 592 | |
| 593 | /* squash extra space characters (branding string) */ |
| 594 | p = s; |
| 595 | while (*p) { |
| 596 | if (isspace(*p)) { |
| 597 | char *r = p + 1; |
| 598 | char *q = r; |
| 599 | *p = ' '; |
| 600 | while (*q && isspace(*q)) |
| 601 | q++; |
| 602 | if (q != (p+1)) |
| 603 | while ((*r++ = *q++)); |
| 604 | } |
| 605 | p++; |
| 606 | } |
| 607 | ret = do_write_string(fd, s); |
| 608 | done: |
| 609 | free(buf); |
| 610 | fclose(file); |
| 611 | return ret; |
| 612 | } |
| 613 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 614 | static int write_nrcpus(int fd, struct perf_header *h __maybe_unused, |
| 615 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 616 | { |
| 617 | long nr; |
| 618 | u32 nrc, nra; |
| 619 | int ret; |
| 620 | |
| 621 | nr = sysconf(_SC_NPROCESSORS_CONF); |
| 622 | if (nr < 0) |
| 623 | return -1; |
| 624 | |
| 625 | nrc = (u32)(nr & UINT_MAX); |
| 626 | |
| 627 | nr = sysconf(_SC_NPROCESSORS_ONLN); |
| 628 | if (nr < 0) |
| 629 | return -1; |
| 630 | |
| 631 | nra = (u32)(nr & UINT_MAX); |
| 632 | |
| 633 | ret = do_write(fd, &nrc, sizeof(nrc)); |
| 634 | if (ret < 0) |
| 635 | return ret; |
| 636 | |
| 637 | return do_write(fd, &nra, sizeof(nra)); |
| 638 | } |
| 639 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 640 | static int write_event_desc(int fd, struct perf_header *h __maybe_unused, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 641 | struct perf_evlist *evlist) |
| 642 | { |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 643 | struct perf_evsel *evsel; |
Namhyung Kim | 74ba9e1 | 2012-09-05 14:02:47 +0900 | [diff] [blame] | 644 | u32 nre, nri, sz; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 645 | int ret; |
| 646 | |
Namhyung Kim | 74ba9e1 | 2012-09-05 14:02:47 +0900 | [diff] [blame] | 647 | nre = evlist->nr_entries; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 648 | |
| 649 | /* |
| 650 | * write number of events |
| 651 | */ |
| 652 | ret = do_write(fd, &nre, sizeof(nre)); |
| 653 | if (ret < 0) |
| 654 | return ret; |
| 655 | |
| 656 | /* |
| 657 | * size of perf_event_attr struct |
| 658 | */ |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 659 | sz = (u32)sizeof(evsel->attr); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 660 | ret = do_write(fd, &sz, sizeof(sz)); |
| 661 | if (ret < 0) |
| 662 | return ret; |
| 663 | |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 664 | list_for_each_entry(evsel, &evlist->entries, node) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 665 | |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 666 | ret = do_write(fd, &evsel->attr, sz); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 667 | if (ret < 0) |
| 668 | return ret; |
| 669 | /* |
| 670 | * write number of unique id per event |
| 671 | * there is one id per instance of an event |
| 672 | * |
| 673 | * copy into an nri to be independent of the |
| 674 | * type of ids, |
| 675 | */ |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 676 | nri = evsel->ids; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 677 | ret = do_write(fd, &nri, sizeof(nri)); |
| 678 | if (ret < 0) |
| 679 | return ret; |
| 680 | |
| 681 | /* |
| 682 | * write event string as passed on cmdline |
| 683 | */ |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 684 | ret = do_write_string(fd, perf_evsel__name(evsel)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 685 | if (ret < 0) |
| 686 | return ret; |
| 687 | /* |
| 688 | * write unique ids for this event |
| 689 | */ |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 690 | ret = do_write(fd, evsel->id, evsel->ids * sizeof(u64)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 691 | if (ret < 0) |
| 692 | return ret; |
| 693 | } |
| 694 | return 0; |
| 695 | } |
| 696 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 697 | static int write_cmdline(int fd, struct perf_header *h __maybe_unused, |
| 698 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 699 | { |
| 700 | char buf[MAXPATHLEN]; |
| 701 | char proc[32]; |
| 702 | u32 i, n; |
| 703 | int ret; |
| 704 | |
| 705 | /* |
| 706 | * actual atual path to perf binary |
| 707 | */ |
| 708 | sprintf(proc, "/proc/%d/exe", getpid()); |
| 709 | ret = readlink(proc, buf, sizeof(buf)); |
| 710 | if (ret <= 0) |
| 711 | return -1; |
| 712 | |
| 713 | /* readlink() does not add null termination */ |
| 714 | buf[ret] = '\0'; |
| 715 | |
| 716 | /* account for binary path */ |
| 717 | n = header_argc + 1; |
| 718 | |
| 719 | ret = do_write(fd, &n, sizeof(n)); |
| 720 | if (ret < 0) |
| 721 | return ret; |
| 722 | |
| 723 | ret = do_write_string(fd, buf); |
| 724 | if (ret < 0) |
| 725 | return ret; |
| 726 | |
| 727 | for (i = 0 ; i < header_argc; i++) { |
| 728 | ret = do_write_string(fd, header_argv[i]); |
| 729 | if (ret < 0) |
| 730 | return ret; |
| 731 | } |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | #define CORE_SIB_FMT \ |
| 736 | "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list" |
| 737 | #define THRD_SIB_FMT \ |
| 738 | "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list" |
| 739 | |
| 740 | struct cpu_topo { |
| 741 | u32 core_sib; |
| 742 | u32 thread_sib; |
| 743 | char **core_siblings; |
| 744 | char **thread_siblings; |
| 745 | }; |
| 746 | |
| 747 | static int build_cpu_topo(struct cpu_topo *tp, int cpu) |
| 748 | { |
| 749 | FILE *fp; |
| 750 | char filename[MAXPATHLEN]; |
| 751 | char *buf = NULL, *p; |
| 752 | size_t len = 0; |
| 753 | u32 i = 0; |
| 754 | int ret = -1; |
| 755 | |
| 756 | sprintf(filename, CORE_SIB_FMT, cpu); |
| 757 | fp = fopen(filename, "r"); |
| 758 | if (!fp) |
| 759 | return -1; |
| 760 | |
| 761 | if (getline(&buf, &len, fp) <= 0) |
| 762 | goto done; |
| 763 | |
| 764 | fclose(fp); |
| 765 | |
| 766 | p = strchr(buf, '\n'); |
| 767 | if (p) |
| 768 | *p = '\0'; |
| 769 | |
| 770 | for (i = 0; i < tp->core_sib; i++) { |
| 771 | if (!strcmp(buf, tp->core_siblings[i])) |
| 772 | break; |
| 773 | } |
| 774 | if (i == tp->core_sib) { |
| 775 | tp->core_siblings[i] = buf; |
| 776 | tp->core_sib++; |
| 777 | buf = NULL; |
| 778 | len = 0; |
| 779 | } |
| 780 | |
| 781 | sprintf(filename, THRD_SIB_FMT, cpu); |
| 782 | fp = fopen(filename, "r"); |
| 783 | if (!fp) |
| 784 | goto done; |
| 785 | |
| 786 | if (getline(&buf, &len, fp) <= 0) |
| 787 | goto done; |
| 788 | |
| 789 | p = strchr(buf, '\n'); |
| 790 | if (p) |
| 791 | *p = '\0'; |
| 792 | |
| 793 | for (i = 0; i < tp->thread_sib; i++) { |
| 794 | if (!strcmp(buf, tp->thread_siblings[i])) |
| 795 | break; |
| 796 | } |
| 797 | if (i == tp->thread_sib) { |
| 798 | tp->thread_siblings[i] = buf; |
| 799 | tp->thread_sib++; |
| 800 | buf = NULL; |
| 801 | } |
| 802 | ret = 0; |
| 803 | done: |
| 804 | if(fp) |
| 805 | fclose(fp); |
| 806 | free(buf); |
| 807 | return ret; |
| 808 | } |
| 809 | |
| 810 | static void free_cpu_topo(struct cpu_topo *tp) |
| 811 | { |
| 812 | u32 i; |
| 813 | |
| 814 | if (!tp) |
| 815 | return; |
| 816 | |
| 817 | for (i = 0 ; i < tp->core_sib; i++) |
| 818 | free(tp->core_siblings[i]); |
| 819 | |
| 820 | for (i = 0 ; i < tp->thread_sib; i++) |
| 821 | free(tp->thread_siblings[i]); |
| 822 | |
| 823 | free(tp); |
| 824 | } |
| 825 | |
| 826 | static struct cpu_topo *build_cpu_topology(void) |
| 827 | { |
| 828 | struct cpu_topo *tp; |
| 829 | void *addr; |
| 830 | u32 nr, i; |
| 831 | size_t sz; |
| 832 | long ncpus; |
| 833 | int ret = -1; |
| 834 | |
| 835 | ncpus = sysconf(_SC_NPROCESSORS_CONF); |
| 836 | if (ncpus < 0) |
| 837 | return NULL; |
| 838 | |
| 839 | nr = (u32)(ncpus & UINT_MAX); |
| 840 | |
| 841 | sz = nr * sizeof(char *); |
| 842 | |
| 843 | addr = calloc(1, sizeof(*tp) + 2 * sz); |
| 844 | if (!addr) |
| 845 | return NULL; |
| 846 | |
| 847 | tp = addr; |
| 848 | |
| 849 | addr += sizeof(*tp); |
| 850 | tp->core_siblings = addr; |
| 851 | addr += sz; |
| 852 | tp->thread_siblings = addr; |
| 853 | |
| 854 | for (i = 0; i < nr; i++) { |
| 855 | ret = build_cpu_topo(tp, i); |
| 856 | if (ret < 0) |
| 857 | break; |
| 858 | } |
| 859 | if (ret) { |
| 860 | free_cpu_topo(tp); |
| 861 | tp = NULL; |
| 862 | } |
| 863 | return tp; |
| 864 | } |
| 865 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 866 | static int write_cpu_topology(int fd, struct perf_header *h __maybe_unused, |
| 867 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 868 | { |
| 869 | struct cpu_topo *tp; |
| 870 | u32 i; |
| 871 | int ret; |
| 872 | |
| 873 | tp = build_cpu_topology(); |
| 874 | if (!tp) |
| 875 | return -1; |
| 876 | |
| 877 | ret = do_write(fd, &tp->core_sib, sizeof(tp->core_sib)); |
| 878 | if (ret < 0) |
| 879 | goto done; |
| 880 | |
| 881 | for (i = 0; i < tp->core_sib; i++) { |
| 882 | ret = do_write_string(fd, tp->core_siblings[i]); |
| 883 | if (ret < 0) |
| 884 | goto done; |
| 885 | } |
| 886 | ret = do_write(fd, &tp->thread_sib, sizeof(tp->thread_sib)); |
| 887 | if (ret < 0) |
| 888 | goto done; |
| 889 | |
| 890 | for (i = 0; i < tp->thread_sib; i++) { |
| 891 | ret = do_write_string(fd, tp->thread_siblings[i]); |
| 892 | if (ret < 0) |
| 893 | break; |
| 894 | } |
| 895 | done: |
| 896 | free_cpu_topo(tp); |
| 897 | return ret; |
| 898 | } |
| 899 | |
| 900 | |
| 901 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 902 | static int write_total_mem(int fd, struct perf_header *h __maybe_unused, |
| 903 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 904 | { |
| 905 | char *buf = NULL; |
| 906 | FILE *fp; |
| 907 | size_t len = 0; |
| 908 | int ret = -1, n; |
| 909 | uint64_t mem; |
| 910 | |
| 911 | fp = fopen("/proc/meminfo", "r"); |
| 912 | if (!fp) |
| 913 | return -1; |
| 914 | |
| 915 | while (getline(&buf, &len, fp) > 0) { |
| 916 | ret = strncmp(buf, "MemTotal:", 9); |
| 917 | if (!ret) |
| 918 | break; |
| 919 | } |
| 920 | if (!ret) { |
| 921 | n = sscanf(buf, "%*s %"PRIu64, &mem); |
| 922 | if (n == 1) |
| 923 | ret = do_write(fd, &mem, sizeof(mem)); |
| 924 | } |
| 925 | free(buf); |
| 926 | fclose(fp); |
| 927 | return ret; |
| 928 | } |
| 929 | |
| 930 | static int write_topo_node(int fd, int node) |
| 931 | { |
| 932 | char str[MAXPATHLEN]; |
| 933 | char field[32]; |
| 934 | char *buf = NULL, *p; |
| 935 | size_t len = 0; |
| 936 | FILE *fp; |
| 937 | u64 mem_total, mem_free, mem; |
| 938 | int ret = -1; |
| 939 | |
| 940 | sprintf(str, "/sys/devices/system/node/node%d/meminfo", node); |
| 941 | fp = fopen(str, "r"); |
| 942 | if (!fp) |
| 943 | return -1; |
| 944 | |
| 945 | while (getline(&buf, &len, fp) > 0) { |
| 946 | /* skip over invalid lines */ |
| 947 | if (!strchr(buf, ':')) |
| 948 | continue; |
| 949 | if (sscanf(buf, "%*s %*d %s %"PRIu64, field, &mem) != 2) |
| 950 | goto done; |
| 951 | if (!strcmp(field, "MemTotal:")) |
| 952 | mem_total = mem; |
| 953 | if (!strcmp(field, "MemFree:")) |
| 954 | mem_free = mem; |
| 955 | } |
| 956 | |
| 957 | fclose(fp); |
| 958 | |
| 959 | ret = do_write(fd, &mem_total, sizeof(u64)); |
| 960 | if (ret) |
| 961 | goto done; |
| 962 | |
| 963 | ret = do_write(fd, &mem_free, sizeof(u64)); |
| 964 | if (ret) |
| 965 | goto done; |
| 966 | |
| 967 | ret = -1; |
| 968 | sprintf(str, "/sys/devices/system/node/node%d/cpulist", node); |
| 969 | |
| 970 | fp = fopen(str, "r"); |
| 971 | if (!fp) |
| 972 | goto done; |
| 973 | |
| 974 | if (getline(&buf, &len, fp) <= 0) |
| 975 | goto done; |
| 976 | |
| 977 | p = strchr(buf, '\n'); |
| 978 | if (p) |
| 979 | *p = '\0'; |
| 980 | |
| 981 | ret = do_write_string(fd, buf); |
| 982 | done: |
| 983 | free(buf); |
| 984 | fclose(fp); |
| 985 | return ret; |
| 986 | } |
| 987 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 988 | static int write_numa_topology(int fd, struct perf_header *h __maybe_unused, |
| 989 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 990 | { |
| 991 | char *buf = NULL; |
| 992 | size_t len = 0; |
| 993 | FILE *fp; |
| 994 | struct cpu_map *node_map = NULL; |
| 995 | char *c; |
| 996 | u32 nr, i, j; |
| 997 | int ret = -1; |
| 998 | |
| 999 | fp = fopen("/sys/devices/system/node/online", "r"); |
| 1000 | if (!fp) |
| 1001 | return -1; |
| 1002 | |
| 1003 | if (getline(&buf, &len, fp) <= 0) |
| 1004 | goto done; |
| 1005 | |
| 1006 | c = strchr(buf, '\n'); |
| 1007 | if (c) |
| 1008 | *c = '\0'; |
| 1009 | |
| 1010 | node_map = cpu_map__new(buf); |
| 1011 | if (!node_map) |
| 1012 | goto done; |
| 1013 | |
| 1014 | nr = (u32)node_map->nr; |
| 1015 | |
| 1016 | ret = do_write(fd, &nr, sizeof(nr)); |
| 1017 | if (ret < 0) |
| 1018 | goto done; |
| 1019 | |
| 1020 | for (i = 0; i < nr; i++) { |
| 1021 | j = (u32)node_map->map[i]; |
| 1022 | ret = do_write(fd, &j, sizeof(j)); |
| 1023 | if (ret < 0) |
| 1024 | break; |
| 1025 | |
| 1026 | ret = write_topo_node(fd, i); |
| 1027 | if (ret < 0) |
| 1028 | break; |
| 1029 | } |
| 1030 | done: |
| 1031 | free(buf); |
| 1032 | fclose(fp); |
| 1033 | free(node_map); |
| 1034 | return ret; |
| 1035 | } |
| 1036 | |
| 1037 | /* |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1038 | * File format: |
| 1039 | * |
| 1040 | * struct pmu_mappings { |
| 1041 | * u32 pmu_num; |
| 1042 | * struct pmu_map { |
| 1043 | * u32 type; |
| 1044 | * char name[]; |
| 1045 | * }[pmu_num]; |
| 1046 | * }; |
| 1047 | */ |
| 1048 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1049 | static int write_pmu_mappings(int fd, struct perf_header *h __maybe_unused, |
| 1050 | struct perf_evlist *evlist __maybe_unused) |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1051 | { |
| 1052 | struct perf_pmu *pmu = NULL; |
| 1053 | off_t offset = lseek(fd, 0, SEEK_CUR); |
| 1054 | __u32 pmu_num = 0; |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1055 | int ret; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1056 | |
| 1057 | /* write real pmu_num later */ |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1058 | ret = do_write(fd, &pmu_num, sizeof(pmu_num)); |
| 1059 | if (ret < 0) |
| 1060 | return ret; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1061 | |
| 1062 | while ((pmu = perf_pmu__scan(pmu))) { |
| 1063 | if (!pmu->name) |
| 1064 | continue; |
| 1065 | pmu_num++; |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1066 | |
| 1067 | ret = do_write(fd, &pmu->type, sizeof(pmu->type)); |
| 1068 | if (ret < 0) |
| 1069 | return ret; |
| 1070 | |
| 1071 | ret = do_write_string(fd, pmu->name); |
| 1072 | if (ret < 0) |
| 1073 | return ret; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | if (pwrite(fd, &pmu_num, sizeof(pmu_num), offset) != sizeof(pmu_num)) { |
| 1077 | /* discard all */ |
| 1078 | lseek(fd, offset, SEEK_SET); |
| 1079 | return -1; |
| 1080 | } |
| 1081 | |
| 1082 | return 0; |
| 1083 | } |
| 1084 | |
| 1085 | /* |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1086 | * default get_cpuid(): nothing gets recorded |
| 1087 | * actual implementation must be in arch/$(ARCH)/util/header.c |
| 1088 | */ |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1089 | int __attribute__ ((weak)) get_cpuid(char *buffer __maybe_unused, |
| 1090 | size_t sz __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1091 | { |
| 1092 | return -1; |
| 1093 | } |
| 1094 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1095 | static int write_cpuid(int fd, struct perf_header *h __maybe_unused, |
| 1096 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1097 | { |
| 1098 | char buffer[64]; |
| 1099 | int ret; |
| 1100 | |
| 1101 | ret = get_cpuid(buffer, sizeof(buffer)); |
| 1102 | if (!ret) |
| 1103 | goto write_it; |
| 1104 | |
| 1105 | return -1; |
| 1106 | write_it: |
| 1107 | return do_write_string(fd, buffer); |
| 1108 | } |
| 1109 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1110 | static int write_branch_stack(int fd __maybe_unused, |
| 1111 | struct perf_header *h __maybe_unused, |
| 1112 | struct perf_evlist *evlist __maybe_unused) |
Stephane Eranian | 330aa67 | 2012-03-08 23:47:46 +0100 | [diff] [blame] | 1113 | { |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1117 | static void print_hostname(struct perf_header *ph, int fd __maybe_unused, |
| 1118 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1119 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1120 | fprintf(fp, "# hostname : %s\n", ph->env.hostname); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1121 | } |
| 1122 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1123 | static void print_osrelease(struct perf_header *ph, int fd __maybe_unused, |
| 1124 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1125 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1126 | fprintf(fp, "# os release : %s\n", ph->env.os_release); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1127 | } |
| 1128 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1129 | static void print_arch(struct perf_header *ph, int fd __maybe_unused, FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1130 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1131 | fprintf(fp, "# arch : %s\n", ph->env.arch); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1132 | } |
| 1133 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1134 | static void print_cpudesc(struct perf_header *ph, int fd __maybe_unused, |
| 1135 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1136 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1137 | fprintf(fp, "# cpudesc : %s\n", ph->env.cpu_desc); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1138 | } |
| 1139 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1140 | static void print_nrcpus(struct perf_header *ph, int fd __maybe_unused, |
| 1141 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1142 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1143 | fprintf(fp, "# nrcpus online : %u\n", ph->env.nr_cpus_online); |
| 1144 | fprintf(fp, "# nrcpus avail : %u\n", ph->env.nr_cpus_avail); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1145 | } |
| 1146 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1147 | static void print_version(struct perf_header *ph, int fd __maybe_unused, |
| 1148 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1149 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1150 | fprintf(fp, "# perf version : %s\n", ph->env.version); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1151 | } |
| 1152 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1153 | static void print_cmdline(struct perf_header *ph, int fd __maybe_unused, |
| 1154 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1155 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1156 | int nr, i; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1157 | char *str; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1158 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1159 | nr = ph->env.nr_cmdline; |
| 1160 | str = ph->env.cmdline; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1161 | |
| 1162 | fprintf(fp, "# cmdline : "); |
| 1163 | |
| 1164 | for (i = 0; i < nr; i++) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1165 | fprintf(fp, "%s ", str); |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1166 | str += strlen(str) + 1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1167 | } |
| 1168 | fputc('\n', fp); |
| 1169 | } |
| 1170 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1171 | static void print_cpu_topology(struct perf_header *ph, int fd __maybe_unused, |
| 1172 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1173 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1174 | int nr, i; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1175 | char *str; |
| 1176 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1177 | nr = ph->env.nr_sibling_cores; |
| 1178 | str = ph->env.sibling_cores; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1179 | |
| 1180 | for (i = 0; i < nr; i++) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1181 | fprintf(fp, "# sibling cores : %s\n", str); |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1182 | str += strlen(str) + 1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1183 | } |
| 1184 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1185 | nr = ph->env.nr_sibling_threads; |
| 1186 | str = ph->env.sibling_threads; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1187 | |
| 1188 | for (i = 0; i < nr; i++) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1189 | fprintf(fp, "# sibling threads : %s\n", str); |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1190 | str += strlen(str) + 1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1194 | static void free_event_desc(struct perf_evsel *events) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1195 | { |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1196 | struct perf_evsel *evsel; |
| 1197 | |
| 1198 | if (!events) |
| 1199 | return; |
| 1200 | |
| 1201 | for (evsel = events; evsel->attr.size; evsel++) { |
| 1202 | if (evsel->name) |
| 1203 | free(evsel->name); |
| 1204 | if (evsel->id) |
| 1205 | free(evsel->id); |
| 1206 | } |
| 1207 | |
| 1208 | free(events); |
| 1209 | } |
| 1210 | |
| 1211 | static struct perf_evsel * |
| 1212 | read_event_desc(struct perf_header *ph, int fd) |
| 1213 | { |
| 1214 | struct perf_evsel *evsel, *events = NULL; |
| 1215 | u64 *id; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1216 | void *buf = NULL; |
Stephane Eranian | 62db906 | 2012-02-09 23:21:07 +0100 | [diff] [blame] | 1217 | u32 nre, sz, nr, i, j; |
| 1218 | ssize_t ret; |
| 1219 | size_t msz; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1220 | |
| 1221 | /* number of events */ |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1222 | ret = readn(fd, &nre, sizeof(nre)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1223 | if (ret != (ssize_t)sizeof(nre)) |
| 1224 | goto error; |
| 1225 | |
| 1226 | if (ph->needs_swap) |
| 1227 | nre = bswap_32(nre); |
| 1228 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1229 | ret = readn(fd, &sz, sizeof(sz)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1230 | if (ret != (ssize_t)sizeof(sz)) |
| 1231 | goto error; |
| 1232 | |
| 1233 | if (ph->needs_swap) |
| 1234 | sz = bswap_32(sz); |
| 1235 | |
Stephane Eranian | 62db906 | 2012-02-09 23:21:07 +0100 | [diff] [blame] | 1236 | /* buffer to hold on file attr struct */ |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1237 | buf = malloc(sz); |
| 1238 | if (!buf) |
| 1239 | goto error; |
| 1240 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1241 | /* the last event terminates with evsel->attr.size == 0: */ |
| 1242 | events = calloc(nre + 1, sizeof(*events)); |
| 1243 | if (!events) |
| 1244 | goto error; |
| 1245 | |
| 1246 | msz = sizeof(evsel->attr); |
Jiri Olsa | 9fafd98 | 2012-03-20 19:15:39 +0100 | [diff] [blame] | 1247 | if (sz < msz) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1248 | msz = sz; |
| 1249 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1250 | for (i = 0, evsel = events; i < nre; evsel++, i++) { |
| 1251 | evsel->idx = i; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1252 | |
Stephane Eranian | 62db906 | 2012-02-09 23:21:07 +0100 | [diff] [blame] | 1253 | /* |
| 1254 | * must read entire on-file attr struct to |
| 1255 | * sync up with layout. |
| 1256 | */ |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1257 | ret = readn(fd, buf, sz); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1258 | if (ret != (ssize_t)sz) |
| 1259 | goto error; |
| 1260 | |
| 1261 | if (ph->needs_swap) |
| 1262 | perf_event__attr_swap(buf); |
| 1263 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1264 | memcpy(&evsel->attr, buf, msz); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1265 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1266 | ret = readn(fd, &nr, sizeof(nr)); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1267 | if (ret != (ssize_t)sizeof(nr)) |
| 1268 | goto error; |
| 1269 | |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1270 | if (ph->needs_swap) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1271 | nr = bswap_32(nr); |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 1272 | evsel->needs_swap = true; |
| 1273 | } |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1274 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1275 | evsel->name = do_read_string(fd, ph); |
| 1276 | |
| 1277 | if (!nr) |
| 1278 | continue; |
| 1279 | |
| 1280 | id = calloc(nr, sizeof(*id)); |
| 1281 | if (!id) |
| 1282 | goto error; |
| 1283 | evsel->ids = nr; |
| 1284 | evsel->id = id; |
| 1285 | |
| 1286 | for (j = 0 ; j < nr; j++) { |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1287 | ret = readn(fd, id, sizeof(*id)); |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1288 | if (ret != (ssize_t)sizeof(*id)) |
| 1289 | goto error; |
| 1290 | if (ph->needs_swap) |
| 1291 | *id = bswap_64(*id); |
| 1292 | id++; |
| 1293 | } |
| 1294 | } |
| 1295 | out: |
| 1296 | if (buf) |
| 1297 | free(buf); |
| 1298 | return events; |
| 1299 | error: |
| 1300 | if (events) |
| 1301 | free_event_desc(events); |
| 1302 | events = NULL; |
| 1303 | goto out; |
| 1304 | } |
| 1305 | |
| 1306 | static void print_event_desc(struct perf_header *ph, int fd, FILE *fp) |
| 1307 | { |
| 1308 | struct perf_evsel *evsel, *events = read_event_desc(ph, fd); |
| 1309 | u32 j; |
| 1310 | u64 *id; |
| 1311 | |
| 1312 | if (!events) { |
| 1313 | fprintf(fp, "# event desc: not available or unable to read\n"); |
| 1314 | return; |
| 1315 | } |
| 1316 | |
| 1317 | for (evsel = events; evsel->attr.size; evsel++) { |
| 1318 | fprintf(fp, "# event : name = %s, ", evsel->name); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1319 | |
| 1320 | fprintf(fp, "type = %d, config = 0x%"PRIx64 |
| 1321 | ", config1 = 0x%"PRIx64", config2 = 0x%"PRIx64, |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1322 | evsel->attr.type, |
| 1323 | (u64)evsel->attr.config, |
| 1324 | (u64)evsel->attr.config1, |
| 1325 | (u64)evsel->attr.config2); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1326 | |
| 1327 | fprintf(fp, ", excl_usr = %d, excl_kern = %d", |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1328 | evsel->attr.exclude_user, |
| 1329 | evsel->attr.exclude_kernel); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1330 | |
David Ahern | 78b961f | 2012-07-20 17:25:52 -0600 | [diff] [blame] | 1331 | fprintf(fp, ", excl_host = %d, excl_guest = %d", |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1332 | evsel->attr.exclude_host, |
| 1333 | evsel->attr.exclude_guest); |
David Ahern | 78b961f | 2012-07-20 17:25:52 -0600 | [diff] [blame] | 1334 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1335 | fprintf(fp, ", precise_ip = %d", evsel->attr.precise_ip); |
David Ahern | 78b961f | 2012-07-20 17:25:52 -0600 | [diff] [blame] | 1336 | |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1337 | if (evsel->ids) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1338 | fprintf(fp, ", id = {"); |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1339 | for (j = 0, id = evsel->id; j < evsel->ids; j++, id++) { |
| 1340 | if (j) |
| 1341 | fputc(',', fp); |
| 1342 | fprintf(fp, " %"PRIu64, *id); |
| 1343 | } |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1344 | fprintf(fp, " }"); |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1345 | } |
| 1346 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1347 | fputc('\n', fp); |
| 1348 | } |
Robert Richter | 4e1b9c6 | 2012-08-16 21:10:22 +0200 | [diff] [blame] | 1349 | |
| 1350 | free_event_desc(events); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1351 | } |
| 1352 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1353 | static void print_total_mem(struct perf_header *ph, int fd __maybe_unused, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1354 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1355 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1356 | fprintf(fp, "# total memory : %Lu kB\n", ph->env.total_mem); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1357 | } |
| 1358 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1359 | static void print_numa_topology(struct perf_header *ph, int fd __maybe_unused, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1360 | FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1361 | { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1362 | u32 nr, c, i; |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1363 | char *str, *tmp; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1364 | uint64_t mem_total, mem_free; |
| 1365 | |
| 1366 | /* nr nodes */ |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1367 | nr = ph->env.nr_numa_nodes; |
| 1368 | str = ph->env.numa_nodes; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1369 | |
| 1370 | for (i = 0; i < nr; i++) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1371 | /* node number */ |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1372 | c = strtoul(str, &tmp, 0); |
| 1373 | if (*tmp != ':') |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1374 | goto error; |
| 1375 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1376 | str = tmp + 1; |
| 1377 | mem_total = strtoull(str, &tmp, 0); |
| 1378 | if (*tmp != ':') |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1379 | goto error; |
| 1380 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1381 | str = tmp + 1; |
| 1382 | mem_free = strtoull(str, &tmp, 0); |
| 1383 | if (*tmp != ':') |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1384 | goto error; |
| 1385 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1386 | fprintf(fp, "# node%u meminfo : total = %"PRIu64" kB," |
| 1387 | " free = %"PRIu64" kB\n", |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1388 | c, mem_total, mem_free); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1389 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1390 | str = tmp + 1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1391 | fprintf(fp, "# node%u cpu list : %s\n", c, str); |
Namhyung Kim | 1234471 | 2012-10-23 22:44:49 +0900 | [diff] [blame] | 1392 | |
| 1393 | str += strlen(str) + 1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1394 | } |
| 1395 | return; |
| 1396 | error: |
| 1397 | fprintf(fp, "# numa topology : not available\n"); |
| 1398 | } |
| 1399 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1400 | static void print_cpuid(struct perf_header *ph, int fd __maybe_unused, FILE *fp) |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1401 | { |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1402 | fprintf(fp, "# cpuid : %s\n", ph->env.cpuid); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1403 | } |
| 1404 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1405 | static void print_branch_stack(struct perf_header *ph __maybe_unused, |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1406 | int fd __maybe_unused, FILE *fp) |
Stephane Eranian | 330aa67 | 2012-03-08 23:47:46 +0100 | [diff] [blame] | 1407 | { |
| 1408 | fprintf(fp, "# contains samples with branch stack\n"); |
| 1409 | } |
| 1410 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1411 | static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused, |
| 1412 | FILE *fp) |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1413 | { |
| 1414 | const char *delimiter = "# pmu mappings: "; |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1415 | char *str, *tmp; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1416 | u32 pmu_num; |
| 1417 | u32 type; |
| 1418 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1419 | pmu_num = ph->env.nr_pmu_mappings; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1420 | if (!pmu_num) { |
| 1421 | fprintf(fp, "# pmu mappings: not available\n"); |
| 1422 | return; |
| 1423 | } |
| 1424 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1425 | str = ph->env.pmu_mappings; |
Namhyung Kim | be4a2de | 2012-09-05 14:02:49 +0900 | [diff] [blame] | 1426 | |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1427 | while (pmu_num) { |
| 1428 | type = strtoul(str, &tmp, 0); |
| 1429 | if (*tmp != ':') |
| 1430 | goto error; |
| 1431 | |
| 1432 | str = tmp + 1; |
| 1433 | fprintf(fp, "%s%s = %" PRIu32, delimiter, str, type); |
| 1434 | |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1435 | delimiter = ", "; |
Namhyung Kim | 7e94cfc | 2012-09-24 17:15:00 +0900 | [diff] [blame] | 1436 | str += strlen(str) + 1; |
| 1437 | pmu_num--; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | fprintf(fp, "\n"); |
| 1441 | |
| 1442 | if (!pmu_num) |
| 1443 | return; |
| 1444 | error: |
| 1445 | fprintf(fp, "# pmu mappings: unable to read\n"); |
| 1446 | } |
| 1447 | |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1448 | static int __event_process_build_id(struct build_id_event *bev, |
| 1449 | char *filename, |
| 1450 | struct perf_session *session) |
| 1451 | { |
| 1452 | int err = -1; |
| 1453 | struct list_head *head; |
| 1454 | struct machine *machine; |
| 1455 | u16 misc; |
| 1456 | struct dso *dso; |
| 1457 | enum dso_kernel_type dso_type; |
| 1458 | |
| 1459 | machine = perf_session__findnew_machine(session, bev->pid); |
| 1460 | if (!machine) |
| 1461 | goto out; |
| 1462 | |
| 1463 | misc = bev->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; |
| 1464 | |
| 1465 | switch (misc) { |
| 1466 | case PERF_RECORD_MISC_KERNEL: |
| 1467 | dso_type = DSO_TYPE_KERNEL; |
| 1468 | head = &machine->kernel_dsos; |
| 1469 | break; |
| 1470 | case PERF_RECORD_MISC_GUEST_KERNEL: |
| 1471 | dso_type = DSO_TYPE_GUEST_KERNEL; |
| 1472 | head = &machine->kernel_dsos; |
| 1473 | break; |
| 1474 | case PERF_RECORD_MISC_USER: |
| 1475 | case PERF_RECORD_MISC_GUEST_USER: |
| 1476 | dso_type = DSO_TYPE_USER; |
| 1477 | head = &machine->user_dsos; |
| 1478 | break; |
| 1479 | default: |
| 1480 | goto out; |
| 1481 | } |
| 1482 | |
| 1483 | dso = __dsos__findnew(head, filename); |
| 1484 | if (dso != NULL) { |
| 1485 | char sbuild_id[BUILD_ID_SIZE * 2 + 1]; |
| 1486 | |
| 1487 | dso__set_build_id(dso, &bev->build_id); |
| 1488 | |
| 1489 | if (filename[0] == '[') |
| 1490 | dso->kernel = dso_type; |
| 1491 | |
| 1492 | build_id__sprintf(dso->build_id, sizeof(dso->build_id), |
| 1493 | sbuild_id); |
| 1494 | pr_debug("build id event received for %s: %s\n", |
| 1495 | dso->long_name, sbuild_id); |
| 1496 | } |
| 1497 | |
| 1498 | err = 0; |
| 1499 | out: |
| 1500 | return err; |
| 1501 | } |
| 1502 | |
| 1503 | static int perf_header__read_build_ids_abi_quirk(struct perf_header *header, |
| 1504 | int input, u64 offset, u64 size) |
| 1505 | { |
| 1506 | struct perf_session *session = container_of(header, struct perf_session, header); |
| 1507 | struct { |
| 1508 | struct perf_event_header header; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 1509 | u8 build_id[PERF_ALIGN(BUILD_ID_SIZE, sizeof(u64))]; |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1510 | char filename[0]; |
| 1511 | } old_bev; |
| 1512 | struct build_id_event bev; |
| 1513 | char filename[PATH_MAX]; |
| 1514 | u64 limit = offset + size; |
| 1515 | |
| 1516 | while (offset < limit) { |
| 1517 | ssize_t len; |
| 1518 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1519 | if (readn(input, &old_bev, sizeof(old_bev)) != sizeof(old_bev)) |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1520 | return -1; |
| 1521 | |
| 1522 | if (header->needs_swap) |
| 1523 | perf_event_header__bswap(&old_bev.header); |
| 1524 | |
| 1525 | len = old_bev.header.size - sizeof(old_bev); |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1526 | if (readn(input, filename, len) != len) |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1527 | return -1; |
| 1528 | |
| 1529 | bev.header = old_bev.header; |
| 1530 | |
| 1531 | /* |
| 1532 | * As the pid is the missing value, we need to fill |
| 1533 | * it properly. The header.misc value give us nice hint. |
| 1534 | */ |
| 1535 | bev.pid = HOST_KERNEL_ID; |
| 1536 | if (bev.header.misc == PERF_RECORD_MISC_GUEST_USER || |
| 1537 | bev.header.misc == PERF_RECORD_MISC_GUEST_KERNEL) |
| 1538 | bev.pid = DEFAULT_GUEST_KERNEL_ID; |
| 1539 | |
| 1540 | memcpy(bev.build_id, old_bev.build_id, sizeof(bev.build_id)); |
| 1541 | __event_process_build_id(&bev, filename, session); |
| 1542 | |
| 1543 | offset += bev.header.size; |
| 1544 | } |
| 1545 | |
| 1546 | return 0; |
| 1547 | } |
| 1548 | |
| 1549 | static int perf_header__read_build_ids(struct perf_header *header, |
| 1550 | int input, u64 offset, u64 size) |
| 1551 | { |
| 1552 | struct perf_session *session = container_of(header, struct perf_session, header); |
| 1553 | struct build_id_event bev; |
| 1554 | char filename[PATH_MAX]; |
| 1555 | u64 limit = offset + size, orig_offset = offset; |
| 1556 | int err = -1; |
| 1557 | |
| 1558 | while (offset < limit) { |
| 1559 | ssize_t len; |
| 1560 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1561 | if (readn(input, &bev, sizeof(bev)) != sizeof(bev)) |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1562 | goto out; |
| 1563 | |
| 1564 | if (header->needs_swap) |
| 1565 | perf_event_header__bswap(&bev.header); |
| 1566 | |
| 1567 | len = bev.header.size - sizeof(bev); |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1568 | if (readn(input, filename, len) != len) |
Robert Richter | 08d95bd | 2012-02-10 15:41:55 +0100 | [diff] [blame] | 1569 | goto out; |
| 1570 | /* |
| 1571 | * The a1645ce1 changeset: |
| 1572 | * |
| 1573 | * "perf: 'perf kvm' tool for monitoring guest performance from host" |
| 1574 | * |
| 1575 | * Added a field to struct build_id_event that broke the file |
| 1576 | * format. |
| 1577 | * |
| 1578 | * Since the kernel build-id is the first entry, process the |
| 1579 | * table using the old format if the well known |
| 1580 | * '[kernel.kallsyms]' string for the kernel build-id has the |
| 1581 | * first 4 characters chopped off (where the pid_t sits). |
| 1582 | */ |
| 1583 | if (memcmp(filename, "nel.kallsyms]", 13) == 0) { |
| 1584 | if (lseek(input, orig_offset, SEEK_SET) == (off_t)-1) |
| 1585 | return -1; |
| 1586 | return perf_header__read_build_ids_abi_quirk(header, input, offset, size); |
| 1587 | } |
| 1588 | |
| 1589 | __event_process_build_id(&bev, filename, session); |
| 1590 | |
| 1591 | offset += bev.header.size; |
| 1592 | } |
| 1593 | err = 0; |
| 1594 | out: |
| 1595 | return err; |
| 1596 | } |
| 1597 | |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1598 | static int process_tracing_data(struct perf_file_section *section __maybe_unused, |
| 1599 | struct perf_header *ph __maybe_unused, |
| 1600 | int fd, void *data) |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1601 | { |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 1602 | trace_report(fd, data, false); |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1603 | return 0; |
| 1604 | } |
| 1605 | |
| 1606 | static int process_build_id(struct perf_file_section *section, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1607 | struct perf_header *ph, int fd, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1608 | void *data __maybe_unused) |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1609 | { |
| 1610 | if (perf_header__read_build_ids(ph, fd, section->offset, section->size)) |
| 1611 | pr_debug("Failed to read buildids, continuing...\n"); |
| 1612 | return 0; |
| 1613 | } |
| 1614 | |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1615 | static int process_hostname(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1616 | struct perf_header *ph, int fd, |
| 1617 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1618 | { |
| 1619 | ph->env.hostname = do_read_string(fd, ph); |
| 1620 | return ph->env.hostname ? 0 : -ENOMEM; |
| 1621 | } |
| 1622 | |
| 1623 | static int process_osrelease(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1624 | struct perf_header *ph, int fd, |
| 1625 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1626 | { |
| 1627 | ph->env.os_release = do_read_string(fd, ph); |
| 1628 | return ph->env.os_release ? 0 : -ENOMEM; |
| 1629 | } |
| 1630 | |
| 1631 | static int process_version(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1632 | struct perf_header *ph, int fd, |
| 1633 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1634 | { |
| 1635 | ph->env.version = do_read_string(fd, ph); |
| 1636 | return ph->env.version ? 0 : -ENOMEM; |
| 1637 | } |
| 1638 | |
| 1639 | static int process_arch(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1640 | struct perf_header *ph, int fd, |
| 1641 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1642 | { |
| 1643 | ph->env.arch = do_read_string(fd, ph); |
| 1644 | return ph->env.arch ? 0 : -ENOMEM; |
| 1645 | } |
| 1646 | |
| 1647 | static int process_nrcpus(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1648 | struct perf_header *ph, int fd, |
| 1649 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1650 | { |
| 1651 | size_t ret; |
| 1652 | u32 nr; |
| 1653 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1654 | ret = readn(fd, &nr, sizeof(nr)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1655 | if (ret != sizeof(nr)) |
| 1656 | return -1; |
| 1657 | |
| 1658 | if (ph->needs_swap) |
| 1659 | nr = bswap_32(nr); |
| 1660 | |
| 1661 | ph->env.nr_cpus_online = nr; |
| 1662 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1663 | ret = readn(fd, &nr, sizeof(nr)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1664 | if (ret != sizeof(nr)) |
| 1665 | return -1; |
| 1666 | |
| 1667 | if (ph->needs_swap) |
| 1668 | nr = bswap_32(nr); |
| 1669 | |
| 1670 | ph->env.nr_cpus_avail = nr; |
| 1671 | return 0; |
| 1672 | } |
| 1673 | |
| 1674 | static int process_cpudesc(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1675 | struct perf_header *ph, int fd, |
| 1676 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1677 | { |
| 1678 | ph->env.cpu_desc = do_read_string(fd, ph); |
| 1679 | return ph->env.cpu_desc ? 0 : -ENOMEM; |
| 1680 | } |
| 1681 | |
| 1682 | static int process_cpuid(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1683 | struct perf_header *ph, int fd, |
| 1684 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1685 | { |
| 1686 | ph->env.cpuid = do_read_string(fd, ph); |
| 1687 | return ph->env.cpuid ? 0 : -ENOMEM; |
| 1688 | } |
| 1689 | |
| 1690 | static int process_total_mem(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1691 | struct perf_header *ph, int fd, |
| 1692 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1693 | { |
| 1694 | uint64_t mem; |
| 1695 | size_t ret; |
| 1696 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1697 | ret = readn(fd, &mem, sizeof(mem)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1698 | if (ret != sizeof(mem)) |
| 1699 | return -1; |
| 1700 | |
| 1701 | if (ph->needs_swap) |
| 1702 | mem = bswap_64(mem); |
| 1703 | |
| 1704 | ph->env.total_mem = mem; |
| 1705 | return 0; |
| 1706 | } |
| 1707 | |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 1708 | static struct perf_evsel * |
| 1709 | perf_evlist__find_by_index(struct perf_evlist *evlist, int idx) |
| 1710 | { |
| 1711 | struct perf_evsel *evsel; |
| 1712 | |
| 1713 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 1714 | if (evsel->idx == idx) |
| 1715 | return evsel; |
| 1716 | } |
| 1717 | |
| 1718 | return NULL; |
| 1719 | } |
| 1720 | |
| 1721 | static void |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1722 | perf_evlist__set_event_name(struct perf_evlist *evlist, |
| 1723 | struct perf_evsel *event) |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 1724 | { |
| 1725 | struct perf_evsel *evsel; |
| 1726 | |
| 1727 | if (!event->name) |
| 1728 | return; |
| 1729 | |
| 1730 | evsel = perf_evlist__find_by_index(evlist, event->idx); |
| 1731 | if (!evsel) |
| 1732 | return; |
| 1733 | |
| 1734 | if (evsel->name) |
| 1735 | return; |
| 1736 | |
| 1737 | evsel->name = strdup(event->name); |
| 1738 | } |
| 1739 | |
| 1740 | static int |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1741 | process_event_desc(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1742 | struct perf_header *header, int fd, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1743 | void *data __maybe_unused) |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 1744 | { |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1745 | struct perf_session *session; |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 1746 | struct perf_evsel *evsel, *events = read_event_desc(header, fd); |
| 1747 | |
| 1748 | if (!events) |
| 1749 | return 0; |
| 1750 | |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1751 | session = container_of(header, struct perf_session, header); |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 1752 | for (evsel = events; evsel->attr.size; evsel++) |
| 1753 | perf_evlist__set_event_name(session->evlist, evsel); |
| 1754 | |
| 1755 | free_event_desc(events); |
| 1756 | |
| 1757 | return 0; |
| 1758 | } |
| 1759 | |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1760 | static int process_cmdline(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1761 | struct perf_header *ph, int fd, |
| 1762 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1763 | { |
| 1764 | size_t ret; |
| 1765 | char *str; |
| 1766 | u32 nr, i; |
| 1767 | struct strbuf sb; |
| 1768 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1769 | ret = readn(fd, &nr, sizeof(nr)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1770 | if (ret != sizeof(nr)) |
| 1771 | return -1; |
| 1772 | |
| 1773 | if (ph->needs_swap) |
| 1774 | nr = bswap_32(nr); |
| 1775 | |
| 1776 | ph->env.nr_cmdline = nr; |
| 1777 | strbuf_init(&sb, 128); |
| 1778 | |
| 1779 | for (i = 0; i < nr; i++) { |
| 1780 | str = do_read_string(fd, ph); |
| 1781 | if (!str) |
| 1782 | goto error; |
| 1783 | |
| 1784 | /* include a NULL character at the end */ |
| 1785 | strbuf_add(&sb, str, strlen(str) + 1); |
| 1786 | free(str); |
| 1787 | } |
| 1788 | ph->env.cmdline = strbuf_detach(&sb, NULL); |
| 1789 | return 0; |
| 1790 | |
| 1791 | error: |
| 1792 | strbuf_release(&sb); |
| 1793 | return -1; |
| 1794 | } |
| 1795 | |
| 1796 | static int process_cpu_topology(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1797 | struct perf_header *ph, int fd, |
| 1798 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1799 | { |
| 1800 | size_t ret; |
| 1801 | u32 nr, i; |
| 1802 | char *str; |
| 1803 | struct strbuf sb; |
| 1804 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1805 | ret = readn(fd, &nr, sizeof(nr)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1806 | if (ret != sizeof(nr)) |
| 1807 | return -1; |
| 1808 | |
| 1809 | if (ph->needs_swap) |
| 1810 | nr = bswap_32(nr); |
| 1811 | |
| 1812 | ph->env.nr_sibling_cores = nr; |
| 1813 | strbuf_init(&sb, 128); |
| 1814 | |
| 1815 | for (i = 0; i < nr; i++) { |
| 1816 | str = do_read_string(fd, ph); |
| 1817 | if (!str) |
| 1818 | goto error; |
| 1819 | |
| 1820 | /* include a NULL character at the end */ |
| 1821 | strbuf_add(&sb, str, strlen(str) + 1); |
| 1822 | free(str); |
| 1823 | } |
| 1824 | ph->env.sibling_cores = strbuf_detach(&sb, NULL); |
| 1825 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1826 | ret = readn(fd, &nr, sizeof(nr)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1827 | if (ret != sizeof(nr)) |
| 1828 | return -1; |
| 1829 | |
| 1830 | if (ph->needs_swap) |
| 1831 | nr = bswap_32(nr); |
| 1832 | |
| 1833 | ph->env.nr_sibling_threads = nr; |
| 1834 | |
| 1835 | for (i = 0; i < nr; i++) { |
| 1836 | str = do_read_string(fd, ph); |
| 1837 | if (!str) |
| 1838 | goto error; |
| 1839 | |
| 1840 | /* include a NULL character at the end */ |
| 1841 | strbuf_add(&sb, str, strlen(str) + 1); |
| 1842 | free(str); |
| 1843 | } |
| 1844 | ph->env.sibling_threads = strbuf_detach(&sb, NULL); |
| 1845 | return 0; |
| 1846 | |
| 1847 | error: |
| 1848 | strbuf_release(&sb); |
| 1849 | return -1; |
| 1850 | } |
| 1851 | |
| 1852 | static int process_numa_topology(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1853 | struct perf_header *ph, int fd, |
| 1854 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1855 | { |
| 1856 | size_t ret; |
| 1857 | u32 nr, node, i; |
| 1858 | char *str; |
| 1859 | uint64_t mem_total, mem_free; |
| 1860 | struct strbuf sb; |
| 1861 | |
| 1862 | /* nr nodes */ |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1863 | ret = readn(fd, &nr, sizeof(nr)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1864 | if (ret != sizeof(nr)) |
| 1865 | goto error; |
| 1866 | |
| 1867 | if (ph->needs_swap) |
| 1868 | nr = bswap_32(nr); |
| 1869 | |
| 1870 | ph->env.nr_numa_nodes = nr; |
| 1871 | strbuf_init(&sb, 256); |
| 1872 | |
| 1873 | for (i = 0; i < nr; i++) { |
| 1874 | /* node number */ |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1875 | ret = readn(fd, &node, sizeof(node)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1876 | if (ret != sizeof(node)) |
| 1877 | goto error; |
| 1878 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1879 | ret = readn(fd, &mem_total, sizeof(u64)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1880 | if (ret != sizeof(u64)) |
| 1881 | goto error; |
| 1882 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1883 | ret = readn(fd, &mem_free, sizeof(u64)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1884 | if (ret != sizeof(u64)) |
| 1885 | goto error; |
| 1886 | |
| 1887 | if (ph->needs_swap) { |
| 1888 | node = bswap_32(node); |
| 1889 | mem_total = bswap_64(mem_total); |
| 1890 | mem_free = bswap_64(mem_free); |
| 1891 | } |
| 1892 | |
| 1893 | strbuf_addf(&sb, "%u:%"PRIu64":%"PRIu64":", |
| 1894 | node, mem_total, mem_free); |
| 1895 | |
| 1896 | str = do_read_string(fd, ph); |
| 1897 | if (!str) |
| 1898 | goto error; |
| 1899 | |
| 1900 | /* include a NULL character at the end */ |
| 1901 | strbuf_add(&sb, str, strlen(str) + 1); |
| 1902 | free(str); |
| 1903 | } |
| 1904 | ph->env.numa_nodes = strbuf_detach(&sb, NULL); |
| 1905 | return 0; |
| 1906 | |
| 1907 | error: |
| 1908 | strbuf_release(&sb); |
| 1909 | return -1; |
| 1910 | } |
| 1911 | |
| 1912 | static int process_pmu_mappings(struct perf_file_section *section __maybe_unused, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1913 | struct perf_header *ph, int fd, |
| 1914 | void *data __maybe_unused) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1915 | { |
| 1916 | size_t ret; |
| 1917 | char *name; |
| 1918 | u32 pmu_num; |
| 1919 | u32 type; |
| 1920 | struct strbuf sb; |
| 1921 | |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1922 | ret = readn(fd, &pmu_num, sizeof(pmu_num)); |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1923 | if (ret != sizeof(pmu_num)) |
| 1924 | return -1; |
| 1925 | |
| 1926 | if (ph->needs_swap) |
| 1927 | pmu_num = bswap_32(pmu_num); |
| 1928 | |
| 1929 | if (!pmu_num) { |
| 1930 | pr_debug("pmu mappings not available\n"); |
| 1931 | return 0; |
| 1932 | } |
| 1933 | |
| 1934 | ph->env.nr_pmu_mappings = pmu_num; |
| 1935 | strbuf_init(&sb, 128); |
| 1936 | |
| 1937 | while (pmu_num) { |
Namhyung Kim | 5323f60 | 2012-12-17 15:38:54 +0900 | [diff] [blame] | 1938 | if (readn(fd, &type, sizeof(type)) != sizeof(type)) |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1939 | goto error; |
| 1940 | if (ph->needs_swap) |
| 1941 | type = bswap_32(type); |
| 1942 | |
| 1943 | name = do_read_string(fd, ph); |
| 1944 | if (!name) |
| 1945 | goto error; |
| 1946 | |
| 1947 | strbuf_addf(&sb, "%u:%s", type, name); |
| 1948 | /* include a NULL character at the end */ |
| 1949 | strbuf_add(&sb, "", 1); |
| 1950 | |
| 1951 | free(name); |
| 1952 | pmu_num--; |
| 1953 | } |
| 1954 | ph->env.pmu_mappings = strbuf_detach(&sb, NULL); |
| 1955 | return 0; |
| 1956 | |
| 1957 | error: |
| 1958 | strbuf_release(&sb); |
| 1959 | return -1; |
| 1960 | } |
| 1961 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1962 | struct feature_ops { |
| 1963 | int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist); |
| 1964 | void (*print)(struct perf_header *h, int fd, FILE *fp); |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1965 | int (*process)(struct perf_file_section *section, |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 1966 | struct perf_header *h, int fd, void *data); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1967 | const char *name; |
| 1968 | bool full_only; |
| 1969 | }; |
| 1970 | |
Robert Richter | 8cdfa78 | 2011-12-07 10:02:56 +0100 | [diff] [blame] | 1971 | #define FEAT_OPA(n, func) \ |
| 1972 | [n] = { .name = #n, .write = write_##func, .print = print_##func } |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1973 | #define FEAT_OPP(n, func) \ |
| 1974 | [n] = { .name = #n, .write = write_##func, .print = print_##func, \ |
| 1975 | .process = process_##func } |
Robert Richter | 8cdfa78 | 2011-12-07 10:02:56 +0100 | [diff] [blame] | 1976 | #define FEAT_OPF(n, func) \ |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1977 | [n] = { .name = #n, .write = write_##func, .print = print_##func, \ |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1978 | .process = process_##func, .full_only = true } |
Robert Richter | 8cdfa78 | 2011-12-07 10:02:56 +0100 | [diff] [blame] | 1979 | |
| 1980 | /* feature_ops not implemented: */ |
Stephane Eranian | 2eeaaa0 | 2012-05-15 13:28:13 +0200 | [diff] [blame] | 1981 | #define print_tracing_data NULL |
| 1982 | #define print_build_id NULL |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 1983 | |
| 1984 | static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = { |
Stephane Eranian | 2eeaaa0 | 2012-05-15 13:28:13 +0200 | [diff] [blame] | 1985 | FEAT_OPP(HEADER_TRACING_DATA, tracing_data), |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 1986 | FEAT_OPP(HEADER_BUILD_ID, build_id), |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1987 | FEAT_OPP(HEADER_HOSTNAME, hostname), |
| 1988 | FEAT_OPP(HEADER_OSRELEASE, osrelease), |
| 1989 | FEAT_OPP(HEADER_VERSION, version), |
| 1990 | FEAT_OPP(HEADER_ARCH, arch), |
| 1991 | FEAT_OPP(HEADER_NRCPUS, nrcpus), |
| 1992 | FEAT_OPP(HEADER_CPUDESC, cpudesc), |
Namhyung Kim | 37e9d75 | 2012-09-24 17:15:03 +0900 | [diff] [blame] | 1993 | FEAT_OPP(HEADER_CPUID, cpuid), |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1994 | FEAT_OPP(HEADER_TOTAL_MEM, total_mem), |
Robert Richter | 7c2f7af | 2012-08-16 21:10:23 +0200 | [diff] [blame] | 1995 | FEAT_OPP(HEADER_EVENT_DESC, event_desc), |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 1996 | FEAT_OPP(HEADER_CMDLINE, cmdline), |
Robert Richter | 8cdfa78 | 2011-12-07 10:02:56 +0100 | [diff] [blame] | 1997 | FEAT_OPF(HEADER_CPU_TOPOLOGY, cpu_topology), |
| 1998 | FEAT_OPF(HEADER_NUMA_TOPOLOGY, numa_topology), |
Stephane Eranian | 330aa67 | 2012-03-08 23:47:46 +0100 | [diff] [blame] | 1999 | FEAT_OPA(HEADER_BRANCH_STACK, branch_stack), |
Namhyung Kim | a1ae565 | 2012-09-24 17:14:59 +0900 | [diff] [blame] | 2000 | FEAT_OPP(HEADER_PMU_MAPPINGS, pmu_mappings), |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2001 | }; |
| 2002 | |
| 2003 | struct header_print_data { |
| 2004 | FILE *fp; |
| 2005 | bool full; /* extended list of headers */ |
| 2006 | }; |
| 2007 | |
| 2008 | static int perf_file_section__fprintf_info(struct perf_file_section *section, |
| 2009 | struct perf_header *ph, |
| 2010 | int feat, int fd, void *data) |
| 2011 | { |
| 2012 | struct header_print_data *hd = data; |
| 2013 | |
| 2014 | if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { |
| 2015 | pr_debug("Failed to lseek to %" PRIu64 " offset for feature " |
| 2016 | "%d, continuing...\n", section->offset, feat); |
| 2017 | return 0; |
| 2018 | } |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2019 | if (feat >= HEADER_LAST_FEATURE) { |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2020 | pr_warning("unknown feature %d\n", feat); |
Robert Richter | f7a8a13 | 2011-12-07 10:02:51 +0100 | [diff] [blame] | 2021 | return 0; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2022 | } |
| 2023 | if (!feat_ops[feat].print) |
| 2024 | return 0; |
| 2025 | |
| 2026 | if (!feat_ops[feat].full_only || hd->full) |
| 2027 | feat_ops[feat].print(ph, fd, hd->fp); |
| 2028 | else |
| 2029 | fprintf(hd->fp, "# %s info available, use -I to display\n", |
| 2030 | feat_ops[feat].name); |
| 2031 | |
| 2032 | return 0; |
| 2033 | } |
| 2034 | |
| 2035 | int perf_header__fprintf_info(struct perf_session *session, FILE *fp, bool full) |
| 2036 | { |
| 2037 | struct header_print_data hd; |
| 2038 | struct perf_header *header = &session->header; |
| 2039 | int fd = session->fd; |
| 2040 | hd.fp = fp; |
| 2041 | hd.full = full; |
| 2042 | |
| 2043 | perf_header__process_sections(header, fd, &hd, |
| 2044 | perf_file_section__fprintf_info); |
| 2045 | return 0; |
| 2046 | } |
| 2047 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2048 | static int do_write_feat(int fd, struct perf_header *h, int type, |
| 2049 | struct perf_file_section **p, |
| 2050 | struct perf_evlist *evlist) |
| 2051 | { |
| 2052 | int err; |
| 2053 | int ret = 0; |
| 2054 | |
| 2055 | if (perf_header__has_feat(h, type)) { |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2056 | if (!feat_ops[type].write) |
| 2057 | return -1; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2058 | |
| 2059 | (*p)->offset = lseek(fd, 0, SEEK_CUR); |
| 2060 | |
| 2061 | err = feat_ops[type].write(fd, h, evlist); |
| 2062 | if (err < 0) { |
| 2063 | pr_debug("failed to write feature %d\n", type); |
| 2064 | |
| 2065 | /* undo anything written */ |
| 2066 | lseek(fd, (*p)->offset, SEEK_SET); |
| 2067 | |
| 2068 | return -1; |
| 2069 | } |
| 2070 | (*p)->size = lseek(fd, 0, SEEK_CUR) - (*p)->offset; |
| 2071 | (*p)++; |
| 2072 | } |
| 2073 | return ret; |
| 2074 | } |
| 2075 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2076 | static int perf_header__adds_write(struct perf_header *header, |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 2077 | struct perf_evlist *evlist, int fd) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 2078 | { |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2079 | int nr_sections; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2080 | struct perf_file_section *feat_sec, *p; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2081 | int sec_size; |
| 2082 | u64 sec_start; |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2083 | int feat; |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2084 | int err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2085 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2086 | nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2087 | if (!nr_sections) |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2088 | return 0; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2089 | |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2090 | feat_sec = p = calloc(sizeof(*feat_sec), nr_sections); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2091 | if (feat_sec == NULL) |
| 2092 | return -ENOMEM; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2093 | |
| 2094 | sec_size = sizeof(*feat_sec) * nr_sections; |
| 2095 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2096 | sec_start = header->data_offset + header->data_size; |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 2097 | lseek(fd, sec_start + sec_size, SEEK_SET); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 2098 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2099 | for_each_set_bit(feat, header->adds_features, HEADER_FEAT_BITS) { |
| 2100 | if (do_write_feat(fd, header, feat, &p, evlist)) |
| 2101 | perf_header__clear_feat(header, feat); |
| 2102 | } |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2103 | |
Xiao Guangrong | f887f30 | 2010-02-04 16:46:42 +0800 | [diff] [blame] | 2104 | lseek(fd, sec_start, SEEK_SET); |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2105 | /* |
| 2106 | * may write more than needed due to dropped feature, but |
| 2107 | * this is okay, reader will skip the mising entries |
| 2108 | */ |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2109 | err = do_write(fd, feat_sec, sec_size); |
| 2110 | if (err < 0) |
| 2111 | pr_debug("failed to write feature section\n"); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2112 | free(feat_sec); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2113 | return err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2114 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 2115 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2116 | int perf_header__write_pipe(int fd) |
| 2117 | { |
| 2118 | struct perf_pipe_file_header f_header; |
| 2119 | int err; |
| 2120 | |
| 2121 | f_header = (struct perf_pipe_file_header){ |
| 2122 | .magic = PERF_MAGIC, |
| 2123 | .size = sizeof(f_header), |
| 2124 | }; |
| 2125 | |
| 2126 | err = do_write(fd, &f_header, sizeof(f_header)); |
| 2127 | if (err < 0) { |
| 2128 | pr_debug("failed to write perf pipe header\n"); |
| 2129 | return err; |
| 2130 | } |
| 2131 | |
| 2132 | return 0; |
| 2133 | } |
| 2134 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2135 | int perf_session__write_header(struct perf_session *session, |
| 2136 | struct perf_evlist *evlist, |
| 2137 | int fd, bool at_exit) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2138 | { |
| 2139 | struct perf_file_header f_header; |
| 2140 | struct perf_file_attr f_attr; |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2141 | struct perf_header *header = &session->header; |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 2142 | struct perf_evsel *evsel, *pair = NULL; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2143 | int err; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2144 | |
| 2145 | lseek(fd, sizeof(f_header), SEEK_SET); |
| 2146 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2147 | if (session->evlist != evlist) |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 2148 | pair = perf_evlist__first(session->evlist); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2149 | |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 2150 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 2151 | evsel->id_offset = lseek(fd, 0, SEEK_CUR); |
| 2152 | err = do_write(fd, evsel->id, evsel->ids * sizeof(u64)); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2153 | if (err < 0) { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2154 | out_err_write: |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2155 | pr_debug("failed to write perf header\n"); |
| 2156 | return err; |
| 2157 | } |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2158 | if (session->evlist != evlist) { |
| 2159 | err = do_write(fd, pair->id, pair->ids * sizeof(u64)); |
| 2160 | if (err < 0) |
| 2161 | goto out_err_write; |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 2162 | evsel->ids += pair->ids; |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 2163 | pair = perf_evsel__next(pair); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2164 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2165 | } |
| 2166 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2167 | header->attr_offset = lseek(fd, 0, SEEK_CUR); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2168 | |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 2169 | list_for_each_entry(evsel, &evlist->entries, node) { |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2170 | f_attr = (struct perf_file_attr){ |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 2171 | .attr = evsel->attr, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2172 | .ids = { |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 2173 | .offset = evsel->id_offset, |
| 2174 | .size = evsel->ids * sizeof(u64), |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2175 | } |
| 2176 | }; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2177 | err = do_write(fd, &f_attr, sizeof(f_attr)); |
| 2178 | if (err < 0) { |
| 2179 | pr_debug("failed to write perf header attribute\n"); |
| 2180 | return err; |
| 2181 | } |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2182 | } |
| 2183 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2184 | header->event_offset = lseek(fd, 0, SEEK_CUR); |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 2185 | header->event_size = trace_event_count * sizeof(struct perf_trace_event_type); |
| 2186 | if (trace_events) { |
| 2187 | err = do_write(fd, trace_events, header->event_size); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2188 | if (err < 0) { |
| 2189 | pr_debug("failed to write perf header events\n"); |
| 2190 | return err; |
| 2191 | } |
| 2192 | } |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 2193 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2194 | header->data_offset = lseek(fd, 0, SEEK_CUR); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2195 | |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2196 | if (at_exit) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2197 | err = perf_header__adds_write(header, evlist, fd); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2198 | if (err < 0) |
| 2199 | return err; |
| 2200 | } |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2201 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2202 | f_header = (struct perf_file_header){ |
| 2203 | .magic = PERF_MAGIC, |
| 2204 | .size = sizeof(f_header), |
| 2205 | .attr_size = sizeof(f_attr), |
| 2206 | .attrs = { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2207 | .offset = header->attr_offset, |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2208 | .size = evlist->nr_entries * sizeof(f_attr), |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2209 | }, |
| 2210 | .data = { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2211 | .offset = header->data_offset, |
| 2212 | .size = header->data_size, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2213 | }, |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 2214 | .event_types = { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2215 | .offset = header->event_offset, |
| 2216 | .size = header->event_size, |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 2217 | }, |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2218 | }; |
| 2219 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2220 | memcpy(&f_header.adds_features, &header->adds_features, sizeof(header->adds_features)); |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 2221 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2222 | lseek(fd, 0, SEEK_SET); |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2223 | err = do_write(fd, &f_header, sizeof(f_header)); |
| 2224 | if (err < 0) { |
| 2225 | pr_debug("failed to write perf header\n"); |
| 2226 | return err; |
| 2227 | } |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2228 | lseek(fd, header->data_offset + header->data_size, SEEK_SET); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2229 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2230 | header->frozen = 1; |
Arnaldo Carvalho de Melo | d5eed90 | 2009-11-19 14:55:56 -0200 | [diff] [blame] | 2231 | return 0; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2232 | } |
| 2233 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2234 | static int perf_header__getbuffer64(struct perf_header *header, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2235 | int fd, void *buf, size_t size) |
| 2236 | { |
Arnaldo Carvalho de Melo | 1e7972c | 2011-01-03 16:50:55 -0200 | [diff] [blame] | 2237 | if (readn(fd, buf, size) <= 0) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2238 | return -1; |
| 2239 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2240 | if (header->needs_swap) |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2241 | mem_bswap_64(buf, size); |
| 2242 | |
| 2243 | return 0; |
| 2244 | } |
| 2245 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2246 | int perf_header__process_sections(struct perf_header *header, int fd, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2247 | void *data, |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2248 | int (*process)(struct perf_file_section *section, |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2249 | struct perf_header *ph, |
| 2250 | int feat, int fd, void *data)) |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 2251 | { |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2252 | struct perf_file_section *feat_sec, *sec; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2253 | int nr_sections; |
| 2254 | int sec_size; |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2255 | int feat; |
| 2256 | int err; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2257 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2258 | nr_sections = bitmap_weight(header->adds_features, HEADER_FEAT_BITS); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2259 | if (!nr_sections) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2260 | return 0; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2261 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2262 | feat_sec = sec = calloc(sizeof(*feat_sec), nr_sections); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2263 | if (!feat_sec) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2264 | return -1; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2265 | |
| 2266 | sec_size = sizeof(*feat_sec) * nr_sections; |
| 2267 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2268 | lseek(fd, header->data_offset + header->data_size, SEEK_SET); |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2269 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2270 | err = perf_header__getbuffer64(header, fd, feat_sec, sec_size); |
| 2271 | if (err < 0) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2272 | goto out_free; |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2273 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2274 | for_each_set_bit(feat, header->adds_features, HEADER_LAST_FEATURE) { |
| 2275 | err = process(sec++, header, feat, fd, data); |
| 2276 | if (err < 0) |
| 2277 | goto out_free; |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 2278 | } |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2279 | err = 0; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2280 | out_free: |
Frederic Weisbecker | 9e827dd | 2009-11-11 04:51:07 +0100 | [diff] [blame] | 2281 | free(feat_sec); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2282 | return err; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2283 | } |
Frederic Weisbecker | 2ba0825 | 2009-10-17 17:12:34 +0200 | [diff] [blame] | 2284 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2285 | static const int attr_file_abi_sizes[] = { |
| 2286 | [0] = PERF_ATTR_SIZE_VER0, |
| 2287 | [1] = PERF_ATTR_SIZE_VER1, |
Jiri Olsa | 239cc47 | 2012-08-07 15:20:42 +0200 | [diff] [blame] | 2288 | [2] = PERF_ATTR_SIZE_VER2, |
Jiri Olsa | 0f6a301 | 2012-08-07 15:20:45 +0200 | [diff] [blame] | 2289 | [3] = PERF_ATTR_SIZE_VER3, |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2290 | 0, |
| 2291 | }; |
| 2292 | |
| 2293 | /* |
| 2294 | * In the legacy file format, the magic number is not used to encode endianness. |
| 2295 | * hdr_sz was used to encode endianness. But given that hdr_sz can vary based |
| 2296 | * on ABI revisions, we need to try all combinations for all endianness to |
| 2297 | * detect the endianness. |
| 2298 | */ |
| 2299 | static int try_all_file_abis(uint64_t hdr_sz, struct perf_header *ph) |
| 2300 | { |
| 2301 | uint64_t ref_size, attr_size; |
| 2302 | int i; |
| 2303 | |
| 2304 | for (i = 0 ; attr_file_abi_sizes[i]; i++) { |
| 2305 | ref_size = attr_file_abi_sizes[i] |
| 2306 | + sizeof(struct perf_file_section); |
| 2307 | if (hdr_sz != ref_size) { |
| 2308 | attr_size = bswap_64(hdr_sz); |
| 2309 | if (attr_size != ref_size) |
| 2310 | continue; |
| 2311 | |
| 2312 | ph->needs_swap = true; |
| 2313 | } |
| 2314 | pr_debug("ABI%d perf.data file detected, need_swap=%d\n", |
| 2315 | i, |
| 2316 | ph->needs_swap); |
| 2317 | return 0; |
| 2318 | } |
| 2319 | /* could not determine endianness */ |
| 2320 | return -1; |
| 2321 | } |
| 2322 | |
| 2323 | #define PERF_PIPE_HDR_VER0 16 |
| 2324 | |
| 2325 | static const size_t attr_pipe_abi_sizes[] = { |
| 2326 | [0] = PERF_PIPE_HDR_VER0, |
| 2327 | 0, |
| 2328 | }; |
| 2329 | |
| 2330 | /* |
| 2331 | * In the legacy pipe format, there is an implicit assumption that endiannesss |
| 2332 | * between host recording the samples, and host parsing the samples is the |
| 2333 | * same. This is not always the case given that the pipe output may always be |
| 2334 | * redirected into a file and analyzed on a different machine with possibly a |
| 2335 | * different endianness and perf_event ABI revsions in the perf tool itself. |
| 2336 | */ |
| 2337 | static int try_all_pipe_abis(uint64_t hdr_sz, struct perf_header *ph) |
| 2338 | { |
| 2339 | u64 attr_size; |
| 2340 | int i; |
| 2341 | |
| 2342 | for (i = 0 ; attr_pipe_abi_sizes[i]; i++) { |
| 2343 | if (hdr_sz != attr_pipe_abi_sizes[i]) { |
| 2344 | attr_size = bswap_64(hdr_sz); |
| 2345 | if (attr_size != hdr_sz) |
| 2346 | continue; |
| 2347 | |
| 2348 | ph->needs_swap = true; |
| 2349 | } |
| 2350 | pr_debug("Pipe ABI%d perf.data file detected\n", i); |
| 2351 | return 0; |
| 2352 | } |
| 2353 | return -1; |
| 2354 | } |
| 2355 | |
Feng Tang | e84ba4e | 2012-10-30 11:56:07 +0800 | [diff] [blame] | 2356 | bool is_perf_magic(u64 magic) |
| 2357 | { |
| 2358 | if (!memcmp(&magic, __perf_magic1, sizeof(magic)) |
| 2359 | || magic == __perf_magic2 |
| 2360 | || magic == __perf_magic2_sw) |
| 2361 | return true; |
| 2362 | |
| 2363 | return false; |
| 2364 | } |
| 2365 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2366 | static int check_magic_endian(u64 magic, uint64_t hdr_sz, |
| 2367 | bool is_pipe, struct perf_header *ph) |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2368 | { |
| 2369 | int ret; |
| 2370 | |
| 2371 | /* check for legacy format */ |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2372 | ret = memcmp(&magic, __perf_magic1, sizeof(magic)); |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2373 | if (ret == 0) { |
| 2374 | pr_debug("legacy perf.data format\n"); |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2375 | if (is_pipe) |
| 2376 | return try_all_pipe_abis(hdr_sz, ph); |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2377 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2378 | return try_all_file_abis(hdr_sz, ph); |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2379 | } |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2380 | /* |
| 2381 | * the new magic number serves two purposes: |
| 2382 | * - unique number to identify actual perf.data files |
| 2383 | * - encode endianness of file |
| 2384 | */ |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2385 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2386 | /* check magic number with one endianness */ |
| 2387 | if (magic == __perf_magic2) |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2388 | return 0; |
| 2389 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2390 | /* check magic number with opposite endianness */ |
| 2391 | if (magic != __perf_magic2_sw) |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2392 | return -1; |
| 2393 | |
| 2394 | ph->needs_swap = true; |
| 2395 | |
| 2396 | return 0; |
| 2397 | } |
| 2398 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2399 | int perf_file_header__read(struct perf_file_header *header, |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2400 | struct perf_header *ph, int fd) |
| 2401 | { |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2402 | int ret; |
| 2403 | |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2404 | lseek(fd, 0, SEEK_SET); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2405 | |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2406 | ret = readn(fd, header, sizeof(*header)); |
| 2407 | if (ret <= 0) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2408 | return -1; |
| 2409 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2410 | if (check_magic_endian(header->magic, |
| 2411 | header->attr_size, false, ph) < 0) { |
| 2412 | pr_debug("magic/endian check failed\n"); |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2413 | return -1; |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2414 | } |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2415 | |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2416 | if (ph->needs_swap) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2417 | mem_bswap_64(header, offsetof(struct perf_file_header, |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2418 | adds_features)); |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2419 | } |
| 2420 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2421 | if (header->size != sizeof(*header)) { |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2422 | /* Support the previous format */ |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2423 | if (header->size == offsetof(typeof(*header), adds_features)) |
| 2424 | bitmap_zero(header->adds_features, HEADER_FEAT_BITS); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2425 | else |
| 2426 | return -1; |
David Ahern | d327fa4 | 2011-10-18 17:34:01 -0600 | [diff] [blame] | 2427 | } else if (ph->needs_swap) { |
David Ahern | d327fa4 | 2011-10-18 17:34:01 -0600 | [diff] [blame] | 2428 | /* |
| 2429 | * feature bitmap is declared as an array of unsigned longs -- |
| 2430 | * not good since its size can differ between the host that |
| 2431 | * generated the data file and the host analyzing the file. |
| 2432 | * |
| 2433 | * We need to handle endianness, but we don't know the size of |
| 2434 | * the unsigned long where the file was generated. Take a best |
| 2435 | * guess at determining it: try 64-bit swap first (ie., file |
| 2436 | * created on a 64-bit host), and check if the hostname feature |
| 2437 | * bit is set (this feature bit is forced on as of fbe96f2). |
| 2438 | * If the bit is not, undo the 64-bit swap and try a 32-bit |
| 2439 | * swap. If the hostname bit is still not set (e.g., older data |
| 2440 | * file), punt and fallback to the original behavior -- |
| 2441 | * clearing all feature bits and setting buildid. |
| 2442 | */ |
David Ahern | 80c0120 | 2012-06-08 11:47:51 -0300 | [diff] [blame] | 2443 | mem_bswap_64(&header->adds_features, |
| 2444 | BITS_TO_U64(HEADER_FEAT_BITS)); |
David Ahern | d327fa4 | 2011-10-18 17:34:01 -0600 | [diff] [blame] | 2445 | |
| 2446 | if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { |
David Ahern | 80c0120 | 2012-06-08 11:47:51 -0300 | [diff] [blame] | 2447 | /* unswap as u64 */ |
| 2448 | mem_bswap_64(&header->adds_features, |
| 2449 | BITS_TO_U64(HEADER_FEAT_BITS)); |
| 2450 | |
| 2451 | /* unswap as u32 */ |
| 2452 | mem_bswap_32(&header->adds_features, |
| 2453 | BITS_TO_U32(HEADER_FEAT_BITS)); |
David Ahern | d327fa4 | 2011-10-18 17:34:01 -0600 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | if (!test_bit(HEADER_HOSTNAME, header->adds_features)) { |
| 2457 | bitmap_zero(header->adds_features, HEADER_FEAT_BITS); |
| 2458 | set_bit(HEADER_BUILD_ID, header->adds_features); |
| 2459 | } |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2460 | } |
| 2461 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2462 | memcpy(&ph->adds_features, &header->adds_features, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2463 | sizeof(ph->adds_features)); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2464 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2465 | ph->event_offset = header->event_types.offset; |
| 2466 | ph->event_size = header->event_types.size; |
| 2467 | ph->data_offset = header->data.offset; |
| 2468 | ph->data_size = header->data.size; |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2469 | return 0; |
| 2470 | } |
| 2471 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2472 | 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] | 2473 | struct perf_header *ph, |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 2474 | int feat, int fd, void *data) |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2475 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2476 | if (lseek(fd, section->offset, SEEK_SET) == (off_t)-1) { |
Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 2477 | pr_debug("Failed to lseek to %" PRIu64 " offset for feature " |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2478 | "%d, continuing...\n", section->offset, feat); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2479 | return 0; |
| 2480 | } |
| 2481 | |
Robert Richter | b1e5a9b | 2011-12-07 10:02:57 +0100 | [diff] [blame] | 2482 | if (feat >= HEADER_LAST_FEATURE) { |
| 2483 | pr_debug("unknown feature %d, continuing...\n", feat); |
| 2484 | return 0; |
| 2485 | } |
| 2486 | |
Robert Richter | f1c67db | 2012-02-10 15:41:56 +0100 | [diff] [blame] | 2487 | if (!feat_ops[feat].process) |
| 2488 | return 0; |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2489 | |
Namhyung Kim | 3d7eb86 | 2012-09-24 17:15:01 +0900 | [diff] [blame] | 2490 | return feat_ops[feat].process(section, ph, fd, data); |
Arnaldo Carvalho de Melo | 37562ea | 2009-11-16 16:32:43 -0200 | [diff] [blame] | 2491 | } |
| 2492 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2493 | 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] | 2494 | struct perf_header *ph, int fd, |
| 2495 | bool repipe) |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2496 | { |
Stephane Eranian | 73323f5 | 2012-02-02 13:54:44 +0100 | [diff] [blame] | 2497 | int ret; |
| 2498 | |
| 2499 | ret = readn(fd, header, sizeof(*header)); |
| 2500 | if (ret <= 0) |
| 2501 | return -1; |
| 2502 | |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2503 | if (check_magic_endian(header->magic, header->size, true, ph) < 0) { |
| 2504 | pr_debug("endian/magic failed\n"); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2505 | return -1; |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | if (ph->needs_swap) |
| 2509 | header->size = bswap_64(header->size); |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2510 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2511 | if (repipe && do_write(STDOUT_FILENO, header, sizeof(*header)) < 0) |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 2512 | return -1; |
| 2513 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2514 | return 0; |
| 2515 | } |
| 2516 | |
| 2517 | static int perf_header__read_pipe(struct perf_session *session, int fd) |
| 2518 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2519 | struct perf_header *header = &session->header; |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2520 | struct perf_pipe_file_header f_header; |
| 2521 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2522 | if (perf_file_header__read_pipe(&f_header, header, fd, |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 2523 | session->repipe) < 0) { |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2524 | pr_debug("incompatible file format\n"); |
| 2525 | return -EINVAL; |
| 2526 | } |
| 2527 | |
| 2528 | session->fd = fd; |
| 2529 | |
| 2530 | return 0; |
| 2531 | } |
| 2532 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 2533 | static int read_attr(int fd, struct perf_header *ph, |
| 2534 | struct perf_file_attr *f_attr) |
| 2535 | { |
| 2536 | struct perf_event_attr *attr = &f_attr->attr; |
| 2537 | size_t sz, left; |
| 2538 | size_t our_sz = sizeof(f_attr->attr); |
| 2539 | int ret; |
| 2540 | |
| 2541 | memset(f_attr, 0, sizeof(*f_attr)); |
| 2542 | |
| 2543 | /* read minimal guaranteed structure */ |
| 2544 | ret = readn(fd, attr, PERF_ATTR_SIZE_VER0); |
| 2545 | if (ret <= 0) { |
| 2546 | pr_debug("cannot read %d bytes of header attr\n", |
| 2547 | PERF_ATTR_SIZE_VER0); |
| 2548 | return -1; |
| 2549 | } |
| 2550 | |
| 2551 | /* on file perf_event_attr size */ |
| 2552 | sz = attr->size; |
Stephane Eranian | 114382a | 2012-02-09 23:21:08 +0100 | [diff] [blame] | 2553 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 2554 | if (ph->needs_swap) |
| 2555 | sz = bswap_32(sz); |
| 2556 | |
| 2557 | if (sz == 0) { |
| 2558 | /* assume ABI0 */ |
| 2559 | sz = PERF_ATTR_SIZE_VER0; |
| 2560 | } else if (sz > our_sz) { |
| 2561 | pr_debug("file uses a more recent and unsupported ABI" |
| 2562 | " (%zu bytes extra)\n", sz - our_sz); |
| 2563 | return -1; |
| 2564 | } |
| 2565 | /* what we have not yet read and that we know about */ |
| 2566 | left = sz - PERF_ATTR_SIZE_VER0; |
| 2567 | if (left) { |
| 2568 | void *ptr = attr; |
| 2569 | ptr += PERF_ATTR_SIZE_VER0; |
| 2570 | |
| 2571 | ret = readn(fd, ptr, left); |
| 2572 | } |
| 2573 | /* read perf_file_section, ids are read in caller */ |
| 2574 | ret = readn(fd, &f_attr->ids, sizeof(f_attr->ids)); |
| 2575 | |
| 2576 | return ret <= 0 ? -1 : 0; |
| 2577 | } |
| 2578 | |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 2579 | static int perf_evsel__prepare_tracepoint_event(struct perf_evsel *evsel, |
| 2580 | struct pevent *pevent) |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 2581 | { |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 2582 | struct event_format *event; |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 2583 | char bf[128]; |
| 2584 | |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 2585 | /* already prepared */ |
| 2586 | if (evsel->tp_format) |
| 2587 | return 0; |
| 2588 | |
| 2589 | event = pevent_find_event(pevent, evsel->attr.config); |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 2590 | if (event == NULL) |
| 2591 | return -1; |
| 2592 | |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 2593 | if (!evsel->name) { |
| 2594 | snprintf(bf, sizeof(bf), "%s:%s", event->system, event->name); |
| 2595 | evsel->name = strdup(bf); |
| 2596 | if (evsel->name == NULL) |
| 2597 | return -1; |
| 2598 | } |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 2599 | |
Arnaldo Carvalho de Melo | fcf65bf | 2012-08-07 09:58:03 -0300 | [diff] [blame] | 2600 | evsel->tp_format = event; |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 2601 | return 0; |
| 2602 | } |
| 2603 | |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 2604 | static int perf_evlist__prepare_tracepoint_events(struct perf_evlist *evlist, |
| 2605 | struct pevent *pevent) |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 2606 | { |
| 2607 | struct perf_evsel *pos; |
| 2608 | |
| 2609 | list_for_each_entry(pos, &evlist->entries, node) { |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 2610 | if (pos->attr.type == PERF_TYPE_TRACEPOINT && |
| 2611 | perf_evsel__prepare_tracepoint_event(pos, pevent)) |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 2612 | return -1; |
| 2613 | } |
| 2614 | |
| 2615 | return 0; |
| 2616 | } |
| 2617 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2618 | int perf_session__read_header(struct perf_session *session, int fd) |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2619 | { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2620 | struct perf_header *header = &session->header; |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2621 | struct perf_file_header f_header; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2622 | struct perf_file_attr f_attr; |
| 2623 | u64 f_id; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2624 | int nr_attrs, nr_ids, i, j; |
| 2625 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2626 | session->evlist = perf_evlist__new(NULL, NULL); |
| 2627 | if (session->evlist == NULL) |
| 2628 | return -ENOMEM; |
| 2629 | |
Tom Zanussi | 8dc5810 | 2010-04-01 23:59:15 -0500 | [diff] [blame] | 2630 | if (session->fd_pipe) |
| 2631 | return perf_header__read_pipe(session, fd); |
| 2632 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 2633 | if (perf_file_header__read(&f_header, header, fd) < 0) |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 2634 | return -EINVAL; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2635 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 2636 | nr_attrs = f_header.attrs.size / f_header.attr_size; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2637 | lseek(fd, f_header.attrs.offset, SEEK_SET); |
| 2638 | |
| 2639 | for (i = 0; i < nr_attrs; i++) { |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2640 | struct perf_evsel *evsel; |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 2641 | off_t tmp; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2642 | |
Stephane Eranian | 69996df | 2012-02-09 23:21:06 +0100 | [diff] [blame] | 2643 | if (read_attr(fd, header, &f_attr) < 0) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2644 | goto out_errno; |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2645 | |
David Ahern | eda3913 | 2011-07-15 12:34:09 -0600 | [diff] [blame] | 2646 | if (header->needs_swap) |
| 2647 | perf_event__attr_swap(&f_attr.attr); |
| 2648 | |
Peter Zijlstra | 1c222bc | 2009-08-06 20:57:41 +0200 | [diff] [blame] | 2649 | tmp = lseek(fd, 0, SEEK_CUR); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2650 | evsel = perf_evsel__new(&f_attr.attr, i); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2651 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2652 | if (evsel == NULL) |
| 2653 | goto out_delete_evlist; |
Arnaldo Carvalho de Melo | 0807d2d | 2012-09-26 12:48:18 -0300 | [diff] [blame] | 2654 | |
| 2655 | evsel->needs_swap = header->needs_swap; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2656 | /* |
| 2657 | * Do it before so that if perf_evsel__alloc_id fails, this |
| 2658 | * entry gets purged too at perf_evlist__delete(). |
| 2659 | */ |
| 2660 | perf_evlist__add(session->evlist, evsel); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2661 | |
| 2662 | nr_ids = f_attr.ids.size / sizeof(u64); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2663 | /* |
| 2664 | * We don't have the cpu and thread maps on the header, so |
| 2665 | * for allocating the perf_sample_id table we fake 1 cpu and |
| 2666 | * hattr->ids threads. |
| 2667 | */ |
| 2668 | if (perf_evsel__alloc_id(evsel, 1, nr_ids)) |
| 2669 | goto out_delete_evlist; |
| 2670 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2671 | lseek(fd, f_attr.ids.offset, SEEK_SET); |
| 2672 | |
| 2673 | for (j = 0; j < nr_ids; j++) { |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2674 | 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] | 2675 | goto out_errno; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2676 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2677 | 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] | 2678 | } |
Arnaldo Carvalho de Melo | 11deb1f | 2009-11-17 01:18:09 -0200 | [diff] [blame] | 2679 | |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2680 | lseek(fd, tmp, SEEK_SET); |
| 2681 | } |
| 2682 | |
Arnaldo Carvalho de Melo | d04b35f | 2011-11-11 22:17:32 -0200 | [diff] [blame] | 2683 | symbol_conf.nr_events = nr_attrs; |
| 2684 | |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 2685 | if (f_header.event_types.size) { |
| 2686 | lseek(fd, f_header.event_types.offset, SEEK_SET); |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 2687 | trace_events = malloc(f_header.event_types.size); |
| 2688 | if (trace_events == NULL) |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 2689 | return -ENOMEM; |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 2690 | if (perf_header__getbuffer64(header, fd, trace_events, |
Arnaldo Carvalho de Melo | ba21594 | 2010-01-14 12:23:10 -0200 | [diff] [blame] | 2691 | f_header.event_types.size)) |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2692 | goto out_errno; |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 2693 | trace_event_count = f_header.event_types.size / sizeof(struct perf_trace_event_type); |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 2694 | } |
Frederic Weisbecker | 03456a1 | 2009-10-06 23:36:47 +0200 | [diff] [blame] | 2695 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 2696 | perf_header__process_sections(header, fd, &session->pevent, |
Stephane Eranian | fbe96f2 | 2011-09-30 15:40:40 +0200 | [diff] [blame] | 2697 | perf_file_section__process); |
Frederic Weisbecker | 4778d2e | 2009-11-11 04:51:05 +0100 | [diff] [blame] | 2698 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2699 | lseek(fd, header->data_offset, SEEK_SET); |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2700 | |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 2701 | if (perf_evlist__prepare_tracepoint_events(session->evlist, |
| 2702 | session->pevent)) |
Arnaldo Carvalho de Melo | cb9dd49 | 2012-06-11 19:03:32 -0300 | [diff] [blame] | 2703 | goto out_delete_evlist; |
| 2704 | |
Arnaldo Carvalho de Melo | 1c0b04d | 2011-03-09 08:13:19 -0300 | [diff] [blame] | 2705 | header->frozen = 1; |
Arnaldo Carvalho de Melo | 4dc0a04 | 2009-11-19 14:55:55 -0200 | [diff] [blame] | 2706 | return 0; |
Arnaldo Carvalho de Melo | 769885f | 2009-12-28 22:48:32 -0200 | [diff] [blame] | 2707 | out_errno: |
| 2708 | return -errno; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2709 | |
| 2710 | out_delete_evlist: |
| 2711 | perf_evlist__delete(session->evlist); |
| 2712 | session->evlist = NULL; |
| 2713 | return -ENOMEM; |
Peter Zijlstra | 7c6a1c6 | 2009-06-25 17:05:54 +0200 | [diff] [blame] | 2714 | } |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 2715 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2716 | int perf_event__synthesize_attr(struct perf_tool *tool, |
Robert Richter | f4d8343 | 2012-08-16 21:10:17 +0200 | [diff] [blame] | 2717 | struct perf_event_attr *attr, u32 ids, u64 *id, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2718 | perf_event__handler_t process) |
Frederic Weisbecker | 0d3a5c8 | 2009-08-16 20:56:37 +0200 | [diff] [blame] | 2719 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2720 | union perf_event *ev; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2721 | size_t size; |
| 2722 | int err; |
| 2723 | |
| 2724 | size = sizeof(struct perf_event_attr); |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 2725 | size = PERF_ALIGN(size, sizeof(u64)); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2726 | size += sizeof(struct perf_event_header); |
| 2727 | size += ids * sizeof(u64); |
| 2728 | |
| 2729 | ev = malloc(size); |
| 2730 | |
Chris Samuel | ce47dc5 | 2010-11-13 13:35:06 +1100 | [diff] [blame] | 2731 | if (ev == NULL) |
| 2732 | return -ENOMEM; |
| 2733 | |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2734 | ev->attr.attr = *attr; |
| 2735 | memcpy(ev->attr.id, id, ids * sizeof(u64)); |
| 2736 | |
| 2737 | ev->attr.header.type = PERF_RECORD_HEADER_ATTR; |
Robert Richter | f4d8343 | 2012-08-16 21:10:17 +0200 | [diff] [blame] | 2738 | ev->attr.header.size = (u16)size; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2739 | |
Robert Richter | f4d8343 | 2012-08-16 21:10:17 +0200 | [diff] [blame] | 2740 | if (ev->attr.header.size == size) |
| 2741 | err = process(tool, ev, NULL, NULL); |
| 2742 | else |
| 2743 | err = -E2BIG; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2744 | |
| 2745 | free(ev); |
| 2746 | |
| 2747 | return err; |
| 2748 | } |
| 2749 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2750 | int perf_event__synthesize_attrs(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2751 | struct perf_session *session, |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2752 | perf_event__handler_t process) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2753 | { |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 2754 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2755 | int err = 0; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2756 | |
Robert Richter | 6606f87 | 2012-08-16 21:10:19 +0200 | [diff] [blame] | 2757 | list_for_each_entry(evsel, &session->evlist->entries, node) { |
| 2758 | err = perf_event__synthesize_attr(tool, &evsel->attr, evsel->ids, |
| 2759 | evsel->id, process); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2760 | if (err) { |
| 2761 | pr_debug("failed to create perf header attribute\n"); |
| 2762 | return err; |
| 2763 | } |
| 2764 | } |
| 2765 | |
| 2766 | return err; |
| 2767 | } |
| 2768 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2769 | int perf_event__process_attr(union perf_event *event, |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2770 | struct perf_evlist **pevlist) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2771 | { |
Robert Richter | f4d8343 | 2012-08-16 21:10:17 +0200 | [diff] [blame] | 2772 | u32 i, ids, n_ids; |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2773 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2774 | struct perf_evlist *evlist = *pevlist; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2775 | |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2776 | if (evlist == NULL) { |
| 2777 | *pevlist = evlist = perf_evlist__new(NULL, NULL); |
| 2778 | if (evlist == NULL) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2779 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2780 | } |
| 2781 | |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2782 | evsel = perf_evsel__new(&event->attr.attr, evlist->nr_entries); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2783 | if (evsel == NULL) |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2784 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2785 | |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2786 | perf_evlist__add(evlist, evsel); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2787 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2788 | ids = event->header.size; |
| 2789 | ids -= (void *)&event->attr.id - (void *)event; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2790 | n_ids = ids / sizeof(u64); |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 2791 | /* |
| 2792 | * We don't have the cpu and thread maps on the header, so |
| 2793 | * for allocating the perf_sample_id table we fake 1 cpu and |
| 2794 | * hattr->ids threads. |
| 2795 | */ |
| 2796 | if (perf_evsel__alloc_id(evsel, 1, n_ids)) |
| 2797 | return -ENOMEM; |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2798 | |
| 2799 | for (i = 0; i < n_ids; i++) { |
Arnaldo Carvalho de Melo | 10d0f08 | 2011-11-11 22:45:41 -0200 | [diff] [blame] | 2800 | perf_evlist__id_add(evlist, evsel, 0, i, event->attr.id[i]); |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2801 | } |
| 2802 | |
Tom Zanussi | 2c46dbb | 2010-04-01 23:59:19 -0500 | [diff] [blame] | 2803 | return 0; |
| 2804 | } |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2805 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2806 | int perf_event__synthesize_event_type(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2807 | u64 event_id, char *name, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2808 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2809 | struct machine *machine) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2810 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2811 | union perf_event ev; |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2812 | size_t size = 0; |
| 2813 | int err = 0; |
| 2814 | |
| 2815 | memset(&ev, 0, sizeof(ev)); |
| 2816 | |
| 2817 | ev.event_type.event_type.event_id = event_id; |
| 2818 | memset(ev.event_type.event_type.name, 0, MAX_EVENT_NAME); |
| 2819 | strncpy(ev.event_type.event_type.name, name, MAX_EVENT_NAME - 1); |
| 2820 | |
| 2821 | ev.event_type.header.type = PERF_RECORD_HEADER_EVENT_TYPE; |
Stephane Eranian | f8f4b28 | 2012-01-20 10:49:12 +0100 | [diff] [blame] | 2822 | size = strlen(ev.event_type.event_type.name); |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 2823 | size = PERF_ALIGN(size, sizeof(u64)); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2824 | ev.event_type.header.size = sizeof(ev.event_type) - |
| 2825 | (sizeof(ev.event_type.event_type.name) - size); |
| 2826 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2827 | err = process(tool, &ev, NULL, machine); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2828 | |
| 2829 | return err; |
| 2830 | } |
| 2831 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2832 | int perf_event__synthesize_event_types(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2833 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2834 | struct machine *machine) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2835 | { |
| 2836 | struct perf_trace_event_type *type; |
| 2837 | int i, err = 0; |
| 2838 | |
Robert Richter | db146f0 | 2012-08-16 21:10:20 +0200 | [diff] [blame] | 2839 | for (i = 0; i < trace_event_count; i++) { |
| 2840 | type = &trace_events[i]; |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2841 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2842 | err = perf_event__synthesize_event_type(tool, type->event_id, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2843 | type->name, process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2844 | machine); |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2845 | if (err) { |
| 2846 | pr_debug("failed to create perf header event type\n"); |
| 2847 | return err; |
| 2848 | } |
| 2849 | } |
| 2850 | |
| 2851 | return err; |
| 2852 | } |
| 2853 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 2854 | int perf_event__process_event_type(struct perf_tool *tool __maybe_unused, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2855 | union perf_event *event) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2856 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2857 | if (perf_header__push_event(event->event_type.event_type.event_id, |
| 2858 | event->event_type.event_type.name) < 0) |
Tom Zanussi | cd19a03 | 2010-04-01 23:59:20 -0500 | [diff] [blame] | 2859 | return -ENOMEM; |
| 2860 | |
| 2861 | return 0; |
| 2862 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2863 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2864 | 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] | 2865 | struct perf_evlist *evlist, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2866 | perf_event__handler_t process) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2867 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2868 | union perf_event ev; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 2869 | struct tracing_data *tdata; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2870 | ssize_t size = 0, aligned_size = 0, padding; |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 2871 | int err __maybe_unused = 0; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2872 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 2873 | /* |
| 2874 | * We are going to store the size of the data followed |
| 2875 | * by the data contents. Since the fd descriptor is a pipe, |
| 2876 | * we cannot seek back to store the size of the data once |
| 2877 | * we know it. Instead we: |
| 2878 | * |
| 2879 | * - write the tracing data to the temp file |
| 2880 | * - get/write the data size to pipe |
| 2881 | * - write the tracing data from the temp file |
| 2882 | * to the pipe |
| 2883 | */ |
| 2884 | tdata = tracing_data_get(&evlist->entries, fd, true); |
| 2885 | if (!tdata) |
| 2886 | return -1; |
| 2887 | |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2888 | memset(&ev, 0, sizeof(ev)); |
| 2889 | |
| 2890 | ev.tracing_data.header.type = PERF_RECORD_HEADER_TRACING_DATA; |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 2891 | size = tdata->size; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 2892 | aligned_size = PERF_ALIGN(size, sizeof(u64)); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2893 | padding = aligned_size - size; |
| 2894 | ev.tracing_data.header.size = sizeof(ev.tracing_data); |
| 2895 | ev.tracing_data.size = aligned_size; |
| 2896 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2897 | process(tool, &ev, NULL, NULL); |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2898 | |
Jiri Olsa | 29208e5 | 2011-10-20 15:59:43 +0200 | [diff] [blame] | 2899 | /* |
| 2900 | * The put function will copy all the tracing data |
| 2901 | * stored in temp file to the pipe. |
| 2902 | */ |
| 2903 | tracing_data_put(tdata); |
| 2904 | |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2905 | write_padded(fd, NULL, 0, padding); |
| 2906 | |
| 2907 | return aligned_size; |
| 2908 | } |
| 2909 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2910 | int perf_event__process_tracing_data(union perf_event *event, |
| 2911 | struct perf_session *session) |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2912 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2913 | ssize_t size_read, padding, size = event->tracing_data.size; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2914 | off_t offset = lseek(session->fd, 0, SEEK_CUR); |
| 2915 | char buf[BUFSIZ]; |
| 2916 | |
| 2917 | /* setup for reading amidst mmap */ |
| 2918 | lseek(session->fd, offset + sizeof(struct tracing_data_event), |
| 2919 | SEEK_SET); |
| 2920 | |
Arnaldo Carvalho de Melo | da37896 | 2012-06-27 13:08:42 -0300 | [diff] [blame] | 2921 | size_read = trace_report(session->fd, &session->pevent, |
| 2922 | session->repipe); |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 2923 | padding = PERF_ALIGN(size_read, sizeof(u64)) - size_read; |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2924 | |
Arnaldo Carvalho de Melo | 2caa48a | 2013-01-24 22:34:33 -0300 | [diff] [blame] | 2925 | if (readn(session->fd, buf, padding) < 0) { |
| 2926 | pr_err("%s: reading input file", __func__); |
| 2927 | return -1; |
| 2928 | } |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 2929 | if (session->repipe) { |
| 2930 | int retw = write(STDOUT_FILENO, buf, padding); |
Arnaldo Carvalho de Melo | 2caa48a | 2013-01-24 22:34:33 -0300 | [diff] [blame] | 2931 | if (retw <= 0 || retw != padding) { |
| 2932 | pr_err("%s: repiping tracing data padding", __func__); |
| 2933 | return -1; |
| 2934 | } |
Tom Zanussi | 454c407 | 2010-05-01 01:41:20 -0500 | [diff] [blame] | 2935 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2936 | |
Arnaldo Carvalho de Melo | 2caa48a | 2013-01-24 22:34:33 -0300 | [diff] [blame] | 2937 | if (size_read + padding != size) { |
| 2938 | pr_err("%s: tracing data size mismatch", __func__); |
| 2939 | return -1; |
| 2940 | } |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2941 | |
Namhyung Kim | 831394b | 2012-09-06 11:10:46 +0900 | [diff] [blame] | 2942 | perf_evlist__prepare_tracepoint_events(session->evlist, |
| 2943 | session->pevent); |
Arnaldo Carvalho de Melo | 8b6ee4c | 2012-08-07 23:36:16 -0300 | [diff] [blame] | 2944 | |
Tom Zanussi | 9215545 | 2010-04-01 23:59:21 -0500 | [diff] [blame] | 2945 | return size_read + padding; |
| 2946 | } |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2947 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2948 | int perf_event__synthesize_build_id(struct perf_tool *tool, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2949 | struct dso *pos, u16 misc, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2950 | perf_event__handler_t process, |
Arnaldo Carvalho de Melo | 743eb86 | 2011-11-28 07:56:39 -0200 | [diff] [blame] | 2951 | struct machine *machine) |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2952 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2953 | union perf_event ev; |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2954 | size_t len; |
| 2955 | int err = 0; |
| 2956 | |
| 2957 | if (!pos->hit) |
| 2958 | return err; |
| 2959 | |
| 2960 | memset(&ev, 0, sizeof(ev)); |
| 2961 | |
| 2962 | len = pos->long_name_len + 1; |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 2963 | len = PERF_ALIGN(len, NAME_ALIGN); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2964 | memcpy(&ev.build_id.build_id, pos->build_id, sizeof(pos->build_id)); |
| 2965 | ev.build_id.header.type = PERF_RECORD_HEADER_BUILD_ID; |
| 2966 | ev.build_id.header.misc = misc; |
Arnaldo Carvalho de Melo | 23346f2 | 2010-04-27 21:17:50 -0300 | [diff] [blame] | 2967 | ev.build_id.pid = machine->pid; |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2968 | ev.build_id.header.size = sizeof(ev.build_id) + len; |
| 2969 | memcpy(&ev.build_id.filename, pos->long_name, pos->long_name_len); |
| 2970 | |
Arnaldo Carvalho de Melo | 45694aa | 2011-11-28 08:30:20 -0200 | [diff] [blame] | 2971 | err = process(tool, &ev, NULL, machine); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2972 | |
| 2973 | return err; |
| 2974 | } |
| 2975 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 2976 | int perf_event__process_build_id(struct perf_tool *tool __maybe_unused, |
Arnaldo Carvalho de Melo | d20deb6 | 2011-11-25 08:19:45 -0200 | [diff] [blame] | 2977 | union perf_event *event, |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2978 | struct perf_session *session) |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2979 | { |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 2980 | __event_process_build_id(&event->build_id, |
| 2981 | event->build_id.filename, |
Zhang, Yanmin | a1645ce | 2010-04-19 13:32:50 +0800 | [diff] [blame] | 2982 | session); |
Tom Zanussi | c7929e4 | 2010-04-01 23:59:22 -0500 | [diff] [blame] | 2983 | return 0; |
| 2984 | } |
Stephane Eranian | a1ac1d3 | 2010-06-17 11:39:01 +0200 | [diff] [blame] | 2985 | |
| 2986 | void disable_buildid_cache(void) |
| 2987 | { |
| 2988 | no_buildid_cache = true; |
| 2989 | } |