Change env var name
[p5sagit/Devel-Size.git] / memnodes.pl
old mode 100644 (file)
new mode 100755 (executable)
index fc1809b..a41ffec
-#!/bin/env perl
+#!/usr/bin/env perl
 
 use strict;
 use warnings;
+use autodie;
+
+use DBI qw(looks_like_number);
+use DBD::SQLite;
+use JSON::XS;
 
 use Getopt::Long;
 
 GetOptions(
-    'json!' => \my $opt_json,
+    'text!' => \my $opt_text,
+    'dot=s' => \my $opt_dot,
+    'db=s'  => \my $opt_db,
+    'verbose|v!' => \my $opt_verbose,
+    'debug|d!' => \my $opt_debug,
+    'showid!' => \my $opt_showid,
 ) or exit 1;
 
+my $j = JSON::XS->new->ascii->pretty(0);
+
+my ($dbh, $node_ins_sth);
+if ($opt_db) {
+    $dbh = DBI->connect("dbi:SQLite:dbname=$opt_db","","", {
+        RaiseError => 1, PrintError => 0, AutoCommit => 0
+    });
+    $dbh->do("PRAGMA synchronous = OFF");
+    $dbh->do("DROP TABLE IF EXISTS node");
+    $dbh->do(q{
+        CREATE TABLE node (
+            id integer primary key,
+            name text,
+            title text,
+            type integer,
+            depth integer,
+            parent_id integer,
+
+            self_size integer,
+            kids_size integer,
+            kids_node_count integer,
+            child_ids text,
+            attr_json text,
+            leaves_json text
+        )
+    });
+    $node_ins_sth = $dbh->prepare(q{
+        INSERT INTO node VALUES (?,?,?,?,?,?,  ?,?,?,?,?,?)
+    });
+}
+
 my @stack;
 my %seqn2node;
 
+use HTML::Entities qw(encode_entities);;
+my $dotnode = sub {
+    my $name = encode_entities(shift);
+    $name =~ s/"/\\"/g;
+    return '"'.$name.'"';
+};
+
+
+my $dot_fh;
+if ($opt_dot) {
+    open $dot_fh, ">$opt_dot";
+    print $dot_fh "digraph {\n"; # }
+    print $dot_fh "graph [overlap=false]\n"; # target="???", URL="???"
+}
+
+sub fmt_size {
+    my $size = shift;
+    my $kb = $size / 1024;
+    return $size if $kb < 5;
+    return sprintf "%.1fKb", $kb if $kb < 1000;
+    return sprintf "%.1fMb", $kb/1024;
+}
+
+
 sub enter_node {
     my $x = shift;
-    if ($opt_json) {
-        print "    " x $x->{depth};
-        print qq({ "id": "$x->{seqn}", "name": "$x->{name}", "depth":$x->{depth}, "children":[ \n);
+    if ($opt_dot) {
+        #printf $fh qq{\tn%d [ %s ]\n}, $x->{id}, $dotnode->($x->{name});
+        #print qq({ "id": "$x->{id}", "name": "$x->{name}", "depth":$x->{depth}, "children":[ \n);
     }
     return;
 }
 
 sub leave_node {
     my $x = shift;
-    delete $seqn2node{$x->{seqn}};
+    delete $seqn2node{$x->{id}};
+
     my $self_size = 0; $self_size += $_  for values %{$x->{leaves}};
     $x->{self_size} = $self_size;
-    if (my $parent = $stack[-1]) {
+
+    my $parent = $stack[-1];
+    if ($parent) {
         # link to parent
-        $x->{parent_seqn} = $parent->{seqn};
+        $x->{parent_id} = $parent->{id};
         # accumulate into parent
         $parent->{kids_node_count} += 1 + ($x->{kids_node_count}||0);
         $parent->{kids_size} += $self_size + $x->{kids_size};
-        push @{$parent->{child_seqn}}, $x->{seqn};
+        push @{$parent->{child_id}}, $x->{id};
     }
     # output
     # ...
-    if ($opt_json) {
-        print "    " x $x->{depth};
-        my $size = $self_size + $x->{kids_size};
-        print qq(], "data":{ "\$area": $size } },\n);
+    if ($opt_dot) {
+        printf "// n%d parent=%s(type=%s)\n", $x->{id},
+                $parent ? $parent->{id} : "",
+                $parent ? $parent->{type} : ""
+            if 0;
+        if ($x->{type} != 2) {
+            my $name = $x->{title} ? "\"$x->{title}\" $x->{name}" : $x->{name};
+
+            if ($x->{kids_size}) {
+                $name .= sprintf " %s+%s=%s", fmt_size($x->{self_size}), fmt_size($x->{kids_size}), fmt_size($x->{self_size}+$x->{kids_size});
+            }
+            else {
+                $name .= sprintf " +%s", fmt_size($x->{self_size});
+            }
+            $name .= " $x->{id}" if $opt_showid;
+
+            my @node_attr = (
+                sprintf("label=%s", $dotnode->($name)),
+                "id=$x->{id}",
+            );
+            my @link_attr;
+            #if ($x->{name} eq 'hek') { push @node_attr, "shape=point"; push @node_attr, "labelfontsize=6"; }
+            if ($parent) { # probably a link
+                my $parent_id = $parent->{id};
+                my @link_attr = ("id=$parent_id");
+                if ($parent->{type} == 2) { # link
+                    (my $link_name = $parent->{name}) =~ s/->$//;
+                    push @link_attr, (sprintf "label=%s", $dotnode->($link_name));
+                    $parent_id = ($stack[-2]||die "panic")->{id};
+                }
+                printf $dot_fh qq{n%d -> n%d [%s];\n},
+                    $parent_id, $x->{id}, join(",", @link_attr);
+            }
+            printf $dot_fh qq{n%d [ %s ];\n}, $x->{id}, join(",", @node_attr);
+        }
+
+    }
+    if ($dbh) {
+        my $attr_json = $j->encode($x->{attr});
+        my $leaves_json = $j->encode($x->{leaves});
+        $node_ins_sth->execute(
+            $x->{id}, $x->{name}, $x->{title}, $x->{type}, $x->{depth}, $x->{parent_id},
+            $x->{self_size}, $x->{kids_size}, $x->{kids_node_count},
+            $x->{child_id} ? join(",", @{$x->{child_id}}) : undef,
+            $attr_json, $leaves_json,
+        );
+        # XXX attribs
     }
     return;
 }
 
-print "memnodes = [" if $opt_json;
+my $indent = ":   ";
 
 while (<>) {
     chomp;
-    my ($type, $seqn, $val, $name, $extra) = split / /, $_, 5;
-    if ($type eq "N") {     # Node ($val is depth)
+    my ($type, $id, $val, $name, $extra) = split / /, $_, 5;
+    if ($type =~ s/^-//) {     # Node type ($val is depth)
+        printf "%s%s %s [#%d @%d]\n", $indent x $val, $name, $extra||'', $id, $val
+            if $opt_text;
         while ($val < @stack) {
             leave_node(my $x = pop @stack);
-            warn "N $seqn d$val ends $x->{seqn} d$x->{depth}: size $x->{self_size}+$x->{kids_size}\n";
+            warn "N $id d$val ends $x->{id} d$x->{depth}: size $x->{self_size}+$x->{kids_size}\n"
+                if $opt_verbose;
         }
-        die 1 if $stack[$val];
-        my $node = $stack[$val] = { seqn => $seqn, name => $name, extra => $extra, attr => [], leaves => {}, depth => $val, self_size=>0, kids_size=>0 };
+        die "panic: stack already has item at depth $val"
+            if $stack[$val];
+        my $node = $stack[$val] = { id => $id, type => $type, name => $name, extra => $extra, attr => {}, leaves => {}, depth => $val, self_size=>0, kids_size=>0 };
         enter_node($node);
-        $seqn2node{$seqn} = $node;
+        $seqn2node{$id} = $node;
     }
     elsif ($type eq "L") {  # Leaf name and memory size
-        my $node = $seqn2node{$seqn} || die;
+        my $node = $seqn2node{$id} || die;
         $node->{leaves}{$name} += $val;
+        printf "%s+%d %s\n", $indent x ($node->{depth}+1), $val, $name
+            if $opt_text;
     }
-    elsif ($type eq "A") {  # Attribute name and value
-        my $node = $seqn2node{$seqn} || die;
-        push @{ $node->{attr} }, $name, $val; # pairs
+    elsif (looks_like_number($type)) {  # Attribute type, name and value
+        my $node = $seqn2node{$id} || die;
+        my $attr = $node->{attr} || die;
+        printf "%s~%s %d [t%d]\n", $indent x ($node->{depth}+1), $name, $val, $type
+            if $opt_text;
+        if ($type == 1 or $type == 5) { # NPattr_NAME
+            warn "Node $id already has attribute $type:$name (value $attr->{$type}{$name})\n"
+                if exists $attr->{$type}{$name};
+            $attr->{$type}{$name} = $val || $id;
+            $node->{title} = $name if $type == 1 and !$val;
+        }
+        elsif (2 <= $type and $type <= 4) { # NPattr_PAD*
+            warn "Node $id already has attribute $type:$name (value $attr->{$type}[$val])\n"
+                if defined $attr->{$type}[$val];
+            $attr->{$type}[$val] = $name;
+        }
+        else {
+            warn "Invalid attribute type '$type' on line $. ($_)";
+        }
     }
     else {
         warn "Invalid type '$type' on line $. ($_)";
+        next;
     }
+    $dbh->commit if $dbh and $id % 10_000 == 0;
 }
 
-my $x;
-while (@stack > 1) {
-    leave_node($x = pop @stack) while @stack;
-    warn "EOF ends $x->{seqn} d$x->{depth}: size $x->{self_size}+$x->{kids_size}\n";
+my $top = $stack[0]; # grab top node before we pop all the nodes
+leave_node(pop @stack) while @stack;
+warn "EOF ends $top->{id} d$top->{depth}: size $top->{self_size}+$top->{kids_size}\n"
+    if $opt_verbose;
+warn Dumper($top) if $opt_verbose;
+
+if ($dot_fh) {
+    print $dot_fh "}\n";
+    close $dot_fh;
+    system("open -a Graphviz $opt_dot");
 }
-print " ];\n" if $opt_json;
+
+$dbh->commit if $dbh;
 
 use Data::Dumper;
-warn Dumper(\$x);
-warn Dumper(\%seqn2node);
+warn Dumper(\%seqn2node) if %seqn2node; # should be empty
 
 =for
 SV(PVAV) fill=1/1       [#1 @0]