Skip to content

Instantly share code, notes, and snippets.

@BotBlake
Created June 30, 2024 14:47
Show Gist options
  • Select an option

  • Save BotBlake/197b3da2d85a811bbf7994eef81a092e to your computer and use it in GitHub Desktop.

Select an option

Save BotBlake/197b3da2d85a811bbf7994eef81a092e to your computer and use it in GitHub Desktop.
GPU Information through Python [Linux]

Idea-Board

This Document is meant to explain every possible approach on how to obtain GPU Information on Linux. Each approach should include the Points: "file", "Step-by-Step", "strengths and weaknesses"

lspci

We will use lspci to deliver hardware information about all PCI Devices. Then we will use lspci again, to deliver detailed output for every "GPU" device.

file

lspci_GPUinfo.py

Step-by-Step

  1. run lshw
  2. iterate over result lines and determine GPUs
  3. run lshw -v -s {pci_id (for each GPU)
  4. Build the dict() gpu_element and fill with the information
  5. append gpu_element to a list of GPUs

strengths and weaknesses

strengths weaknesses
No Installation python external
output parsing difficult
unreliable
import subprocess
from json import dumps
all_devices = subprocess.check_output(["lspci"], text=True).strip().split("\n")
print("-" * 20)
gpu_count = 0
gpu_list = list()
for device in all_devices:
if "VGA compatible controller" in device or "3D controller" in device:
try:
pci_id = device.split()[0]
# Get detailed information for the GPU device
detail_output = (
subprocess.check_output(["lspci", "-v", "-s", pci_id], text=True)
.strip()
.split("\n")
)
except subprocess.CalledProcessError:
continue
gpu_element = dict()
vendor = "Unknown"
product_name = "Unknown"
# Get vendor and product name from the initial lspci line
if "NVIDIA" in device:
vendor = "NVIDIA"
product_name = " ".join(device.split()[2:])
elif "AMD" in device or "Advanced Micro Devices" in device:
vendor = "AMD"
product_name = " ".join(device.split()[2:])
elif "Intel" in device:
vendor = "Intel"
product_name = device.split(":")[-1].strip()
else:
product_name = " ".join(device.split()[1:])
gpu_element["product"] = product_name
gpu_element["vendor"] = vendor
businfo = f"pci@{pci_id}"
detail_info = dict()
for detail_line in detail_output:
if detail_line.strip() == "":
continue
try:
key, value = detail_line.split(":", 1)
detail_info[key.strip()] = value.strip()
except ValueError:
# Handle lines that do not contain key-value pairs
detail_info["Info"] = (
gpu_element.get("Info", "") + detail_line.strip() + " "
)
# Adjusting for iGPU / dedicated GPU
if "DeviceName" in detail_info:
device_class = detail_info["DeviceName"]
else:
device_class = "dedicated"
configuration = {
"driver": detail_info["Kernel driver in use"],
}
gpu_element = {
"id": f"GPU{gpu_count+1}",
"class": device_class,
"description": detail_info["Subsystem"],
"product": product_name,
"vendor": vendor,
"physid": pci_id,
"businfo": businfo,
"configuration": configuration,
}
gpu_list.append(gpu_element)
gpu_count += 1
print(f"Total GPUs found: {gpu_count}")
print(dumps(gpu_list, indent=4))
@JkBoyo
Copy link

JkBoyo commented Jul 24, 2024

this looked helpful.
Sorry if you already had it......

@JkBoyo
Copy link

JkBoyo commented Jul 24, 2024

Next time we chat we should talk about lshw -C display. I think it may be a quicker approach to what you're looking for.

@JkBoyo
Copy link

JkBoyo commented Jul 25, 2024

Widely used distros that should be aimed at making sure are compatible.

  1. Debian/Ubuntu
  2. Fedora Linux
  3. opeSUSE
  4. Arch Linux
  5. Gentoo

These seem to be the base os's that most branches go off of per Wikipedia 👀

A better source should probably be acquired but I felt this was a good start.

I have that Dev server I was planning on installing. I can do testing of any variation from whichever hardware info tool we plan to use and see what variations in reporting there are.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment