blib/lib/Rex/GPU.pm

filter: Statementstmt 100% 106 / 106 Branchbran 94.4% 51 / 54 Conditioncond 79.1% 19 / 24 MC/DCmcdc 71.4% 10 / 14 Subroutinesub 100% 16 / 16 Podpod 100% 5 / 5 totaltotal 94.5% 207 / 219 CC 39 SCAR 27.3
CC
39
Cov
96%
gpu_setup
27.0
_check_connection
7.0
_invocant
5.5
CRAP 15.3
↑ Summary lib/Rex/GPU/NVIDIA/Setup/UbuntuDrivers.pm »
1 # ABSTRACT: GPU detection and driver management for Rex
2  
3 package Rex::GPU;
4 13 our $VERSION = '0.006';
5 13 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 ▶use Rex::GPU::Detect;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Rex::GPU::NVIDIA;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Rex::GPU::AMD ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 13 require Rex::Exporter;
15 13 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 13 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 13 @EXPORT = qw(
20 gpu_detect
21 gpu_setup
22 );
23  
24  
25  
26 14 ▶sub gpu_detect {
Subroutine gpu_detect: called CC=2 SCAR=7.1
Pod documented
27 14 ▶ my ( $class, %opts ) = _invocant(@_) ? @_ : ( __PACKAGE__, @_ );
Branch: 1 branch
BranchTrueFalseTotal
_invocant(@_) ? : 0 14 14
28 14 my $detect = $class->detect_class($opts{detect});
29 14 return $class->_step($detect, 'Rex::GPU::Detect', 'detect');
30 }
31  
32  
33 51 ▶sub _check_connection {
Subroutine _check_connection: called CC=7 SCAR=19.5
34 51 my ( $class ) = @_;
35 51 ▶ my $conn = Rex::get_current_connection() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $conn = Rex::get_current_connection() or return 8 43 51
36 8 ▶ return if Rex::is_local();
Branch: 1 branch
BranchTrueFalseTotal
if Rex::is_local() 1 7 8
37  
38 7 ▶ my $type = eval { $conn->{conn}->get_connection_type() } // '';
Condition100%2/2
eval { do { $conn->{"conn"}->get_connection_type } } // ""
l!l
61
MC/DC100%1/1
eval { do { $conn->{"conn"}->get_connection_type } } // ""
eval { do { $conn->{"conn"}->get_connection_type } }
Truth table100%2/2
A
A= eval { do { $conn->{"conn"}->get_connection_type } }
Aresult
11
00
39 7 ▶ return if $type eq 'LibSSH';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "LibSSH" 1 6 7
40  
41 6 my $sftp = eval { Rex::get_sftp() };
42 6 ▶ return if $sftp && eval { $sftp->stat('/'); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if $sftp and eval { do { $sftp->stat("/"); 1 } } 1 5 6
Condition100%3/3
$sftp and eval { do { $sftp->stat("/"); 1 } }
!ll&&!rl&&r
411
MC/DC100%2/2
$sftp and eval { do { $sftp->stat("/"); 1 } }
$sftp eval { do { $sftp->stat("/"); 1 } }
Truth table100%3/3
A and B
A= $sftpB= eval { do { $sftp->stat("/"); 1 } }
ABresult
0X0
100
111
43  
44 5 die "This host has no SFTP subsystem and you are not using the LibSSH "
45 . "connection backend.\n"
46 . "Add 'set connection => \"LibSSH\"' to your Rexfile and install "
47 . "Rex::LibSSH to deploy to SFTP-less hosts.\n";
48 }
49  
50 89 ▶sub gpu_setup {
Subroutine gpu_setup: called CC=27 SCAR=33.0
Pod documented
51 89 ▶ my ( $class, %opts ) = _invocant(@_) ? @_ : ( __PACKAGE__, @_ );
Branch: 1 branch
BranchTrueFalseTotal
_invocant(@_) ? : 0 89 89
52  
53 89 $class->_step($class, __PACKAGE__, '_check_connection');
54  
55 # A class of the user's (nvidia => / detect => / amd =>, set
56 # gpu_nvidia_class / gpu_detect_class / gpu_amd_class, karr #71, k1) that
57 # cannot be loaded dies here, before anything touches the host.
58 88 my $nvidia = $class->nvidia_class($opts{nvidia});
59 84 $class->detect_class($opts{detect});
60 80 my $amd = $class->amd_class($opts{amd});
61  
62 # A containerd_config typo dies here (karr #66), not after the driver
63 # install and a reboot.
64 77 ▶ my $runtime = $opts{containerd_config} // 'rke2';
Condition100%2/2
$opts{"containerd_config"} // "rke2"
l!l
2453
MC/DC100%1/1
$opts{"containerd_config"} // "rke2"
$opts{"containerd_config"}
Truth table100%2/2
A
A= $opts{"containerd_config"}
Aresult
11
00
65 77 $nvidia->_check_containerd_runtime($runtime, 'none');
66  
67 # A custom setup (setup => / set gpu_nvidia_setup, karr #34) that cannot be
68 # loaded dies here, before detection installs pciutils -- on every host, not
69 # only on one with a GPU.
70 73 $nvidia->custom_setup($opts{setup});
71  
72 my $gpus = $class->_step($class, __PACKAGE__, 'gpu_detect',
73 72 ▶ ( defined $opts{detect} ? ( detect => $opts{detect} ) : () ));
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'detect'} ? : 4 68 72
74  
75 72 ▶ if ($gpus->{nvidia} && @{$gpus->{nvidia}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gpus->{"nvidia"} and @{$gpus->{"nvidia"};}) 39 33 72
Condition66%2/3
$gpus->{"nvidia"} and @{$gpus->{"nvidia"};}
!ll&&!rl&&r
03339
MC/DC50%1/2
$gpus->{"nvidia"} and @{$gpus->{"nvidia"};}
$gpus->{"nvidia"} @{$gpus->{"nvidia"};}
Truth table66%2/3
A and B
A= $gpus->{"nvidia"}B= @{$gpus->{"nvidia"};}
ABresult
0X0
100
111
76 39 my @compute = grep { $_->{compute} } @{$gpus->{nvidia}};
77 39 ▶ if (@compute) {
Branch: 1 branch
BranchTrueFalseTotal
if (@compute) 34 5 39
78 34 Rex::Logger::info("CUDA-capable NVIDIA GPU: " . $_->{name}) for @compute;
79 # Every compute GPU (karr #33): the driver has to fit all of them.
80 # NVSwitch (karr #23): passed only when there is one, so every other
81 # host gets exactly the call it got before.
82 34 ▶ my $nvswitch = $gpus->{nvswitch} // [];
Condition100%2/2
$gpus->{"nvswitch"} // []
l!l
313
MC/DC100%1/1
$gpus->{"nvswitch"} // []
$gpus->{"nvswitch"}
Truth table100%2/2
A
A= $gpus->{"nvswitch"}
Aresult
11
00
83 34 ▶ Rex::Logger::info("NVSwitch host (".scalar(@$nvswitch)." NVSwitch): NVIDIA Fabric "
Branch: 1 branch
BranchTrueFalseTotal
if @$nvswitch 1 33 34
84 ."Manager is installed with the driver") if @$nvswitch;
85 $class->_step($nvidia, 'Rex::GPU::NVIDIA', 'install_driver',
86 reboot => ($opts{reboot} ? 1 : 0),
87 gpus => \@compute,
88 34 ▶ ( map { defined $opts{$_} ? ( $_ => $opts{$_} ) : () } qw( setup requirement ) ),
Branch: 3 branches
BranchTrueFalseTotal
$opts{'reboot'} ? : 5 29 34
defined $opts{$_} ? : 2 66 68
@$nvswitch ? : 1 33 34
89 ( @$nvswitch ? ( nvswitches => $nvswitch ) : () ),
90 );
91 33 $class->_step($nvidia, 'Rex::GPU::NVIDIA', 'install_container_toolkit');
92 33 $class->_step($nvidia, 'Rex::GPU::NVIDIA', 'generate_cdi_specs');
93  
94 33 ▶ if ($runtime ne 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if ($runtime ne "none") 26 7 33
95 26 $class->_step($nvidia, 'Rex::GPU::NVIDIA', 'configure_containerd', $runtime);
96 }
97  
98 # The full check, toolkit included, once the toolkit is there (karr
99 # #42); install_driver checks only the driver.
100 33 $class->_step($nvidia, 'Rex::GPU::NVIDIA', 'verify_nvidia');
101 }
102 }
103  
104 # AMD (karr k1): the amdgpu driver for every ROCm-capable AMD GPU -- an
105 # integrated one (APU) only with amd_integrated => 1. Every other one is
106 # named in a warning with the reason. Unknown (compute undef) gets nothing.
107 71 ▶ if ($gpus->{amd} && @{$gpus->{amd}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gpus->{"amd"} and @{$gpus->{"amd"};}) 41 30 71
Condition66%2/3
$gpus->{"amd"} and @{$gpus->{"amd"};}
!ll&&!rl&&r
03041
MC/DC50%1/2
$gpus->{"amd"} and @{$gpus->{"amd"};}
$gpus->{"amd"} @{$gpus->{"amd"};}
Truth table66%2/3
A and B
A= $gpus->{"amd"}B= @{$gpus->{"amd"};}
ABresult
0X0
100
111
108 41 my @install;
109 41 for my $gpu (@{$gpus->{amd}}) {
110 my $why = !defined $gpu->{compute} ? 'no hardware row for it, compute unknown'
111 : !$gpu->{compute} ? 'not ROCm-capable'
112 : $gpu->{integrated} && !$opts{amd_integrated}
113 61 ▶ ? 'integrated GPU (APU); pass amd_integrated => 1 to install for it'
Branch: 3 branches
BranchTrueFalseTotal
!defined($gpu->{'compute'}) ? : 5 56 61
!$gpu->{'compute'} ? : 4 52 56
$gpu->{'integrated'} && !$opts{'amd_integrated'} ? : 3 49 52
Condition100%3/3
$gpu->{"integrated"} and not $opts{"amd_integrated"}
!ll&&!rl&&r
34153
MC/DC100%2/2
$gpu->{"integrated"} and not $opts{"amd_integrated"}
$gpu->{"integrated"} not $opts{"amd_integrated"}
Truth table100%3/3
A and not(B)
A= $gpu->{"integrated"}B= not $opts{"amd_integrated"}
ABresult
0X0
100
111
114 : undef;
115 61 ▶ if (defined $why) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $why) 12 49 61
116 12 Rex::Logger::info("AMD GPU detected: $gpu->{name} -- no driver installed: $why", "warn");
117 12 next;
118 }
119 49 Rex::Logger::info("ROCm-capable AMD GPU: " . $gpu->{name});
120 49 push @install, $gpu;
121 }
122 41 ▶ if (@install) {
Branch: 1 branch
BranchTrueFalseTotal
if (@install) 34 7 41
123 {
124 # k90: a die for GPUs whose rows disagree (APU + amdgpu-dkms GPU,
125 # amd_integrated => 1) names gpu_setup's ways out, not method/gpus.
126 34 local $Rex::GPU::AMD::GPU_SETUP = 1;
127 # k104: "Nothing was changed on the host" is install_driver's own;
128 # gpu_detect (pciutils) and the NVIDIA steps may have changed the host
129 # before it, so here it is said for the AMD GPUs. k110: so are
130 # "Nothing was installed" (last) and "No driver package was installed"
131 # (last, or before ";") of the dies after apt-get update -- the NVIDIA
132 # steps may have installed their driver. Any other die (an object,
133 # another sentence) is passed on as it is.
134 eval {
135 $class->_step($amd, 'Rex::GPU::AMD', 'install_driver',
136 gpus => \@install,
137 34 ▶ reboot => ($opts{reboot} ? 1 : 0),
Branch: 1 branch
BranchTrueFalseTotal
$opts{'reboot'} ? : 5 29 34
138 );
139 16 1;
140 34 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $class->_step($amd, "Rex::GPU::AMD", "install_driver", "gpus", \@install, "reboot", $opts{"reboot"} ? 1 : 0); 1 } } or do { my $error = $@; my $noun = @install > 1 ? "GPUs" : "GPU"; unless (ref $error) { $error =~ s/ Nothing was changed on the host\n\z/ Nothing was changed for the AMD $noun\n/u; $error =~ s/ Nothing was installed\n\z/ Nothing was installed for the AMD $noun\n/u; $error =~ s/ No driver package was installed(?=;|\n\z)/ No driver package was installed for the AMD $noun/u; } ; die $error } 16 18 34
141 18 my $error = $@;
142 18 ▶ my $noun = @install > 1 ? 'GPUs' : 'GPU';
Branch: 1 branch
BranchTrueFalseTotal
@install > 1 ? : 13 5 18
143 18 ▶ unless (ref $error) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $error) 17 1 18
144 17 $error =~ s/ Nothing was changed on the host\n\z/ Nothing was changed for the AMD $noun\n/;
145 17 $error =~ s/ Nothing was installed\n\z/ Nothing was installed for the AMD $noun\n/;
146 17 $error =~ s/ No driver package was installed(?=;|\n\z)/ No driver package was installed for the AMD $noun/;
147 }
148 18 die $error;
149 };
150 }
151 16 $class->_step($amd, 'Rex::GPU::AMD', 'verify_amd', gpus => \@install);
152 }
153 }
154  
155 53 return $gpus;
156 }
157  
158  
159 88 ▶sub nvidia_class {
Subroutine nvidia_class: called CC=1 SCAR=0.0
Pod documented
160 88 my ( $class, $nvidia ) = @_;
161 88 return Rex::GPU::NVIDIA->configured_class($nvidia);
162 }
163  
164  
165 98 ▶sub detect_class {
Subroutine detect_class: called CC=1 SCAR=0.0
Pod documented
166 98 my ( $class, $detect ) = @_;
167 98 return Rex::GPU::Detect->configured_class($detect);
168 }
169  
170  
171 84 ▶sub amd_class {
Subroutine amd_class: called CC=1 SCAR=0.0
Pod documented
172 84 my ( $class, $amd ) = @_;
173 84 return Rex::GPU::AMD->configured_class($amd);
174 }
175  
176 # One step of the pipeline. With the built-in class ($target eq $base) it is
177 # called through its function name, with exactly the arguments it always got:
178 # Rex::Rancher's tests, kubernetes-ocp's and t/ replace those globs
179 # (*Rex::GPU::NVIDIA::install_driver, *Rex::GPU::gpu_detect, ...) and record
180 # @_. A class of the user's gets a method call instead.
181 384 ▶sub _step {
Subroutine _step: called CC=2 SCAR=6.9
182 384 my ( $class, $target, $base, $name, @args ) = @_;
183 384 ▶ return $target eq $base ? $base->can($name)->(@args) : $target->$name(@args);
Branch: 1 branch
BranchTrueFalseTotal
$target eq $base ? : 359 25 384
184 }
185  
186 # Called as a method (Rex::GPU->gpu_setup, My::GPU->gpu_setup): the invocant,
187 # a class name or object that isa Rex::GPU. Called as a function
188 # (gpu_setup(...) from a Rexfile, Rex::GPU::gpu_setup(...)): undef -- a first
189 # argument such as 'containerd_config' is no package of ours, and nothing is
190 # loaded to find out.
191 103 ▶sub _invocant {
Subroutine _invocant: called CC=5 SCAR=17.0
192 103 my ( $first ) = @_;
193 103 ▶ return unless defined $first && ( !ref $first || blessed $first );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first and !ref($first) || builtin::blessed($first) 31 72 103
Condition50%3/6
defined $first and !ref($first) || builtin::blessed($first)
!ll&&!rl&&r
31072
not ref $first or builtin::blessed($first)
l!l&&r!l&&!r
7200
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
194 72 ▶ return $first if eval { $first->isa(__PACKAGE__) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $first->isa(__PACKAGE__) } } 0 72 72
195 72 return;
196 }
197  
198 13 1;
199  
200 __END__
↑ Summary lib/Rex/GPU/NVIDIA/Setup/UbuntuDrivers.pm »