1use crate::{Bytes, Result};
16
17#[derive(Clone, Debug, PartialEq, Eq)]
19#[non_exhaustive]
20pub struct Gpu {
21 pub name: String,
24 pub vendor: GpuVendor,
26 pub kind: GpuKind,
28 pub vendor_id: Option<u16>,
30 pub device_id: Option<u16>,
32 pub memory: Option<Bytes>,
34 pub driver: Option<String>,
36}
37
38#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
40#[non_exhaustive]
41pub enum GpuVendor {
42 Nvidia,
44 Amd,
46 Intel,
48 Apple,
50 Qualcomm,
52 Arm,
54 Microsoft,
56 Vmware,
58 Other,
60}
61
62impl GpuVendor {
63 #[must_use]
65 pub const fn from_pci(id: u16) -> GpuVendor {
66 match id {
67 0x10de => Self::Nvidia,
68 0x1002 | 0x1022 => Self::Amd,
69 0x8086 => Self::Intel,
70 0x106b => Self::Apple,
71 0x5143 | 0x17cb => Self::Qualcomm,
72 0x13b5 => Self::Arm,
73 0x1414 => Self::Microsoft,
74 0x15ad => Self::Vmware,
75 _ => Self::Other,
76 }
77 }
78
79 #[must_use]
81 pub const fn name(self) -> &'static str {
82 match self {
83 Self::Nvidia => "NVIDIA",
84 Self::Amd => "AMD",
85 Self::Intel => "Intel",
86 Self::Apple => "Apple",
87 Self::Qualcomm => "Qualcomm",
88 Self::Arm => "Arm",
89 Self::Microsoft => "Microsoft",
90 Self::Vmware => "VMware",
91 Self::Other => "Other",
92 }
93 }
94}
95
96#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
98#[non_exhaustive]
99pub enum GpuKind {
100 Integrated,
102 Discrete,
104 Virtual,
106 Unknown,
108}
109
110#[allow(dead_code)] pub(crate) const fn kind_of(vendor: GpuVendor) -> GpuKind {
115 match vendor {
116 GpuVendor::Intel | GpuVendor::Apple | GpuVendor::Qualcomm | GpuVendor::Arm => {
117 GpuKind::Integrated
118 }
119 GpuVendor::Nvidia => GpuKind::Discrete,
120 GpuVendor::Microsoft | GpuVendor::Vmware => GpuKind::Virtual,
121 GpuVendor::Amd | GpuVendor::Other => GpuKind::Unknown,
122 }
123}
124
125pub fn list() -> Result<Vec<Gpu>> {
132 crate::sys::gpus()
133}
134
135#[cfg(test)]
136mod tests {
137 use super::*;
138
139 #[test]
140 fn test_gpu_vendor_from_pci() {
141 assert_eq!(GpuVendor::from_pci(0x10de), GpuVendor::Nvidia);
142 assert_eq!(GpuVendor::from_pci(0x1002), GpuVendor::Amd);
143 assert_eq!(GpuVendor::from_pci(0x8086), GpuVendor::Intel);
144 assert_eq!(GpuVendor::from_pci(0x1234), GpuVendor::Other);
145 assert_eq!(kind_of(GpuVendor::Nvidia), GpuKind::Discrete);
146 assert_eq!(kind_of(GpuVendor::Amd), GpuKind::Unknown);
147 }
148
149 #[test]
150 fn test_gpu_live_list_or_unavailable() {
151 match list() {
152 Ok(list) => {
153 for g in list {
154 assert!(!g.name.is_empty(), "{g:?}");
155 }
156 }
157 Err(e) => assert!(e.is_unavailable(), "{e}"),
158 }
159 }
160}