| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# This script accumulates the execution paths of all calls to kmalloc |
| 4 |
# in the kernel. On Ctrl-C (or exit of the command provided by -c option), |
| 5 |
# it sorts, filters and displays them on |
| 6 |
# stdout. |
| 7 |
# |
| 8 |
# The -e (exclude) option can be used to specify a comma-separated list |
| 9 |
# - any stack with contents matching any of the items in the list will |
| 10 |
# be excluded from the output. |
| 11 |
# |
| 12 |
# The -m (min) option can be used to specify the minimum number of |
| 13 |
# occurrences a stack needs to be included in the output. |
| 14 |
# |
| 15 |
# The -t (top) option can be used to specify printing only the |
| 16 |
# top N stack traces. |
| 17 |
# |
| 18 |
# The -c (command) option runs starts the command and script |
| 19 |
# only runs for the duration of the command. |
| 20 |
# |
| 21 |
# The -o (options) option passes the options to systemap. |
| 22 |
# |
| 23 |
# Usage: ./kmalloc-top [-m min] [-e exclude list] [-t top_n] [-c command] |
| 24 |
# [-o options] |
| 25 |
# Ctrl-c |
| 26 |
|
| 27 |
use Getopt::Std; |
| 28 |
|
| 29 |
my $kmalloc_stacks; |
| 30 |
my $total_kmallocs; |
| 31 |
my $filtered_kmallocs; |
| 32 |
my $sorted_stacks; |
| 33 |
my $first_n = 1000000000; |
| 34 |
my $min_count = 1; |
| 35 |
my $exclude; |
| 36 |
my $options; |
| 37 |
|
| 38 |
$SIG{INT} = \&sigint_handler; |
| 39 |
|
| 40 |
getopts('c:e:m:t:o:'); |
| 41 |
|
| 42 |
if ($opt_e) { |
| 43 |
$exclude = join('|', split(/,/, $opt_e)); |
| 44 |
print "Will exclude stacks containing: $exclude\n"; |
| 45 |
} |
| 46 |
|
| 47 |
if ($opt_t) { |
| 48 |
$first_n = $opt_t; |
| 49 |
print "Will print only the top $first_n stacks.\n"; |
| 50 |
} |
| 51 |
|
| 52 |
if ($opt_m) { |
| 53 |
$min_count = $opt_m; |
| 54 |
} |
| 55 |
|
| 56 |
if ($opt_c) { |
| 57 |
$command="-c \"$opt_c\"" |
| 58 |
} |
| 59 |
|
| 60 |
if ($opt_o) { |
| 61 |
$options=$opt_o |
| 62 |
} |
| 63 |
|
| 64 |
print "Will print stacks with counts >= $min_count.\n"; |
| 65 |
print STDERR "Press Ctrl-C to stop.\n"; |
| 66 |
|
| 67 |
#The systemtap script that instruments the kmalloc |
| 68 |
$script=" |
| 69 |
global kmalloc_stack |
| 70 |
|
| 71 |
probe kernel.function(\"__kmalloc\") { kmalloc_stack[backtrace()]++ } |
| 72 |
|
| 73 |
probe timer.ms(100), end |
| 74 |
{ |
| 75 |
foreach (stack in kmalloc_stack) { |
| 76 |
printf(\"<hashkey>\\n\") |
| 77 |
print_syms(stack) |
| 78 |
printf(\"</hashkey>\\n\") |
| 79 |
printf(\"<hashval>%d</hashval>\\n\", kmalloc_stack[stack]) |
| 80 |
} |
| 81 |
delete kmalloc_stack |
| 82 |
} |
| 83 |
"; |
| 84 |
|
| 85 |
open STREAM, "stap $options -e '$script' $command|" or die "Couldn't get output stream $!"; |
| 86 |
|
| 87 |
while (<STREAM>) { |
| 88 |
if (/<hashval>(.*?)<\/hashval>/) { |
| 89 |
update_hash($key, $1); |
| 90 |
$key = ""; |
| 91 |
} elsif ($_ !~ (/<hashkey>|<\/hashkey>/)) { |
| 92 |
$key .= $_; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
$num_keys_before_filtering = scalar keys %kmalloc_stacks; |
| 97 |
$total_kmallocs = count_kmallocs(); |
| 98 |
filter_stacks(); |
| 99 |
sort_stacks(); |
| 100 |
top_stacks(); |
| 101 |
sort_stacks(); |
| 102 |
$num_keys_after_filtering = scalar keys %kmalloc_stacks; |
| 103 |
$filtered_kmallocs = count_kmallocs(); |
| 104 |
summarize(); |
| 105 |
exit(); |
| 106 |
|
| 107 |
sub update_hash |
| 108 |
{ |
| 109 |
my($key, $val) = @_; |
| 110 |
$kmalloc_stacks{$key} += $val; |
| 111 |
} |
| 112 |
|
| 113 |
sub filter_stacks |
| 114 |
{ |
| 115 |
while (($stack, $count) = each %kmalloc_stacks) { |
| 116 |
if ($count < $min_count) { |
| 117 |
delete $kmalloc_stacks{$stack}; |
| 118 |
} elsif ($exclude && $stack =~ /$exclude/) { |
| 119 |
delete $kmalloc_stacks{$stack}; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
sub top_stacks |
| 125 |
{ |
| 126 |
$count=0; |
| 127 |
foreach $stack(@sorted_stacks) { |
| 128 |
$count+=1; |
| 129 |
if ($count > $first_n) { |
| 130 |
delete $kmalloc_stacks{$stack}; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
sub sort_stacks |
| 136 |
{ |
| 137 |
@sorted_stacks = sort { $kmalloc_stacks{$b} <=> $kmalloc_stacks{$a} } keys %kmalloc_stacks; |
| 138 |
} |
| 139 |
|
| 140 |
sub count_kmallocs { |
| 141 |
$count = 0; |
| 142 |
foreach $stack(%kmalloc_stacks) { |
| 143 |
$count += $kmalloc_stacks{$stack}; |
| 144 |
} |
| 145 |
return $count; |
| 146 |
} |
| 147 |
|
| 148 |
sub summarize { |
| 149 |
print "\n"; |
| 150 |
foreach $stack(@sorted_stacks) { |
| 151 |
print "This path seen $kmalloc_stacks{$stack} times:\n$stack\n"; |
| 152 |
} |
| 153 |
|
| 154 |
if ($total_kmallocs > 0) { |
| 155 |
$percent = ($filtered_kmallocs)*100/$total_kmallocs; |
| 156 |
} else { |
| 157 |
$percent = 0; |
| 158 |
} |
| 159 |
print "Num stacks before filtering: $num_keys_before_filtering\n"; |
| 160 |
print "Num stacks after filtering: $num_keys_after_filtering\n"; |
| 161 |
print "Total kmallocs (before filtering): $total_kmallocs\n"; |
| 162 |
print "Total kmallocs (after filtering): $filtered_kmallocs\n"; |
| 163 |
print "The filter stacks have $percent of the total kmallocs\n"; |
| 164 |
|
| 165 |
close(STREAM); |
| 166 |
} |
| 167 |
|
| 168 |
sub sigint_handler |
| 169 |
{ |
| 170 |
system("pkill kmalloc-stacks"); |
| 171 |
} |