| OLD | NEW |
| (Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Creates an html report that allows you to view binary size by component.""" |
| 7 |
| 8 import argparse |
| 9 import json |
| 10 import logging |
| 11 import os |
| 12 import shutil |
| 13 import sys |
| 14 |
| 15 import analyze |
| 16 import helpers |
| 17 |
| 18 |
| 19 # Node dictionary keys. These are output in json read by the webapp so |
| 20 # keep them short to save file size. |
| 21 # Note: If these change, the webapp must also change. |
| 22 _NODE_TYPE_KEY = 'k' |
| 23 _NODE_TYPE_BUCKET = 'b' |
| 24 _NODE_TYPE_PATH = 'p' |
| 25 _NODE_TYPE_SYMBOL = 's' |
| 26 _NODE_NAME_KEY = 'n' |
| 27 _NODE_CHILDREN_KEY = 'children' |
| 28 _NODE_SYMBOL_TYPE_KEY = 't' |
| 29 _NODE_SYMBOL_TYPE_VTABLE = 'v' |
| 30 _NODE_SYMBOL_TYPE_GENERATED = '*' |
| 31 _NODE_SYMBOL_SIZE_KEY = 'value' |
| 32 _NODE_MAX_DEPTH_KEY = 'maxDepth' |
| 33 _NODE_LAST_PATH_ELEMENT_KEY = 'lastPathElement' |
| 34 |
| 35 # The display name of the bucket where we put symbols without path. |
| 36 _NAME_NO_PATH_BUCKET = '(No Path)' |
| 37 |
| 38 # Try to keep data buckets smaller than this to avoid killing the |
| 39 # graphing lib. |
| 40 _BIG_BUCKET_LIMIT = 3000 |
| 41 |
| 42 |
| 43 def _GetOrMakeChildNode(node, node_type, name): |
| 44 child = node[_NODE_CHILDREN_KEY].get(name) |
| 45 if child is None: |
| 46 child = { |
| 47 _NODE_TYPE_KEY: node_type, |
| 48 _NODE_NAME_KEY: name, |
| 49 } |
| 50 if node_type != _NODE_TYPE_SYMBOL: |
| 51 child[_NODE_CHILDREN_KEY] = {} |
| 52 node[_NODE_CHILDREN_KEY][name] = child |
| 53 else: |
| 54 assert child[_NODE_TYPE_KEY] == node_type |
| 55 return child |
| 56 |
| 57 |
| 58 def _SplitLargeBucket(bucket): |
| 59 """Split the given node into sub-buckets when it's too big.""" |
| 60 old_children = bucket[_NODE_CHILDREN_KEY] |
| 61 count = 0 |
| 62 for symbol_type, symbol_bucket in old_children.iteritems(): |
| 63 count += len(symbol_bucket[_NODE_CHILDREN_KEY]) |
| 64 if count > _BIG_BUCKET_LIMIT: |
| 65 new_children = {} |
| 66 bucket[_NODE_CHILDREN_KEY] = new_children |
| 67 current_bucket = None |
| 68 index = 0 |
| 69 for symbol_type, symbol_bucket in old_children.iteritems(): |
| 70 for symbol_name, value in symbol_bucket[_NODE_CHILDREN_KEY].iteritems(): |
| 71 if index % _BIG_BUCKET_LIMIT == 0: |
| 72 group_no = (index / _BIG_BUCKET_LIMIT) + 1 |
| 73 node_name = '%s subgroup %d' % (_NAME_NO_PATH_BUCKET, group_no) |
| 74 current_bucket = _GetOrMakeChildNode( |
| 75 bucket, _NODE_TYPE_PATH, node_name) |
| 76 index += 1 |
| 77 symbol_size = value[_NODE_SYMBOL_SIZE_KEY] |
| 78 _AddSymbolIntoFileNode(current_bucket, symbol_type, symbol_name, |
| 79 symbol_size, True) |
| 80 |
| 81 |
| 82 def _MakeChildrenDictsIntoLists(node): |
| 83 """Recursively converts all children from dicts -> lists.""" |
| 84 children = node.get(_NODE_CHILDREN_KEY) |
| 85 if children: |
| 86 children = children.values() # Convert dict -> list. |
| 87 node[_NODE_CHILDREN_KEY] = children |
| 88 for child in children: |
| 89 _MakeChildrenDictsIntoLists(child) |
| 90 if len(children) > _BIG_BUCKET_LIMIT: |
| 91 logging.warning('Bucket found with %d entries. Might be unusable.', |
| 92 len(children)) |
| 93 |
| 94 |
| 95 def _AddSymbolIntoFileNode(node, symbol_type, symbol_name, symbol_size, |
| 96 include_symbols): |
| 97 """Puts symbol into the file path node |node|.""" |
| 98 node[_NODE_LAST_PATH_ELEMENT_KEY] = True |
| 99 # Don't bother with buckets when not including symbols. |
| 100 if include_symbols: |
| 101 node = _GetOrMakeChildNode(node, _NODE_TYPE_BUCKET, symbol_type) |
| 102 node[_NODE_SYMBOL_TYPE_KEY] = symbol_type |
| 103 |
| 104 # 'node' is now the symbol-type bucket. Make the child entry. |
| 105 if include_symbols or not symbol_name: |
| 106 node_name = symbol_name or '[Anonymous]' |
| 107 elif symbol_name.startswith('*'): |
| 108 node_name = symbol_name |
| 109 else: |
| 110 node_name = symbol_type |
| 111 node = _GetOrMakeChildNode(node, _NODE_TYPE_SYMBOL, node_name) |
| 112 node[_NODE_SYMBOL_SIZE_KEY] = node.get(_NODE_SYMBOL_SIZE_KEY, 0) + symbol_size |
| 113 node[_NODE_SYMBOL_TYPE_KEY] = symbol_type |
| 114 |
| 115 |
| 116 def _MakeCompactTree(root_group, include_symbols): |
| 117 result = { |
| 118 _NODE_NAME_KEY: '/', |
| 119 _NODE_CHILDREN_KEY: {}, |
| 120 _NODE_TYPE_KEY: 'p', |
| 121 _NODE_MAX_DEPTH_KEY: 0, |
| 122 } |
| 123 for symbol in root_group: |
| 124 file_path = symbol.path or _NAME_NO_PATH_BUCKET |
| 125 node = result |
| 126 depth = 0 |
| 127 for path_part in file_path.split(os.path.sep): |
| 128 if not path_part: |
| 129 continue |
| 130 depth += 1 |
| 131 node = _GetOrMakeChildNode(node, _NODE_TYPE_PATH, path_part) |
| 132 |
| 133 symbol_type = symbol.section |
| 134 if symbol.name: |
| 135 if symbol.name.endswith('[vtable]'): |
| 136 symbol_type = _NODE_SYMBOL_TYPE_VTABLE |
| 137 elif symbol.name.endswith(']'): |
| 138 symbol_type = _NODE_SYMBOL_TYPE_GENERATED |
| 139 _AddSymbolIntoFileNode(node, symbol_type, symbol.name, symbol.size, |
| 140 include_symbols) |
| 141 depth += 2 |
| 142 result[_NODE_MAX_DEPTH_KEY] = max(result[_NODE_MAX_DEPTH_KEY], depth) |
| 143 |
| 144 # The (no path) bucket can be extremely large if we failed to get |
| 145 # path information. Split it into subgroups if needed. |
| 146 no_path_bucket = result[_NODE_CHILDREN_KEY].get(_NAME_NO_PATH_BUCKET) |
| 147 if no_path_bucket and include_symbols: |
| 148 _SplitLargeBucket(no_path_bucket) |
| 149 |
| 150 _MakeChildrenDictsIntoLists(result) |
| 151 |
| 152 return result |
| 153 |
| 154 |
| 155 def _CopyTemplateFiles(dest_dir): |
| 156 d3_out = os.path.join(dest_dir, 'd3') |
| 157 if not os.path.exists(d3_out): |
| 158 os.makedirs(d3_out, 0755) |
| 159 d3_src = os.path.join(helpers.SRC_ROOT, 'third_party', 'd3', 'src') |
| 160 template_src = os.path.join(os.path.dirname(__file__), 'template') |
| 161 shutil.copy(os.path.join(d3_src, 'LICENSE'), d3_out) |
| 162 shutil.copy(os.path.join(d3_src, 'd3.js'), d3_out) |
| 163 shutil.copy(os.path.join(template_src, 'index.html'), dest_dir) |
| 164 shutil.copy(os.path.join(template_src, 'D3SymbolTreeMap.js'), dest_dir) |
| 165 |
| 166 |
| 167 def main(): |
| 168 parser = argparse.ArgumentParser() |
| 169 parser.add_argument('--report-dir', metavar='PATH', required=True, |
| 170 help='Write output to the specified directory. An HTML ' |
| 171 'report is generated here.') |
| 172 parser.add_argument('--include-bss', action='store_true', |
| 173 help='Include symbols from .bss (which consume no real ' |
| 174 'space)') |
| 175 parser.add_argument('--include-symbols', action='store_true', |
| 176 help='Use per-symbol granularity rather than per-file.') |
| 177 analyze.AddOptions(parser) |
| 178 args = helpers.AddCommonOptionsAndParseArgs(parser) |
| 179 |
| 180 result = analyze.AnalyzeWithArgs(args) |
| 181 root_group = result.symbol_group |
| 182 if not args.include_bss: |
| 183 root_group = root_group.WhereInSection('b').Inverted() |
| 184 root_group = root_group.WhereBiggerThan(0) |
| 185 |
| 186 # Copy report boilerplate into output directory. This also proves that the |
| 187 # output directory is safe for writing, so there should be no problems writing |
| 188 # the nm.out file later. |
| 189 _CopyTemplateFiles(args.report_dir) |
| 190 |
| 191 logging.info('Creating JSON objects') |
| 192 tree_root = _MakeCompactTree(root_group, args.include_symbols) |
| 193 |
| 194 logging.info('Serializing') |
| 195 with open(os.path.join(args.report_dir, 'data.js'), 'w') as out_file: |
| 196 out_file.write('var tree_data=') |
| 197 # Use separators without whitespace to get a smaller file. |
| 198 json.dump(tree_root, out_file, ensure_ascii=False, check_circular=False, |
| 199 separators=(',', ':')) |
| 200 |
| 201 print 'Report saved to ' + args.report_dir + '/index.html' |
| 202 |
| 203 if __name__ == '__main__': |
| 204 sys.exit(main()) |
| OLD | NEW |