Perf_event_open 解决问题之一。

上一篇博客说Perf_event_open遇到了问题,获得的计数器不准。

我分析原因,可能是性能计数器是一个核中的硬件,它只能计数在该核中运行的程序。而在多核处理器中,任务调度可能会将我需要计数的程序分配给其他的核,而这种分配是动态的,所以每次得到的不一样。这可能是原因之一。

很凑巧,我误打误撞竟然得到了较为正确的做法。在官方手册中说明。

 The pid and cpu arguments allow specifying which process and CPU to
       monitor:

       pid == 0 and cpu == -1
              This measures the calling process/thread on any CPU.

       pid == 0 and cpu >= 0
              This measures the calling process/thread only when running on
              the specified CPU.

       pid > 0 and cpu == -1
              This measures the specified process/thread on any CPU.

       pid > 0 and cpu >= 0
              This measures the specified process/thread only when running
              on the specified CPU.

       pid == -1 and cpu >= 0
              This measures all processes/threads on the specified CPU.
              This requires CAP_SYS_ADMIN capability or a
              /proc/sys/kernel/perf_event_paranoid value of less than 1.

       pid == -1 and cpu == -1
              This setting is invalid and will return an error.

       When pid is greater than zero, permission to perform this system call
       is governed by a ptrace access mode PTRACE_MODE_READ_REALCREDS check;
       see ptrace(2).

我将pid == 0和cpu >=0,再按照CPU 多设置几组并行统计,能得到较为准确的值。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值