A bug of linux-perf-events.h
At [line 46](https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/87f24593d246c54cf065a34f2a7aa7ea650a8094/2023/03/21/performancecounters/linux-perf-events.h#L46) in [linux-perf-events.h](https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/master/2023/03/21/performancecounters/linux-perf-events.h#L46), `fd` is updated in every iteration:
```c
fd = static_cast<int>(syscall(__NR_perf_event_open, &attribs, pid, cpu, group, flags));
```
But in fact, we need to record the **first** `fd` as the group leader. It can modified as:
```c
int _fd = static_cast<int>(syscall(__NR_perf_event_open, &attribs, pid, cpu, group, flags));
if (i == 0 && _fd != 0) {
// update only when i == 0
fd = _fd;
group = _fd;
}
```
Related discussion can be found at https://stackoverflow.com/a/76073915/3741571.
关闭于 2023-04-21 1 条评论