6#ifndef __NR_perf_event_open
9#define __NR_perf_event_open 319
10#elif defined(__x86_64__)
11#define __NR_perf_event_open 298
12#elif defined(__i386__)
13#define __NR_perf_event_open 336
15#define __NR_perf_event_open 364
22 pid_t
pid,
int cpu,
int group_fd,
unsigned long flags )
26 ret = syscall( __NR_perf_event_open,
27 hw_event,
pid, cpu, group_fd, flags );
53#ifdef __SIZEOF_INT128__
56 return (
u64)(((
unsigned __int128)
a *
b) >> shift);
66 asm (
"mull %[b]" :
"=a" (low),
"=d" (high)
67 : [
a]
"a" (
a), [
b]
"rm" (
b) );
69 return low | ((
u64)high) << 32;
96#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
100#if defined(__x86_64__) || defined(__i386__)
103static inline unsigned long long rdtsc(
void) {
107 __asm__
volatile(
"rdtsc" :
"=a" (
a),
"=d" (d));
109 return ((
unsigned long long)
a) | (((
unsigned long long)d) << 32);
112static inline unsigned long long rdpmc(
unsigned int counter) {
114 unsigned int low, high;
116 __asm__
volatile(
"rdpmc" :
"=a" (low),
"=d" (high) :
"c" (counter));
118 return (
unsigned long long)low | ((
unsigned long long)high) <<32;
121#define barrier() __asm__ volatile("" ::: "memory")
124#elif defined(__aarch64__)
131#define __stringify_1(x...) #x
132#define __stringify(x...) __stringify_1(x)
134#define read_sysreg(r) ({ \
136 asm volatile("mrs %0, " __stringify(r) : "=r" (__val)); \
140static u64 read_pmccntr(
void)
142 return read_sysreg(pmccntr_el0);
145#define PMEVCNTR_READ(idx) \
146 static u64 read_pmevcntr_##idx(void) { \
147 return read_sysreg(pmevcntr##idx##_el0); \
185static u64 rdpmc(
unsigned int counter)
187 static u64 (*
const read_f[])(void) = {
223 return (read_f[counter])();
228static u64 rdtsc(
void) {
return read_sysreg(cntvct_el0); }
230#define barrier() asm volatile("dmb ish" : : : "memory")
234#if defined(__x86_64__) || defined(__i386__) || defined(__aarch64__)
239 ret =
b + ((
a -
b) &
c);
246 unsigned long long reset,
247 unsigned long long *en,
248 unsigned long long *ru) {
250 struct perf_event_mmap_page *pc = addr;
252 uint32_t seq, time_mult = 0, time_shift = 0, index, width;
254 uint64_t enabled, running;
255 uint64_t cyc = 0, time_offset = 0, time_cycles = 0, time_mask = ~0ULL;
275 enabled = pc->time_enabled;
277 running = pc->time_running;
282 if ( (pc->cap_user_time) && (enabled != running)) {
284 time_offset = pc->time_offset;
285 time_mult = pc->time_mult;
286 time_shift = pc->time_shift;
288 if (pc->cap_user_time_short) {
289 time_cycles = pc->time_cycles;
290 time_mask = pc->time_mask;
305 width = pc->pmc_width;
307 if (user_reset_flag == 1) {
316 if (pc->cap_usr_rdpmc && index) {
319 pmc =
rdpmc(index-1);
322 if (user_reset_flag == 1) {
333 return 0xffffffffffffffffULL;
338 }
while (pc->lock != seq);
340 if (enabled != running) {
343 cyc = adjust_cap_usr_time_short(cyc, time_cycles, time_mask);
361 struct perf_event_mmap_page *pc = addr;
386 if (pc->cap_usr_rdpmc && index) {
392 }
while (pc->lock != seq);
404 return (
unsigned long long)(-1);
409 return (
unsigned long long)(-1);
423 struct perf_event_mmap_page *pc =
pe->mmap_buf;
427 PAPIERROR(
"perf_event_mmap_page is NULL" );
431 head = pc->data_head;
440 struct perf_event_mmap_page *pc =
pe->mmap_buf;
443 pc->data_tail = tail;
456typedef union event_union {
457 struct perf_event_header header;
469 uint64_t old =
pe->tail;
470 unsigned char *data = ((
unsigned char*)
pe->mmap_buf) + getpagesize();
475 SUBDBG(
"WARNING: failed to keep up with mmap data. head = %" PRIu64
476 ", tail = %" PRIu64
". Discarding samples.\n", head, old );
481 for( ; old != head; ) {
484 size_t size = event->
header.size;
488 if ( ( old &
pe->mask ) + size != ( ( old + size ) &
pe->mask ) ) {
489 uint64_t offset = old;
490 uint64_t len =
min(
sizeof ( *event ), size ), cpy;
491 void *dst = &event_copy;
494 cpy =
min(
pe->mask + 1 - ( offset &
pe->mask ), len );
495 memcpy( dst, &data[offset &
pe->mask], cpy );
497 dst = ((
unsigned char*)dst) + cpy;
505 SUBDBG(
"event->type = %08x\n", event->header.type );
506 SUBDBG(
"event->size = %d\n", event->header.size );
508 switch ( event->header.type ) {
509 case PERF_RECORD_SAMPLE:
511 (
vptr_t ) (
unsigned long ) event->ip.ip,
515 case PERF_RECORD_LOST:
516 SUBDBG(
"Warning: because of a mmap buffer overrun, %" PRId64
517 " events were lost.\n"
518 "Loss was recorded when counter id %#"PRIx64
519 " overflowed.\n", event->lost.lost, event->lost.id );
522 SUBDBG(
"Error: unexpected header type - %d\n",
523 event->header.type );
static double a[MATRIX_SIZE][MATRIX_SIZE]
static double b[MATRIX_SIZE][MATRIX_SIZE]
static double c[MATRIX_SIZE][MATRIX_SIZE]
unsigned long AO_t __attribute__((__aligned__(4)))
#define SUBDBG(format, args...)
void PAPIERROR(char *format,...)
static uint64_t mmap_read_head(pe_event_info_t *pe)
static u64 mul_u32_u32(u32 a, u32 b)
static u64 mul_u64_u32_shr(u64 a, u32 b, unsigned int shift)
static long sys_perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags)
static unsigned long long mmap_read_reset_count(void *addr __attribute__((unused)))
static void mmap_write_tail(pe_event_info_t *pe, uint64_t tail)
static unsigned long long mmap_read_self(void *addr __attribute__((unused)), int user_reset_flag __attribute__((unused)), unsigned long long reset __attribute__((unused)), unsigned long long *en __attribute__((unused)), unsigned long long *ru __attribute__((unused)))
static void mmap_read(int cidx, ThreadInfo_t **thr, pe_event_info_t *pe, int profile_index)
static const pme_power_entry_t * pe
struct perf_event_header header
struct perf_event_header header
struct perf_event_header header