blib/lib/Rex/GPU.pm

filter: Statementstmt 100% 78 / 78 Branchbran 91.6% 33 / 36 Conditioncond 76.1% 16 / 21 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 92.7% 153 / 165 CC 28 SCAR 21.4
CC
28
Cov
94%
gpu_setup
16.0
_check_connection
7.0
_invocant
5.5
CRAP 8.5
« lib/Rex/GPU/NVIDIA/Requirement.pm ↑ Summary lib/Rex/GPU/NVIDIA.pm »
1 # ABSTRACT: GPU detection and driver management for Rex
2  
3 package Rex::GPU;
4 10 our $VERSION = '0.005';
5 10 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10 ▶use Rex::GPU::Detect;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 10 ▶use Rex::GPU::NVIDIA;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 10 require Rex::Exporter;
14 10 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 10 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 10 @EXPORT = qw(
19 gpu_detect
20 gpu_setup
21 );
22  
23  
24  
25 14 ▶sub gpu_detect {
Subroutine gpu_detect: called CC=2 SCAR=7.1
Pod documented
26 14 ▶ my ( $class, %opts ) = _invocant(@_) ? @_ : ( __PACKAGE__, @_ );
Branch: 1 branch
BranchTrueFalseTotal
_invocant(@_) ? : 0 14 14
27 14 my $detect = $class->detect_class($opts{detect});
28 14 return $class->_step($detect, 'Rex::GPU::Detect', 'detect');
29 }
30  
31  
32 26 ▶sub _check_connection {
Subroutine _check_connection: called CC=7 SCAR=19.5
33 26 my ( $class ) = @_;
34 26 ▶ my $conn = Rex::get_current_connection() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $conn = Rex::get_current_connection() or return 8 18 26
35 8 ▶ return if Rex::is_local();
Branch: 1 branch
BranchTrueFalseTotal
if Rex::is_local() 1 7 8
36  
37 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
38 7 ▶ return if $type eq 'LibSSH';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "LibSSH" 1 6 7
39  
40 6 my $sftp = eval { Rex::get_sftp() };
41 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
42  
43 5 die "This host has no SFTP subsystem and you are not using the LibSSH "
44 . "connection backend.\n"
45 . "Add 'set connection => \"LibSSH\"' to your Rexfile and install "
46 . "Rex::LibSSH to deploy to SFTP-less hosts.\n";
47 }
48  
49 46 ▶sub gpu_setup {
Subroutine gpu_setup: called CC=16 SCAR=27.7
Pod documented
50 46 ▶ my ( $class, %opts ) = _invocant(@_) ? @_ : ( __PACKAGE__, @_ );
Branch: 1 branch
BranchTrueFalseTotal
_invocant(@_) ? : 0 46 46
51  
52 46 $class->_step($class, __PACKAGE__, '_check_connection');
53  
54 # A class of the user's (nvidia => / detect =>, set gpu_nvidia_class /
55 # gpu_detect_class, karr #71) that cannot be loaded dies here, before
56 # anything touches the host.
57 45 my $nvidia = $class->nvidia_class($opts{nvidia});
58 41 $class->detect_class($opts{detect});
59  
60 # A containerd_config typo dies here (karr #66), not after the driver
61 # install and a reboot.
62 37 ▶ my $runtime = $opts{containerd_config} // 'rke2';
Condition100%2/2
$opts{"containerd_config"} // "rke2"
l!l
1720
MC/DC100%1/1
$opts{"containerd_config"} // "rke2"
$opts{"containerd_config"}
Truth table100%2/2
A
A= $opts{"containerd_config"}
Aresult
11
00
63 37 $nvidia->_check_containerd_runtime($runtime, 'none');
64  
65 # A custom setup (setup => / set gpu_nvidia_setup, karr #34) that cannot be
66 # loaded dies here, before detection installs pciutils -- on every host, not
67 # only on one with a GPU.
68 34 $nvidia->custom_setup($opts{setup});
69  
70 my $gpus = $class->_step($class, __PACKAGE__, 'gpu_detect',
71 33 ▶ ( defined $opts{detect} ? ( detect => $opts{detect} ) : () ));
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'detect'} ? : 4 29 33
72  
73 33 ▶ if ($gpus->{nvidia} && @{$gpus->{nvidia}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gpus->{"nvidia"} and @{$gpus->{"nvidia"};}) 31 2 33
Condition66%2/3
$gpus->{"nvidia"} and @{$gpus->{"nvidia"};}
!ll&&!rl&&r
0231
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
74 31 my @compute = grep { $_->{compute} } @{$gpus->{nvidia}};
75 31 ▶ if (@compute) {
Branch: 1 branch
BranchTrueFalseTotal
if (@compute) 26 5 31
76 26 Rex::Logger::info("CUDA-capable NVIDIA GPU: " . $_->{name}) for @compute;
77 # Every compute GPU (karr #33): the driver has to fit all of them.
78 # NVSwitch (karr #23): passed only when there is one, so every other
79 # host gets exactly the call it got before.
80 26 ▶ my $nvswitch = $gpus->{nvswitch} // [];
Condition100%2/2
$gpus->{"nvswitch"} // []
l!l
233
MC/DC100%1/1
$gpus->{"nvswitch"} // []
$gpus->{"nvswitch"}
Truth table100%2/2
A
A= $gpus->{"nvswitch"}
Aresult
11
00
81 26 ▶ Rex::Logger::info("NVSwitch host (".scalar(@$nvswitch)." NVSwitch): NVIDIA Fabric "
Branch: 1 branch
BranchTrueFalseTotal
if @$nvswitch 1 25 26
82 ."Manager is installed with the driver") if @$nvswitch;
83 $class->_step($nvidia, 'Rex::GPU::NVIDIA', 'install_driver',
84 reboot => ($opts{reboot} ? 1 : 0),
85 gpus => \@compute,
86 26 ▶ ( map { defined $opts{$_} ? ( $_ => $opts{$_} ) : () } qw( setup requirement ) ),
Branch: 3 branches
BranchTrueFalseTotal
$opts{'reboot'} ? : 5 21 26
defined $opts{$_} ? : 2 50 52
@$nvswitch ? : 1 25 26
87 ( @$nvswitch ? ( nvswitches => $nvswitch ) : () ),
88 );
89 25 $class->_step($nvidia, 'Rex::GPU::NVIDIA', 'install_container_toolkit');
90 25 $class->_step($nvidia, 'Rex::GPU::NVIDIA', 'generate_cdi_specs');
91  
92 25 ▶ if ($runtime ne 'none') {
Branch: 1 branch
BranchTrueFalseTotal
if ($runtime ne "none") 18 7 25
93 18 $class->_step($nvidia, 'Rex::GPU::NVIDIA', 'configure_containerd', $runtime);
94 }
95  
96 # The full check, toolkit included, once the toolkit is there (karr
97 # #42); install_driver checks only the driver.
98 25 $class->_step($nvidia, 'Rex::GPU::NVIDIA', 'verify_nvidia');
99 }
100 }
101  
102 32 ▶ if ($gpus->{amd} && @{$gpus->{amd}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gpus->{"amd"} and @{$gpus->{"amd"};}) 2 30 32
Condition66%2/3
$gpus->{"amd"} and @{$gpus->{"amd"};}
!ll&&!rl&&r
0302
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
103 2 Rex::Logger::info("AMD GPU detected — driver support not yet implemented", "warn");
104 }
105  
106 32 return $gpus;
107 }
108  
109  
110 45 ▶sub nvidia_class {
Subroutine nvidia_class: called CC=1 SCAR=0.0
Pod documented
111 45 my ( $class, $nvidia ) = @_;
112 45 return Rex::GPU::NVIDIA->configured_class($nvidia);
113 }
114  
115  
116 55 ▶sub detect_class {
Subroutine detect_class: called CC=1 SCAR=0.0
Pod documented
117 55 my ( $class, $detect ) = @_;
118 55 return Rex::GPU::Detect->configured_class($detect);
119 }
120  
121 # One step of the pipeline. With the built-in class ($target eq $base) it is
122 # called through its function name, with exactly the arguments it always got:
123 # Rex::Rancher's tests, kubernetes-ocp's and t/ replace those globs
124 # (*Rex::GPU::NVIDIA::install_driver, *Rex::GPU::gpu_detect, ...) and record
125 # @_. A class of the user's gets a method call instead.
126 212 ▶sub _step {
Subroutine _step: called CC=2 SCAR=6.9
127 212 my ( $class, $target, $base, $name, @args ) = @_;
128 212 ▶ return $target eq $base ? $base->can($name)->(@args) : $target->$name(@args);
Branch: 1 branch
BranchTrueFalseTotal
$target eq $base ? : 194 18 212
129 }
130  
131 # Called as a method (Rex::GPU->gpu_setup, My::GPU->gpu_setup): the invocant,
132 # a class name or object that isa Rex::GPU. Called as a function
133 # (gpu_setup(...) from a Rexfile, Rex::GPU::gpu_setup(...)): undef -- a first
134 # argument such as 'containerd_config' is no package of ours, and nothing is
135 # loaded to find out.
136 60 ▶sub _invocant {
Subroutine _invocant: called CC=5 SCAR=17.0
137 60 my ( $first ) = @_;
138 60 ▶ return unless defined $first && ( !ref $first || blessed $first );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first and !ref($first) || builtin::blessed($first) 22 38 60
Condition50%3/6
defined $first and !ref($first) || builtin::blessed($first)
!ll&&!rl&&r
22038
not ref $first or builtin::blessed($first)
l!l&&r!l&&!r
3800
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
139 38 ▶ return $first if eval { $first->isa(__PACKAGE__) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $first->isa(__PACKAGE__) } } 0 38 38
140 38 return;
141 }
142  
143 10 1;
144  
145 __END__
« lib/Rex/GPU/NVIDIA/Requirement.pm ↑ Summary lib/Rex/GPU/NVIDIA.pm »