Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Performance Counter Help, Nvidia GPU

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?

like image 435
Alex E Avatar asked Apr 03 '16 19:04

Alex E


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


2 Answers

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

like image 55
Alex E Avatar answered Oct 02 '22 07:10

Alex E


Doing a PerformanceCounterCategory.GetCategories() will get you a list of all categories on your system, once you find the correct catagory you can use PerformanceCounterCategory.GetCounters() will list all counters in that catagory so you can get the correct name.

var cat = new PerformanceCounterCategory("Whatever the correct category name is");
var counters = cat.GetCounters();

Update:

Try this to search for it

static void Main(string[] args)
{
    var counters = PerformanceCounterCategory.GetCategories()
        .SelectMany(x=>x.GetCounters("")).Where(x => x.CounterName.Contains("GPU"));

    foreach (var counter in counters)
    {
        Console.WriteLine("{0} - {1}", counter.CategoryName, counter.CounterName);
    }
    Console.ReadLine();
}
like image 22
Scott Chamberlain Avatar answered Oct 02 '22 09:10

Scott Chamberlain