How do I use performance counters inside of the kernel?

血红的双手。 提交于 2019-12-21 17:37:35

问题


I want to access performance counters inside the kernel. I found many ways to use performance counters in user space, but can you tell me some way to use those in kernel space.

Please don't specify tool name, I want to write my own code, preferably a kernel module. I am using Ubuntu with kernel 3.18.1.


回答1:


http://www.cise.ufl.edu/~sb3/files/pmc.pdf http://www.cs.inf.ethz.ch/stricker/lab/doc/intel-part4.pdf The first pdf contains description on how to use pmc.

The second contains the address of perfeventsel0 and perfeventsel1. Ive shown an example below.U'll need to set the event number and umask as per ur requirement.

void SetUpEvent(void){
int reg_addr=0x186; 
int event_no=0x0024; 
int umask=0x3F00; 
int enable_bits=0x430000; 
int event=event_no | umask | enable_bits;


__asm__ ("wrmsr" : : "c"(reg_addr), "a"(event), "d"(0x00));

}

/* Read the performance monitor counter */
long int ReadCounter(void){
    long int count;
    long int eax_low, edx_high;
    int reg_addr=0xC1; 


    __asm__("rdmsr" : "=a"(eax_low), "=d"(edx_high) : "c"(reg_addr));
    count = ((long int)eax_low | (long int)edx_high<<32);

    return count;
}



回答2:


You should check if you CPU and other HW support you needs. Try look into oprofile source code. It have kernel module and userspace api. You can for example cut part of interesting code from oprofile kernel module part and use it into you module. I gues you module should have several reader or listeners with circle buffers for events keeping. You can also look inside linux/drivers/oprofile and to correspond linux/arch/.../oprofile. Inside make menuconfig you can config it like module or build-in and add additional timers. Available events and counters you can find under oprofile/events/ of oprofile tool (TLB_MISS, CPU_CYCLES, CYCLES_DATA_STALL, ...).

ARM Performance monitoring register

Under linux/arch/arm64/kernel/perf_regs.c you can find arm specific details.



来源:https://stackoverflow.com/questions/29765275/how-do-i-use-performance-counters-inside-of-the-kernel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!