我正在阅读 Lemire 编写的code,它对 CPU 周期数和指令数进行了基准测试。
以下是主要逻辑:
#define N_CONFIG 2
int CONFIGS[N_CONFIG] = {PERF_COUNT_HW_CPU_CYCLES, PERF_COUNT_HW_INSTRUCTIONS};
const int pid = 0; // current process
const int cpu = -1; // all cpus
const unsigned long flags = 0;
int group = -1; // no group
for (uint32_t i = 0; i < N_CONFIG; i++) {
attr.config = CONFIGS[i];
fd =
(int)syscall(__NR_perf_event_open, &event.attr, pid, cpu, group, flags);
ioctl(fd, PERF_EVENT_IOC_ID, &ids[i]);
if (group == -1) {
group = fd;
}
}
那么如何从
fd
读取CPU周期数和指令数?
在
end()
方法中:
if (read(fd, temp_result_vec.data(), temp_result_vec.size() * 8) == -1) {
report_error("read");
}
// our actual results are in slots 1,3,5, ... of this structure
如何理解“我们的实际结果在这个结构体的1、3、5、……槽位”?这个结构指的是
std::vector<uint64_t>
,其大小是2 * N_CONFIG + 1
.