如何使 perf_event_open() 中的PERF_COUNT_SW_CONTEXT_SWITCHES配置工作?

How to make the PERF_COUNT_SW_CONTEXT_SWITCHES config in perf_event_open() work?

本文关键字:CONTEXT SW COUNT SWITCHES 配置 工作 PERF 中的 perf 何使 event      更新时间:2023-10-16

我正在为我编写的软件设置分析,但我无法使用perf_event_open获得上下文切换计数。

为了测试问题,我也尝试使用perf_event_openman_page上提供的示例代码。使用sched_yield并使用任务集在同一内核上运行并行进程,以强制上下文切换。使用perf_event_open()的上下文切换计数仍为 0。(在使用 perf stat 时,我得到非零数字:对于大循环,在数千中(。我也尝试使用mmap进行文件读取/强制页面错误。

#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 <iostream>
#include <string.h>
#include <sys/mman.h>
using namespace std;
int buf_size_shift = 8;
static unsigned perf_mmap_size(int buf_size_shift)
{
return ((1U << buf_size_shift) + 1) * sysconf(_SC_PAGESIZE);
}
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)
{
struct perf_event_attr pe;
long long count;
int fd;
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = PERF_TYPE_SOFTWARE;
//pe.sample_type = PERF_SAMPLE_CALLCHAIN; /* this is what allows you to obtain callchains */
pe.size = sizeof(struct perf_event_attr);
pe.config = PERF_COUNT_SW_CONTEXT_SWITCHES;
pe.disabled = 1;
pe.exclude_kernel = 1;
pe.sample_period = 1000;
pe.exclude_hv = 1;
fd = perf_event_open(&pe, 0, -1, -1, 0); 
if (fd == -1) {
fprintf(stderr, "Error opening leader %llxn", pe.config);
exit(EXIT_FAILURE);
}
/* associate a buffer with the file */
struct perf_event_mmap_page *mpage;
mpage = (perf_event_mmap_page*) mmap(NULL,  perf_mmap_size(buf_size_shift),
PROT_READ|PROT_WRITE, MAP_SHARED,
fd, 0);
if (mpage == (struct perf_event_mmap_page *)-1L) {
close(fd);
return -1;
}
ioctl(fd, PERF_EVENT_IOC_RESET, 0);
ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
printf("Measuring instruction count for this printfn");
long long sum = 0;
for (long long i = 0; i < 10000000000; i++) {
sum += i;
if (i%1000000 == 0)
cout << i << " : " << sum << endl;
} 
ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
read(fd, &count, sizeof(long long));
printf("Used %lld csn", count);
close(fd);
}

此用于type = PERF_COUNT_SOFTWAREconfig = PERF_COUNT_SW_CONTEXT_SWITCHES的代码即使在强制上下文切换的情况下也会在计数中输出 0。而其他指标正在发挥作用。

在使用 mmap 环形缓冲区时,我在读取它时看到PERF_RECORD_SWITCH记录,而根据我的理解,正在记录上下文切换事件。

有关性能计数和环形缓冲区中的数据如何相关的任何信息也值得赞赏。

事件不计算在内,因为您禁用了来自内核的事件 (exclude_kernel = 1;(,并且PERF_TYPE_SOFTWARE事件通常由内核提供

如果删除exclude_kernel,则对事件进行计数。

计数与环形缓冲区中记录的事件之间的连接是sample_periodpe.sample_period = 1000;设置意味着每 1000 个交换机事件,就会将一个PERF_RECORD_SAMPLE事件写入环形缓冲区。

以下读取缓冲区的示例仅用于说明一般方法。实际上,您需要处理环绕缓冲区末尾的事件,并执行更多的一致性检查。

auto tail = mpage->data_tail;
const auto head = mpage->data_head;
const auto size = mpage->data_size;
char* data = reinterpret_cast<char*>(mpage) + sysconf(_SC_PAGESIZE);
int events = 0;
while (true) {
if (tail >= head) break;
auto event_header_p = (struct perf_event_header*)(data + (tail % size));
std::cout << "event << " << event_header_p->type << ", size: " << event_header_p->size << "n";
tail += event_header_p->size;
events++;
}

您应该在缓冲区中找到相应数量的PERF_RECORD_SAMPLE == 9类型的事件(除非存在溢出(。如果要读取它们,则需要将指针强制转换为适当的结构。PERF_RECORD_SAMPLE事件(或任何其他事件(的实际布局取决于您的perf_event_attr配置,并记录在perf_event_open中。