blib/lib/Rex/GPU/Detect.pm

filter: Statementstmt 100% 219 / 219 Branchbran 96.3% 104 / 108 Conditioncond 79.7% 55 / 69 MC/DCmcdc 64.8% 24 / 37 Subroutinesub 100% 38 / 38 Podpod 100% 9 / 9 totaltotal 93.5% 449 / 480 CC 87 SCAR 19.0
CC
87
Cov
95%
detect
16.0
_nvidia_compute_by_name
10.0
_detect_vgpu
9.0
CRAP 6.7
« lib/Rex/GPU/NVIDIA/Setup.pm ↑ Summary lib/Rex/GPU/NVIDIA/Setup/Ubuntu.pm »
1 # ABSTRACT: GPU hardware detection via PCI class codes
2  
3 package Rex::GPU::Detect;
4 27 our $VERSION = '0.005';
5 27 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 27 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 27 ▶use Rex::Commands::Gather ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 27 ▶use Rex::Commands::Pkg;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 ▶use Rex::Commands::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶use Rex::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 27 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 27 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 27 ▶use Rex::GPU::NVIDIA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 27 ▶use Rex::GPU::NVIDIA::Requirement;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 27 ▶use Rex::GPU::NVIDIA::VGPU;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 27 require Rex::Exporter;
20 27 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 27 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 27 @EXPORT = qw(
25 detect
26 );
27  
28 # PCI class codes for display controllers
29 # [0300] = VGA controller, [0302] = 3D controller (datacenter GPUs)
30 27 my $PCI_DISPLAY_RE = qr/\[03(?:00|02)\]/;
31  
32 # [0380] = "Display controller" (karr #75): AMD Instinct MI100/MI200/MI300
33 # enumerate there. Accepted for vendor 1002 only, never as a display class of
34 # its own -- an NVIDIA device at [0380] stays unlisted, as before.
35 27 my $AMD_ACCELERATOR_RE = qr/\[0380\]/;
36  
37 # Virtual GPU vendor IDs — skip these (no host driver needed): virtio,
38 # QEMU/QXL, VMware, VirtualBox. Read through virtual_display_vendor_ids, by
39 # this lspci path and by Rex::GPU::Detect::Sysfs alike (karr #73).
40 27 my @VIRTUAL_DISPLAY_VENDOR_IDS = qw( 1af4 1b36 15ad 80ee );
41  
42 # NVIDIA vendor ID
43 27 my $NVIDIA_VENDOR_RE = qr/\[10de:[0-9a-f]{4}\]/i;
44  
45 # AMD vendor ID
46 27 my $AMD_VENDOR_RE = qr/\[1002:[0-9a-f]{4}\]/i;
47  
48 # NVSwitch (karr #23): an HGX baseboard's NVSwitches enumerate as NVIDIA
49 # (10de) "Bridge" devices, PCI class [0680] (PCI_CLASS_BRIDGE_OTHER, the class
50 # the NVSwitch kernel driver claims: open-gpu-kernel-modules
51 # kernel-open/nvidia/linux_nvswitch.c). A device counts as an NVSwitch only if
52 # its ID is listed here or pci.ids named it "... NVSwitch" -- NVIDIA also
53 # made other 10de:0680 devices (nForce chipset bridges), and an unknown one
54 # must not trigger a Fabric Manager install. IDs from the Fabric Manager user
55 # guide's baseboard topology listings (docs.nvidia.com/datacenter/tesla/
56 # fabric-manager-user-guide/, checked 2026-09-24) and pci.ids:
57 # 1ac2 HGX-2 (V100) NVSwitch gen1
58 # 1af1 HGX A100 NVSwitch gen2, "GA100 [A100 NVSwitch]"
59 # 22a3 HGX H100/H200/H800/H20 NVSwitch gen3, "GH100 [H100 NVSwitch]"
60 # NOT here, deliberately: HGX B200/B300/B100 (NVSwitch gen4). The guide says
61 # their NVSwitches "are not recognized as PCIe devices on the host system";
62 # the host sees ConnectX-7 bridge functions instead, so lspci cannot find them.
63 # GB200/GB300 NVL72 compute trays run no Fabric Manager at all (it runs on
64 # the NVLink switch trays). Read through nvswitch_device_ids, by this lspci
65 # path and by Rex::GPU::Detect::Sysfs alike (karr #73).
66 27 my $NVSWITCH_CLASS_RE = qr/\[0680\]/;
67 27 my %NVSWITCH_DEVICE_IDS = (
68 '1ac2' => { generation => 1 },
69 '1af1' => { generation => 2 },
70 '22a3' => { generation => 3 }
71 );
72  
73 # Rex::GPU::Detect::open_kernel_module_required and legacy_driver_requirement
74 # below are wrappers over Rex::GPU::NVIDIA::Requirement, kept with their exact
75 # return values for Rex::GPU::NVIDIA's install paths (epic karr #25).
76  
77  
78  
79 110 ▶sub detect {
Subroutine detect: called CC=16 SCAR=27.7
Pod documented
80 110 ▶ my $class = _invocant(@_) // return __PACKAGE__->configured_class->detect(@_);
Condition100%2/2
_invocant(@_) // (return __PACKAGE__->configured_class->detect(@_))
l!l
5654
MC/DC100%1/1
_invocant(@_) // (return __PACKAGE__->configured_class->detect(@_))
_invocant(@_)
Truth table100%2/2
A
A= _invocant(@_)
Aresult
11
00
81 56 $class->_ensure_lspci;
82  
83 53 my $pci_output = run "lspci -nn 2>&1 | grep -E '\\[03(00|02)\\]|\\[0380\\].*\\[1002:'",
84 auto_die => 0;
85  
86 53 my $result = { nvidia => [], amd => [], nvswitch => [] };
87  
88 53 ▶ return $result unless $pci_output;
Branch: 1 branch
BranchTrueFalseTotal
unless $pci_output 3 50 53
89  
90 # Virtual displays are skipped PER LINE, not by matching the whole blob: a
91 # passthrough host (vfio-pci) or cloud GPU VM shows an emulated console
92 # (QXL, virtio-vga, ...) next to the real card, and a blob match hid the
93 # real card (karr #17). Vendor checks run first, so a [10de:]/[1002:] line
94 # is never classified virtual — only a line that is not NVIDIA/AMD can be.
95 50 my $virtual_ids = join '|', map { quotemeta } $class->virtual_display_vendor_ids;
96 50 my $virtual_re = qr/\[(?:$virtual_ids):[0-9a-f]{4}\]/i;
97 50 my $virtual = 0;
98 50 my @nvidia_slots;
99 50 for my $line (split /\n/, $pci_output) {
100 # [0380] counts for AMD only (karr #75), whatever the grep let through.
101 74 ▶ next if $line =~ $AMD_ACCELERATOR_RE && $line !~ $AMD_VENDOR_RE;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$AMD_ACCELERATOR_RE/u and not $line =~ /$AMD_VENDOR_RE/u 2 72 74
Condition100%3/3
$line =~ /$AMD_ACCELERATOR_RE/u and not $line =~ /$AMD_VENDOR_RE/u
!ll&&!rl&&r
7022
MC/DC100%2/2
$line =~ /$AMD_ACCELERATOR_RE/u and not $line =~ /$AMD_VENDOR_RE/u
$line =~ /$AMD_ACCELERATOR_RE/u not $line =~ /$AMD_VENDOR_RE/u
Truth table100%3/3
A and not(B)
A= $line =~ /$AMD_ACCELERATOR_RE/uB= not $line =~ /$AMD_VENDOR_RE/u
ABresult
0X0
100
111
102 72 ▶ if ($line =~ $NVIDIA_VENDOR_RE) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /$NVIDIA_VENDOR_RE/u) { } 58 14 72
elsif ($line =~ /$AMD_VENDOR_RE/u) { } 6 8 14
elsif ($line =~ /$virtual_re/u) { } 6 2 8
103 58 my $gpu = $class->_parse_nvidia_line($line);
104 58 ▶ next unless $gpu;
Branch: 1 branch
BranchTrueFalseTotal
unless $gpu 0 58 58
105 58 push @{$result->{nvidia}}, $gpu;
106 58 push @nvidia_slots, $class->_pci_slot($line);
107 }
108 elsif ($line =~ $AMD_VENDOR_RE) {
109 6 my $gpu = $class->_parse_amd_line($line);
110 6 ▶ push @{$result->{amd}}, $gpu if $gpu;
Branch: 1 branch
BranchTrueFalseTotal
if $gpu 6 0 6
111 }
112 elsif ($line =~ $virtual_re) {
113 6 $virtual++;
114 6 Rex::Logger::info(" [skip] virtual display: $line");
115 }
116 }
117  
118 Rex::Logger::info("Virtual GPU detected (virtio/QEMU/VMware/VBox) — skipping")
119 50 ▶ if $virtual && !@{$result->{nvidia}} && !@{$result->{amd}};
Branch: 1 branch
BranchTrueFalseTotal
if $virtual and not @{$result->{"nvidia"};} and not @{$result->{"amd"};} 3 47 50
Condition83%5/6
$virtual and not @{$result->{"nvidia"};} and not @{$result->{"amd"};}
!ll&&!rl&&r
4703
$virtual and not @{$result->{"nvidia"};}
!ll&&!rl&&r
4523
MC/DC66%2/3
$virtual and not @{$result->{"nvidia"};} and not @{$result->{"amd"};}
$virtual not @{$result->{"nvidia"};} not @{$result->{"amd"};}
Truth table75%3/4
A and not(B) and not(C)
A= $virtualB= not @{$result->{"nvidia"};}C= not @{$result->{"amd"};}
ABCresult
0XX0
10X0
1100
1111
120  
121 # NVSwitch (karr #23): only where there is an NVIDIA GPU for it to connect,
122 # so a host without one runs no extra command.
123 50 ▶ $result->{nvswitch} = $class->_detect_nvswitch if @{$result->{nvidia}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$result->{"nvidia"};} 40 10 50
124  
125 # vGPU guest (karr #24): likewise only with an NVIDIA GPU, read-only.
126 50 ▶ $class->_detect_vgpu($result->{nvidia}, \@nvidia_slots) if @{$result->{nvidia}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$result->{"nvidia"};} 40 10 50
127  
128 50 return $result;
129 }
130  
131 # karr #46: lspci present => install nothing (read-only; OCP needs no
132 # pciutils then). `command -v` through run, not can_run: can_run stats the
133 # path through the file interface, which needs SFTP on SSH/OpenSSH, and it
134 # answers under the same PATH the `lspci -nn` below runs with. Rex::Pkg dies
135 # "OS/Provider not supported" on the RHEL-family names lsb_release gives
136 # (karr #39), so those get dnf + rpm -q, as Setup::RHEL's install_helpers
137 # does; the name list is Rex::GPU::NVIDIA's (nvidia_class), not a copy.
138 56 ▶sub _ensure_lspci {
Subroutine _ensure_lspci: called CC=6 SCAR=17.9
139 56 my ( $class ) = @_;
140 56 ▶ return if $class->_has_lspci;
Branch: 1 branch
BranchTrueFalseTotal
if $class->_has_lspci 47 9 56
141  
142 9 ▶ if ($class->nvidia_class->_rhel_family_name(Rex::Commands::Gather::operating_system())) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->nvidia_class->_rhel_family_name(Rex::Commands::Gather::operating_system())) { } 4 5 9
143 4 Rex::Logger::info('lspci not found -- installing pciutils with dnf');
144 4 run 'dnf install -y pciutils', auto_die => 0;
145 4 run 'rpm -q pciutils 2>&1', auto_die => 0;
146 4 ▶ croak 'pciutils not installed after dnf install -- check dnf output; '
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 1 3 4
147 .'GPU detection needs lspci' if $? != 0;
148 }
149 else {
150 5 ▶ pkg ["pciutils"], ensure => "present" unless is_installed("pciutils");
Branch: 1 branch
BranchTrueFalseTotal
unless is_installed("pciutils") 4 1 5
151 }
152  
153 8 ▶ croak 'lspci not found on the host after installing pciutils -- '
Branch: 1 branch
BranchTrueFalseTotal
unless $class->_has_lspci 2 6 8
154 .'GPU detection needs lspci on the PATH' unless $class->_has_lspci;
155 }
156  
157 37 ▶sub _has_lspci {
Subroutine _has_lspci: called CC=2 SCAR=6.9
158 37 my ( $class ) = @_;
159 37 run 'command -v lspci >/dev/null 2>&1', auto_die => 0;
160 37 ▶ return $? == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$? == 0 ? : 26 11 37
161 }
162  
163 # Read-only: `lspci -nn -d 10de:` filtered to class [0680]; one hashref per
164 # recognised NVSwitch.
165 40 ▶sub _detect_nvswitch {
Subroutine _detect_nvswitch: called CC=5 SCAR=16.1
166 40 my ( $class ) = @_;
167 40 my $out = run "lspci -nn -d 10de: 2>/dev/null | grep -F '[0680]'", auto_die => 0;
168 40 my @switches;
169 40 ▶ for my $line (split /\n/, $out // '') {
Condition50%1/2
$out // ""
l!l
400
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
170 27 my $switch = $class->_parse_nvswitch_line($line);
171 27 ▶ push @switches, $switch if $switch;
Branch: 1 branch
BranchTrueFalseTotal
if $switch 4 23 27
172 }
173 40 ▶ Rex::Logger::info(' [ok] NVSwitch: '.scalar(@switches).' ('.$switches[0]{name}.')')
Branch: 1 branch
BranchTrueFalseTotal
if @switches 1 39 40
174 if @switches;
175 40 return \@switches;
176 }
177  
178 # vGPU guest (karr #24): lspci -nn shows a vGPU with the physical GPU's
179 # device ID, so only the subsystem ID tells it apart -- NVIDIA gives every
180 # vGPU type its own (Rex::GPU::NVIDIA::VGPU). `lspci -vmmnn -d 10de:` prints
181 # it per slot; each GPU gets subsystem_vendor_id/subsystem_id (undef if its
182 # slot is not in that output), vgpu 0|1 and, for 1, vgpu_type. compute is
183 # not touched: whether a driver can be installed is install_driver's call.
184 40 ▶sub _detect_vgpu {
Subroutine _detect_vgpu: called CC=9 SCAR=22.0
185 40 my ( $class, $gpus, $slots ) = @_;
186 40 my $out = run 'lspci -vmmnn -d 10de: 2>/dev/null', auto_die => 0;
187 40 my $sub = $class->_parse_lspci_vmm($out);
188 40 for my $i (0 .. $#$gpus) {
189 58 my $gpu = $gpus->[$i];
190 58 ▶ my $rec = defined $slots->[$i] ? $sub->{ $slots->[$i] } : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $slots->[$i] ? : 57 1 58
191 # a slot whose device ID is not the GPU's is not that GPU
192 undef $rec if $rec && defined $gpu->{device_id}
193 58 ▶ && ( $rec->{device_id} // '' ) ne lc $gpu->{device_id};
Branch: 1 branch
BranchTrueFalseTotal
if $rec and defined $gpu->{"device_id"} and ($rec->{"device_id"} // "") ne lc $gpu->{"device_id"} 1 57 58
Condition75%6/8
$rec and defined $gpu->{"device_id"} and ($rec->{"device_id"} // "") ne lc $gpu->{"device_id"}
!ll&&!rl&&r
5161
$rec and defined $gpu->{"device_id"}
!ll&&!rl&&r
5107
$rec->{"device_id"} // ""
l!l
70
MC/DC33%1/3
$rec and defined $gpu->{"device_id"} and ($rec->{"device_id"} // "") ne lc $gpu->{"device_id"}
$rec defined $gpu->{"device_id"} $rec->{"device_id"}
Truth table50%2/4
A and B and C
A= $recB= defined $gpu->{"device_id"}C= $rec->{"device_id"}
ABCresult
0XX0
10X0
1100
1111
194 58 ▶ $gpu->{subsystem_vendor_id} = $rec ? $rec->{subsystem_vendor_id} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$rec ? : 6 52 58
195 58 ▶ $gpu->{subsystem_id} = $rec ? $rec->{subsystem_id} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$rec ? : 6 52 58
196 58 $class->_mark_vgpu($gpu);
197 }
198 40 return;
199 }
200  
201 # vgpu 0|1 and, for 1, vgpu_type on a GPU hash whose device_id,
202 # subsystem_vendor_id and subsystem_id are set (four lowercase hex digits or
203 # undef). Shared with Rex::GPU::Detect::Sysfs, which reads the subsystem IDs
204 # from sysfs instead of lspci -vmmnn.
205 89 ▶sub _mark_vgpu {
Subroutine _mark_vgpu: called CC=3 SCAR=11.0
206 89 my ( $class, $gpu ) = @_;
207 my $type = $class->vgpu_class->type_for(
208 89 $gpu->{device_id}, $gpu->{subsystem_id}, $gpu->{subsystem_vendor_id});
209 89 ▶ $gpu->{vgpu} = defined $type ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 4 85 89
210 89 ▶ return unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 85 4 89
211 4 $gpu->{vgpu_type} = $type;
212 Rex::Logger::info(' [vgpu] NVIDIA: '.$gpu->{name}.' is an NVIDIA vGPU guest device (type '
213 .$type.', 10de:'.$gpu->{device_id}.' subsystem '.$gpu->{subsystem_id}
214 4 .') -- it needs the licensed NVIDIA vGPU guest driver');
215 4 return;
216 }
217  
218 # `lspci -vmmnn` records (blank-line separated "Key:<TAB>value" lines) =>
219 # { slot => { device_id, subsystem_vendor_id, subsystem_id } }, IDs
220 # lowercase, undef where lspci printed none. Anything else is ignored.
221 45 ▶sub _parse_lspci_vmm {
Subroutine _parse_lspci_vmm: called CC=9 SCAR=22.0
222 45 my ( $class, $out ) = @_;
223 45 my %by_slot;
224 45 ▶ for my $record (split /\n\s*\n/, $out // '') {
Condition100%2/2
$out // ""
l!l
441
MC/DC100%1/1
$out // ""
$out
Truth table100%2/2
A
A= $out
Aresult
11
00
225 36 my %f;
226 36 for my $line (split /\n/, $record) {
227 148 ▶ $f{$1} = $2 if $line =~ /\A(Slot|Device|SVendor|SDevice):\s*(.*?)\s*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A(Slot|Device|SVendor|SDevice):\s*(.*?)\s*\z/u 62 86 148
228 }
229 36 ▶ next unless defined $f{Slot};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f{"Slot"} 20 16 36
230 16 my $slot = $class->_normalize_slot($f{Slot});
231 16 ▶ next unless defined $slot;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slot 5 11 16
232 my %id = map {
233 11 ▶ my ( $id ) = ( $f{$_} // '' ) =~ /\[([0-9a-f]{4})\]\z/i;
Condition100%2/2
$f{$_} // ""
l!l
312
MC/DC100%1/1
$f{$_} // ""
$f{$_}
Truth table100%2/2
A
A= $f{$_}
Aresult
11
00
234 33 ▶ ( $_ => defined $id ? lc $id : undef );
Branch: 1 branch
BranchTrueFalseTotal
defined $id ? : 31 2 33
235 } qw( Device SVendor SDevice );
236 $by_slot{$slot} = {
237 device_id => $id{Device},
238 subsystem_vendor_id => $id{SVendor},
239 subsystem_id => $id{SDevice}
240 11 };
241 }
242 45 return \%by_slot;
243 }
244  
245 # The PCI address an `lspci -nn` line starts with, normalized.
246 61 ▶sub _pci_slot {
Subroutine _pci_slot: called CC=1 SCAR=0.0
247 61 my ( $class, $line ) = @_;
248 61 my ( $slot ) = $line =~ /\A(\S+)\s/;
249 61 return $class->_normalize_slot($slot);
250 }
251  
252 # lspci -nn prints every slot with its domain once any device has a non-zero
253 # one; -vmm prints it only for a device whose domain is non-zero. The
254 # domain 0000 is dropped so both forms of the same device compare equal.
255 77 ▶sub _normalize_slot {
Subroutine _normalize_slot: called CC=5 SCAR=16.1
256 77 my ( $class, $slot ) = @_;
257 77 ▶ return unless defined $slot
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slot and $slot =~ /\A(?:([0-9a-f]{4,}):)?([0-9a-f]{2}:[0-9a-f]{2}\.[0-7])\z/iu 6 71 77
Condition66%2/3
defined $slot and $slot =~ /\A(?:([0-9a-f]{4,}):)?([0-9a-f]{2}:[0-9a-f]{2}\.[0-7])\z/iu
!ll&&!rl&&r
0671
MC/DC50%1/2
defined $slot and $slot =~ /\A(?:([0-9a-f]{4,}):)?([0-9a-f]{2}:[0-9a-f]{2}\.[0-7])\z/iu
defined $slot $slot =~ /\A(?:([0-9a-f]{4,}):)?([0-9a-f]{2}:[0-9a-f]{2}\.[0-7])\z/iu
Truth table66%2/3
A and B
A= defined $slotB= $slot =~ /\A(?:([0-9a-f]{4,}):)?([0-9a-f]{2}:[0-9a-f]{2}\.[0-7])\z/iu
ABresult
0X0
100
111
258 && $slot =~ /\A(?:([0-9a-f]{4,}):)?([0-9a-f]{2}:[0-9a-f]{2}\.[0-7])\z/i;
259 71 my ( $domain, $bdf ) = ( $1, lc $2 );
260 71 ▶ return defined $domain && $domain !~ /\A0+\z/ ? lc($domain).':'.$bdf : $bdf;
Branch: 1 branch
BranchTrueFalseTotal
defined $domain && !($domain =~ /\A0+\z/u) ? : 17 54 71
Condition100%3/3
defined $domain and not $domain =~ /\A0+\z/u
!ll&&!rl&&r
50417
MC/DC100%2/2
defined $domain and not $domain =~ /\A0+\z/u
defined $domain not $domain =~ /\A0+\z/u
Truth table100%3/3
A and not(B)
A= defined $domainB= not $domain =~ /\A0+\z/u
ABresult
0X0
100
111
261 }
262  
263 34 ▶sub _parse_nvswitch_line {
Subroutine _parse_nvswitch_line: called CC=7 SCAR=19.5
264 34 my ( $class, $line ) = @_;
265 34 ▶ return unless defined $line && $line =~ $NVSWITCH_CLASS_RE && $line =~ $NVIDIA_VENDOR_RE;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line and $line =~ /$NVSWITCH_CLASS_RE/u and $line =~ /$NVIDIA_VENDOR_RE/u 25 9 34
Condition83%5/6
defined $line and $line =~ /$NVSWITCH_CLASS_RE/u and $line =~ /$NVIDIA_VENDOR_RE/u
!ll&&!rl&&r
2419
defined $line and $line =~ /$NVSWITCH_CLASS_RE/u
!ll&&!rl&&r
02410
MC/DC66%2/3
defined $line and $line =~ /$NVSWITCH_CLASS_RE/u and $line =~ /$NVIDIA_VENDOR_RE/u
defined $line $line =~ /$NVSWITCH_CLASS_RE/u $line =~ /$NVIDIA_VENDOR_RE/u
Truth table75%3/4
A and B and C
A= defined $lineB= $line =~ /$NVSWITCH_CLASS_RE/uC= $line =~ /$NVIDIA_VENDOR_RE/u
ABCresult
0XX0
10X0
1100
1111
266 9 my ($device_id) = $line =~ /\[10de:([0-9a-f]{4})\]/i;
267 9 my ($name) = $line =~ /:\s+NVIDIA\s+Corporation\s+(.+?)\s*\[10de:/;
268 9 ▶ $name //= 'Unknown NVIDIA bridge';
Condition50%1/2
$name //= "Unknown NVIDIA bridge"
l!l
90
MC/DC0%0/1
$name //= "Unknown NVIDIA bridge"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
269 9 ▶ unless (( grep { $_ eq lc $device_id } $class->nvswitch_device_ids )
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq lc $device_id;} $class->nvswitch_device_ids or $name =~ /\bNVSwitch\b/iu) 2 7 9
Condition100%3/3
grep {$_ eq lc $device_id;} $class->nvswitch_device_ids or $name =~ /\bNVSwitch\b/iu
l!l&&r!l&&!r
612
MC/DC100%2/2
grep {$_ eq lc $device_id;} $class->nvswitch_device_ids or $name =~ /\bNVSwitch\b/iu
grep {$_ eq lc $device_id;} $class->nvswitch_device_ids $name =~ /\bNVSwitch\b/iu
Truth table100%3/3
A or B
A= grep {$_ eq lc $device_id;} $class->nvswitch_device_idsB= $name =~ /\bNVSwitch\b/iu
ABresult
1X1
011
000
270 || $name =~ /\bNVSwitch\b/i) {
271 2 Rex::Logger::info(" [skip] NVIDIA bridge device not known as an NVSwitch: $name [10de:$device_id]");
272 2 return;
273 }
274 return {
275 7 name => $name,
276 vendor => 'nvidia',
277 pci_class => '0680',
278 device_id => lc $device_id
279 };
280 }
281  
282 762 ▶sub _parse_nvidia_line {
Subroutine _parse_nvidia_line: called CC=4 SCAR=13.9
283 762 my ( $class, $line ) = @_;
284  
285 762 my ($pci_class) = $line =~ /\[(03\d{2})\]/;
286 762 my ($device_id) = $line =~ /\[10de:([0-9a-f]{4})\]/i;
287 762 my ($name) = $line =~ /:\s+NVIDIA\s+Corporation\s+(.+?)\s*\[10de:/;
288 762 ▶ $name //= 'Unknown NVIDIA GPU';
Condition50%1/2
$name //= "Unknown NVIDIA GPU"
l!l
7620
MC/DC0%0/1
$name //= "Unknown NVIDIA GPU"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
289 762 ▶ $pci_class //= '0300';
Condition50%1/2
$pci_class //= "0300"
l!l
7620
MC/DC0%0/1
$pci_class //= "0300"
$pci_class
Truth table50%1/2
A
A= $pci_class
Aresult
11
00
290  
291 762 my $compute = $class->_is_nvidia_compute($pci_class, $name, $device_id);
292  
293 762 ▶ my $status = $compute ? 'ok' : 'skip';
Branch: 1 branch
BranchTrueFalseTotal
$compute ? : 677 85 762
294 762 Rex::Logger::info(" [$status] NVIDIA: $name (PCI class $pci_class)");
295  
296 return {
297 762 name => $name,
298 vendor => 'nvidia',
299 pci_class => $pci_class,
300 compute => $compute,
301 device_id => $device_id, # e.g. "2e12"; undef if the line had no [10de:XXXX]
302 };
303 }
304  
305 # 1 (compute) or 0: the generation row or the PCI class decides; only where
306 # neither does, the name (karr #73: Rex::GPU::Detect::Sysfs has no name and
307 # overrides that half to return undef, "undecided"). Always exactly one
308 # value, also in list context.
309 877 ▶sub _is_nvidia_compute {
Subroutine _is_nvidia_compute: called CC=2 SCAR=6.9
310 877 my ( $class, $pci_class, $name, $device_id ) = @_;
311 877 my $compute = $class->_nvidia_compute_by_generation($pci_class, $name, $device_id);
312 877 ▶ return $compute if defined $compute;
Branch: 1 branch
BranchTrueFalseTotal
if defined $compute 827 50 877
313 50 return scalar $class->_nvidia_compute_by_name($pci_class, $name, $device_id);
314 }
315  
316 # 0 or 1 where the device ID's generation row or the PCI class decides; undef
317 # (bare return) where neither does.
318 877 ▶sub _nvidia_compute_by_generation {
Subroutine _nvidia_compute_by_generation: called CC=6 SCAR=17.9
319 877 my ( $class, $pci_class, $name, $device_id ) = @_;
320  
321 # The generation decides, not the marketing name (karr #45, #54; maintainer
322 # decision: every GPU usable for AI counts, MX/GT/GTX 9xx included). The
323 # rows of Rex::GPU::NVIDIA::Requirement cover every ID 0000-2FFF plus
324 # Blackwell Ultra: Maxwell .. Blackwell Ultra => 1, Kepler or older => 0.
325 # lspci prints the ID even when a stale pci.ids leaves the name as "Device".
326 877 my $req = $class->requirement_class->for_device_id($device_id);
327  
328 # A Kepler-or-older row wins over the PCI class (karr #55): a class-0302
329 # Tesla K80/K40/K20 is skipped like a Kepler display card, instead of
330 # reaching plan as compute and stopping the install for every other GPU.
331 877 ▶ if (defined $req->compute && !$req->compute) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $req->compute and not $req->compute) 92 785 877
Condition100%3/3
defined $req->compute and not $req->compute
!ll&&!rl&&r
5473192
MC/DC100%2/2
defined $req->compute and not $req->compute
defined $req->compute not $req->compute
Truth table100%3/3
A and not(B)
A= defined $req->computeB= not $req->compute
ABresult
0X0
100
111
332 # Skipped, not died: gpu_setup (and Rex::Rancher's gpu => 1) go on
333 # without a driver for the old card, and install for the newer ones.
334 92 ▶ Rex::Logger::info(' NVIDIA GPU '.$name.' (10de:'.$req->device_id.') is '
Branch: 1 branch
BranchTrueFalseTotal
defined $req->max_branch ? : 92 0 92
335 .$req->generation.' silicon'
336 .( defined $req->max_branch
337 ? ': it needs driver branch '.$req->max_branch.' or older, which current '
338 .'distributions no longer package'
339 : '' )
340 .' -- skipped, no driver installed', 'warn');
341 92 return 0;
342 }
343  
344 # PCI class [0302] = 3D Controller — compute/datacenter GPU; with no row
345 # (or no ID) the class alone decides.
346 785 ▶ return 1 if $pci_class eq '0302';
Branch: 1 branch
BranchTrueFalseTotal
if $pci_class eq "0302" 244 541 785
347 541 ▶ return 1 if $req->compute;
Branch: 1 branch
BranchTrueFalseTotal
if $req->compute 491 50 541
348 50 return;
349 }
350  
351 # The name rules, for a GPU the generation and the class left undecided: 1
352 # for a name of Maxwell or later, else 0 with the unknown-model warning.
353 44 ▶sub _nvidia_compute_by_name {
Subroutine _nvidia_compute_by_name: called CC=10 SCAR=23.0
354 44 my ( $class, $pci_class, $name, $device_id ) = @_;
355  
356 # Name rules: reached only for an ID no generation row covers (0x3000 and
357 # up, bar Blackwell Ultra: silicon newer than the table) or no ID at all.
358 # Each one names only products of Maxwell or later: checked against every
359 # name in NVIDIA's supportedchips lists (615.71.09), no Kepler-or-older
360 # product matches. pci.ids names a few Kepler IDs like later products
361 # (0FC5 "GK107 [GeForce GT 1030]", 11C7 "GK106 [GeForce GTX 750 Ti]", GK107
362 # "...-A1" samples) -- the Kepler row catches those by ID before this. No
363 # negative rules: anything else is the unknown default below, 0 either way.
364 44 ▶ return 1 if $name =~ /\bRTX\b/i; # Turing and later only
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\bRTX\b/iu 1 43 44
365 43 ▶ return 1 if $name =~ /\bGTX\s*1\d{3}\b/i; # GTX 10xx Pascal, 16xx Turing
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\bGTX\s*1\d{3}\b/iu 2 41 43
366 41 ▶ return 1 if $name =~ /\bGTX\s*(?:9\d{2}|745|750)(?!\d)/i; # Maxwell (GTX 76x-78x are Kepler)
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\bGTX\s*(?:9\d{2}|745|750)(?!\d)/iu 2 39 41
367 39 ▶ return 1 if $name =~ /\bGT\s*1\d{3}\b/i; # GT 1010/1030 Pascal
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\bGT\s*1\d{3}\b/iu 1 38 39
368 38 ▶ return 1 if $name =~ /\bGeForce\s+MX\s*\d{3}\b/i; # MX110..MX570; not GeForce2/4 MX
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\bGeForce\s+MX\s*\d{3}\b/iu 1 37 38
369 37 ▶ return 1 if $name =~ /\bTITAN\s+(?:X|Xp|V|RTX)\b/i; # not the Kepler GTX TITAN/Black/Z
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\bTITAN\s+(?:X|Xp|V|RTX)\b/iu 1 36 37
370 36 ▶ return 1 if $name =~ /\bQuadro\s+(?:GP|GV|[MPT])\s*\d/i; # Quadro M/P/T/GP/GV; K is mixed
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\bQuadro\s+(?:GP|GV|[MPT])\s*\d/iu 1 35 36
371 35 ▶ return 1 if $name =~ /\bTesla\s+(?:[PV]\d|T4\b|M\d{1,2}\b)/; # not Fermi M20x0, Kepler K
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\bTesla\s+(?:[PV]\d|T4\b|M\d{1,2}\b)/u 1 34 35
372 34 ▶ return 1 if $name =~ /\b[AHLVP]\d{1,3}[GSi]?\b/; # A100, H100, L40, V100, P40, ...
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\b[AHLVP]\d{1,3}[GSi]?\b/u 5 29 34
373  
374 # Unknown — safe default
375 29 Rex::Logger::info(" Unknown NVIDIA GPU model: $name — not in compute list", "warn");
376 29 return 0;
377 }
378  
379  
380 70 ▶sub open_kernel_module_required {
Subroutine open_kernel_module_required: called CC=3 SCAR=11.0
Pod documented
381 70 ▶ my $class = _invocant(@_) // return __PACKAGE__->configured_class->open_kernel_module_required(@_);
Condition100%2/2
_invocant(@_) // (return __PACKAGE__->configured_class->open_kernel_module_required(@_))
l!l
3535
MC/DC100%1/1
_invocant(@_) // (return __PACKAGE__->configured_class->open_kernel_module_required(@_))
_invocant(@_)
Truth table100%2/2
A
A= _invocant(@_)
Aresult
11
00
382 35 my ( undef, $device_id ) = @_;
383 35 ▶ return $class->requirement_class->for_device_id($device_id)->kernel_module eq 'open'
Branch: 1 branch
BranchTrueFalseTotal
$class->requirement_class->for_device_id($device_id)->kernel_module eq 'open' ? : 16 19 35
384 ? 1 : 0;
385 }
386  
387  
388 90 ▶sub legacy_driver_requirement {
Subroutine legacy_driver_requirement: called CC=3 SCAR=11.0
Pod documented
389 90 ▶ my $class = _invocant(@_) // return __PACKAGE__->configured_class->legacy_driver_requirement(@_);
Condition100%2/2
_invocant(@_) // (return __PACKAGE__->configured_class->legacy_driver_requirement(@_))
l!l
4545
MC/DC100%1/1
_invocant(@_) // (return __PACKAGE__->configured_class->legacy_driver_requirement(@_))
_invocant(@_)
Truth table100%2/2
A
A= _invocant(@_)
Aresult
11
00
390 45 my ( undef, $device_id ) = @_;
391 45 my $req = $class->requirement_class->for_device_id($device_id);
392 45 ▶ return unless defined $req->max_branch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $req->max_branch 21 24 45
393 24 return { generation => $req->generation, max_branch => $req->max_branch };
394 }
395  
396 11 ▶sub _parse_amd_line {
Subroutine _parse_amd_line: called CC=3 SCAR=11.0
397 11 my ( $class, $line ) = @_;
398  
399 11 my ($pci_class) = $line =~ /\[(03\d{2})\]/;
400 # Same form as _parse_nvidia_line: vendor stripped, the rest up to the
401 # [1002:xxxx] id as lspci prints it. Vendor 1002 spellings across pci.ids
402 # versions: current, the 2010-era "nee ATI" one, and the pre-2010 one.
403 11 my ($name) = $line =~ /:\s+(?:Advanced\s+Micro\s+Devices,\s+Inc\.\s+\[AMD\/ATI\]|Advanced\s+Micro\s+Devices\s+\[AMD\]\s+nee\s+ATI|ATI\s+Technologies\s+Inc\.?)\s+(.+?)\s*\[1002:/;
404 11 ▶ $name //= 'Unknown AMD GPU';
Condition100%2/2
$name //= "Unknown AMD GPU"
l!l
101
MC/DC100%1/1
$name //= "Unknown AMD GPU"
$name
Truth table100%2/2
A
A= $name
Aresult
11
00
405 11 ▶ $pci_class //= '0300';
Condition50%1/2
$pci_class //= "0300"
l!l
110
MC/DC0%0/1
$pci_class //= "0300"
$pci_class
Truth table50%1/2
A
A= $pci_class
Aresult
11
00
406  
407 11 Rex::Logger::info(" [info] AMD: $name (PCI class $pci_class)");
408  
409 return {
410 11 name => $name,
411 vendor => 'amd',
412 pci_class => $pci_class,
413 compute => 0, # AMD compute support not yet implemented
414 };
415 }
416  
417  
418 189 ▶sub configured_class {
Subroutine configured_class: called CC=6 SCAR=18.0
Pod documented
419 189 my ( $class, $detect ) = @_;
420 189 my $origin = 'detect =>';
421 189 ▶ unless (defined $detect && $detect ne '') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $detect and $detect ne "") 176 13 189
Condition66%2/3
defined $detect and $detect ne ""
!ll&&!rl&&r
176013
MC/DC50%1/2
defined $detect and $detect ne ""
defined $detect $detect ne ""
Truth table66%2/3
A and B
A= defined $detectB= $detect ne ""
ABresult
0X0
100
111
422 176 $detect = Rex::Config->get('gpu_detect_class');
423 176 $origin = 'set gpu_detect_class';
424 176 ▶ return __PACKAGE__ unless defined $detect && $detect ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $detect and $detect ne "" 173 3 176
Condition100%3/3
defined $detect and $detect ne ""
!ll&&!rl&&r
21713
MC/DC100%2/2
defined $detect and $detect ne ""
defined $detect $detect ne ""
Truth table100%3/3
A and B
A= defined $detectB= $detect ne ""
ABresult
0X0
100
111
425 }
426 16 ▶ croak 'GPU detection class from '.$origin.' must be a class name, not '.$detect
Branch: 1 branch
BranchTrueFalseTotal
if ref $detect 0 16 16
427 .'. Nothing was changed on the host'
428 if ref $detect;
429 16 return $class->nvidia_class->load_user_class($detect,
430 base => __PACKAGE__, what => 'GPU detection class', origin => $origin);
431 }
432  
433 956 ▶sub requirement_class { 'Rex::GPU::NVIDIA::Requirement' }
Subroutine requirement_class: called CC=1 SCAR=0.0
Pod documented
434  
435 89 ▶sub vgpu_class { 'Rex::GPU::NVIDIA::VGPU' }
Subroutine vgpu_class: called CC=1 SCAR=0.0
Pod documented
436  
437 25 ▶sub nvidia_class { 'Rex::GPU::NVIDIA' }
Subroutine nvidia_class: called CC=1 SCAR=0.0
Pod documented
438  
439 34 ▶sub nvswitch_device_ids { sort keys %NVSWITCH_DEVICE_IDS }
Subroutine nvswitch_device_ids: called CC=1 SCAR=0.0
Pod documented
440  
441 84 ▶sub virtual_display_vendor_ids { @VIRTUAL_DISPLAY_VENDOR_IDS }
Subroutine virtual_display_vendor_ids: called CC=1 SCAR=0.0
Pod documented
442  
443 # Called as a method (Rex::GPU::Detect->detect, My::GPU::Detect->detect):
444 # the invocant, a class name or object that isa Rex::GPU::Detect. Called as a
445 # function (detect() from a Rexfile, Rex::GPU::Detect::detect(),
446 # open_kernel_module_required('2b85')): undef -- a device ID is no package of
447 # ours, and nothing is loaded to find out.
448 305 ▶sub _invocant {
Subroutine _invocant: called CC=5 SCAR=16.5
449 305 my ( $first ) = @_;
450 305 ▶ return unless defined $first && ( !ref $first || blessed $first );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first and !ref($first) || builtin::blessed($first) 57 248 305
Condition50%3/6
defined $first and !ref($first) || builtin::blessed($first)
!ll&&!rl&&r
570248
not ref $first or builtin::blessed($first)
l!l&&r!l&&!r
24800
MC/DC33%1/3
defined $first and !ref($first) || builtin::blessed($first)
defined $first not ref $first builtin::blessed($first)
Truth table50%2/4
A and not(B) or C
A= defined $firstB= not ref $firstC= builtin::blessed($first)
ABCresult
0XX0
1000
11X1
1011
451 248 ▶ return $first if eval { $first->isa(__PACKAGE__) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $first->isa(__PACKAGE__) } } 170 78 248
452 78 return;
453 }
454  
455 27 1;
456  
457 __END__
« lib/Rex/GPU/NVIDIA/Setup.pm ↑ Summary lib/Rex/GPU/NVIDIA/Setup/Ubuntu.pm »