1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
//---------------------------------------------------------------------------
#include <vcl.h>
#include <nvapi.h>
// magic numbers, do not change them
#define NVAPI_MAX_PHYSICAL_GPUS 64
#define NVAPI_MAX_USAGES_PER_GPU 34
#define NVAPI_MAX_THERMAL_SENSORS_PER_GPU 3
#define NVAPI_MAX_CLOCKS_PER_GPU 0x120;
#define NVAPI_MAX_PSTATES_PER_GPU 8
#define NVAPI_MAX_COOLER_PER_GPU 20
#define NVAPI_MAX_MEMORY_VALUES_PER_GPU 5
// 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);
typedef int (*NvAPI_EnumNvidiaDisplayHandle_t) (NvU32 thisEnum, NvDisplayHandle *pNvDispHandle);
typedef int (*NvAPI_GetPhysicalGPUsFromDisplay_t) (NvDisplayHandle hNvDisp, NvPhysicalGpuHandle nvGPUHandle[NVAPI_MAX_PHYSICAL_GPUS], NvU32 *pGpuCount);
typedef int (*NvAPI_GPU_GetThermalSettings_t) (NvPhysicalGpuHandle hPhysicalGpu, NvU32 sensorIndex, NV_GPU_THERMAL_SETTINGS *pThermalSettings);
//typedef int (*NvAPI_GPU_GetMemoryInfo_t) (NvDisplayHandle displayHandle, NvMemoryInfo nvMemoryInfo);
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Memo1->Clear();
Memo1->Lines->Add("Gpu Usage");
HMODULE hmod = LoadLibraryA("nvapi.dll");
if (hmod == NULL) Memo1->Lines->Add("Couldn't find nvapi.dll");
// 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_GPU_GetThermalSettings_t NvAPI_GPU_GetThermalSettings = NULL;
NvAPI_EnumNvidiaDisplayHandle_t NvAPI_EnumNvidiaDisplayHandle = NULL;
NvAPI_GetPhysicalGPUsFromDisplay_t NvAPI_GetPhysicalGPUsFromDisplay = NULL;
// NvAPI_GPU_GetMemoryInfo_t NvAPI_GPU_GetMemoryInfo = 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);
NvAPI_GPU_GetThermalSettings = (NvAPI_GPU_GetThermalSettings_t) (*NvAPI_QueryInterface)(0xE3640A56);
NvAPI_EnumNvidiaDisplayHandle = (NvAPI_EnumNvidiaDisplayHandle_t) (*NvAPI_QueryInterface)(0x9ABDD40D);
NvAPI_GetPhysicalGPUsFromDisplay = (NvAPI_GetPhysicalGPUsFromDisplay_t) (*NvAPI_QueryInterface)(0x34EF9506);
// NvAPI_GPU_GetMemoryInfo = (NvAPI_GPU_GetMemoryInfo_t) (*NvAPI_QueryInterface)(0x774AA982);
if (NvAPI_Initialize == NULL || NvAPI_EnumPhysicalGPUs == NULL || NvAPI_EnumPhysicalGPUs == NULL || NvAPI_GPU_GetUsages == NULL)
Memo1->Lines->Add("Couldn't get functions in nvapi.dll");
// initialize NvAPI library, call it once before calling any other NvAPI functions
(*NvAPI_Initialize)();
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 < 1; i++)
{
(*NvAPI_GPU_GetUsages)(gpuHandles[0], gpuUsages);
int usage = gpuUsages[3];
Memo1->Lines->Add(usage);
Sleep(1000);
}
Memo1->Lines->Add("-------");
Memo1->Lines->Add("Gpu Temperature");
// http://open-hardware-monitor.googlecode.com/svn/trunk/Hardware/Nvidia/NVAPI.cs
// http://www.spieleprogrammierer.de/18-c-cplusplus-csharp-delphi-java-python-und-lua/10823-frage-zu-cplusplus-betreffs-temperatur-von-nvidia-grakas-auslesen/page-2/
// http://open-hardware-monitor.googlecode.com/svn/trunk/Hardware/Nvidia/NvidiaGPU.cs
// http://open-hardware-monitor.googlecode.com/svn/trunk/Hardware/
int i=0;
unsigned long cnt;
NvDisplayHandle hDisplay_a[NVAPI_MAX_PHYSICAL_GPUS * 2] = {0};
NvPhysicalGpuHandle nvGPUHandle = {0};
NvAPI_EnumNvidiaDisplayHandle(i, &hDisplay_a[i]);
NvAPI_GetPhysicalGPUsFromDisplay(hDisplay_a[0], &nvGPUHandle, &cnt);
NV_GPU_THERMAL_SETTINGS set = {0};
set.version = NV_GPU_THERMAL_SETTINGS_VER;
set.count = 0;
set.sensor[0].controller = NVAPI_THERMAL_CONTROLLER_UNKNOWN;
set.sensor[0].target = NVAPI_THERMAL_TARGET_GPU;
NvAPI_GPU_GetThermalSettings(nvGPUHandle, 0 ,&set);
Memo1->Lines->Add(set.sensor[0].currentTemp);
}
//--------------------------------------------------------------------------- |
Partager