Add PRE_ATTR and use it for array indices.
[p5sagit/Devel-Size.git] / memnodes.pl
CommitLineData
5aa3ad8e 1#!/usr/bin/env perl
2c631ee0 2
3use strict;
4use warnings;
5aa3ad8e 5use autodie;
2c631ee0 6
e8f4c506 7use DBI qw(looks_like_number);
b2fc39a5 8use DBD::SQLite;
f60f09e5 9use JSON::XS;
de73b186 10use Devel::Dwarn;
b2fc39a5 11
fc6614ee 12use Getopt::Long;
13
de73b186 14# XXX import these from the XS code
15use constant NPtype_NAME => 0x01;
16use constant NPtype_LINK => 0x02;
17use constant NPtype_SV => 0x03;
18use constant NPtype_MAGIC => 0x04;
19use constant NPtype_OP => 0x05;
20
21use constant NPattr_LEAFSIZE => 0x00;
22use constant NPattr_NAME => 0x01;
23use constant NPattr_PADFAKE => 0x02;
24use constant NPattr_PADNAME => 0x03;
25use constant NPattr_PADTMP => 0x04;
26use constant NPattr_NOTE => 0x05;
68cafb30 27use constant NPattr_PRE_ATTR => 0x06;
de73b186 28
29
fc6614ee 30GetOptions(
5aa3ad8e 31 'text!' => \my $opt_text,
32 'dot=s' => \my $opt_dot,
b2fc39a5 33 'db=s' => \my $opt_db,
e8f4c506 34 'verbose|v!' => \my $opt_verbose,
35 'debug|d!' => \my $opt_debug,
98128850 36 'showid!' => \my $opt_showid,
fc6614ee 37) or exit 1;
94fab3d1 38
f60f09e5 39my $j = JSON::XS->new->ascii->pretty(0);
40
1915946b 41my ($dbh, $node_ins_sth);
42if ($opt_db) {
43 $dbh = DBI->connect("dbi:SQLite:dbname=$opt_db","","", {
44 RaiseError => 1, PrintError => 0, AutoCommit => 0
45 });
46 $dbh->do("PRAGMA synchronous = OFF");
47 $dbh->do("DROP TABLE IF EXISTS node");
48 $dbh->do(q{
49 CREATE TABLE node (
50 id integer primary key,
51 name text,
52 title text,
98128850 53 type integer,
1915946b 54 depth integer,
55 parent_id integer,
56
57 self_size integer,
58 kids_size integer,
59 kids_node_count integer,
60 child_ids text,
61 attr_json text,
62 leaves_json text
63 )
64 });
65 $node_ins_sth = $dbh->prepare(q{
98128850 66 INSERT INTO node VALUES (?,?,?,?,?,?, ?,?,?,?,?,?)
1915946b 67 });
68}
b2fc39a5 69
2c631ee0 70my @stack;
71my %seqn2node;
72
7020702a 73use HTML::Entities qw(encode_entities);;
74my $dotnode = sub {
75 my $name = encode_entities(shift);
76 $name =~ s/"/\\"/g;
7020702a 77 return '"'.$name.'"';
78};
ee2793c1 79
ee2793c1 80
5aa3ad8e 81my $dot_fh;
ee2793c1 82if ($opt_dot) {
5aa3ad8e 83 open $dot_fh, ">$opt_dot";
84 print $dot_fh "digraph {\n"; # }
85 print $dot_fh "graph [overlap=false]\n"; # target="???", URL="???"
ee2793c1 86}
87
0741448c 88sub fmt_size {
89 my $size = shift;
90 my $kb = $size / 1024;
91 return $size if $kb < 5;
92 return sprintf "%.1fKb", $kb if $kb < 1000;
93 return sprintf "%.1fMb", $kb/1024;
94}
95
ee2793c1 96
94fab3d1 97sub enter_node {
98 my $x = shift;
de73b186 99
100 my $parent = $stack[-1];
101 if ($parent) {
102
68cafb30 103 if ($parent->{name} eq 'SV(PVAV)') {
104 Dwarn $x->{attr};
105 my $index = $x->{attr}{index};
106 # If node is an AVelem of a CvPADLIST propagate pad name to AVelem
107 if (@stack >= 4 and (my $cvpl = $stack[-4])->{name} eq 'CvPADLIST') {
108 # cache the pad names so we can eat them in order
109 my $padnames = $cvpl->{_cached}{padnames} ||= do {
110 my @names = @{ $cvpl->{attr}{+NPattr_PADNAME} || []};
111 $_ = "my(".($_||'').")" for @names;
112 $names[0] = '@_';
113 \@names;
114 };
115 #die Dwarn $x;
116 $x->{name} = $padnames->[$index] || "?";
117 $x->{name} =~ s/my\(SVs_PADTMP\)/PADTMP/; # XXX hack for neatness
118 }
119 else {
120 $x->{name} = "[$index]";
de73b186 121 }
122 }
ee2793c1 123 }
de73b186 124
125 return $x;
94fab3d1 126}
127
de73b186 128
94fab3d1 129sub leave_node {
130 my $x = shift;
b2fc39a5 131 delete $seqn2node{$x->{id}};
ee2793c1 132
94fab3d1 133 my $self_size = 0; $self_size += $_ for values %{$x->{leaves}};
134 $x->{self_size} = $self_size;
ee2793c1 135
136 my $parent = $stack[-1];
137 if ($parent) {
2c631ee0 138 # link to parent
5a78486c 139 $x->{parent_id} = $parent->{id};
2c631ee0 140 # accumulate into parent
141 $parent->{kids_node_count} += 1 + ($x->{kids_node_count}||0);
94fab3d1 142 $parent->{kids_size} += $self_size + $x->{kids_size};
5a78486c 143 push @{$parent->{child_id}}, $x->{id};
2c631ee0 144 }
de73b186 145
2c631ee0 146 # output
147 # ...
ee2793c1 148 if ($opt_dot) {
1915946b 149 printf "// n%d parent=%s(type=%s)\n", $x->{id},
0741448c 150 $parent ? $parent->{id} : "",
151 $parent ? $parent->{type} : ""
152 if 0;
de73b186 153 if ($x->{type} != NPtype_LINK) {
0741448c 154 my $name = $x->{title} ? "\"$x->{title}\" $x->{name}" : $x->{name};
155
156 if ($x->{kids_size}) {
157 $name .= sprintf " %s+%s=%s", fmt_size($x->{self_size}), fmt_size($x->{kids_size}), fmt_size($x->{self_size}+$x->{kids_size});
158 }
159 else {
160 $name .= sprintf " +%s", fmt_size($x->{self_size});
161 }
98128850 162 $name .= " $x->{id}" if $opt_showid;
0741448c 163
164 my @node_attr = (
165 sprintf("label=%s", $dotnode->($name)),
166 "id=$x->{id}",
167 );
168 my @link_attr;
169 #if ($x->{name} eq 'hek') { push @node_attr, "shape=point"; push @node_attr, "labelfontsize=6"; }
1915946b 170 if ($parent) { # probably a link
1915946b 171 my $parent_id = $parent->{id};
0741448c 172 my @link_attr = ("id=$parent_id");
de73b186 173 if ($parent->{type} == NPtype_LINK) { # link
1915946b 174 (my $link_name = $parent->{name}) =~ s/->$//;
175 push @link_attr, (sprintf "label=%s", $dotnode->($link_name));
176 $parent_id = ($stack[-2]||die "panic")->{id};
177 }
5aa3ad8e 178 printf $dot_fh qq{n%d -> n%d [%s];\n},
1915946b 179 $parent_id, $x->{id}, join(",", @link_attr);
180 }
5aa3ad8e 181 printf $dot_fh qq{n%d [ %s ];\n}, $x->{id}, join(",", @node_attr);
1915946b 182 }
183
ee2793c1 184 }
b2fc39a5 185 if ($dbh) {
f60f09e5 186 my $attr_json = $j->encode($x->{attr});
e78b28ca 187 my $leaves_json = $j->encode($x->{leaves});
b2fc39a5 188 $node_ins_sth->execute(
98128850 189 $x->{id}, $x->{name}, $x->{title}, $x->{type}, $x->{depth}, $x->{parent_id},
b2fc39a5 190 $x->{self_size}, $x->{kids_size}, $x->{kids_node_count},
f60f09e5 191 $x->{child_id} ? join(",", @{$x->{child_id}}) : undef,
e78b28ca 192 $attr_json, $leaves_json,
b2fc39a5 193 );
194 # XXX attribs
195 }
94fab3d1 196 return;
2c631ee0 197}
198
5aa3ad8e 199my $indent = ": ";
de73b186 200my @attr_type_name = (qw(size NAME PADFAKE my PADTMP NOTE));
68cafb30 201my $pending_pre_attr = {};
94fab3d1 202
2c631ee0 203while (<>) {
204 chomp;
de73b186 205
b2fc39a5 206 my ($type, $id, $val, $name, $extra) = split / /, $_, 5;
de73b186 207
ee2793c1 208 if ($type =~ s/^-//) { # Node type ($val is depth)
5aa3ad8e 209 printf "%s%s %s [#%d @%d]\n", $indent x $val, $name, $extra||'', $id, $val
210 if $opt_text;
2c631ee0 211 while ($val < @stack) {
94fab3d1 212 leave_node(my $x = pop @stack);
e8f4c506 213 warn "N $id d$val ends $x->{id} d$x->{depth}: size $x->{self_size}+$x->{kids_size}\n"
214 if $opt_verbose;
2c631ee0 215 }
c5078bcb 216 die "panic: stack already has item at depth $val"
217 if $stack[$val];
de73b186 218 my $node = enter_node({
219 id => $id, type => $type, name => $name, extra => $extra,
68cafb30 220 attr => { %$pending_pre_attr },
221 leaves => {}, depth => $val, self_size=>0, kids_size=>0
de73b186 222 });
68cafb30 223 %$pending_pre_attr = ();
de73b186 224 $stack[$val] = $node;
b2fc39a5 225 $seqn2node{$id} = $node;
2c631ee0 226 }
de73b186 227 # --- Leaf name and memory size
228 elsif ($type eq "L") {
b2fc39a5 229 my $node = $seqn2node{$id} || die;
2c631ee0 230 $node->{leaves}{$name} += $val;
5aa3ad8e 231 printf "%s+%d %s\n", $indent x ($node->{depth}+1), $val, $name
232 if $opt_text;
2c631ee0 233 }
de73b186 234 # --- Attribute type, name and value
235 elsif (looks_like_number($type)) {
b2fc39a5 236 my $node = $seqn2node{$id} || die;
e8f4c506 237 my $attr = $node->{attr} || die;
de73b186 238
68cafb30 239 # attributes to queue up and apply to the next node
240 if (NPattr_PRE_ATTR == $type) {
241 $pending_pre_attr->{$name} = $val;
242 }
243 # attributes where the string is a key (or always empty and the type is the key)
244 elsif ($type == NPattr_NAME or $type == NPattr_NOTE) {
de73b186 245 printf "%s~%s(%s) %d [t%d]\n", $indent x ($node->{depth}+1), $attr_type_name[$type], $name, $val, $type
246 if $opt_text;
e8f4c506 247 warn "Node $id already has attribute $type:$name (value $attr->{$type}{$name})\n"
248 if exists $attr->{$type}{$name};
249 $attr->{$type}{$name} = $val || $id;
e8f4c506 250 $node->{title} = $name if $type == 1 and !$val;
251 }
68cafb30 252 # attributes where the number is a key (or always zero)
de73b186 253 elsif (NPattr_PADFAKE==$type or NPattr_PADTMP==$type or NPattr_PADNAME==$type) {
254 printf "%s~%s('%s') %d [t%d]\n", $indent x ($node->{depth}+1), $attr_type_name[$type], $name, $val, $type
255 if $opt_text;
e8f4c506 256 warn "Node $id already has attribute $type:$name (value $attr->{$type}[$val])\n"
257 if defined $attr->{$type}[$val];
de73b186 258 $attr->{+NPattr_PADNAME}[$val] = $name; # store all as NPattr_PADNAME
e8f4c506 259 }
260 else {
de73b186 261 printf "%s~%s %d [t%d]\n", $indent x ($node->{depth}+1), $name, $val, $type
262 if $opt_text;
e8f4c506 263 warn "Invalid attribute type '$type' on line $. ($_)";
264 }
2c631ee0 265 }
266 else {
267 warn "Invalid type '$type' on line $. ($_)";
e8f4c506 268 next;
2c631ee0 269 }
b2fc39a5 270 $dbh->commit if $dbh and $id % 10_000 == 0;
2c631ee0 271}
272
c5078bcb 273my $top = $stack[0]; # grab top node before we pop all the nodes
274leave_node(pop @stack) while @stack;
275warn "EOF ends $top->{id} d$top->{depth}: size $top->{self_size}+$top->{kids_size}\n"
276 if $opt_verbose;
277warn Dumper($top) if $opt_verbose;
5aa3ad8e 278
279if ($dot_fh) {
280 print $dot_fh "}\n";
281 close $dot_fh;
282 system("open -a Graphviz $opt_dot");
2c631ee0 283}
94fab3d1 284
b2fc39a5 285$dbh->commit if $dbh;
286
2c631ee0 287use Data::Dumper;
5aa3ad8e 288warn Dumper(\%seqn2node) if %seqn2node; # should be empty
2c631ee0 289
290=for
291SV(PVAV) fill=1/1 [#1 @0]
292: +64 sv =64
293: +16 av_max =80
294: AVelem-> [#2 @1]
295: : SV(RV) [#3 @2]
296: : : +24 sv =104
297: : : RV-> [#4 @3]
298: : : : SV(PVAV) fill=-1/-1 [#5 @4]
299: : : : : +64 sv =168
300: AVelem-> [#6 @1]
301: : SV(IV) [#7 @2]
302: : : +24 sv =192
303192 at -e line 1.
304=cut
305__DATA__
306N 1 0 SV(PVAV) fill=1/1
307L 1 64 sv
308L 1 16 av_max
309N 2 1 AVelem->
310N 3 2 SV(RV)
311L 3 24 sv
312N 4 3 RV->
313N 5 4 SV(PVAV) fill=-1/-1
314L 5 64 sv
315N 6 1 AVelem->
316N 7 2 SV(IV)
317L 7 24 sv