Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 1 | #include "evsel.h" |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 2 | #include "evlist.h" |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 3 | #include "../perf.h" |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 4 | #include "util.h" |
Arnaldo Carvalho de Melo | 86bd5e8 | 2011-01-03 23:09:46 -0200 | [diff] [blame] | 5 | #include "cpumap.h" |
Arnaldo Carvalho de Melo | fd78260 | 2011-01-18 15:15:24 -0200 | [diff] [blame] | 6 | #include "thread_map.h" |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 7 | |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 8 | #include <unistd.h> |
| 9 | #include <sys/mman.h> |
| 10 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 11 | #include <linux/bitops.h> |
| 12 | #include <linux/hash.h> |
| 13 | |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 14 | #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 15 | #define SID(e, x, y) xyarray__entry(e->id, x, y) |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 16 | |
Lin Ming | 23a2f3a | 2011-01-07 11:11:09 +0800 | [diff] [blame] | 17 | struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx) |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 18 | { |
| 19 | struct perf_evsel *evsel = zalloc(sizeof(*evsel)); |
| 20 | |
| 21 | if (evsel != NULL) { |
| 22 | evsel->idx = idx; |
Lin Ming | 23a2f3a | 2011-01-07 11:11:09 +0800 | [diff] [blame] | 23 | evsel->attr = *attr; |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 24 | INIT_LIST_HEAD(&evsel->node); |
| 25 | } |
| 26 | |
| 27 | return evsel; |
| 28 | } |
| 29 | |
| 30 | int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads) |
| 31 | { |
| 32 | evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int)); |
| 33 | return evsel->fd != NULL ? 0 : -ENOMEM; |
| 34 | } |
| 35 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 36 | int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads) |
| 37 | { |
| 38 | evsel->id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id)); |
| 39 | return evsel->id != NULL ? 0 : -ENOMEM; |
| 40 | } |
| 41 | |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 42 | int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus) |
| 43 | { |
| 44 | evsel->counts = zalloc((sizeof(*evsel->counts) + |
| 45 | (ncpus * sizeof(struct perf_counts_values)))); |
| 46 | return evsel->counts != NULL ? 0 : -ENOMEM; |
| 47 | } |
| 48 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 49 | void perf_evsel__free_fd(struct perf_evsel *evsel) |
| 50 | { |
| 51 | xyarray__delete(evsel->fd); |
| 52 | evsel->fd = NULL; |
| 53 | } |
| 54 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 55 | void perf_evsel__free_id(struct perf_evsel *evsel) |
| 56 | { |
| 57 | xyarray__delete(evsel->id); |
| 58 | evsel->id = NULL; |
| 59 | } |
| 60 | |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 61 | void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads) |
| 62 | { |
| 63 | int cpu, thread; |
| 64 | |
| 65 | for (cpu = 0; cpu < ncpus; cpu++) |
| 66 | for (thread = 0; thread < nthreads; ++thread) { |
| 67 | close(FD(evsel, cpu, thread)); |
| 68 | FD(evsel, cpu, thread) = -1; |
| 69 | } |
| 70 | } |
| 71 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 72 | void perf_evlist__munmap(struct perf_evlist *evlist, int ncpus) |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 73 | { |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 74 | int cpu; |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 75 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 76 | for (cpu = 0; cpu < ncpus; cpu++) { |
| 77 | if (evlist->mmap[cpu].base != NULL) { |
| 78 | munmap(evlist->mmap[cpu].base, evlist->mmap_len); |
| 79 | evlist->mmap[cpu].base = NULL; |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 80 | } |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 81 | } |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 82 | } |
| 83 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 84 | int perf_evlist__alloc_mmap(struct perf_evlist *evlist, int ncpus) |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 85 | { |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 86 | evlist->mmap = zalloc(ncpus * sizeof(struct perf_mmap)); |
| 87 | return evlist->mmap != NULL ? 0 : -ENOMEM; |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 88 | } |
| 89 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 90 | void perf_evsel__delete(struct perf_evsel *evsel) |
| 91 | { |
| 92 | assert(list_empty(&evsel->node)); |
| 93 | xyarray__delete(evsel->fd); |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 94 | xyarray__delete(evsel->id); |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 95 | free(evsel); |
| 96 | } |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 97 | |
| 98 | int __perf_evsel__read_on_cpu(struct perf_evsel *evsel, |
| 99 | int cpu, int thread, bool scale) |
| 100 | { |
| 101 | struct perf_counts_values count; |
| 102 | size_t nv = scale ? 3 : 1; |
| 103 | |
| 104 | if (FD(evsel, cpu, thread) < 0) |
| 105 | return -EINVAL; |
| 106 | |
Arnaldo Carvalho de Melo | 4eed11d | 2011-01-04 00:13:17 -0200 | [diff] [blame] | 107 | if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0) |
| 108 | return -ENOMEM; |
| 109 | |
Arnaldo Carvalho de Melo | c52b12e | 2011-01-03 17:45:52 -0200 | [diff] [blame] | 110 | if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0) |
| 111 | return -errno; |
| 112 | |
| 113 | if (scale) { |
| 114 | if (count.run == 0) |
| 115 | count.val = 0; |
| 116 | else if (count.run < count.ena) |
| 117 | count.val = (u64)((double)count.val * count.ena / count.run + 0.5); |
| 118 | } else |
| 119 | count.ena = count.run = 0; |
| 120 | |
| 121 | evsel->counts->cpu[cpu] = count; |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | int __perf_evsel__read(struct perf_evsel *evsel, |
| 126 | int ncpus, int nthreads, bool scale) |
| 127 | { |
| 128 | size_t nv = scale ? 3 : 1; |
| 129 | int cpu, thread; |
| 130 | struct perf_counts_values *aggr = &evsel->counts->aggr, count; |
| 131 | |
| 132 | aggr->val = 0; |
| 133 | |
| 134 | for (cpu = 0; cpu < ncpus; cpu++) { |
| 135 | for (thread = 0; thread < nthreads; thread++) { |
| 136 | if (FD(evsel, cpu, thread) < 0) |
| 137 | continue; |
| 138 | |
| 139 | if (readn(FD(evsel, cpu, thread), |
| 140 | &count, nv * sizeof(u64)) < 0) |
| 141 | return -errno; |
| 142 | |
| 143 | aggr->val += count.val; |
| 144 | if (scale) { |
| 145 | aggr->ena += count.ena; |
| 146 | aggr->run += count.run; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | evsel->counts->scaled = 0; |
| 152 | if (scale) { |
| 153 | if (aggr->run == 0) { |
| 154 | evsel->counts->scaled = -1; |
| 155 | aggr->val = 0; |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | if (aggr->run < aggr->ena) { |
| 160 | evsel->counts->scaled = 1; |
| 161 | aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5); |
| 162 | } |
| 163 | } else |
| 164 | aggr->ena = aggr->run = 0; |
| 165 | |
| 166 | return 0; |
| 167 | } |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 168 | |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 169 | static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, |
Arnaldo Carvalho de Melo | 9d04f17 | 2011-01-12 00:08:18 -0200 | [diff] [blame] | 170 | struct thread_map *threads, bool group, bool inherit) |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 171 | { |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 172 | int cpu, thread; |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 173 | |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 174 | if (evsel->fd == NULL && |
| 175 | perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0) |
Arnaldo Carvalho de Melo | 4eed11d | 2011-01-04 00:13:17 -0200 | [diff] [blame] | 176 | return -1; |
| 177 | |
Arnaldo Carvalho de Melo | 86bd5e8 | 2011-01-03 23:09:46 -0200 | [diff] [blame] | 178 | for (cpu = 0; cpu < cpus->nr; cpu++) { |
Arnaldo Carvalho de Melo | f08199d | 2011-01-11 23:42:19 -0200 | [diff] [blame] | 179 | int group_fd = -1; |
| 180 | |
Arnaldo Carvalho de Melo | 9d04f17 | 2011-01-12 00:08:18 -0200 | [diff] [blame] | 181 | evsel->attr.inherit = (cpus->map[cpu] < 0) && inherit; |
| 182 | |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 183 | for (thread = 0; thread < threads->nr; thread++) { |
| 184 | FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr, |
| 185 | threads->map[thread], |
Arnaldo Carvalho de Melo | f08199d | 2011-01-11 23:42:19 -0200 | [diff] [blame] | 186 | cpus->map[cpu], |
| 187 | group_fd, 0); |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 188 | if (FD(evsel, cpu, thread) < 0) |
| 189 | goto out_close; |
Arnaldo Carvalho de Melo | f08199d | 2011-01-11 23:42:19 -0200 | [diff] [blame] | 190 | |
| 191 | if (group && group_fd == -1) |
| 192 | group_fd = FD(evsel, cpu, thread); |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 193 | } |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | |
| 198 | out_close: |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 199 | do { |
| 200 | while (--thread >= 0) { |
| 201 | close(FD(evsel, cpu, thread)); |
| 202 | FD(evsel, cpu, thread) = -1; |
| 203 | } |
| 204 | thread = threads->nr; |
| 205 | } while (--cpu >= 0); |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 206 | return -1; |
| 207 | } |
| 208 | |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 209 | static struct { |
| 210 | struct cpu_map map; |
| 211 | int cpus[1]; |
| 212 | } empty_cpu_map = { |
| 213 | .map.nr = 1, |
| 214 | .cpus = { -1, }, |
| 215 | }; |
| 216 | |
| 217 | static struct { |
| 218 | struct thread_map map; |
| 219 | int threads[1]; |
| 220 | } empty_thread_map = { |
| 221 | .map.nr = 1, |
| 222 | .threads = { -1, }, |
| 223 | }; |
| 224 | |
Arnaldo Carvalho de Melo | f08199d | 2011-01-11 23:42:19 -0200 | [diff] [blame] | 225 | int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, |
Arnaldo Carvalho de Melo | 9d04f17 | 2011-01-12 00:08:18 -0200 | [diff] [blame] | 226 | struct thread_map *threads, bool group, bool inherit) |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 227 | { |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 228 | if (cpus == NULL) { |
| 229 | /* Work around old compiler warnings about strict aliasing */ |
| 230 | cpus = &empty_cpu_map.map; |
| 231 | } |
| 232 | |
| 233 | if (threads == NULL) |
| 234 | threads = &empty_thread_map.map; |
| 235 | |
Arnaldo Carvalho de Melo | 9d04f17 | 2011-01-12 00:08:18 -0200 | [diff] [blame] | 236 | return __perf_evsel__open(evsel, cpus, threads, group, inherit); |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 237 | } |
| 238 | |
Arnaldo Carvalho de Melo | f08199d | 2011-01-11 23:42:19 -0200 | [diff] [blame] | 239 | int perf_evsel__open_per_cpu(struct perf_evsel *evsel, |
Arnaldo Carvalho de Melo | 9d04f17 | 2011-01-12 00:08:18 -0200 | [diff] [blame] | 240 | struct cpu_map *cpus, bool group, bool inherit) |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 241 | { |
Arnaldo Carvalho de Melo | 9d04f17 | 2011-01-12 00:08:18 -0200 | [diff] [blame] | 242 | return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group, inherit); |
Arnaldo Carvalho de Melo | 0252208 | 2011-01-04 11:55:27 -0200 | [diff] [blame] | 243 | } |
| 244 | |
Arnaldo Carvalho de Melo | f08199d | 2011-01-11 23:42:19 -0200 | [diff] [blame] | 245 | int perf_evsel__open_per_thread(struct perf_evsel *evsel, |
Arnaldo Carvalho de Melo | 9d04f17 | 2011-01-12 00:08:18 -0200 | [diff] [blame] | 246 | struct thread_map *threads, bool group, bool inherit) |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 247 | { |
Arnaldo Carvalho de Melo | 9d04f17 | 2011-01-12 00:08:18 -0200 | [diff] [blame] | 248 | return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group, inherit); |
Arnaldo Carvalho de Melo | 4829060 | 2011-01-03 17:48:12 -0200 | [diff] [blame] | 249 | } |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 250 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 251 | static int __perf_evlist__mmap(struct perf_evlist *evlist, int cpu, int prot, |
| 252 | int mask, int fd) |
| 253 | { |
| 254 | evlist->mmap[cpu].prev = 0; |
| 255 | evlist->mmap[cpu].mask = mask; |
| 256 | evlist->mmap[cpu].base = mmap(NULL, evlist->mmap_len, prot, |
| 257 | MAP_SHARED, fd, 0); |
| 258 | if (evlist->mmap[cpu].base == MAP_FAILED) |
| 259 | return -1; |
| 260 | |
| 261 | perf_evlist__add_pollfd(evlist, fd); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int perf_evlist__id_hash(struct perf_evlist *evlist, struct perf_evsel *evsel, |
| 266 | int cpu, int thread, int fd) |
| 267 | { |
| 268 | struct perf_sample_id *sid; |
| 269 | u64 read_data[4] = { 0, }; |
| 270 | int hash, id_idx = 1; /* The first entry is the counter value */ |
| 271 | |
| 272 | if (!(evsel->attr.read_format & PERF_FORMAT_ID) || |
| 273 | read(fd, &read_data, sizeof(read_data)) == -1) |
| 274 | return -1; |
| 275 | |
| 276 | if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) |
| 277 | ++id_idx; |
| 278 | if (evsel->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) |
| 279 | ++id_idx; |
| 280 | |
| 281 | sid = SID(evsel, cpu, thread); |
| 282 | sid->id = read_data[id_idx]; |
| 283 | sid->evsel = evsel; |
| 284 | hash = hash_64(sid->id, PERF_EVLIST__HLIST_BITS); |
| 285 | hlist_add_head(&sid->node, &evlist->heads[hash]); |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | /** perf_evlist__mmap - Create per cpu maps to receive events |
| 290 | * |
| 291 | * @evlist - list of events |
| 292 | * @cpus - cpu map being monitored |
| 293 | * @threads - threads map being monitored |
| 294 | * @pages - map length in pages |
| 295 | * @overwrite - overwrite older events? |
| 296 | * |
| 297 | * If overwrite is false the user needs to signal event consuption using: |
| 298 | * |
| 299 | * struct perf_mmap *m = &evlist->mmap[cpu]; |
| 300 | * unsigned int head = perf_mmap__read_head(m); |
| 301 | * |
| 302 | * perf_mmap__write_tail(m, head) |
| 303 | */ |
| 304 | int perf_evlist__mmap(struct perf_evlist *evlist, struct cpu_map *cpus, |
| 305 | struct thread_map *threads, int pages, bool overwrite) |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 306 | { |
| 307 | unsigned int page_size = sysconf(_SC_PAGE_SIZE); |
| 308 | int mask = pages * page_size - 1, cpu; |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 309 | struct perf_evsel *first_evsel, *evsel; |
| 310 | int thread, prot = PROT_READ | (overwrite ? 0 : PROT_WRITE); |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 311 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 312 | if (evlist->mmap == NULL && |
| 313 | perf_evlist__alloc_mmap(evlist, cpus->nr) < 0) |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 314 | return -ENOMEM; |
| 315 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 316 | if (evlist->pollfd == NULL && |
| 317 | perf_evlist__alloc_pollfd(evlist, cpus->nr, threads->nr) < 0) |
| 318 | return -ENOMEM; |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 319 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 320 | evlist->mmap_len = (pages + 1) * page_size; |
| 321 | first_evsel = list_entry(evlist->entries.next, struct perf_evsel, node); |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 322 | |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 323 | list_for_each_entry(evsel, &evlist->entries, node) { |
| 324 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
| 325 | evsel->id == NULL && |
| 326 | perf_evsel__alloc_id(evsel, cpus->nr, threads->nr) < 0) |
| 327 | return -ENOMEM; |
| 328 | |
| 329 | for (cpu = 0; cpu < cpus->nr; cpu++) { |
| 330 | for (thread = 0; thread < threads->nr; thread++) { |
| 331 | int fd = FD(evsel, cpu, thread); |
| 332 | |
| 333 | if (evsel->idx || thread) { |
| 334 | if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, |
| 335 | FD(first_evsel, cpu, 0)) != 0) |
| 336 | goto out_unmap; |
| 337 | } else if (__perf_evlist__mmap(evlist, cpu, prot, mask, fd) < 0) |
| 338 | goto out_unmap; |
| 339 | |
| 340 | if ((evsel->attr.read_format & PERF_FORMAT_ID) && |
| 341 | perf_evlist__id_hash(evlist, evsel, cpu, thread, fd) < 0) |
| 342 | goto out_unmap; |
| 343 | } |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | return 0; |
| 348 | |
| 349 | out_unmap: |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 350 | for (cpu = 0; cpu < cpus->nr; cpu++) { |
| 351 | if (evlist->mmap[cpu].base != NULL) { |
| 352 | munmap(evlist->mmap[cpu].base, evlist->mmap_len); |
| 353 | evlist->mmap[cpu].base = NULL; |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 354 | } |
Arnaldo Carvalho de Melo | 70db753 | 2011-01-12 22:39:13 -0200 | [diff] [blame] | 355 | } |
Arnaldo Carvalho de Melo | 70082dd | 2011-01-12 17:03:24 -0200 | [diff] [blame] | 356 | return -1; |
| 357 | } |
Arnaldo Carvalho de Melo | d0dd74e | 2011-01-21 13:46:41 -0200 | [diff] [blame^] | 358 | |
| 359 | static int event__parse_id_sample(const event_t *event, u64 type, |
| 360 | struct sample_data *sample) |
| 361 | { |
| 362 | const u64 *array = event->sample.array; |
| 363 | |
| 364 | array += ((event->header.size - |
| 365 | sizeof(event->header)) / sizeof(u64)) - 1; |
| 366 | |
| 367 | if (type & PERF_SAMPLE_CPU) { |
| 368 | u32 *p = (u32 *)array; |
| 369 | sample->cpu = *p; |
| 370 | array--; |
| 371 | } |
| 372 | |
| 373 | if (type & PERF_SAMPLE_STREAM_ID) { |
| 374 | sample->stream_id = *array; |
| 375 | array--; |
| 376 | } |
| 377 | |
| 378 | if (type & PERF_SAMPLE_ID) { |
| 379 | sample->id = *array; |
| 380 | array--; |
| 381 | } |
| 382 | |
| 383 | if (type & PERF_SAMPLE_TIME) { |
| 384 | sample->time = *array; |
| 385 | array--; |
| 386 | } |
| 387 | |
| 388 | if (type & PERF_SAMPLE_TID) { |
| 389 | u32 *p = (u32 *)array; |
| 390 | sample->pid = p[0]; |
| 391 | sample->tid = p[1]; |
| 392 | } |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | int event__parse_sample(const event_t *event, u64 type, bool sample_id_all, |
| 398 | struct sample_data *data) |
| 399 | { |
| 400 | const u64 *array; |
| 401 | |
| 402 | data->cpu = data->pid = data->tid = -1; |
| 403 | data->stream_id = data->id = data->time = -1ULL; |
| 404 | |
| 405 | if (event->header.type != PERF_RECORD_SAMPLE) { |
| 406 | if (!sample_id_all) |
| 407 | return 0; |
| 408 | return event__parse_id_sample(event, type, data); |
| 409 | } |
| 410 | |
| 411 | array = event->sample.array; |
| 412 | |
| 413 | if (type & PERF_SAMPLE_IP) { |
| 414 | data->ip = event->ip.ip; |
| 415 | array++; |
| 416 | } |
| 417 | |
| 418 | if (type & PERF_SAMPLE_TID) { |
| 419 | u32 *p = (u32 *)array; |
| 420 | data->pid = p[0]; |
| 421 | data->tid = p[1]; |
| 422 | array++; |
| 423 | } |
| 424 | |
| 425 | if (type & PERF_SAMPLE_TIME) { |
| 426 | data->time = *array; |
| 427 | array++; |
| 428 | } |
| 429 | |
| 430 | if (type & PERF_SAMPLE_ADDR) { |
| 431 | data->addr = *array; |
| 432 | array++; |
| 433 | } |
| 434 | |
| 435 | data->id = -1ULL; |
| 436 | if (type & PERF_SAMPLE_ID) { |
| 437 | data->id = *array; |
| 438 | array++; |
| 439 | } |
| 440 | |
| 441 | if (type & PERF_SAMPLE_STREAM_ID) { |
| 442 | data->stream_id = *array; |
| 443 | array++; |
| 444 | } |
| 445 | |
| 446 | if (type & PERF_SAMPLE_CPU) { |
| 447 | u32 *p = (u32 *)array; |
| 448 | data->cpu = *p; |
| 449 | array++; |
| 450 | } |
| 451 | |
| 452 | if (type & PERF_SAMPLE_PERIOD) { |
| 453 | data->period = *array; |
| 454 | array++; |
| 455 | } |
| 456 | |
| 457 | if (type & PERF_SAMPLE_READ) { |
| 458 | fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n"); |
| 459 | return -1; |
| 460 | } |
| 461 | |
| 462 | if (type & PERF_SAMPLE_CALLCHAIN) { |
| 463 | data->callchain = (struct ip_callchain *)array; |
| 464 | array += 1 + data->callchain->nr; |
| 465 | } |
| 466 | |
| 467 | if (type & PERF_SAMPLE_RAW) { |
| 468 | u32 *p = (u32 *)array; |
| 469 | data->raw_size = *p; |
| 470 | p++; |
| 471 | data->raw_data = p; |
| 472 | } |
| 473 | |
| 474 | return 0; |
| 475 | } |