BOOLEAN EtpInitializeD3DStatistics( VOID ) { LOGICAL result; HDEVINFO deviceInfoSet; ULONG memberIndex; SP_DEVICE_INTERFACE_DATA deviceInterfaceData; PSP_DEVICE_INTERFACE_DETAIL_DATA detailData; SP_DEVINFO_DATA deviceInfoData; ULONG detailDataSize; D3DKMT_OPENADAPTERFROMDEVICENAME openAdapterFromDeviceName; D3DKMT_QUERYSTATISTICS queryStatistics; deviceInfoSet = SetupDiGetClassDevsW_I(&GUID_DISPLAY_DEVICE_ARRIVAL_I, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT); if (!deviceInfoSet) return FALSE; memberIndex = 0; deviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); while (SetupDiEnumDeviceInterfaces_I(deviceInfoSet, NULL, &GUID_DISPLAY_DEVICE_ARRIVAL_I, memberIndex, &deviceInterfaceData)) { detailDataSize = 0x100; detailData = PhAllocate(detailDataSize); detailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); deviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA); if (!(result = SetupDiGetDeviceInterfaceDetailW_I(deviceInfoSet, &deviceInterfaceData, detailData, detailDataSize, &detailDataSize, &deviceInfoData)) && GetLastError() == ERROR_INSUFFICIENT_BUFFER) { PhFree(detailData); detailData = PhAllocate(detailDataSize); if (detailDataSize >= sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA)) detailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); result = SetupDiGetDeviceInterfaceDetailW_I(deviceInfoSet, &deviceInterfaceData, detailData, detailDataSize, &detailDataSize, &deviceInfoData); } if (result) { openAdapterFromDeviceName.pDeviceName = detailData->DevicePath; if (NT_SUCCESS(D3DKMTOpenAdapterFromDeviceName_I(&openAdapterFromDeviceName))) { memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS)); queryStatistics.Type = D3DKMT_QUERYSTATISTICS_ADAPTER; queryStatistics.AdapterLuid = openAdapterFromDeviceName.AdapterLuid; if (NT_SUCCESS(D3DKMTQueryStatistics_I(&queryStatistics))) { PETP_GPU_ADAPTER gpuAdapter; ULONG i; gpuAdapter = EtpAllocateGpuAdapter(queryStatistics.QueryResult.AdapterInformation.NbSegments); gpuAdapter->AdapterLuid = openAdapterFromDeviceName.AdapterLuid; gpuAdapter->Description = EtpQueryDeviceDescription(deviceInfoSet, &deviceInfoData); gpuAdapter->NodeCount = queryStatistics.QueryResult.AdapterInformation.NodeCount; gpuAdapter->SegmentCount = queryStatistics.QueryResult.AdapterInformation.NbSegments; RtlInitializeBitMap(&gpuAdapter->ApertureBitMap, gpuAdapter->ApertureBitMapBuffer, queryStatistics.QueryResult.AdapterInformation.NbSegments); PhAddItemList(EtpGpuAdapterList, gpuAdapter); EtGpuTotalNodeCount += gpuAdapter->NodeCount; EtGpuTotalSegmentCount += gpuAdapter->SegmentCount; gpuAdapter->FirstNodeIndex = EtGpuNextNodeIndex; EtGpuNextNodeIndex += gpuAdapter->NodeCount; for (i = 0; i < gpuAdapter->SegmentCount; i++) { memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS)); queryStatistics.Type = D3DKMT_QUERYSTATISTICS_SEGMENT; queryStatistics.AdapterLuid = gpuAdapter->AdapterLuid; queryStatistics.QuerySegment.SegmentId = i; if (NT_SUCCESS(D3DKMTQueryStatistics_I(&queryStatistics))) { ULONG64 commitLimit; ULONG aperture; commitLimit = queryStatistics.QueryResult.SegmentInformation.CommitLimit; aperture = queryStatistics.QueryResult.SegmentInformation.Aperture; if (aperture) EtGpuSharedLimit += commitLimit; else EtGpuDedicatedLimit += commitLimit; if (aperture) RtlSetBits(&gpuAdapter->ApertureBitMap, i, 1); } } } } } PhFree(detailData); memberIndex++; } SetupDiDestroyDeviceInfoList_I(deviceInfoSet); EtGpuNodeBitMapBuffer = PhAllocate(BYTES_NEEDED_FOR_BITS(EtGpuTotalNodeCount)); RtlInitializeBitMap(&EtGpuNodeBitMap, EtGpuNodeBitMapBuffer, EtGpuTotalNodeCount); RtlSetBits(&EtGpuNodeBitMap, 0, 1); EtGpuNodeBitMapBitsSet = 1; return TRUE; }
BOOLEAN EtpInitializeD3DStatistics( VOID ) { PWSTR deviceInterfaceList; ULONG deviceInterfaceListLength = 0; PWSTR deviceInterface; D3DKMT_OPENADAPTERFROMDEVICENAME openAdapterFromDeviceName; D3DKMT_QUERYSTATISTICS queryStatistics; D3DKMT_CLOSEADAPTER closeAdapter; if (CM_Get_Device_Interface_List_Size( &deviceInterfaceListLength, (PGUID)&GUID_DISPLAY_DEVICE_ARRIVAL, NULL, CM_GET_DEVICE_INTERFACE_LIST_PRESENT ) != CR_SUCCESS) { return FALSE; } deviceInterfaceList = PhAllocate(deviceInterfaceListLength * sizeof(WCHAR)); memset(deviceInterfaceList, 0, deviceInterfaceListLength * sizeof(WCHAR)); if (CM_Get_Device_Interface_List( (PGUID)&GUID_DISPLAY_DEVICE_ARRIVAL, NULL, deviceInterfaceList, deviceInterfaceListLength, CM_GET_DEVICE_INTERFACE_LIST_PRESENT ) != CR_SUCCESS) { PhFree(deviceInterfaceList); return FALSE; } for (deviceInterface = deviceInterfaceList; *deviceInterface; deviceInterface += PhCountStringZ(deviceInterface) + 1) { memset(&openAdapterFromDeviceName, 0, sizeof(D3DKMT_OPENADAPTERFROMDEVICENAME)); openAdapterFromDeviceName.pDeviceName = deviceInterface; if (NT_SUCCESS(D3DKMTOpenAdapterFromDeviceName(&openAdapterFromDeviceName))) { memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS)); queryStatistics.Type = D3DKMT_QUERYSTATISTICS_ADAPTER; queryStatistics.AdapterLuid = openAdapterFromDeviceName.AdapterLuid; if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics))) { PETP_GPU_ADAPTER gpuAdapter; ULONG i; gpuAdapter = EtpAllocateGpuAdapter(queryStatistics.QueryResult.AdapterInformation.NbSegments); gpuAdapter->AdapterLuid = openAdapterFromDeviceName.AdapterLuid; gpuAdapter->Description = EtpQueryDeviceDescription(deviceInterface); gpuAdapter->NodeCount = queryStatistics.QueryResult.AdapterInformation.NodeCount; gpuAdapter->SegmentCount = queryStatistics.QueryResult.AdapterInformation.NbSegments; RtlInitializeBitMap(&gpuAdapter->ApertureBitMap, gpuAdapter->ApertureBitMapBuffer, queryStatistics.QueryResult.AdapterInformation.NbSegments); PhAddItemList(EtpGpuAdapterList, gpuAdapter); EtGpuTotalNodeCount += gpuAdapter->NodeCount; EtGpuTotalSegmentCount += gpuAdapter->SegmentCount; gpuAdapter->FirstNodeIndex = EtGpuNextNodeIndex; EtGpuNextNodeIndex += gpuAdapter->NodeCount; for (i = 0; i < gpuAdapter->SegmentCount; i++) { memset(&queryStatistics, 0, sizeof(D3DKMT_QUERYSTATISTICS)); queryStatistics.Type = D3DKMT_QUERYSTATISTICS_SEGMENT; queryStatistics.AdapterLuid = gpuAdapter->AdapterLuid; queryStatistics.QuerySegment.SegmentId = i; if (NT_SUCCESS(D3DKMTQueryStatistics(&queryStatistics))) { ULONG64 commitLimit; ULONG aperture; if (WindowsVersion >= WINDOWS_8) { commitLimit = queryStatistics.QueryResult.SegmentInformation.CommitLimit; aperture = queryStatistics.QueryResult.SegmentInformation.Aperture; } else { commitLimit = queryStatistics.QueryResult.SegmentInformationV1.CommitLimit; aperture = queryStatistics.QueryResult.SegmentInformationV1.Aperture; } if (aperture) EtGpuSharedLimit += commitLimit; else EtGpuDedicatedLimit += commitLimit; if (aperture) RtlSetBits(&gpuAdapter->ApertureBitMap, i, 1); } } } memset(&closeAdapter, 0, sizeof(D3DKMT_CLOSEADAPTER)); closeAdapter.hAdapter = openAdapterFromDeviceName.hAdapter; D3DKMTCloseAdapter(&closeAdapter); } } PhFree(deviceInterfaceList); EtGpuNodeBitMapBuffer = PhAllocate(BYTES_NEEDED_FOR_BITS(EtGpuTotalNodeCount)); RtlInitializeBitMap(&EtGpuNodeBitMap, EtGpuNodeBitMapBuffer, EtGpuTotalNodeCount); RtlSetBits(&EtGpuNodeBitMap, 0, 1); EtGpuNodeBitMapBitsSet = 1; return TRUE; }