Jump to content
  • Advertisement
Sign in to follow this  

[fixed]how to get current gpu usage by nvapi for nvidia card?

This topic is 2158 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i found that last version nvapi library if full static library, some code display the method of get gpu usage on the internet is based on nvapi_QueryInterface interface, it is a export function in nvapi.dll?

i can't find nvapi_QueryInterface in the last version of nvapi.h,and there is nothing useful when i search "usage" in the header file.


this is the code based on nvapi_QueryInterface:

// Getting Nvidia GPU Usage
// Reference: Open Hardware Monitor (http://code.google.com/p/open-hardware-monitor)
#include <windows.h>
#include <iostream>
// magic numbers, do not change them
// function pointer types
typedef int *(*NvAPI_QueryInterface_t)(unsigned int offset);
typedef int (*NvAPI_Initialize_t)();
typedef int (*NvAPI_EnumPhysicalGPUs_t)(int **handles, int *count);
typedef int (*NvAPI_GPU_GetUsages_t)(int *handle, unsigned int *usages);
int main()
    HMODULE hmod = LoadLibraryA("nvapi.dll");
    if (hmod == NULL)
        std::cerr << "Couldn't find nvapi.dll" << std::endl;
        return 1;
    // nvapi.dll internal function pointers
    NvAPI_QueryInterface_t      NvAPI_QueryInterface     = NULL;
    NvAPI_Initialize_t          NvAPI_Initialize         = NULL;
    NvAPI_EnumPhysicalGPUs_t    NvAPI_EnumPhysicalGPUs   = NULL;
    NvAPI_GPU_GetUsages_t       NvAPI_GPU_GetUsages      = NULL;
    // nvapi_QueryInterface is a function used to retrieve other internal functions in nvapi.dll
    NvAPI_QueryInterface = (NvAPI_QueryInterface_t) GetProcAddress(hmod, "nvapi_QueryInterface");
    // some useful internal functions that aren't exported by nvapi.dll
    NvAPI_Initialize = (NvAPI_Initialize_t) (*NvAPI_QueryInterface)(0x0150E828);
    NvAPI_EnumPhysicalGPUs = (NvAPI_EnumPhysicalGPUs_t) (*NvAPI_QueryInterface)(0xE5AC921F);
    NvAPI_GPU_GetUsages = (NvAPI_GPU_GetUsages_t) (*NvAPI_QueryInterface)(0x189A1FDF);
    if (NvAPI_Initialize == NULL || NvAPI_EnumPhysicalGPUs == NULL ||
        NvAPI_EnumPhysicalGPUs == NULL || NvAPI_GPU_GetUsages == NULL)
        std::cerr << "Couldn't get functions in nvapi.dll" << std::endl;
        return 2;
    // initialize NvAPI library, call it once before calling any other NvAPI functions
    int          gpuCount = 0;
    int         *gpuHandles[NVAPI_MAX_PHYSICAL_GPUS] = { NULL };
    unsigned int gpuUsages[NVAPI_MAX_USAGES_PER_GPU] = { 0 };
    // gpuUsages[0] must be this value, otherwise NvAPI_GPU_GetUsages won't work
    gpuUsages[0] = (NVAPI_MAX_USAGES_PER_GPU * 4) | 0x10000;
    (*NvAPI_EnumPhysicalGPUs)(gpuHandles, &gpuCount);
    // print GPU usage every second
    for (int i = 0; i < 100; i++)
        (*NvAPI_GPU_GetUsages)(gpuHandles[0], gpuUsages);
        int usage = gpuUsages[3];
        std::cout << "GPU Usage: " << usage << std::endl;
    return 0;

    reference: http://eliang.blogspot.com/2011/05/getting-nvidia-gpu-usage-in-c.html


    thank you very much.


Share this post

Link to post
Share on other sites
            PstatesInfo.version = NV_GPU_DYNAMIC_PSTATES_INFO_EX_VER;

            for( INT i=0; i<pUsageData->GpuCount; ++i )
                ErrorCode = NvAPI_GPU_GetDynamicPstatesInfoEx( DefaultCard.GpuInfo[i].nvGpuHandle, &PstatesInfo );

                pUsageData->GpuUsage[i] = PstatesInfo.utilization[0].percentage;
ok, i get it.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!