Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Idea] Get GPU information using pure Rust #2

Open
PandaFoss opened this issue Sep 2, 2020 · 5 comments
Open

[Idea] Get GPU information using pure Rust #2

PandaFoss opened this issue Sep 2, 2020 · 5 comments

Comments

@PandaFoss
Copy link
Contributor

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.

@Phate6660
Copy link
Owner

Sorry for taking a while to respond. 😅

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.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.

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.

@Phate6660
Copy link
Owner

Arch - /usr/share/hwdata/pci.ids
Gentoo - /usr/share/misc/pci.ids

@PandaFoss
Copy link
Contributor Author

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.

@MathGeniusJodie
Copy link

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

@MathGeniusJodie
Copy link

MathGeniusJodie commented Apr 30, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants