2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
7 * Released under the GPL v2. (and only v2, not any later version)
11 #include "thread_map.h"
19 #include <linux/bitops.h>
20 #include <linux/hash.h>
22 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
23 #define SID(e, x, y) xyarray__entry(e->sample_id, x, y)
25 void perf_evlist__init(struct perf_evlist
*evlist
, struct cpu_map
*cpus
,
26 struct thread_map
*threads
)
30 for (i
= 0; i
< PERF_EVLIST__HLIST_SIZE
; ++i
)
31 INIT_HLIST_HEAD(&evlist
->heads
[i
]);
32 INIT_LIST_HEAD(&evlist
->entries
);
33 perf_evlist__set_maps(evlist
, cpus
, threads
);
36 struct perf_evlist
*perf_evlist__new(struct cpu_map
*cpus
,
37 struct thread_map
*threads
)
39 struct perf_evlist
*evlist
= zalloc(sizeof(*evlist
));
42 perf_evlist__init(evlist
, cpus
, threads
);
47 static void perf_evlist__purge(struct perf_evlist
*evlist
)
49 struct perf_evsel
*pos
, *n
;
51 list_for_each_entry_safe(pos
, n
, &evlist
->entries
, node
) {
52 list_del_init(&pos
->node
);
53 perf_evsel__delete(pos
);
56 evlist
->nr_entries
= 0;
59 void perf_evlist__exit(struct perf_evlist
*evlist
)
64 evlist
->pollfd
= NULL
;
67 void perf_evlist__delete(struct perf_evlist
*evlist
)
69 perf_evlist__purge(evlist
);
70 perf_evlist__exit(evlist
);
74 void perf_evlist__add(struct perf_evlist
*evlist
, struct perf_evsel
*entry
)
76 list_add_tail(&entry
->node
, &evlist
->entries
);
80 int perf_evlist__add_default(struct perf_evlist
*evlist
)
82 struct perf_event_attr attr
= {
83 .type
= PERF_TYPE_HARDWARE
,
84 .config
= PERF_COUNT_HW_CPU_CYCLES
,
86 struct perf_evsel
*evsel
= perf_evsel__new(&attr
, 0);
91 perf_evlist__add(evlist
, evsel
);
95 int perf_evlist__alloc_pollfd(struct perf_evlist
*evlist
)
97 int nfds
= evlist
->cpus
->nr
* evlist
->threads
->nr
* evlist
->nr_entries
;
98 evlist
->pollfd
= malloc(sizeof(struct pollfd
) * nfds
);
99 return evlist
->pollfd
!= NULL
? 0 : -ENOMEM
;
102 void perf_evlist__add_pollfd(struct perf_evlist
*evlist
, int fd
)
104 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
105 evlist
->pollfd
[evlist
->nr_fds
].fd
= fd
;
106 evlist
->pollfd
[evlist
->nr_fds
].events
= POLLIN
;
110 static void perf_evlist__id_hash(struct perf_evlist
*evlist
,
111 struct perf_evsel
*evsel
,
112 int cpu
, int thread
, u64 id
)
115 struct perf_sample_id
*sid
= SID(evsel
, cpu
, thread
);
119 hash
= hash_64(sid
->id
, PERF_EVLIST__HLIST_BITS
);
120 hlist_add_head(&sid
->node
, &evlist
->heads
[hash
]);
123 void perf_evlist__id_add(struct perf_evlist
*evlist
, struct perf_evsel
*evsel
,
124 int cpu
, int thread
, u64 id
)
126 perf_evlist__id_hash(evlist
, evsel
, cpu
, thread
, id
);
127 evsel
->id
[evsel
->ids
++] = id
;
130 static int perf_evlist__id_add_fd(struct perf_evlist
*evlist
,
131 struct perf_evsel
*evsel
,
132 int cpu
, int thread
, int fd
)
134 u64 read_data
[4] = { 0, };
135 int id_idx
= 1; /* The first entry is the counter value */
137 if (!(evsel
->attr
.read_format
& PERF_FORMAT_ID
) ||
138 read(fd
, &read_data
, sizeof(read_data
)) == -1)
141 if (evsel
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
143 if (evsel
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
146 perf_evlist__id_add(evlist
, evsel
, cpu
, thread
, read_data
[id_idx
]);
150 struct perf_evsel
*perf_evlist__id2evsel(struct perf_evlist
*evlist
, u64 id
)
152 struct hlist_head
*head
;
153 struct hlist_node
*pos
;
154 struct perf_sample_id
*sid
;
157 if (evlist
->nr_entries
== 1)
158 return list_entry(evlist
->entries
.next
, struct perf_evsel
, node
);
160 hash
= hash_64(id
, PERF_EVLIST__HLIST_BITS
);
161 head
= &evlist
->heads
[hash
];
163 hlist_for_each_entry(sid
, pos
, head
, node
)
169 union perf_event
*perf_evlist__mmap_read(struct perf_evlist
*evlist
, int idx
)
171 /* XXX Move this to perf.c, making it generally available */
172 unsigned int page_size
= sysconf(_SC_PAGE_SIZE
);
173 struct perf_mmap
*md
= &evlist
->mmap
[idx
];
174 unsigned int head
= perf_mmap__read_head(md
);
175 unsigned int old
= md
->prev
;
176 unsigned char *data
= md
->base
+ page_size
;
177 union perf_event
*event
= NULL
;
179 if (evlist
->overwrite
) {
181 * If we're further behind than half the buffer, there's a chance
182 * the writer will bite our tail and mess up the samples under us.
184 * If we somehow ended up ahead of the head, we got messed up.
186 * In either case, truncate and restart at head.
188 int diff
= head
- old
;
189 if (diff
> md
->mask
/ 2 || diff
< 0) {
190 fprintf(stderr
, "WARNING: failed to keep up with mmap data.\n");
193 * head points to a known good entry, start there.
202 event
= (union perf_event
*)&data
[old
& md
->mask
];
203 size
= event
->header
.size
;
206 * Event straddles the mmap boundary -- header should always
207 * be inside due to u64 alignment of output.
209 if ((old
& md
->mask
) + size
!= ((old
+ size
) & md
->mask
)) {
210 unsigned int offset
= old
;
211 unsigned int len
= min(sizeof(*event
), size
), cpy
;
212 void *dst
= &evlist
->event_copy
;
215 cpy
= min(md
->mask
+ 1 - (offset
& md
->mask
), len
);
216 memcpy(dst
, &data
[offset
& md
->mask
], cpy
);
222 event
= &evlist
->event_copy
;
230 if (!evlist
->overwrite
)
231 perf_mmap__write_tail(md
, old
);
236 void perf_evlist__munmap(struct perf_evlist
*evlist
)
240 for (i
= 0; i
< evlist
->nr_mmaps
; i
++) {
241 if (evlist
->mmap
[i
].base
!= NULL
) {
242 munmap(evlist
->mmap
[i
].base
, evlist
->mmap_len
);
243 evlist
->mmap
[i
].base
= NULL
;
251 int perf_evlist__alloc_mmap(struct perf_evlist
*evlist
)
253 evlist
->nr_mmaps
= evlist
->cpus
->nr
;
254 if (evlist
->cpus
->map
[0] == -1)
255 evlist
->nr_mmaps
= evlist
->threads
->nr
;
256 evlist
->mmap
= zalloc(evlist
->nr_mmaps
* sizeof(struct perf_mmap
));
257 return evlist
->mmap
!= NULL
? 0 : -ENOMEM
;
260 static int __perf_evlist__mmap(struct perf_evlist
*evlist
, struct perf_evsel
*evsel
,
261 int idx
, int prot
, int mask
, int fd
)
263 evlist
->mmap
[idx
].prev
= 0;
264 evlist
->mmap
[idx
].mask
= mask
;
265 evlist
->mmap
[idx
].base
= mmap(NULL
, evlist
->mmap_len
, prot
,
267 if (evlist
->mmap
[idx
].base
== MAP_FAILED
) {
268 if (evlist
->cpus
->map
[idx
] == -1 && evsel
->attr
.inherit
)
269 ui__warning("Inherit is not allowed on per-task "
270 "events using mmap.\n");
274 perf_evlist__add_pollfd(evlist
, fd
);
278 static int perf_evlist__mmap_per_cpu(struct perf_evlist
*evlist
, int prot
, int mask
)
280 struct perf_evsel
*evsel
;
283 for (cpu
= 0; cpu
< evlist
->cpus
->nr
; cpu
++) {
286 for (thread
= 0; thread
< evlist
->threads
->nr
; thread
++) {
287 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
288 int fd
= FD(evsel
, cpu
, thread
);
292 if (__perf_evlist__mmap(evlist
, evsel
, cpu
,
293 prot
, mask
, output
) < 0)
296 if (ioctl(fd
, PERF_EVENT_IOC_SET_OUTPUT
, output
) != 0)
300 if ((evsel
->attr
.read_format
& PERF_FORMAT_ID
) &&
301 perf_evlist__id_add_fd(evlist
, evsel
, cpu
, thread
, fd
) < 0)
310 for (cpu
= 0; cpu
< evlist
->cpus
->nr
; cpu
++) {
311 if (evlist
->mmap
[cpu
].base
!= NULL
) {
312 munmap(evlist
->mmap
[cpu
].base
, evlist
->mmap_len
);
313 evlist
->mmap
[cpu
].base
= NULL
;
319 static int perf_evlist__mmap_per_thread(struct perf_evlist
*evlist
, int prot
, int mask
)
321 struct perf_evsel
*evsel
;
324 for (thread
= 0; thread
< evlist
->threads
->nr
; thread
++) {
327 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
328 int fd
= FD(evsel
, 0, thread
);
332 if (__perf_evlist__mmap(evlist
, evsel
, thread
,
333 prot
, mask
, output
) < 0)
336 if (ioctl(fd
, PERF_EVENT_IOC_SET_OUTPUT
, output
) != 0)
340 if ((evsel
->attr
.read_format
& PERF_FORMAT_ID
) &&
341 perf_evlist__id_add_fd(evlist
, evsel
, 0, thread
, fd
) < 0)
349 for (thread
= 0; thread
< evlist
->threads
->nr
; thread
++) {
350 if (evlist
->mmap
[thread
].base
!= NULL
) {
351 munmap(evlist
->mmap
[thread
].base
, evlist
->mmap_len
);
352 evlist
->mmap
[thread
].base
= NULL
;
358 /** perf_evlist__mmap - Create per cpu maps to receive events
360 * @evlist - list of events
361 * @pages - map length in pages
362 * @overwrite - overwrite older events?
364 * If overwrite is false the user needs to signal event consuption using:
366 * struct perf_mmap *m = &evlist->mmap[cpu];
367 * unsigned int head = perf_mmap__read_head(m);
369 * perf_mmap__write_tail(m, head)
371 * Using perf_evlist__read_on_cpu does this automatically.
373 int perf_evlist__mmap(struct perf_evlist
*evlist
, int pages
, bool overwrite
)
375 unsigned int page_size
= sysconf(_SC_PAGE_SIZE
);
376 int mask
= pages
* page_size
- 1;
377 struct perf_evsel
*evsel
;
378 const struct cpu_map
*cpus
= evlist
->cpus
;
379 const struct thread_map
*threads
= evlist
->threads
;
380 int prot
= PROT_READ
| (overwrite
? 0 : PROT_WRITE
);
382 if (evlist
->mmap
== NULL
&& perf_evlist__alloc_mmap(evlist
) < 0)
385 if (evlist
->pollfd
== NULL
&& perf_evlist__alloc_pollfd(evlist
) < 0)
388 evlist
->overwrite
= overwrite
;
389 evlist
->mmap_len
= (pages
+ 1) * page_size
;
391 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
392 if ((evsel
->attr
.read_format
& PERF_FORMAT_ID
) &&
393 evsel
->sample_id
== NULL
&&
394 perf_evsel__alloc_id(evsel
, cpus
->nr
, threads
->nr
) < 0)
398 if (evlist
->cpus
->map
[0] == -1)
399 return perf_evlist__mmap_per_thread(evlist
, prot
, mask
);
401 return perf_evlist__mmap_per_cpu(evlist
, prot
, mask
);
404 int perf_evlist__create_maps(struct perf_evlist
*evlist
, pid_t target_pid
,
405 pid_t target_tid
, const char *cpu_list
)
407 evlist
->threads
= thread_map__new(target_pid
, target_tid
);
409 if (evlist
->threads
== NULL
)
412 if (cpu_list
== NULL
&& target_tid
!= -1)
413 evlist
->cpus
= cpu_map__dummy_new();
415 evlist
->cpus
= cpu_map__new(cpu_list
);
417 if (evlist
->cpus
== NULL
)
418 goto out_delete_threads
;
423 thread_map__delete(evlist
->threads
);
427 void perf_evlist__delete_maps(struct perf_evlist
*evlist
)
429 cpu_map__delete(evlist
->cpus
);
430 thread_map__delete(evlist
->threads
);
432 evlist
->threads
= NULL
;
435 int perf_evlist__set_filters(struct perf_evlist
*evlist
)
437 const struct thread_map
*threads
= evlist
->threads
;
438 const struct cpu_map
*cpus
= evlist
->cpus
;
439 struct perf_evsel
*evsel
;
446 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
447 filter
= evsel
->filter
;
450 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
451 for (thread
= 0; thread
< threads
->nr
; thread
++) {
452 fd
= FD(evsel
, cpu
, thread
);
453 err
= ioctl(fd
, PERF_EVENT_IOC_SET_FILTER
, filter
);
463 u64
perf_evlist__sample_type(struct perf_evlist
*evlist
)
465 struct perf_evsel
*pos
;
468 list_for_each_entry(pos
, &evlist
->entries
, node
) {
470 type
= pos
->attr
.sample_type
;
471 else if (type
!= pos
->attr
.sample_type
)
472 die("non matching sample_type");
478 bool perf_evlist__sample_id_all(const struct perf_evlist
*evlist
)
480 bool value
= false, first
= true;
481 struct perf_evsel
*pos
;
483 list_for_each_entry(pos
, &evlist
->entries
, node
) {
485 value
= pos
->attr
.sample_id_all
;
487 } else if (value
!= pos
->attr
.sample_id_all
)
488 die("non matching sample_id_all");