jaypipes / ghw

Go HardWare discovery/inspection library
Apache License 2.0
1.62k stars 174 forks source link

GPU information cannot be queried after virtual machine GPU pass through. By traversing the PCI information, it is found that the driver is not NVIDIA but vfio PCI. Can I add a vendor Name judge whether the PCI identification is GPU #314

Open huyuan1999 opened 2 years ago

huyuan1999 commented 2 years ago
package main

import (
    "fmt"
    "github.com/jaypipes/ghw"
    "github.com/sirupsen/logrus"
    "os"
    "strings"
)

func Exists(path string) bool {
    _, err := os.Stat(path)
    if err != nil {
        if os.IsExist(err) {
            return true
        }
        return false
    }
    return true
}

func IsDir(path string) bool {
    s, err := os.Stat(path)
    if err != nil {
        return false
    }
    return s.IsDir()
}

func IsFile(path string) bool {
    if Exists(path) {
        return !IsDir(path)
    }
    return false
}

func main() {
    pcidbPath := "/tmp/pci.ids"
    if IsFile(pcidbPath) {
        os.Setenv("PCIDB_PATH", "/tmp/pci.ids")
    }

    pci, err := ghw.PCI()
    if err != nil {
        logrus.Errorln(err)
        return
    }

    for _, devices := range pci.ListDevices() {
        if strings.Index(strings.ToLower(devices.Driver), "nvidia") != -1 || strings.Index(strings.ToLower(devices.Vendor.Name), "nvidia") != -1 {
            fmt.Printf("==================== nvidia %s ====================\n", devices.Address)
            fmt.Println("Address", devices.Address)
            fmt.Println("Vendor.ID", devices.Vendor.ID)
            fmt.Println("Vendor.Name", devices.Vendor.Name)
            fmt.Println("Driver", devices.Driver)
            fmt.Println("devices.Product.ID", devices.Product.ID)
            fmt.Println("devices.Product.VendorID", devices.Product.VendorID)
            fmt.Println("devices.Product.Name", devices.Product.Name)
        }
    }
}

image

ffromani commented 2 years ago

Hi @huyuan1999 ! Thanks for the report. I understand parts of the problem, because indeed inside a VM a VFIO device can be misreported, but I'm not completely sure what's the question here, could you please elaborate? Are you requesting for example a way to override the pci.db?

red-0ne commented 2 years ago

I think what @huyuan1999 is looking for is a way to determine if a PCI device with a nvidia device.Vendor.Name is a GPU or not. Since device.Driver does not return nvidia but vfio-pci, we cannot rely on that property.

We are facing the same problem here. A possible workaround would be to check if device.Class.Name is a Display controller. @fromanirh is it reasonable to do it that way?

ydcool commented 4 months ago

Same issue here, any updates?

ffromani commented 4 months ago

Thanks for elaborating. Rather than make logic on the name, I'd indeed make logic on device class. I'd use ids rather than names.

ffromani commented 4 months ago

Is the gpu subpackage returning the correct information?