我正在编写一个使用perf_event_open监视非核心事件的程序。我需要监视uncore_cha类型,但是perf事件打开会给出无效的参数错误。
根据perf_event_open手册页(http://man7.org/linux/man-pages/man2/perf_event_open.2.html),可以在perf_event_attr结构的type字段中设置动态PMU。
可以在/ sys / bus / event_source / devices /'pmu-type'/ type中找到类型值。 设置此值后,perf_event_open返回-1并引发Error开幕领导者错误。在检查errno时,发现它是无效的参数错误。
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/perf_event.h>
#include <asm/unistd.h>
#include <linux/pmu.h>
#include <linux/hw_breakpoint.h>
long
perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
int cpu, int group_fd, unsigned long flags)
{
int ret;
ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
group_fd, flags);
return ret;
}
int
main(int argc, char **argv)
{
struct perf_event_attr pe;
long long count;
int fd;
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = 25; // // Type value 25 for PMU-Type uncore_cha_0
pe.size = sizeof(struct perf_event_attr);
pe.config = 0x0111; //Event to be monitored
pe.disabled = 1;
pe.exclude_kernel = 1;
pe.exclude_hv = 1;
fd = perf_event_open(&pe, 0, -1, -1, 0);
if (fd == -1) {
fprintf(stderr, "Error opening leader %llx\n", pe.config);
exit(EXIT_FAILURE);
}
ioctl(fd, PERF_EVENT_IOC_RESET, 0);
ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
printf("Measuring instruction count for this printf\n");
ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
read(fd, &count, sizeof(long long));
printf("Used %lld instructions\n", count);
close(fd);
}
任何对此的帮助将不胜感激。