当前位置:   article > 正文

如何获取显卡的GPU占用率和显存占用情况_vulkan 获取 gpu使用率

vulkan 获取 gpu使用率

在游戏的性能调优过程中,经常会需要获取CPU占用率、IO、显卡GPU占用率等基础性能数据,下面就简述一下获取nvdia显卡GPU占用率的方法。 nvdia 显卡在驱动安装后,可以通过nvapi.dll来获取显卡的相关信息。有关nvdia、AMD显卡sdk的相关资料在它们的网站上均有下载,但貌似非注册开发者可获取到的信息有限。

下面提供一个简单封装了的用来获取显卡信息的c++类,由于代码是根据网上可找到的有限资料写成,难免可能存在错误,如有问题欢迎指出。

  1. // KGpuMon.h 源代码
  2. #pragma once
  3. #define MAX_GPU_NUM 4L // 监控的GPU个数(NVIDIA定义的最多GPU个数是64,这里最多只监控4个)
  4. #define MY_PROCESS_ERROR(Condition) do { if (!(Condition)) goto Exit0; } while (false)
  5. #define MAX_DISPLAY_CARDS 4 // 最多监控4块显卡(暂时应该够了吧)
  6. // 常量定义
  7. #define MAX_PHYSICAL_GPUS 64
  8. #define SHORT_STRING_MAX 64
  9. #define MAX_THERMAL_SENSORS_PER_GPU 3
  10. #define MAX_CLOCKS_PER_GPU 0x120
  11. #define MAX_PSTATES_PER_GPU 8
  12. #define MAX_USAGES_PER_GPU 33
  13. #define MAX_COOLER_PER_GPU 20
  14. #define MAX_MEMORY_VALUES_PER_GPU 5
  15. // 接口ID值
  16. #define ID_NvAPI_Initialize 0x0150E828
  17. #define ID_NvAPI_GPU_GetFullName 0xCEEE8E9F
  18. #define ID_NvAPI_GPU_GetThermalSettings 0xE3640A56
  19. #define ID_NvAPI_EnumNvidiaDisplayHandle 0x9ABDD40D
  20. #define ID_NvAPI_GetPhysicalGPUsFromDisplay 0x34EF9506
  21. #define ID_NvAPI_EnumPhysicalGPUs 0xE5AC921F
  22. #define ID_NvAPI_GPU_GetTachReading 0x5F608315
  23. #define ID_NvAPI_GPU_GetAllClocks 0x1BD69F49
  24. #define ID_NvAPI_GPU_GetPStates 0x60DED2ED
  25. #define ID_NvAPI_GPU_GetUsages 0x189A1FDF
  26. #define ID_NvAPI_GPU_GetCoolerSettings 0xDA141340
  27. #define ID_NvAPI_GPU_SetCoolerLevels 0x891FA0AE
  28. #define ID_NvAPI_GPU_GetMemoryInfo 0x774AA982
  29. #define ID_NvAPI_GetDisplayDriverVersion 0xF951A4D1
  30. #define ID_NvAPI_GetInterfaceVersionString 0x01053FA5
  31. #define ID_NvAPI_GPU_GetPCIIdentifiers 0x2DDFB66E
  32. // 版本号参数定义
  33. #define GPU_THERMAL_SETTINGS_VER (sizeof(NvGPUThermalSettings) | 0x10000)
  34. #define GPU_CLOCKS_VER (sizeof(NvClocks) | 0x20000)
  35. #define GPU_PSTATES_VER (sizeof(NvPStates) | 0x10000)
  36. #define GPU_USAGES_VER (sizeof(NvUsages) | 0x10000)
  37. #define GPU_COOLER_SETTINGS_VER (sizeof(NvGPUCoolerSettings) | 0x20000)
  38. #define GPU_MEMORY_INFO_VER (sizeof(NvMemoryInfo) | 0x20000)
  39. #define DISPLAY_DRIVER_VERSION_VER (sizeof(NvDisplayDriverVersion) | 0x10000)
  40. #define GPU_COOLER_LEVELS_VER (sizeof(NvGPUCoolerLevels) | 0x10000)
  41. enum NvStatus
  42. {
  43. enumNvStatus_OK = 0,
  44. enumNvStatus_ERROR = -1,
  45. enumNvStatus_LIBRARY_NOT_FOUND = -2,
  46. enumNvStatus_NO_IMPLEMENTATION = -3,
  47. enumNvStatus_API_NOT_INTIALIZED = -4,
  48. enumNvStatus_INVALID_ARGUMENT = -5,
  49. enumNvStatus_NVIDIA_DEVICE_NOT_FOUND = -6,
  50. enumNvStatus_END_ENUMERATION = -7,
  51. enumNvStatus_INVALID_HANDLE = -8,
  52. enumNvStatus_INCOMPATIBLE_STRUCT_VERSION = -9,
  53. enumNvStatus_HANDLE_INVALIDATED = -10,
  54. enumNvStatus_OPENGL_CONTEXT_NOT_CURRENT = -11,
  55. enumNvStatus_NO_GL_EXPERT = -12,
  56. enumNvStatus_INSTRUMENTATION_DISABLED = -13,
  57. enumNvStatus_EXPECTED_LOGICAL_GPU_HANDLE = -100,
  58. enumNvStatus_EXPECTED_PHYSICAL_GPU_HANDLE = -101,
  59. enumNvStatus_EXPECTED_DISPLAY_HANDLE = -102,
  60. enumNvStatus_INVALID_COMBINATION = -103,
  61. enumNvStatus_NOT_SUPPORTED = -104,
  62. enumNvStatus_PORTID_NOT_FOUND = -105,
  63. enumNvStatus_EXPECTED_UNATTACHED_DISPLAY_HANDLE = -106,
  64. enumNvStatus_INVALID_PERF_LEVEL = -107,
  65. enumNvStatus_DEVICE_BUSY = -108,
  66. enumNvStatus_NV_PERSIST_FILE_NOT_FOUND = -109,
  67. enumNvStatus_PERSIST_DATA_NOT_FOUND = -110,
  68. enumNvStatus_EXPECTED_TV_DISPLAY = -111,
  69. enumNvStatus_EXPECTED_TV_DISPLAY_ON_DCONNECTOR = -112,
  70. enumNvStatus_NO_ACTIVE_SLI_TOPOLOGY = -113,
  71. enumNvStatus_SLI_RENDERING_MODE_NOTALLOWED = -114,
  72. enumNvStatus_EXPECTED_DIGITAL_FLAT_PANEL = -115,
  73. enumNvStatus_ARGUMENT_EXCEED_MAX_SIZE = -116,
  74. enumNvStatus_DEVICE_SWITCHING_NOT_ALLOWED = -117,
  75. enumNvStatus_TESTING_CLOCKS_NOT_SUPPORTED = -118,
  76. enumNvStatus_UNKNOWN_UNDERSCAN_CONFIG = -119,
  77. enumNvStatus_TIMEOUT_RECONFIGURING_GPU_TOPO = -120,
  78. enumNvStatus_DATA_NOT_FOUND = -121,
  79. enumNvStatus_EXPECTED_ANALOG_DISPLAY = -122,
  80. enumNvStatus_NO_VIDLINK = -123,
  81. enumNvStatus_REQUIRES_REBOOT = -124,
  82. enumNvStatus_INVALID_HYBRID_MODE = -125,
  83. enumNvStatus_MIXED_TARGET_TYPES = -126,
  84. enumNvStatus_SYSWOW64_NOT_SUPPORTED = -127,
  85. enumNvStatus_IMPLICIT_SET_GPU_TOPOLOGY_CHANGE_NOT_ALLOWED = -128,
  86. enumNvStatus_REQUEST_USER_TO_CLOSE_NON_MIGRATABLE_APPS = -129,
  87. enumNvStatus_OUT_OF_MEMORY = -130,
  88. enumNvStatus_WAS_STILL_DRAWING = -131,
  89. enumNvStatus_FILE_NOT_FOUND = -132,
  90. enumNvStatus_TOO_MANY_UNIQUE_STATE_OBJECTS = -133,
  91. enumNvStatus_INVALID_CALL = -134,
  92. enumNvStatus_D3D10_1_LIBRARY_NOT_FOUND = -135,
  93. enumNvStatus_FUNCTION_NOT_FOUND = -136
  94. };
  95. enum NvThermalController
  96. {
  97. enumNvThermalController_NONE = 0,
  98. enumNvThermalController_GPU_INTERNAL,
  99. enumNvThermalController_ADM1032,
  100. enumNvThermalController_MAX6649,
  101. enumNvThermalController_MAX1617,
  102. enumNvThermalController_LM99,
  103. enumNvThermalController_LM89,
  104. enumNvThermalController_LM64,
  105. enumNvThermalController_ADT7473,
  106. enumNvThermalController_SBMAX6649,
  107. enumNvThermalController_VBIOSEVT,
  108. enumNvThermalController_OS,
  109. enumNvThermalController_UNKNOWN = -1,
  110. };
  111. enum NvThermalTarget
  112. {
  113. enumNvThermalTarget_NONE = 0,
  114. enumNvThermalTarget_GPU = 1,
  115. enumNvThermalTarget_MEMORY = 2,
  116. enumNvThermalTarget_POWER_SUPPLY = 4,
  117. enumNvThermalTarget_BOARD = 8,
  118. enumNvThermalTarget_ALL = 15,
  119. enumNvThermalTarget_UNKNOWN = -1
  120. };
  121. typedef struct _NvSensor
  122. {
  123. NvThermalController Controller;
  124. unsigned int DefaultMinTemp;
  125. unsigned int DefaultMaxTemp;
  126. unsigned int CurrentTemp;
  127. NvThermalTarget Target;
  128. }NvSensor;
  129. typedef struct _NvGPUThermalSettings
  130. {
  131. unsigned int Version;
  132. unsigned int Count;
  133. NvSensor Sensor[MAX_THERMAL_SENSORS_PER_GPU];
  134. }NvGPUThermalSettings;
  135. typedef struct _NvClocks
  136. {
  137. unsigned int Version;
  138. unsigned int Clock[MAX_CLOCKS_PER_GPU];
  139. }NvClocks;
  140. typedef struct _NvPState
  141. {
  142. bool Present;
  143. int Percentage;
  144. }NvPState;
  145. typedef struct _NvPStates
  146. {
  147. unsigned int Version;
  148. unsigned int Flags;
  149. NvPState PStates[MAX_PSTATES_PER_GPU];
  150. }NvPStates;
  151. typedef struct _NvUsages
  152. {
  153. unsigned int Version;
  154. unsigned int Usages[MAX_USAGES_PER_GPU];
  155. }NvUsages;
  156. typedef struct _NvCooler
  157. {
  158. int Type;
  159. int Controller;
  160. int DefaultMin;
  161. int DefaultMax;
  162. int CurrentMin;
  163. int CurrentMax;
  164. int CurrentLevel;
  165. int DefaultPolicy;
  166. int CurrentPolicy;
  167. int Target;
  168. int ControlType;
  169. int Active;
  170. }NvCooler;
  171. typedef struct _NvGPUCoolerSettings
  172. {
  173. unsigned int Version;
  174. unsigned int Count;
  175. NvCooler Coolers[MAX_COOLER_PER_GPU];
  176. }NvGPUCoolerSettings;
  177. typedef struct _NvLevel
  178. {
  179. int Level;
  180. int Policy;
  181. }NvLevel;
  182. typedef struct _NvGPUCoolerLevels
  183. {
  184. unsigned int Version;
  185. NvLevel Levels[MAX_COOLER_PER_GPU];
  186. }NvGPUCoolerLevels;
  187. typedef struct _NvMemoryInfo
  188. {
  189. unsigned int Version;
  190. unsigned int Values[MAX_MEMORY_VALUES_PER_GPU];
  191. }NvMemoryInfo;
  192. typedef struct _NvDisplayDriverVersion
  193. {
  194. unsigned int Version;
  195. unsigned int DriverVersion;
  196. unsigned int BldChangeListNum;
  197. char szBuildBranch[SHORT_STRING_MAX];
  198. char szAdapter[SHORT_STRING_MAX];
  199. }NvDisplayDriverVersion;
  200. typedef int NvPhysicalGpuHandle;
  201. typedef int NvDisplayHandle;
  202. // 函数定义
  203. typedef void* (*nvapi_QueryInterfaceType)(unsigned int uiInterfaceID);
  204. typedef NvStatus (*NvAPI_InitializeType)();
  205. typedef NvStatus (*NvAPI_GPU_GetFullNameType)(const NvPhysicalGpuHandle gpuHandle, char *pszName);
  206. typedef NvStatus (*NvAPI_GPU_GetThermalSettingsType)(const NvPhysicalGpuHandle gpuHandle, int sensorIndex, NvGPUThermalSettings *pnvGPUThermalSettings);
  207. typedef NvStatus (*NvAPI_EnumNvidiaDisplayHandleType)(const int thisEnum, NvDisplayHandle *pDisplayHandle);
  208. typedef NvStatus (*NvAPI_GetPhysicalGPUsFromDisplayType)(const NvDisplayHandle displayHandle, NvPhysicalGpuHandle *pGpuHandles, unsigned int *pGpuCount);
  209. typedef NvStatus (*NvAPI_EnumPhysicalGPUsType)(NvPhysicalGpuHandle *pGpuHandles, int *pGpuCount);
  210. typedef NvStatus (*NvAPI_GPU_GetTachReadingType)(const NvPhysicalGpuHandle gpuHandle, int *pnValue);
  211. typedef NvStatus (*NvAPI_GPU_GetAllClocksType)(const NvPhysicalGpuHandle gpuHandle, NvClocks *pnvClocks);
  212. typedef NvStatus (*NvAPI_GPU_GetPStatesType)(const NvPhysicalGpuHandle gpuHandle, NvPStates *pnvPStates);
  213. typedef NvStatus (*NvAPI_GPU_GetUsagesType)(const NvPhysicalGpuHandle gpuHandle, NvUsages *pnvUsages);
  214. typedef NvStatus (*NvAPI_GPU_GetCoolerSettingsType)(const NvPhysicalGpuHandle gpuHandle, int coolerIndex, NvGPUCoolerSettings *pnvGPUCoolerSettings);
  215. typedef NvStatus (*NvAPI_GPU_SetCoolerLevelsType)(const NvPhysicalGpuHandle gpuHandle, int coolerIndex, NvGPUCoolerLevels *pnvGPUCoolerLevels);
  216. typedef NvStatus (*NvAPI_GPU_GetMemoryInfoType)(const NvDisplayHandle displayHandle, NvMemoryInfo *pnvMemoryInfo);
  217. typedef NvStatus (*NvAPI_GetDisplayDriverVersionType)(const NvDisplayHandle displayHandle, NvDisplayDriverVersion *pnvDisplayDriverVersion);
  218. typedef NvStatus (*NvAPI_GetInterfaceVersionStringType)(char *pszVersion);
  219. typedef NvStatus (*NvAPI_GPU_GetPCIIdentifiersType)(
  220. const NvPhysicalGpuHandle gpuHandle,
  221. unsigned int *puiDeviceId,
  222. unsigned int *puiSubSystemId,
  223. unsigned int *puiRevisionId,
  224. unsigned int *puiExtDeviceId
  225. );
  226. //*****************************以下是类内部使用的结构体************************************//
  227. /*
  228. * @brief GPU信息
  229. */
  230. typedef struct _GPU_INFO
  231. {
  232. NvPhysicalGpuHandle nvGpuHandle; // GPU句柄
  233. int nUsage; // GPU占用率
  234. }GPU_INFO;
  235. /*
  236. * @brief 显卡信息
  237. */
  238. typedef struct _DISPLAY_CARD_INFO
  239. {
  240. NvDisplayHandle nvDisplayHandle; // 显卡句柄
  241. int nGpuCount; // Gpu个数
  242. DWORD dwTotalMemory; // 总显存大小(KB)
  243. DWORD dwFreeMemory; // 空闲显存大小(KB)
  244. GPU_INFO sGpuInfo[MAX_GPU_NUM]; // GPU信息
  245. }DISPLAY_CARD_INFO;
  246. //*****************************************************************************************//
  247. /*
  248. * @brief 显卡相关信息(可用于导出的结构体)
  249. */
  250. typedef struct _DISPLAY_INFO
  251. {
  252. int nGpuCount; // Gpu个数
  253. int nGpuUsages[MAX_GPU_NUM]; // Gpu占用率
  254. DWORD dwTotalMemory; // 总显存大小(KB)
  255. DWORD dwFreeMemory; // 空闲显存大小(KB)
  256. }DISPLAY_INFO;
  257. class KGpuMon
  258. {
  259. public:
  260. KGpuMon(void);
  261. ~KGpuMon(void);
  262. BOOL Init();
  263. BOOL Unit();
  264. int GetDisplayCardCount();
  265. BOOL GetDisplayInfo(const int nCardIndex, DISPLAY_INFO *pDisplayInfo);
  266. private:
  267. int EnumDisplayCards();
  268. BOOL GetGpuHandles(const NvDisplayHandle nvDisplayHandle, DISPLAY_CARD_INFO *pCardInfo);
  269. BOOL GetDisplayCardGpuUsages(const NvDisplayHandle nvDisplayHandle, DISPLAY_CARD_INFO *pCardInfo);
  270. BOOL GetDisplayCardMemoryInfo(const NvDisplayHandle nvDisplayHandle, DISPLAY_CARD_INFO *pCardInfo);
  271. private:
  272. nvapi_QueryInterfaceType m_pfnNvapi_QueryInterface;
  273. NvAPI_InitializeType m_pfnNvAPI_Initialize;
  274. NvAPI_GPU_GetFullNameType m_pfnNvAPI_GPU_GetFullName;
  275. NvAPI_GPU_GetThermalSettingsType m_pfnNvAPI_GPU_GetThermalSettings;
  276. NvAPI_EnumNvidiaDisplayHandleType m_pfnNvAPI_EnumNvidiaDisplayHandle;
  277. NvAPI_GetPhysicalGPUsFromDisplayType m_pfnNvAPI_GetPhysicalGPUsFromDisplay;
  278. NvAPI_EnumPhysicalGPUsType m_pfnNvAPI_EnumPhysicalGPUs;
  279. NvAPI_GPU_GetTachReadingType m_pfnNvAPI_GPU_GetTachReading;
  280. NvAPI_GPU_GetAllClocksType m_pfnNvAPI_GPU_GetAllClocks;
  281. NvAPI_GPU_GetPStatesType m_pfnNvAPI_GPU_GetPStates;
  282. NvAPI_GPU_GetUsagesType m_pfnNvAPI_GPU_GetUsages;
  283. NvAPI_GPU_GetCoolerSettingsType m_pfnNvAPI_GPU_GetCoolerSettings;
  284. NvAPI_GPU_SetCoolerLevelsType m_pfnNvAPI_GPU_SetCoolerLevels;
  285. NvAPI_GPU_GetMemoryInfoType m_pfnNvAPI_GPU_GetMemoryInfo;
  286. NvAPI_GetDisplayDriverVersionType m_pfnNvAPI_GetDisplayDriverVersion;
  287. NvAPI_GetInterfaceVersionStringType m_pfnNvAPI_GetInterfaceVersionString;
  288. NvAPI_GPU_GetPCIIdentifiersType m_pfnNvAPI_GPU_GetPCIIdentifiers;
  289. int m_nDisplayCardCount;
  290. DISPLAY_CARD_INFO* m_pDisplayCards;
  291. HMODULE m_hNvApiDll;
  292. };
  293. //**************************************************************************************************/
  294. // KGpuMon.cpp 源代码
  295. #include "StdAfx.h"
  296. #include "KGpuMon.h"
  297. KGpuMon::KGpuMon(void)
  298. {
  299. m_hNvApiDll = NULL;
  300. m_nDisplayCardCount = 0;
  301. m_pDisplayCards = NULL;
  302. }
  303. KGpuMon::~KGpuMon(void)
  304. {
  305. Unit();
  306. }
  307. BOOL KGpuMon::Init()
  308. {
  309. BOOL bResult = FALSE;
  310. BOOL bRetCode = FALSE;
  311. int nIndex = 0;
  312. int nResult = 0;
  313. m_hNvApiDll = LoadLibrary(_T("nvapi.dll"));
  314. if (m_hNvApiDll)
  315. {
  316. m_pfnNvapi_QueryInterface = (nvapi_QueryInterfaceType)GetProcAddress(m_hNvApiDll, "nvapi_QueryInterface");
  317. if (m_pfnNvapi_QueryInterface)
  318. {
  319. m_pfnNvAPI_Initialize = (NvAPI_InitializeType)m_pfnNvapi_QueryInterface(ID_NvAPI_Initialize);
  320. m_pfnNvAPI_GPU_GetFullName = (NvAPI_GPU_GetFullNameType)m_pfnNvapi_QueryInterface(ID_NvAPI_GPU_GetFullName);
  321. m_pfnNvAPI_GPU_GetThermalSettings = (NvAPI_GPU_GetThermalSettingsType)m_pfnNvapi_QueryInterface(ID_NvAPI_GPU_GetThermalSettings);
  322. m_pfnNvAPI_EnumNvidiaDisplayHandle = (NvAPI_EnumNvidiaDisplayHandleType)m_pfnNvapi_QueryInterface(ID_NvAPI_EnumNvidiaDisplayHandle);
  323. m_pfnNvAPI_GetPhysicalGPUsFromDisplay = (NvAPI_GetPhysicalGPUsFromDisplayType)m_pfnNvapi_QueryInterface(ID_NvAPI_GetPhysicalGPUsFromDisplay);
  324. m_pfnNvAPI_EnumPhysicalGPUs = (NvAPI_EnumPhysicalGPUsType)m_pfnNvapi_QueryInterface(ID_NvAPI_EnumPhysicalGPUs);
  325. m_pfnNvAPI_GPU_GetTachReading = (NvAPI_GPU_GetTachReadingType)m_pfnNvapi_QueryInterface(ID_NvAPI_GPU_GetTachReading);
  326. m_pfnNvAPI_GPU_GetAllClocks = (NvAPI_GPU_GetAllClocksType)m_pfnNvapi_QueryInterface(ID_NvAPI_GPU_GetAllClocks);
  327. m_pfnNvAPI_GPU_GetPStates = (NvAPI_GPU_GetPStatesType)m_pfnNvapi_QueryInterface(ID_NvAPI_GPU_GetPStates);
  328. m_pfnNvAPI_GPU_GetUsages = (NvAPI_GPU_GetUsagesType)m_pfnNvapi_QueryInterface(ID_NvAPI_GPU_GetUsages);
  329. m_pfnNvAPI_GPU_GetCoolerSettings = (NvAPI_GPU_GetCoolerSettingsType)m_pfnNvapi_QueryInterface(ID_NvAPI_GPU_GetCoolerSettings);
  330. m_pfnNvAPI_GPU_SetCoolerLevels = (NvAPI_GPU_SetCoolerLevelsType)m_pfnNvapi_QueryInterface(ID_NvAPI_GPU_SetCoolerLevels);
  331. m_pfnNvAPI_GPU_GetMemoryInfo = (NvAPI_GPU_GetMemoryInfoType)m_pfnNvapi_QueryInterface(ID_NvAPI_GPU_GetMemoryInfo);
  332. m_pfnNvAPI_GetDisplayDriverVersion = (NvAPI_GetDisplayDriverVersionType)m_pfnNvapi_QueryInterface(ID_NvAPI_GetDisplayDriverVersion);
  333. m_pfnNvAPI_GetInterfaceVersionString = (NvAPI_GetInterfaceVersionStringType)m_pfnNvapi_QueryInterface(ID_NvAPI_GetInterfaceVersionString);
  334. m_pfnNvAPI_GPU_GetPCIIdentifiers = (NvAPI_GPU_GetPCIIdentifiersType)m_pfnNvapi_QueryInterface(ID_NvAPI_GPU_GetPCIIdentifiers);
  335. if (m_pfnNvAPI_Initialize)
  336. {
  337. nResult = m_pfnNvAPI_Initialize();
  338. if (enumNvStatus_OK == nResult)
  339. {
  340. m_pDisplayCards = new DISPLAY_CARD_INFO[MAX_DISPLAY_CARDS];
  341. ZeroMemory(m_pDisplayCards, MAX_DISPLAY_CARDS * sizeof(DISPLAY_CARD_INFO));
  342. // 获取显卡个数
  343. nResult = EnumDisplayCards();
  344. MY_PROCESS_ERROR(nResult > 0);
  345. // 获取每块显卡的GPU个数
  346. for (nIndex = 0; nIndex < m_nDisplayCardCount; ++nIndex)
  347. {
  348. bRetCode = GetGpuHandles(m_pDisplayCards[nIndex].nvDisplayHandle, &m_pDisplayCards[nIndex]);
  349. MY_PROCESS_ERROR(bRetCode);
  350. }
  351. bResult = TRUE;
  352. }
  353. }
  354. }
  355. }
  356. Exit0:
  357. return bResult;
  358. }
  359. BOOL KGpuMon::Unit()
  360. {
  361. m_nDisplayCardCount = 0;
  362. if (m_pDisplayCards)
  363. {
  364. delete []m_pDisplayCards;
  365. m_pDisplayCards = NULL;
  366. }
  367. if (m_hNvApiDll)
  368. {
  369. FreeLibrary(m_hNvApiDll);
  370. m_hNvApiDll = NULL;
  371. }
  372. return TRUE;
  373. }
  374. BOOL KGpuMon::GetDisplayInfo(const int nCardIndex, DISPLAY_INFO *pDisplayInfo)
  375. {
  376. BOOL bResult = FALSE;
  377. int nIndex = 0;
  378. if (nCardIndex < m_nDisplayCardCount)
  379. {
  380. bResult = GetDisplayCardGpuUsages(m_pDisplayCards[nCardIndex].nvDisplayHandle, &m_pDisplayCards[nCardIndex]);
  381. MY_PROCESS_ERROR(bResult);
  382. pDisplayInfo->nGpuCount = m_pDisplayCards[nCardIndex].nGpuCount;
  383. for (nIndex = 0; nIndex < pDisplayInfo->nGpuCount; ++nIndex)
  384. {
  385. pDisplayInfo->nGpuUsages[nIndex] = m_pDisplayCards[nCardIndex].sGpuInfo[nIndex].nUsage;
  386. }
  387. bResult = GetDisplayCardMemoryInfo(m_pDisplayCards[nCardIndex].nvDisplayHandle, &m_pDisplayCards[nCardIndex]);
  388. MY_PROCESS_ERROR(bResult);
  389. pDisplayInfo->dwTotalMemory = m_pDisplayCards[nCardIndex].dwTotalMemory;
  390. pDisplayInfo->dwFreeMemory = m_pDisplayCards[nCardIndex].dwFreeMemory;
  391. }
  392. Exit0:
  393. return bResult;
  394. }
  395. int KGpuMon::GetDisplayCardCount()
  396. {
  397. return m_nDisplayCardCount;
  398. }
  399. int KGpuMon::EnumDisplayCards()
  400. {
  401. NvStatus nvResult;
  402. NvDisplayHandle nvDisplayCardHandle;
  403. int nIndex = 0;
  404. m_nDisplayCardCount = 0;
  405. if (m_pfnNvAPI_EnumNvidiaDisplayHandle)
  406. {
  407. for (nIndex = 0; nIndex < MAX_DISPLAY_CARDS; ++nIndex)
  408. {
  409. nvResult = m_pfnNvAPI_EnumNvidiaDisplayHandle(nIndex, &nvDisplayCardHandle);
  410. if (enumNvStatus_OK == nvResult)
  411. {
  412. m_pDisplayCards[m_nDisplayCardCount].nvDisplayHandle = nvDisplayCardHandle;
  413. ++m_nDisplayCardCount;
  414. }
  415. }
  416. }
  417. return m_nDisplayCardCount;
  418. }
  419. BOOL KGpuMon::GetGpuHandles(const NvDisplayHandle nvDisplayHandle, DISPLAY_CARD_INFO *pCardInfo)
  420. {
  421. BOOL bResult = FALSE;
  422. NvStatus nvStatus;
  423. NvPhysicalGpuHandle *pnvHandles = NULL;
  424. int nIndex = 0;
  425. unsigned int uiGpuCount = 0;
  426. if (m_pfnNvAPI_GetPhysicalGPUsFromDisplay)
  427. {
  428. pnvHandles = new NvPhysicalGpuHandle[MAX_PHYSICAL_GPUS];
  429. nvStatus = m_pfnNvAPI_GetPhysicalGPUsFromDisplay(nvDisplayHandle, pnvHandles, &uiGpuCount);
  430. if (enumNvStatus_OK == nvStatus)
  431. {
  432. pCardInfo->nGpuCount = min(uiGpuCount, MAX_GPU_NUM);
  433. for (nIndex = 0; nIndex < pCardInfo->nGpuCount; ++nIndex)
  434. {
  435. pCardInfo->sGpuInfo[nIndex].nvGpuHandle = pnvHandles[nIndex];
  436. }
  437. bResult = TRUE;
  438. }
  439. delete []pnvHandles;
  440. pnvHandles = NULL;
  441. }
  442. return bResult;
  443. }
  444. BOOL KGpuMon::GetDisplayCardGpuUsages(const NvDisplayHandle nvDisplayHandle, DISPLAY_CARD_INFO *pCardInfo)
  445. {
  446. BOOL bResult = FALSE;
  447. int nIndex = 0;
  448. NvStatus nvStatus = enumNvStatus_ERROR;
  449. NvUsages *pnvUsages = NULL;
  450. if (m_pfnNvAPI_GPU_GetUsages)
  451. {
  452. pnvUsages = new NvUsages;
  453. pnvUsages->Version = GPU_USAGES_VER;
  454. for (nIndex = 0; nIndex < pCardInfo->nGpuCount; ++nIndex)
  455. {
  456. nvStatus = m_pfnNvAPI_GPU_GetUsages(pCardInfo->sGpuInfo[nIndex].nvGpuHandle, pnvUsages);
  457. if (enumNvStatus_OK == nvStatus)
  458. {
  459. pCardInfo->sGpuInfo[nIndex].nUsage = pnvUsages->Usages[2];
  460. }
  461. }
  462. delete pnvUsages;
  463. pnvUsages = NULL;
  464. bResult = (enumNvStatus_OK == nvStatus) ? TRUE : FALSE;
  465. }
  466. return bResult;
  467. }
  468. BOOL KGpuMon::GetDisplayCardMemoryInfo(const NvDisplayHandle nvDisplayHandle, DISPLAY_CARD_INFO *pCardInfo)
  469. {
  470. BOOL bResult = FALSE;
  471. int nIndex = 0;
  472. NvStatus nvStatus = enumNvStatus_ERROR;
  473. NvUsages *pnvUsages = NULL;
  474. NvMemoryInfo sMemoryInfo;
  475. if (m_pfnNvAPI_GPU_GetMemoryInfo)
  476. {
  477. sMemoryInfo.Version = GPU_MEMORY_INFO_VER;
  478. nvStatus = m_pfnNvAPI_GPU_GetMemoryInfo(nvDisplayHandle, &sMemoryInfo);
  479. if (enumNvStatus_OK == nvStatus)
  480. {
  481. pCardInfo->dwTotalMemory = (DWORD)(sMemoryInfo.Values[0]);
  482. pCardInfo->dwFreeMemory = (DWORD)(sMemoryInfo.Values[4]);
  483. bResult = TRUE;
  484. }
  485. }
  486. return bResult;
  487. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/245696?site
推荐阅读
相关标签
  

闽ICP备14008679号