4 use File::Basename qw(&dirname);
7 use Statistics::Basic::StdDev;
14 open F,"$fname" or die $fname;
16 defined(my $r=<F>) or die $fname;
17 close F or die $fname;
22 # ==> build/Cadence-1.0.0-0.12.20200504git5787908.fc33.src.rpm.dtneeded <==
23 # /usr/lib/debug/.dwz/Cadence isdwzcommon 479079 NA
24 # /usr/bin/cadence-jackmeter /usr/lib/debug/.dwz/Cadence 717792 1329040
25 # /usr/bin/cadence-xycontroller /usr/lib/debug/.dwz/Cadence 1763616 2369832
26 # ==> build/CVector-1.0.3.1-21.fc33.src.rpm.dtneeded <==
27 # /usr/lib64/libCVector-1.0.3.so.2.0.0 nodwzcommon 28088 28896
28 for my $dtneededfn (glob "build/*.dtneeded") {
30 open F,$dtneededfn or die "$dtneededfn: $1";
34 my($fn,$dwzcommon,$dwzsize,$dtsize)=/^(\S+) (\S+) (\S+) (\S+)$/ or die "$dtneededfn: $_";
35 die if $dwzcommon eq "isdwzcommon" && $dtsize ne "NA";
38 die if $dwzcommon eq "isdwzcommon";
41 $$ref=[$dwzcommon,$dwzsize,$dtsize];
44 close F or die "close $dtneededfn: $1";
47 chdir "dtneeded.out" or die "dtneeded.out: $!";
56 die $File::Find::dir if $File::Find::dir=~m{/$};
57 my $binfn=$File::Find::name;
59 my $target=readlink $binfn or die $binfn;
64 #warn "$binfn,target=rel=$$ref\n";
66 my $base=dirname(".$binfn");
67 my $abs=File::Spec->rel2abs($target,$base);
68 $final="/".File::Spec->abs2rel($abs);
69 #warn "$binfn,base=$base,target=$target,abs=$abs,rel=$$ref\n";
71 1 while $final=~s{/[^/]+/[.][.]/}{/};
72 1 while $final=~s{/[^/]+/[.][.]$}{};
73 if ($final=~m{/[.][.]}) {
74 warn "$binfn,target=$target,final=$final\n";
77 my $ref=\$SYMLINK{$binfn};
83 die $binfn if $binfn!~m{^[.]/};
84 my $bin=readfile $binfn;
85 $binfn=~s{^[.]}{} or die;
86 my $rpath=($bin=~m{^.*\Q(R\E(?:UN)?\QPATH)\E\s*Library r(?:un)?path: \Q[\E(.*)\Q]\E$}m)[0];
88 my $dirname=$File::Find::dir;
89 die if $dirname!~s{^[.]/}{/};
90 $rpath=~s{\$ORIGIN}{$dirname}g;
92 my $soname=($bin=~m{^.*\Q(SONAME)\E\s*Library soname: \Q[\E(.*)\Q]\E$}m)[0];
95 "needed"=>[$bin=~m{^\s*0x0000000000000001\s*\Q(NEEDED)\E\s*\QShared library: [\E(.*)\Q]\E$}gm],
97 $h->{"rpath"}=$rpath if $rpath;
99 #warn "$binfn=".Dumper($h)."\n" if $binfn=~/libc.so.6/;
100 # my $total=keys %F; warn "$total...\n" if 0==$total%1000;
102 my $sonamefn=$File::Find::dir."/".$soname;
103 $sonamefn=~s{^[.]/}{/} or die $sonamefn;
104 my $ref=\$SONAME{$sonamefn};
105 # warn "soname=<$sonamefn> <$binfn> vs. <".$$ref->{"binfn"}.">\n" if $$ref;
108 push @DEBUG,$binfn if $bin=~m{ \Q(DEBUG) \E };
112 #while (my($src,$target)=each %SYMLINK) {
113 # die "$src->$target" if exists $SYMLINK{$target};
117 my $dwzsizeduplall=0;
122 BINFN: for my $binfn (@DEBUG) {
123 # warn "$binfn...\n".Dumper([sort @{$F{$binfn}{"needed"}}]);
124 die $binfn if exists $SYMLINK{$binfn};
130 for my $needed (@{$h->{"needed"}}) {
132 if ($needed=~m{^/}) {
133 $needed=$SYMLINK{$needed} while exists $SYMLINK{$needed};
136 # die "$binfn: $l: $needed" if $needed=~m{/};
138 my $rpath=$h->{"rpath"};
139 push @rpath,split /:/,$rpath if $rpath;
140 push @rpath,qw(/lib64 /usr/lib64);
141 for my $rpath (@rpath) {
143 warn "$binfn: $l: $rpath";
146 my $fn="$rpath/$needed";
147 $fn=$SYMLINK{$fn} while exists $SYMLINK{$fn};
148 next if !$SONAME{$fn};
152 warn "$binfn: $l: $needed not found; rpath=".join(":",@rpath)."\n" if !$found;
154 push @l,$found if $found&&!$l{$found}++;
157 #warn Dumper $binfn,\%l;
159 my $dwzsizedupltot=0;
162 for my $l (keys(%l)) {
165 warn "$binfn: $l: missing\n";
169 warn "$binfn: $l: ambiguous\n";
172 my $dtsize=$ref->[2];
173 die if !defined $dtsize;
175 my $dwzsize=$ref->[1];
176 die if !defined $dwzsize;
177 $dwzsizetot+=$dwzsize;
178 $dwzsizedupltot+=$dwzsize;
180 my $dwzcommon=$ref->[0];
181 next if $dwzcommon eq "nodwzcommon";
182 die if $dwzcommon eq "isdwzcommon";
183 my $duplicate=$dwzcommons{$dwzcommon}++;
184 my $dwzcommonref=$D{$dwzcommon};
185 die if !$dwzcommonref;
186 die if $dwzcommonref->[0] ne "isdwzcommon";
187 die if $dwzcommonref->[2] ne "NA";
188 my $dwzcommonsize=$dwzcommonref->[1];
189 $dwzsizetot+=$dwzcommonsize if !$duplicate;
190 $dwzsizedupltot+=$dwzcommonsize;
192 print "$binfn: dwzsizetot=$dwzsizetot dtsizetot=$dtsizetot\n";
193 warn "$binfn: ".Dumper(\%dwzcommons);
194 $dwzsizeall+=$dwzsizetot;
195 $dwzsizeduplall+=$dwzsizedupltot;
196 $dtsizeall+=$dtsizetot;
197 push @ratioall ,$dwzsizetot /$dtsizetot;
198 push @ratioduplall,$dwzsizedupltot/$dtsizetot;
199 # warn "$binfn done\n".Dumper([sort keys(%l)]);
201 print "dwzsizeall =$dwzsizeall" ." dtsizeall=$dtsizeall =".$dwzsizeall /$dtsizeall." avg=".List::Util::sum(@ratioall )/@ratioall ." stddev=".Statistics::Basic::stddev(\@ratioall )."\n";
202 print "dwzsizeduplall=$dwzsizeduplall"." dtsizeall=$dtsizeall =".$dwzsizeduplall/$dtsizeall." avg=".List::Util::sum(@ratioduplall)/@ratioduplall." stddev=".Statistics::Basic::stddev(\@ratioduplall)."\n";
203 print "computed=$computed of DEBUG=".(0+@DEBUG)." =".$computed/@DEBUG."\n";
204 #print Dumper \@ratioall;