3 # Copyright 2009 the V8 project authors. All rights reserved.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following
12 # disclaimer in the documentation and/or other materials provided
13 # with the distribution.
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived
16 # from this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 # This is an utility for converting V8 heap logs into .hp files that can
31 # be further processed using 'hp2ps' tool (bundled with GHC and Valgrind)
32 # to produce heap usage histograms.
35 # $ ./shell --log-gc script.js
36 # $ tools/process-heap-prof.py v8.log | hp2ps -c > script-heap-graph.ps
37 # ('-c' enables color, see hp2ps manual page for more options)
39 # $ tools/process-heap-prof.py --js-cons-profile v8.log | hp2ps -c > script-heap-graph.ps
40 # to get JS constructor profile
43 import csv, sys, time, optparse
45 def ProcessLogFile(filename, options):
46 if options.js_cons_profile:
47 itemname = 'heap-js-cons-item'
49 itemname = 'heap-sample-item'
51 first_call_time = None
55 logfile = open(filename, 'rb')
57 logreader = csv.reader(logfile)
60 print('DATE "%s"' % time.asctime(time.localtime()))
61 print('SAMPLE_UNIT "seconds"')
62 print('VALUE_UNIT "bytes"')
65 if row[0] == 'heap-sample-begin' and row[1] == 'Heap':
66 sample_time = float(row[3])/1000.0
67 if first_call_time == None:
68 first_call_time = sample_time
69 sample_time -= first_call_time
70 print('BEGIN_SAMPLE %.2f' % sample_time)
72 elif row[0] == 'heap-sample-end' and row[1] == 'Heap':
73 print('END_SAMPLE %.2f' % sample_time)
75 elif row[0] == itemname and sampling:
78 print('%d' % (int(row[2]))),
80 print('%d' % (int(row[3]))),
85 sys.exit('can\'t open %s' % filename)
89 result = optparse.OptionParser()
90 result.add_option("--js_cons_profile", help="Constructor profile",
91 default=False, action="store_true")
92 result.add_option("--size", help="Report object size",
93 default=False, action="store_true")
94 result.add_option("--count", help="Report object count",
95 default=False, action="store_true")
99 def ProcessOptions(options):
100 if not options.size and not options.count:
106 parser = BuildOptions()
107 (options, args) = parser.parse_args()
108 if not ProcessOptions(options):
113 print "Missing logfile"
116 ProcessLogFile(args[0], options)
119 if __name__ == '__main__':