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