lib/Rex/GPU/Detect/Sysfs.pm

filter: Statementstmt 100% 91 / 91 Branchbran 97.2% 35 / 36 Conditioncond 82.0% 32 / 39 MC/DCmcdc 66.6% 14 / 21 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 92.5% 187 / 202 CC 37 SCAR 21.2
CC
37
Cov
95%
detect
16.0
_nvswitches_from_sysfs
8.0
_parse_sysfs_pci
7.0
CRAP 8.3
« lib/Rex/GPU/NVIDIA/Requirement.pm ↑ Summary lib/Rex/GPU/NVIDIA.pm »
1 # ABSTRACT: GPU detection from sysfs, without lspci or pciutils
2  
3 package Rex::GPU::Detect::Sysfs;
4 3 our $VERSION = '0.006';
5 3 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Rex::Commands::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶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 3 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 42 ▶sub detect {
Subroutine detect: called CC=16 SCAR=27.7
Pod documented
31 42 ▶ my $class = Rex::GPU::Detect::_invocant(@_) // __PACKAGE__;
Condition100%2/2
Rex::GPU::Detect::_invocant(@_) // __PACKAGE__
l!l
411
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 42 my $devices = $class->_read_pci_devices;
33  
34 38 my $result = { nvidia => [], amd => [], nvswitch => [] };
35 38 my %virtual = map { lc($_) => 1 } $class->virtual_display_vendor_ids;
36 38 my $virtual = 0;
37 38 for my $dev (@$devices) {
38 # Classes 0380 (karr #75) and 1200 (karr k1) count for vendor 1002 only.
39 next unless $dev->{class} eq '0300' || $dev->{class} eq '0302'
40 51 ▶ || ( $dev->{class} =~ /\A(?:0380|1200)\z/ && $dev->{vendor} eq '1002' );
Branch: 1 branch
BranchTrueFalseTotal
unless $dev->{"class"} eq "0300" or $dev->{"class"} eq "0302" or $dev->{"class"} =~ /\A(?:0380|1200)\z/u and $dev->{"vendor"} eq 1002 9 42 51
Condition100%9/9
$dev->{"class"} eq "0300" or $dev->{"class"} eq "0302" or $dev->{"class"} =~ /\A(?:0380|1200)\z/u and $dev->{"vendor"} eq 1002
l!l&&r!l&&!r
3849
$dev->{"class"} eq "0300" or $dev->{"class"} eq "0302"
l!l&&r!l&&!r
34413
$dev->{"class"} =~ /\A(?:0380|1200)\z/u and $dev->{"vendor"} eq 1002
!ll&&!rl&&r
634
MC/DC100%4/4
$dev->{"class"} eq "0300" or $dev->{"class"} eq "0302" or $dev->{"class"} =~ /\A(?:0380|1200)\z/u and $dev->{"vendor"} eq 1002
$dev->{"class"} eq "0300" $dev->{"class"} eq "0302" $dev->{"class"} =~ /\A(?:0380|1200)\z/u $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"} =~ /\A(?:0380|1200)\z/uD= $dev->{"vendor"} eq 1002
ABCDresult
1XXX1
01XX1
00111
000X0
00100
41 42 ▶ if ($dev->{vendor} eq '10de') {
Branch: 3 branches
BranchTrueFalseTotal
if ($dev->{'vendor'} eq '10de') { } 31 11 42
elsif ($dev->{'vendor'} eq '1002') { } 8 3 11
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 8 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 38 ▶ if $virtual && !@{$result->{nvidia}} && !@{$result->{amd}};
Branch: 1 branch
BranchTrueFalseTotal
if $virtual and not @{$result->{"nvidia"};} and not @{$result->{"amd"};} 1 37 38
Condition83%5/6
$virtual and not @{$result->{"nvidia"};} and not @{$result->{"amd"};}
!ll&&!rl&&r
3701
$virtual and not @{$result->{"nvidia"};}
!ll&&!rl&&r
3611
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: the AMD driver state only where there is an AMD GPU.
58 38 ▶ $class->_resolve_amd($result->{amd}) if @{$result->{amd}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$result->{"amd"};} 8 30 38
59  
60 # As with lspci: NVSwitches only where there is an NVIDIA GPU.
61 38 ▶ $result->{nvswitch} = $class->_nvswitches_from_sysfs($devices) if @{$result->{nvidia}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$result->{"nvidia"};} 24 14 38
62  
63 38 return $result;
64 }
65  
66 42 ▶sub _read_pci_devices {
Subroutine _read_pci_devices: called CC=3 SCAR=11.0
67 42 my ( $class ) = @_;
68 42 my $out = run $SYSFS_READ, auto_die => 0;
69 42 ▶ croak 'GPU detection from sysfs: could not read /sys/bus/pci/devices on the host (exit '
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 1 41 42
70 .( $? >> 8 ).') -- no GPU list is reported for a host that could not be looked at. '
71 .'Nothing was changed on the host'
72 if $? != 0;
73 41 my $devices = $class->_parse_sysfs_pci($out);
74 38 ▶ Rex::Logger::info(' [info] no PCI device listed under /sys/bus/pci/devices') unless @$devices;
Branch: 1 branch
BranchTrueFalseTotal
unless @$devices 1 37 38
75 38 return $devices;
76 }
77  
78 # The lines of $SYSFS_READ => [ { slot, class, vendor, device,
79 # subsystem_vendor, subsystem_device } ] sorted by slot; class the four hex
80 # digits of base class and subclass ("0300"), the IDs four lowercase hex
81 # digits or undef. Dies for a line it cannot use and for an unreadable class
82 # or vendor: that device could be the GPU.
83 41 ▶sub _parse_sysfs_pci {
Subroutine _parse_sysfs_pci: called CC=7 SCAR=19.5
84 41 my ( $class, $out ) = @_;
85 41 my @devices;
86 41 ▶ for my $line (split /\n/, $out // '') {
Condition50%1/2
$out // ""
l!l
410
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
87 54 ▶ next unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/u 0 54 54
88 54 my @field = split ' ', $line;
89 54 ▶ croak 'GPU detection from sysfs: unexpected line from /sys/bus/pci/devices: \''.$line
Branch: 1 branch
BranchTrueFalseTotal
unless @field == 6 1 53 54
90 .'\' -- no GPU list is reported. Nothing was changed on the host'
91 unless @field == 6;
92 53 my ( $slot, $pci_class, $vendor, $device, $sub_vendor, $sub_device ) = @field;
93 53 my ( $class_code ) = $pci_class =~ /\A0x([0-9a-f]{4})[0-9a-f]{2}\z/i;
94 53 my $vendor_id = $class->_sysfs_id($vendor);
95 53 ▶ 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 51 53
Condition100%3/3
defined $class_code and defined $vendor_id
!ll&&!rl&&r
1151
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
96 .'(class '.$pci_class.', vendor '.$vendor.'), so whether it is a GPU is unknown -- '
97 .'no GPU list is reported; run again if the device was being removed. Nothing '
98 .'was changed on the host'
99 unless defined $class_code && defined $vendor_id;
100 51 push @devices, {
101 slot => $slot,
102 class => lc $class_code,
103 vendor => $vendor_id,
104 device => $class->_sysfs_id($device),
105 subsystem_vendor => $class->_sysfs_id($sub_vendor),
106 subsystem_device => $class->_sysfs_id($sub_device)
107 };
108 }
109 38 return [ sort { $a->{slot} cmp $b->{slot} } @devices ];
110 }
111  
112 # "0x10de" => "10de"; "-" (unreadable) or anything malformed => undef. Always
113 # one value, also in the list of a hash constructor.
114 206 ▶sub _sysfs_id {
Subroutine _sysfs_id: called CC=3 SCAR=11.1
115 206 my ( $class, $value ) = @_;
116 206 ▶ 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 ? : 199 7 206
Condition66%2/3
defined $value and $value =~ /\A0x([0-9a-f]{4})\z/iu
!ll&&!rl&&r
07199
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
117 }
118  
119 31 ▶sub _nvidia_gpu_from_sysfs {
Subroutine _nvidia_gpu_from_sysfs: called CC=4 SCAR=13.9
120 31 my ( $class, $dev ) = @_;
121 31 my $device_id = $dev->{device};
122 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
123 31 my $compute = $class->_is_nvidia_compute($dev->{class}, $name, $device_id);
124 31 ▶ my $status = !defined $compute ? 'undecided' : $compute ? 'ok' : 'skip';
Branch: 2 branches
BranchTrueFalseTotal
!defined($compute) ? : 5 26 31
$compute ? : 21 5 26
125 Rex::Logger::info(' ['.$status.'] NVIDIA: '.$name.' (PCI class '.$dev->{class}.', '
126 31 .$dev->{slot}.')');
127 my $gpu = {
128 name => $name,
129 vendor => 'nvidia',
130 pci_class => $dev->{class},
131 compute => $compute,
132 device_id => $device_id,
133 subsystem_vendor_id => $dev->{subsystem_vendor},
134 subsystem_id => $dev->{subsystem_device}
135 31 };
136 31 $class->_mark_vgpu($gpu);
137 31 return $gpu;
138 }
139  
140 # The lspci path's name rules need a name; sysfs has none. Where the
141 # generation row and the PCI class left compute open, it stays open: undef,
142 # never a guess (the safe default: gpu_setup installs nothing for it).
143 6 ▶sub _nvidia_compute_by_name {
Subroutine _nvidia_compute_by_name: called CC=1 SCAR=0.0
144 6 my ( $class, $pci_class, $name, $device_id ) = @_;
145 6 Rex::Logger::info(' '.$name.': no generation row for its device ID and no name in '
146 .'sysfs to judge it by -- compute left undecided (undef)', 'warn');
147 6 return;
148 }
149  
150 # The same element as the lspci path (Rex::GPU::Detect::_amd_gpu); the status
151 # line comes from _resolve_amd, once the driver state is read.
152 8 ▶sub _amd_gpu_from_sysfs {
Subroutine _amd_gpu_from_sysfs: called CC=2 SCAR=7.2
153 8 my ( $class, $dev ) = @_;
154 return $class->_amd_gpu(
155 name => 'AMD GPU [1002:'.( $dev->{device} // '????' ).']',
156 pci_class => $dev->{class},
157 slot => $class->amd_class->normalize_slot($dev->{slot}),
158 device_id => $dev->{device},
159 8 ▶ );
Condition50%1/2
$dev->{"device"} // "????"
l!l
80
MC/DC0%0/1
$dev->{"device"} // "????"
$dev->{"device"}
Truth table50%1/2
A
A= $dev->{"device"}
Aresult
11
00
160 }
161  
162 24 ▶sub _nvswitches_from_sysfs {
Subroutine _nvswitches_from_sysfs: called CC=8 SCAR=20.8
163 24 my ( $class, $devices ) = @_;
164 24 my %known = map { lc($_) => 1 } $class->nvswitch_device_ids;
165 24 my @switches;
166 24 for my $dev (@$devices) {
167 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
168 4 my $id = $dev->{device};
169 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
170 Rex::Logger::info(' [skip] NVIDIA bridge device not known as an NVSwitch: 10de:'
171 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
172 .'NVSwitch device ID counts)');
173 2 next;
174 }
175 2 push @switches, {
176 name => 'NVIDIA NVSwitch [10de:'.$id.']',
177 vendor => 'nvidia',
178 pci_class => '0680',
179 device_id => $id
180 };
181 }
182 24 ▶ Rex::Logger::info(' [ok] NVSwitch: '.scalar(@switches).' ('.$switches[0]{name}.')')
Branch: 1 branch
BranchTrueFalseTotal
if @switches 2 22 24
183 if @switches;
184 24 return \@switches;
185 }
186  
187 3 1;
188  
189 __END__
« lib/Rex/GPU/NVIDIA/Requirement.pm ↑ Summary lib/Rex/GPU/NVIDIA.pm »