PMC计数器统一使用(type, 编号)这样的二元组标识。打开、关闭,读取结果,都要使用这样的二元组,明确的告诉CPU是针对那个PMC计数器的。
这其中,编号要到CPU手册中查找。
[]total 0。。。。。。drwxr-xr-x 5 root root 0 Oct 8 11:19 uncore_imc_4/drwxr-xr-x 5 root root 0 Oct 8 11:19 uncore_imc_3/drwxr-xr-x 5 root root 0 Oct 8 11:19 uncore_imc_2/drwxr-xr-x 5 root root 0 Oct 8 11:19 uncore_imc_1/drwxr-xr-x 5 root root 0 Oct 8 11:19 uncore_imc_0/。。。。。。
[][]17[]18
在uncore_imc_N目录下,有一个type文件,就是Linux映射出的type。uncore_imc_0的type是17,uncore_imc_1的type是18。
也就是说,我这里两个内存通道的编号分别是17、18。
struct perf_event_attr {__u32 type; /* 这个值定为 17 */__u32 size; /* Size of attribute structure */__u64 config; /* 这个值定为 0x10b0 */......
你man一下perf_event_open(),翻到最后,有个perf_event_open的例子:
[root@el8-168 ~]PERF_EVENT_OPEN(2) Linux Programmer's Manual PERF_EVENT_OPEN(2)......EXAMPLEThe following is a short example that measures the total instruction count of a call to printf(3).static longperf_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;}intmain(int argc, char **argv){struct perf_event_attr pe;long long count;int fd;memset(&pe, 0, sizeof(struct perf_event_attr));pe.type = 《《这里改为上面得到的通道号:type》》pe.size = sizeof(struct perf_event_attr);pe.config = 《《这里改为PMC编号》》;pe.disabled = 1;pe.exclude_kernel = 1;pe.exclude_hv = 1;。。。。。。
把这个例子粘贴到编辑器中,修改一下pe.type和pe.cofig,就可以了。下面是我的程序的输出结果:
[]==========Parent PID is 11423==========PID is 11425-----------SUB Process at CPU: 1----------PERF_COUNT_HW_CACHE_L1D_RD (1): 164342.00 (100.00%) 164342L2_RQSTS.REFERENCES (1): 163905.00 (100.00%) 163905LLC Reference (1): 163855.00 (100.00%) 163855LLC Misses (1): 163840.00 (100.00%) 163840uncore_imc_0/UNC_M_RD_CAS_RANK0.ALLBANKS (1): 43345.00 (100.00%) 43345uncore_imc_0/UNC_M_RD_CAS_RANK4.ALLBANKS (1): 41115.00 (100.00%) 41115uncore_imc_1/UNC_M_RD_CAS_RANK0.ALLBANKS (1): 41383.00 (100.00%) 41383uncore_imc_1/UNC_M_RD_CAS_RANK4.ALLBANKS (1): 41105.00 (100.00%) 41105
我发起了163840次内存读,可以看到,流量被均分到0号通道的RANK0、RANK4和1号通道的RANK0、RANK4。
好了,本篇就到这里。基础原理有其复杂性,我尽量详细点讲述。基础原理相当于内功。内功越深厚,“招式”理解的越多、越准确。

