mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-10-18 02:29:04 +02:00
gpu: add vendor-neutral sensor metrics from dxgkrnl (#2571)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
d06f168926
commit
15f290c8c8
@@ -36,6 +36,20 @@ const (
|
||||
KMTQAITYPE_ADAPTERREGISTRYINFO = 8
|
||||
// KMTQAITYPE_PHYSICALADAPTERDEVICEIDS pPrivateDriverData points to a D3DKMT_QUERY_DEVICE_IDS structure that specifies the device ID(s) of the physical adapters. Supported starting with Windows 10 (WDDM 2.0).
|
||||
KMTQAITYPE_PHYSICALADAPTERDEVICEIDS = 31
|
||||
// KMTQAITYPE_DRIVERVERSION pPrivateDriverData points to a D3DKMT_DRIVERVERSION value that indicates the WDDM version of the display miniport driver.
|
||||
KMTQAITYPE_DRIVERVERSION = 13
|
||||
// KMTQAITYPE_PHYSICALADAPTERCOUNT pPrivateDriverData points to a UINT that receives the number of physical adapters in the LDA chain. Supported starting with Windows 10 (WDDM 2.0).
|
||||
KMTQAITYPE_PHYSICALADAPTERCOUNT = 30
|
||||
// KMTQAITYPE_KMD_DRIVER_VERSION pPrivateDriverData points to a LARGE_INTEGER that receives the kernel mode driver version. Supported starting with WDDM 2.2.
|
||||
KMTQAITYPE_KMD_DRIVER_VERSION = 49
|
||||
// KMTQAITYPE_NODEPERFDATA pPrivateDriverData points to a D3DKMT_NODE_PERFDATA structure. Supported starting with WDDM 2.4.
|
||||
KMTQAITYPE_NODEPERFDATA = 61
|
||||
// KMTQAITYPE_ADAPTERPERFDATA pPrivateDriverData points to a D3DKMT_ADAPTER_PERFDATA structure. Supported starting with WDDM 2.4.
|
||||
KMTQAITYPE_ADAPTERPERFDATA = 62
|
||||
// KMTQAITYPE_ADAPTERPERFDATA_CAPS pPrivateDriverData points to a D3DKMT_ADAPTER_PERFDATACAPS structure. Supported starting with WDDM 2.4.
|
||||
KMTQAITYPE_ADAPTERPERFDATA_CAPS = 63
|
||||
// KMTQUITYPE_GPUVERSION pPrivateDriverData points to a D3DKMT_GPUVERSION structure. Supported starting with WDDM 2.4.
|
||||
KMTQUITYPE_GPUVERSION = 64
|
||||
)
|
||||
|
||||
var ErrNoGPUDevices = errors.New("no GPU devices found")
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
// Copyright The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build windows
|
||||
|
||||
package gdi32
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
// OpenAdapterFromLUID opens a handle to the adapter with the given LUID.
|
||||
// The handle must be released with CloseAdapter.
|
||||
func OpenAdapterFromLUID(luid windows.LUID) (D3DKMT_HANDLE, error) {
|
||||
openAdapter := D3DKMT_OPENADAPTERFROMLUID{
|
||||
AdapterLUID: luid,
|
||||
}
|
||||
|
||||
if err := D3DKMTOpenAdapterFromLuid(&openAdapter); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return openAdapter.HAdapter, nil
|
||||
}
|
||||
|
||||
// CloseAdapter closes a handle opened by OpenAdapterFromLUID.
|
||||
func CloseAdapter(hAdapter D3DKMT_HANDLE) error {
|
||||
return D3DKMTCloseAdapter(&D3DKMT_CLOSEADAPTER{
|
||||
HAdapter: hAdapter,
|
||||
})
|
||||
}
|
||||
|
||||
// queryAdapterInfo calls D3DKMTQueryAdapterInfo with data as private driver data.
|
||||
// data is used as input and output.
|
||||
func queryAdapterInfo[T any](hAdapter D3DKMT_HANDLE, queryType int32, data *T) error {
|
||||
query := D3DKMT_QUERYADAPTERINFO{
|
||||
hAdapter: hAdapter,
|
||||
queryType: queryType,
|
||||
pPrivateDriverData: unsafe.Pointer(data),
|
||||
privateDriverDataSize: uint32(unsafe.Sizeof(*data)),
|
||||
}
|
||||
|
||||
return D3DKMTQueryAdapterInfo(&query)
|
||||
}
|
||||
|
||||
// QueryPhysicalAdapterCount returns the number of physical adapters in the LDA chain of the adapter.
|
||||
func QueryPhysicalAdapterCount(hAdapter D3DKMT_HANDLE) (uint32, error) {
|
||||
var count uint32
|
||||
|
||||
if err := queryAdapterInfo(hAdapter, KMTQAITYPE_PHYSICALADAPTERCOUNT, &count); err != nil {
|
||||
return 0, fmt.Errorf("physical adapter count: %w", err)
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
// QueryWDDMVersion returns the WDDM version of the display miniport driver
|
||||
// as D3DKMT_DRIVERVERSION value, e.g. 3200 for WDDM 3.2.
|
||||
func QueryWDDMVersion(hAdapter D3DKMT_HANDLE) (uint32, error) {
|
||||
var version int32
|
||||
|
||||
if err := queryAdapterInfo(hAdapter, KMTQAITYPE_DRIVERVERSION, &version); err != nil {
|
||||
return 0, fmt.Errorf("WDDM version: %w", err)
|
||||
}
|
||||
|
||||
return uint32(version), nil
|
||||
}
|
||||
|
||||
// QueryKMDDriverVersion returns the kernel mode driver version, packed as four 16-bit parts.
|
||||
func QueryKMDDriverVersion(hAdapter D3DKMT_HANDLE) (uint64, error) {
|
||||
var version uint64
|
||||
|
||||
if err := queryAdapterInfo(hAdapter, KMTQAITYPE_KMD_DRIVER_VERSION, &version); err != nil {
|
||||
return 0, fmt.Errorf("KMD driver version: %w", err)
|
||||
}
|
||||
|
||||
return version, nil
|
||||
}
|
||||
|
||||
// QueryAdapterPerfData returns the current performance data of a physical adapter.
|
||||
func QueryAdapterPerfData(hAdapter D3DKMT_HANDLE, physicalAdapterIndex uint32) (D3DKMT_ADAPTER_PERFDATA, error) {
|
||||
data := D3DKMT_ADAPTER_PERFDATA{
|
||||
PhysicalAdapterIndex: physicalAdapterIndex,
|
||||
}
|
||||
|
||||
if err := queryAdapterInfo(hAdapter, KMTQAITYPE_ADAPTERPERFDATA, &data); err != nil {
|
||||
return data, fmt.Errorf("adapter perf data: %w", err)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// QueryAdapterPerfDataCaps returns the performance data capabilities of a physical adapter.
|
||||
func QueryAdapterPerfDataCaps(hAdapter D3DKMT_HANDLE, physicalAdapterIndex uint32) (D3DKMT_ADAPTER_PERFDATACAPS, error) {
|
||||
data := D3DKMT_ADAPTER_PERFDATACAPS{
|
||||
PhysicalAdapterIndex: physicalAdapterIndex,
|
||||
}
|
||||
|
||||
if err := queryAdapterInfo(hAdapter, KMTQAITYPE_ADAPTERPERFDATA_CAPS, &data); err != nil {
|
||||
return data, fmt.Errorf("adapter perf data caps: %w", err)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// QueryNodePerfData returns the current performance data of an engine (node) of a physical adapter.
|
||||
// The node ordinal matches the eng_N part of the GPU Engine performance counter instances.
|
||||
// Querying a node ordinal past the last node returns an error.
|
||||
func QueryNodePerfData(hAdapter D3DKMT_HANDLE, physicalAdapterIndex, nodeOrdinal uint32) (D3DKMT_NODE_PERFDATA, error) {
|
||||
data := D3DKMT_NODE_PERFDATA{
|
||||
NodeOrdinal: nodeOrdinal,
|
||||
PhysicalAdapterIndex: physicalAdapterIndex,
|
||||
}
|
||||
|
||||
if err := queryAdapterInfo(hAdapter, KMTQAITYPE_NODEPERFDATA, &data); err != nil {
|
||||
return data, fmt.Errorf("node %d perf data: %w", nodeOrdinal, err)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// QueryGPUVersion returns the BIOS version and architecture of a physical adapter.
|
||||
func QueryGPUVersion(hAdapter D3DKMT_HANDLE, physicalAdapterIndex uint32) (D3DKMT_GPUVERSION, error) {
|
||||
data := D3DKMT_GPUVERSION{
|
||||
PhysicalAdapterIndex: physicalAdapterIndex,
|
||||
}
|
||||
|
||||
if err := queryAdapterInfo(hAdapter, KMTQUITYPE_GPUVERSION, &data); err != nil {
|
||||
return data, fmt.Errorf("GPU version: %w", err)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// FormatKMDDriverVersion formats a version returned by QueryKMDDriverVersion as a.b.c.d.
|
||||
func FormatKMDDriverVersion(version uint64) string {
|
||||
return fmt.Sprintf("%d.%d.%d.%d",
|
||||
uint16(version>>48),
|
||||
uint16(version>>32),
|
||||
uint16(version>>16),
|
||||
uint16(version),
|
||||
)
|
||||
}
|
||||
|
||||
// FormatWDDMVersion formats a version returned by QueryWDDMVersion as major.minor, e.g. 3200 as 3.2.
|
||||
func FormatWDDMVersion(version uint32) string {
|
||||
return fmt.Sprintf("%d.%d", version/1000, version%1000/100)
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
// Copyright The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build windows
|
||||
|
||||
package gdi32_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/prometheus-community/windows_exporter/internal/headers/gdi32"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/sys/windows"
|
||||
)
|
||||
|
||||
// TestPerfDataQueries calls the perf data queries on every adapter.
|
||||
// The queries depend on WDDM 2.4+ and driver support, so failures are only logged.
|
||||
func TestPerfDataQueries(t *testing.T) {
|
||||
devices, err := gdi32.GetGPUDevices()
|
||||
if len(devices) == 0 {
|
||||
t.Skipf("no GPU devices found: %v", err)
|
||||
}
|
||||
|
||||
for _, device := range devices {
|
||||
t.Run(device.AdapterString, func(t *testing.T) {
|
||||
hAdapter, err := gdi32.OpenAdapterFromLUID(device.LUID)
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, gdi32.CloseAdapter(hAdapter))
|
||||
})
|
||||
|
||||
count, err := gdi32.QueryPhysicalAdapterCount(hAdapter)
|
||||
t.Logf("physical adapter count: %d, err: %v", count, err)
|
||||
|
||||
wddmVersion, err := gdi32.QueryWDDMVersion(hAdapter)
|
||||
t.Logf("WDDM version: %s, err: %v", gdi32.FormatWDDMVersion(wddmVersion), err)
|
||||
|
||||
driverVersion, err := gdi32.QueryKMDDriverVersion(hAdapter)
|
||||
t.Logf("driver version: %s, err: %v", gdi32.FormatKMDDriverVersion(driverVersion), err)
|
||||
|
||||
version, err := gdi32.QueryGPUVersion(hAdapter, 0)
|
||||
t.Logf("BIOS version: %q, architecture: %q, err: %v",
|
||||
windows.UTF16ToString(version.BiosVersion[:]), windows.UTF16ToString(version.GpuArchitecture[:]), err)
|
||||
|
||||
caps, err := gdi32.QueryAdapterPerfDataCaps(hAdapter, 0)
|
||||
t.Logf("adapter perf data caps: %+v, err: %v", caps, err)
|
||||
|
||||
perfData, err := gdi32.QueryAdapterPerfData(hAdapter, 0)
|
||||
t.Logf("adapter perf data: %+v, err: %v", perfData, err)
|
||||
|
||||
for node := range uint32(64) {
|
||||
nodePerfData, err := gdi32.QueryNodePerfData(hAdapter, 0, node)
|
||||
if err != nil {
|
||||
t.Logf("node %d: %v", node, err)
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
require.Equal(t, node, nodePerfData.NodeOrdinal)
|
||||
t.Logf("node perf data: %+v", nodePerfData)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatKMDDriverVersion(t *testing.T) {
|
||||
require.Equal(t, "32.0.15.8266", gdi32.FormatKMDDriverVersion(32<<48|0<<32|15<<16|8266))
|
||||
}
|
||||
|
||||
func TestFormatWDDMVersion(t *testing.T) {
|
||||
require.Equal(t, "3.2", gdi32.FormatWDDMVersion(3200))
|
||||
require.Equal(t, "1.1", gdi32.FormatWDDMVersion(1105))
|
||||
}
|
||||
@@ -96,3 +96,70 @@ type GPUDevice struct {
|
||||
DeviceNumber win32.UINT
|
||||
FunctionNumber win32.UINT
|
||||
}
|
||||
|
||||
// D3DKMT_NODE_PERFDATA is the output of KMTQAITYPE_NODEPERFDATA.
|
||||
// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/d3dkmthk/ns-d3dkmthk-_d3dkmt_node_perfdata
|
||||
type D3DKMT_NODE_PERFDATA struct {
|
||||
NodeOrdinal uint32
|
||||
PhysicalAdapterIndex uint32
|
||||
// Frequency is the current clock frequency of the engine in hertz.
|
||||
Frequency uint64
|
||||
// MaxFrequency is the maximum clock frequency of the engine in hertz, while not overclocked.
|
||||
MaxFrequency uint64
|
||||
// MaxFrequencyOC is the maximum clock frequency of the engine in hertz, while overclocked.
|
||||
MaxFrequencyOC uint64
|
||||
// Voltage is the current voltage of the engine in milli volts.
|
||||
Voltage win32.ULONG
|
||||
VoltageMax win32.ULONG
|
||||
VoltageMaxOC win32.ULONG
|
||||
// MaxTransitionLatency is the maximum transition latency to change the frequency in 100 nanoseconds.
|
||||
MaxTransitionLatency uint64
|
||||
}
|
||||
|
||||
// D3DKMT_ADAPTER_PERFDATA is the output of KMTQAITYPE_ADAPTERPERFDATA.
|
||||
// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/d3dkmthk/ns-d3dkmthk-_d3dkmt_adapter_perfdata
|
||||
type D3DKMT_ADAPTER_PERFDATA struct {
|
||||
PhysicalAdapterIndex uint32
|
||||
// MemoryFrequency is the clock frequency of the memory in hertz.
|
||||
MemoryFrequency uint64
|
||||
// MaxMemoryFrequency is the max clock frequency of the memory in hertz, while not overclocked.
|
||||
MaxMemoryFrequency uint64
|
||||
// MaxMemoryFrequencyOC is the max clock frequency of the memory in hertz, while overclocked.
|
||||
MaxMemoryFrequencyOC uint64
|
||||
// MemoryBandwidth is the total amount of memory transferred in bytes.
|
||||
MemoryBandwidth uint64
|
||||
// PCIEBandwidth is the total amount of memory transferred over PCIe in bytes.
|
||||
PCIEBandwidth uint64
|
||||
// FanRPM is the current rpm of the main fan.
|
||||
FanRPM win32.ULONG
|
||||
// Power is the current power draw of the adapter in tenths of a percent (1 = 0.1%).
|
||||
Power win32.ULONG
|
||||
// Temperature is the main temperature sensor reading in tenths of a degree Celsius (1 = 0.1 °C).
|
||||
Temperature win32.ULONG
|
||||
// PowerStateOverride is 1 if the GPU is powered on, otherwise 0.
|
||||
PowerStateOverride uint8
|
||||
}
|
||||
|
||||
// D3DKMT_ADAPTER_PERFDATACAPS is the output of KMTQAITYPE_ADAPTERPERFDATA_CAPS.
|
||||
// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/d3dkmthk/ns-d3dkmthk-_d3dkmt_adapter_perfdatacaps
|
||||
type D3DKMT_ADAPTER_PERFDATACAPS struct {
|
||||
PhysicalAdapterIndex uint32
|
||||
// MaxMemoryBandwidth is the max memory bandwidth in bytes for 1 second.
|
||||
MaxMemoryBandwidth uint64
|
||||
// MaxPCIEBandwidth is the max PCIe bandwidth in bytes for 1 second.
|
||||
MaxPCIEBandwidth uint64
|
||||
// MaxFanRPM is the max fan rpm.
|
||||
MaxFanRPM win32.ULONG
|
||||
// TemperatureMax is the max temperature before damage, in tenths of a degree Celsius.
|
||||
TemperatureMax win32.ULONG
|
||||
// TemperatureWarning is the temperature at which the GPU starts throttling, in tenths of a degree Celsius.
|
||||
TemperatureWarning win32.ULONG
|
||||
}
|
||||
|
||||
// D3DKMT_GPUVERSION is the output of KMTQAITYPE_GPUVERSION.
|
||||
// https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/d3dkmthk/ns-d3dkmthk-_d3dkmt_gpuversion
|
||||
type D3DKMT_GPUVERSION struct {
|
||||
PhysicalAdapterIndex uint32
|
||||
BiosVersion [32]uint16
|
||||
GpuArchitecture [32]uint16
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user