1

我的目的是使用模型特定寄存器 (MSR) 测量特定应用程序的不同性能事件的计数。

因为,这可以通过在 ring 0 处使用 RDMSR 和 WRMSR 来完成,我使用 Linux 模块来调用这些指令并使用“insmod”插入它。

然后我在 /dev 目录 (mknod) 中创建了字符设备文件,并使用 ioctl 调用在用户模式下调用它。

当我在两个 ioctl 调用之间使用计算循环时,它给了我正确的计数。但是,当我使用 system("./sample") 调用而不是循环时,它给了我错误的值。

struct MsrInOut {
        unsigned int op;                            // MsrOperation
        unsigned int ecx;                           // msr identifier
        union {
                struct {
                        unsigned int eax;           // low double word
                        unsigned int edx;           // high double word
                };
                unsigned long long value; // quad word
        };
};
{ MSR_WRITE, 0x186, 0x00410280, 0x00 }, // ia32_perfevtsel1,lcache misses(very important)

ioctl(fd, IOCTL_MSR_CMDS, (long long)msr_start);
system("./sample"); //binary file of same computational for loop 
ioctl(fd, IOCTL_MSR_CMDS, (long long)msr_stop);

我阅读了这些调用,并且知道这些调用会重置 MSR。有没有办法克服这个问题?或者有没有其他方法可以在 C 程序中运行应用程序?

我提到了这个http://www.mindfruit.co.uk/2012/11/a-linux-kernel-module-for.html
如果有人知道答案,请告诉我。在此先感谢。

4

0 回答 0