当读取 CPUS 上的性能计数器 (PMC) 时,必须kernel.perf_event_paranoid
(<=1
请参阅内核文档)
下面的程序读取 PMC 并且应该提前退出1
,如果它无法打开计数器,kernel.perf_event_paranoid
即>1
(在 后检查syscall
)
我测试过
- ArchLinux
Linux host1 5.17.9-arch1-1 #1 SMP PREEMPT Wed, 18 May 2022 17:30:11 +0000 x86_64 GNU/Linux
- 乌班图
Linux host2 5.15.0-30-generic #31-Ubuntu SMP Thu May 5 10:00:34 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
现在,默认情况下,参数为2
(Arch)、4
(Ubuntu)。我运行该程序,它应该无法打开性能计数器。在 Arch 上,它仍然可以做到,在 Ubuntu 上失败(正如预期的那样)。
经过修补和指针从卡米尔在 SU SE 上,我发现有Ubuntu
一个补丁需要设置才能4
真正生效。
- ArchLinux 上有类似的东西吗?
- 这种行为也可以在其他发行版上重现吗?
该脚本可以帮助重现
#!/bin/bash
cat >pmc.c <<'EOF'
#include <linux/perf_event.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
static struct perf_event_attr attr;
static int fdperf = -1;
static struct perf_event_mmap_page *buf = 0;
long long cpucycles_amd64rdpmc(void) {
long long result;
unsigned int seq;
long long index;
long long offset;
if (fdperf == -1) {
attr.type = PERF_TYPE_HARDWARE;
attr.config = PERF_COUNT_HW_CPU_CYCLES;
attr.exclude_kernel = 1;
fdperf = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0);
if (fdperf == -1){
fprintf(stderr, "\033[31m--> could not open perf counter. Check paranoid setting\033[0m\n");
exit(1);
}
buf = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ, MAP_SHARED, fdperf, 0);
}
do {
seq = buf->lock;
asm volatile("" ::: "memory");
index = buf->index;
offset = buf->offset;
asm volatile("rdpmc;shlq $32,%%rdx;orq %%rdx,%%rax"
: "=a"(result)
: "c"(index - 1)
: "%rdx");
asm volatile("" ::: "memory");
} while (buf->lock != seq);
result += offset;
result &= 0xffffffffffff;
return result;
}
int main() {
long long c = cpucycles_amd64rdpmc();
printf("counter: %llx\n", c);
return 0;
}
EOF
param_name=kernel.perf_event_paranoid
read_pmc() {
echo -n "--> reading, "
sysctl "${param_name}"
}
set_pmc() {
n=$1
echo -e "--> setting to ${n}"
sudo sysctl -w "${param_name}=${n}"
read_pmc
echo "should be ${n}"
}
run() {
echo "--> running"
./pmc
}
#compile
gcc pmc.c -o pmc
read_pmc
run
echo -e "\n--> if ${param_name} as >1, that should have printed the error message\n\n"
set_pmc 1
run
echo -e "\n--> that should have worked and printed the counter.\n"
#re-set to 2
set_pmc 2
run
echo -e "\033[31m--> that should NOT have worked but it printed the counter\033[0m.\n"
rm pmc.c pmc
(rdpmc 代码基于 cpucycles/amd64rdpmc.c超级警察)
有关的: