lib/Rex/GPU/Detect/Sysfs.pm

filter: Statementstmt 100% 91 / 91 Branchbran 97.0% 33 / 34 Conditioncond 82.0% 32 / 39 MC/DCmcdc 66.6% 14 / 21 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 92.5% 185 / 200 CC 36 SCAR 20.6
CC
36
Cov
95%
detect
15.0
_nvswitches_from_sysfs
8.0
_parse_sysfs_pci
7.0
CRAP 7.8
« lib/Rex/GPU/NVIDIA/Setup/Debian.pm ↑ Summary lib/Rex/GPU/NVIDIA/Setup.pm »
1 # ABSTRACT: GPU detection from sysfs, without lspci or pciutils
2  
3 package Rex::GPU::Detect::Sysfs;
4 2 our $VERSION = '0.005';
5 2 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Rex::Commands::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use parent 'Rex::GPU::Detect';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # karr #73 (kubernetes-ocp k196): the same result as Rex::GPU::Detect::detect,
15 # read from /sys/bus/pci/devices instead of lspci -- nothing is installed to
16 # detect. One read-only command, shell builtins only (no fork per file; an
17 # HGX host has hundreds of PCI functions), over the exec channel: these hosts
18 # have no SFTP, so no Rex file command. Per device one line: the directory
19 # name, then class, vendor, device, subsystem_vendor, subsystem_device as the
20 # kernel prints them (0x030000, 0x10de, ...), "-" for a file it could not
21 # read. A missing or unreadable directory exits 3; an empty one prints
22 # nothing and exits 0. A dangling entry (device removed meanwhile) is skipped.
23 2 my $SYSFS_READ = q{cd /sys/bus/pci/devices && test -r . || exit 3; }
24 .q{for d in *; do test -e "$d" || continue; l=$d; }
25 .q{for f in class vendor device subsystem_vendor subsystem_device; do }
26 .q{v=; read -r v 2>/dev/null <"$d/$f"; l="$l ${v:--}"; done; }
27 .q{printf '%s\n' "$l"; done};
28  
29  
30 35 ▶sub detect {
Subroutine detect: called CC=15 SCAR=27.1
Pod documented
31 35 ▶ my $class = Rex::GPU::Detect::_invocant(@_) // __PACKAGE__;
Condition100%2/2
Rex::GPU::Detect::_invocant(@_) // __PACKAGE__
l!l
341
MC/DC100%1/1
Rex::GPU::Detect::_invocant(@_) // __PACKAGE__
Rex::GPU::Detect::_invocant(@_)
Truth table100%2/2
A
A= Rex::GPU::Detect::_invocant(@_)
Aresult
11
00
32 35 my $devices = $class->_read_pci_devices;
33  
34 31 my $result = { nvidia => [], amd => [], nvswitch => [] };
35 31 my %virtual = map { lc($_) => 1 } $class->virtual_display_vendor_ids;
36 31 my $virtual = 0;
37 31 for my $dev (@$devices) {
38 # Class 0380 (AMD Instinct, karr #75) counts for vendor 1002 only.
39 next unless $dev->{class} eq '0300' || $dev->{class} eq '0302'
40 42 ▶ || ( $dev->{class} eq '0380' && $dev->{vendor} eq '1002' );
Branch: 1 branch
BranchTrueFalseTotal
unless $dev->{"class"} eq "0300" or $dev->{"class"} eq "0302" or $dev->{"class"} eq "0380" and $dev->{"vendor"} eq 1002 6 36 42
Condition100%9/9
$dev->{"class"} eq "0300" or $dev->{"class"} eq "0302" or $dev->{"class"} eq "0380" and $dev->{"vendor"} eq 1002
l!l&&r!l&&!r
3516
$dev->{"class"} eq "0300" or $dev->{"class"} eq "0302"
l!l&&r!l&&!r
3147
$dev->{"class"} eq "0380" and $dev->{"vendor"} eq 1002
!ll&&!rl&&r
511
MC/DC100%4/4
$dev->{"class"} eq "0300" or $dev->{"class"} eq "0302" or $dev->{"class"} eq "0380" and $dev->{"vendor"} eq 1002
$dev->{"class"} eq "0300" $dev->{"class"} eq "0302" $dev->{"class"} eq "0380" $dev->{"vendor"} eq 1002
Truth table100%5/5
A or B or C and D
A= $dev->{"class"} eq "0300"B= $dev->{"class"} eq "0302"C= $dev->{"class"} eq "0380"D= $dev->{"vendor"} eq 1002
ABCDresult
1XXX1
01XX1
00111
000X0
00100
41 36 ▶ if ($dev->{vendor} eq '10de') {
Branch: 3 branches
BranchTrueFalseTotal
if ($dev->{'vendor'} eq '10de') { } 31 5 36
elsif ($dev->{'vendor'} eq '1002') { } 2 3 5
elsif ($virtual{$dev->{'vendor'}}) { } 2 1 3
42 31 push @{$result->{nvidia}}, $class->_nvidia_gpu_from_sysfs($dev);
43 }
44 elsif ($dev->{vendor} eq '1002') {
45 2 push @{$result->{amd}}, $class->_amd_gpu_from_sysfs($dev);
46 }
47 elsif ($virtual{ $dev->{vendor} }) {
48 2 $virtual++;
49 Rex::Logger::info(' [skip] virtual display: '.$dev->{vendor}.':'
50 2 ▶ .( $dev->{device} // '????' ).' at '.$dev->{slot});
Condition50%1/2
$dev->{"device"} // "????"
l!l
20
MC/DC0%0/1
$dev->{"device"} // "????"
$dev->{"device"}
Truth table50%1/2
A
A= $dev->{"device"}
Aresult
11
00
51 }
52 }
53  
54 Rex::Logger::info('Virtual GPU detected (virtio/QEMU/VMware/VBox) -- skipping')
55 31 ▶ if $virtual && !@{$result->{nvidia}} && !@{$result->{amd}};
Branch: 1 branch
BranchTrueFalseTotal
if $virtual and not @{$result->{"nvidia"};} and not @{$result->{"amd"};} 1 30 31
Condition83%5/6
$virtual and not @{$result->{"nvidia"};} and not @{$result->{"amd"};}
!ll&&!rl&&r
3001
$virtual and not @{$result->{"nvidia"};}
!ll&&!rl&&r
2911
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
56  
57 # As with lspci: NVSwitches only where there is an NVIDIA GPU.
58 31 ▶ $result->{nvswitch} = $class->_nvswitches_from_sysfs($devices) if @{$result->{nvidia}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$result->{"nvidia"};} 24 7 31
59  
60 31 return $result;
61 }
62  
63 35 ▶sub _read_pci_devices {
Subroutine _read_pci_devices: called CC=3 SCAR=11.0
64 35 my ( $class ) = @_;
65 35 my $out = run $SYSFS_READ, auto_die => 0;
66 35 ▶ croak 'GPU detection from sysfs: could not read /sys/bus/pci/devices on the host (exit '
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 1 34 35
67 .( $? >> 8 ).') -- no GPU list is reported for a host that could not be looked at. '
68 .'Nothing was changed on the host'
69 if $? != 0;
70 34 my $devices = $class->_parse_sysfs_pci($out);
71 31 ▶ Rex::Logger::info(' [info] no PCI device listed under /sys/bus/pci/devices') unless @$devices;
Branch: 1 branch
BranchTrueFalseTotal
unless @$devices 1 30 31
72 31 return $devices;
73 }
74  
75 # The lines of $SYSFS_READ => [ { slot, class, vendor, device,
76 # subsystem_vendor, subsystem_device } ] sorted by slot; class the four hex
77 # digits of base class and subclass ("0300"), the IDs four lowercase hex
78 # digits or undef. Dies for a line it cannot use and for an unreadable class
79 # or vendor: that device could be the GPU.
80 34 ▶sub _parse_sysfs_pci {
Subroutine _parse_sysfs_pci: called CC=7 SCAR=19.5
81 34 my ( $class, $out ) = @_;
82 34 my @devices;
83 34 ▶ for my $line (split /\n/, $out // '') {
Condition50%1/2
$out // ""
l!l
340
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
84 45 ▶ next unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/u 0 45 45
85 45 my @field = split ' ', $line;
86 45 ▶ croak 'GPU detection from sysfs: unexpected line from /sys/bus/pci/devices: \''.$line
Branch: 1 branch
BranchTrueFalseTotal
unless @field == 6 1 44 45
87 .'\' -- no GPU list is reported. Nothing was changed on the host'
88 unless @field == 6;
89 44 my ( $slot, $pci_class, $vendor, $device, $sub_vendor, $sub_device ) = @field;
90 44 my ( $class_code ) = $pci_class =~ /\A0x([0-9a-f]{4})[0-9a-f]{2}\z/i;
91 44 my $vendor_id = $class->_sysfs_id($vendor);
92 44 ▶ croak 'GPU detection from sysfs: PCI device '.$slot.' has an unreadable class or vendor '
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class_code and defined $vendor_id 2 42 44
Condition100%3/3
defined $class_code and defined $vendor_id
!ll&&!rl&&r
1142
MC/DC100%2/2
defined $class_code and defined $vendor_id
defined $class_code defined $vendor_id
Truth table100%3/3
A and B
A= defined $class_codeB= defined $vendor_id
ABresult
0X0
100
111
93 .'(class '.$pci_class.', vendor '.$vendor.'), so whether it is a GPU is unknown -- '
94 .'no GPU list is reported; run again if the device was being removed. Nothing '
95 .'was changed on the host'
96 unless defined $class_code && defined $vendor_id;
97 42 push @devices, {
98 slot => $slot,
99 class => lc $class_code,
100 vendor => $vendor_id,
101 device => $class->_sysfs_id($device),
102 subsystem_vendor => $class->_sysfs_id($sub_vendor),
103 subsystem_device => $class->_sysfs_id($sub_device)
104 };
105 }
106 31 return [ sort { $a->{slot} cmp $b->{slot} } @devices ];
107 }
108  
109 # "0x10de" => "10de"; "-" (unreadable) or anything malformed => undef. Always
110 # one value, also in the list of a hash constructor.
111 170 ▶sub _sysfs_id {
Subroutine _sysfs_id: called CC=3 SCAR=11.1
112 170 my ( $class, $value ) = @_;
113 170 ▶ return defined $value && $value =~ /\A0x([0-9a-f]{4})\z/i ? lc $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $value && $value =~ /\A0x([0-9a-f]{4})\z/iu ? : 163 7 170
Condition66%2/3
defined $value and $value =~ /\A0x([0-9a-f]{4})\z/iu
!ll&&!rl&&r
07163
MC/DC50%1/2
defined $value and $value =~ /\A0x([0-9a-f]{4})\z/iu
defined $value $value =~ /\A0x([0-9a-f]{4})\z/iu
Truth table66%2/3
A and B
A= defined $valueB= $value =~ /\A0x([0-9a-f]{4})\z/iu
ABresult
0X0
100
111
114 }
115  
116 31 ▶sub _nvidia_gpu_from_sysfs {
Subroutine _nvidia_gpu_from_sysfs: called CC=4 SCAR=13.9
117 31 my ( $class, $dev ) = @_;
118 31 my $device_id = $dev->{device};
119 31 ▶ my $name = 'NVIDIA GPU [10de:'.( $device_id // '????' ).']';
Condition100%2/2
$device_id // "????"
l!l
292
MC/DC100%1/1
$device_id // "????"
$device_id
Truth table100%2/2
A
A= $device_id
Aresult
11
00
120 31 my $compute = $class->_is_nvidia_compute($dev->{class}, $name, $device_id);
121 31 ▶ my $status = !defined $compute ? 'undecided' : $compute ? 'ok' : 'skip';
Branch: 2 branches
BranchTrueFalseTotal
!defined($compute) ? : 5 26 31
$compute ? : 21 5 26
122 Rex::Logger::info(' ['.$status.'] NVIDIA: '.$name.' (PCI class '.$dev->{class}.', '
123 31 .$dev->{slot}.')');
124 my $gpu = {
125 name => $name,
126 vendor => 'nvidia',
127 pci_class => $dev->{class},
128 compute => $compute,
129 device_id => $device_id,
130 subsystem_vendor_id => $dev->{subsystem_vendor},
131 subsystem_id => $dev->{subsystem_device}
132 31 };
133 31 $class->_mark_vgpu($gpu);
134 31 return $gpu;
135 }
136  
137 # The lspci path's name rules need a name; sysfs has none. Where the
138 # generation row and the PCI class left compute open, it stays open: undef,
139 # never a guess (the safe default: gpu_setup installs nothing for it).
140 6 ▶sub _nvidia_compute_by_name {
Subroutine _nvidia_compute_by_name: called CC=1 SCAR=0.0
141 6 my ( $class, $pci_class, $name, $device_id ) = @_;
142 6 Rex::Logger::info(' '.$name.': no generation row for its device ID and no name in '
143 .'sysfs to judge it by -- compute left undecided (undef)', 'warn');
144 6 return;
145 }
146  
147 2 ▶sub _amd_gpu_from_sysfs {
Subroutine _amd_gpu_from_sysfs: called CC=2 SCAR=7.0
148 2 my ( $class, $dev ) = @_;
149 2 ▶ my $name = 'AMD GPU [1002:'.( $dev->{device} // '????' ).']';
Condition50%1/2
$dev->{"device"} // "????"
l!l
20
MC/DC0%0/1
$dev->{"device"} // "????"
$dev->{"device"}
Truth table50%1/2
A
A= $dev->{"device"}
Aresult
11
00
150 2 Rex::Logger::info(' [info] AMD: '.$name.' (PCI class '.$dev->{class}.', '.$dev->{slot}.')');
151 return {
152 name => $name,
153 vendor => 'amd',
154 pci_class => $dev->{class},
155 2 compute => 0 # AMD compute support not yet implemented
156 };
157 }
158  
159 24 ▶sub _nvswitches_from_sysfs {
Subroutine _nvswitches_from_sysfs: called CC=8 SCAR=20.8
160 24 my ( $class, $devices ) = @_;
161 24 my %known = map { lc($_) => 1 } $class->nvswitch_device_ids;
162 24 my @switches;
163 24 for my $dev (@$devices) {
164 36 ▶ next unless $dev->{vendor} eq '10de' && $dev->{class} eq '0680';
Branch: 1 branch
BranchTrueFalseTotal
unless $dev->{"vendor"} eq "10de" and $dev->{"class"} eq "0680" 32 4 36
Condition100%3/3
$dev->{"vendor"} eq "10de" and $dev->{"class"} eq "0680"
!ll&&!rl&&r
1314
MC/DC100%2/2
$dev->{"vendor"} eq "10de" and $dev->{"class"} eq "0680"
$dev->{"vendor"} eq "10de" $dev->{"class"} eq "0680"
Truth table100%3/3
A and B
A= $dev->{"vendor"} eq "10de"B= $dev->{"class"} eq "0680"
ABresult
0X0
100
111
165 4 my $id = $dev->{device};
166 4 ▶ unless (defined $id && $known{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id and $known{$id}) 2 2 4
Condition66%2/3
defined $id and $known{$id}
!ll&&!rl&&r
022
MC/DC50%1/2
defined $id and $known{$id}
defined $id $known{$id}
Truth table66%2/3
A and B
A= defined $idB= $known{$id}
ABresult
0X0
100
111
167 Rex::Logger::info(' [skip] NVIDIA bridge device not known as an NVSwitch: 10de:'
168 2 ▶ .( $id // '????' ).' at '.$dev->{slot}.' (sysfs has no name; only a known '
Condition50%1/2
$id // "????"
l!l
20
MC/DC0%0/1
$id // "????"
$id
Truth table50%1/2
A
A= $id
Aresult
11
00
169 .'NVSwitch device ID counts)');
170 2 next;
171 }
172 2 push @switches, {
173 name => 'NVIDIA NVSwitch [10de:'.$id.']',
174 vendor => 'nvidia',
175 pci_class => '0680',
176 device_id => $id
177 };
178 }
179 24 ▶ Rex::Logger::info(' [ok] NVSwitch: '.scalar(@switches).' ('.$switches[0]{name}.')')
Branch: 1 branch
BranchTrueFalseTotal
if @switches 2 22 24
180 if @switches;
181 24 return \@switches;
182 }
183  
184 2 1;
185  
186 __END__
« lib/Rex/GPU/NVIDIA/Setup/Debian.pm ↑ Summary lib/Rex/GPU/NVIDIA/Setup.pm »