我在采样模式下使用perf_event_open()
对分支堆栈的值进行采样,但是我不知道为什么!
attr.sample_type=PERF_SAMPLE_IP|PERF_SAMPLE_BRANCH_STACK
如果我没有将PERF_SAMPLE_BRANCH_STACK
设置为attr.sample_type
,一切都很好!我不知道为什么!
static int perf_event_open(struct perf_event_attr *attr,
pid_t pid,int cpu,int group_fd,unsigned long flags)
{
return syscall(__NR_perf_event_open,attr,pid,cpu,group_fd,flags);
}
int main(int argc, char** argv)
{
pid_t pid = 0;
// create a perf fd
struct perf_event_attr attr;
memset(&attr,0,sizeof(struct perf_event_attr));
attr.size=sizeof(struct perf_event_attr);
// disable at init time
attr.disabled=1;
// set what is the event
attr.type=PERF_TYPE_HARDWARE;
attr.config=PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
// how many clocks to trigger sampling
attr.sample_period=1000000;
// what to sample is IP
attr.sample_type=PERF_SAMPLE_IP|PERF_SAMPLE_BRANCH_STACK;
// notify every 1 overflow
attr.wakeup_events=1;
attr.branch_sample_type = PERF_SAMPLE_BRANCH_ANY_RETURN;
// open perf fd
int perf_fd=perf_event_open(&attr,pid,-1,-1,0);
if(perf_fd<0)
{
perror("perf_event_open() failed!");
return errno;
}
失败!错误:不支持该操作!
答案 0 :(得分:0)
错误:不支持该操作
perf_event_open() manual page说出此错误:
EOPNOTSUPP Returned if an event requiring a specific hardware feature is requested but there is no hardware support. This includes requesting low-skid events if not supported, branch tracing if it is not available, sampling if no PMU interrupt is available, and branch stacks for software events.
关于PERF_SAMPLE_BRANCH_STACK
,它说:
PERF_SAMPLE_BRANCH_STACK (since Linux 3.4) This provides a record of recent branches, as provided by CPU branch sampling hardware (such as Intel Last Branch Record). Not all hardware supports this fea‐ ture.
因此,看来您的硬件不支持此功能。
答案 1 :(得分:0)
我可以想到在您的情况下发生该错误的三个原因:
PERF_SAMPLE_BRANCH_STACK
受支持,并且硬件中支持某些分支过滤器,但是当前任何POWER处理器均不支持PERF_SAMPLE_BRANCH_ANY_RETURN
。您说通过删除PERF_SAMPLE_BRANCH_STACK
可以使代码正常工作,但这并不能告诉我们问题是来自PERF_SAMPLE_BRANCH_STACK
还是PERF_SAMPLE_BRANCH_ANY_RETURN
。ANY_RETURN
过滤器。并非所有的英特尔处理器都在硬件中支持ANY_RETURN
过滤器。从Core2开始支持此过滤器。但是,在Intel处理器上,对于硬件不支持的分支过滤器,Linux提供了软件过滤,因此PERF_SAMPLE_BRANCH_ANY_RETURN
仍应在这些处理器上工作。
我可能还有其他原因错过了。