Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Obtaining CPU Utilization Metrics #64

Open
Kiryuumaru opened this issue Mar 25, 2024 · 5 comments
Open

Question: Obtaining CPU Utilization Metrics #64

Kiryuumaru opened this issue Mar 25, 2024 · 5 comments

Comments

@Kiryuumaru
Copy link

Kiryuumaru commented Mar 25, 2024

First off, big thanks for this awesome lib! I'm digging into CPU metrics on Windows and wondering how to get the PercentProcessorUtility instead of PercentProcessorTime. It seems more in line with Task Manager's display. Any tips on grabbing this specific metric?

@Jinjinov
Copy link
Owner

I am glad you like my lib :)

Take a look at GetCpuList https://github.com/Jinjinov/Hardware.Info/blob/master/Hardware.Info/Windows/HardwareInfoRetrieval.cs#L247
and replace PercentProcessorTime with PercentProcessorUtility like so:

    ulong percentProcessorUtility = 0ul;

    string queryString = "SELECT Name, PercentProcessorUtility FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name != '_Total'";
    ManagementObjectSearcher percentProcessorUtilityMOS = new ManagementObjectSearcher(_managementScope, queryString, _enumerationOptions);

    queryString = "SELECT PercentProcessorUtility FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name = '_Total'";
    ManagementObjectSearcher totalPercentProcessorUtilityMOS = new ManagementObjectSearcher(_managementScope, queryString, _enumerationOptions);

    try
    {
        foreach (ManagementBaseObject mo in percentProcessorUtilityMOS.Get())
        {
            // for each core:
            //Name = GetPropertyString(mo["Name"])
            //PercentProcessorUtility = GetPropertyValue<ulong>(mo["PercentProcessorUtility"])
        }

        foreach (ManagementBaseObject mo in totalPercentProcessorUtilityMOS.Get())
        {
            // total:
            percentProcessorUtility = GetPropertyValue<ulong>(mo["PercentProcessorUtility"]);
        }
    }
    catch (ManagementException)
    {
        // https://github.com/Jinjinov/Hardware.Info/issues/30
    }
    finally
    {
        percentProcessorUtilityMOS.Dispose();

        totalPercentProcessorUtilityMOS.Dispose();
    }

@Kiryuumaru
Copy link
Author

Thank you for providing the solution! I've successfully implemented the usage of PercentProcessorUtility on Windows and it's working. I was wondering if there's a possibility to include support for this feature directly into the library in future updates? It would be really beneficial for users who rely on obtaining CPU utilization metrics

@Jinjinov
Copy link
Owner

I just noticed that I am using PercentProcessorTime under Win32_PerfFormattedData_PerfOS_Processor but PercentProcessorUtility is listed only under Win32_PerfRawData_Counters_ProcessorInformation

Googling "PercentProcessorUtility Win32_PerfFormattedData_PerfOS_Processor" actually returns no results.

As you can see here https://github.com/Jinjinov/Hardware.Info?tab=readme-ov-file#known-issues the main problem with these counters is their slow initialization.

If PercentProcessorUtility was under Win32_PerfFormattedData_PerfOS_Processor I would have no problem adding it into the lib. But querying another counter Win32_PerfRawData_Counters_ProcessorInformation might slow things down. I would have to test it extensively before adding it.

How large is the actual difference between PercentProcessorUtility and PercentProcessorTime values?

@Kiryuumaru
Copy link
Author

Kiryuumaru commented Mar 27, 2024

Thanks for looking into it! From my real-time comparisons, I've noticed a pretty big gap between PercentProcessorUtility and PercentProcessorTime in real-time comparisons. During stress tests, PercentProcessorUtility seems to track Task Manager's numbers better, but it doesn't cap at 100% like Task Manager does—it goes beyond, hitting around 200%. As for Linux ubuntu, its PercentProcessorTime lines up pretty well with its system monitor, unlike Windows's PercentProcessorTime with Task Manager.

@Jinjinov
Copy link
Owner

Could you tell me a few startup times of your app under different conditions?

If you start the stopwatch in the first line of void Main() and immediately execute these, how long do they take

  • new HardwareInfo() and RefreshCPUList(false)
  • new HardwareInfo() and RefreshCPUList(true)
  • your code for PercentProcessorUtility
  • new HardwareInfo() and RefreshCPUList(false) and your code for PercentProcessorUtility
  • new HardwareInfo() and RefreshCPUList(true) and your code for PercentProcessorUtility

If you tell me these times for both DEBUG and RELEASE (or at least RELEASE) it would really help me a lot.

It would allow me to compare the startup times on your PC with those on my PC.

If I am going to query another counter Win32_PerfRawData_Counters_ProcessorInformation then I really want to test the impact on app startup time on as many PCs as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants