Tweak node naming (remove + )
[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;
b2fc39a5 10
fc6614ee 11use Getopt::Long;
12
13GetOptions(
5aa3ad8e 14 'text!' => \my $opt_text,
15 'dot=s' => \my $opt_dot,
b2fc39a5 16 'db=s' => \my $opt_db,
e8f4c506 17 'verbose|v!' => \my $opt_verbose,
18 'debug|d!' => \my $opt_debug,
98128850 19 'showid!' => \my $opt_showid,
fc6614ee 20) or exit 1;
94fab3d1 21
f60f09e5 22my $j = JSON::XS->new->ascii->pretty(0);
23
1915946b 24my ($dbh, $node_ins_sth);
25if ($opt_db) {
26 $dbh = DBI->connect("dbi:SQLite:dbname=$opt_db","","", {
27 RaiseError => 1, PrintError => 0, AutoCommit => 0
28 });
29 $dbh->do("PRAGMA synchronous = OFF");
30 $dbh->do("DROP TABLE IF EXISTS node");
31 $dbh->do(q{
32 CREATE TABLE node (
33 id integer primary key,
34 name text,
35 title text,
98128850 36 type integer,
1915946b 37 depth integer,
38 parent_id integer,
39
40 self_size integer,
41 kids_size integer,
42 kids_node_count integer,
43 child_ids text,
44 attr_json text,
45 leaves_json text
46 )
47 });
48 $node_ins_sth = $dbh->prepare(q{
98128850 49 INSERT INTO node VALUES (?,?,?,?,?,?, ?,?,?,?,?,?)
1915946b 50 });
51}
b2fc39a5 52
2c631ee0 53my @stack;
54my %seqn2node;
55
7020702a 56use HTML::Entities qw(encode_entities);;
57my $dotnode = sub {
58 my $name = encode_entities(shift);
59 $name =~ s/"/\\"/g;
7020702a 60 return '"'.$name.'"';
61};
ee2793c1 62
ee2793c1 63
5aa3ad8e 64my $dot_fh;
ee2793c1 65if ($opt_dot) {
5aa3ad8e 66 open $dot_fh, ">$opt_dot";
67 print $dot_fh "digraph {\n"; # }
68 print $dot_fh "graph [overlap=false]\n"; # target="???", URL="???"
ee2793c1 69}
70
0741448c 71sub fmt_size {
72 my $size = shift;
73 my $kb = $size / 1024;
74 return $size if $kb < 5;
75 return sprintf "%.1fKb", $kb if $kb < 1000;
76 return sprintf "%.1fMb", $kb/1024;
77}
78
ee2793c1 79
94fab3d1 80sub enter_node {
81 my $x = shift;
ee2793c1 82 if ($opt_dot) {
83 #printf $fh qq{\tn%d [ %s ]\n}, $x->{id}, $dotnode->($x->{name});
84 #print qq({ "id": "$x->{id}", "name": "$x->{name}", "depth":$x->{depth}, "children":[ \n);
85 }
94fab3d1 86 return;
87}
88
89sub leave_node {
90 my $x = shift;
b2fc39a5 91 delete $seqn2node{$x->{id}};
ee2793c1 92
94fab3d1 93 my $self_size = 0; $self_size += $_ for values %{$x->{leaves}};
94 $x->{self_size} = $self_size;
ee2793c1 95
96 my $parent = $stack[-1];
97 if ($parent) {
2c631ee0 98 # link to parent
5a78486c 99 $x->{parent_id} = $parent->{id};
2c631ee0 100 # accumulate into parent
101 $parent->{kids_node_count} += 1 + ($x->{kids_node_count}||0);
94fab3d1 102 $parent->{kids_size} += $self_size + $x->{kids_size};
5a78486c 103 push @{$parent->{child_id}}, $x->{id};
2c631ee0 104 }
105 # output
106 # ...
ee2793c1 107 if ($opt_dot) {
1915946b 108 printf "// n%d parent=%s(type=%s)\n", $x->{id},
0741448c 109 $parent ? $parent->{id} : "",
110 $parent ? $parent->{type} : ""
111 if 0;
1915946b 112 if ($x->{type} != 2) {
0741448c 113 my $name = $x->{title} ? "\"$x->{title}\" $x->{name}" : $x->{name};
114
115 if ($x->{kids_size}) {
116 $name .= sprintf " %s+%s=%s", fmt_size($x->{self_size}), fmt_size($x->{kids_size}), fmt_size($x->{self_size}+$x->{kids_size});
117 }
118 else {
119 $name .= sprintf " +%s", fmt_size($x->{self_size});
120 }
98128850 121 $name .= " $x->{id}" if $opt_showid;
0741448c 122
123 my @node_attr = (
124 sprintf("label=%s", $dotnode->($name)),
125 "id=$x->{id}",
126 );
127 my @link_attr;
128 #if ($x->{name} eq 'hek') { push @node_attr, "shape=point"; push @node_attr, "labelfontsize=6"; }
1915946b 129 if ($parent) { # probably a link
1915946b 130 my $parent_id = $parent->{id};
0741448c 131 my @link_attr = ("id=$parent_id");
1915946b 132 if ($parent->{type} == 2) { # link
133 (my $link_name = $parent->{name}) =~ s/->$//;
134 push @link_attr, (sprintf "label=%s", $dotnode->($link_name));
135 $parent_id = ($stack[-2]||die "panic")->{id};
136 }
5aa3ad8e 137 printf $dot_fh qq{n%d -> n%d [%s];\n},
1915946b 138 $parent_id, $x->{id}, join(",", @link_attr);
139 }
5aa3ad8e 140 printf $dot_fh qq{n%d [ %s ];\n}, $x->{id}, join(",", @node_attr);
1915946b 141 }
142
ee2793c1 143 }
b2fc39a5 144 if ($dbh) {
f60f09e5 145 my $attr_json = $j->encode($x->{attr});
e78b28ca 146 my $leaves_json = $j->encode($x->{leaves});
b2fc39a5 147 $node_ins_sth->execute(
98128850 148 $x->{id}, $x->{name}, $x->{title}, $x->{type}, $x->{depth}, $x->{parent_id},
b2fc39a5 149 $x->{self_size}, $x->{kids_size}, $x->{kids_node_count},
f60f09e5 150 $x->{child_id} ? join(",", @{$x->{child_id}}) : undef,
e78b28ca 151 $attr_json, $leaves_json,
b2fc39a5 152 );
153 # XXX attribs
154 }
94fab3d1 155 return;
2c631ee0 156}
157
5aa3ad8e 158my $indent = ": ";
94fab3d1 159
2c631ee0 160while (<>) {
161 chomp;
b2fc39a5 162 my ($type, $id, $val, $name, $extra) = split / /, $_, 5;
ee2793c1 163 if ($type =~ s/^-//) { # Node type ($val is depth)
5aa3ad8e 164 printf "%s%s %s [#%d @%d]\n", $indent x $val, $name, $extra||'', $id, $val
165 if $opt_text;
2c631ee0 166 while ($val < @stack) {
94fab3d1 167 leave_node(my $x = pop @stack);
e8f4c506 168 warn "N $id d$val ends $x->{id} d$x->{depth}: size $x->{self_size}+$x->{kids_size}\n"
169 if $opt_verbose;
2c631ee0 170 }
c5078bcb 171 die "panic: stack already has item at depth $val"
172 if $stack[$val];
ee2793c1 173 my $node = $stack[$val] = { id => $id, type => $type, name => $name, extra => $extra, attr => {}, leaves => {}, depth => $val, self_size=>0, kids_size=>0 };
94fab3d1 174 enter_node($node);
b2fc39a5 175 $seqn2node{$id} = $node;
2c631ee0 176 }
177 elsif ($type eq "L") { # Leaf name and memory size
b2fc39a5 178 my $node = $seqn2node{$id} || die;
2c631ee0 179 $node->{leaves}{$name} += $val;
5aa3ad8e 180 printf "%s+%d %s\n", $indent x ($node->{depth}+1), $val, $name
181 if $opt_text;
2c631ee0 182 }
e8f4c506 183 elsif (looks_like_number($type)) { # Attribute type, name and value
b2fc39a5 184 my $node = $seqn2node{$id} || die;
e8f4c506 185 my $attr = $node->{attr} || die;
5aa3ad8e 186 printf "%s~%s %d [t%d]\n", $indent x ($node->{depth}+1), $name, $val, $type
187 if $opt_text;
188 if ($type == 1 or $type == 5) { # NPattr_NAME
e8f4c506 189 warn "Node $id already has attribute $type:$name (value $attr->{$type}{$name})\n"
190 if exists $attr->{$type}{$name};
191 $attr->{$type}{$name} = $val || $id;
e8f4c506 192 $node->{title} = $name if $type == 1 and !$val;
193 }
194 elsif (2 <= $type and $type <= 4) { # NPattr_PAD*
195 warn "Node $id already has attribute $type:$name (value $attr->{$type}[$val])\n"
196 if defined $attr->{$type}[$val];
197 $attr->{$type}[$val] = $name;
198 }
199 else {
200 warn "Invalid attribute type '$type' on line $. ($_)";
201 }
2c631ee0 202 }
203 else {
204 warn "Invalid type '$type' on line $. ($_)";
e8f4c506 205 next;
2c631ee0 206 }
b2fc39a5 207 $dbh->commit if $dbh and $id % 10_000 == 0;
2c631ee0 208}
209
c5078bcb 210my $top = $stack[0]; # grab top node before we pop all the nodes
211leave_node(pop @stack) while @stack;
212warn "EOF ends $top->{id} d$top->{depth}: size $top->{self_size}+$top->{kids_size}\n"
213 if $opt_verbose;
214warn Dumper($top) if $opt_verbose;
5aa3ad8e 215
216if ($dot_fh) {
217 print $dot_fh "}\n";
218 close $dot_fh;
219 system("open -a Graphviz $opt_dot");
2c631ee0 220}
94fab3d1 221
b2fc39a5 222$dbh->commit if $dbh;
223
2c631ee0 224use Data::Dumper;
5aa3ad8e 225warn Dumper(\%seqn2node) if %seqn2node; # should be empty
2c631ee0 226
227=for
228SV(PVAV) fill=1/1 [#1 @0]
229: +64 sv =64
230: +16 av_max =80
231: AVelem-> [#2 @1]
232: : SV(RV) [#3 @2]
233: : : +24 sv =104
234: : : RV-> [#4 @3]
235: : : : SV(PVAV) fill=-1/-1 [#5 @4]
236: : : : : +64 sv =168
237: AVelem-> [#6 @1]
238: : SV(IV) [#7 @2]
239: : : +24 sv =192
240192 at -e line 1.
241=cut
242__DATA__
243N 1 0 SV(PVAV) fill=1/1
244L 1 64 sv
245L 1 16 av_max
246N 2 1 AVelem->
247N 3 2 SV(RV)
248L 3 24 sv
249N 4 3 RV->
250N 5 4 SV(PVAV) fill=-1/-1
251L 5 64 sv
252N 6 1 AVelem->
253N 7 2 SV(IV)
254L 7 24 sv