Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> |
| 3 | * |
| 4 | * Parts came from builtin-{top,stat,record}.c, see those files for further |
| 5 | * copyright notes. |
| 6 | * |
| 7 | * Released under the GPL v2. (and only v2, not any later version) |
| 8 | */ |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 9 | #include <poll.h> |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 10 | #include "cpumap.h" |
| 11 | #include "thread_map.h" |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 12 | #include "evlist.h" |
| 13 | #include "evsel.h" |
| 14 | #include "util.h" |
| 15 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 16 | #include <sys/mman.h> |
| 17 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 18 | #include <linux/bitops.h> |
| 19 | #include <linux/hash.h> |
| 20 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 21 | #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 22 | #define SID(e, x, y) xyarray__entry(e->sample_id, x, y) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 23 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 24 | void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus, |
| 25 | struct thread_map *threads) |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 26 | { |
| 27 | int i; |
| 28 | |
| 29 | for (i = 0; i < PERF_EVLIST__HLIST_SIZE; ++i) |
| 30 | INIT_HLIST_HEAD(&evlist->heads[i]); |
| 31 | INIT_LIST_HEAD(&evlist->entries); |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 32 | perf_evlist__set_maps(evlist, cpus, threads); |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 33 | } |
| 34 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 35 | struct perf_evlist *perf_evlist__new(struct cpu_map *cpus, |
| 36 | struct thread_map *threads) |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 37 | { |
| 38 | struct perf_evlist *evlist = zalloc(sizeof(*evlist)); |
| 39 | |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 40 | if (evlist != NULL) |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 41 | perf_evlist__init(evlist, cpus, threads); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 42 | |
| 43 | return evlist; |
| 44 | } |
| 45 | |
| 46 | static void perf_evlist__purge(struct perf_evlist *evlist) |
| 47 | { |
| 48 | struct perf_evsel *pos, *n; |
| 49 | |
| 50 | list_for_each_entry_safe(pos, n, &evlist->entries, node) { |
| 51 | list_del_init(&pos->node); |
| 52 | perf_evsel__delete(pos); |
| 53 | } |
| 54 | |
| 55 | evlist->nr_entries = 0; |
| 56 | } |
| 57 | |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 58 | void perf_evlist__exit(struct perf_evlist *evlist) |
| 59 | { |
| 60 | free(evlist->mmap); |
| 61 | free(evlist->pollfd); |
| 62 | evlist->mmap = NULL; |
| 63 | evlist->pollfd = NULL; |
| 64 | } |
| 65 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 66 | void perf_evlist__delete(struct perf_evlist *evlist) |
| 67 | { |
| 68 | perf_evlist__purge(evlist); |
Arnaldo Carvalho de Melo | ef1d1af | 2011-01-18 21:41:45 -0200 | [diff] [blame] | 69 | perf_evlist__exit(evlist); |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 70 | free(evlist); |
| 71 | } |
| 72 | |
| 73 | void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry) |
| 74 | { |
| 75 | list_add_tail(&entry->node, &evlist->entries); |
| 76 | ++evlist->nr_entries; |
| 77 | } |
| 78 | |
| 79 | int perf_evlist__add_default(struct perf_evlist *evlist) |
| 80 | { |
| 81 | struct perf_event_attr attr = { |
| 82 | .type = PERF_TYPE_HARDWARE, |
| 83 | .config = PERF_COUNT_HW_CPU_CYCLES, |
| 84 | }; |
| 85 | struct perf_evsel *evsel = perf_evsel__new(&attr, 0); |
| 86 | |
| 87 | if (evsel == NULL) |
Stephane Eranian | cc2d86b | 2011-06-07 18:19:36 +0200 | [diff] [blame] | 88 | goto error; |
| 89 | |
| 90 | /* use strdup() because free(evsel) assumes name is allocated */ |
| 91 | evsel->name = strdup("cycles"); |
| 92 | if (!evsel->name) |
| 93 | goto error_free; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 94 | |
| 95 | perf_evlist__add(evlist, evsel); |
| 96 | return 0; |
Stephane Eranian | cc2d86b | 2011-06-07 18:19:36 +0200 | [diff] [blame] | 97 | error_free: |
| 98 | perf_evsel__delete(evsel); |
| 99 | error: |
| 100 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 101 | } |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 102 | |
Arnaldo Carvalho de Melo | 4152ab3 | 2011-07-25 11:06:19 -0300 | [diff] [blame] | 103 | void perf_evlist__disable(struct perf_evlist *evlist) |
| 104 | { |
| 105 | int cpu, thread; |
| 106 | struct perf_evsel *pos; |
| 107 | |
| 108 | for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { |
| 109 | list_for_each_entry(pos, &evlist->entries, node) { |
| 110 | for (thread = 0; thread < evlist->threads->nr; thread++) |
| 111 | ioctl(FD(pos, cpu, thread), PERF_EVENT_IOC_DISABLE); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
David Ahern | 764e16a3 | 2011-08-25 10:17:55 -0600 | [diff] [blame^] | 116 | void perf_evlist__enable(struct perf_evlist *evlist) |
| 117 | { |
| 118 | int cpu, thread; |
| 119 | struct perf_evsel *pos; |
| 120 | |
| 121 | for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { |
| 122 | list_for_each_entry(pos, &evlist->entries, node) { |
| 123 | for (thread = 0; thread < evlist->threads->nr; thread++) |
| 124 | ioctl(FD(pos, cpu, thread), PERF_EVENT_IOC_ENABLE); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 129 | int perf_evlist__alloc_pollfd(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 130 | { |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 131 | int nfds = evlist->cpus->nr * evlist->threads->nr * evlist->nr_entries; |
Arnaldo Carvalho de Melo | 5c58104 | 2011-01-11 22:30:02 -0200 | [diff] [blame] | 132 | evlist->pollfd = malloc(sizeof(struct pollfd) * nfds); |
| 133 | return evlist->pollfd != NULL ? 0 : -ENOMEM; |
| 134 | } |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 135 | |
| 136 | void perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd) |
| 137 | { |
| 138 | fcntl(fd, F_SETFL, O_NONBLOCK); |
| 139 | evlist->pollfd[evlist->nr_fds].fd = fd; |
| 140 | evlist->pollfd[evlist->nr_fds].events = POLLIN; |
| 141 | evlist->nr_fds++; |
| 142 | } |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 143 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 144 | static void perf_evlist__id_hash(struct perf_evlist *evlist, |
| 145 | struct perf_evsel *evsel, |
| 146 | int cpu, int thread, u64 id) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 147 | { |
Arnaldo Carvalho de Melo | 3d3b5e9 | 2011-03-04 22:29:39 -0300 | [diff] [blame] | 148 | int hash; |
| 149 | struct perf_sample_id *sid = SID(evsel, cpu, thread); |
| 150 | |
| 151 | sid->id = id; |
| 152 | sid->evsel = evsel; |
| 153 | hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); |
| 154 | hlist_add_head(&sid->node, &evlist->heads[hash]); |
| 155 | } |
| 156 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 157 | void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, |
| 158 | int cpu, int thread, u64 id) |
| 159 | { |
| 160 | perf_evlist__id_hash(evlist, evsel, cpu, thread, id); |
| 161 | evsel->id[evsel->ids++] = id; |
| 162 | } |
| 163 | |
| 164 | static int perf_evlist__id_add_fd(struct perf_evlist *evlist, |
| 165 | struct perf_evsel *evsel, |
| 166 | int cpu, int thread, int fd) |
Arnaldo Carvalho de Melo | 3d3b5e9 | 2011-03-04 22:29:39 -0300 | [diff] [blame] | 167 | { |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 168 | u64 read_data[4] = { 0, }; |
Arnaldo Carvalho de Melo | 3d3b5e9 | 2011-03-04 22:29:39 -0300 | [diff] [blame] | 169 | int id_idx = 1; /* The first entry is the counter value */ |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 170 | |
| 171 | if (!(evsel->attr.read_format & PERF_FORMAT_ID) || |
| 172 | read(fd, &read_data, sizeof(read_data)) == -1) |
| 173 | return -1; |
| 174 | |
| 175 | if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 176 | ++id_idx; |
| 177 | if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 178 | ++id_idx; |
| 179 | |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 180 | perf_evlist__id_add(evlist, evsel, cpu, thread, read_data[id_idx]); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 181 | return 0; |
| 182 | } |
| 183 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 184 | struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id) |
| 185 | { |
| 186 | struct hlist_head *head; |
| 187 | struct hlist_node *pos; |
| 188 | struct perf_sample_id *sid; |
| 189 | int hash; |
| 190 | |
| 191 | if (evlist->nr_entries == 1) |
| 192 | return list_entry(evlist->entries.next, struct perf_evsel, node); |
| 193 | |
| 194 | hash = hash_64(id, PERF_EVLIST__HLIST_BITS); |
| 195 | head = &evlist->heads[hash]; |
| 196 | |
| 197 | hlist_for_each_entry(sid, pos, head, node) |
| 198 | if (sid->id == id) |
| 199 | return sid->evsel; |
| 200 | return NULL; |
| 201 | } |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 202 | |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 203 | union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx) |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 204 | { |
| 205 | /* XXX Move this to perf.c, making it generally available */ |
| 206 | unsigned int page_size = sysconf(_SC_PAGE_SIZE); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 207 | struct perf_mmap *md = &evlist->mmap[idx]; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 208 | unsigned int head = perf_mmap__read_head(md); |
| 209 | unsigned int old = md->prev; |
| 210 | unsigned char *data = md->base + page_size; |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 211 | union perf_event *event = NULL; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 212 | |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 213 | if (evlist->overwrite) { |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 214 | /* |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 215 | * If we're further behind than half the buffer, there's a chance |
| 216 | * the writer will bite our tail and mess up the samples under us. |
| 217 | * |
| 218 | * If we somehow ended up ahead of the head, we got messed up. |
| 219 | * |
| 220 | * In either case, truncate and restart at head. |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 221 | */ |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 222 | int diff = head - old; |
| 223 | if (diff > md->mask / 2 || diff < 0) { |
| 224 | fprintf(stderr, "WARNING: failed to keep up with mmap data.\n"); |
| 225 | |
| 226 | /* |
| 227 | * head points to a known good entry, start there. |
| 228 | */ |
| 229 | old = head; |
| 230 | } |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | if (old != head) { |
| 234 | size_t size; |
| 235 | |
Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 236 | event = (union perf_event *)&data[old & md->mask]; |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 237 | size = event->header.size; |
| 238 | |
| 239 | /* |
| 240 | * Event straddles the mmap boundary -- header should always |
| 241 | * be inside due to u64 alignment of output. |
| 242 | */ |
| 243 | if ((old & md->mask) + size != ((old + size) & md->mask)) { |
| 244 | unsigned int offset = old; |
| 245 | unsigned int len = min(sizeof(*event), size), cpy; |
| 246 | void *dst = &evlist->event_copy; |
| 247 | |
| 248 | do { |
| 249 | cpy = min(md->mask + 1 - (offset & md->mask), len); |
| 250 | memcpy(dst, &data[offset & md->mask], cpy); |
| 251 | offset += cpy; |
| 252 | dst += cpy; |
| 253 | len -= cpy; |
| 254 | } while (len); |
| 255 | |
| 256 | event = &evlist->event_copy; |
| 257 | } |
| 258 | |
| 259 | old += size; |
| 260 | } |
| 261 | |
| 262 | md->prev = old; |
Arnaldo Carvalho de Melo | 7bb4115 | 2011-01-29 09:08:13 -0200 | [diff] [blame] | 263 | |
| 264 | if (!evlist->overwrite) |
| 265 | perf_mmap__write_tail(md, old); |
| 266 | |
Arnaldo Carvalho de Melo | 04391de | 2011-01-15 10:40:59 -0200 | [diff] [blame] | 267 | return event; |
| 268 | } |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 269 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 270 | void perf_evlist__munmap(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 271 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 272 | int i; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 273 | |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 274 | for (i = 0; i < evlist->nr_mmaps; i++) { |
| 275 | if (evlist->mmap[i].base != NULL) { |
| 276 | munmap(evlist->mmap[i].base, evlist->mmap_len); |
| 277 | evlist->mmap[i].base = NULL; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 278 | } |
| 279 | } |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 280 | |
| 281 | free(evlist->mmap); |
| 282 | evlist->mmap = NULL; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 283 | } |
| 284 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 285 | int perf_evlist__alloc_mmap(struct perf_evlist *evlist) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 286 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 287 | evlist->nr_mmaps = evlist->cpus->nr; |
| 288 | if (evlist->cpus->map[0] == -1) |
| 289 | evlist->nr_mmaps = evlist->threads->nr; |
| 290 | evlist->mmap = zalloc(evlist->nr_mmaps * sizeof(struct perf_mmap)); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 291 | return evlist->mmap != NULL ? 0 : -ENOMEM; |
| 292 | } |
| 293 | |
Arnaldo Carvalho de Melo | bccdaba | 2011-06-02 10:39:43 -0300 | [diff] [blame] | 294 | static int __perf_evlist__mmap(struct perf_evlist *evlist, |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 295 | int idx, int prot, int mask, int fd) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 296 | { |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 297 | evlist->mmap[idx].prev = 0; |
| 298 | evlist->mmap[idx].mask = mask; |
| 299 | evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot, |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 300 | MAP_SHARED, fd, 0); |
Arnaldo Carvalho de Melo | bccdaba | 2011-06-02 10:39:43 -0300 | [diff] [blame] | 301 | if (evlist->mmap[idx].base == MAP_FAILED) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 302 | return -1; |
| 303 | |
| 304 | perf_evlist__add_pollfd(evlist, fd); |
| 305 | return 0; |
| 306 | } |
| 307 | |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 308 | static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot, int mask) |
| 309 | { |
| 310 | struct perf_evsel *evsel; |
| 311 | int cpu, thread; |
| 312 | |
| 313 | for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { |
| 314 | int output = -1; |
| 315 | |
| 316 | for (thread = 0; thread < evlist->threads->nr; thread++) { |
| 317 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 318 | int fd = FD(evsel, cpu, thread); |
| 319 | |
| 320 | if (output == -1) { |
| 321 | output = fd; |
Arnaldo Carvalho de Melo | bccdaba | 2011-06-02 10:39:43 -0300 | [diff] [blame] | 322 | if (__perf_evlist__mmap(evlist, cpu, |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 323 | prot, mask, output) < 0) |
| 324 | goto out_unmap; |
| 325 | } else { |
| 326 | if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) |
| 327 | goto out_unmap; |
| 328 | } |
| 329 | |
| 330 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
| 331 | perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0) |
| 332 | goto out_unmap; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | |
| 339 | out_unmap: |
| 340 | for (cpu = 0; cpu < evlist->cpus->nr; cpu++) { |
| 341 | if (evlist->mmap[cpu].base != NULL) { |
| 342 | munmap(evlist->mmap[cpu].base, evlist->mmap_len); |
| 343 | evlist->mmap[cpu].base = NULL; |
| 344 | } |
| 345 | } |
| 346 | return -1; |
| 347 | } |
| 348 | |
| 349 | static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot, int mask) |
| 350 | { |
| 351 | struct perf_evsel *evsel; |
| 352 | int thread; |
| 353 | |
| 354 | for (thread = 0; thread < evlist->threads->nr; thread++) { |
| 355 | int output = -1; |
| 356 | |
| 357 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 358 | int fd = FD(evsel, 0, thread); |
| 359 | |
| 360 | if (output == -1) { |
| 361 | output = fd; |
Arnaldo Carvalho de Melo | bccdaba | 2011-06-02 10:39:43 -0300 | [diff] [blame] | 362 | if (__perf_evlist__mmap(evlist, thread, |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 363 | prot, mask, output) < 0) |
| 364 | goto out_unmap; |
| 365 | } else { |
| 366 | if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, output) != 0) |
| 367 | goto out_unmap; |
| 368 | } |
| 369 | |
| 370 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
| 371 | perf_evlist__id_add_fd(evlist, evsel, 0, thread, fd) < 0) |
| 372 | goto out_unmap; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | return 0; |
| 377 | |
| 378 | out_unmap: |
| 379 | for (thread = 0; thread < evlist->threads->nr; thread++) { |
| 380 | if (evlist->mmap[thread].base != NULL) { |
| 381 | munmap(evlist->mmap[thread].base, evlist->mmap_len); |
| 382 | evlist->mmap[thread].base = NULL; |
| 383 | } |
| 384 | } |
| 385 | return -1; |
| 386 | } |
| 387 | |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 388 | /** perf_evlist__mmap - Create per cpu maps to receive events |
| 389 | * |
| 390 | * @evlist - list of events |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 391 | * @pages - map length in pages |
| 392 | * @overwrite - overwrite older events? |
| 393 | * |
| 394 | * If overwrite is false the user needs to signal event consuption using: |
| 395 | * |
| 396 | * struct perf_mmap *m = &evlist->mmap[cpu]; |
| 397 | * unsigned int head = perf_mmap__read_head(m); |
| 398 | * |
| 399 | * perf_mmap__write_tail(m, head) |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 400 | * |
| 401 | * Using perf_evlist__read_on_cpu does this automatically. |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 402 | */ |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 403 | int perf_evlist__mmap(struct perf_evlist *evlist, int pages, bool overwrite) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 404 | { |
| 405 | unsigned int page_size = sysconf(_SC_PAGE_SIZE); |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 406 | int mask = pages * page_size - 1; |
| 407 | struct perf_evsel *evsel; |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 408 | const struct cpu_map *cpus = evlist->cpus; |
| 409 | const struct thread_map *threads = evlist->threads; |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 410 | int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 411 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 412 | if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 413 | return -ENOMEM; |
| 414 | |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 415 | if (evlist->pollfd == NULL && perf_evlist__alloc_pollfd(evlist) < 0) |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 416 | return -ENOMEM; |
| 417 | |
| 418 | evlist->overwrite = overwrite; |
| 419 | evlist->mmap_len = (pages + 1) * page_size; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 420 | |
| 421 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 422 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
Arnaldo Carvalho de Melo | a91e543 | 2011-03-10 11:15:54 -0300 | [diff] [blame] | 423 | evsel->sample_id == NULL && |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 424 | perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0) |
| 425 | return -ENOMEM; |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 426 | } |
| 427 | |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 428 | if (evlist->cpus->map[0] == -1) |
| 429 | return perf_evlist__mmap_per_thread(evlist, prot, mask); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 430 | |
Arnaldo Carvalho de Melo | aece948 | 2011-05-15 09:39:00 -0300 | [diff] [blame] | 431 | return perf_evlist__mmap_per_cpu(evlist, prot, mask); |
Arnaldo Carvalho de Melo | f8a9530 | 2011-01-30 10:46:46 -0200 | [diff] [blame] | 432 | } |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 433 | |
| 434 | int perf_evlist__create_maps(struct perf_evlist *evlist, pid_t target_pid, |
| 435 | pid_t target_tid, const char *cpu_list) |
| 436 | { |
| 437 | evlist->threads = thread_map__new(target_pid, target_tid); |
| 438 | |
| 439 | if (evlist->threads == NULL) |
| 440 | return -1; |
| 441 | |
Arnaldo Carvalho de Melo | b901941 | 2011-04-25 16:25:20 -0300 | [diff] [blame] | 442 | if (cpu_list == NULL && target_tid != -1) |
Arnaldo Carvalho de Melo | 7e2ed09 | 2011-01-30 11:59:43 -0200 | [diff] [blame] | 443 | evlist->cpus = cpu_map__dummy_new(); |
| 444 | else |
| 445 | evlist->cpus = cpu_map__new(cpu_list); |
| 446 | |
| 447 | if (evlist->cpus == NULL) |
| 448 | goto out_delete_threads; |
| 449 | |
| 450 | return 0; |
| 451 | |
| 452 | out_delete_threads: |
| 453 | thread_map__delete(evlist->threads); |
| 454 | return -1; |
| 455 | } |
| 456 | |
| 457 | void perf_evlist__delete_maps(struct perf_evlist *evlist) |
| 458 | { |
| 459 | cpu_map__delete(evlist->cpus); |
| 460 | thread_map__delete(evlist->threads); |
| 461 | evlist->cpus = NULL; |
| 462 | evlist->threads = NULL; |
| 463 | } |
Frederic Weisbecker | 0a10247 | 2011-02-26 04:51:54 +0100 | [diff] [blame] | 464 | |
| 465 | int perf_evlist__set_filters(struct perf_evlist *evlist) |
| 466 | { |
| 467 | const struct thread_map *threads = evlist->threads; |
| 468 | const struct cpu_map *cpus = evlist->cpus; |
| 469 | struct perf_evsel *evsel; |
| 470 | char *filter; |
| 471 | int thread; |
| 472 | int cpu; |
| 473 | int err; |
| 474 | int fd; |
| 475 | |
| 476 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 477 | filter = evsel->filter; |
| 478 | if (!filter) |
| 479 | continue; |
| 480 | for (cpu = 0; cpu < cpus->nr; cpu++) { |
| 481 | for (thread = 0; thread < threads->nr; thread++) { |
| 482 | fd = FD(evsel, cpu, thread); |
| 483 | err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter); |
| 484 | if (err) |
| 485 | return err; |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | return 0; |
| 491 | } |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 492 | |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 493 | bool perf_evlist__valid_sample_type(const struct perf_evlist *evlist) |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 494 | { |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 495 | struct perf_evsel *pos, *first; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 496 | |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 497 | pos = first = list_entry(evlist->entries.next, struct perf_evsel, node); |
| 498 | |
| 499 | list_for_each_entry_continue(pos, &evlist->entries, node) { |
| 500 | if (first->attr.sample_type != pos->attr.sample_type) |
| 501 | return false; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 502 | } |
| 503 | |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 504 | return true; |
| 505 | } |
| 506 | |
| 507 | u64 perf_evlist__sample_type(const struct perf_evlist *evlist) |
| 508 | { |
| 509 | struct perf_evsel *first; |
| 510 | |
| 511 | first = list_entry(evlist->entries.next, struct perf_evsel, node); |
| 512 | return first->attr.sample_type; |
| 513 | } |
| 514 | |
| 515 | bool perf_evlist__valid_sample_id_all(const struct perf_evlist *evlist) |
| 516 | { |
| 517 | struct perf_evsel *pos, *first; |
| 518 | |
| 519 | pos = first = list_entry(evlist->entries.next, struct perf_evsel, node); |
| 520 | |
| 521 | list_for_each_entry_continue(pos, &evlist->entries, node) { |
| 522 | if (first->attr.sample_id_all != pos->attr.sample_id_all) |
| 523 | return false; |
| 524 | } |
| 525 | |
| 526 | return true; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | bool perf_evlist__sample_id_all(const struct perf_evlist *evlist) |
| 530 | { |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 531 | struct perf_evsel *first; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 532 | |
Arnaldo Carvalho de Melo | c2a7065 | 2011-06-02 11:04:54 -0300 | [diff] [blame] | 533 | first = list_entry(evlist->entries.next, struct perf_evsel, node); |
| 534 | return first->attr.sample_id_all; |
Frederic Weisbecker | 7442996 | 2011-05-21 17:49:00 +0200 | [diff] [blame] | 535 | } |