summaryrefslogtreecommitdiffstats
path: root/tools/memory_watcher/scripts/summary.pl
blob: aca4e0ddae88fcc103e150c066ed367f80933c4f (plain)
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
#!/usr/bin/perl
#
# Read a memtrace logfile from stdin and group memory allocations by logical
# code component. The code component is guessed from the callstack, and
# is something like {v8, sqlite, disk cache, skia, etc..}
#
# Usage:
#
#   summary.pl
#
#      [STDIN] -- The memwatcher.logXXXX file to summarize.
#

sub process_stdin() {
  my %leaks = ();
  my $total_bytes = 0;

  while(<STDIN>) {
    my $line = $_;
    chomp($line);
    my $bytes, $loc;
    ($bytes, $loc) = ($line =~ m/[ \t]*([0-9]*)[ \t]*[0-9\.%]*[ \t]*(.*)/);
    chomp($loc);
    while(<STDIN>) {
	my $cont = $_;
        chomp($cont);
        last if $cont =~ m/=====/;
	$loc .= "\n" . $cont;
    }
    my $location_blame = "";

#    print "Found: $bytes, $loc\n";
    
    if ($loc =~ m/v8::internal::Snapshot::Deserialize/) {
      $location_blame = "v8 Snapshot Deserialize";
    } elsif ($loc =~ m/RenderStyle::create/) {
      $location_blame = "RenderStyle::create";
    } elsif ($loc =~ m/v8::internal::OldSpace::SlowAllocateRaw/) {
      $location_blame = "v8 OldSpace";
    } elsif ($loc =~ m/sqlite/) {
      $location_blame = "sqlite";
    } elsif ($loc =~ m/ TransportDIB::Map/) {
      $location_blame = "Shared Memory Backing Store";
    } elsif ($loc =~ m/imagedecoder/) {
      $location_blame = "img decoder";
    } elsif ($loc =~ m/SkBitmap/) {
      $location_blame = "skia";
    } elsif ($loc =~ m/disk_cache/) {
      $location_blame = "disk cache";
    } elsif ($loc =~ m/skia/) {
      $location_blame = "skia";
    } elsif ($loc =~ m/:WSA/) {
      $location_blame = "net";
    } elsif ($loc =~ m/dns/) {
      $location_blame = "net";
    } elsif ($loc =~ m/trunk\\net/) {
      $location_blame = "net";
    } elsif ($loc =~ m/WinHttp/) {
      $location_blame = "WinHttp";
    } elsif ($loc =~ m/:I_Crypt/) {
      $location_blame = "WinHttpSSL";
    } elsif ($loc =~ m/CryptGetTls/) {
      $location_blame = "WinHttpSSL";
    } elsif ($loc =~ m/WinVerifyTrust/) {
      $location_blame = "WinHttpSSL";
    } elsif ($loc =~ m/Cert/) {
      $location_blame = "WinHttpSSL";
    } elsif ($loc =~ m/plugin/) {
      $location_blame = "plugin";
    } elsif ($loc =~ m/NP_/) {
      $location_blame = "plugin";
    } elsif ($loc =~ m/hunspell/) {
      $location_blame = "hunspell";
    } elsif ($loc =~ m/TextCodec/) {
      $location_blame = "fonts";
    } elsif ($loc =~ m/glyph/) {
      $location_blame = "fonts";
    } elsif ($loc =~ m/cssparser/) {
      $location_blame = "webkit css";
    } elsif ($loc =~ m/::CSS/) {
      $location_blame = "webkit css";
    } elsif ($loc =~ m/Arena/) {
      $location_blame = "webkit arenas";
    } elsif ($loc =~ m/WebCore::.*ResourceLoader::addData/) {
      $location_blame = "WebCore *ResourceLoader addData";
    } elsif ($loc =~ m/OnUpdateVisitedLinks/) {
      $location_blame = "OnUpdateVisitedLinks";
    } elsif ($loc =~ m/IPC/) {
      $location_blame = "ipc";
    } elsif ($loc =~ m/trunk\\chrome\\browser/) {
      $location_blame = "browser";
    } elsif ($loc =~ m/trunk\\chrome\\renderer/) {
      $location_blame = "renderer";
    } elsif ($loc =~ m/webcore\\html/) {
      $location_blame = "webkit webcore html";
    } elsif ($loc =~ m/webkit.*string/) {
      $location_blame = "webkit strings";
    } elsif ($loc =~ m/htmltokenizer/) {
      $location_blame = "webkit HTMLTokenizer";
    } elsif ($loc =~ m/javascriptcore/) {
      $location_blame = "webkit javascriptcore";
    } elsif ($loc =~ m/webkit/) {
      $location_blame = "webkit other";
    } elsif ($loc =~ m/safe_browsing/) {
      $location_blame = "safe_browsing";
    } elsif ($loc =~ m/VisitedLinkMaster/) {
      $location_blame = "VisitedLinkMaster";
    } elsif ($loc =~ m/NewDOMUI/) {
      $location_blame = "NewDOMUI";
    } elsif ($loc =~ m/RegistryControlledDomainService/) {
      $location_blame = "RegistryControlledDomainService";
    } elsif ($loc =~ m/URLRequestChromeJob::DataAvailable/) {
      $location_blame = "URLRequestChromeJob DataAvailable";
    } elsif ($loc =~ m/history_publisher/) {
      $location_blame = "history publisher";
    } else {
      $location_blame = "unknown";
    }

    # Surface large outliers in an "interesting" group.
    my $interesting_group = "unknown";
    my $interesting_size = 10000000;  # Make this smaller as needed.
    # TODO(jar): Add this as a pair of shell arguments.
    if ($bytes > $interesting_size && $location_blame eq $interesting_group) {
      # Create a special group for the exact stack that contributed so much.
      $location_blame = $loc;
    }

    $total_bytes += $bytes;
    $leaks{$location_blame} += $bytes;
  }

  # now dump our hash table
  my $sum = 0;
  my @keys = sort { $leaks{$b} <=> $leaks{$a}  }keys %leaks;
  for ($i=0; $i<@keys; $i++) {
    my $key = @keys[$i];
    printf "%11s\t(%3.2f%%)\t%s\n", comma_print($leaks{$key}), (100* $leaks{$key} / $total_bytes), $key;
    $sum += $leaks{$key};
  }
  printf("TOTAL: %s\n", comma_print($sum));
}

# Insert commas into an integer after each three digits for printing.
sub comma_print {
    my $num = "$_[0]";
    $num =~ s/(\d{1,3}?)(?=(\d{3})+$)/$1,/g;
    return $num;
}

# ----- Main ------------------------------------------------

process_stdin();