| 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 for group in by_section: | 84 for group in by_section: |
| 84 for symbol in group: | 85 for symbol in group: |
| 85 # Do not write name when full_name exists. It will be derived on load. | 86 # Do not write name when full_name exists. It will be derived on load. |
| 86 file_obj.write(symbol.full_name or symbol.name) | 87 file_obj.write(symbol.full_name or symbol.name) |
| 88 if symbol.aliases and symbol.aliases is not prev_aliases: |
| 89 file_obj.write('\t0%x' % symbol.num_aliases) |
| 90 prev_aliases = symbol.aliases |
| 87 if symbol.flags: | 91 if symbol.flags: |
| 88 file_obj.write('\t%x' % symbol.flags) | 92 file_obj.write('\t%x' % symbol.flags) |
| 89 file_obj.write('\n') | 93 file_obj.write('\n') |
| 90 _LogSize(file_obj, 'names (final)') # For libchrome: adds 3.5mb. | 94 _LogSize(file_obj, 'names (final)') # For libchrome: adds 3.5mb. |
| 91 | 95 |
| 92 | 96 |
| 93 def _LoadSizeInfoFromFile(file_obj): | 97 def _LoadSizeInfoFromFile(file_obj): |
| 94 """Loads a size_info from the given file.""" | 98 """Loads a size_info from the given file.""" |
| 95 lines = iter(file_obj) | 99 lines = iter(file_obj) |
| 96 next(lines) # Comment line. | 100 next(lines) # Comment line. |
| (...skipping 28 matching lines...) Expand all Loading... |
| 125 ret.append(fields) | 129 ret.append(fields) |
| 126 return ret | 130 return ret |
| 127 | 131 |
| 128 addresses = read_numeric(delta=True) | 132 addresses = read_numeric(delta=True) |
| 129 sizes = read_numeric(delta=False) | 133 sizes = read_numeric(delta=False) |
| 130 path_indices = read_numeric(delta=True) | 134 path_indices = read_numeric(delta=True) |
| 131 | 135 |
| 132 raw_symbols = [None] * sum(section_counts) | 136 raw_symbols = [None] * sum(section_counts) |
| 133 symbol_idx = 0 | 137 symbol_idx = 0 |
| 134 for section_index, cur_section_name in enumerate(section_names): | 138 for section_index, cur_section_name in enumerate(section_names): |
| 139 alias_counter = 0 |
| 135 for i in xrange(section_counts[section_index]): | 140 for i in xrange(section_counts[section_index]): |
| 136 line = next(lines)[:-1] | 141 parts = next(lines)[:-1].split('\t') |
| 137 name = line | 142 flags_part = None |
| 138 flags = 0 | 143 aliases_part = None |
| 139 last_tab_idx = line.find('\t', -3) # Allows for two digits of flags. | 144 |
| 140 if last_tab_idx != -1: | 145 if len(parts) == 3: |
| 141 flags = int(line[last_tab_idx + 1:], 16) | 146 aliases_part = parts[1] |
| 142 name = line[:last_tab_idx] | 147 flags_part = parts[2] |
| 148 elif len(parts) == 2: |
| 149 if parts[1][0] == '0': |
| 150 aliases_part = parts[1] |
| 151 else: |
| 152 flags_part = parts[1] |
| 153 |
| 154 name = parts[0] |
| 155 flags = int(flags_part, 16) if flags_part else 0 |
| 156 num_aliases = int(aliases_part, 16) if aliases_part else 0 |
| 143 | 157 |
| 144 new_sym = models.Symbol.__new__(models.Symbol) | 158 new_sym = models.Symbol.__new__(models.Symbol) |
| 145 new_sym.section_name = cur_section_name | 159 new_sym.section_name = cur_section_name |
| 146 new_sym.address = addresses[section_index][i] | 160 new_sym.address = addresses[section_index][i] |
| 147 new_sym.size = sizes[section_index][i] | 161 new_sym.size = sizes[section_index][i] |
| 148 new_sym.name = name | 162 new_sym.name = name |
| 149 paths = path_tuples[path_indices[section_index][i]] | 163 paths = path_tuples[path_indices[section_index][i]] |
| 150 new_sym.object_path = paths[0] | 164 new_sym.object_path = paths[0] |
| 151 new_sym.source_path = paths[1] | 165 new_sym.source_path = paths[1] |
| 152 new_sym.flags = flags | 166 new_sym.flags = flags |
| 153 new_sym.padding = 0 # Derived | 167 new_sym.padding = 0 # Derived |
| 154 new_sym.full_name = None # Derived | 168 new_sym.full_name = None # Derived |
| 169 |
| 170 if num_aliases: |
| 171 assert alias_counter == 0 |
| 172 new_sym.aliases = [new_sym] |
| 173 alias_counter = num_aliases - 1 |
| 174 elif alias_counter > 0: |
| 175 new_sym.aliases = raw_symbols[symbol_idx - 1].aliases |
| 176 new_sym.aliases.append(new_sym) |
| 177 alias_counter -= 1 |
| 178 else: |
| 179 new_sym.aliases = None |
| 180 |
| 155 raw_symbols[symbol_idx] = new_sym | 181 raw_symbols[symbol_idx] = new_sym |
| 156 symbol_idx += 1 | 182 symbol_idx += 1 |
| 183 prev_sym = new_sym |
| 157 | 184 |
| 158 return models.SizeInfo(section_sizes, raw_symbols, metadata=metadata) | 185 return models.SizeInfo(section_sizes, raw_symbols, metadata=metadata) |
| 159 | 186 |
| 160 | 187 |
| 161 def SaveSizeInfo(size_info, path): | 188 def SaveSizeInfo(size_info, path): |
| 162 """Saves |size_info| to |path}.""" | 189 """Saves |size_info| to |path}.""" |
| 163 if os.environ.get('MEASURE_GZIP') == '1': | 190 if os.environ.get('SUPERSIZE_MEASURE_GZIP') == '1': |
| 164 with gzip.open(path, 'wb') as f: | 191 with gzip.open(path, 'wb') as f: |
| 165 _SaveSizeInfoToFile(size_info, f) | 192 _SaveSizeInfoToFile(size_info, f) |
| 166 else: | 193 else: |
| 167 # It is seconds faster to do gzip in a separate step. 6s -> 3.5s. | 194 # It is seconds faster to do gzip in a separate step. 6s -> 3.5s. |
| 168 stringio = cStringIO.StringIO() | 195 stringio = cStringIO.StringIO() |
| 169 _SaveSizeInfoToFile(size_info, stringio) | 196 _SaveSizeInfoToFile(size_info, stringio) |
| 170 | 197 |
| 171 logging.debug('Serialization complete. Gzipping...') | 198 logging.debug('Serialization complete. Gzipping...') |
| 172 stringio.seek(0) | 199 stringio.seek(0) |
| 173 with gzip.open(path, 'wb') as f: | 200 with gzip.open(path, 'wb') as f: |
| 174 shutil.copyfileobj(stringio, f) | 201 shutil.copyfileobj(stringio, f) |
| 175 | 202 |
| 176 | 203 |
| 177 def LoadSizeInfo(path): | 204 def LoadSizeInfo(path): |
| 178 """Returns a SizeInfo loaded from |path|.""" | 205 """Returns a SizeInfo loaded from |path|.""" |
| 179 with gzip.open(path) as f: | 206 with gzip.open(path) as f: |
| 180 return _LoadSizeInfoFromFile(f) | 207 return _LoadSizeInfoFromFile(f) |
| OLD | NEW |