forked from timbunce/devel-sizeme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sizeme_graph.pl
executable file
·298 lines (231 loc) · 9.18 KB
/
sizeme_graph.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env perl
BEGIN {
die qq{$0 requires Mojolicious::Lite, which isn't installed.
Currently requires Mojolicious::Lite which isn't available for perl 5.8.
If this affects you you can run Devel::SizeMe with your normal perl and
run sizeme_graph.pl with a different perl, perhaps on a different machine.
\n}
unless eval "require Mojolicious::Lite";
}
=head1 NAME
sizeme_graph.pl - web server providing an interactive treemap of Devel::SizeMe data
=head1 SYNOPSIS
sizeme_graph.pl --db sizeme.db daemon
sizeme_graph.pl daemon # same as above
Then open a web browser on http://127.0.0.1:3000
=head1 DESCRIPTION
Reads a database created by sizeme_store.pl and provides a web interface with
an interactive treemap of the data.
Currently requires Mojolicious::Lite which isn't available for perl 5.8.
If this affects you you can run Devel::SizeMe with your normal perl and
run sizeme_graph.pl with a different perl, perhaps on a different machine.
=head2 TODO
Current implementation is all very alpha and rather hackish.
Split out the db and tree code into a separate module.
Use a history management library so the back button works and we can have
links to specific nodes.
Better tool-tip and/or add a scrollable information area below the treemap
that could contain details and links.
Make the treemap resize to fit the browser window (as NYTProf does).
Protect against nodes with thousands of children
perhaps replace all with single merged child that has no children itself
but just a warning as a title.
Implement other visualizations, such as a space-tree
http://thejit.org/static/v20/Jit/Examples/Spacetree/example2.html
=cut
use strict;
use warnings;
use Mojolicious::Lite; # possibly needs v3
use JSON::XS;
use Getopt::Long;
use Storable qw(dclone);
use Devel::Dwarn;
use Devel::SizeMe::Graph;
require ORLite;
my $j = JSON::XS->new;
GetOptions(
'db=s' => \(my $opt_db = 'sizeme.db'),
'showid!' => \my $opt_showid,
'debug!' => \my $opt_debug,
) or exit 1;
die "Can't open $opt_db: $!\n" unless -r $opt_db;
warn "Reading $opt_db\n";
# XXX currently uses ORLite but doesn't actually make use of it in any useful way
ORLite->import({
file => $opt_db,
package => "SizeMe",
readonly => 1,
#user_version => 1, # XXX
#unicode => 1, # XXX
});
my $static_dir = $INC{'Devel/SizeMe/Graph.pm'} or die 'panic';
$static_dir =~ s:\.pm$:/static:;
die "panic $static_dir" unless -d $static_dir;
if ( $Mojolicious::VERSION >= 2.49 ) {
push @{ app->static->paths }, $static_dir;
} else {
app->static->root($static_dir);
}
# Documentation browser under "/perldoc"
plugin 'PODRenderer';
get '/' => sub {
my $self = shift;
$self->render('index');
};
# /jit_tree are AJAX requests from the treemap visualization
get '/jit_tree/:id/:depth' => sub {
my $self = shift;
my $id = $self->stash('id');
my $depth = $self->stash('depth');
# hack, would be best done on the client side
my $logarea = (defined $self->param('logarea'))
? $self->param('logarea')
: Mojo::URL->new($self->req->headers->referrer)->query->param('logarea');
my $node_tree = _fetch_node_tree($id, $depth);
my $jit_tree = _transform_node_tree($node_tree, sub {
my ($node) = @_;
my $children = delete $node->{children}; # XXX edits the src tree
my $area = $node->{self_size}+$node->{kids_size};
$node->{'$area'} = ($logarea) ? log($area) : $area; # XXX move to jit js
my $jit_node = {
id => $node->{id},
name => ($node->{title} || $node->{name}).($opt_showid ? " #$node->{id}" : ""),
data => $node,
};
$jit_node->{children} = $children if $children;
return $jit_node;
});
if (1){ # debug
#use Data::Dump qw(pp);
local $jit_tree->{children};
Dwarn(dclone($jit_tree)); # dclone to avoid stringification
}
$self->render_json($jit_tree);
};
sub _fetch_node_tree {
my ($id, $depth) = @_;
warn "#$id fetching\n"
if $opt_debug;
my $node = SizeMe->selectrow_hashref("select * from node where id = ?", undef, $id)
or die "Node '$id' not found"; # shouldn't die
$node->{$_} += 0 for (qw(child_count kids_node_count kids_size self_size));
$node->{leaves} = $j->decode(delete $node->{leaves_json});
$node->{attr} = $j->decode(delete $node->{attr_json});
$node->{name} .= "->" if $node->{type} == 2 && $node->{name};
if ($node->{child_ids}) {
my @child_ids = split /,/, $node->{child_ids};
my $children;
# if this node has only one child then we merge that child into this node
# this makes the treemap more usable
if (@child_ids == 1
# && $node->{type} == 2 # only collapse links XXX
) {
warn "#$id fetch only child $child_ids[0]\n"
if $opt_debug;
my $child = _fetch_node_tree($child_ids[0], $depth); # same depth
# merge node into child
# XXX id, depth, parent_id
warn "Merged $node->{name} (#$node->{id} d$node->{depth}) with only child $child->{name} #$child->{id}\n"
if $opt_debug;
$child->{name} = "$node->{name} $child->{name}";
$child->{$_} += $node->{$_} for (qw(self_size));
$child->{$_} = $node->{$_} for (qw(parent_id));
$child->{title} = join " ", grep { defined && length } $child->{title}, $node->{title};
#warn "Titled $child->{title}" if $child->{title};
# somewhat hackish attribute merging
for my $attr_type (keys %{ $node->{attr} }) {
my $src = $node->{attr}{$attr_type};
if (ref $src eq 'HASH') { # eg NPattr_NAME: {attr}{1}{$name} = $value
my $dst = $child->{attr}{$attr_type} ||= {};
for my $k (keys %$src) {
warn "Node $child->{id} attr $attr_type:$k=$dst->{$k} overwritten by $src->{$k}\n"
if defined $dst->{$k};
$dst->{$k} = $src->{$k};
}
}
elsif (ref $src eq 'ARRAY') { # eg NPattr_PADNAME: {attr}{2}[$val] = $name
my $dst = $child->{attr}{$attr_type} ||= [];
my $idx = @$src;
while (--$idx >= 0) {
warn "Node $child->{id} attr $attr_type:$idx=$dst->[$idx] overwritten by $src->[$idx]\n"
if defined $dst->[$idx];
$dst->[$idx] = $src->[$idx];
}
}
else { # assume scalar
warn "Node $child->{id} attr $attr_type=$child->{attr}{$attr_type} overwritten by $src\n"
if exists $child->{attr}{$attr_type};
$child->{attr}{$attr_type} = $src;
}
}
$child->{leaves}{$_} += $node->{leaves}{$_}
for keys %{ $node->{leaves} };
$child->{_ids_merged} .= ",$node->{id}";
my @child_ids = split /,/, $node->{child_ids};
$child->{child_count} = @child_ids;
$node = $child; # use the merged child as this node
}
if ($depth) { # recurse to required depth
$children = [ map { _fetch_node_tree($_, $depth-1) } @child_ids ];
$node->{children} = $children;
$node->{child_count} = @$children;
}
}
return $node;
}
sub _transform_node_tree { # recurse depth first
my ($node, $transform) = @_;
if (my $children = $node->{children}) {
$_ = _transform_node_tree($_, $transform) for @$children;
}
return $transform->($node);
}
app->start;
{ # just to reserve the namespace for future use
package Devel::SizeMe::Graph;
1;
}
__DATA__
@@ index.html.ep
% layout 'default';
% title 'Welcome';
Welcome to the Mojolicious real-time web framework!
@@ layouts/default.html.ep
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Perl Memory Treemap</title>
<!-- CSS Files -->
<link type="text/css" href="css/base.css" rel="stylesheet" />
<link type="text/css" href="css/Treemap.css" rel="stylesheet" />
<!--[if IE]><script language="javascript" type="text/javascript" src="excanvas.js"></script><![endif]-->
<!-- JIT Library File -->
<script language="javascript" type="text/javascript" src="jit-yc.js"></script>
<script language="javascript" type="text/javascript" src="jquery-1.8.1-min.js"></script>
<script language="javascript" type="text/javascript" src="sprintf.js"></script>
<script language="javascript" type="text/javascript" src="treemap.js"></script>
</head>
<body onload="init();">
<div id="container">
<div id="left-container">
<div class="text">
<h4> Perl Memory TreeMap </h4>
Click on a node to zoom in.<br /><br />
</div>
<a id="back" href="#" class="theme button white">Go to Parent</a>
<br />
<form name=params>
<label for="logarea"> Logarithmic scale
<input type=checkbox id="logarea" name="logarea">
</form>
</div>
<div id="center-container">
<div id="infovis"></div>
</div>
<div id="right-container">
<div id="inner-details"></div>
</div>
<div id="log"></div>
</div>
</body>
</html>