|
| 1 | +// © Broadcom. All Rights Reserved. |
| 2 | +// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package host |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "flag" |
| 10 | + "fmt" |
| 11 | + "io" |
| 12 | + "text/tabwriter" |
| 13 | + |
| 14 | + "github.com/vmware/govmomi/cli" |
| 15 | + "github.com/vmware/govmomi/cli/flags" |
| 16 | + "github.com/vmware/govmomi/property" |
| 17 | + "github.com/vmware/govmomi/vim25/mo" |
| 18 | + "github.com/vmware/govmomi/vim25/types" |
| 19 | +) |
| 20 | + |
| 21 | +type info struct { |
| 22 | + *flags.ClientFlag |
| 23 | + *flags.HostSystemFlag |
| 24 | + *flags.OutputFlag |
| 25 | +} |
| 26 | + |
| 27 | +func init() { |
| 28 | + cli.Register("gpu.host.info", &info{}) |
| 29 | +} |
| 30 | + |
| 31 | +func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) { |
| 32 | + cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) |
| 33 | + cmd.ClientFlag.Register(ctx, f) |
| 34 | + |
| 35 | + cmd.HostSystemFlag, ctx = flags.NewHostSystemFlag(ctx) |
| 36 | + cmd.HostSystemFlag.Register(ctx, f) |
| 37 | + |
| 38 | + cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) |
| 39 | + cmd.OutputFlag.Register(ctx, f) |
| 40 | +} |
| 41 | + |
| 42 | +func (cmd *info) Description() string { |
| 43 | + return `Display GPU information for a host. |
| 44 | +
|
| 45 | +Examples: |
| 46 | + govc gpu.host.info -host hostname |
| 47 | + govc gpu.host.info -host hostname -json | jq . |
| 48 | + govc gpu.host.info -host hostname -json | jq -r '.devices[] | select(.deviceName | contains("NVIDIA"))' |
| 49 | + govc find / -type h | xargs -n1 govc gpu.host.info -host # all hosts` |
| 50 | +} |
| 51 | + |
| 52 | +func (cmd *info) Process(ctx context.Context) error { |
| 53 | + if err := cmd.ClientFlag.Process(ctx); err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + if err := cmd.HostSystemFlag.Process(ctx); err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + if err := cmd.OutputFlag.Process(ctx); err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +// Support NVIDIA devices, and exclude virtual GPUs, which have a SubDeviceId of 0x0000 |
| 66 | +func isPhysicalGPU(device types.HostPciDevice) bool { |
| 67 | + return device.VendorId == 0x10de && device.SubDeviceId != 0x0000 |
| 68 | +} |
| 69 | + |
| 70 | +type gpuInfo struct { |
| 71 | + PciId string `json:"pciId"` |
| 72 | + DeviceName string `json:"deviceName"` |
| 73 | + VendorName string `json:"vendorName"` |
| 74 | + DeviceId int16 `json:"deviceId"` |
| 75 | + VendorId int16 `json:"vendorId"` |
| 76 | + SubVendorId int16 `json:"subVendorId"` |
| 77 | + SubDeviceId int16 `json:"subDeviceId"` |
| 78 | + ClassId int16 `json:"classId"` |
| 79 | + Bus uint8 `json:"bus"` |
| 80 | + Slot uint8 `json:"slot"` |
| 81 | + Function uint8 `json:"function"` |
| 82 | + Bridge string `json:"bridge,omitempty"` |
| 83 | +} |
| 84 | + |
| 85 | +type infoResult struct { |
| 86 | + Devices []gpuInfo `json:"devices"` |
| 87 | +} |
| 88 | + |
| 89 | +func (r *infoResult) Write(w io.Writer) error { |
| 90 | + tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0) |
| 91 | + |
| 92 | + for _, gpu := range r.Devices { |
| 93 | + fmt.Fprintf(tw, "PCI ID: %s\n", gpu.PciId) |
| 94 | + fmt.Fprintf(tw, " Device Name: %s\n", gpu.DeviceName) |
| 95 | + fmt.Fprintf(tw, " Vendor Name: %s\n", gpu.VendorName) |
| 96 | + fmt.Fprintf(tw, " Device ID: 0x%04x\n", gpu.DeviceId) |
| 97 | + fmt.Fprintf(tw, " Vendor ID: 0x%04x\n", gpu.VendorId) |
| 98 | + fmt.Fprintf(tw, " SubVendor ID: 0x%04x\n", gpu.SubVendorId) |
| 99 | + fmt.Fprintf(tw, " SubDevice ID: 0x%04x\n", gpu.SubDeviceId) |
| 100 | + fmt.Fprintf(tw, " Class ID: 0x%04x\n", gpu.ClassId) |
| 101 | + fmt.Fprintf(tw, " Bus: 0x%02x, Slot: 0x%02x, Function: 0x%02x\n", gpu.Bus, gpu.Slot, gpu.Function) |
| 102 | + if gpu.Bridge != "" { |
| 103 | + fmt.Fprintf(tw, " Parent Bridge: %s\n", gpu.Bridge) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return tw.Flush() |
| 108 | +} |
| 109 | + |
| 110 | +func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error { |
| 111 | + host, err := cmd.HostSystem() |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + if host == nil { |
| 117 | + return flag.ErrHelp |
| 118 | + } |
| 119 | + |
| 120 | + var h mo.HostSystem |
| 121 | + pc := property.DefaultCollector(host.Client()) |
| 122 | + err = pc.RetrieveOne(ctx, host.Reference(), []string{"hardware"}, &h) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + |
| 127 | + var res infoResult |
| 128 | + for _, device := range h.Hardware.PciDevice { |
| 129 | + if isPhysicalGPU(device) { |
| 130 | + res.Devices = append(res.Devices, gpuInfo{ |
| 131 | + PciId: device.Id, |
| 132 | + DeviceName: device.DeviceName, |
| 133 | + VendorName: device.VendorName, |
| 134 | + DeviceId: device.DeviceId, |
| 135 | + VendorId: device.VendorId, |
| 136 | + SubVendorId: device.SubVendorId, |
| 137 | + SubDeviceId: device.SubDeviceId, |
| 138 | + ClassId: device.ClassId, |
| 139 | + Bus: device.Bus, |
| 140 | + Slot: device.Slot, |
| 141 | + Function: device.Function, |
| 142 | + Bridge: device.ParentBridge, |
| 143 | + }) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return cmd.WriteResult(&res) |
| 148 | +} |
0 commit comments