lib/Rex/GPU/AMD.pm

filter: Statementstmt 99.1% 367 / 370 Branchbran 93.4% 185 / 198 Conditioncond 71.8% 107 / 149 MC/DCmcdc 57.1% 48 / 84 Subroutinesub 100% 47 / 47 Podpod 100% 19 / 19 totaltotal 89.1% 773 / 867 CC 170 SCAR 22.6
CC
170
Cov
92%
parse_state
20.2
install_rocm
17.1
state_problems
16.7
CRAP 9.6
« lib/Rex/GPU/AMD/Hardware.pm ↑ Summary lib/Rex/GPU/NVIDIA/Requirement.pm »
1 # ABSTRACT: AMD GPU driver (amdgpu) and ROCm management (experimental)
2  
3 package Rex::GPU::AMD;
4 40 our $VERSION = '0.006';
5 40 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Rex::Commands::Run ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use Rex::Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use Rex::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 40 ▶use Rex::GPU::NVIDIA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 40 ▶use Rex::GPU::AMD::Hardware;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 40 ▶use Rex::GPU::AMD::Setup;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 40 ▶use Rex::GPU::AMD::Setup::Apt;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 40 ▶use Rex::GPU::AMD::Setup::Debian;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 40 ▶use Rex::GPU::AMD::Setup::RHEL;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 40 ▶use Rex::GPU::AMD::Setup::Ubuntu;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # karr k1. No Rex::Exporter: the functions are called fully qualified
23 # (Rex::GPU::AMD::install_driver(...), the contract with kubernetes-ocp) or
24 # as class methods, so a Rexfile that also uses Rex::GPU::NVIDIA keeps its
25 # install_driver.
26 #
27 # Every host command of this module and its setup classes goes through ONE
28 # method, host_run: a subclass that overrides it sees (and may refuse) every
29 # command, and the golden harness records them. Files are written with
30 # printf + mv over the exec channel, never through Rex's file commands: these
31 # hosts have no SFTP subsystem.
32  
33 # True while Rex::GPU::gpu_setup runs install_driver (local there, k90): its
34 # die for GPUs whose rows disagree then names gpu_setup's ways out. Internal:
35 # no option, so install_driver -- a subclass's override included -- gets the
36 # arguments it always got from gpu_setup.
37 40 our $GPU_SETUP;
38  
39 # The driver state, one read-only command with shell builtins only (a fork
40 # only per AMD device for its driver/render symlink). Lines:
41 # amdgpu 0|1 /sys/module/amdgpu exists
42 # kfd 0|1 /dev/kfd is a character device
43 # pci SLOT CLASS DEVICE DRV every vendor 0x1002 device of class 0x03xxxx
44 # or 0x1200xx; DRV the bound driver or "-"
45 # vram SLOT BYTES / gtt SLOT BYTES mem_info_vram_total / mem_info_gtt_total
46 # render renderDN VENDOR SLOT every /sys/class/drm/renderD*
47 # node N / prop KEY VALUE KFD topology nodes, decimal properties
48 # end the read got to its end
49 40 my $STATE_SCRIPT =
50 q{[ -d /sys/module/amdgpu ] && echo 'amdgpu 1' || echo 'amdgpu 0'; }
51 . q{[ -c /dev/kfd ] && echo 'kfd 1' || echo 'kfd 0'; }
52 . q{for d in /sys/bus/pci/devices/*; do [ -r "$d/vendor" ] && [ -r "$d/class" ] || continue; }
53 . q{read -r v < "$d/vendor"; [ "$v" = 0x1002 ] || continue; read -r c < "$d/class"; }
54 . q{case $c in 0x03*|0x1200*) ;; *) continue;; esac; i=-; [ -r "$d/device" ] && read -r i < "$d/device"; }
55 . q{r=-; [ -e "$d/driver" ] && r=$(cd -P "$d/driver" 2>/dev/null && echo "${PWD##*/}"); }
56 . q{echo "pci ${d##*/} $c $i ${r:--}"; }
57 . q{[ -r "$d/mem_info_vram_total" ] && read -r m < "$d/mem_info_vram_total" && echo "vram ${d##*/} $m"; }
58 . q{[ -r "$d/mem_info_gtt_total" ] && read -r m < "$d/mem_info_gtt_total" && echo "gtt ${d##*/} $m"; }
59 . q{done; }
60 . q{for d in /sys/class/drm/renderD*; do [ -r "$d/device/vendor" ] || continue; read -r v < "$d/device/vendor"; }
61 . q{s=$(cd -P "$d/device" 2>/dev/null && echo "${PWD##*/}"); echo "render ${d##*/} $v ${s:--}"; done; }
62 . q{for n in /sys/class/kfd/kfd/topology/nodes/*; do [ -r "$n/properties" ] || continue; echo "node ${n##*/}"; }
63 . q{while read -r k v; do case $k in gfx_target_version|vendor_id|device_id|location_id|domain|drm_render_minor) }
64 . q{echo "prop $k $v";; esac; done < "$n/properties"; done; }
65 . q{echo end};
66  
67  
68 458 ▶sub install_driver {
Subroutine install_driver: called CC=14 SCAR=26.4
Pod documented
69 458 ▶ my $class = _invocant(@_) // return __PACKAGE__->configured_class->install_driver(@_);
Condition100%2/2
_invocant(@_) // (return __PACKAGE__->configured_class->install_driver(@_))
l!l
252206
MC/DC100%1/1
_invocant(@_) // (return __PACKAGE__->configured_class->install_driver(@_))
_invocant(@_)
Truth table100%2/2
A
A= _invocant(@_)
Aresult
11
00
70 252 my ( undef, %opts ) = @_;
71 252 $class->_check_options('install_driver', \%opts, qw( gpus method reboot ));
72 250 ▶ my $method = $opts{method} // 'auto';
Condition100%2/2
$opts{"method"} // "auto"
l!l
19159
MC/DC100%1/1
$opts{"method"} // "auto"
$opts{"method"}
Truth table100%2/2
A
A= $opts{"method"}
Aresult
11
00
73 250 ▶ die "install_driver: method must be 'auto', 'inbox' or 'dkms', not '$method'. "
Branch: 1 branch
BranchTrueFalseTotal
unless $method =~ /\A(?:auto|inbox|dkms)\z/u 1 249 250
74 ."Nothing was changed on the host\n"
75 unless $method =~ /\A(?:auto|inbox|dkms)\z/;
76 249 my $gpus = $class->_check_gpus('install_driver', $opts{gpus});
77  
78 242 my $state = $class->read_state;
79 242 ▶ die "install_driver: could not read the AMD driver state from the host (sysfs, KFD "
Branch: 1 branch
BranchTrueFalseTotal
unless $state 3 239 242
80 ."topology). Nothing was changed on the host\n"
81 unless $state;
82 239 my @targets = $class->_targets($state, $gpus);
83 239 ▶ die "install_driver: no AMD GPU found on the host (vendor 1002, PCI class 03xx or 1200 "
Branch: 1 branch
BranchTrueFalseTotal
unless @targets 1 238 239
84 ."under /sys/bus/pci/devices). Nothing was changed on the host\n"
85 unless @targets;
86 238 for my $missing (grep { $_->{missing} } @targets) {
87 3 die 'install_driver: '.$class->gpu_label($missing).' is not on the host. '
88 ."Nothing was changed on the host\n";
89 }
90 235 $class->_check_method($method, \@targets);
91  
92 # A working driver is never touched, whatever the method (karr k1): an
93 # explicit method that is not the working driver's only gets a warning.
94 233 ▶ unless ($class->state_problems($state, \@targets)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->state_problems($state, \@targets)) 12 221 233
95 12 ▶ if ($method ne 'auto') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method ne "auto") 6 6 12
96 6 ▶ my $source = $class->module_source // '';
Condition100%2/2
$class->module_source // ""
l!l
51
MC/DC100%1/1
$class->module_source // ""
$class->module_source
Truth table100%2/2
A
A= $class->module_source
Aresult
11
00
97 6 ▶ if ($source ne $method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source ne $method) 3 3 6
98 3 ▶ my $have = { inbox => 'inbox ', dkms => 'DKMS ' }->{$source} // '';
Condition100%2/2
{"inbox", "inbox ", "dkms", "DKMS "}->{$source} // ""
l!l
21
MC/DC100%1/1
{"inbox", "inbox ", "dkms", "DKMS "}->{$source} // ""
{"inbox", "inbox ", "dkms", "DKMS "}->{$source}
Truth table100%2/2
A
A= {"inbox", "inbox ", "dkms", "DKMS "}->{$source}
Aresult
11
00
99 3 Rex::Logger::info("install_driver: method => '$method' asked, but a working ${have}amdgpu "
100 .'already drives the GPUs; install_driver never replaces a working driver. Nothing was '
101 .'changed on the host', 'warn');
102 }
103 }
104 12 Rex::Logger::info('AMD driver already active: '.join(', ', map { $class->gpu_label($_) } @targets));
105 12 return 'present';
106 }
107  
108 221 ▶ $class->_warn_inbox_for_dkms(\@targets) if $method eq 'inbox';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "inbox" 30 191 221
109 221 my $setup = $class->setup_for(gpus => \@targets, state => $state);
110 220 ▶ $method = $class->_auto_method(\@targets, $setup) if $method eq 'auto';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "auto" 38 182 220
111 Rex::Logger::info("Installing the AMD driver ($method) for "
112 198 .join(', ', map { $class->gpu_label($_) } @targets));
113 198 my $installed = $setup->install($method);
114 46 return $class->_activate($method, \@targets, $state, $installed, $opts{reboot});
115 }
116  
117 # Before the present check: method => 'dkms' for a GPU AMD supports only
118 # with the inbox driver dies, also on a host where the driver works.
119 235 ▶sub _check_method {
Subroutine _check_method: called CC=4 SCAR=13.9
120 235 my ( $class, $method, $targets ) = @_;
121 235 ▶ return unless $method eq 'dkms';
Branch: 1 branch
BranchTrueFalseTotal
unless $method eq "dkms" 78 157 235
122 157 ▶ my @inbox = grep { ( $_->{driver} // '' ) eq 'inbox' } @$targets;
Condition50%1/2
$_->{"driver"} // ""
l!l
1570
MC/DC0%0/1
$_->{"driver"} // ""
$_->{"driver"}
Truth table50%1/2
A
A= $_->{"driver"}
Aresult
11
00
123 die "install_driver: method => 'dkms', but AMD supports "
124 157 ▶ .join(', ', map { $class->gpu_label($_) } @inbox)
Branch: 1 branch
BranchTrueFalseTotal
if @inbox 2 155 157
125 ." only with the distribution kernel's inbox amdgpu, not amdgpu-dkms. "
126 ."Nothing was changed on the host\n"
127 if @inbox;
128 155 return;
129 }
130  
131 # After the present check, only when the inbox driver is really installed
132 # for a GPU AMD supports with amdgpu-dkms.
133 30 ▶sub _warn_inbox_for_dkms {
Subroutine _warn_inbox_for_dkms: called CC=3 SCAR=11.1
134 30 my ( $class, $targets ) = @_;
135 Rex::Logger::info(' '.$class->gpu_label($_).": AMD supports it with amdgpu-dkms; using the "
136 ."inbox driver as asked (method => 'inbox')", 'warn')
137 30 ▶ for grep { ( $_->{driver} // '' ) eq 'dkms' } @$targets;
Condition50%1/2
$_->{"driver"} // ""
l!l
300
MC/DC0%0/1
$_->{"driver"} // ""
$_->{"driver"}
Truth table50%1/2
A
A= $_->{"driver"}
Aresult
11
00
138 30 return;
139 }
140  
141 38 ▶sub _auto_method {
Subroutine _auto_method: called CC=9 SCAR=22.0
142 38 my ( $class, $targets, $setup ) = @_;
143 38 my %by;
144 38 push @{ $by{ $_->{driver} } }, $class->gpu_label($_) for grep { defined $_->{driver} } @$targets;
145 22 $class->_die_mixed_drivers($setup, join(', ', @{ $by{inbox} }), join(', ', @{ $by{dkms} }),
146 44 ▶ !grep { ( $_->{driver} // '' ) eq 'inbox' && !$_->{integrated} } @$targets)
Condition80%4/5
($_->{'driver'} // '') eq 'inbox' && !$_->{'integrated'}
!ll&&!rl&&r
22202
$_->{"driver"} // ""
l!l
440
MC/DC50%1/2
($_->{'driver'} // '') eq 'inbox' && !$_->{'integrated'}
$_->{"driver"} !$_->{'integrated'}
Truth table66%2/3
A && not(B)
A= $_->{"driver"}B= !$_->{'integrated'}
ABresult
0X0
100
111
147 38 ▶ if $by{inbox} && $by{dkms};
Branch: 1 branch
BranchTrueFalseTotal
if $by{"inbox"} and $by{"dkms"} 22 16 38
Condition100%3/3
$by{"inbox"} and $by{"dkms"}
!ll&&!rl&&r
10622
MC/DC100%2/2
$by{"inbox"} and $by{"dkms"}
$by{"inbox"} $by{"dkms"}
Truth table100%3/3
A and B
A= $by{"inbox"}B= $by{"dkms"}
ABresult
0X0
100
111
148 16 ▶ return 'inbox' if $by{inbox};
Branch: 1 branch
BranchTrueFalseTotal
if $by{"inbox"} 6 10 16
149 10 ▶ return 'dkms' if $by{dkms};
Branch: 1 branch
BranchTrueFalseTotal
if $by{"dkms"} 9 1 10
150 1 my $method = $setup->fallback_method;
151 1 ▶ Rex::Logger::info(" no hardware row says which driver AMD supports for these GPUs: $method "
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'inbox' ? : 0 1 1
152 .( $method eq 'inbox' ? '(the running kernel has or can get amdgpu.ko)'
153 : '(the running kernel has no amdgpu.ko and cannot get one)' ));
154 1 return $method;
155 }
156  
157 # Rows that disagree under auto (karr k85): the way out it names must not
158 # die in turn. method => 'inbox' only where plan_inbox would let every GPU
159 # through (the same checks, host-read-only, in its order), else gpus => with
160 # the amdgpu-dkms GPUs only. On a host no driver may go to, that reason. A
161 # Radeon GPU outside AMD's matrix on this host (k82) is in both ways out --
162 # every GPU for inbox, and its row says dkms -- and both plans die for it:
163 # no way out is named, check_radeon dies as the plans would.
164 #
165 # Called from Rex::GPU::gpu_setup ($GPU_SETUP, k90), whose caller cannot pass
166 # method or gpus: its ways out first -- leaving out amd_integrated, only
167 # where that skips every inbox-only GPU ($apus: all of them integrated) --
168 # then install_driver called directly, with the hints above.
169 22 ▶sub _die_mixed_drivers {
Subroutine _die_mixed_drivers: called CC=9 SCAR=22.0
170 22 my ( $class, $setup, $inbox, $dkms, $apus ) = @_;
171 22 ▶ if (my $why = $setup->host_unsupported) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $why = $setup->host_unsupported) 2 20 22
172 2 die 'install_driver: '.$why.". Nothing was changed on the host\n";
173 }
174 20 $setup->check_radeon;
175 19 my $only = $setup->inbox_only_unsupported;
176 19 ▶ my $all = $only ? undef : $setup->inbox_unsupported;
Branch: 1 branch
BranchTrueFalseTotal
$only ? : 9 10 19
177 19 ▶ my $hint = ( $only ? "method => 'inbox' is no way out on this host for ".$inbox.': '.$only.'. '
Branch: 3 branches
BranchTrueFalseTotal
$only ? : 9 10 19
$all ? : 2 8 10
$only || $all ? : 11 8 19
Condition100%3/3
$only or $all
l!l&&r!l&&!r
928
MC/DC100%2/2
$only or $all
$only $all
Truth table100%3/3
A or B
A= $onlyB= $all
ABresult
1X1
011
000
178 : $all ? "method => 'inbox' is no way out on this host: ".$all.'. '
179 : "Pass method => 'inbox' to drive all of them with the inbox driver. " )
180 .( $only || $all ? 'Pass gpus => with '.$dkms.' only to leave '.$inbox.' out. ' : '' );
181 19 ▶ $hint = ( $apus ? "Leave out gpu_setup's amd_integrated => 1 to give the driver to ".$dkms
Branch: 2 branches
BranchTrueFalseTotal
if $Rex::GPU::AMD::GPU_SETUP 6 13 19
$apus ? : 4 2 6
182 .' only; gpu_setup then skips '.$inbox.' with a warning. Or call'
183 : 'Call' )
184 .' Rex::GPU::AMD->install_driver directly instead of gpu_setup: '.lcfirst($hint)
185 if $GPU_SETUP;
186 19 die 'install_driver: AMD supports '.$inbox.' only with the inbox driver and '.$dkms
187 .' with amdgpu-dkms, and one amdgpu drives every GPU of a host. '.$hint
188 ."Nothing was changed on the host\n";
189 }
190  
191 # After the install: load the module if it was not loaded, else (or if that
192 # was not enough) reboot -- only with reboot => 1, else warn and return.
193 46 ▶sub _activate {
Subroutine _activate: called CC=7 SCAR=19.5
194 46 my ( $class, $method, $targets, $before, $installed, $reboot ) = @_;
195 46 ▶ my $what = 'AMD driver ('.$method.') '.( $installed ? 'installed' : 'already installed' );
Branch: 1 branch
BranchTrueFalseTotal
$installed ? : 42 4 46
196 46 my @problems;
197 46 ▶ unless ($before->{amdgpu}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($before->{"amdgpu"}) 42 4 46
198 42 $class->host_run('modprobe amdgpu');
199 42 @problems = $class->state_problems($class->read_state, $targets);
200 42 ▶ unless (@problems) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@problems) 41 1 42
201 41 Rex::Logger::info($what.', amdgpu loaded and verified');
202 41 return $method;
203 }
204 }
205 5 ▶ unless ($reboot) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reboot) 2 3 5
206 2 ▶ Rex::Logger::info($what.', not active yet'.( @problems ? ' ('.join('; ', @problems).')' : '' )
Branch: 1 branch
BranchTrueFalseTotal
@problems ? : 1 1 2
207 .' -- reboot the host (or pass reboot => 1) to activate it', 'warn');
208 2 return $method;
209 }
210 3 $class->reboot_and_wait;
211 2 @problems = $class->state_problems($class->read_state, $targets);
212 2 ▶ die $what.', but not usable after the reboot: '.join('; ', @problems)."\n" if @problems;
Branch: 1 branch
BranchTrueFalseTotal
if @problems 1 1 2
213 1 Rex::Logger::info($what.' and verified after the reboot');
214 1 return $method;
215 }
216  
217  
218 40 ▶sub verify_amd {
Subroutine verify_amd: called CC=7 SCAR=19.5
Pod documented
219 40 ▶ my $class = _invocant(@_) // return __PACKAGE__->configured_class->verify_amd(@_);
Condition100%2/2
_invocant(@_) // (return __PACKAGE__->configured_class->verify_amd(@_))
l!l
2119
MC/DC100%1/1
_invocant(@_) // (return __PACKAGE__->configured_class->verify_amd(@_))
_invocant(@_)
Truth table100%2/2
A
A= _invocant(@_)
Aresult
11
00
220 21 my ( undef, %opts ) = @_;
221 21 my $gpus = eval {
222 21 $class->_check_options('verify_amd', \%opts, 'gpus');
223 20 $class->_check_gpus('verify_amd', $opts{gpus});
224 };
225 21 ▶ unless ($gpus) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gpus) 4 17 21
226 4 ( my $error = $@ ) =~ s/\s+\z//;
227 4 $error =~ s/ Nothing was changed on the host\z//;
228 4 Rex::Logger::info($error, 'warn');
229 4 return { ok => 0, gpus => [] };
230 }
231 17 my $state = eval { $class->read_state };
232 17 ▶ unless ($state) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($state) 1 16 17
233 1 Rex::Logger::info('verify_amd: could not read the AMD driver state from the host', 'warn');
234 1 return { ok => 0, gpus => [] };
235 }
236 16 my @targets = $class->_targets($state, $gpus);
237 16 my @problems = $class->state_problems($state, \@targets);
238 16 Rex::Logger::info(' AMD: '.$_, 'warn') for @problems;
239 Rex::Logger::info(@problems ? 'AMD driver verification failed ('.scalar(@problems).' problem(s))'
240 16 ▶ : 'AMD driver verified: '.join(', ', map { $class->gpu_label($_) } @targets));
Branch: 1 branch
BranchTrueFalseTotal
@problems ? : 10 6 16
241 return {
242 ok => @problems ? 0 : 1,
243 gpus => [ map {
244 16 ▶ { slot => $_->{slot},
Branch: 1 branch
BranchTrueFalseTotal
@problems ? : 10 6 16
245 device_id => $_->{device_id},
246 gfx_target => $_->{gfx_target},
247 vram_bytes => $_->{vram_bytes},
248 gtt_bytes => $_->{gtt_bytes},
249 kfd_node => $_->{kfd_nodes}[0],
250 18 kfd_nodes => [ @{ $_->{kfd_nodes} } ] }
251 } @targets ],
252 };
253 }
254  
255  
256 181 ▶sub install_rocm {
Subroutine install_rocm: called CC=17 SCAR=28.4
Pod documented
257 181 ▶ my $class = _invocant(@_) // return __PACKAGE__->configured_class->install_rocm(@_);
Condition100%2/2
_invocant(@_) // (return __PACKAGE__->configured_class->install_rocm(@_))
l!l
9289
MC/DC100%1/1
_invocant(@_) // (return __PACKAGE__->configured_class->install_rocm(@_))
_invocant(@_)
Truth table100%2/2
A
A= _invocant(@_)
Aresult
11
00
258 92 my ( undef, %opts ) = @_;
259 92 $class->_check_options('install_rocm', \%opts, qw( release gpus targets ));
260 91 my $releases = $class->rocm_releases;
261 91 my $known = join ', ', sort keys %$releases;
262 die "install_rocm: release => '...' is required (known: $known). Nothing was changed on the host\n"
263 91 ▶ unless defined $opts{release};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts{"release"} 1 90 91
264 my $rocm = $releases->{ $opts{release} }
265 90 ▶ or die "install_rocm: unknown ROCm release '$opts{release}' (known: $known). "
Branch: 1 branch
BranchTrueFalseTotal
my $rocm = $$releases{$opts{"release"}} or die qq[install_rocm: unknown ROCm release '$opts{"release"}' (known: $known). ] . "Nothing was changed on the host\n" 88 2 90
266 ."Nothing was changed on the host\n";
267 die "install_rocm: pass gpus or targets, not both. Nothing was changed on the host\n"
268 88 ▶ if defined $opts{gpus} && defined $opts{targets};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"gpus"} and defined $opts{"targets"} 1 87 88
Condition100%3/3
defined $opts{"gpus"} and defined $opts{"targets"}
!ll&&!rl&&r
8161
MC/DC100%2/2
defined $opts{"gpus"} and defined $opts{"targets"}
defined $opts{"gpus"} defined $opts{"targets"}
Truth table100%3/3
A and B
A= defined $opts{"gpus"}B= defined $opts{"targets"}
ABresult
0X0
100
111
269  
270 87 my @targets;
271 87 ▶ if (defined $opts{targets}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $opts{'targets'}) { } 76 11 87
elsif (defined $opts{'gpus'}) { } 6 5 11
272 die "install_rocm: targets must be an arrayref of gfx target names. Nothing was changed on the host\n"
273 76 ▶ unless ref $opts{targets} eq 'ARRAY' && !grep { !defined $_ || ref $_ } @{ $opts{targets} };
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts{"targets"} eq "ARRAY" and not grep({not defined $_ or ref $_;} @{$opts{"targets"};}) 1 75 76
Condition50%3/6
ref $opts{"targets"} eq "ARRAY" and not grep({not defined $_ or ref $_;} @{$opts{"targets"};})
!ll&&!rl&&r
1075
!defined($_) || ref $_
l!l&&r!l&&!r
0083
MC/DC50%1/2
ref $opts{"targets"} eq "ARRAY" and not grep({not defined $_ or ref $_;} @{$opts{"targets"};})
ref $opts{"targets"} eq "ARRAY" not grep({not defined $_ or ref $_;} @{$opts{"targets"};})
MC/DC0%0/2
!defined($_) || ref $_
!defined($_) ref $_
Truth table66%2/3
A and not(B)
A= ref $opts{"targets"} eq "ARRAY"B= not grep({not defined $_ or ref $_;} @{$opts{"targets"};})
ABresult
0X0
100
111
Truth table33%1/3
not(A) || B
A= !defined($_)B= ref $_
ABresult
1X1
011
000
274 75 @targets = map { lc } @{ $opts{targets} };
275 }
276 elsif (defined $opts{gpus}) {
277 6 for my $gpu (@{ $class->_check_gpus('install_rocm', $opts{gpus}) }) {
278 die 'install_rocm: '.$class->gpu_label($gpu).' has no gfx_target; pass targets => [...]. '
279 ."Nothing was changed on the host\n"
280 8 ▶ unless defined $gpu->{gfx_target};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gpu->{"gfx_target"} 1 7 8
281 7 push @targets, lc $gpu->{gfx_target};
282 }
283 }
284 else {
285 5 my $state = $class->read_state;
286 5 ▶ @targets = map { $_->{gfx_target} } grep { defined $_->{gfx_target} } @{ $state ? $state->{nodes} : [] };
Branch: 1 branch
BranchTrueFalseTotal
$state ? : 5 0 5
287 }
288 84 my %seen;
289 84 @targets = grep { !$seen{$_}++ } @targets;
290 84 ▶ die "install_rocm: no gfx target -- no AMD GPU with a running driver (KFD) found; pass "
Branch: 1 branch
BranchTrueFalseTotal
unless @targets 2 82 84
291 ."targets => [...]. Nothing was changed on the host\n"
292 unless @targets;
293 82 my %ships = map { $_ => 1 } @{ $rocm->{targets} };
294 82 for my $gfx (@targets) {
295 die "install_rocm: ROCm $rocm->{release} has no package for $gfx (it ships: "
296 1 .join(' ', @{ $rocm->{targets} })."). Nothing was changed on the host\n"
297 92 ▶ unless $ships{$gfx};
Branch: 1 branch
BranchTrueFalseTotal
unless $ships{$gfx} 1 91 92
298 }
299 81 return $class->setup_for->install_rocm($rocm, \@targets);
300 }
301  
302  
303 93 ▶sub rocm_releases {
Subroutine rocm_releases: called CC=1 SCAR=0.0
Pod documented
304 return {
305 93 '10.1' => {
306 release => '10.1',
307 package_prefix => 'amdrocm10.1-',
308 targets => [qw( gfx908 gfx90a gfx942 gfx950 gfx1010 gfx1011 gfx1012
309 gfx1030 gfx1031 gfx1032 gfx1033 gfx1034 gfx1035 gfx1036
310 gfx1100 gfx1101 gfx1102 gfx1103 gfx1150 gfx1151 gfx1152 gfx1153
311 gfx1200 gfx1201 gfx1250 )],
312 extra_packages => [qw( libatomic1 libquadmath0 )],
313 repo_url => 'https://stable.repo.amd.com/rocm/core/packages/',
314 key_url => 'https://stable.repo.amd.com/rocm/gpg/packages.gpg',
315 key_fingerprint => 'D0F004A0025A1145C7807FCD0701EAC4D5E02107',
316 },
317 };
318 }
319  
320  
321 747 ▶sub state_script { $STATE_SCRIPT }
Subroutine state_script: called CC=1 SCAR=0.0
Pod documented
322  
323 308 ▶sub read_state {
Subroutine read_state: called CC=1 SCAR=0.0
Pod documented
324 308 my ( $class ) = @_;
325 308 my $out = $class->host_run($class->state_script);
326 308 return $class->parse_state($out);
327 }
328  
329 338 ▶sub parse_state {
Subroutine parse_state: called CC=20 SCAR=30.0
Pod documented
330 338 my ( $class, $out ) = @_;
331 338 ▶ return unless defined $out && $out =~ /^end\r?$/m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out and $out =~ /^end\r?$/mu 16 322 338
Condition66%2/3
defined $out and $out =~ /^end\r?$/mu
!ll&&!rl&&r
016322
MC/DC50%1/2
defined $out and $out =~ /^end\r?$/mu
defined $out $out =~ /^end\r?$/mu
Truth table66%2/3
A and B
A= defined $outB= $out =~ /^end\r?$/mu
ABresult
0X0
100
111
332 322 my %state = ( amdgpu => 0, kfd => 0, pci => [], render => [], nodes => [] );
333 322 my ( %pci, $node );
334 322 for my $line (split /\r?\n/, $out) {
335 2257 ▶ if ($line =~ /\A(amdgpu|kfd) ([01])\z/) {
Branch: 6 branches
BranchTrueFalseTotal
if ($line =~ /\A(amdgpu|kfd) ([01])\z/u) { } 644 1613 2257
elsif ($line =~ /\Apci (\S+) 0x([0-9a-f]{4})[0-9a-f]{2} (\S+) (\S+)\z/iu) { } 352 1261 1613
elsif ($line =~ /\A(vram|gtt) (\S+) (\d+)\z/u) { } 140 1121 1261
elsif ($line =~ /\Arender (renderD\d+) (\S+) (\S+)\z/u) { } 94 1027 1121
elsif ($line =~ /\Anode (\d+)\z/u) { } 120 907 1027
elsif ($node and $line =~ /\Aprop (\w+) (\d+)\z/u) { } 585 552 1137
336 644 $state{$1} = $2;
337 }
338 elsif ($line =~ /\Apci (\S+) 0x([0-9a-f]{4})[0-9a-f]{2} (\S+) (\S+)\z/i) {
339 352 ▶ my $slot = $class->normalize_slot($1) // next;
Condition50%1/2
$class->normalize_slot($1) // (next)
l!l
3520
MC/DC0%0/1
$class->normalize_slot($1) // (next)
$class->normalize_slot($1)
Truth table50%1/2
A
A= $class->normalize_slot($1)
Aresult
11
00
340 352 ▶ my $dev = { slot => $slot, class => lc $2, device_id => _hex4($3),
Branch: 1 branch
BranchTrueFalseTotal
$4 eq '-' ? : 247 105 352
341 bound_driver => ( $4 eq '-' ? undef : $4 ), vram_bytes => undef, gtt_bytes => undef };
342 352 push @{ $state{pci} }, $dev;
343 352 $pci{$slot} = $dev;
344 }
345 elsif ($line =~ /\A(vram|gtt) (\S+) (\d+)\z/) {
346 140 ▶ my $dev = $pci{ $class->normalize_slot($2) // '' } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $dev = $pci{$class->normalize_slot($2) // ""} or next 140 0 140
Condition50%1/2
$class->normalize_slot($2) // ""
l!l
1400
MC/DC0%0/1
$class->normalize_slot($2) // ""
$class->normalize_slot($2)
Truth table50%1/2
A
A= $class->normalize_slot($2)
Aresult
11
00
347 140 $dev->{$1.'_bytes'} = $3;
348 }
349 elsif ($line =~ /\Arender (renderD\d+) (\S+) (\S+)\z/) {
350 94 push @{ $state{render} }, { name => $1, vendor => _hex4($2), slot => $class->normalize_slot($3) };
351 }
352 elsif ($line =~ /\Anode (\d+)\z/) {
353 120 push @{ $state{nodes} }, $node = { id => $1 + 0 };
354 }
355 ▶ elsif ($node && $line =~ /\Aprop (\w+) (\d+)\z/) {
Condition100%3/3
$node and $line =~ /\Aprop (\w+) (\d+)\z/u
!ll&&!rl&&r
23092585
MC/DC100%2/2
$node and $line =~ /\Aprop (\w+) (\d+)\z/u
$node $line =~ /\Aprop (\w+) (\d+)\z/u
Truth table100%3/3
A and B
A= $nodeB= $line =~ /\Aprop (\w+) (\d+)\z/u
ABresult
0X0
100
111
356 585 $node->{$1} = $2 + 0;
357 }
358 }
359 322 for my $n (@{ $state{nodes} }) {
360 120 $n->{gfx_target} = $class->hardware_class->gfx_target_from_version($n->{gfx_target_version});
361 120 ▶ $n->{vendor} = sprintf '%04x', delete $n->{vendor_id} if defined $n->{vendor_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $n->{"vendor_id"} 93 27 120
362 120 ▶ $n->{device_id} = sprintf '%04x', $n->{device_id} if defined $n->{device_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $n->{"device_id"} 93 27 120
363 # location_id = bus << 8 | devfn, both decimal in the topology
364 $n->{slot} = sprintf '%04x:%02x:%02x.%x', $n->{domain} // 0, $n->{location_id} >> 8,
365 ( $n->{location_id} & 0xff ) >> 3, $n->{location_id} & 7
366 120 ▶ if defined $n->{location_id};
Branch: 1 branch
BranchTrueFalseTotal
if defined $n->{"location_id"} 93 27 120
Condition50%1/2
$n->{"domain"} // 0
l!l
930
MC/DC0%0/1
$n->{"domain"} // 0
$n->{"domain"}
Truth table50%1/2
A
A= $n->{"domain"}
Aresult
11
00
367 }
368 322 @{ $state{nodes} } = sort { $a->{id} <=> $b->{id} } @{ $state{nodes} };
369 322 return \%state;
370 }
371  
372  
373 293 ▶sub state_problems {
Subroutine state_problems: called CC=14 SCAR=28.1
Pod documented
374 293 my ( $class, $state, $gpus ) = @_;
375 293 ▶ return ('the AMD driver state could not be read from the host') unless $state;
Branch: 1 branch
BranchTrueFalseTotal
unless $state 0 293 293
376 293 my @problems;
377 293 ▶ push @problems, 'the amdgpu kernel module is not loaded (no /sys/module/amdgpu)' unless $state->{amdgpu};
Branch: 1 branch
BranchTrueFalseTotal
unless $state->{"amdgpu"} 219 74 293
378 293 ▶ push @problems, 'no /dev/kfd (the ROCm compute interface of amdgpu)' unless $state->{kfd};
Branch: 1 branch
BranchTrueFalseTotal
unless $state->{"kfd"} 225 68 293
379 0 ▶ my @gpus = $gpus && @$gpus ? @$gpus : map { $class->_target($state, $_) } @{ $state->{pci} };
Branch: 1 branch
BranchTrueFalseTotal
$gpus && @$gpus ? : 293 0 293
Condition33%1/3
$gpus and @$gpus
!ll&&!rl&&r
00293
MC/DC0%0/2
$gpus and @$gpus
$gpus @$gpus
Truth table33%1/3
A and B
A= $gpusB= @$gpus
ABresult
0X0
100
111
380 293 ▶ push @problems, 'no AMD GPU found on the host' unless @gpus;
Branch: 1 branch
BranchTrueFalseTotal
unless @gpus 0 293 293
381 293 for my $gpu (@gpus) {
382 320 my $label = $class->gpu_label($gpu);
383 320 ▶ if ($gpu->{missing}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gpu->{"missing"}) 1 319 320
384 1 push @problems, $label.': not on the host';
385 1 next;
386 }
387 319 ▶ push @problems, $label.': no KFD compute node (/sys/class/kfd/kfd/topology)'
Branch: 1 branch
BranchTrueFalseTotal
unless $class->kfd_nodes($state, $gpu) 255 64 319
388 unless $class->kfd_nodes($state, $gpu);
389 push @problems, $label.': no render node (/sys/class/drm/renderD*)'
390 69 ▶ unless grep { ( $_->{vendor} // '' ) eq '1002' && defined $_->{slot} && $_->{slot} eq $gpu->{slot} }
Condition50%4/8
($_->{'vendor'} // '') eq '1002' && defined $_->{'slot'} && $_->{'slot'} eq $gpu->{'slot'}
!ll&&!rl&&r
0465
($_->{"vendor"} // "") eq 1002 and defined $_->{"slot"}
!ll&&!rl&&r
0069
$_->{"vendor"} // ""
l!l
690
MC/DC33%1/3
($_->{'vendor'} // '') eq '1002' && defined $_->{'slot'} && $_->{'slot'} eq $gpu->{'slot'}
$_->{"vendor"} defined $_->{"slot"} $_->{'slot'} eq $gpu->{'slot'}
Truth table50%2/4
A and B && C
A= $_->{"vendor"}B= defined $_->{"slot"}C= $_->{'slot'} eq $gpu->{'slot'}
ABCresult
0XX0
10X0
1100
1111
391 319 ▶ @{ $state->{render} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {($_->{"vendor"} // "") eq 1002 and defined $_->{"slot"} and $_->{"slot"} eq $gpu->{"slot"};} @{$$state{"render"};} 254 65 319
392 }
393 293 return @problems;
394 }
395  
396 616 ▶sub kfd_nodes {
Subroutine kfd_nodes: called CC=6 SCAR=20.5
Pod documented
397 616 my ( $class, $state, $gpu ) = @_;
398 616 my $slot = $class->normalize_slot($gpu->{slot});
399 616 my $id = _hex4($gpu->{device_id});
400 return grep {
401 $_->{gfx_target_version}
402 && ( defined $_->{slot} ? defined $slot && $_->{slot} eq $slot
403 155 ▶ : defined $id && ( $_->{device_id} // '' ) eq $id )
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'slot'} ? : 116 0 116
Condition45%5/11
$_->{'gfx_target_version'} && (defined $_->{'slot'} ? defined $slot && $_->{'slot'} eq $slot : defined $id && ($_->{'device_id'} // '') eq $id)
!ll&&!rl&&r
399107
defined $slot and $_->{"slot"} eq $slot
!ll&&!rl&&r
09107
defined $id and ($_->{"device_id"} // "") eq $id
!ll&&!rl&&r
000
$_->{"device_id"} // ""
l!l
00
MC/DC100%2/2
$_->{'gfx_target_version'} && (defined $_->{'slot'} ? defined $slot && $_->{'slot'} eq $slot : defined $id && ($_->{'device_id'} // '') eq $id)
$_->{'gfx_target_version'} (defined $_->{'slot'} ? defined $slot && $_->{'slot'} eq $slot : defined $id && ($_->{'device_id'} // '') eq $id)
MC/DC50%1/2
defined $slot and $_->{"slot"} eq $slot
defined $slot $_->{"slot"} eq $slot
MC/DC0%0/2
defined $id and ($_->{"device_id"} // "") eq $id
defined $id $_->{"device_id"}
Truth table100%3/3
A && B
A= $_->{'gfx_target_version'}B= (defined $_->{'slot'} ? defined $slot && $_->{'slot'} eq $slot : defined $id && ($_->{'device_id'} // '') eq $id)
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= defined $slotB= $_->{"slot"} eq $slot
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $idB= $_->{"device_id"}
ABresult
0X0
100
111
404 616 } @{ $state->{nodes} };
405 }
406  
407 # The GPUs a call is about, each resolved against the state: the given ones
408 # (matched by slot, else by device ID to a device not taken yet; one not on
409 # the host gets missing => 1), or every AMD GPU the state lists.
410 255 ▶sub _targets {
Subroutine _targets: called CC=7 SCAR=19.5
411 255 my ( $class, $state, $gpus ) = @_;
412 255 ▶ return map { $class->_target($state, $_) } @{ $state->{pci} } unless @$gpus;
Branch: 1 branch
BranchTrueFalseTotal
unless @$gpus 13 242 255
413 242 my ( @targets, %taken );
414 242 for my $gpu (@$gpus) {
415 269 my $slot = $class->normalize_slot($gpu->{slot});
416 269 my $id = _hex4($gpu->{device_id});
417 my ( $dev ) = grep {
418 !$taken{ $_->{slot} }
419 325 ▶ && ( defined $slot ? $_->{slot} eq $slot : ( $_->{device_id} // '' ) eq $id )
Branch: 1 branch
BranchTrueFalseTotal
defined $slot ? : 295 3 298
Condition80%4/5
!$taken{$_->{'slot'}} && (defined $slot ? $_->{'slot'} eq $slot : ($_->{'device_id'} // '') eq $id)
!ll&&!rl&&r
2733265
$_->{"device_id"} // ""
l!l
30
MC/DC100%2/2
!$taken{$_->{'slot'}} && (defined $slot ? $_->{'slot'} eq $slot : ($_->{'device_id'} // '') eq $id)
!$taken{$_->{'slot'}} (defined $slot ? $_->{'slot'} eq $slot : ($_->{'device_id'} // '') eq $id)
MC/DC0%0/1
$_->{"device_id"} // ""
$_->{"device_id"}
Truth table100%3/3
not(A) && B
A= !$taken{$_->{'slot'}}B= (defined $slot ? $_->{'slot'} eq $slot : ($_->{'device_id'} // '') eq $id)
ABresult
0X0
100
111
Truth table50%1/2
A
A= $_->{"device_id"}
Aresult
11
00
420 269 } @{ $state->{pci} };
421 269 ▶ unless ($dev) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dev) 4 265 269
422 4 push @targets, { name => $gpu->{name}, slot => $slot, device_id => $id, missing => 1,
423 kfd_nodes => [] };
424 4 next;
425 }
426 265 $taken{ $dev->{slot} } = 1;
427 265 push @targets, $class->_target($state, $dev, $gpu->{name});
428 }
429 242 return @targets;
430 }
431  
432 278 ▶sub _target {
Subroutine _target: called CC=2 SCAR=6.9
433 278 my ( $class, $state, $dev, $name ) = @_;
434 278 my @nodes = $class->kfd_nodes($state, $dev);
435 my $hw = $class->hardware_class->resolve(
436 device_id => $dev->{device_id},
437 278 ▶ kfd_gfx_target => ( @nodes ? $nodes[0]{gfx_target} : undef ),
Branch: 1 branch
BranchTrueFalseTotal
@nodes ? : 24 254 278
438 name => $name,
439 );
440 return {
441 name => $name,
442 slot => $dev->{slot},
443 device_id => $dev->{device_id},
444 bound_driver => $dev->{bound_driver},
445 vram_bytes => $dev->{vram_bytes},
446 gtt_bytes => $dev->{gtt_bytes},
447 278 kfd_nodes => [ map { $_->{id} } @nodes ],
448 %$hw,
449 };
450 }
451  
452  
453 653 ▶sub gpu_label {
Subroutine gpu_label: called CC=5 SCAR=16.1
Pod documented
454 653 my ( $class, $gpu ) = @_;
455 653 ▶ my $slot = $class->normalize_slot($gpu->{slot}) // $gpu->{slot};
Condition66%2/3
$class->normalize_slot($gpu->{"slot"}) // $gpu->{"slot"}
l!l&&r!l&&!r
65003
MC/DC50%1/2
$class->normalize_slot($gpu->{"slot"}) // $gpu->{"slot"}
$class->normalize_slot($gpu->{"slot"}) $gpu->{"slot"}
Truth table66%2/3
A // B
A= $class->normalize_slot($gpu->{"slot"})B= $gpu->{"slot"}
ABresult
1X1
011
000
456 return join ' ', 'AMD GPU', ( defined $slot ? $slot : () ),
457 '[1002:'.( _hex4($gpu->{device_id}) // '????' ).']',
458 653 ▶ ( defined $gpu->{gfx_target} ? '('.$gpu->{gfx_target}.')' : () );
Branch: 2 branches
BranchTrueFalseTotal
defined $slot ? : 650 3 653
defined $gpu->{'gfx_target'} ? : 645 8 653
Condition100%2/2
_hex4($gpu->{"device_id"}) // "????"
l!l
6512
MC/DC100%1/1
_hex4($gpu->{"device_id"}) // "????"
_hex4($gpu->{"device_id"})
Truth table100%2/2
A
A= _hex4($gpu->{"device_id"})
Aresult
11
00
459 }
460  
461  
462 99 ▶sub module_source {
Subroutine module_source: called CC=6 SCAR=18.5
Pod documented
463 99 my ( $class, %arg ) = @_;
464 99 ▶ my $kernel = $arg{kernel} // _trim($class->host_run('uname -r'));
Condition66%2/3
$arg{"kernel"} // _trim($class->host_run("uname -r"))
l!l&&r!l&&!r
9360
MC/DC0%0/2
$arg{"kernel"} // _trim($class->host_run("uname -r"))
$arg{"kernel"} _trim($class->host_run("uname -r"))
Truth table66%2/3
A // B
A= $arg{"kernel"}B= _trim($class->host_run("uname -r"))
ABresult
1X1
011
000
465 99 my $path = _trim($class->host_run('modinfo -n amdgpu 2>/dev/null'));
466 99 ▶ return unless length $path && $path =~ m{\A/};
Branch: 1 branch
BranchTrueFalseTotal
unless length $path and $path =~ m[\A/]u 37 62 99
Condition66%2/3
length $path and $path =~ m[\A/]u
!ll&&!rl&&r
37062
MC/DC50%1/2
length $path and $path =~ m[\A/]u
length $path $path =~ m[\A/]u
Truth table66%2/3
A and B
A= length $pathB= $path =~ m[\A/]u
ABresult
0X0
100
111
467 62 ▶ return 'inbox' if $path =~ m{\A(?:/usr)?/lib/modules/\Q$kernel\E/kernel/};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[\A(?:/usr)?/lib/modules/\Q$kernel\E/kernel/]u 23 39 62
468 39 ▶ return 'dkms' if $path =~ m{\A(?:/usr)?/lib/modules/\Q$kernel\E/(?:updates|extra)/};
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[\A(?:/usr)?/lib/modules/\Q$kernel\E/(?:updates|extra)/]u 39 0 39
469 0 return 'other';
470 }
471  
472  
473 5229 ▶sub host_run {
Subroutine host_run: called CC=1 SCAR=0.0
Pod documented
474 5229 my ( $class, $command, %opt ) = @_;
475 5229 my $out = Rex::Commands::Run::run($command, auto_die => 0, %opt);
476 5229 return $out;
477 }
478  
479  
480 # /etc/os-release, not Rex::Commands::Gather: Rex reports the lsb_release
481 # names where lsb_release is installed (Rocky, RockyLinux, AlmaLinux, ... --
482 # karr #39), a release can come back with its dots in an unexpected form,
483 # and its probes are commands this module's host_run would not see. ID and
484 # VERSION_ID are the distribution's own, stable keys.
485 302 ▶sub read_os_release {
Subroutine read_os_release: called CC=6 SCAR=18.2
Pod documented
486 302 my ( $class ) = @_;
487 302 my $out = $class->host_run('cat /etc/os-release 2>/dev/null');
488 302 my %os;
489 302 ▶ for my $line (split /\r?\n/, $out // '') {
Condition50%1/2
$out // ""
l!l
3020
MC/DC0%0/1
$out // ""
$out
Truth table50%1/2
A
A= $out
Aresult
11
00
490 1705 ▶ next unless $line =~ /\A([A-Z][A-Z0-9_]*)=(.*)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\A([A-Z][A-Z0-9_]*)=(.*)\z/u 0 1705 1705
491 1705 my ( $key, $value ) = ( $1, $2 );
492 1705 $value =~ s/\s+\z//;
493 1705 ▶ $value = $1 if $value =~ /\A"(.*)"\z/ || $value =~ /\A'(.*)'\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A"(.*)"\z/u or $value =~ /\A'(.*)'\z/u 1285 420 1705
Condition66%2/3
$value =~ /\A"(.*)"\z/u or $value =~ /\A'(.*)'\z/u
l!l&&r!l&&!r
12850420
MC/DC50%1/2
$value =~ /\A"(.*)"\z/u or $value =~ /\A'(.*)'\z/u
$value =~ /\A"(.*)"\z/u $value =~ /\A'(.*)'\z/u
Truth table66%2/3
A or B
A= $value =~ /\A"(.*)"\z/uB= $value =~ /\A'(.*)'\z/u
ABresult
1X1
011
000
494 1705 $os{$key} = $value;
495 }
496 302 return \%os;
497 }
498  
499  
500 300 ▶sub setup_class_for_os {
Subroutine setup_class_for_os: called CC=5 SCAR=16.1
Pod documented
501 300 my ( $class, $os ) = @_;
502 300 ▶ my $id = lc( $os->{ID} // '' );
Condition50%1/2
$os->{"ID"} // ""
l!l
3000
MC/DC0%0/1
$os->{"ID"} // ""
$os->{"ID"}
Truth table50%1/2
A
A= $os->{"ID"}
Aresult
11
00
503 300 ▶ return 'Rex::GPU::AMD::Setup::Ubuntu' if $id eq 'ubuntu';
Branch: 1 branch
BranchTrueFalseTotal
if $id eq "ubuntu" 159 141 300
504 141 ▶ return 'Rex::GPU::AMD::Setup::Debian' if $id eq 'debian';
Branch: 1 branch
BranchTrueFalseTotal
if $id eq "debian" 97 44 141
505 44 ▶ return 'Rex::GPU::AMD::Setup::RHEL' if $id =~ /\A(?:rhel|rocky|almalinux)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $id =~ /\A(?:rhel|rocky|almalinux)\z/u 42 2 44
506 2 return;
507 }
508  
509 302 ▶sub setup_for {
Subroutine setup_for: called CC=4 SCAR=14.1
Pod documented
510 302 my ( $class, %arg ) = @_;
511 302 my $os = $class->read_os_release;
512 my $setup = $class->setup_class_for_os($os)
513 or die 'Unsupported OS for the AMD driver: '
514 302 ▶ .( $os->{PRETTY_NAME} // $os->{ID} // 'unknown (no /etc/os-release)' )
Branch: 1 branch
BranchTrueFalseTotal
my $setup = $class->setup_class_for_os($os) or die 'Unsupported OS for the AMD driver: ' . (($os->{"PRETTY_NAME"} // $os->{"ID"}) // "unknown (no /etc/os-release)") . " (supported: Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux). Nothing was changed on the host\n" 300 2 302
Condition60%3/5
($os->{"PRETTY_NAME"} // $os->{"ID"}) // "unknown (no /etc/os-release)"
l!l
20
$os->{"PRETTY_NAME"} // $os->{"ID"}
l!l&&r!l&&!r
110
MC/DC0%0/2
($os->{"PRETTY_NAME"} // $os->{"ID"}) // "unknown (no /etc/os-release)"
$os->{"PRETTY_NAME"} $os->{"ID"}
Truth table66%2/3
A // B
A= $os->{"PRETTY_NAME"}B= $os->{"ID"}
ABresult
1X1
011
000
515 ." (supported: Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux). Nothing was changed on the host\n";
516 300 return $setup->new(amd => $class, os_release => $os, %arg);
517 }
518  
519  
520 455 ▶sub hardware_class { 'Rex::GPU::AMD::Hardware' }
Subroutine hardware_class: called CC=1 SCAR=0.0
Pod documented
521  
522 # Always one value (undef included), also in the list of a hash constructor.
523 2433 ▶sub normalize_slot {
Subroutine normalize_slot: called CC=4 SCAR=13.9
Pod documented
524 2433 my ( $class, $slot ) = @_;
525 2433 ▶ return defined $slot
Branch: 1 branch
BranchTrueFalseTotal
defined $slot && $slot =~ /\A(?:([0-9a-f]{4,}):)?([0-9a-f]{2}):([0-9a-f]{2})\.([0-7])\z/iu ? : 2425 8 2433
Condition100%5/5
defined $slot and $slot =~ /\A(?:([0-9a-f]{4,}):)?([0-9a-f]{2}):([0-9a-f]{2})\.([0-7])\z/iu
!ll&&!rl&&r
712425
$1 // "0000"
l!l
239530
MC/DC100%2/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
MC/DC100%1/1
$1 // "0000"
$1
Truth table100%3/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
Truth table100%2/2
A
A= $1
Aresult
11
00
526 && $slot =~ /\A(?:([0-9a-f]{4,}):)?([0-9a-f]{2}):([0-9a-f]{2})\.([0-7])\z/i
527 ? lc join('', $1 // '0000', ':', $2, ':', $3, '.', $4)
528 : undef;
529 }
530  
531  
532 3 ▶sub reboot_and_wait {
Subroutine reboot_and_wait: called CC=8 SCAR=21.2
Pod documented
533 3 my ( $class ) = @_;
534 3 Rex::Logger::info('Rebooting host to activate the AMD driver...');
535 3 my $boot_id = $class->_read_boot_id;
536 3 ▶ Rex::Logger::info(' boot_id not readable before reboot, checking only that the host answers')
Branch: 1 branch
BranchTrueFalseTotal
unless length $boot_id 2 1 3
537 unless length $boot_id;
538 3 $class->host_run("nohup sh -c 'sleep 2 && shutdown -r now' >/dev/null 2>&1 &");
539 3 sleep 20;
540 3 my $conn = Rex::get_current_connection()->{conn};
541 3 for my $i (1 .. 60) {
542 62 eval { $conn->disconnect() };
543 62 eval { $conn->reconnect() };
544 62 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 2 60 62
545 2 my $test = eval { $class->host_run('echo ok') };
546 2 ▶ if (defined $test && $test =~ /^ok\r?$/m
Branch: 1 branch
BranchTrueFalseTotal
if (defined $test and $test =~ /^ok\r?$/mu and !length($boot_id) || $class->_new_boot_id($boot_id)) 2 0 2
Condition44%4/9
defined $test and $test =~ /^ok\r?$/mu and !length($boot_id) || $class->_new_boot_id($boot_id)
!ll&&!rl&&r
002
defined $test and $test =~ /^ok\r?$/mu
!ll&&!rl&&r
002
not length $boot_id or $class->_new_boot_id($boot_id)
l!l&&r!l&&!r
110
MC/DC0%0/4
defined $test and $test =~ /^ok\r?$/mu and !length($boot_id) || $class->_new_boot_id($boot_id)
defined $test $test =~ /^ok\r?$/mu not length $boot_id $class->_new_boot_id($boot_id)
Truth table40%2/5
A and B and not(C) or D
A= defined $testB= $test =~ /^ok\r?$/muC= not length $boot_idD= $class->_new_boot_id($boot_id)
ABCDresult
0XXX0
10XX0
11000
111X1
11011
547 && ( !length $boot_id || $class->_new_boot_id($boot_id) )) {
548 2 Rex::Logger::info(' Host is back online (after ~'.( $i * 5 + 20 ).'s)');
549 2 return;
550 }
551 }
552 60 Rex::Logger::info(" Waiting for host to come back... ($i/60)");
553 60 sleep 5;
554 }
555 1 die "Host did not come back after reboot\n";
556 }
557  
558 1 ▶sub _new_boot_id {
Subroutine _new_boot_id: called CC=3 SCAR=12.5
559 1 my ( $class, $old ) = @_;
560 1 my $now = $class->_read_boot_id;
561 1 ▶ return length $now && $now ne $old ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
length $now && $now ne $old ? : 1 0 1
Condition33%1/3
length $now and $now ne $old
!ll&&!rl&&r
001
MC/DC0%0/2
length $now and $now ne $old
length $now $now ne $old
Truth table33%1/3
A and B
A= length $nowB= $now ne $old
ABresult
0X0
100
111
562 }
563  
564 4 ▶sub _read_boot_id {
Subroutine _read_boot_id: called CC=3 SCAR=11.0
565 4 my ( $class ) = @_;
566 4 my $out = eval { $class->host_run('cat /proc/sys/kernel/random/boot_id 2>/dev/null') };
567 4 ▶ return '' unless defined $out;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out 0 4 4
568 4 ▶ return $out =~ /^\s*([0-9a-fA-F-]{32,36})\s*$/m ? lc $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$out =~ /^\s*([0-9a-fA-F-]{32,36})\s*$/mu ? : 2 2 4
569 }
570  
571  
572 398 ▶sub configured_class {
Subroutine configured_class: called CC=6 SCAR=17.9
Pod documented
573 398 my ( $class, $amd ) = @_;
574 398 my $origin = 'amd =>';
575 398 ▶ unless (defined $amd && $amd ne '') {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $amd and $amd ne "") 386 12 398
Condition100%3/3
defined $amd and $amd ne ""
!ll&&!rl&&r
385112
MC/DC100%2/2
defined $amd and $amd ne ""
defined $amd $amd ne ""
Truth table100%3/3
A and B
A= defined $amdB= $amd ne ""
ABresult
0X0
100
111
576 386 $amd = Rex::Config->get('gpu_amd_class');
577 386 $origin = 'set gpu_amd_class';
578 386 ▶ return __PACKAGE__ unless defined $amd && $amd ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $amd and $amd ne "" 384 2 386
Condition100%3/3
defined $amd and $amd ne ""
!ll&&!rl&&r
193652
MC/DC100%2/2
defined $amd and $amd ne ""
defined $amd $amd ne ""
Truth table100%3/3
A and B
A= defined $amdB= $amd ne ""
ABresult
0X0
100
111
579 }
580 14 ▶ croak 'AMD class from '.$origin.' must be a class name, not '.$amd.'. Nothing was changed on the host'
Branch: 1 branch
BranchTrueFalseTotal
if ref $amd 0 14 14
581 if ref $amd;
582 14 return Rex::GPU::NVIDIA->load_user_class($amd,
583 base => __PACKAGE__, what => 'AMD class', origin => $origin);
584 }
585  
586 365 ▶sub _check_options {
Subroutine _check_options: called CC=3 SCAR=11.0
587 365 my ( $class, $fn, $opts, @known ) = @_;
588 365 my %known = map { $_ => 1 } @known;
589 365 my @unknown = sort grep { !$known{$_} } keys %$opts;
590 365 ▶ die "$fn: unknown option".( @unknown > 1 ? 's' : '' ).' '.join(', ', @unknown)
Branch: 2 branches
BranchTrueFalseTotal
if @unknown 4 361 365
@unknown > 1 ? : 1 3 4
591 .' (known: '.join(', ', @known)."). Nothing was changed on the host\n"
592 if @unknown;
593 361 return;
594 }
595  
596 # gpus => undef or a non-empty arrayref of hashrefs, each with a valid slot
597 # or device_id. Returns the arrayref ([] for undef: every AMD GPU of the
598 # host). An explicit [] dies (karr k1): a caller that filtered every GPU out
599 # must not get all of them.
600 275 ▶sub _check_gpus {
Subroutine _check_gpus: called CC=15 SCAR=27.1
601 275 my ( $class, $fn, $gpus ) = @_;
602 275 ▶ return [] unless defined $gpus;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gpus 17 258 275
603 die "$fn: gpus must be an arrayref of GPU hashrefs. Nothing was changed on the host\n"
604 258 ▶ unless ref $gpus eq 'ARRAY' && !grep { ref $_ ne 'HASH' } @$gpus;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $gpus eq "ARRAY" and not grep({ref $_ ne "HASH";} @$gpus) 3 255 258
Condition100%3/3
ref $gpus eq "ARRAY" and not grep({ref $_ ne "HASH";} @$gpus)
!ll&&!rl&&r
21255
MC/DC100%2/2
ref $gpus eq "ARRAY" and not grep({ref $_ ne "HASH";} @$gpus)
ref $gpus eq "ARRAY" not grep({ref $_ ne "HASH";} @$gpus)
Truth table100%3/3
A and not(B)
A= ref $gpus eq "ARRAY"B= not grep({ref $_ ne "HASH";} @$gpus)
ABresult
0X0
100
111
605 255 ▶ die "$fn: gpus is empty; omit gpus for every AMD GPU of the host. Nothing was changed on the host\n"
Branch: 1 branch
BranchTrueFalseTotal
unless @$gpus 4 251 255
606 unless @$gpus;
607 251 for my $gpu (@$gpus) {
608 281 ▶ my $name = $gpu->{name} // 'a GPU';
Condition100%2/2
$gpu->{"name"} // "a GPU"
l!l
2738
MC/DC100%1/1
$gpu->{"name"} // "a GPU"
$gpu->{"name"}
Truth table100%2/2
A
A= $gpu->{"name"}
Aresult
11
00
609 die "$fn: $name is vendor '$gpu->{vendor}', not amd. Nothing was changed on the host\n"
610 281 ▶ if defined $gpu->{vendor} && $gpu->{vendor} ne 'amd';
Branch: 1 branch
BranchTrueFalseTotal
if defined $gpu->{"vendor"} and $gpu->{"vendor"} ne "amd" 1 280 281
Condition100%3/3
defined $gpu->{"vendor"} and $gpu->{"vendor"} ne "amd"
!ll&&!rl&&r
112691
MC/DC100%2/2
defined $gpu->{"vendor"} and $gpu->{"vendor"} ne "amd"
defined $gpu->{"vendor"} $gpu->{"vendor"} ne "amd"
Truth table100%3/3
A and B
A= defined $gpu->{"vendor"}B= $gpu->{"vendor"} ne "amd"
ABresult
0X0
100
111
611 die "$fn: $name has neither a slot nor a device_id. Nothing was changed on the host\n"
612 280 ▶ unless defined $gpu->{slot} || defined $gpu->{device_id};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gpu->{"slot"} or defined $gpu->{"device_id"} 1 279 280
Condition100%3/3
defined $gpu->{"slot"} or defined $gpu->{"device_id"}
l!l&&r!l&&!r
27361
MC/DC100%2/2
defined $gpu->{"slot"} or defined $gpu->{"device_id"}
defined $gpu->{"slot"} defined $gpu->{"device_id"}
Truth table100%3/3
A or B
A= defined $gpu->{"slot"}B= defined $gpu->{"device_id"}
ABresult
1X1
011
000
613 die "$fn: $name has a slot that is no PCI address: '$gpu->{slot}'. Nothing was changed on the host\n"
614 279 ▶ if defined $gpu->{slot} && !defined $class->normalize_slot($gpu->{slot});
Branch: 1 branch
BranchTrueFalseTotal
if defined $gpu->{"slot"} and not defined $class->normalize_slot($gpu->{"slot"}) 1 278 279
Condition100%3/3
defined $gpu->{"slot"} and not defined $class->normalize_slot($gpu->{"slot"})
!ll&&!rl&&r
62721
MC/DC100%2/2
defined $gpu->{"slot"} and not defined $class->normalize_slot($gpu->{"slot"})
defined $gpu->{"slot"} not defined $class->normalize_slot($gpu->{"slot"})
Truth table100%3/3
A and not(B)
A= defined $gpu->{"slot"}B= not defined $class->normalize_slot($gpu->{"slot"})
ABresult
0X0
100
111
615 die "$fn: $name has a device_id that is not four hex digits: '$gpu->{device_id}'. "
616 ."Nothing was changed on the host\n"
617 278 ▶ if defined $gpu->{device_id} && !defined _hex4($gpu->{device_id});
Branch: 1 branch
BranchTrueFalseTotal
if defined $gpu->{"device_id"} and not defined _hex4($gpu->{"device_id"}) 1 277 278
Condition100%3/3
defined $gpu->{"device_id"} and not defined _hex4($gpu->{"device_id"})
!ll&&!rl&&r
32741
MC/DC100%2/2
defined $gpu->{"device_id"} and not defined _hex4($gpu->{"device_id"})
defined $gpu->{"device_id"} not defined _hex4($gpu->{"device_id"})
Truth table100%3/3
A and not(B)
A= defined $gpu->{"device_id"}B= not defined _hex4($gpu->{"device_id"})
ABresult
0X0
100
111
618 }
619 247 return $gpus;
620 }
621  
622 # '0x1586' / '1586' => '1586'; anything else undef.
623 2259 ▶sub _hex4 {
Subroutine _hex4: called CC=3 SCAR=11.0
624 2259 my ( $id ) = @_;
625 2259 ▶ return defined $id && $id =~ /\A(?:0x)?([0-9a-f]{4})\z/i ? lc $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $id && $id =~ /\A(?:0x)?([0-9a-f]{4})\z/iu ? : 2253 6 2259
Condition100%3/3
defined $id and $id =~ /\A(?:0x)?([0-9a-f]{4})\z/iu
!ll&&!rl&&r
512253
MC/DC100%2/2
defined $id and $id =~ /\A(?:0x)?([0-9a-f]{4})\z/iu
defined $id $id =~ /\A(?:0x)?([0-9a-f]{4})\z/iu
Truth table100%3/3
A and B
A= defined $idB= $id =~ /\A(?:0x)?([0-9a-f]{4})\z/iu
ABresult
0X0
100
111
626 }
627  
628 105 ▶sub _trim {
Subroutine _trim: called CC=2 SCAR=7.0
629 105 my ( $text ) = @_;
630 105 ▶ $text //= '';
Condition50%1/2
$text //= ""
l!l
1050
MC/DC0%0/1
$text //= ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
631 105 $text =~ s/\A\s+|\s+\z//g;
632 105 return $text;
633 }
634  
635 # As in Rex::GPU::NVIDIA: the invocant when called as a method on a class or
636 # object that isa Rex::GPU::AMD, undef when called as a function -- a first
637 # argument such as 'gpus' is no package of ours.
638 679 ▶sub _invocant {
Subroutine _invocant: called CC=5 SCAR=16.5
639 679 my ( $first ) = @_;
640 679 ▶ return unless defined $first && ( !ref $first || blessed $first );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first and !ref($first) || builtin::blessed($first) 16 663 679
Condition50%3/6
defined $first and !ref($first) || builtin::blessed($first)
!ll&&!rl&&r
160663
not ref $first or builtin::blessed($first)
l!l&&r!l&&!r
66300
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
641 663 ▶ return $first if eval { $first->isa(__PACKAGE__) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $first->isa(__PACKAGE__) } } 365 298 663
642 298 return;
643 }
644  
645 40 1;
646  
647 __END__
« lib/Rex/GPU/AMD/Hardware.pm ↑ Summary lib/Rex/GPU/NVIDIA/Requirement.pm »