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"
18 #include <linux/bitops.h>
19 #include <linux/hash.h>
21 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
22 #define SID(e, x, y) xyarray__entry(e->id, x, y)
24 void perf_evlist__init(struct perf_evlist
*evlist
, struct cpu_map
*cpus
,
25 struct thread_map
*threads
)
29 for (i
= 0; i
< PERF_EVLIST__HLIST_SIZE
; ++i
)
30 INIT_HLIST_HEAD(&evlist
->heads
[i
]);
31 INIT_LIST_HEAD(&evlist
->entries
);
32 perf_evlist__set_maps(evlist
, cpus
, threads
);
35 struct perf_evlist
*perf_evlist__new(struct cpu_map
*cpus
,
36 struct thread_map
*threads
)
38 struct perf_evlist
*evlist
= zalloc(sizeof(*evlist
));
41 perf_evlist__init(evlist
, cpus
, threads
);
46 static void perf_evlist__purge(struct perf_evlist
*evlist
)
48 struct perf_evsel
*pos
, *n
;
50 list_for_each_entry_safe(pos
, n
, &evlist
->entries
, node
) {
51 list_del_init(&pos
->node
);
52 perf_evsel__delete(pos
);
55 evlist
->nr_entries
= 0;
58 void perf_evlist__exit(struct perf_evlist
*evlist
)
63 evlist
->pollfd
= NULL
;
66 void perf_evlist__delete(struct perf_evlist
*evlist
)
68 perf_evlist__purge(evlist
);
69 perf_evlist__exit(evlist
);
73 void perf_evlist__add(struct perf_evlist
*evlist
, struct perf_evsel
*entry
)
75 list_add_tail(&entry
->node
, &evlist
->entries
);
79 int perf_evlist__add_default(struct perf_evlist
*evlist
)
81 struct perf_event_attr attr
= {
82 .type
= PERF_TYPE_HARDWARE
,
83 .config
= PERF_COUNT_HW_CPU_CYCLES
,
85 struct perf_evsel
*evsel
= perf_evsel__new(&attr
, 0);
90 perf_evlist__add(evlist
, evsel
);
94 int perf_evlist__alloc_pollfd(struct perf_evlist
*evlist
)
96 int nfds
= evlist
->cpus
->nr
* evlist
->threads
->nr
* evlist
->nr_entries
;
97 evlist
->pollfd
= malloc(sizeof(struct pollfd
) * nfds
);
98 return evlist
->pollfd
!= NULL
? 0 : -ENOMEM
;
101 void perf_evlist__add_pollfd(struct perf_evlist
*evlist
, int fd
)
103 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
104 evlist
->pollfd
[evlist
->nr_fds
].fd
= fd
;
105 evlist
->pollfd
[evlist
->nr_fds
].events
= POLLIN
;
109 void perf_evlist__id_hash(struct perf_evlist
*evlist
, struct perf_evsel
*evsel
,
110 int cpu
, int thread
, u64 id
)
113 struct perf_sample_id
*sid
= SID(evsel
, cpu
, thread
);
117 hash
= hash_64(sid
->id
, PERF_EVLIST__HLIST_BITS
);
118 hlist_add_head(&sid
->node
, &evlist
->heads
[hash
]);
121 static int perf_evlist__id_hash_fd(struct perf_evlist
*evlist
,
122 struct perf_evsel
*evsel
,
123 int cpu
, int thread
, int fd
)
125 u64 read_data
[4] = { 0, };
126 int id_idx
= 1; /* The first entry is the counter value */
128 if (!(evsel
->attr
.read_format
& PERF_FORMAT_ID
) ||
129 read(fd
, &read_data
, sizeof(read_data
)) == -1)
132 if (evsel
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_ENABLED
)
134 if (evsel
->attr
.read_format
& PERF_FORMAT_TOTAL_TIME_RUNNING
)
137 perf_evlist__id_hash(evlist
, evsel
, cpu
, thread
, read_data
[id_idx
]);
141 struct perf_evsel
*perf_evlist__id2evsel(struct perf_evlist
*evlist
, u64 id
)
143 struct hlist_head
*head
;
144 struct hlist_node
*pos
;
145 struct perf_sample_id
*sid
;
148 if (evlist
->nr_entries
== 1)
149 return list_entry(evlist
->entries
.next
, struct perf_evsel
, node
);
151 hash
= hash_64(id
, PERF_EVLIST__HLIST_BITS
);
152 head
= &evlist
->heads
[hash
];
154 hlist_for_each_entry(sid
, pos
, head
, node
)
160 union perf_event
*perf_evlist__read_on_cpu(struct perf_evlist
*evlist
, int cpu
)
162 /* XXX Move this to perf.c, making it generally available */
163 unsigned int page_size
= sysconf(_SC_PAGE_SIZE
);
164 struct perf_mmap
*md
= &evlist
->mmap
[cpu
];
165 unsigned int head
= perf_mmap__read_head(md
);
166 unsigned int old
= md
->prev
;
167 unsigned char *data
= md
->base
+ page_size
;
168 union perf_event
*event
= NULL
;
170 if (evlist
->overwrite
) {
172 * If we're further behind than half the buffer, there's a chance
173 * the writer will bite our tail and mess up the samples under us.
175 * If we somehow ended up ahead of the head, we got messed up.
177 * In either case, truncate and restart at head.
179 int diff
= head
- old
;
180 if (diff
> md
->mask
/ 2 || diff
< 0) {
181 fprintf(stderr
, "WARNING: failed to keep up with mmap data.\n");
184 * head points to a known good entry, start there.
193 event
= (union perf_event
*)&data
[old
& md
->mask
];
194 size
= event
->header
.size
;
197 * Event straddles the mmap boundary -- header should always
198 * be inside due to u64 alignment of output.
200 if ((old
& md
->mask
) + size
!= ((old
+ size
) & md
->mask
)) {
201 unsigned int offset
= old
;
202 unsigned int len
= min(sizeof(*event
), size
), cpy
;
203 void *dst
= &evlist
->event_copy
;
206 cpy
= min(md
->mask
+ 1 - (offset
& md
->mask
), len
);
207 memcpy(dst
, &data
[offset
& md
->mask
], cpy
);
213 event
= &evlist
->event_copy
;
221 if (!evlist
->overwrite
)
222 perf_mmap__write_tail(md
, old
);
227 void perf_evlist__munmap(struct perf_evlist
*evlist
)
231 for (cpu
= 0; cpu
< evlist
->cpus
->nr
; cpu
++) {
232 if (evlist
->mmap
[cpu
].base
!= NULL
) {
233 munmap(evlist
->mmap
[cpu
].base
, evlist
->mmap_len
);
234 evlist
->mmap
[cpu
].base
= NULL
;
239 int perf_evlist__alloc_mmap(struct perf_evlist
*evlist
)
241 evlist
->mmap
= zalloc(evlist
->cpus
->nr
* sizeof(struct perf_mmap
));
242 return evlist
->mmap
!= NULL
? 0 : -ENOMEM
;
245 static int __perf_evlist__mmap(struct perf_evlist
*evlist
, int cpu
, int prot
,
248 evlist
->mmap
[cpu
].prev
= 0;
249 evlist
->mmap
[cpu
].mask
= mask
;
250 evlist
->mmap
[cpu
].base
= mmap(NULL
, evlist
->mmap_len
, prot
,
252 if (evlist
->mmap
[cpu
].base
== MAP_FAILED
)
255 perf_evlist__add_pollfd(evlist
, fd
);
259 /** perf_evlist__mmap - Create per cpu maps to receive events
261 * @evlist - list of events
262 * @pages - map length in pages
263 * @overwrite - overwrite older events?
265 * If overwrite is false the user needs to signal event consuption using:
267 * struct perf_mmap *m = &evlist->mmap[cpu];
268 * unsigned int head = perf_mmap__read_head(m);
270 * perf_mmap__write_tail(m, head)
272 * Using perf_evlist__read_on_cpu does this automatically.
274 int perf_evlist__mmap(struct perf_evlist
*evlist
, int pages
, bool overwrite
)
276 unsigned int page_size
= sysconf(_SC_PAGE_SIZE
);
277 int mask
= pages
* page_size
- 1, cpu
;
278 struct perf_evsel
*first_evsel
, *evsel
;
279 const struct cpu_map
*cpus
= evlist
->cpus
;
280 const struct thread_map
*threads
= evlist
->threads
;
281 int thread
, prot
= PROT_READ
| (overwrite
? 0 : PROT_WRITE
);
283 if (evlist
->mmap
== NULL
&& perf_evlist__alloc_mmap(evlist
) < 0)
286 if (evlist
->pollfd
== NULL
&& perf_evlist__alloc_pollfd(evlist
) < 0)
289 evlist
->overwrite
= overwrite
;
290 evlist
->mmap_len
= (pages
+ 1) * page_size
;
291 first_evsel
= list_entry(evlist
->entries
.next
, struct perf_evsel
, node
);
293 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
294 if ((evsel
->attr
.read_format
& PERF_FORMAT_ID
) &&
296 perf_evsel__alloc_id(evsel
, cpus
->nr
, threads
->nr
) < 0)
299 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
300 for (thread
= 0; thread
< threads
->nr
; thread
++) {
301 int fd
= FD(evsel
, cpu
, thread
);
303 if (evsel
->idx
|| thread
) {
304 if (ioctl(fd
, PERF_EVENT_IOC_SET_OUTPUT
,
305 FD(first_evsel
, cpu
, 0)) != 0)
307 } else if (__perf_evlist__mmap(evlist
, cpu
, prot
, mask
, fd
) < 0)
310 if ((evsel
->attr
.read_format
& PERF_FORMAT_ID
) &&
311 perf_evlist__id_hash_fd(evlist
, evsel
, cpu
, thread
, fd
) < 0)
320 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
321 if (evlist
->mmap
[cpu
].base
!= NULL
) {
322 munmap(evlist
->mmap
[cpu
].base
, evlist
->mmap_len
);
323 evlist
->mmap
[cpu
].base
= NULL
;
329 int perf_evlist__create_maps(struct perf_evlist
*evlist
, pid_t target_pid
,
330 pid_t target_tid
, const char *cpu_list
)
332 evlist
->threads
= thread_map__new(target_pid
, target_tid
);
334 if (evlist
->threads
== NULL
)
337 if (target_tid
!= -1)
338 evlist
->cpus
= cpu_map__dummy_new();
340 evlist
->cpus
= cpu_map__new(cpu_list
);
342 if (evlist
->cpus
== NULL
)
343 goto out_delete_threads
;
348 thread_map__delete(evlist
->threads
);
352 void perf_evlist__delete_maps(struct perf_evlist
*evlist
)
354 cpu_map__delete(evlist
->cpus
);
355 thread_map__delete(evlist
->threads
);
357 evlist
->threads
= NULL
;
360 int perf_evlist__set_filters(struct perf_evlist
*evlist
)
362 const struct thread_map
*threads
= evlist
->threads
;
363 const struct cpu_map
*cpus
= evlist
->cpus
;
364 struct perf_evsel
*evsel
;
371 list_for_each_entry(evsel
, &evlist
->entries
, node
) {
372 filter
= evsel
->filter
;
375 for (cpu
= 0; cpu
< cpus
->nr
; cpu
++) {
376 for (thread
= 0; thread
< threads
->nr
; thread
++) {
377 fd
= FD(evsel
, cpu
, thread
);
378 err
= ioctl(fd
, PERF_EVENT_IOC_SET_FILTER
, filter
);