Skip to content

Instantly share code, notes, and snippets.

@chaosgame
Created March 29, 2011 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaosgame/891812 to your computer and use it in GitHub Desktop.
Save chaosgame/891812 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/perf_event.h>
#include <asm/unistd.h>
#include <sys/ioctl.h>
static inline
int sys_perf_event_open(struct perf_event_attr *attr,
pid_t pid, int cpu, int group_fd,
unsigned long flags)
{
attr->size = sizeof(*attr);
return syscall(__NR_perf_event_open, attr, pid, cpu,
group_fd, flags);
}
static inline
void write_pmcnt_register()
{
asm volatile ("mcr p15, 0, r0, c9, c13, 2" :: );
}
int main()
{
struct perf_event_attr attr;
memset(&attr, '\0', sizeof(attr));
attr.disabled = 1;
attr.type = PERF_TYPE_HARDWARE;
attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
int fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
ioctl(fd, PERF_EVENT_IOC_ENABLE);
write_pmcnt_register();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment