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