bin/storage2dot

filter: Statementstmt 50.6% 81 / 160 Branchbran 36.8% 28 / 76 Conditioncond 46.6% 7 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 90.9% 10 / 11 Podpod n/a totaltotal 46.8% 126 / 269 CC 44 SCAR 46.3
CC
44
Cov
46%
collect_from_remote
245.9
main
50.5
END
2.5
CRAP 102.9
« bin/storage-merge-dots ↑ Summary blib/lib/StorageDisplay/Data/Loop.pm »
1 #!/usr/bin/env perl
2 #
3 # This file is part of StorageDisplay
4 #
5 # This software is copyright (c) 2014-2023 by Vincent Danjean.
6 #
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 #
10  
11 # PODNAME: storage2dot
12 # ABSTRACT: analyse and generate a graphical view of a machine storage
13  
14  
15 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 16 our $VERSION = '2.06'; # VERSION
19  
20 # delay module loading so that local data collect can be done
21 # without extra modules
22 # use StorageDisplay;
23 16 ▶use StorageDisplay::Collect;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 16 my $cleanup_readmode=0;
26 0 ▶sub collect_from_remote {
Subroutine collect_from_remote: not called CC=17 SCAR=55.0
27 0 my $remote = shift;
28 0 my $content='';
29 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Net::OpenSSH; "Net::OpenSSH"->import; require Term::ReadKey; "Term::ReadKey"->import; 1 } } or die "Cannot load required modules (Net::OpenSSH and/or Term::ReadKey) for remote data collect: $!\n" 0 0 0
30 0 require Net::OpenSSH;
31 0 Net::OpenSSH->import;
32 0 require Term::ReadKey;
33 0 Term::ReadKey->import;
34 0 1;
35 } or die "Cannot load required modules (Net::OpenSSH and/or Term::ReadKey) for remote data collect: $!\n";
36 16 ▶ END {
Subroutine END: called CC=2 SCAR=9.2
37 16 ▶ if ($cleanup_readmode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cleanup_readmode) 0 16 16
38 # in case of bug, always restore normal mode
39 0 ReadMode('normal');
40 }
41 }
42 0 my $ssh = Net::OpenSSH->new($remote);
43 0 ▶ $ssh->error and
Branch: 1 branch
BranchTrueFalseTotal
$ssh->error and die q[Couldn't establish SSH connection: ] . $ssh->error 0 0 0
44 die "Couldn't establish SSH connection: ". $ssh->error;
45  
46 0 my ($in, $out, $pid) = $ssh->open2(
47 #'cat',
48 'perl', '--', '-',
49 );
50  
51 0 my $fdperlmod;
52 open($fdperlmod, '<', $INC{'StorageDisplay/Collect.pm'})
53 0 ▶ or die "Cannot open ".INC{'StorageDisplay/Collect.pm'}.": $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fdperlmod, "<", $INC{"StorageDisplay/Collect.pm"} or die 'Cannot open ' . do { "StorageDisplay/Collect.pm" }->INC . ": $!\n" 0 0 0
54 #use Sys::Syscall;
55 #Sys::Syscall::sendfile($in, $fdperlmod);
56 {
57 0 ▶ while(defined(my $line=<$fdperlmod>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fdperlmod)) 0 0 0
58 0 ▶ last if $line =~ m/^__END__\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^__END__\s*$/ 0 0 0
59 0 print $in $line;
60 }
61 0 close $fdperlmod;
62 }
63 #print $in "StorageDisplay::Collect::dump_collect;\n";
64 0 my @args = (@_, 'LocalBySSH');
65 0 my $cmd = "StorageDisplay::Collect::dump_collect('".join("','", @args)."');\n";
66 0 print STDERR 'Running through SSH: ',$cmd;
67 0 print $in $cmd;
68 0 print $in "__END__\n";
69 0 flush $in;
70  
71 16 ▶ use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 16 ▶ use POSIX ":sys_wait_h";
Subroutine BEGIN: called CC=1 SCAR=0.0
73 0 my $sel = IO::Select->new(\*STDIN, $out);
74 0 my $timeout = 1;
75 0 $cleanup_readmode=1;
76 0 ReadMode('noecho');
77 0 my ($in_closed,$out_closed) = (0,0);
78 0 while(1) {
79 0 $!=0;
80 0 my @ready = $sel->can_read($timeout);
81 0 ▶ if ($!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 0 0 0
82 0 die "Error with select: $!\n";
83 }
84 0 ▶ if (scalar(@ready)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @ready) { } 0 0 0
85 0 foreach my $fd (@ready) {
86 0 ▶ if ($fd == $out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fd == $out) { } 0 0 0
87 0 my $line=<$out>;
88 0 ▶ if (defined($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) { } 0 0 0
89 0 $content .= $line;
90 } else {
91 0 $sel->remove($out);
92 0 close $out;
93 0 $out_closed=1;
94 }
95 } else {
96 0 my $line=<STDIN>;
97 0 ▶ if (print $in $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (print $in $line) { } 0 0 0
98 0 flush $in;
99 } else {
100 0 $sel->remove(\*STDIN);
101 0 close $in;
102 0 $in_closed=1;
103 }
104 }
105 }
106 } else {
107 0 my $res = waitpid($pid, WNOHANG);
108 0 ▶ if ($res==-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res == -1) 0 0 0
109 0 die "Some error occurred ".($? >> 8).": $!\n";
110 }
111 0 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 0 0 0
112 0 ▶ if (!$in_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_closed) 0 0 0
113 0 $sel->remove(\*STDIN);
114 0 close $in;
115 }
116 0 ReadMode('normal');
117 0 last;
118 }
119 #print STDERR "timeout for $pid\n";
120 }
121 }
122 0 ▶ if (!$out_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out_closed) 0 0 0
123 0 ▶ while (defined(my $line=<$out>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $out)) 0 0 0
124 0 $content .= $out;
125 }
126 0 $sel->remove($out);
127 0 close $out;
128 }
129 0 ReadMode('normal');
130 0 $cleanup_readmode=0;
131 0 return $content;
132 }
133  
134 16 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
135 16 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
136 16 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
137 16 $Data::Dumper::Sortkeys = 1;
138 16 $Data::Dumper::Purity = 1;
139  
140 #use Carp::Always;
141  
142 16 my $remote;
143 my $data;
144 16 my $output;
145  
146 16 my $collect;
147 16 my $recordfile;
148 16 my $replayfile;
149  
150 16 my $verbose;
151 16 my $help;
152 16 my $man;
153  
154 16 ▶GetOptions ("d|data=s" => \$data, # string
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("d|data=s", \$data, "r|remote=s", \$remote, "o|output=s", \$output, "c|collect-only", \$collect, "record-file=s", \$recordfile, "replay-file=s", \$replayfile, "verbose", \$verbose, "h|help", \$help, "man", \$man) or pod2usage(2) 16 0 16
155 "r|remote=s" => \$remote, # string
156 "o|output=s" => \$output, # string
157 "c|collect-only" => \$collect, # flag
158 "record-file=s" => \$recordfile, # string
159 "replay-file=s" => \$replayfile, # string
160 "verbose" => \$verbose, # flag
161 "h|help" => \$help, # flag
162 "man" => \$man, # flag
163 ) or pod2usage(2);
164  
165 16 ▶sub main() {
Subroutine main: called CC=27 SCAR=39.2
166 16 ▶ pod2usage(-exitval => 0, -verbose => 1) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 1 15 16
167 15 ▶ pod2usage(-exitval => 0, -verbose => 2) if $man;
Branch: 1 branch
BranchTrueFalseTotal
if $man 1 14 15
168  
169 14 ▶ if (defined($data) && (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data and defined $remote || defined $recordfile || defined $replayfile) 0 14 14
Condition44%4/9
defined $data and defined $remote || defined $recordfile || defined $replayfile
!ll&&!rl&&r
770
defined $remote or defined $recordfile or defined $replayfile
l!l&&r!l&&!r
007
defined $remote or defined $recordfile
l!l&&r!l&&!r
007
MC/DC0%0/4
defined $data and defined $remote || defined $recordfile || defined $replayfile
defined $data defined $remote defined $recordfile defined $replayfile
Truth table40%2/5
A and B or C or D
A= defined $dataB= defined $remoteC= defined $recordfileD= defined $replayfile
ABCDresult
0XXX0
10000
11XX1
101X1
10011
170 defined($remote)
171 || defined($recordfile)
172 || defined($replayfile)
173 )) {
174 0 die "E: --data cannot be used with --remote, --record, nor --replay\n";
175 }
176  
177 14 ▶ if (defined($replayfile) && (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replayfile and defined $remote || defined $recordfile) 0 14 14
Condition50%3/6
defined $replayfile and defined $remote || defined $recordfile
!ll&&!rl&&r
770
defined $remote or defined $recordfile
l!l&&r!l&&!r
007
MC/DC0%0/3
defined $replayfile and defined $remote || defined $recordfile
defined $replayfile defined $remote defined $recordfile
Truth table50%2/4
A and B or C
A= defined $replayfileB= defined $remoteC= defined $recordfile
ABCresult
0XX0
1000
11X1
1011
178 defined($remote)
179 || defined($recordfile)
180 )) {
181 0 die "E: --replay cannot be used with --remote, nor --record\n";
182 }
183  
184 14 ▶ if (!$collect) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($collect) 7 7 14
185 require StorageDisplay or
186 7 ▶ die "Cannot load the StorageDisplay module to handle collected data: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
require StorageDisplay or die "Cannot load the StorageDisplay module to handle collected data: $!\n" 7 0 7
187 }
188  
189 14 my $infos;
190  
191 14 ▶ if ($replayfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($replayfile) 7 7 14
192 require StorageDisplay::Collect::CMD::Replay or
193 7 ▶ die "Replay requested, but unable to load the StorageDisplay::Collect::CMD::Replay module: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
require StorageDisplay::Collect::CMD::Replay or die "Replay requested, but unable to load the StorageDisplay::Collect::CMD::Replay module: $!\n" 7 0 7
194 7 my $dh;
195 7 ▶ open($dh, "<", $replayfile)
Branch: 1 branch
BranchTrueFalseTotal
open $dh, "<", $replayfile or die "Cannot open '${replayfile}': $!" 7 0 7
196 or die "Cannot open '$replayfile': $!" ;
197 7 my $replay=join('', <$dh>);
198 7 my $replaydata;
199 7 close($dh);
200 {
201 7 my $VAR1;
202 7 eval($replay); ## no critic (ProhibitStringyEval)
203 #print STDERR "c: $content\n";
204 7 $replaydata = $VAR1;
205 }
206 7 $infos = StorageDisplay::Collect->new(
207 'Replay', 'replay-data' => $replaydata)->collect();
208 }
209  
210 14 my $contents;
211 my @recorder;
212 14 ▶ if (defined($recordfile)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $recordfile) 0 14 14
213 0 @recorder = ('Proxy::Recorder', 'recorder-reader');
214 }
215 14 ▶ if (defined($data)) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $data) { } 7 7 14
elsif (defined $remote) { } 0 7 7
elsif (not defined $infos) { } 0 7 7
216 7 my $dh;
217 7 ▶ open($dh, "<", $data)
Branch: 1 branch
BranchTrueFalseTotal
open $dh, "<", $data or die "Cannot open '${data}': $!" 7 0 7
218 or die "Cannot open '$data': $!" ;
219 7 $contents=join('', <$dh>);
220 7 close($dh);
221 } elsif (defined($remote)) {
222 0 $contents = collect_from_remote($remote, @recorder);
223 } elsif (not defined($infos)) {
224 0 $infos = StorageDisplay::Collect->new(@recorder, 'Local')->collect();
225 }
226  
227 # data are in $contents (if got through Data::Dumper) or directly in $infos
228 14 ▶ if (defined($contents)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $contents) 7 7 14
229 # moving data from $contents to $infos
230 {
231 7 my $VAR1;
232 7 eval($contents); ## no critic (ProhibitStringyEval)
233 #print STDERR "c: $content\n";
234 7 $infos = $VAR1;
235 }
236 }
237  
238 14 ▶ if (defined($recordfile)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $recordfile) 0 14 14
239 0 ▶ if (! exists($infos->{'recorder'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $infos->{'recorder'}) { } 0 0 0
240 0 print STDERR "W: skpping recording: no records!\n";
241 } else {
242 0 my $dh;
243 0 ▶ open($dh, ">", $recordfile)
Branch: 1 branch
BranchTrueFalseTotal
open $dh, ">", $recordfile or die "Cannot open '${data}': $!" 0 0 0
244 or die "Cannot open '$data': $!";
245 0 print $dh Dumper($infos->{'recorder'});
246 0 close($dh);
247 }
248 }
249 14 delete($infos->{'recorder'});
250  
251 14 my $oldout;
252 14 ▶ if (defined($output)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output) 14 0 14
253 # dzil do not want Two-argument "open"
254 # so, commented-out as we do not use it
255 # if this change, a way to write this would have to be found
256 # open(my $oldout, ">&STDOUT") or die "Can't dup STDOUT: $!";
257 14 ▶ open(STDOUT, '>', $output) or die "Can't redirect STDOUT to $output: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", $output or die "Can't redirect STDOUT to ${output}: $!" 14 0 14
258 }
259  
260 14 ▶ if ($collect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($collect) 7 7 14
261 7 print Dumper($infos);
262 7 return;
263 }
264 7 my $st=StorageDisplay->new('infos' => $infos);
265  
266 7 $st->createElems();
267 7 $st->display;
268 }
269  
270 16 main
271  
272 __END__
« bin/storage-merge-dots ↑ Summary blib/lib/StorageDisplay/Data/Loop.pm »