我可以从Windows中的用户模式程序读取CPU性能计数器吗? [英] Can I read the CPU performance counters from a user-mode program in Windows?

查看:122
本文介绍了我可以从Windows中的用户模式程序读取CPU性能计数器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编程并阅读所有最新x86硬件上提供的硬件性能计数器

I would like to program and read the hardware performance counters offered on all recent x86 hardware.

在Linux上,有各种perf_events系统可以执行此操作(perf实用程序可以从未经修改的程序外部执行此操作).

On Linux there are the various perf_events systems to do this (and the perf utility to do it from outside an unmodified program).

Windows中是否有这样的内置工具?如果没有内置工具,则第二好的选择可能是使用第三方代码的另一种方法,但这不需要我签名驱动程序.

Is there any such built-in facility in Windows? If no built-in facility exists, the second best would be another approach perhaps using third-party code, but that doesn't require me to get a driver signed.

推荐答案

简短答案

否,Windows中没有内置功能.同样,Linux perf命令在Windows 10的Linux子系统上不起作用.

No, there's no built-in facility in Windows. Also the linux perf command doesn't work on the Linux Subsystem for Windows 10.

好答案

要访问这些计数器,您需要结合以下说明:

To get access to those counters, you'll need a combination of these instructions:

  • rdpmc __readpmc (see related answer)
  • rdmsr __readmsr
  • wrmsr __writemsr

不幸的是,这些指令只能从内核模式调用,因此您需要与驱动程序接口.虽然编写驱动程序代码本身很容易,但是获得驱动程序签名并不是那么容易(尤其是您提到过要单独进行此操作).

Unfortunately these instructions can only be called from kernel mode, so you'll need to interface with a driver. While writing the driver code itself is easy, getting the driver signed is not that easy (especially as you mentioned you want to do this as an individual).

这就是为什么我建议您研究现有项目,例如打开硬件监视器

That's why I advise you to look into existing projects like Open Hardware Monitor and the pcm project by Intel.

打开硬件监视器

开源项目是用C#编写的,包括二进制文件和 C源代码 OpenLibSys.org开发的.sys(64位)驱动程序.如果要在项目中使用此驱动程序,则只需包含其

This open-source project is written in C# and includes binaries and C source-code of a WinRing0.sys (32-bit) / WinRing0x64.sys (64-bit) driver developed by OpenLibSys.org. If you want to use this driver in your project, you only need to include their copyright notice.

PCM

开源项目用C ++编写,并且还包含类似驱动程序的源代码(请参见WinMSRDriver目录),但您必须自己构建它,以便再次遇到签名问题.

This open-source project is written in C++ and also contains source for a similar driver (see WinMSRDriver directory), but you have to build it yourself so you'll turn into the signing problem again.

无论如何,想提及这个项目,因为它可能包含许多您可能感兴趣的代码.

Anyway, wanted to mention this project because it probably contains a lot of code which might be of your interest.

用户模式访问

现在,一旦您加载了该驱动程序(Open Hardware Monitor在应用程序启动时就自动提取并加载了该驱动程序,这非常简洁),您就可以使用Windows API函数 DeviceIoControl ,当然还有 CloseHandle 从您的用户模式应用程序.

Now, once you have that driver loaded (Open Hardware Monitor extracts and loads the driver automatically on start of the application which is pretty neat), you can start calling those driver IOCTL's by using the Windows API functions CreateFile / DeviceIoControl and of course CloseHandle from your user-mode application.

这篇关于我可以从Windows中的用户模式程序读取CPU性能计数器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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