nixinfo
nixinfo copied to clipboard
[Idea] Get GPU information using pure Rust
First of all, I love the initiative of the project. I started to learn Rust a few weeks ago and as my first project I set out to build a neofetch style tool, without realizing the existence of rsfetch, so I'd like to contribute here in what I can.
I saw that external commands are used to obtain information from the GPU, so I started to investigate how else could be done without depending on grep and lspci.
I found in the pciutils repository the following:
In runs on the following systems:
Linux (via /sys/bus/pci, /proc/bus/pci or i386 ports) FreeBSD (via /dev/pci) NetBSD (via libpci) OpenBSD (via /dev/pci) GNU/kFreeBSD (via /dev/pci) Solaris/i386 (direct port access) Aix (via /dev/pci and odmget) GNU Hurd (direct port access) Windows (direct port access, see README.Windows for caveats) CYGWIN (direct port access) BeOS (via syscalls) Haiku (via /dev/misc/poke) Darwin (via IOKit) DOS/DJGPP (via i386 ports) SylixOS (via /proc/pci)
There are many possible places to search, depending on the kernel (or operating system). But in some cases it's a matter of reading a plain text file. In order not to make the issue too long, I recommend the first part of this article. As you can see in the article, it is possible to read the vendor identifier, device identifier and class identifier.
I quote again the previous repository, the following:
The database of PCI IDs (the pci.ids file) gets out of date much faster than I release new versions of this package, so it is maintained separately.
It lives at https://pci-ids.ucw.cz/, where you can browse the database, download the most recent pci.ids file (e.g., by running the update-ids utility) and also submit new entries.
If we go through that website, we can corroborate that the GPU information (in addition to other devices that are not relevant) can be obtained from these ID's.
At first I thought that the pci.ids file should be downloaded regularly to keep it updated but, at least in Arch Linux, I found it in /usr/share/hwdata/pci.ids.
I'm not sure this is the best way to solve it, but I think it can work, without relying on system commands or external crates.
Sorry for taking a while to respond. :sweat_smile:
This is great! I had originally really wanted to read a file in /proc or /sys because you can gather a lot of info from those 2 places alone. But I couldn't find anything related to the GPU after passing through it a couple times.
At first I thought that the
pci.idsfile should be downloaded regularly to keep it updated but, at least in Arch Linux, I found it in/usr/share/hwdata/pci.ids.
Interesting, I'm on Gentoo and I have it at /usr/share/misc/pci.ids It might be best if it was checked for in a known location, and downloaded only if it was missing. I'm going to create a comment directly after this which tracks the location of this file on different distros, because I can only assume it's going to be in different places.
Thank you for bringing this to my attention. It was genuinely irritating me that I'm using system commands to get the GPU model.
Arch - /usr/share/hwdata/pci.ids
Gentoo - /usr/share/misc/pci.ids
I have asked users from other distros and these are some other locations where the file is located:
Fedora 32 (Silverblue) - /usr/share/hwdata/pci.ids
Linux Mint - /usr/share/hwdata/pci.ids
Devuan - /usr/share/misc/pci.ids
Slackware - Not found, but lspci works properly
I don't know if it has anything to do with it, but the version of Slackware is 32-bit, as I was told. If I get more information to advance the topic, I will publish it here.
I've been using /sys/class/drm/card0/device/vendor and /sys/class/drm/card0/device/device on linux, this of course only gets the first gpu
I wrote a quick toy program in rust to print the name of the gpu and it runs in 0.3ms on average, much faster than grepping lspci, I highly recommend implementing this, I can work on a pull request