Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I read the CPU performance counters from a user-mode program in Windows?

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

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

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.

like image 614
BeeOnRope Avatar asked Aug 01 '17 03:08

BeeOnRope


People also ask

How do I view performance counters in Windows?

You can view performance counters using the Microsoft Windows Reliability and Performance Monitor application. Click Start > Run. In the Open field, enter perfmon , and then click OK. From Monitoring Tools, select Performance Monitor.

How do I access performance counters?

Type "perfmon" into the command prompt and start the Performance Monitor application. On the left-hand navigation panel, select Monitoring Tools then select Performance Monitor. In the main window you will see the performance counters currently being observed.

Where are performance counters stored?

When you run a Data Collector Set, the data that is collected for performance counters is stored to a log file (. blg) in the location that was defined when the Data Collector Set was created. In Windows Performance Monitor, you can view log files to see a visual representation of performance counter data.

Which Windows tool allows the monitoring of counters on a Windows system?

Performance Monitor is available on Windows operating systems. You can use Performance Monitor to collect and view performance data from performance counters and trace events in real-time or from a log file.


1 Answers

Short answer

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

Long answer

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.

Open Hardware Monitor

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

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.

User-Mode access

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.

like image 89
huysentruitw Avatar answered Sep 22 '22 02:09

huysentruitw