| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 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 """Scans the Chromium source for histograms that are absent from histograms.xml. | 5 """Scans the Chromium source for histograms that are absent from histograms.xml. |
| 6 | 6 |
| 7 This is a heuristic scan, so a clean run of this script does not guarantee that | 7 This is a heuristic scan, so a clean run of this script does not guarantee that |
| 8 all histograms in the Chromium source are properly mapped. Notably, field | 8 all histograms in the Chromium source are properly mapped. Notably, field |
| 9 trials are entirely ignored by this script. | 9 trials are entirely ignored by this script. |
| 10 | 10 |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 | 282 |
| 283 Args: | 283 Args: |
| 284 name: The string to hash (a histogram name). | 284 name: The string to hash (a histogram name). |
| 285 | 285 |
| 286 Returns: | 286 Returns: |
| 287 Histogram hash as a string representing a hex number (with leading 0x). | 287 Histogram hash as a string representing a hex number (with leading 0x). |
| 288 """ | 288 """ |
| 289 return '0x' + hashlib.md5(name).hexdigest()[:16] | 289 return '0x' + hashlib.md5(name).hexdigest()[:16] |
| 290 | 290 |
| 291 | 291 |
| 292 def output_csv(unmapped_histograms, location_map): |
| 293 for histogram in sorted(unmapped_histograms): |
| 294 parts = location_map[histogram].split(':') |
| 295 assert len(parts) == 2 |
| 296 (filename, line_number) = parts |
| 297 print '%s,%s,%s,%s' % (filename, line_number, histogram, |
| 298 hashHistogramName(histogram)) |
| 299 |
| 300 |
| 301 def output_log(unmapped_histograms, location_map, verbose): |
| 302 if len(unmapped_histograms): |
| 303 logging.info('') |
| 304 logging.info('') |
| 305 logging.info('Histograms in Chromium but not in XML files:') |
| 306 logging.info('-------------------------------------------------') |
| 307 for histogram in sorted(unmapped_histograms): |
| 308 if verbose: |
| 309 logging.info('%s: %s - %s', location_map[histogram], histogram, |
| 310 hashHistogramName(histogram)) |
| 311 else: |
| 312 logging.info(' %s - %s', histogram, hashHistogramName(histogram)) |
| 313 else: |
| 314 logging.info('Success! No unmapped histograms found.') |
| 315 |
| 316 |
| 292 def main(): | 317 def main(): |
| 293 # Find default paths. | 318 # Find default paths. |
| 294 default_root = path_util.GetInputFile('/') | 319 default_root = path_util.GetInputFile('/') |
| 295 default_histograms_path = path_util.GetInputFile( | 320 default_histograms_path = path_util.GetInputFile( |
| 296 'tools/metrics/histograms/histograms.xml') | 321 'tools/metrics/histograms/histograms.xml') |
| 297 default_extra_histograms_path = path_util.GetInputFile( | 322 default_extra_histograms_path = path_util.GetInputFile( |
| 298 'tools/histograms/histograms.xml') | 323 'tools/histograms/histograms.xml') |
| 299 | 324 |
| 300 # Parse command line options | 325 # Parse command line options |
| 301 parser = optparse.OptionParser() | 326 parser = optparse.OptionParser() |
| 302 parser.add_option( | 327 parser.add_option( |
| 303 '--root-directory', dest='root_directory', default=default_root, | 328 '--root-directory', dest='root_directory', default=default_root, |
| 304 help='scan within DIRECTORY for histograms [optional, defaults to "%s"]' % | 329 help='scan within DIRECTORY for histograms [optional, defaults to "%s"]' % |
| 305 default_root, | 330 default_root, |
| 306 metavar='DIRECTORY') | 331 metavar='DIRECTORY') |
| 307 parser.add_option( | 332 parser.add_option( |
| 308 '--histograms-file', dest='histograms_file_location', | 333 '--histograms-file', dest='histograms_file_location', |
| 309 default=default_histograms_path, | 334 default=default_histograms_path, |
| 310 help='read histogram definitions from FILE (relative to --root-directory) ' | 335 help='read histogram definitions from FILE (relative to --root-directory) ' |
| 311 '[optional, defaults to "%s"]' % default_histograms_path, | 336 '[optional, defaults to "%s"]' % default_histograms_path, |
| 312 metavar='FILE') | 337 metavar='FILE') |
| 313 parser.add_option( | 338 parser.add_option( |
| 314 '--exrta_histograms-file', dest='extra_histograms_file_location', | 339 '--exrta_histograms-file', dest='extra_histograms_file_location', |
| 315 default=default_extra_histograms_path, | 340 default=default_extra_histograms_path, |
| 316 help='read additional histogram definitions from FILE (relative to ' | 341 help='read additional histogram definitions from FILE (relative to ' |
| 317 '--root-directory) [optional, defaults to "%s"]' % | 342 '--root-directory) [optional, defaults to "%s"]' % |
| 318 default_extra_histograms_path, | 343 default_extra_histograms_path, |
| 319 metavar='FILE') | 344 metavar='FILE') |
| 320 parser.add_option( | 345 parser.add_option( |
| 346 '--csv', action='store_true', dest='output_as_csv', default=False, |
| 347 help=( |
| 348 'output as csv for ease of parsing ' + |
| 349 '[optional, defaults to %default]')) |
| 350 parser.add_option( |
| 321 '--verbose', action='store_true', dest='verbose', default=False, | 351 '--verbose', action='store_true', dest='verbose', default=False, |
| 322 help=( | 352 help=( |
| 323 'print file position information with histograms ' + | 353 'print file position information with histograms ' + |
| 324 '[optional, defaults to %default]')) | 354 '[optional, defaults to %default]')) |
| 325 | 355 |
| 326 (options, args) = parser.parse_args() | 356 (options, args) = parser.parse_args() |
| 327 if args: | 357 if args: |
| 328 parser.print_help() | 358 parser.print_help() |
| 329 sys.exit(1) | 359 sys.exit(1) |
| 330 | 360 |
| 331 logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) | 361 logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO) |
| 332 | 362 |
| 333 try: | 363 try: |
| 334 os.chdir(options.root_directory) | 364 os.chdir(options.root_directory) |
| 335 except EnvironmentError as e: | 365 except EnvironmentError as e: |
| 336 logging.error("Could not change to root directory: %s", e) | 366 logging.error("Could not change to root directory: %s", e) |
| 337 sys.exit(1) | 367 sys.exit(1) |
| 338 chromium_histograms, location_map = readChromiumHistograms() | 368 chromium_histograms, location_map = readChromiumHistograms() |
| 339 xml_histograms = readXmlHistograms(options.histograms_file_location) | 369 xml_histograms = readXmlHistograms(options.histograms_file_location) |
| 340 unmapped_histograms = chromium_histograms - xml_histograms | 370 unmapped_histograms = chromium_histograms - xml_histograms |
| 341 | 371 |
| 342 if os.path.isfile(options.extra_histograms_file_location): | 372 if os.path.isfile(options.extra_histograms_file_location): |
| 343 xml_histograms2 = readXmlHistograms(options.extra_histograms_file_location) | 373 xml_histograms2 = readXmlHistograms(options.extra_histograms_file_location) |
| 344 unmapped_histograms -= xml_histograms2 | 374 unmapped_histograms -= xml_histograms2 |
| 345 else: | 375 else: |
| 346 logging.warning('No such file: %s', options.extra_histograms_file_location) | 376 logging.warning('No such file: %s', options.extra_histograms_file_location) |
| 347 | 377 |
| 348 if len(unmapped_histograms): | 378 if options.output_as_csv: |
| 349 logging.info('') | 379 output_csv(unmapped_histograms, location_map) |
| 350 logging.info('') | |
| 351 logging.info('Histograms in Chromium but not in XML files:') | |
| 352 logging.info('-------------------------------------------------') | |
| 353 for histogram in sorted(unmapped_histograms): | |
| 354 if options.verbose: | |
| 355 logging.info('%s: %s - %s', location_map[histogram], histogram, | |
| 356 hashHistogramName(histogram)) | |
| 357 else: | |
| 358 logging.info(' %s - %s', histogram, hashHistogramName(histogram)) | |
| 359 else: | 380 else: |
| 360 logging.info('Success! No unmapped histograms found.') | 381 output_log(unmapped_histograms, location_map, options.verbose) |
| 361 | 382 |
| 362 | 383 |
| 363 if __name__ == '__main__': | 384 if __name__ == '__main__': |
| 364 main() | 385 main() |
| OLD | NEW |