Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 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 |
| 6 | # met: |
| 7 | # |
| 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. |
| 17 | # |
| 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. |
| 29 | |
| 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. |
| 33 | |
| 34 | # Sample usage: |
| 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) |
| 38 | # or |
| 39 | # $ tools/process-heap-prof.py --js-cons-profile v8.log | hp2ps -c > script-heap-graph.ps |
| 40 | # to get JS constructor profile |
| 41 | |
| 42 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 43 | import csv, sys, time, optparse |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 45 | def ProcessLogFile(filename, options): |
| 46 | if options.js_cons_profile: |
| 47 | itemname = 'heap-js-cons-item' |
| 48 | else: |
| 49 | itemname = 'heap-sample-item' |
| 50 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 51 | first_call_time = None |
| 52 | sample_time = 0.0 |
| 53 | sampling = False |
| 54 | try: |
| 55 | logfile = open(filename, 'rb') |
| 56 | try: |
| 57 | logreader = csv.reader(logfile) |
| 58 | |
| 59 | print('JOB "v8"') |
| 60 | print('DATE "%s"' % time.asctime(time.localtime())) |
| 61 | print('SAMPLE_UNIT "seconds"') |
| 62 | print('VALUE_UNIT "bytes"') |
| 63 | |
| 64 | for row in logreader: |
| 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) |
| 71 | sampling = True |
| 72 | elif row[0] == 'heap-sample-end' and row[1] == 'Heap': |
| 73 | print('END_SAMPLE %.2f' % sample_time) |
| 74 | sampling = False |
| 75 | elif row[0] == itemname and sampling: |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 76 | print(row[1]), |
| 77 | if options.count: |
| 78 | print('%d' % (int(row[2]))), |
| 79 | if options.size: |
| 80 | print('%d' % (int(row[3]))), |
| 81 | print |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 82 | finally: |
| 83 | logfile.close() |
| 84 | except: |
| 85 | sys.exit('can\'t open %s' % filename) |
| 86 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 87 | |
| 88 | def BuildOptions(): |
| 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") |
| 96 | return result |
| 97 | |
| 98 | |
| 99 | def ProcessOptions(options): |
| 100 | if not options.size and not options.count: |
| 101 | options.size = True |
| 102 | return True |
| 103 | |
| 104 | |
| 105 | def Main(): |
| 106 | parser = BuildOptions() |
| 107 | (options, args) = parser.parse_args() |
| 108 | if not ProcessOptions(options): |
| 109 | parser.print_help() |
| 110 | sys.exit(); |
| 111 | |
| 112 | if not args: |
| 113 | print "Missing logfile" |
| 114 | sys.exit(); |
| 115 | |
| 116 | ProcessLogFile(args[0], options) |
| 117 | |
| 118 | |
| 119 | if __name__ == '__main__': |
| 120 | sys.exit(Main()) |