mirror of
https://github.com/yanfeizhang/coder-kung-fu.git
synced 2026-10-28 19:25:24 +08:00
feat:添加新实验
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
- [likely 和 unlikely 汇编结果对比](tests/cpu/test01)
|
||||
- [物理机 cpu 系统利用率的计算过程](tests/cpu/test06)
|
||||
- [容器 cpu 系统利用率的计算过程](tests/cpu/test07)
|
||||
- [容器 cpu 系统利用率的计算过程](tests/cpu/test07)
|
||||
- [直接使用perf_event_open获取硬件计数](tests/cpu/test08)
|
||||
43
tests/cpu/test08/main.c
Normal file
43
tests/cpu/test08/main.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <linux/perf_event.h>
|
||||
|
||||
// 封装perf_event_open系统调用
|
||||
int perf_event_open(struct perf_event_attr *attr,pid_t pid,int ,int group_fd,unsigned long flags)
|
||||
{
|
||||
return syscall(__NR_perf_event_open,attr,pid,cpu,group_fd,flags);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// 第一步:创建perf文件描述符
|
||||
// 准备参数
|
||||
struct perf_event_attr attr;
|
||||
memset(&attr,0,sizeof(struct perf_event_attr));
|
||||
attr.size=sizeof(struct perf_event_attr);
|
||||
attr.type=PERF_TYPE_HARDWARE; // 监测硬件
|
||||
attr.config=PERF_COUNT_HW_INSTRUCTIONS; // 监测指令数
|
||||
|
||||
// pid=0表示只检测当前进程
|
||||
// cpu=-1表示检测所有cpu核
|
||||
int fd=perf_event_open(&attr,0,-1,-1,0);
|
||||
if(fd<0)
|
||||
{
|
||||
perror("Cannot open perf fd!");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// 第二步:定时获取指标计数
|
||||
// 每隔1秒打印一次当前计数
|
||||
while(1)
|
||||
{
|
||||
uint64_t instructions;
|
||||
read(fd,&instructions,sizeof(instructions));
|
||||
printf("instructions=%ld\n",instructions);
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user