Merged in Governor-Tarkin/swg-src (pull request #17)
[swg-src.git] / tools / makeMultiplyReport.pl
blob2d050be4e42bba782e8c04cb9440b7355f0d611e
1 # Script to take output from the Transform::multiply tracking REPORT_LOG output
2 # and generate a report of callstacks sorted in descending frequency order.
4 use strict;
6 my $inCallstack = 0;
7 my $callstackFrequency = 0;
8 my $callstackLinesRef;
9 my $debug = 0;
11 my %callstacksByFrequency;
13 sub submitCallstack
15 die "bad callstackFrequency [$callstackFrequency]" if (!($callstackFrequency =~ m/^\d+$/));
16 return if (@$callstackLinesRef < 1);
18 # Make sure there's an array reference to hold all callstacks mapping to this frequency.
19 if (!exists $callstacksByFrequency{$callstackFrequency})
21 $callstacksByFrequency{$callstackFrequency} = [];
24 # Get array ref.
25 my $callstackArrayRef = $callstacksByFrequency{$callstackFrequency};
27 # Add callstack lines array to it.
28 push @$callstackArrayRef, $callstackLinesRef;
31 sub resetCallstack
33 $callstackLinesRef = [];
36 while (<>)
38 #-- Clean up line: remove line number and time info from log.
39 chomp();
40 s/\d+\s+\d+\.\d+\s+//;
42 #-- Process line.
44 # Check if this matches a start of callstack line.
45 if (m/Transform::multiply/)
47 if ($inCallstack)
49 # Add existing (now complete) callstack, restart a new one immediately following, starting on this line.
50 submitCallstack();
52 else
54 # Mark that we're now in a callstack so we know to scan following lines.
55 $inCallstack = 1;
57 resetCallstack();
59 # Parse out frequency.
60 if (m/called (\d+) times/)
62 $callstackFrequency = $1;
64 else
66 die "Failed to get call frequency on input line [$_]";
69 # Remove unique callstack numeric info at end since we sort differently (by frequency) than raw output.
70 s/\s\(\d+ of \d+ unique callstacks\)//;
72 # Add header line to callstack lines.
73 push @$callstackLinesRef, $_;
75 print "Found callstack frequency [$callstackFrequency]\n" if $debug;
77 elsif ($inCallstack)
79 # Fixiup lines I bumbled output on.
80 s/(\d+) caller \d+/caller $1/;
82 if (m/caller \d+/)
84 if (!m/unknown/)
86 # Looks like a good callstack line, keep it.
87 push @$callstackLinesRef, $_;
90 else
92 # Looks like this callstack is done.
93 submitCallstack();
94 $inCallstack = 0;
99 # Print out callstacks sorted by descending numeric frequency.
100 my @frequencies = sort { return $b <=> $a; } (keys %callstacksByFrequency);
102 print "There are ", @frequencies + 0, " unique callstack frequencies.\n";
103 foreach my $frequency (@frequencies)
105 my $callstackArrayRef = $callstacksByFrequency{$frequency};
106 my $callstackCount = @$callstackArrayRef;
108 print "========================================\n";
109 print "FREQUENCY: $frequency ($callstackCount callstacks)\n";
110 print "========================================\n";
111 print "\n";
112 for (my $i = 0; $i < $callstackCount; ++$i)
114 my $callstackLinesRef = $$callstackArrayRef[$i];
115 my $lineCount = @$callstackLinesRef;
117 print "$$callstackLinesRef[0]\n";
118 for (my $lineIndex = 1; $lineIndex < $lineCount; ++$lineIndex)
120 print "\t$$callstackLinesRef[$lineIndex]\n";
122 print "\n";
126 print "DONE.\n";