Debian Codegraph @

examples/dprofpp.graphviz

raw
1 #!/usr/bin/perl
2 #
3 # This is a replacement for Devel::DProf's dproffpp,
4 # which instead produces a graph from the tmon.out
5 # file.
6 #
7 # Example usage:
8 # perl -d:DProf test.pl
9 # dprofpp.graphviz tmon.out > tmon.png
10
11 use strict;
12 use lib '..';
13 use GraphViz;
14
15 1 until <> eq "PART2\n";
16
17 my %package;
18 my %subroutine;
19 my %name;
20 my %id;
21
22 my %calls;
23 my %call_tree;
24
25 my @stack = (-1);
26 $package{-1} = 'main';
27 $subroutine{-1} = 'main';
28 $name{-1} = 'main::main';
29
30 my $maxcalls;
31
32 while (defined(my $line = <>)) {
33 chomp $line;
34
35 if (my($id, $package, $subroutine) = $line =~ m/^& (.+?) (.+?) (.+?)$/) {
36 my $name = "$package::$subroutine";
37 $name{$id} = $package . '::' . $subroutine;
38 $package{$id} = $package;
39 $subroutine{$id} = $subroutine;
40 } elsif (my($id) = $line =~ m/^\+ (.+?)$/) {
41 if (ignore($id)) {
42 $calls{$id}++;
43 $call_tree{$stack[-1]}{$id}++;
44 $maxcalls = $call_tree{$stack[-1]}{$id} > $maxcalls ? $call_tree{$stack[-1]}{$id} : $maxcalls;
45 }
46 push @stack, $id;
47 } elsif (my($id) = $line =~ m/^\- (.+?)$/) {
48 die "Pop problem!" unless $id = pop @stack;
49 }
50 }
51
52 die "Stack not empty: (" . (join ', ', @stack) . ')!' if @stack > 1;
53
54 my %time;
55 my $maxtime;
56
57 my %id = reverse %name;
58
59 my $text = `dprofpp -q -O 50000 tmon.out`;
60 foreach my $line (split /\n/, $text) {
61 my(undef, $time, $excl, $cumul, $calls, $secspcall, $callspsec, $name) = split /\s+/, $line;
62 next unless ignore($id{$name});
63 $maxtime = $excl > $maxtime ? $excl : $maxtime;
64 $time{$id{$name}} = $excl;
65 # print "$name $excl\n";
66 }
67
68 my $g = GraphViz->new;
69
70 my %traversed;
71
72 traverse(-1);
73
74 sub traverse {
75 my $id = shift;
76 return if $traversed{$id}++;
77
78 my $count = $time{$id};
79 my $ratio = $count / $maxtime;
80 my $w = 100 * (1 - $ratio);
81
82 $g->add_node($name{$id}, label => $subroutine{$id}, cluster => $package{$id}, color => "0,1,$ratio", w => $w);
83
84 my @called = sort keys %{$call_tree{$id}};
85
86 foreach my $called_id (@called) {
87 traverse($called_id);
88 my $count = $call_tree{$id}{$called_id};
89 my $ratio = $count / $maxcalls;
90 my $w = 100 * (1 - $ratio);
91
92
93 $count = "" if $count == 1;
94 $g->add_edge($name{$id} => $name{$called_id}, label => $count, color => "0,1,$ratio", w => $w, len => 2);
95 }
96 }
97
98 #print $g->_as_debug;
99 print $g->as_png;
100
101 sub ignore {
102 my $id = shift;
103 return 0 if $subroutine{$id} eq 'BEGIN';
104 return 0 if $subroutine{$id} eq 'END';
105 return 0 if $subroutine{$id} eq '__ANON__';
106 # return 0 if $subroutine{$id} =~ /double|square|cons|id|flip|fst|snd|min|max/; # temporary for clarity
107 return 1;
108 }