28 static GUID GUID_DISPLAY_DEVICE_ARRIVAL_I = { 0x1ca05180, 0xa699, 0x450a, { 0x9a, 0x0c, 0xde, 0x4f, 0xbe, 0x3d, 0xdd, 0x89 } };
77 HMODULE setupapiHandle;
79 if (gdi32Handle = GetModuleHandle(L
"gdi32.dll"))
81 D3DKMTOpenAdapterFromDeviceName_I = (PVOID)GetProcAddress(gdi32Handle,
"D3DKMTOpenAdapterFromDeviceName");
82 D3DKMTCloseAdapter_I = (PVOID)GetProcAddress(gdi32Handle,
"D3DKMTCloseAdapter");
83 D3DKMTQueryStatistics_I = (PVOID)GetProcAddress(gdi32Handle,
"D3DKMTQueryStatistics");
86 if (setupapiHandle = LoadLibrary(L
"setupapi.dll"))
88 SetupDiGetClassDevsW_I = (PVOID)GetProcAddress(setupapiHandle,
"SetupDiGetClassDevsW");
89 SetupDiDestroyDeviceInfoList_I = (PVOID)GetProcAddress(setupapiHandle,
"SetupDiDestroyDeviceInfoList");
90 SetupDiEnumDeviceInterfaces_I = (PVOID)GetProcAddress(setupapiHandle,
"SetupDiEnumDeviceInterfaces");
91 SetupDiGetDeviceInterfaceDetailW_I = (PVOID)GetProcAddress(setupapiHandle,
"SetupDiGetDeviceInterfaceDetailW");
92 SetupDiGetDeviceRegistryPropertyW_I = (PVOID)GetProcAddress(setupapiHandle,
"SetupDiGetDeviceRegistryPropertyW");
96 D3DKMTOpenAdapterFromDeviceName_I &&
97 D3DKMTCloseAdapter_I &&
98 D3DKMTQueryStatistics_I &&
99 SetupDiGetClassDevsW_I &&
100 SetupDiDestroyDeviceInfoList_I &&
101 SetupDiEnumDeviceInterfaces_I &&
102 SetupDiGetDeviceInterfaceDetailW_I
128 memset(EtGpuNodesTotalRunningTimeDelta, 0,
sizeof(
PH_UINT64_DELTA) * EtGpuTotalNodeCount);
129 EtGpuNodesHistory = PhAllocate(
sizeof(PH_CIRCULAR_BUFFER_FLOAT) * EtGpuTotalNodeCount);
140 &ProcessesUpdatedCallbackRegistration
161 for (i = 0; i < EtpGpuAdapterList->
Count; i++)
165 for (j = 0; j < gpuAdapter->
NodeCount; j++)
172 if (
NT_SUCCESS(D3DKMTQueryStatistics_I(&queryStatistics)))
202 HDEVINFO deviceInfoSet;
204 SP_DEVICE_INTERFACE_DATA deviceInterfaceData;
205 PSP_DEVICE_INTERFACE_DETAIL_DATA detailData;
206 SP_DEVINFO_DATA deviceInfoData;
207 ULONG detailDataSize;
211 deviceInfoSet = SetupDiGetClassDevsW_I(&GUID_DISPLAY_DEVICE_ARRIVAL_I, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
217 deviceInterfaceData.cbSize =
sizeof(SP_DEVICE_INTERFACE_DATA);
219 while (SetupDiEnumDeviceInterfaces_I(deviceInfoSet, NULL, &GUID_DISPLAY_DEVICE_ARRIVAL_I, memberIndex, &deviceInterfaceData))
221 detailDataSize = 0x100;
222 detailData = PhAllocate(detailDataSize);
223 detailData->cbSize =
sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
224 deviceInfoData.cbSize =
sizeof(SP_DEVINFO_DATA);
226 if (!(result = SetupDiGetDeviceInterfaceDetailW_I(deviceInfoSet, &deviceInterfaceData, detailData, detailDataSize, &detailDataSize, &deviceInfoData)) &&
227 GetLastError() == ERROR_INSUFFICIENT_BUFFER)
230 detailData = PhAllocate(detailDataSize);
232 if (detailDataSize >=
sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA))
233 detailData->cbSize =
sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
235 result = SetupDiGetDeviceInterfaceDetailW_I(deviceInfoSet, &deviceInterfaceData, detailData, detailDataSize, &detailDataSize, &deviceInfoData);
240 openAdapterFromDeviceName.
pDeviceName = detailData->DevicePath;
242 if (
NT_SUCCESS(D3DKMTOpenAdapterFromDeviceName_I(&openAdapterFromDeviceName)))
248 if (
NT_SUCCESS(D3DKMTQueryStatistics_I(&queryStatistics)))
274 if (
NT_SUCCESS(D3DKMTQueryStatistics_I(&queryStatistics)))
308 SetupDiDestroyDeviceInfoList_I(deviceInfoSet);
319 _In_ ULONG NumberOfSegments
328 adapter = PhAllocate(sizeNeeded);
329 memset(adapter, 0, sizeNeeded);
335 _In_ HDEVINFO DeviceInfoSet,
336 _In_ PSP_DEVINFO_DATA DeviceInfoData
343 if (!SetupDiGetDeviceRegistryPropertyW_I)
349 if (!(result = SetupDiGetDeviceRegistryPropertyW_I(
357 )) && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
362 result = SetupDiGetDeviceRegistryPropertyW_I(
384 static VOID EtpUpdateSegmentInformation(
392 ULONG64 dedicatedUsage;
395 if (Block && !Block->ProcessItem->QueryHandle)
401 for (i = 0; i < EtpGpuAdapterList->
Count; i++)
403 gpuAdapter = EtpGpuAdapterList->
Items[i];
418 queryStatistics.
hProcess = Block->ProcessItem->QueryHandle;
426 if (
NT_SUCCESS(D3DKMTQueryStatistics_I(&queryStatistics)))
430 ULONG64 bytesCommitted;
442 sharedUsage += bytesCommitted;
444 dedicatedUsage += bytesCommitted;
448 ULONG64 bytesCommitted;
460 sharedUsage += bytesCommitted;
462 dedicatedUsage += bytesCommitted;
470 Block->GpuDedicatedUsage = dedicatedUsage;
471 Block->GpuSharedUsage = sharedUsage;
480 static VOID EtpUpdateNodeInformation(
488 ULONG64 totalRunningTime;
489 ULONG64 systemRunningTime;
491 if (Block && !Block->ProcessItem->QueryHandle)
494 totalRunningTime = 0;
495 systemRunningTime = 0;
497 for (i = 0; i < EtpGpuAdapterList->
Count; i++)
499 gpuAdapter = EtpGpuAdapterList->
Items[i];
501 for (j = 0; j < gpuAdapter->
NodeCount; j++)
517 queryStatistics.
hProcess = Block->ProcessItem->QueryHandle;
525 if (
NT_SUCCESS(D3DKMTQueryStatistics_I(&queryStatistics)))
551 PhUpdateDelta(&Block->GpuRunningTimeDelta, totalRunningTime);
555 LARGE_INTEGER performanceCounter;
558 PhUpdateDelta(&EtClockTotalRunningTimeDelta, performanceCounter.QuadPart);
559 PhUpdateDelta(&EtGpuTotalRunningTimeDelta, totalRunningTime);
560 PhUpdateDelta(&EtGpuSystemRunningTimeDelta, systemRunningTime);
565 _In_opt_ PVOID Parameter,
566 _In_opt_ PVOID Context
569 static ULONG runCount = 0;
573 PLIST_ENTRY listEntry;
574 FLOAT maxNodeValue = 0;
579 EtpUpdateSegmentInformation(NULL);
580 EtpUpdateNodeInformation(NULL);
584 if (elapsedTime != 0)
602 EtGpuNodeBitMap.
Buffer = newBuffer;
620 EtpUpdateSegmentInformation(block);
621 EtpUpdateNodeInformation(block);
623 if (elapsedTime != 0)
631 if (maxNodeValue < block->GpuNodeUsage)
634 maxNodeBlock = block;
637 listEntry = listEntry->Flink;
652 usage = (FLOAT)(EtGpuNodesTotalRunningTimeDelta[i].Delta / elapsedTime);
691 return EtpGpuAdapterList->
Count;
701 for (i = 0; i < EtpGpuAdapterList->
Count; i++)
703 gpuAdapter = EtpGpuAdapterList->
Items[i];
718 if (Index >= EtpGpuAdapterList->
Count)
738 SIZE_T numberOfBytes;
742 BitMap->Buffer = PhAllocate(numberOfBytes);
744 memset(BitMap->Buffer, 0, numberOfBytes);
760 _In_ HANDLE ProcessHandle,
772 for (i = 0; i < EtpGpuAdapterList->
Count; i++)
774 gpuAdapter = EtpGpuAdapterList->
Items[i];
777 Statistics->NodeCount += gpuAdapter->
NodeCount;
784 queryStatistics.
hProcess = ProcessHandle;
787 if (
NT_SUCCESS(status = D3DKMTQueryStatistics_I(&queryStatistics)))
789 ULONG64 bytesCommitted;
801 Statistics->SharedCommitted += bytesCommitted;
803 Statistics->DedicatedCommitted += bytesCommitted;
807 for (j = 0; j < gpuAdapter->
NodeCount; j++)
812 queryStatistics.
hProcess = ProcessHandle;
815 if (
NT_SUCCESS(D3DKMTQueryStatistics_I(&queryStatistics)))
825 queryStatistics.
hProcess = ProcessHandle;
827 if (
NT_SUCCESS(D3DKMTQueryStatistics_I(&queryStatistics)))