|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// |
| 3 | +// Copyright The Prometheus Authors |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +//go:build windows |
| 17 | + |
| 18 | +package setupapi |
| 19 | + |
| 20 | +import ( |
| 21 | + "sync" |
| 22 | + "unsafe" |
| 23 | + |
| 24 | + "golang.org/x/sys/windows" |
| 25 | +) |
| 26 | + |
| 27 | +//nolint:gochecknoglobals |
| 28 | +var GUID_DISPLAY_ADAPTER = sync.OnceValue(func() *windows.GUID { |
| 29 | + return &windows.GUID{ |
| 30 | + Data1: 0x4d36e968, |
| 31 | + Data2: 0xe325, |
| 32 | + Data3: 0x11ce, |
| 33 | + Data4: [8]byte{0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}, |
| 34 | + } |
| 35 | +}) |
| 36 | + |
| 37 | +func GetGPUDevices() ([]GPUDevice, error) { |
| 38 | + hDevInfo, _, err := procSetupDiGetClassDevsW.Call( |
| 39 | + uintptr(unsafe.Pointer(GUID_DISPLAY_ADAPTER())), |
| 40 | + 0, |
| 41 | + 0, |
| 42 | + DIGCF_PRESENT, |
| 43 | + ) |
| 44 | + |
| 45 | + if windows.Handle(hDevInfo) == windows.InvalidHandle { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + var ( |
| 50 | + devices []GPUDevice |
| 51 | + deviceData SP_DEVINFO_DATA |
| 52 | + propertyBuffer [256]uint16 |
| 53 | + ) |
| 54 | + |
| 55 | + deviceData.CbSize = uint32(unsafe.Sizeof(deviceData)) |
| 56 | + |
| 57 | + for i := 0; ; i++ { |
| 58 | + ret, _, _ := procSetupDiEnumDeviceInfo.Call(hDevInfo, uintptr(i), uintptr(unsafe.Pointer(&deviceData))) |
| 59 | + if ret == 0 { |
| 60 | + break // No more devices |
| 61 | + } |
| 62 | + |
| 63 | + ret, _, _ = procSetupDiGetDeviceRegistryPropertyW.Call( |
| 64 | + hDevInfo, |
| 65 | + uintptr(unsafe.Pointer(&deviceData)), |
| 66 | + uintptr(SPDRP_DEVICEDESC), |
| 67 | + 0, |
| 68 | + uintptr(unsafe.Pointer(&propertyBuffer[0])), |
| 69 | + uintptr(len(propertyBuffer)*2), |
| 70 | + 0, |
| 71 | + ) |
| 72 | + |
| 73 | + gpuDevice := GPUDevice{} |
| 74 | + |
| 75 | + if ret == 0 { |
| 76 | + gpuDevice.DeviceDesc = "" |
| 77 | + } else { |
| 78 | + gpuDevice.DeviceDesc = windows.UTF16ToString(propertyBuffer[:]) |
| 79 | + } |
| 80 | + |
| 81 | + ret, _, _ = procSetupDiGetDeviceRegistryPropertyW.Call( |
| 82 | + hDevInfo, |
| 83 | + uintptr(unsafe.Pointer(&deviceData)), |
| 84 | + uintptr(SPDRP_FRIENDLYNAME), |
| 85 | + 0, |
| 86 | + uintptr(unsafe.Pointer(&propertyBuffer[0])), |
| 87 | + uintptr(len(propertyBuffer)*2), |
| 88 | + 0, |
| 89 | + ) |
| 90 | + |
| 91 | + if ret == 0 { |
| 92 | + gpuDevice.FriendlyName = "" |
| 93 | + } else { |
| 94 | + gpuDevice.FriendlyName = windows.UTF16ToString(propertyBuffer[:]) |
| 95 | + } |
| 96 | + |
| 97 | + ret, _, _ = procSetupDiGetDeviceRegistryPropertyW.Call( |
| 98 | + hDevInfo, |
| 99 | + uintptr(unsafe.Pointer(&deviceData)), |
| 100 | + uintptr(SPDRP_HARDWAREID), |
| 101 | + 0, |
| 102 | + uintptr(unsafe.Pointer(&propertyBuffer[0])), |
| 103 | + uintptr(len(propertyBuffer)*2), |
| 104 | + 0, |
| 105 | + ) |
| 106 | + |
| 107 | + if ret == 0 { |
| 108 | + gpuDevice.HardwareID = "unknown" |
| 109 | + } else { |
| 110 | + gpuDevice.HardwareID = windows.UTF16ToString(propertyBuffer[:]) |
| 111 | + } |
| 112 | + |
| 113 | + ret, _, _ = procSetupDiGetDeviceRegistryPropertyW.Call( |
| 114 | + hDevInfo, |
| 115 | + uintptr(unsafe.Pointer(&deviceData)), |
| 116 | + uintptr(SPDRP_PHYSICAL_DEVICE_OBJECT_NAME), |
| 117 | + 0, |
| 118 | + uintptr(unsafe.Pointer(&propertyBuffer[0])), |
| 119 | + uintptr(len(propertyBuffer)*2), |
| 120 | + 0, |
| 121 | + ) |
| 122 | + |
| 123 | + if ret == 0 { |
| 124 | + gpuDevice.PhysicalDeviceObjectName = "unknown" |
| 125 | + } else { |
| 126 | + gpuDevice.PhysicalDeviceObjectName = windows.UTF16ToString(propertyBuffer[:]) |
| 127 | + } |
| 128 | + |
| 129 | + devices = append(devices, gpuDevice) |
| 130 | + } |
| 131 | + |
| 132 | + _, _, _ = procSetupDiDestroyDeviceInfoList.Call(hDevInfo) |
| 133 | + |
| 134 | + return devices, nil |
| 135 | +} |
0 commit comments