Add "->" to link names for --text.
[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
37836f2a 103 if ($x->{name} eq 'AVelem' and $parent->{name} eq 'SV(PVAV)') {
68cafb30 104 my $index = $x->{attr}{index};
105 # If node is an AVelem of a CvPADLIST propagate pad name to AVelem
106 if (@stack >= 4 and (my $cvpl = $stack[-4])->{name} eq 'CvPADLIST') {
107 # cache the pad names so we can eat them in order
108 my $padnames = $cvpl->{_cached}{padnames} ||= do {
109 my @names = @{ $cvpl->{attr}{+NPattr_PADNAME} || []};
110 $_ = "my(".($_||'').")" for @names;
111 $names[0] = '@_';
112 \@names;
113 };
114 #die Dwarn $x;
115 $x->{name} = $padnames->[$index] || "?";
116 $x->{name} =~ s/my\(SVs_PADTMP\)/PADTMP/; # XXX hack for neatness
117 }
118 else {
119 $x->{name} = "[$index]";
de73b186 120 }
121 }
ee2793c1 122 }
de73b186 123
124 return $x;
94fab3d1 125}
126
de73b186 127
94fab3d1 128sub leave_node {
129 my $x = shift;
b2fc39a5 130 delete $seqn2node{$x->{id}};
ee2793c1 131
94fab3d1 132 my $self_size = 0; $self_size += $_ for values %{$x->{leaves}};
133 $x->{self_size} = $self_size;
ee2793c1 134
135 my $parent = $stack[-1];
136 if ($parent) {
2c631ee0 137 # link to parent
5a78486c 138 $x->{parent_id} = $parent->{id};
2c631ee0 139 # accumulate into parent
140 $parent->{kids_node_count} += 1 + ($x->{kids_node_count}||0);
94fab3d1 141 $parent->{kids_size} += $self_size + $x->{kids_size};
5a78486c 142 push @{$parent->{child_id}}, $x->{id};
2c631ee0 143 }
de73b186 144
2c631ee0 145 # output
146 # ...
ee2793c1 147 if ($opt_dot) {
1915946b 148 printf "// n%d parent=%s(type=%s)\n", $x->{id},
0741448c 149 $parent ? $parent->{id} : "",
150 $parent ? $parent->{type} : ""
151 if 0;
de73b186 152 if ($x->{type} != NPtype_LINK) {
0741448c 153 my $name = $x->{title} ? "\"$x->{title}\" $x->{name}" : $x->{name};
154
155 if ($x->{kids_size}) {
156 $name .= sprintf " %s+%s=%s", fmt_size($x->{self_size}), fmt_size($x->{kids_size}), fmt_size($x->{self_size}+$x->{kids_size});
157 }
158 else {
159 $name .= sprintf " +%s", fmt_size($x->{self_size});
160 }
98128850 161 $name .= " $x->{id}" if $opt_showid;
0741448c 162
163 my @node_attr = (
164 sprintf("label=%s", $dotnode->($name)),
165 "id=$x->{id}",
166 );
167 my @link_attr;
168 #if ($x->{name} eq 'hek') { push @node_attr, "shape=point"; push @node_attr, "labelfontsize=6"; }
1915946b 169 if ($parent) { # probably a link
1915946b 170 my $parent_id = $parent->{id};
0741448c 171 my @link_attr = ("id=$parent_id");
de73b186 172 if ($parent->{type} == NPtype_LINK) { # link
1915946b 173 (my $link_name = $parent->{name}) =~ s/->$//;
174 push @link_attr, (sprintf "label=%s", $dotnode->($link_name));
175 $parent_id = ($stack[-2]||die "panic")->{id};
176 }
5aa3ad8e 177 printf $dot_fh qq{n%d -> n%d [%s];\n},
1915946b 178 $parent_id, $x->{id}, join(",", @link_attr);
179 }
5aa3ad8e 180 printf $dot_fh qq{n%d [ %s ];\n}, $x->{id}, join(",", @node_attr);
1915946b 181 }
182
ee2793c1 183 }
b2fc39a5 184 if ($dbh) {
f60f09e5 185 my $attr_json = $j->encode($x->{attr});
e78b28ca 186 my $leaves_json = $j->encode($x->{leaves});
b2fc39a5 187 $node_ins_sth->execute(
98128850 188 $x->{id}, $x->{name}, $x->{title}, $x->{type}, $x->{depth}, $x->{parent_id},
b2fc39a5 189 $x->{self_size}, $x->{kids_size}, $x->{kids_node_count},
f60f09e5 190 $x->{child_id} ? join(",", @{$x->{child_id}}) : undef,
e78b28ca 191 $attr_json, $leaves_json,
b2fc39a5 192 );
193 # XXX attribs
194 }
94fab3d1 195 return;
2c631ee0 196}
197
5aa3ad8e 198my $indent = ": ";
de73b186 199my @attr_type_name = (qw(size NAME PADFAKE my PADTMP NOTE));
68cafb30 200my $pending_pre_attr = {};
94fab3d1 201
2c631ee0 202while (<>) {
203 chomp;
de73b186 204
b2fc39a5 205 my ($type, $id, $val, $name, $extra) = split / /, $_, 5;
de73b186 206
ee2793c1 207 if ($type =~ s/^-//) { # Node type ($val is depth)
65b2cf7d 208 printf "%s%s%s %s [#%d @%d]\n", $indent x $val, $name,
209 ($type == NPtype_LINK) ? "->" : "",
210 $extra||'', $id, $val
5aa3ad8e 211 if $opt_text;
2c631ee0 212 while ($val < @stack) {
94fab3d1 213 leave_node(my $x = pop @stack);
e8f4c506 214 warn "N $id d$val ends $x->{id} d$x->{depth}: size $x->{self_size}+$x->{kids_size}\n"
215 if $opt_verbose;
2c631ee0 216 }
c5078bcb 217 die "panic: stack already has item at depth $val"
218 if $stack[$val];
de73b186 219 my $node = enter_node({
220 id => $id, type => $type, name => $name, extra => $extra,
68cafb30 221 attr => { %$pending_pre_attr },
222 leaves => {}, depth => $val, self_size=>0, kids_size=>0
de73b186 223 });
68cafb30 224 %$pending_pre_attr = ();
de73b186 225 $stack[$val] = $node;
b2fc39a5 226 $seqn2node{$id} = $node;
2c631ee0 227 }
de73b186 228 # --- Leaf name and memory size
229 elsif ($type eq "L") {
b2fc39a5 230 my $node = $seqn2node{$id} || die;
2c631ee0 231 $node->{leaves}{$name} += $val;
5aa3ad8e 232 printf "%s+%d %s\n", $indent x ($node->{depth}+1), $val, $name
233 if $opt_text;
2c631ee0 234 }
de73b186 235 # --- Attribute type, name and value
236 elsif (looks_like_number($type)) {
b2fc39a5 237 my $node = $seqn2node{$id} || die;
e8f4c506 238 my $attr = $node->{attr} || die;
de73b186 239
68cafb30 240 # attributes to queue up and apply to the next node
241 if (NPattr_PRE_ATTR == $type) {
242 $pending_pre_attr->{$name} = $val;
243 }
244 # attributes where the string is a key (or always empty and the type is the key)
245 elsif ($type == NPattr_NAME or $type == NPattr_NOTE) {
de73b186 246 printf "%s~%s(%s) %d [t%d]\n", $indent x ($node->{depth}+1), $attr_type_name[$type], $name, $val, $type
247 if $opt_text;
e8f4c506 248 warn "Node $id already has attribute $type:$name (value $attr->{$type}{$name})\n"
249 if exists $attr->{$type}{$name};
250 $attr->{$type}{$name} = $val || $id;
e8f4c506 251 $node->{title} = $name if $type == 1 and !$val;
252 }
68cafb30 253 # attributes where the number is a key (or always zero)
de73b186 254 elsif (NPattr_PADFAKE==$type or NPattr_PADTMP==$type or NPattr_PADNAME==$type) {
255 printf "%s~%s('%s') %d [t%d]\n", $indent x ($node->{depth}+1), $attr_type_name[$type], $name, $val, $type
256 if $opt_text;
e8f4c506 257 warn "Node $id already has attribute $type:$name (value $attr->{$type}[$val])\n"
258 if defined $attr->{$type}[$val];
de73b186 259 $attr->{+NPattr_PADNAME}[$val] = $name; # store all as NPattr_PADNAME
e8f4c506 260 }
261 else {
de73b186 262 printf "%s~%s %d [t%d]\n", $indent x ($node->{depth}+1), $name, $val, $type
263 if $opt_text;
e8f4c506 264 warn "Invalid attribute type '$type' on line $. ($_)";
265 }
2c631ee0 266 }
267 else {
268 warn "Invalid type '$type' on line $. ($_)";
e8f4c506 269 next;
2c631ee0 270 }
b2fc39a5 271 $dbh->commit if $dbh and $id % 10_000 == 0;
2c631ee0 272}
273
c5078bcb 274my $top = $stack[0]; # grab top node before we pop all the nodes
275leave_node(pop @stack) while @stack;
276warn "EOF ends $top->{id} d$top->{depth}: size $top->{self_size}+$top->{kids_size}\n"
277 if $opt_verbose;
278warn Dumper($top) if $opt_verbose;
5aa3ad8e 279
280if ($dot_fh) {
281 print $dot_fh "}\n";
282 close $dot_fh;
283 system("open -a Graphviz $opt_dot");
2c631ee0 284}
94fab3d1 285
b2fc39a5 286$dbh->commit if $dbh;
287
2c631ee0 288use Data::Dumper;
5aa3ad8e 289warn Dumper(\%seqn2node) if %seqn2node; # should be empty
2c631ee0 290
291=for
292SV(PVAV) fill=1/1 [#1 @0]
293: +64 sv =64
294: +16 av_max =80
295: AVelem-> [#2 @1]
296: : SV(RV) [#3 @2]
297: : : +24 sv =104
298: : : RV-> [#4 @3]
299: : : : SV(PVAV) fill=-1/-1 [#5 @4]
300: : : : : +64 sv =168
301: AVelem-> [#6 @1]
302: : SV(IV) [#7 @2]
303: : : +24 sv =192
304192 at -e line 1.
305=cut
306__DATA__
307N 1 0 SV(PVAV) fill=1/1
308L 1 64 sv
309L 1 16 av_max
310N 2 1 AVelem->
311N 3 2 SV(RV)
312L 3 24 sv
313N 4 3 RV->
314N 5 4 SV(PVAV) fill=-1/-1
315L 5 64 sv
316N 6 1 AVelem->
317N 7 2 SV(IV)
318L 7 24 sv