Files
dmex 6d330ca52f Improve UI rendering and plugin reliability
Create controls at their initial layout sizes and reduce system information panel flicker. Recover GPU/NPU monitoring after adapter restarts, correct sensor aggregation, and optimize graph updates. Restrict upload destinations, bound updater downloads, use monotonic transfer timing, and fix resource leaks and firewall cache races.
2026-10-24 02:35:15 +11:00

1627 lines
54 KiB
C

/*
* Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved.
*
* This file is part of System Informer.
*
* Authors:
*
* wj32 2011-2015
* dmex 2016-2026
*
*/
#include "exttools.h"
#include <devguid.h>
#include <ntddvdeo.h>
#include "gpumon.h"
static PH_CALLBACK_REGISTRATION EtGpuProcessesUpdatedCallbackRegistration;
BOOLEAN EtGpuEnabled = FALSE;
BOOLEAN EtGpuSupported = FALSE;
BOOLEAN EtGpuD3DEnabled = FALSE;
BOOLEAN EtGpuAdapterStatsEnabled = FALSE;
BOOLEAN EtGpuD3DEnumProcesses = FALSE;
PPH_LIST EtpGpuAdapterList;
ULONG EtGpuTotalNodeCount = 0;
ULONG EtGpuTotalSegmentCount = 0;
ULONG EtGpuNextNodeIndex = 0;
ULONG64 EtGpuSystemRunningTimeDelta = 0;
FLOAT EtGpuNodeUsage = 0;
PH_CIRCULAR_BUFFER_FLOAT EtGpuNodeHistory;
PH_CIRCULAR_BUFFER_ULONG EtMaxGpuNodeHistory; // ID of max. GPU usage process
PH_CIRCULAR_BUFFER_FLOAT EtMaxGpuNodeUsageHistory;
PPH_UINT64_DELTA EtGpuNodesTotalRunningTimeDelta;
PPH_UINT64_DELTA EtGpuNodesSystemRunningTimeDelta;
PPH_CIRCULAR_BUFFER_FLOAT EtGpuNodesHistory;
ULONG64 EtGpuDedicatedLimit = 0;
ULONG64 EtGpuDedicatedUsage = 0;
ULONG64 EtGpuSharedLimit = 0;
ULONG64 EtGpuSharedUsage = 0;
FLOAT EtGpuPowerUsageLimit = 100.0f;
FLOAT EtGpuPowerUsage = 0.0f;
FLOAT EtGpuTemperatureLimit = 0.0f;
FLOAT EtGpuTemperature = 0.0f;
ULONG64 EtGpuFanRpmLimit = 0;
ULONG64 EtGpuFanRpm = 0;
PH_CIRCULAR_BUFFER_ULONG64 EtGpuDedicatedHistory;
PH_CIRCULAR_BUFFER_ULONG64 EtGpuSharedHistory;
PH_CIRCULAR_BUFFER_FLOAT EtGpuPowerUsageHistory;
PH_CIRCULAR_BUFFER_FLOAT EtGpuTemperatureHistory;
PH_CIRCULAR_BUFFER_ULONG64 EtGpuFanRpmHistory;
#define ET_UPDATE_PROCESS_STATISTICS_MINMAX(Minimum, Maximum, Difference, Value) \
if ((Value) != 0 && ((Minimum) == 0 || (Value) < (Minimum))) \
(Minimum) = (Value); \
if ((Value) != 0 && ((Maximum) == 0 || (Value) > (Maximum))) \
(Maximum) = (Value); \
(Difference) = (Maximum) - (Minimum);
/**
* Initializes GPU monitoring functionality, allocating history buffers
* and registering necessary callbacks if monitoring is supported.
*
* \param DiscoveredAdapterList The transient adapter discovery list owned by
* the load callback.
*/
VOID EtGpuMonitorInitialization(
_In_opt_ PPH_LIST DiscoveredAdapterList
)
{
if (PhGetIntegerSetting(SETTING_NAME_ENABLE_GPU_MONITOR))
{
EtGpuSupported = EtWindowsVersion >= WINDOWS_10_RS4; // Note: Changed to RS4 due to reports of BSODs on LTSB versions of RS3 (dmex)
EtGpuD3DEnabled = EtGpuSupported && !!PhGetIntegerSetting(SETTING_NAME_ENABLE_GPUPERFCOUNTERS);
EtGpuAdapterStatsEnabled = EtGpuSupported && !!PhGetIntegerSetting(SETTING_NAME_ENABLE_GPU_ADAPTER_STATS);
EtGpuD3DEnumProcesses = EtWindowsVersion >= WINDOWS_11;
EtpGpuAdapterList = PhCreateList(4);
if (EtpGpuInitializeD3DStatistics(DiscoveredAdapterList))
EtGpuEnabled = TRUE;
}
if (EtGpuD3DEnabled)
{
EtPerfCounterInitialization();
}
if (EtGpuEnabled)
{
ULONG i;
PhInitializeCircularBuffer_FLOAT(&EtGpuNodeHistory, EtSampleCount);
PhInitializeCircularBuffer_ULONG(&EtMaxGpuNodeHistory, EtSampleCount);
PhInitializeCircularBuffer_FLOAT(&EtMaxGpuNodeUsageHistory, EtSampleCount);
PhInitializeCircularBuffer_ULONG64(&EtGpuDedicatedHistory, EtSampleCount);
PhInitializeCircularBuffer_ULONG64(&EtGpuSharedHistory, EtSampleCount);
if (EtGpuSupported)
{
PhInitializeCircularBuffer_FLOAT(&EtGpuPowerUsageHistory, EtSampleCount);
PhInitializeCircularBuffer_FLOAT(&EtGpuTemperatureHistory, EtSampleCount);
PhInitializeCircularBuffer_ULONG64(&EtGpuFanRpmHistory, EtSampleCount);
}
if (!EtGpuD3DEnabled)
{
EtGpuNodesTotalRunningTimeDelta = PhAllocateZero(sizeof(PH_UINT64_DELTA) * EtGpuTotalNodeCount);
EtGpuNodesSystemRunningTimeDelta = PhAllocateZero(sizeof(PH_UINT64_DELTA) * EtGpuTotalNodeCount);
}
EtGpuNodesHistory = PhAllocateZero(sizeof(PH_CIRCULAR_BUFFER_FLOAT) * EtGpuTotalNodeCount);
for (i = 0; i < EtGpuTotalNodeCount; i++)
{
PhInitializeCircularBuffer_FLOAT(&EtGpuNodesHistory[i], EtSampleCount);
}
PhRegisterCallback(
PhGetGeneralCallback(GeneralCallbackProcessProviderUpdatedEvent),
EtGpuProcessesUpdatedCallback,
NULL,
&EtGpuProcessesUpdatedCallbackRegistration
);
}
}
/**
* Adds a discovered GPU adapter to the internal monitoring list.
*
* \param DeviceInterface The device interface path of the adapter.
* \param AdapterHandle A handle to the D3D adapter.
* \param AdapterLuid The Locally Unique Identifier of the adapter.
* \param NumberOfSegments The number of memory segments for this adapter.
* \param NumberOfNodes The number of compute nodes for this adapter.
* \return A pointer to the newly allocated and added GPU adapter structure.
*/
PETP_GPU_ADAPTER EtpAddGpuAdapter(
_In_ PPH_STRING DeviceInterface,
_In_ D3DKMT_HANDLE AdapterHandle,
_In_ LUID AdapterLuid,
_In_ ULONG NumberOfSegments,
_In_ ULONG NumberOfNodes
)
{
PETP_GPU_ADAPTER adapter;
adapter = EtpAllocateGpuAdapter(NumberOfSegments);
adapter->DeviceInterface = PhReferenceObject(DeviceInterface);
adapter->AdapterLuid = AdapterLuid;
adapter->NodeCount = NumberOfNodes;
adapter->SegmentCount = NumberOfSegments;
RtlInitializeBitMap(&adapter->ApertureBitMap, adapter->ApertureBitMapBuffer, NumberOfSegments);
if (DeviceInterface)
{
PPH_STRING description;
if (EtQueryDeviceProperties(DeviceInterface, &description, NULL, NULL, NULL, NULL))
{
adapter->Description = description;
}
}
// Open a dedicated long-lived handle for reuse in update callbacks. The caller's
// handle (AdapterHandle) is transient and is closed when the graphics-adapter
// enumeration is torn down, so it must not be cached here. Prefer opening our own
// handle from the device interface; only fall back to the caller's handle when no
// device interface is available to reopen from. (#2924)
adapter->CachedAdapterHandle = 0;
if (DeviceInterface)
{
EtOpenAdapterFromDeviceName(&adapter->CachedAdapterHandle, NULL, PhGetString(DeviceInterface));
}
else
{
adapter->CachedAdapterHandle = AdapterHandle;
}
if (EtGpuSupported)
{
// The caller's handle is still valid here, use it when ours could not be opened.
D3DKMT_HANDLE adapterHandle = adapter->CachedAdapterHandle ? adapter->CachedAdapterHandle : AdapterHandle;
adapter->NodeNameList = PhCreateList(adapter->NodeCount);
for (ULONG i = 0; i < adapter->NodeCount; i++)
{
D3DKMT_NODEMETADATA metaDataInfo;
memset(&metaDataInfo, 0, sizeof(D3DKMT_NODEMETADATA));
metaDataInfo.NodeOrdinalAndAdapterIndex = MAKEWORD(i, 0);
if (adapterHandle && NT_SUCCESS(EtQueryAdapterInformation(
adapterHandle,
KMTQAITYPE_NODEMETADATA,
&metaDataInfo,
sizeof(D3DKMT_NODEMETADATA)
)))
{
PhAddItemList(adapter->NodeNameList, EtGetNodeEngineTypeString(&metaDataInfo));
}
else
{
PhAddItemList(adapter->NodeNameList, PhReferenceEmptyString());
}
}
}
PhAddItemList(EtpGpuAdapterList, adapter);
return adapter;
}
/**
* Initializes GPU D3D statistics from a previously discovered adapter list.
*
* \param DiscoveredAdapterList The adapter discovery list to inspect.
* \return TRUE if at least one adapter was successfully initialized, FALSE otherwise.
*/
BOOLEAN EtpGpuInitializeD3DStatistics(
_In_opt_ PPH_LIST DiscoveredAdapterList
)
{
D3DKMT_QUERYSTATISTICS queryStatistics;
D3DKMT_ADAPTER_PERFDATACAPS perfCaps;
ULONG processedCount = 0;
if (!DiscoveredAdapterList)
return FALSE;
for (ULONG i = 0; i < DiscoveredAdapterList->Count; i++)
{
PET_DISCOVERED_ADAPTER entry = DiscoveredAdapterList->Items[i];
D3DKMT_HANDLE adapterHandle = 0;
LUID adapterLuid;
//
// Skip only adapters positively identified as NPU/media-only. Older drivers
// (e.g. some AMD) advertise D3D graphics capability without the newer
// DXCORE_HARDWARE_TYPE_ATTRIBUTE_GPU tag, so treat any GPU/compute/D3D
// graphics capability as a GPU. (#2924)
//
if (entry->AttributesValid &&
!entry->Attributes.TypeGpu &&
!entry->Attributes.TypeComputeAccelerator &&
!entry->Attributes.D3D11Graphics &&
!entry->Attributes.D3D12Graphics &&
!entry->Attributes.D3D12CoreCompute &&
(entry->Attributes.TypeNpu || entry->Attributes.TypeMediaAccelerator))
continue;
adapterLuid = entry->AdapterLuid;
if (entry->AdapterHandle)
{
adapterHandle = entry->AdapterHandle;
}
else if (entry->DeviceInterface)
{
if (!NT_SUCCESS(EtOpenAdapterFromDeviceName(
&adapterHandle,
&adapterLuid,
PhGetString(entry->DeviceInterface)
)))
{
continue;
}
}
if (!adapterHandle)
continue;
if (EtGpuSupported && processedCount > 0)
{
if (EtIsSoftwareDevice(adapterHandle))
{
if (entry->AdapterHandle == 0)
EtCloseAdapterHandle(adapterHandle);
continue;
}
}
if (EtGpuSupported)
{
D3DKMT_SEGMENTSIZEINFO segmentInfo;
memset(&segmentInfo, 0, sizeof(D3DKMT_SEGMENTSIZEINFO));
if (NT_SUCCESS(EtQueryAdapterInformation(
adapterHandle,
KMTQAITYPE_GETSEGMENTSIZE,
&segmentInfo,
sizeof(D3DKMT_SEGMENTSIZEINFO)
)))
{
EtGpuDedicatedLimit += segmentInfo.DedicatedVideoMemorySize;
EtGpuSharedLimit += segmentInfo.SharedSystemMemorySize;
}
memset(&perfCaps, 0, sizeof(D3DKMT_ADAPTER_PERFDATACAPS));
if (NT_SUCCESS(EtQueryAdapterInformation(
adapterHandle,
KMTQAITYPE_ADAPTERPERFDATA_CAPS,
&perfCaps,
sizeof(D3DKMT_ADAPTER_PERFDATACAPS)
)))
{
//
// Use the highest limit since we show one graph for all.
// The temperature caps are in deci-Celsius (1 = 0.1C).
//
EtGpuTemperatureLimit = max(EtGpuTemperatureLimit, (FLOAT)max(perfCaps.TemperatureWarning, perfCaps.TemperatureMax) / 10);
EtGpuFanRpmLimit = max(EtGpuFanRpmLimit, (ULONG64)perfCaps.MaxFanRPM);
}
}
memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS));
queryStatistics.Type = D3DKMT_QUERYSTATISTICS_ADAPTER;
queryStatistics.AdapterLuid = adapterLuid;
if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics)))
{
PETP_GPU_ADAPTER gpuAdapter;
gpuAdapter = EtpAddGpuAdapter(
entry->DeviceInterface,
adapterHandle,
adapterLuid,
queryStatistics.QueryResult.AdapterInformation.NbSegments,
queryStatistics.QueryResult.AdapterInformation.NodeCount
);
gpuAdapter->FirstNodeIndex = EtGpuNextNodeIndex;
EtGpuTotalNodeCount += gpuAdapter->NodeCount;
EtGpuTotalSegmentCount += gpuAdapter->SegmentCount;
EtGpuNextNodeIndex += gpuAdapter->NodeCount;
for (ULONG ii = 0; ii < gpuAdapter->SegmentCount; ii++)
{
memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS));
queryStatistics.Type = D3DKMT_QUERYSTATISTICS_SEGMENT;
queryStatistics.AdapterLuid = gpuAdapter->AdapterLuid;
queryStatistics.QuerySegment.SegmentId = ii;
if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics)))
{
ULONG64 commitLimit;
ULONG aperture;
if (EtWindowsVersion >= WINDOWS_8)
{
commitLimit = queryStatistics.QueryResult.SegmentInformation.CommitLimit;
aperture = queryStatistics.QueryResult.SegmentInformation.Aperture;
}
else
{
PD3DKMT_QUERYSTATISTICS_SEGMENT_INFORMATION_V1 segmentInfoV1;
segmentInfoV1 = (PD3DKMT_QUERYSTATISTICS_SEGMENT_INFORMATION_V1)&queryStatistics.QueryResult;
commitLimit = segmentInfoV1->CommitLimit;
aperture = segmentInfoV1->Aperture;
}
if (!EtGpuSupported || !EtGpuD3DEnabled)
{
if (aperture)
EtGpuSharedLimit += commitLimit;
else
EtGpuDedicatedLimit += commitLimit;
}
if (aperture)
RtlSetBits(&gpuAdapter->ApertureBitMap, ii, 1);
}
}
}
if (entry->AdapterHandle == 0)
EtCloseAdapterHandle(adapterHandle);
processedCount++;
}
if (EtGpuSupported && processedCount > 0)
{
// Set limit at 100C (dmex)
if (EtGpuTemperatureLimit == 0)
EtGpuTemperatureLimit = 100;
}
if (EtGpuTotalNodeCount == 0)
return FALSE;
return TRUE;
}
/**
* Allocates a new GPU adapter structure and initializes its segment tracking data.
*
* \param NumberOfSegments The number of segments the adapter has.
* \return A pointer to the newly allocated GPU adapter structure.
*/
PETP_GPU_ADAPTER EtpAllocateGpuAdapter(
_In_ ULONG NumberOfSegments
)
{
PETP_GPU_ADAPTER adapter;
SIZE_T sizeNeeded;
sizeNeeded = FIELD_OFFSET(ETP_GPU_ADAPTER, ApertureBitMapBuffer);
sizeNeeded += BYTES_NEEDED_FOR_BITS(NumberOfSegments);
adapter = PhAllocate(sizeNeeded);
memset(adapter, 0, sizeNeeded);
return adapter;
}
/**
* Zeros out the GPU adapter statistics for a given process block.
*
* \param Block The process block to clear.
*/
static VOID EtpZeroProcessGpuAdapterStatistics(
_In_ PET_PROCESS_BLOCK Block
)
{
Block->GpuVirtualMemoryUsage = 0;
Block->GpuVidPnSourceCount = 0;
Block->GpuTotalBytesEvicted = 0;
Block->GpuDmaBufferSize = 0;
Block->GpuDmaAllocationListBytes = 0;
Block->GpuDmaPatchLocationListBytes = 0;
Block->GpuInterferenceTotal = 0;
}
/**
* Updates the segment usage information (dedicated and shared memory) for a specific process.
*
* \param Block The process block to update.
*/
VOID EtpGpuUpdateProcessSegmentInformation(
_In_ PET_PROCESS_BLOCK Block
)
{
ULONG i;
ULONG j;
PETP_GPU_ADAPTER gpuAdapter;
D3DKMT_QUERYSTATISTICS queryStatistics;
ULONG64 dedicatedUsage;
ULONG64 sharedUsage;
ULONG64 commitUsage;
ULONG64 dedicatedCommitted;
ULONG64 sharedCommitted;
if (!Block->ProcessItem->QueryHandle)
return;
dedicatedUsage = 0;
sharedUsage = 0;
commitUsage = 0;
dedicatedCommitted = 0;
sharedCommitted = 0;
for (i = 0; i < EtpGpuAdapterList->Count; i++)
{
gpuAdapter = EtpGpuAdapterList->Items[i];
// Query dedicated usage (local segment group)
memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS));
queryStatistics.Type = D3DKMT_QUERYSTATISTICS_PROCESS_SEGMENT_GROUP;
queryStatistics.AdapterLuid = gpuAdapter->AdapterLuid;
queryStatistics.hProcess = Block->ProcessItem->QueryHandle;
queryStatistics.QueryProcessSegmentGroup = D3DKMT_MEMORY_SEGMENT_GROUP_LOCAL;
if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics)))
{
dedicatedUsage += queryStatistics.QueryResult.ProcessSegmentGroupInformation.Usage;
}
// Query shared usage (non-local segment group)
memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS));
queryStatistics.Type = D3DKMT_QUERYSTATISTICS_PROCESS_SEGMENT_GROUP;
queryStatistics.AdapterLuid = gpuAdapter->AdapterLuid;
queryStatistics.hProcess = Block->ProcessItem->QueryHandle;
queryStatistics.QueryProcessSegmentGroup = D3DKMT_MEMORY_SEGMENT_GROUP_NON_LOCAL;
if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics)))
{
sharedUsage += queryStatistics.QueryResult.ProcessSegmentGroupInformation.Usage;
}
for (j = 0; j < gpuAdapter->SegmentCount; j++)
{
memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS));
queryStatistics.Type = D3DKMT_QUERYSTATISTICS_PROCESS_SEGMENT;
queryStatistics.AdapterLuid = gpuAdapter->AdapterLuid;
queryStatistics.hProcess = Block->ProcessItem->QueryHandle;
queryStatistics.QueryProcessSegment.SegmentId = j;
if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics)))
{
ULONG64 bytesCommitted;
if (EtWindowsVersion >= WINDOWS_8)
bytesCommitted = queryStatistics.QueryResult.ProcessSegmentInformation.BytesCommitted;
else
bytesCommitted = (ULONG)queryStatistics.QueryResult.ProcessSegmentInformation.BytesCommitted;
if (RtlCheckBit(&gpuAdapter->ApertureBitMap, j))
sharedCommitted += bytesCommitted;
else
dedicatedCommitted += bytesCommitted;
}
}
memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS));
queryStatistics.Type = D3DKMT_QUERYSTATISTICS_PROCESS;
queryStatistics.AdapterLuid = gpuAdapter->AdapterLuid;
queryStatistics.hProcess = Block->ProcessItem->QueryHandle;
if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics)))
{
commitUsage += queryStatistics.QueryResult.ProcessInformation.SystemMemory.BytesAllocated;
}
}
Block->GpuDedicatedUsage = dedicatedUsage;
Block->GpuSharedUsage = sharedUsage;
Block->GpuCommitUsage = commitUsage;
Block->GpuDedicatedCommitted = dedicatedCommitted;
Block->GpuSharedCommitted = sharedCommitted;
}
/**
* Updates detailed GPU adapter statistics (e.g., virtual memory, DMA buffers, evictions)
* for a specific process block.
*
* \param Block The process block to update.
*/
VOID EtpGpuUpdateProcessAdapterStatistics(
_In_ PET_PROCESS_BLOCK Block
)
{
ULONG i;
PETP_GPU_ADAPTER gpuAdapter;
D3DKMT_QUERYSTATISTICS queryStatistics;
ULONG virtualMemoryUsage;
ULONG vidPnSourceCount;
ULONG64 bytesEvicted;
ULONG64 dmaBufferSize;
ULONG dmaAllocationListBytes;
ULONG dmaPatchLocationListBytes;
ULONG64 interferenceTotal;
if (!EtGpuAdapterStatsEnabled)
return;
if (!Block->ProcessItem->QueryHandle)
{
EtpZeroProcessGpuAdapterStatistics(Block);
return;
}
virtualMemoryUsage = 0;
vidPnSourceCount = 0;
bytesEvicted = 0;
dmaBufferSize = 0;
dmaAllocationListBytes = 0;
dmaPatchLocationListBytes = 0;
interferenceTotal = 0;
for (i = 0; i < EtpGpuAdapterList->Count; i++)
{
gpuAdapter = EtpGpuAdapterList->Items[i];
memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS));
queryStatistics.Type = D3DKMT_QUERYSTATISTICS_PROCESS_ADAPTER;
queryStatistics.AdapterLuid = gpuAdapter->AdapterLuid;
queryStatistics.hProcess = Block->ProcessItem->QueryHandle;
if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics)))
{
D3DKMT_QUERYSTATISTICS_PROCESS_ADAPTER_INFORMATION* adapterInfo =
&queryStatistics.QueryResult.ProcessAdapterInformation;
virtualMemoryUsage += adapterInfo->VirtualMemoryUsage;
vidPnSourceCount += adapterInfo->VidPnSourceCount;
bytesEvicted += adapterInfo->CommitmentData.TotalBytesEvictedFromProcess;
dmaBufferSize += adapterInfo->DmaBuffer.Size.Bytes;
dmaAllocationListBytes += adapterInfo->DmaBuffer.AllocationListBytes;
dmaPatchLocationListBytes += adapterInfo->DmaBuffer.PatchLocationListBytes;
#if (DXGKDDI_INTERFACE_VERSION >= DXGKDDI_INTERFACE_VERSION_WDDM2_1)
for (ULONG k = 0; k < D3DKMT_QUERYSTATISTICS_PROCESS_INTERFERENCE_BUCKET_COUNT; k++)
interferenceTotal += adapterInfo->ProcessInterferenceCounters.InterferenceCount[k];
#endif
}
}
Block->GpuVirtualMemoryUsage = virtualMemoryUsage;
Block->GpuVidPnSourceCount = vidPnSourceCount;
Block->GpuTotalBytesEvicted = bytesEvicted;
Block->GpuDmaBufferSize = dmaBufferSize;
Block->GpuDmaAllocationListBytes = dmaAllocationListBytes;
Block->GpuDmaPatchLocationListBytes = dmaPatchLocationListBytes;
Block->GpuInterferenceTotal = interferenceTotal;
}
/**
* Updates the global system-wide GPU segment information (total dedicated and shared memory usage).
*/
VOID EtpGpuUpdateSystemSegmentInformation(
VOID
)
{
ULONG i;
ULONG j;
PETP_GPU_ADAPTER gpuAdapter;
D3DKMT_QUERYSTATISTICS queryStatistics;
ULONG64 dedicatedUsage;
ULONG64 sharedUsage;
dedicatedUsage = 0;
sharedUsage = 0;
for (i = 0; i < EtpGpuAdapterList->Count; i++)
{
gpuAdapter = EtpGpuAdapterList->Items[i];
for (j = 0; j < gpuAdapter->SegmentCount; j++)
{
memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS));
queryStatistics.Type = D3DKMT_QUERYSTATISTICS_SEGMENT;
queryStatistics.AdapterLuid = gpuAdapter->AdapterLuid;
queryStatistics.QuerySegment.SegmentId = j;
if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics)))
{
ULONG64 bytesResident;
ULONG aperture;
if (EtWindowsVersion >= WINDOWS_8)
{
bytesResident = queryStatistics.QueryResult.SegmentInformation.BytesResident;
aperture = queryStatistics.QueryResult.SegmentInformation.Aperture;
}
else
{
PD3DKMT_QUERYSTATISTICS_SEGMENT_INFORMATION_V1 segmentInfo;
segmentInfo = (PD3DKMT_QUERYSTATISTICS_SEGMENT_INFORMATION_V1)&queryStatistics.QueryResult;
bytesResident = segmentInfo->BytesResident;
aperture = segmentInfo->Aperture;
}
if (aperture) // RtlCheckBit(&gpuAdapter->ApertureBitMap, j)
sharedUsage += bytesResident;
else
dedicatedUsage += bytesResident;
}
}
}
EtGpuDedicatedUsage = dedicatedUsage;
EtGpuSharedUsage = sharedUsage;
}
/**
* Updates the GPU node running time information for a specific process block.
*
* \param Block The process block to update.
*/
VOID EtpGpuUpdateProcessNodeInformation(
_In_ PET_PROCESS_BLOCK Block
)
{
ULONG i;
ULONG j;
PETP_GPU_ADAPTER gpuAdapter;
D3DKMT_QUERYSTATISTICS queryStatistics;
ULONG64 totalRunningTime;
for (i = 0; i < EtGpuTotalNodeCount; i++)
Block->GpuNodesRunningTimeDelta[i].Delta = 0;
if (!Block->ProcessItem->QueryHandle)
return;
totalRunningTime = 0;
for (i = 0; i < EtpGpuAdapterList->Count; i++)
{
gpuAdapter = EtpGpuAdapterList->Items[i];
for (j = 0; j < gpuAdapter->NodeCount; j++)
{
memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS));
queryStatistics.Type = D3DKMT_QUERYSTATISTICS_PROCESS_NODE;
queryStatistics.AdapterLuid = gpuAdapter->AdapterLuid;
queryStatistics.hProcess = Block->ProcessItem->QueryHandle;
queryStatistics.QueryProcessNode.NodeId = j;
if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics)))
{
ULONG nodeIndex;
ULONG64 runningTime;
nodeIndex = gpuAdapter->FirstNodeIndex + j;
runningTime = queryStatistics.QueryResult.ProcessNodeInformation.RunningTime.QuadPart;
PhUpdateDelta(&Block->GpuNodesRunningTimeDelta[nodeIndex], runningTime);
totalRunningTime += runningTime;
//totalContextSwitches += queryStatistics.QueryResult.ProcessNodeInformation.ContextSwitch;
}
}
}
PhUpdateDelta(&Block->GpuRunningTimeDelta, totalRunningTime);
}
/**
* Updates the global system-wide GPU node running time information.
*/
VOID EtpGpuUpdateSystemNodeInformation(
VOID
)
{
PETP_GPU_ADAPTER gpuAdapter;
D3DKMT_QUERYSTATISTICS queryStatistics;
ULONG64 totalSystemDelta = 0;
for (ULONG i = 0; i < EtpGpuAdapterList->Count; i++)
{
gpuAdapter = EtpGpuAdapterList->Items[i];
for (ULONG j = 0; j < gpuAdapter->NodeCount; j++)
{
memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS));
queryStatistics.Type = D3DKMT_QUERYSTATISTICS_NODE;
queryStatistics.AdapterLuid = gpuAdapter->AdapterLuid;
queryStatistics.QueryNode.NodeId = j;
if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics)))
{
ULONG nodeIndex = gpuAdapter->FirstNodeIndex + j;
ULONG64 runningTime;
ULONG64 systemRunningTime;
runningTime = queryStatistics.QueryResult.NodeInformation.GlobalInformation.RunningTime.QuadPart;
systemRunningTime = queryStatistics.QueryResult.NodeInformation.SystemInformation.RunningTime.QuadPart;
PhUpdateDelta(&EtGpuNodesTotalRunningTimeDelta[nodeIndex], runningTime);
PhUpdateDelta(&EtGpuNodesSystemRunningTimeDelta[nodeIndex], systemRunningTime);
totalSystemDelta += EtGpuNodesSystemRunningTimeDelta[nodeIndex].Delta;
}
}
}
EtGpuSystemRunningTimeDelta = totalSystemDelta;
}
PPH_LIST EtpBuildGpuProcessList(
_Out_ PBOOLEAN FilterAvailable
)
{
PPH_LIST gpuProcessList;
ULONG i;
BOOLEAN querySucceeded;
*FilterAvailable = FALSE;
if (!EtpGpuAdapterList->Count)
return NULL;
gpuProcessList = PhCreateList(128);
querySucceeded = FALSE;
for (i = 0; i < EtpGpuAdapterList->Count; i++)
{
PETP_GPU_ADAPTER gpuAdapter = EtpGpuAdapterList->Items[i];
PULONG processIds = NULL;
PHANDLE processHandles = NULL;
SIZE_T processCount = 0;
NTSTATUS status;
if (!gpuAdapter->CachedAdapterHandle)
continue;
if (EtGpuD3DEnumProcesses)
{
status = EtAdapterEnumProcessList(
gpuAdapter->AdapterLuid,
gpuAdapter->ProcessIdCount,
&processIds,
&processCount
);
if (!NT_SUCCESS(status))
{
if (status == STATUS_PROCEDURE_NOT_FOUND)
{
EtGpuD3DEnumProcesses = FALSE;
}
}
else
{
querySucceeded = TRUE;
gpuAdapter->ProcessIdCount = processCount;
}
}
if (!EtGpuD3DEnumProcesses)
{
status = EtAdapterGetProcessList(
gpuAdapter->AdapterLuid,
gpuAdapter->ProcessHandleCount,
&processHandles,
&processCount
);
if (!NT_SUCCESS(status))
continue;
querySucceeded = TRUE;
gpuAdapter->ProcessHandleCount = processCount;
}
for (SIZE_T j = 0; j < processCount; j++)
{
HANDLE pid;
BOOLEAN found = FALSE;
if (processIds)
{
pid = UlongToHandle(processIds[j]);
}
else
{
PROCESS_BASIC_INFORMATION basicInfo;
if (!NT_SUCCESS(PhGetProcessBasicInformation(processHandles[j], &basicInfo)))
continue;
pid = basicInfo.UniqueProcessId;
}
for (SIZE_T k = 0; k < gpuProcessList->Count; k++)
{
if (gpuProcessList->Items[k] == pid)
{
found = TRUE;
break;
}
}
if (!found)
PhAddItemList(gpuProcessList, pid);
}
if (processIds)
PhFree(processIds);
if (processHandles)
{
for (SIZE_T j = 0; j < processCount; j++)
NtClose(processHandles[j]);
PhFree(processHandles);
}
}
if (!querySucceeded)
{
PhDereferenceObject(gpuProcessList);
return NULL;
}
*FilterAvailable = TRUE;
return gpuProcessList;
}
/**
* Resets the running time deltas for the nodes of an adapter.
*
* \param GpuAdapter The adapter whose node running times restarted.
*/
VOID EtpGpuResetAdapterNodeDeltas(
_In_ PETP_GPU_ADAPTER GpuAdapter
)
{
PLIST_ENTRY listEntry;
if (EtGpuNodesTotalRunningTimeDelta && EtGpuNodesSystemRunningTimeDelta)
{
for (ULONG i = 0; i < GpuAdapter->NodeCount; i++)
{
PhInitializeDelta(&EtGpuNodesTotalRunningTimeDelta[GpuAdapter->FirstNodeIndex + i]);
PhInitializeDelta(&EtGpuNodesSystemRunningTimeDelta[GpuAdapter->FirstNodeIndex + i]);
}
}
listEntry = EtProcessBlockListHead.Flink;
while (listEntry != &EtProcessBlockListHead)
{
PET_PROCESS_BLOCK block = CONTAINING_RECORD(listEntry, ET_PROCESS_BLOCK, ListEntry);
if (block->GpuNodesRunningTimeDelta)
{
ULONG64 adapterRunningTime = 0;
for (ULONG i = 0; i < GpuAdapter->NodeCount; i++)
{
PPH_UINT64_DELTA nodeDelta = &block->GpuNodesRunningTimeDelta[GpuAdapter->FirstNodeIndex + i];
adapterRunningTime += nodeDelta->Value;
PhInitializeDelta(nodeDelta);
}
//
// The total includes the other adapters, remove only the running time of this
// adapter so the next delta doesn't wrap. (dmex)
//
if (block->GpuRunningTimeDelta.Value > adapterRunningTime)
block->GpuRunningTimeDelta.Value -= adapterRunningTime;
else
block->GpuRunningTimeDelta.Value = 0;
}
listEntry = listEntry->Flink;
}
}
/**
* Refreshes the LUID of adapters that were restarted.
*
* \remarks The kernel assigns a new LUID when the adapter is restarted (driver update,
* disable/enable), and statistics queried with the previous LUID fail. The node and segment
* counts are not refreshed, the per-node buffers are allocated during initialization. (#3115)
*/
VOID EtpGpuRefreshAdapterLuids(
VOID
)
{
for (ULONG i = 0; i < EtpGpuAdapterList->Count; i++)
{
PETP_GPU_ADAPTER gpuAdapter = EtpGpuAdapterList->Items[i];
D3DKMT_QUERYSTATISTICS queryStatistics;
D3DKMT_HANDLE adapterHandle;
LUID adapterLuid;
if (!gpuAdapter->DeviceInterface)
continue;
memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS));
queryStatistics.Type = D3DKMT_QUERYSTATISTICS_ADAPTER;
queryStatistics.AdapterLuid = gpuAdapter->AdapterLuid;
if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics)))
continue;
if (!NT_SUCCESS(EtOpenAdapterFromDeviceName(&adapterHandle, &adapterLuid, PhGetString(gpuAdapter->DeviceInterface))))
continue;
if (gpuAdapter->CachedAdapterHandle)
EtCloseAdapterHandle(gpuAdapter->CachedAdapterHandle);
gpuAdapter->CachedAdapterHandle = adapterHandle;
if (!RtlIsEqualLuid(&gpuAdapter->AdapterLuid, &adapterLuid))
{
WriteNoFence64((PLONG64)&gpuAdapter->AdapterLuid, *(PLONG64)&adapterLuid);
EtpGpuResetAdapterNodeDeltas(gpuAdapter);
}
}
}
/**
* Callback function triggered when process provider updates.
* Updates GPU statistics for all running processes and global counters.
*
* \param Parameter Event parameter (e.g., PPH_PROVIDER_UPDATED_EVENT).
* \param Context User-defined context.
*/
_Function_class_(PH_CALLBACK_FUNCTION)
VOID NTAPI EtGpuProcessesUpdatedCallback(
_In_ PVOID Parameter,
_In_ PVOID Context
)
{
PPH_PROVIDER_UPDATED_EVENT updateEvent = Parameter;
ULONG runCount;
FLOAT elapsedTime = 0; // total GPU node elapsed time in micro-seconds
FLOAT tempGpuUsage = 0;
ULONG i;
PLIST_ENTRY listEntry;
FLOAT maxNodeValue = 0;
PET_PROCESS_BLOCK maxNodeBlock = NULL;
if (!updateEvent)
return;
runCount = updateEvent->RunCount;
EtpGpuRefreshAdapterLuids();
if (EtGpuD3DEnabled)
{
FLOAT gpuTotal;
ULONG64 dedicatedTotal;
ULONG64 sharedTotal;
gpuTotal = EtLookupTotalGpuUtilization();
dedicatedTotal = EtLookupTotalGpuDedicated();
sharedTotal = EtLookupTotalGpuShared();
if (gpuTotal > 1)
gpuTotal = 1;
if (gpuTotal > tempGpuUsage)
tempGpuUsage = gpuTotal;
EtGpuNodeUsage = tempGpuUsage;
EtGpuDedicatedUsage = dedicatedTotal;
EtGpuSharedUsage = sharedTotal;
}
else
{
EtpGpuUpdateSystemSegmentInformation();
EtpGpuUpdateSystemNodeInformation();
elapsedTime = (FLOAT)EtGpuSystemRunningTimeDelta;
if (elapsedTime != 0)
{
for (i = 0; i < EtGpuTotalNodeCount; i++)
{
ULONG64 nodeElapsedTime;
FLOAT usage;
nodeElapsedTime = EtGpuNodesSystemRunningTimeDelta[i].Delta;
if (nodeElapsedTime == 0)
continue;
usage = (FLOAT)EtGpuNodesTotalRunningTimeDelta[i].Delta / (FLOAT)nodeElapsedTime;
if (usage > 1)
usage = 1;
if (usage > tempGpuUsage)
tempGpuUsage = usage;
}
}
EtGpuNodeUsage = tempGpuUsage;
}
if (EtGpuSupported && EtpGpuAdapterList->Count)
{
FLOAT powerUsage;
FLOAT temperature;
ULONG64 fanRpm;
powerUsage = 0.0f;
temperature = 0.0f;
fanRpm = 0;
for (i = 0; i < EtpGpuAdapterList->Count; i++)
{
PETP_GPU_ADAPTER gpuAdapter;
D3DKMT_ADAPTER_PERFDATA adapterPerfData;
gpuAdapter = EtpGpuAdapterList->Items[i];
// Use cached adapter handle instead of opening/closing on every update
D3DKMT_HANDLE adapterHandle = gpuAdapter->CachedAdapterHandle;
// If cached handle is invalid (0), try to re-open it
if (!adapterHandle && gpuAdapter->DeviceInterface)
{
if (NT_SUCCESS(EtOpenAdapterFromDeviceName(&adapterHandle, NULL, PhGetString(gpuAdapter->DeviceInterface))))
{
gpuAdapter->CachedAdapterHandle = adapterHandle;
}
else
{
continue;
}
}
if (!adapterHandle)
continue;
memset(&adapterPerfData, 0, sizeof(D3DKMT_ADAPTER_PERFDATA));
if (NT_SUCCESS(EtQueryAdapterInformation(
adapterHandle,
KMTQAITYPE_ADAPTERPERFDATA,
&adapterPerfData,
sizeof(D3DKMT_ADAPTER_PERFDATA)
)))
{
//powerUsage += (((FLOAT)adapterPerfData.Power / 1000) * 100);
//temperature += (((FLOAT)adapterPerfData.Temperature / 1000) * 100);
//
// Use the highest value since we show one graph for all and
// adapters without sensors report zero. (#3115)
// Value * 100 / 1000 is equivalent to dividing by 10. (dmex)
//
powerUsage = max(powerUsage, (FLOAT)adapterPerfData.Power / 10);
temperature = max(temperature, (FLOAT)adapterPerfData.Temperature / 10);
fanRpm = max(fanRpm, (ULONG64)adapterPerfData.FanRPM);
}
else
{
// If query fails, invalidate the cached handle so we try to reopen next time.
// We only own the handle when it was opened from the device interface.
if (gpuAdapter->DeviceInterface)
EtCloseAdapterHandle(adapterHandle);
gpuAdapter->CachedAdapterHandle = 0;
}
}
EtGpuPowerUsage = powerUsage;
EtGpuTemperature = temperature;
EtGpuFanRpm = fanRpm;
//
// Update the limits if we see higher values
//
if (EtGpuPowerUsage > EtGpuPowerUsageLimit)
{
//
// Possibly over-clocked power limit
//
EtGpuPowerUsageLimit = EtGpuPowerUsage;
}
if (EtGpuTemperature > EtGpuTemperatureLimit)
{
//
// Damn that card is hawt
//
EtGpuTemperatureLimit = EtGpuTemperature;
}
if (EtGpuFanRpm > EtGpuFanRpmLimit)
{
//
// Fan go brrrrrr
//
EtGpuFanRpmLimit = EtGpuFanRpm;
}
}
// Update per-process statistics.
// Note: no lock is needed because we only ever modify the list on this same thread.
PPH_LIST gpuProcessList = NULL;
BOOLEAN gpuProcessFilterAvailable = FALSE;
if (EtGpuEnabled)
{
gpuProcessList = EtpBuildGpuProcessList(&gpuProcessFilterAvailable);
}
listEntry = EtProcessBlockListHead.Flink;
while (listEntry != &EtProcessBlockListHead)
{
PET_PROCESS_BLOCK block;
BOOLEAN found = TRUE;
block = CONTAINING_RECORD(listEntry, ET_PROCESS_BLOCK, ListEntry);
if (!block->GpuNodesUtilization)
{
ULONG gpuColumnCount = EtGetGpuAdapterCount() + EtGpuTotalNodeCount;
block->GpuNodesRunningTimeDelta = PhAllocateZero(sizeof(PH_UINT64_DELTA) * EtGpuTotalNodeCount);
block->GpuNodesUtilization = PhAllocateZero(sizeof(FLOAT) * EtGpuTotalNodeCount);
block->GpuNodesTextCacheValid = PhAllocateZero(sizeof(BOOLEAN) * gpuColumnCount);
block->GpuNodesTextCacheLength = PhAllocateZero(sizeof(SIZE_T) * gpuColumnCount);
block->GpuNodesTextCache = PhAllocateZero(sizeof(WCHAR) * 64 * gpuColumnCount);
}
if (FlagOn(block->ProcessItem->State, PH_PROCESS_ITEM_REMOVED))
{
listEntry = listEntry->Flink;
continue;
}
if (gpuProcessFilterAvailable)
{
found = FALSE;
if (gpuProcessList)
{
for (SIZE_T j = 0; j < gpuProcessList->Count; j++)
{
if (gpuProcessList->Items[j] == block->ProcessItem->ProcessId)
{
found = TRUE;
break;
}
}
}
}
if (EtGpuD3DEnabled)
{
ULONG64 sharedUsage;
ULONG64 dedicatedUsage;
ULONG64 commitUsage;
ULONG64 dedicatedCommitted;
ULONG64 sharedCommitted;
if (found)
{
block->GpuNodeUtilization = EtLookupProcessGpuUtilization(block->ProcessItem->ProcessId);
for (i = 0; i < EtpGpuAdapterList->Count; i++)
{
PETP_GPU_ADAPTER gpuAdapter = EtpGpuAdapterList->Items[i];
for (ULONG j = 0; j < gpuAdapter->NodeCount; j++)
{
ULONG nodeIndex = gpuAdapter->FirstNodeIndex + j;
block->GpuNodesUtilization[nodeIndex] = EtLookupProcessGpuEngineUtilization(
block->ProcessItem->ProcessId,
gpuAdapter->AdapterLuid,
j
);
}
}
if (EtLookupProcessGpuMemoryCounters(
block->ProcessItem->ProcessId,
&sharedUsage,
&dedicatedUsage,
&commitUsage,
&dedicatedCommitted,
&sharedCommitted
))
{
block->GpuSharedUsage = sharedUsage;
block->GpuDedicatedUsage = dedicatedUsage;
block->GpuCommitUsage = commitUsage;
block->GpuDedicatedCommitted = dedicatedCommitted;
block->GpuSharedCommitted = sharedCommitted;
}
else
{
block->GpuSharedUsage = 0;
block->GpuDedicatedUsage = 0;
block->GpuCommitUsage = 0;
block->GpuDedicatedCommitted = 0;
block->GpuSharedCommitted = 0;
}
EtpGpuUpdateProcessAdapterStatistics(block);
}
else
{
// Process doesn't use GPU, zero out stats
block->GpuNodeUtilization = 0;
memset(block->GpuNodesUtilization, 0, sizeof(FLOAT) * EtGpuTotalNodeCount);
block->GpuSharedUsage = 0;
block->GpuDedicatedUsage = 0;
block->GpuCommitUsage = 0;
block->GpuDedicatedCommitted = 0;
block->GpuSharedCommitted = 0;
EtpZeroProcessGpuAdapterStatistics(block);
}
if (runCount != 0)
{
block->GpuCurrentUsage = block->GpuNodeUtilization;
block->GpuCurrentMemUsage = (ULONG)(block->GpuDedicatedUsage / PAGE_SIZE);
block->GpuCurrentMemSharedUsage = (ULONG)(block->GpuSharedUsage / PAGE_SIZE);
block->GpuCurrentCommitUsage = (ULONG)(block->GpuCommitUsage / PAGE_SIZE);
ET_CIRCULAR_BUFFER_ADD_FLOAT(&block->GpuHistory, block->GpuCurrentUsage);
ET_CIRCULAR_BUFFER_ADD_ULONG(&block->GpuMemoryHistory, block->GpuCurrentMemUsage);
ET_CIRCULAR_BUFFER_ADD_ULONG(&block->GpuMemorySharedHistory, block->GpuCurrentMemSharedUsage);
ET_CIRCULAR_BUFFER_ADD_ULONG(&block->GpuCommittedHistory, block->GpuCurrentCommitUsage);
}
}
else
{
// Only query GPU stats if process uses GPU or if we don't have a GPU process list
if (found)
{
EtpGpuUpdateProcessSegmentInformation(block);
EtpGpuUpdateProcessNodeInformation(block);
EtpGpuUpdateProcessAdapterStatistics(block);
}
else
{
// Process doesn't use GPU, zero out stats
block->GpuDedicatedUsage = 0;
block->GpuSharedUsage = 0;
block->GpuCommitUsage = 0;
block->GpuDedicatedCommitted = 0;
block->GpuSharedCommitted = 0;
block->GpuNodeUtilization = 0;
memset(block->GpuNodesUtilization, 0, sizeof(FLOAT) * EtGpuTotalNodeCount);
EtpZeroProcessGpuAdapterStatistics(block);
}
if (elapsedTime != 0)
{
if (found)
{
block->GpuNodeUtilization = (FLOAT)block->GpuRunningTimeDelta.Delta / elapsedTime;
for (i = 0; i < EtGpuTotalNodeCount; i++)
{
if (EtGpuNodesSystemRunningTimeDelta[i].Delta != 0)
{
block->GpuNodesUtilization[i] =
(FLOAT)block->GpuNodesRunningTimeDelta[i].Delta /
(FLOAT)EtGpuNodesSystemRunningTimeDelta[i].Delta;
if (block->GpuNodesUtilization[i] > 1)
block->GpuNodesUtilization[i] = 1;
}
else
{
block->GpuNodesUtilization[i] = 0;
}
}
if (block->GpuNodeUtilization > 1)
block->GpuNodeUtilization = 1;
}
if (runCount != 0)
{
block->GpuCurrentUsage = block->GpuNodeUtilization;
block->GpuCurrentMemUsage = (ULONG)(block->GpuDedicatedUsage / PAGE_SIZE);
block->GpuCurrentMemSharedUsage = (ULONG)(block->GpuSharedUsage / PAGE_SIZE);
block->GpuCurrentCommitUsage = (ULONG)(block->GpuCommitUsage / PAGE_SIZE);
ET_CIRCULAR_BUFFER_ADD_FLOAT(&block->GpuHistory, block->GpuCurrentUsage);
ET_CIRCULAR_BUFFER_ADD_ULONG(&block->GpuMemoryHistory, block->GpuCurrentMemUsage);
ET_CIRCULAR_BUFFER_ADD_ULONG(&block->GpuMemorySharedHistory, block->GpuCurrentMemSharedUsage);
ET_CIRCULAR_BUFFER_ADD_ULONG(&block->GpuCommittedHistory, block->GpuCurrentCommitUsage);
}
}
}
if (runCount != 0)
{
ET_UPDATE_PROCESS_STATISTICS_MINMAX(block->GpuDedicatedUsageMin, block->GpuDedicatedUsageMax, block->GpuDedicatedUsageDiff, block->GpuDedicatedUsage);
ET_UPDATE_PROCESS_STATISTICS_MINMAX(block->GpuSharedUsageMin, block->GpuSharedUsageMax, block->GpuSharedUsageDiff, block->GpuSharedUsage);
ET_UPDATE_PROCESS_STATISTICS_MINMAX(block->GpuCommitUsageMin, block->GpuCommitUsageMax, block->GpuCommitUsageDiff, block->GpuCommitUsage);
ET_UPDATE_PROCESS_STATISTICS_MINMAX(block->GpuTotalUsageMin, block->GpuTotalUsageMax, block->GpuTotalUsageDiff, block->GpuDedicatedUsage + block->GpuSharedUsage + block->GpuCommitUsage);
}
if (maxNodeValue < block->GpuNodeUtilization)
{
maxNodeValue = block->GpuNodeUtilization;
maxNodeBlock = block;
}
listEntry = listEntry->Flink;
}
// Clean up GPU process list if we built one
if (gpuProcessList)
{
PhDereferenceObject(gpuProcessList);
}
// Update history buffers.
if (runCount != 0)
{
ET_CIRCULAR_BUFFER_ADD_FLOAT(&EtGpuNodeHistory, EtGpuNodeUsage);
ET_CIRCULAR_BUFFER_ADD_ULONG64(&EtGpuDedicatedHistory, EtGpuDedicatedUsage);
ET_CIRCULAR_BUFFER_ADD_ULONG64(&EtGpuSharedHistory, EtGpuSharedUsage);
if (EtGpuSupported)
{
ET_CIRCULAR_BUFFER_ADD_FLOAT(&EtGpuPowerUsageHistory, EtGpuPowerUsage);
ET_CIRCULAR_BUFFER_ADD_FLOAT(&EtGpuTemperatureHistory, EtGpuTemperature);
ET_CIRCULAR_BUFFER_ADD_ULONG64(&EtGpuFanRpmHistory, EtGpuFanRpm);
}
if (EtGpuD3DEnabled)
{
for (i = 0; i < EtGpuTotalNodeCount; i++)
{
FLOAT usage;
usage = EtLookupTotalGpuEngineUtilization(i);
if (usage > 1)
usage = 1;
ET_CIRCULAR_BUFFER_ADD_FLOAT(&EtGpuNodesHistory[i], usage);
}
}
else
{
if (elapsedTime != 0)
{
for (i = 0; i < EtGpuTotalNodeCount; i++)
{
FLOAT usage;
if (EtGpuNodesSystemRunningTimeDelta[i].Delta != 0)
usage = (FLOAT)EtGpuNodesTotalRunningTimeDelta[i].Delta / (FLOAT)EtGpuNodesSystemRunningTimeDelta[i].Delta;
else
usage = 0;
if (usage > 1)
usage = 1;
ET_CIRCULAR_BUFFER_ADD_FLOAT(&EtGpuNodesHistory[i], usage);
}
}
else
{
for (i = 0; i < EtGpuTotalNodeCount; i++)
{
ET_CIRCULAR_BUFFER_ADD_FLOAT(&EtGpuNodesHistory[i], 0);
}
}
}
if (maxNodeBlock)
{
ET_CIRCULAR_BUFFER_ADD_ULONG(&EtMaxGpuNodeHistory, HandleToUlong(maxNodeBlock->ProcessItem->ProcessId));
ET_CIRCULAR_BUFFER_ADD_FLOAT(&EtMaxGpuNodeUsageHistory, maxNodeBlock->GpuNodeUtilization);
PhReferenceProcessRecordForStatistics(maxNodeBlock->ProcessItem->Record);
}
else
{
ET_CIRCULAR_BUFFER_ADD_ULONG(&EtMaxGpuNodeHistory, 0);
ET_CIRCULAR_BUFFER_ADD_FLOAT(&EtMaxGpuNodeUsageHistory, 0);
}
}
}
ULONG EtGetGpuAdapterCount(
VOID
)
{
return EtpGpuAdapterList->Count;
}
ULONG EtGetGpuAdapterIndexFromNodeIndex(
_In_ ULONG NodeIndex
)
{
ULONG i;
PETP_GPU_ADAPTER gpuAdapter;
for (i = 0; i < EtpGpuAdapterList->Count; i++)
{
gpuAdapter = EtpGpuAdapterList->Items[i];
if (NodeIndex >= gpuAdapter->FirstNodeIndex && NodeIndex < gpuAdapter->FirstNodeIndex + gpuAdapter->NodeCount)
return i;
}
return ULONG_MAX;
}
PPH_STRING EtGetGpuAdapterNodeDescription(
_In_ ULONG Index,
_In_ ULONG NodeIndex
)
{
PETP_GPU_ADAPTER gpuAdapter;
if (Index >= EtpGpuAdapterList->Count)
return NULL;
gpuAdapter = EtpGpuAdapterList->Items[Index];
if (!gpuAdapter->NodeNameList)
return NULL;
return gpuAdapter->NodeNameList->Items[NodeIndex - gpuAdapter->FirstNodeIndex];
}
PPH_STRING EtGetGpuAdapterDescription(
_In_ ULONG Index
)
{
PPH_STRING description;
if (Index >= EtpGpuAdapterList->Count)
return NULL;
description = ((PETP_GPU_ADAPTER)EtpGpuAdapterList->Items[Index])->Description;
if (description)
{
PhReferenceObject(description);
return description;
}
else
{
return NULL;
}
}
VOID EtQueryProcessGpuStatistics(
_In_ HANDLE ProcessHandle,
_Out_ PET_PROCESS_GPU_STATISTICS Statistics
)
{
ULONG i;
ULONG j;
PETP_GPU_ADAPTER gpuAdapter;
D3DKMT_QUERYSTATISTICS queryStatistics;
memset(Statistics, 0, sizeof(ET_PROCESS_GPU_STATISTICS));
for (i = 0; i < EtpGpuAdapterList->Count; i++)
{
gpuAdapter = EtpGpuAdapterList->Items[i];
Statistics->SegmentCount += gpuAdapter->SegmentCount;
Statistics->NodeCount += gpuAdapter->NodeCount;
for (j = 0; j < gpuAdapter->SegmentCount; j++)
{
memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS));
queryStatistics.Type = D3DKMT_QUERYSTATISTICS_PROCESS_SEGMENT;
queryStatistics.AdapterLuid = gpuAdapter->AdapterLuid;
queryStatistics.hProcess = ProcessHandle;
queryStatistics.QueryProcessSegment.SegmentId = j;
if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics)))
{
ULONG64 bytesCommitted;
if (EtWindowsVersion >= WINDOWS_8)
bytesCommitted = queryStatistics.QueryResult.ProcessSegmentInformation.BytesCommitted;
else
bytesCommitted = (ULONG)queryStatistics.QueryResult.ProcessSegmentInformation.BytesCommitted;
if (RtlCheckBit(&gpuAdapter->ApertureBitMap, j))
Statistics->SharedCommitted += bytesCommitted;
else
Statistics->DedicatedCommitted += bytesCommitted;
}
}
for (j = 0; j < gpuAdapter->NodeCount; j++)
{
memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS));
queryStatistics.Type = D3DKMT_QUERYSTATISTICS_PROCESS_NODE;
queryStatistics.AdapterLuid = gpuAdapter->AdapterLuid;
queryStatistics.hProcess = ProcessHandle;
queryStatistics.QueryProcessNode.NodeId = j;
if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics)))
{
Statistics->RunningTime += queryStatistics.QueryResult.ProcessNodeInformation.RunningTime.QuadPart;
Statistics->ContextSwitches += queryStatistics.QueryResult.ProcessNodeInformation.ContextSwitch;
}
}
memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS));
queryStatistics.Type = D3DKMT_QUERYSTATISTICS_PROCESS;
queryStatistics.AdapterLuid = gpuAdapter->AdapterLuid;
queryStatistics.hProcess = ProcessHandle;
if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics)))
{
Statistics->BytesAllocated += queryStatistics.QueryResult.ProcessInformation.SystemMemory.BytesAllocated;
Statistics->BytesReserved += queryStatistics.QueryResult.ProcessInformation.SystemMemory.BytesReserved;
Statistics->WriteCombinedBytesAllocated += queryStatistics.QueryResult.ProcessInformation.SystemMemory.WriteCombinedBytesAllocated;
Statistics->WriteCombinedBytesReserved += queryStatistics.QueryResult.ProcessInformation.SystemMemory.WriteCombinedBytesReserved;
Statistics->CachedBytesAllocated += queryStatistics.QueryResult.ProcessInformation.SystemMemory.CachedBytesAllocated;
Statistics->CachedBytesReserved += queryStatistics.QueryResult.ProcessInformation.SystemMemory.CachedBytesReserved;
Statistics->SectionBytesAllocated += queryStatistics.QueryResult.ProcessInformation.SystemMemory.SectionBytesAllocated;
Statistics->SectionBytesReserved += queryStatistics.QueryResult.ProcessInformation.SystemMemory.SectionBytesReserved;
}
}
}
/**
* Queries the D3D client hint (e.g., DX9, DX10, DX11, Vulkan) for a specific process and adapter.
*
* \param AdapterLuid The LUID of the GPU adapter.
* \param ProcessHandle A handle to the process.
* \return The D3DKMT_CLIENTHINT value representing the client hint, or D3DKMT_CLIENTHINT_UNKNOWN on failure.
*/
D3DKMT_CLIENTHINT EtQueryProcessGpuClientHint(
_In_ LUID AdapterLuid,
_In_ HANDLE ProcessHandle
)
{
D3DKMT_QUERYSTATISTICS queryStatistics;
memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS));
queryStatistics.Type = D3DKMT_QUERYSTATISTICS_PROCESS_ADAPTER;
queryStatistics.AdapterLuid = AdapterLuid;
queryStatistics.hProcess = ProcessHandle;
if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics)))
{
return queryStatistics.QueryResult.ProcessAdapterInformation.ClientHint;
}
return D3DKMT_CLIENTHINT_UNKNOWN;
}