Perf_event_open系统调用

时间:2018-04-14 23:41:18

标签: c system-calls perf

我正在尝试访问PMU硬件性能计数器详细信息,主要关注CPu周期。下面是它的C代码。

    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/syscall.h>
    #include <string.h>
    #include <sys/ioctl.h>
    #include <linux/perf_event.h>
    #include <linux/hw_breakpoint.h>
    #include <asm/unistd.h>
    #include <errno.h>
    #include <stdint.h>
    #include <inttypes.h>
    #include <time.h>


    static 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)
    {
    int s = 1; 
    struct perf_event_attr pe1,pe2;
    long long count1,count2;
    int fd1,fd2;
    struct timespec time, time2;

    time.tv_sec = 0;
    time.tv_nsec = 100000000000;

    long pid = strtol(argv[1],NULL,10);
    printf("pid : %ld \n",pid);

    memset(&pe1, 0, sizeof(struct perf_event_attr));
    pe1.type = PERF_TYPE_HARDWARE;
    pe1.size = sizeof(struct perf_event_attr);
    pe1.config = PERF_COUNT_HW_INSTRUCTIONS;

    memset(&pe2, 0, sizeof(struct perf_event_attr));
    pe2.type = PERF_TYPE_HARDWARE;
    pe2.size = sizeof(struct perf_event_attr);
    pe2.config = PERF_COUNT_HW_CACHE_MISSES;

    fd1 = perf_event_open(&pe1, pid, -1, -1, 0);
    if (fd1 == -1) {
    fprintf(stderr, "Error opening leader %llx\n", pe1.config);
    exit(EXIT_FAILURE);
    }
    fd2 = perf_event_open(&pe2, pid, -1, -1, 0);
    if (fd2 == -1) {
    fprintf(stderr, "Error opening leader %llx\n", pe2.config);
    exit(EXIT_FAILURE);
    }
    while(s<=3){ 
    read(fd1, &count1, sizeof(long long));
    printf("cpu cycles : %lld \n", count1);
    read(fd2, &count2, sizeof(long long));
    printf("cache misses : %lld \n", count2);
    nanosleep(&time, &time2);
    s++;
    }

    close(fd1);
    close(fd2);
    }

当我通过将其编译为gcc code1.c -o code1来运行上面的代码并通过将pid作为参数传递为./code1来运行它时,我得到以下输出

    pid : 5367 
    cpu cycles : 0 
    cache misses : 0 
    cpu cycles : 0 
    cache misses : 0 
    cpu cycles : 0 
    cache misses : 0

。对于我系统中的大部分pid来说,情况都是一样的。这是否意味着所有的pid都没有使用任何cpu周期?这是否意味着不计算特定PID的周期?

我根据您输入的Ioctl命令编辑了代码,但我不确定这是否是必须完成的。

这是代码

    static 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)   
  {
  int s = 1; 
  struct perf_event_attr pe1,pe2;
  long long count1,count2;
  int fd1,fd2;
  struct timespec time, time2;

 time.tv_sec = 2;
 time.tv_nsec = 100000000000;

 long pid = strtol(argv[1],NULL,10);
 printf("pid : %ld \n",pid);

 memset(&pe1, 0, sizeof(struct perf_event_attr));
 pe1.type = PERF_TYPE_HARDWARE;
 pe1.size = sizeof(struct perf_event_attr);
 pe1.config = PERF_COUNT_HW_CPU_CYCLES;

 memset(&pe2, 0, sizeof(struct perf_event_attr));
 pe2.type = PERF_TYPE_HARDWARE;
 pe2.size = sizeof(struct perf_event_attr);
 pe2.config = PERF_COUNT_HW_CACHE_MISSES;

 fd1 = perf_event_open(&pe1, pid, -1, -1, 0);
 if (fd1 == -1) {
    fprintf(stderr, "Error opening leader %llx\n", pe1.config);
    exit(EXIT_FAILURE);
}

 ioctl(fd1, PERF_EVENT_IOC_RESET, 0);
 ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);

 ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
 read(fd1, &count1, sizeof(long long));
 printf("hardware instructions : %lld \n", count1);
 fd2 = perf_event_open(&pe2, pid, -1, -1, 0);
 if (fd2 == -1) {
    fprintf(stderr, "Error opening leader %llx\n", pe2.config);
    exit(EXIT_FAILURE);
 }
 ioctl(fd2, PERF_EVENT_IOC_RESET, 0);
 ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);

 ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
 read(fd2, &count2, sizeof(long long));
 printf("cache-misses : %lld \n", count2);

close(fd1);
close(fd2);

}

但我仍然得到0。

    pid : 5 
    hardware instructions : 0 
    cache-misses : 0 

我理解这个概念错了吗?

来自此链接的代码可以很好地提供统计信息[link] perf_event_open - how to monitoring multiple events

但我想传递PID并检查它。

1 个答案:

答案 0 :(得分:1)

您缺少启用/禁用计数器所需的ioctl命令。像这样的东西 -

fd1 = perf_event_open(&pe1, pid, -1, -1, 0);
if (fd1 == -1) {
fprintf(stderr, "Error opening leader %llx\n", pe1.config);
exit(EXIT_FAILURE);
}

ioctl(fd1, PERF_EVENT_IOC_RESET, 0);
ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);

ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
read(fd1, &count1, sizeof(long long));
printf("hardware instructions : %lld \n", count1);

fd2 = perf_event_open(&pe2, pid, -1, -1, 0);
if (fd2 == -1) {
fprintf(stderr, "Error opening leader %llx\n", pe2.config);
exit(EXIT_FAILURE);
}

ioctl(fd2, PERF_EVENT_IOC_RESET, 0);
ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);

ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
read(fd2, &count2, sizeof(long long));
printf("cache-misses : %lld \n", count2);

此外,fd1文件描述符将涉及计算退出指令的数量,而不是cpu周期数。

修改

要计算HW循环次数,配置应为 -

pe1.config = PERF_COUNT_HW_CPU_CYCLES