Whamcloud - gitweb
branch: HEAD
[fs/lustre-release.git] / lustre / tests / leak_finder.pl
1 #!/usr/bin/perl -w
2
3 use IO::Handle;
4
5 STDOUT->autoflush(1);
6 STDERR->autoflush(1);
7
8 my ($line, $memory);
9 my $debug_line = 0;
10
11 my $total = 0;
12 my $max = 0;
13
14 while ($line = <>) {
15     $debug_line++;
16     my ($file, $func, $lno, $name, $size, $addr, $type);
17     if ($line =~ m/^.*(\.).*\((.*):(\d+):(.*)\(\)\) (k|v|slab-)(.*) '(.*)': (\d+) at (.*)\..*$/){
18         $file = $2;
19         $lno  = $3;
20         $func = $4;
21         $type = $6;
22         $name = $7;
23         $size = $8;
24         $addr = $9;
25
26         # we can't dump the log after portals has exited, so skip "leaks"
27         # from memory freed in the portals module unloading.
28         if ($func eq 'portals_handle_init') {
29             next;
30         }
31         printf("%8s %6d bytes at %s called %s (%s:%s:%d)\n", $type, $size,
32                $addr, $name, $file, $func, $lno);
33     } else {
34         next;
35     }
36
37     if (index($type, 'alloced') >= 0) {
38         if (defined($memory->{$addr})) {
39             print STDERR "*** Two allocs with the same address ($size bytes at $addr, $file:$func:$lno)\n";
40             print STDERR "    first malloc at $memory->{$addr}->{file}:$memory->{$addr}->{func}:$memory->{$addr}->{lno}, second at $file:$func:$lno\n";
41             next;
42         }
43
44         $memory->{$addr}->{name} = $name;
45         $memory->{$addr}->{size} = $size;
46         $memory->{$addr}->{file} = $file;
47         $memory->{$addr}->{func} = $func;
48         $memory->{$addr}->{lno} = $lno;
49         $memory->{$addr}->{debug_line} = $debug_line;
50
51         $total += $size;
52         if ($total > $max) {
53             $max = $total;
54         }
55     } else {
56         if (!defined($memory->{$addr})) {
57             print STDERR "*** Free without malloc ($size bytes at $addr, $file:$func:$lno)\n";
58             next;
59         }
60         my ($oldname, $oldsize, $oldfile, $oldfunc, $oldlno) = $memory->{$addr};
61
62         if ($memory->{$addr}->{size} != $size) {
63             print STDERR "*** Free different size ($memory->{$addr}->{size} alloced, $size freed).\n";
64             print STDERR "    malloc at $memory->{$addr}->{file}:$memory->{$addr}->{func}:$memory->{$addr}->{lno}, free at $file:$func:$lno\n";
65             next;
66         }
67
68         delete $memory->{$addr};
69         $total -= $size;
70     }
71 }
72
73 # Sort leak output by allocation time
74 my @sorted = sort {
75     return $memory->{$a}->{debug_line} <=> $memory->{$b}->{debug_line};
76 } keys(%{$memory});
77
78 my $key;
79 foreach $key (@sorted) {
80     my ($oldname, $oldsize, $oldfile, $oldfunc, $oldlno) = $memory->{$key};
81     print STDERR "*** Leak: $memory->{$key}->{size} bytes allocated at $key ($memory->{$key}->{file}:$memory->{$key}->{func}:$memory->{$key}->{lno}, debug file line $memory->{$key}->{debug_line})\n";
82 }
83
84 print STDERR "maximum used: $max, amount leaked: $total\n";