如何在内核内部使用性能计数器? [英] How do I use performance counters inside of the kernel?

查看:125
本文介绍了如何在内核内部使用性能计数器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问内核内部的性能计数器.我找到了许多在用户空间中使用性能计数器的方法,但是您能告诉我在内核空间中使用性能计数器的一些方法吗?

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.

不要指定工具名称,我想编写自己的代码,最好是内核模块.我在内核3.18.1中使用Ubuntu.

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.

推荐答案

http ://www.cise.ufl.edu/~sb3/files/pmc.pdf http://www.cs.inf.ethz.ch /stricker/lab/doc/intel-part4.pdf 第一个pdf包含有关如何使用pmc的描述.

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.

第二个包含perfeventsel0和perfeventsel1的地址. 我已经在下面显示了一个示例,您需要根据您的要求设置事件编号和umask.

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;
}

这篇关于如何在内核内部使用性能计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆