how to get the special process GPU usage with the PDH API

Chief Guo 21 Reputation points
2025-12-01T03:28:27.0666667+00:00

Hello all

I want to get the special process GPU usage, how could I conbine the counter path. I have tried the "\GPU Engine(%s#%d)\Utilization Percentage", %s is the process name, the %d is the process id or instance index. but all of these testing are failed.

How to conbine the counter path.

Windows development | Windows API - Win32
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2025-12-01T03:28:36.63+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To retrieve GPU usage for a specific process using the Performance Data Helper (PDH) API, you need to construct the counter path correctly. The counter path format you are trying to use, \GPU Engine(%s#%d)\Utilization Percentage, is close, but you should ensure that you are using the correct instance name and index for the GPU engine you are targeting.

    Here are some steps to help you combine the counter path:

    1. Identify the GPU Engine: Make sure you know the exact name of the GPU engine you want to monitor. This can often be found in the Performance Monitor or similar tools.
    2. Use the Correct Instance Index: The instance index should correspond to the specific instance of the GPU engine you are interested in. If there are multiple instances, you can use #0, #1, etc., to specify them.
    3. Check the Counter Path Syntax: Ensure that your counter path follows the correct syntax. For example:
      • If the GPU engine is named "NVIDIA GPU", and you are interested in the first instance, your counter path might look like this: \GPU Engine(NVIDIA GPU#0)\Utilization Percentage.
    4. Use Wildcards if Necessary: If you are unsure about the instance names, you can use wildcards to expand the list of available instances. For example, you could use \GPU Engine(*)\Utilization Percentage to see all available GPU engines and their utilization percentages.

    Make sure to test the counter path in the PDH API to ensure it returns the expected results. If you continue to experience issues, verify that your GPU driver supports the necessary instrumentation features and that you have the appropriate permissions to access the performance counters.

    0 comments No comments

  2. Danny Nguyen (WICLOUD CORPORATION) 5,065 Reputation points Microsoft External Staff Moderator
    2025-12-01T06:35:34.9933333+00:00

    Hi @Chief Guo ,

    GPU Engine counters don’t take process name / PID in a format like:

    
    \GPU Engine(%s#%d)\Utilization Percentage
    
    

    That’s why your attempts fail. The instance names for GPU Engine are defined by the OS/driver and must be enumerated, not guessed.

    1. Enumerate valid instances

    Use PdhEnumObjectItems to see what GPU Engine instances exist:

    
    DWORD counterSize = 0, instanceSize = 0;
    
    PdhEnumObjectItemsW(
    
        NULL, NULL, L"GPU Engine",
    
        NULL, &counterSize,
    
        NULL, &instanceSize,
    
        PERF_DETAIL_WIZARD, 0
    
    );
    
    WCHAR* instances = (WCHAR*)malloc(instanceSize * sizeof(WCHAR));
    
    PdhEnumObjectItemsW(
    
        NULL, NULL, L"GPU Engine",
    
        NULL, &counterSize,
    
        instances, &instanceSize,
    
        PERF_DETAIL_WIZARD, 0
    
    );
    
    // `instances` is MULTI_SZ with names like "pid_1234_engtype_3D_0", etc.
    
    

    Then build the path using the exact instance string:

    
    const wchar_t* inst = L"pid_1234_engtype_3D_0"; // from list
    
    wchar_t path[512];
    
    swprintf_s(path, L"\\GPU Engine(%s)\\Utilization Percentage", inst);
    
    PdhAddCounterW(hQuery, path, 0, &hCounter);
    
    

    General PDH usage (enumerate -> add counter -> collect data) is documented here:

    Using the PDH Functions to Consume Counter Data

    2. Per‑process GPU usage

    If your GPU Engine instances include pid_XXXX, you can:

    1. Enumerate all instances.
    2. Filter ones containing pid_<yourPid>.
    3. Add \GPU Engine(<thatInstance>)\Utilization Percentage for each and sum them.

    If your system doesn’t expose PIDs in GPU Engine instance names, look at:

    
    \GPU Process(*)\Utilization Percentage
    
    

    instead, which typically has pid_XXXX instances.

    You can inspect what’s available with:

    
    Get-Counter -ListSet 'GPU*'
    
    

    Hope this helps


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.