1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/ring_buffer.h>
8 #include <linux/perf_event.h>
10 #include <perf/event.h>
11 #include <perf/evsel.h>
12 #include <internal/mmap.h>
13 #include <internal/lib.h>
14 #include <linux/kernel.h>
15 #include <linux/math64.h>
16 #include <linux/stringify.h>
19 void perf_mmap__init(struct perf_mmap
*map
, struct perf_mmap
*prev
,
20 bool overwrite
, libperf_unmap_cb_t unmap_cb
)
22 /* Assume fields were zero initialized. */
24 map
->overwrite
= overwrite
;
25 map
->unmap_cb
= unmap_cb
;
26 refcount_set(&map
->refcnt
, 0);
31 size_t perf_mmap__mmap_len(struct perf_mmap
*map
)
33 return map
->mask
+ 1 + page_size
;
36 int perf_mmap__mmap(struct perf_mmap
*map
, struct perf_mmap_param
*mp
,
37 int fd
, struct perf_cpu cpu
)
41 map
->base
= mmap(NULL
, perf_mmap__mmap_len(map
), mp
->prot
,
43 if (map
->base
== MAP_FAILED
) {
53 void perf_mmap__munmap(struct perf_mmap
*map
)
58 zfree(&map
->event_copy
);
59 map
->event_copy_sz
= 0;
61 munmap(map
->base
, perf_mmap__mmap_len(map
));
64 refcount_set(&map
->refcnt
, 0);
70 void perf_mmap__get(struct perf_mmap
*map
)
72 refcount_inc(&map
->refcnt
);
75 void perf_mmap__put(struct perf_mmap
*map
)
77 BUG_ON(map
->base
&& refcount_read(&map
->refcnt
) == 0);
79 if (refcount_dec_and_test(&map
->refcnt
))
80 perf_mmap__munmap(map
);
83 static inline void perf_mmap__write_tail(struct perf_mmap
*md
, u64 tail
)
85 ring_buffer_write_tail(md
->base
, tail
);
88 u64
perf_mmap__read_head(struct perf_mmap
*map
)
90 return ring_buffer_read_head(map
->base
);
93 static bool perf_mmap__empty(struct perf_mmap
*map
)
95 struct perf_event_mmap_page
*pc
= map
->base
;
97 return perf_mmap__read_head(map
) == map
->prev
&& !pc
->aux_size
;
100 void perf_mmap__consume(struct perf_mmap
*map
)
102 if (!map
->overwrite
) {
105 perf_mmap__write_tail(map
, old
);
108 if (refcount_read(&map
->refcnt
) == 1 && perf_mmap__empty(map
))
112 static int overwrite_rb_find_range(void *buf
, int mask
, u64
*start
, u64
*end
)
114 struct perf_event_header
*pheader
;
115 u64 evt_head
= *start
;
118 pr_debug2("%s: buf=%p, start=%"PRIx64
"\n", __func__
, buf
, *start
);
119 pheader
= (struct perf_event_header
*)(buf
+ (*start
& mask
));
121 if (evt_head
- *start
>= (unsigned int)size
) {
122 pr_debug("Finished reading overwrite ring buffer: rewind\n");
123 if (evt_head
- *start
> (unsigned int)size
)
124 evt_head
-= pheader
->size
;
129 pheader
= (struct perf_event_header
*)(buf
+ (evt_head
& mask
));
131 if (pheader
->size
== 0) {
132 pr_debug("Finished reading overwrite ring buffer: get start\n");
137 evt_head
+= pheader
->size
;
138 pr_debug3("move evt_head: %"PRIx64
"\n", evt_head
);
140 WARN_ONCE(1, "Shouldn't get here\n");
145 * Report the start and end of the available data in ringbuffer
147 static int __perf_mmap__read_init(struct perf_mmap
*md
)
149 u64 head
= perf_mmap__read_head(md
);
151 unsigned char *data
= md
->base
+ page_size
;
154 md
->start
= md
->overwrite
? head
: old
;
155 md
->end
= md
->overwrite
? old
: head
;
157 if ((md
->end
- md
->start
) < md
->flush
)
160 size
= md
->end
- md
->start
;
161 if (size
> (unsigned long)(md
->mask
) + 1) {
162 if (!md
->overwrite
) {
163 WARN_ONCE(1, "failed to keep up with mmap data. (warn only once)\n");
166 perf_mmap__consume(md
);
171 * Backward ring buffer is full. We still have a chance to read
172 * most of data from it.
174 if (overwrite_rb_find_range(data
, md
->mask
, &md
->start
, &md
->end
))
181 int perf_mmap__read_init(struct perf_mmap
*map
)
184 * Check if event was unmapped due to a POLLHUP/POLLERR.
186 if (!refcount_read(&map
->refcnt
))
189 return __perf_mmap__read_init(map
);
193 * Mandatory for overwrite mode
194 * The direction of overwrite mode is backward.
195 * The last perf_mmap__read() will set tail to map->core.prev.
196 * Need to correct the map->core.prev to head which is the end of next read.
198 void perf_mmap__read_done(struct perf_mmap
*map
)
201 * Check if event was unmapped due to a POLLHUP/POLLERR.
203 if (!refcount_read(&map
->refcnt
))
206 map
->prev
= perf_mmap__read_head(map
);
209 /* When check_messup is true, 'end' must points to a good entry */
210 static union perf_event
*perf_mmap__read(struct perf_mmap
*map
,
211 u64
*startp
, u64 end
)
213 unsigned char *data
= map
->base
+ page_size
;
214 union perf_event
*event
= NULL
;
215 int diff
= end
- *startp
;
217 if (diff
>= (int)sizeof(event
->header
)) {
220 event
= (union perf_event
*)&data
[*startp
& map
->mask
];
221 size
= event
->header
.size
;
223 if (size
< sizeof(event
->header
) || diff
< (int)size
)
227 * Event straddles the mmap boundary -- header should always
228 * be inside due to u64 alignment of output.
230 if ((*startp
& map
->mask
) + size
!= ((*startp
+ size
) & map
->mask
)) {
231 unsigned int offset
= *startp
;
232 unsigned int len
= size
, cpy
;
233 void *dst
= map
->event_copy
;
235 if (size
> map
->event_copy_sz
) {
236 dst
= realloc(map
->event_copy
, size
);
239 map
->event_copy
= dst
;
240 map
->event_copy_sz
= size
;
244 cpy
= min(map
->mask
+ 1 - (offset
& map
->mask
), len
);
245 memcpy(dst
, &data
[offset
& map
->mask
], cpy
);
251 event
= (union perf_event
*)map
->event_copy
;
261 * Read event from ring buffer one by one.
262 * Return one event for each call.
265 * perf_mmap__read_init()
266 * while(event = perf_mmap__read_event()) {
267 * //process the event
268 * perf_mmap__consume()
270 * perf_mmap__read_done()
272 union perf_event
*perf_mmap__read_event(struct perf_mmap
*map
)
274 union perf_event
*event
;
277 * Check if event was unmapped due to a POLLHUP/POLLERR.
279 if (!refcount_read(&map
->refcnt
))
282 /* non-overwrite doesn't pause the ringbuffer */
284 map
->end
= perf_mmap__read_head(map
);
286 event
= perf_mmap__read(map
, &map
->start
, map
->end
);
289 map
->prev
= map
->start
;
294 #if defined(__i386__) || defined(__x86_64__)
295 static u64
read_perf_counter(unsigned int counter
)
297 unsigned int low
, high
;
299 asm volatile("rdpmc" : "=a" (low
), "=d" (high
) : "c" (counter
));
301 return low
| ((u64
)high
) << 32;
304 static u64
read_timestamp(void)
306 unsigned int low
, high
;
308 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
310 return low
| ((u64
)high
) << 32;
312 #elif defined(__aarch64__)
313 #define read_sysreg(r) ({ \
315 asm volatile("mrs %0, " __stringify(r) : "=r" (__val)); \
319 static u64
read_pmccntr(void)
321 return read_sysreg(pmccntr_el0
);
324 #define PMEVCNTR_READ(idx) \
325 static u64 read_pmevcntr_##idx(void) { \
326 return read_sysreg(pmevcntr##idx##_el0); \
362 * Read a value direct from PMEVCNTR<idx>
364 static u64
read_perf_counter(unsigned int counter
)
366 static u64 (* const read_f
[])(void) = {
401 if (counter
< ARRAY_SIZE(read_f
))
402 return (read_f
[counter
])();
407 static u64
read_timestamp(void) { return read_sysreg(cntvct_el0
); }
409 /* __riscv_xlen contains the witdh of the native base integer, here 64-bit */
410 #elif defined(__riscv) && __riscv_xlen == 64
412 /* TODO: implement rv32 support */
414 #define CSR_CYCLE 0xc00
415 #define CSR_TIME 0xc01
417 #define csr_read(csr) \
419 register unsigned long __v; \
420 __asm__ __volatile__ ("csrr %0, %1" \
426 static unsigned long csr_read_num(int csr_num
)
428 #define switchcase_csr_read(__csr_num, __val) {\
430 __val = csr_read(__csr_num); \
432 #define switchcase_csr_read_2(__csr_num, __val) {\
433 switchcase_csr_read(__csr_num + 0, __val) \
434 switchcase_csr_read(__csr_num + 1, __val)}
435 #define switchcase_csr_read_4(__csr_num, __val) {\
436 switchcase_csr_read_2(__csr_num + 0, __val) \
437 switchcase_csr_read_2(__csr_num + 2, __val)}
438 #define switchcase_csr_read_8(__csr_num, __val) {\
439 switchcase_csr_read_4(__csr_num + 0, __val) \
440 switchcase_csr_read_4(__csr_num + 4, __val)}
441 #define switchcase_csr_read_16(__csr_num, __val) {\
442 switchcase_csr_read_8(__csr_num + 0, __val) \
443 switchcase_csr_read_8(__csr_num + 8, __val)}
444 #define switchcase_csr_read_32(__csr_num, __val) {\
445 switchcase_csr_read_16(__csr_num + 0, __val) \
446 switchcase_csr_read_16(__csr_num + 16, __val)}
448 unsigned long ret
= 0;
451 switchcase_csr_read_32(CSR_CYCLE
, ret
)
457 #undef switchcase_csr_read_32
458 #undef switchcase_csr_read_16
459 #undef switchcase_csr_read_8
460 #undef switchcase_csr_read_4
461 #undef switchcase_csr_read_2
462 #undef switchcase_csr_read
465 static u64
read_perf_counter(unsigned int counter
)
467 return csr_read_num(CSR_CYCLE
+ counter
);
470 static u64
read_timestamp(void)
472 return csr_read_num(CSR_TIME
);
476 static u64
read_perf_counter(unsigned int counter __maybe_unused
) { return 0; }
477 static u64
read_timestamp(void) { return 0; }
480 int perf_mmap__read_self(struct perf_mmap
*map
, struct perf_counts_values
*count
)
482 struct perf_event_mmap_page
*pc
= map
->base
;
483 u32 seq
, idx
, time_mult
= 0, time_shift
= 0;
484 u64 cnt
, cyc
= 0, time_offset
= 0, time_cycles
= 0, time_mask
= ~0ULL;
486 if (!pc
|| !pc
->cap_user_rdpmc
)
490 seq
= READ_ONCE(pc
->lock
);
493 count
->ena
= READ_ONCE(pc
->time_enabled
);
494 count
->run
= READ_ONCE(pc
->time_running
);
496 if (pc
->cap_user_time
&& count
->ena
!= count
->run
) {
497 cyc
= read_timestamp();
498 time_mult
= READ_ONCE(pc
->time_mult
);
499 time_shift
= READ_ONCE(pc
->time_shift
);
500 time_offset
= READ_ONCE(pc
->time_offset
);
502 if (pc
->cap_user_time_short
) {
503 time_cycles
= READ_ONCE(pc
->time_cycles
);
504 time_mask
= READ_ONCE(pc
->time_mask
);
508 idx
= READ_ONCE(pc
->index
);
509 cnt
= READ_ONCE(pc
->offset
);
510 if (pc
->cap_user_rdpmc
&& idx
) {
511 s64 evcnt
= read_perf_counter(idx
- 1);
512 u16 width
= READ_ONCE(pc
->pmc_width
);
514 evcnt
<<= 64 - width
;
515 evcnt
>>= 64 - width
;
521 } while (READ_ONCE(pc
->lock
) != seq
);
523 if (count
->ena
!= count
->run
) {
526 /* Adjust for cap_usr_time_short, a nop if not */
527 cyc
= time_cycles
+ ((cyc
- time_cycles
) & time_mask
);
529 delta
= time_offset
+ mul_u64_u32_shr(cyc
, time_mult
, time_shift
);