| OLD | NEW |
| 1 # Copyright 2017 The Chromium Authors. All rights reserved. | 1 # Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Deals with loading & saving .size files.""" | 5 """Deals with loading & saving .size files.""" |
| 6 | 6 |
| 7 import cStringIO | 7 import cStringIO |
| 8 import calendar | 8 import calendar |
| 9 import collections | 9 import collections |
| 10 import datetime | 10 import datetime |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 # Using an OrderedDict makes the indices more repetitive (better compression). | 46 # Using an OrderedDict makes the indices more repetitive (better compression). |
| 47 path_tuples = collections.OrderedDict.fromkeys( | 47 path_tuples = collections.OrderedDict.fromkeys( |
| 48 (s.object_path, s.source_path) for s in size_info.symbols) | 48 (s.object_path, s.source_path) for s in size_info.symbols) |
| 49 for i, key in enumerate(path_tuples): | 49 for i, key in enumerate(path_tuples): |
| 50 path_tuples[key] = i | 50 path_tuples[key] = i |
| 51 file_obj.write('%d\n' % len(path_tuples)) | 51 file_obj.write('%d\n' % len(path_tuples)) |
| 52 file_obj.writelines('%s\t%s\n' % pair for pair in path_tuples) | 52 file_obj.writelines('%s\t%s\n' % pair for pair in path_tuples) |
| 53 _LogSize(file_obj, 'paths') # For libchrome, adds 200kb. | 53 _LogSize(file_obj, 'paths') # For libchrome, adds 200kb. |
| 54 | 54 |
| 55 # Symbol counts by section. | 55 # Symbol counts by section. |
| 56 by_section = size_info.symbols.GroupBySectionName().Sorted( | 56 by_section = size_info.symbols.GroupedBySectionName().Sorted( |
| 57 key=lambda s:(s[0].IsBss(), s[0].address, s.name)) | 57 key=lambda s:(s[0].IsBss(), s[0].address, s.full_name)) |
| 58 file_obj.write('%s\n' % '\t'.join(g.name for g in by_section)) | 58 file_obj.write('%s\n' % '\t'.join(g.name for g in by_section)) |
| 59 file_obj.write('%s\n' % '\t'.join(str(len(g)) for g in by_section)) | 59 file_obj.write('%s\n' % '\t'.join(str(len(g)) for g in by_section)) |
| 60 | 60 |
| 61 def write_numeric(func, delta=False): | 61 def write_numeric(func, delta=False): |
| 62 for group in by_section: | 62 for group in by_section: |
| 63 prev_value = 0 | 63 prev_value = 0 |
| 64 last_sym = group[-1] | 64 last_sym = group[-1] |
| 65 for symbol in group: | 65 for symbol in group: |
| 66 value = func(symbol) | 66 value = func(symbol) |
| 67 if delta: | 67 if delta: |
| 68 value, prev_value = value - prev_value, value | 68 value, prev_value = value - prev_value, value |
| 69 file_obj.write(str(value)) | 69 file_obj.write(str(value)) |
| 70 if symbol is not last_sym: | 70 if symbol is not last_sym: |
| 71 file_obj.write(' ') | 71 file_obj.write(' ') |
| 72 file_obj.write('\n') | 72 file_obj.write('\n') |
| 73 | 73 |
| 74 write_numeric(lambda s: s.address, delta=True) | 74 write_numeric(lambda s: s.address, delta=True) |
| 75 _LogSize(file_obj, 'addresses') # For libchrome, adds 300kb. | 75 _LogSize(file_obj, 'addresses') # For libchrome, adds 300kb. |
| 76 # Do not write padding, it will be recalcualted from addresses on load. | 76 # Do not write padding, it will be recalcualted from addresses on load. |
| 77 write_numeric(lambda s: s.size_without_padding) | 77 write_numeric(lambda s: s.size_without_padding) |
| 78 _LogSize(file_obj, 'sizes') # For libchrome, adds 300kb | 78 _LogSize(file_obj, 'sizes') # For libchrome, adds 300kb |
| 79 write_numeric(lambda s: path_tuples[(s.object_path, s.source_path)], | 79 write_numeric(lambda s: path_tuples[(s.object_path, s.source_path)], |
| 80 delta=True) | 80 delta=True) |
| 81 _LogSize(file_obj, 'path indices') # For libchrome: adds 125kb. | 81 _LogSize(file_obj, 'path indices') # For libchrome: adds 125kb. |
| 82 | 82 |
| 83 prev_aliases = None | 83 prev_aliases = None |
| 84 for group in by_section: | 84 for group in by_section: |
| 85 for symbol in group: | 85 for symbol in group: |
| 86 # Do not write name when full_name exists. It will be derived on load. | 86 file_obj.write(symbol.full_name) |
| 87 file_obj.write(symbol.full_name or symbol.name) | |
| 88 if symbol.aliases and symbol.aliases is not prev_aliases: | 87 if symbol.aliases and symbol.aliases is not prev_aliases: |
| 89 file_obj.write('\t0%x' % symbol.num_aliases) | 88 file_obj.write('\t0%x' % symbol.num_aliases) |
| 90 prev_aliases = symbol.aliases | 89 prev_aliases = symbol.aliases |
| 91 if symbol.flags: | 90 if symbol.flags: |
| 92 file_obj.write('\t%x' % symbol.flags) | 91 file_obj.write('\t%x' % symbol.flags) |
| 93 file_obj.write('\n') | 92 file_obj.write('\n') |
| 94 _LogSize(file_obj, 'names (final)') # For libchrome: adds 3.5mb. | 93 _LogSize(file_obj, 'names (final)') # For libchrome: adds 3.5mb. |
| 95 | 94 |
| 96 | 95 |
| 97 def _LoadSizeInfoFromFile(file_obj): | 96 def _LoadSizeInfoFromFile(file_obj): |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 | 143 |
| 145 if len(parts) == 3: | 144 if len(parts) == 3: |
| 146 aliases_part = parts[1] | 145 aliases_part = parts[1] |
| 147 flags_part = parts[2] | 146 flags_part = parts[2] |
| 148 elif len(parts) == 2: | 147 elif len(parts) == 2: |
| 149 if parts[1][0] == '0': | 148 if parts[1][0] == '0': |
| 150 aliases_part = parts[1] | 149 aliases_part = parts[1] |
| 151 else: | 150 else: |
| 152 flags_part = parts[1] | 151 flags_part = parts[1] |
| 153 | 152 |
| 154 name = parts[0] | 153 full_name = parts[0] |
| 155 flags = int(flags_part, 16) if flags_part else 0 | 154 flags = int(flags_part, 16) if flags_part else 0 |
| 156 num_aliases = int(aliases_part, 16) if aliases_part else 0 | 155 num_aliases = int(aliases_part, 16) if aliases_part else 0 |
| 157 | 156 |
| 158 new_sym = models.Symbol.__new__(models.Symbol) | 157 new_sym = models.Symbol.__new__(models.Symbol) |
| 159 new_sym.section_name = cur_section_name | 158 new_sym.section_name = cur_section_name |
| 160 new_sym.address = addresses[section_index][i] | 159 new_sym.address = addresses[section_index][i] |
| 161 new_sym.size = sizes[section_index][i] | 160 new_sym.size = sizes[section_index][i] |
| 162 new_sym.name = name | 161 new_sym.full_name = full_name |
| 163 paths = path_tuples[path_indices[section_index][i]] | 162 paths = path_tuples[path_indices[section_index][i]] |
| 164 new_sym.object_path = paths[0] | 163 new_sym.object_path = paths[0] |
| 165 new_sym.source_path = paths[1] | 164 new_sym.source_path = paths[1] |
| 166 new_sym.flags = flags | 165 new_sym.flags = flags |
| 167 new_sym.padding = 0 # Derived | 166 new_sym.padding = 0 # Derived |
| 168 new_sym.full_name = None # Derived | 167 new_sym.template_name = '' # Derived |
| 168 new_sym.name = '' # Derived |
| 169 | 169 |
| 170 if num_aliases: | 170 if num_aliases: |
| 171 assert alias_counter == 0 | 171 assert alias_counter == 0 |
| 172 new_sym.aliases = [new_sym] | 172 new_sym.aliases = [new_sym] |
| 173 alias_counter = num_aliases - 1 | 173 alias_counter = num_aliases - 1 |
| 174 elif alias_counter > 0: | 174 elif alias_counter > 0: |
| 175 new_sym.aliases = raw_symbols[symbol_idx - 1].aliases | 175 new_sym.aliases = raw_symbols[symbol_idx - 1].aliases |
| 176 new_sym.aliases.append(new_sym) | 176 new_sym.aliases.append(new_sym) |
| 177 alias_counter -= 1 | 177 alias_counter -= 1 |
| 178 else: | 178 else: |
| 179 new_sym.aliases = None | 179 new_sym.aliases = None |
| 180 | 180 |
| 181 raw_symbols[symbol_idx] = new_sym | 181 raw_symbols[symbol_idx] = new_sym |
| 182 symbol_idx += 1 | 182 symbol_idx += 1 |
| 183 | 183 |
| 184 return models.SizeInfo(section_sizes, models.SymbolGroup(raw_symbols), | 184 return models.SizeInfo(section_sizes, raw_symbols, metadata=metadata) |
| 185 metadata=metadata) | |
| 186 | 185 |
| 187 | 186 |
| 188 def SaveSizeInfo(size_info, path): | 187 def SaveSizeInfo(size_info, path): |
| 189 """Saves |size_info| to |path}.""" | 188 """Saves |size_info| to |path}.""" |
| 190 if os.environ.get('SUPERSIZE_MEASURE_GZIP') == '1': | 189 if os.environ.get('SUPERSIZE_MEASURE_GZIP') == '1': |
| 191 with gzip.open(path, 'wb') as f: | 190 with gzip.open(path, 'wb') as f: |
| 192 _SaveSizeInfoToFile(size_info, f) | 191 _SaveSizeInfoToFile(size_info, f) |
| 193 else: | 192 else: |
| 194 # It is seconds faster to do gzip in a separate step. 6s -> 3.5s. | 193 # It is seconds faster to do gzip in a separate step. 6s -> 3.5s. |
| 195 stringio = cStringIO.StringIO() | 194 stringio = cStringIO.StringIO() |
| 196 _SaveSizeInfoToFile(size_info, stringio) | 195 _SaveSizeInfoToFile(size_info, stringio) |
| 197 | 196 |
| 198 logging.debug('Serialization complete. Gzipping...') | 197 logging.debug('Serialization complete. Gzipping...') |
| 199 stringio.seek(0) | 198 stringio.seek(0) |
| 200 with gzip.open(path, 'wb') as f: | 199 with gzip.open(path, 'wb') as f: |
| 201 shutil.copyfileobj(stringio, f) | 200 shutil.copyfileobj(stringio, f) |
| 202 | 201 |
| 203 | 202 |
| 204 def LoadSizeInfo(path): | 203 def LoadSizeInfo(path): |
| 205 """Returns a SizeInfo loaded from |path|.""" | 204 """Returns a SizeInfo loaded from |path|.""" |
| 206 with gzip.open(path) as f: | 205 with gzip.open(path) as f: |
| 207 return _LoadSizeInfoFromFile(f) | 206 return _LoadSizeInfoFromFile(f) |
| OLD | NEW |