| OLD | NEW |
| (Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """Utility methods.""" |
| 6 |
| 7 import atexit |
| 8 import distutils.spawn |
| 9 import logging |
| 10 import os |
| 11 import platform |
| 12 import resource |
| 13 import sys |
| 14 |
| 15 |
| 16 SRC_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) |
| 17 |
| 18 |
| 19 def AddCommonOptionsAndParseArgs(parser): |
| 20 parser.add_argument('--no-pypy', action='store_true', |
| 21 help='Do not automatically switch to pypy when available') |
| 22 parser.add_argument('-v', |
| 23 '--verbose', |
| 24 default=0, |
| 25 action='count', |
| 26 help='Verbose level (multiple times for more)') |
| 27 |
| 28 args = parser.parse_args() |
| 29 |
| 30 logging.basicConfig(level=logging.WARNING - args.verbose * 10, |
| 31 format='%(levelname).1s %(relativeCreated)6d %(message)s') |
| 32 |
| 33 if not args.no_pypy and platform.python_implementation() == 'CPython': |
| 34 # Switch to pypy if it's available. |
| 35 pypy_path = distutils.spawn.find_executable('pypy') |
| 36 if pypy_path: |
| 37 logging.debug('Switching to pypy.') |
| 38 os.execv(pypy_path, [pypy_path] + sys.argv) |
| 39 # NOTE! Running with python: 6s. Running with pypy: 3s |
| 40 logging.warning('This script runs more than 2x faster if you install pypy.') |
| 41 |
| 42 if logging.getLogger().isEnabledFor(logging.DEBUG): |
| 43 atexit.register(_LogPeakRamUsage) |
| 44 return args |
| 45 |
| 46 |
| 47 def _LogPeakRamUsage(): |
| 48 peak_ram_usage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss |
| 49 peak_ram_usage += resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss |
| 50 logging.info('Peak RAM usage was %d MB.', peak_ram_usage / 1024) |
| OLD | NEW |