C# Performance Counter Help, Nvidia GPU

Alex E picture Alex E · Apr 3, 2016 · Viewed 7.5k times · Source

So I've been experimenting with the performance counter class in C# and have had great success probing the CPU counters and almost everything I can find in the windows performance monitor. HOWEVER, I cannot gain access to the "NVIDIA GPU" category...

So for example, the following line of code is how it usually works.

PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");

That code works fine, but the GPU category that appeared in the performance monitor, just as the processor category did, is not accessible by C#. The following line of code attempts to access it.

PerformanceCounter gpuCounter = new PerformanceCounter("NVIDIA GPU", "% GPU Usage","#0 Quadro K1100M(id=1, NVAPI ID=256)");

Instead it throws a "Category does not exist" exception...

Here is what it looks like from within the Performance Monitor

enter image description here

The category clearly exists, so my question is... how do I access this counter?

Answer

Alex E picture Alex E · Apr 5, 2016

After spending six hours on this, I am ready to share my results. There were several testing methods used to search for this missing performance counter.

1) The first was a very simple search for keywords. This included PerformaceCounterCategory's as well as every single instance of performance counter running on my system (around 12,000 of them). This method did not find anything GPU or NVIDIA related, but worked flawlessly for the CPU.

2) The next test was performed by running the GPU at a very specific % usage and a search was performed that evaluated every percentage performance counter running within a narrow range of values. This method also did not return anything GPU related, but again worked flawlessly for the CPU.

This lead to one conclusion. There truly is no performance counter for the GPU, not even under a false alias.


Solution:

Nowhere does it state that the performance monitor must explicitly use performance counters, and a small hint to this was staring us in the face this entire time. The instance variable in the performance monitor was labeled "NVAPI ID=256". The NVIDIA API was designed to allow performance monitoring capabilities to NVIDIA hardware. Although it can perform actions similar to performance counters, it does not use the same data types to perform its task. We now have evidence that the Performance Monitor was using the NVAPI to gather information instead of using a performance counter. This was very misleading because every other entry corresponds to a counter. So we can conclude that the only proper way to access these variables is through the NVAPI.

I was able to do this successfully by first downloading the NVAPI from the NVIDIA website here.

Next I used some sample code found here from a blogger that is using Open Hardware Monitor source code, and here from another post on this site.

The code is written in C++, but after moving some libraries around and making a few .dll's everything ports nicely to C#.

Here is a picture of the result. Thank you everyone for your help and support!

enter image description here