def _OptimizeChrome(chrome_dir, temp_dir, output_dir, log_files, input_dll=None, input_pdb=None): _LOGGER.info('Optimizing Chrome.') # Generate the ordering file for chrome.dll. cmd = [runner._GetExePath('reorder.exe'), '--verbose', '--output-stats', '--input-dll=%s' % (input_dll if input_dll else os.path.join(chrome_dir, 'chrome.dll')), '--instrumented-dll=%s' % os.path.join(temp_dir, r'instrumented', 'chrome.dll'), '--output-file=%s' % os.path.join(temp_dir, 'chrome.dll-order.json'),] cmd.extend(log_files) ret = _Subprocess(cmd) if ret != 0: raise OptimizationError('Failed to generate an ordering for chrome.dll') if os.path.isdir(output_dir): _LOGGER.info('Removing pre-existing output dir "%s".', output_dir) _RmTree(output_dir) _LOGGER.info('Copying "%s" to output dir "%s".', chrome_dir, output_dir) _CopyChromeFiles(chrome_dir, output_dir, input_dll, input_pdb) cmd = [runner._GetExePath('relink.exe'), '--verbose', '--input-dll=%s' % os.path.join(output_dir, 'chrome.dll'), '--input-pdb=%s' % os.path.join(output_dir, 'chrome_dll.pdb'), '--output-dll=%s' % os.path.join(output_dir, 'chrome.dll'), '--output-pdb=%s' % os.path.join(output_dir, 'chrome_dll.pdb'), '--order-file=%s' % os.path.join(temp_dir, 'chrome.dll-order.json'),] ret = _Subprocess(cmd) if ret != 0: raise OptimizationError('Failed to reorder chrome.dll')
def InstrumentChrome(chrome_dir, output_dir): """Makes an instrumented copy of the Chrome files in chrome_dir in output_dir. Args: chrome_dir: the directory containing the input files. output_dir: the directory where the output will be generated. Raises: InstrumentationError if instrumentation fails. """ _LOGGER.info('Copying chrome files from "%s" to "%s".', chrome_dir, output_dir) chrome_utils.CopyChromeFiles(chrome_dir, output_dir) # Drop the call-trace client DLL into the temp dir. shutil.copy2(runner._GetExePath('call_trace_client.dll'), output_dir) for file in _EXECUTABLES: _LOGGER.info('Instrumenting "%s".', file) src_file = os.path.join(chrome_dir, file) dst_file = os.path.join(output_dir, file) cmd = [runner._GetExePath('instrument.exe'), '--input-dll=%s' % src_file, '--output-dll=%s' % dst_file, '--call-trace-client=RPC'] ret = chrome_utils.Subprocess(cmd) if ret != 0: raise InstrumentationError('Failed to instrument "%s".' % file)
def _OptimizeChrome(chrome_dir, temp_dir, output_dir, log_files): _LOGGER.info('Optimizing Chrome.') # Generate the ordering file for chrome.dll. cmd = [runner._GetExePath('reorder.exe'), '--verbose', '--output-stats', '--input-image=%s' % os.path.join(chrome_dir, 'chrome.dll'), '--instrumented-image=%s' % os.path.join(temp_dir, 'instrumented', 'chrome.dll'), '--output-file=%s' % os.path.join(temp_dir, 'chrome.dll-order.json'),] cmd.extend(log_files) ret = chrome_utils.Subprocess(cmd) if ret != 0: raise OptimizationError('Failed to generate an ordering for chrome.dll') if os.path.isfile(output_dir): raise OptimizationError('File present at output dir location: "%s"', output_dir) _LOGGER.info('Copying "%s" to output dir "%s".', chrome_dir, output_dir) chrome_utils.CopyChromeFiles(chrome_dir, output_dir) cmd = [runner._GetExePath('relink.exe'), '--verbose', '--input-image=%s' % os.path.join(chrome_dir, 'chrome.dll'), '--input-pdb=%s' % os.path.join(chrome_dir, 'chrome_dll.pdb'), '--output-image=%s' % os.path.join(output_dir, 'chrome.dll'), '--output-pdb=%s' % os.path.join(output_dir, 'chrome_dll.pdb'), '--order-file=%s' % os.path.join(temp_dir, 'chrome.dll-order.json'), '--overwrite'] ret = chrome_utils.Subprocess(cmd) if ret != 0: raise OptimizationError('Failed to reorder chrome.dll')
def InstrumentChrome(chrome_dir, output_dir, mode, args): """Makes an instrumented copy of the Chrome files in chrome_dir in output_dir. Args: chrome_dir: the directory containing the input files. output_dir: the directory where the output will be generated. mode: the instrumentation mode to use. args: additional arguments for instrument.exe Raises: InstrumentationError if instrumentation fails. """ if mode not in _MODE_INFO: raise InstrumentationError("Unrecognized mode: %s" % mode) _LOGGER.info('Copying chrome files from "%s" to "%s".', chrome_dir, output_dir) chrome_utils.CopyChromeFiles(chrome_dir, output_dir) # Drop the agent DLL, if any, into the output dir. agent_dll = _MODE_INFO[mode] if agent_dll: shutil.copy2(runner._GetExePath(agent_dll), output_dir) for path in EXECUTABLES: InstrumentExecutable( chrome_dir, output_dir, mode, args, agent_dll, path, False) for path in EXECUTABLES_OPTIONAL: InstrumentExecutable( chrome_dir, output_dir, mode, args, agent_dll, path, True)
def _InstrumentChrome(chrome_dir, temp_dir, input_dll=None, input_pdb=None): _LOGGER.info('Copying chrome files from "%s" to "%s".', chrome_dir, temp_dir) _CopyChromeFiles(chrome_dir, temp_dir, input_dll, input_pdb) # Drop call_trace.dll in the temp dir. shutil.copy2(runner._GetExePath('call_trace.dll'), temp_dir) for file in _EXECUTABLES: _LOGGER.info('Instrumenting "%s".', file) src_file = os.path.join(temp_dir, file) dst_file = os.path.join(temp_dir, file) cmd = [runner._GetExePath('instrument.exe'), '--input-dll=%s' % src_file, '--output-dll=%s' % dst_file] ret = _Subprocess(cmd) if ret != 0: raise OptimizationError('Failed to instrument "%s".' % file)
def InstrumentChrome(chrome_dir, output_dir, client_dll): """Makes an instrumented copy of the Chrome files in chrome_dir in output_dir. Args: chrome_dir: the directory containing the input files. output_dir: the directory where the output will be generated. client_dll: the DLL implementing the call trace profiler interface Raises: InstrumentationError if instrumentation fails. """ _LOGGER.info('Copying chrome files from "%s" to "%s".', chrome_dir, output_dir) chrome_utils.CopyChromeFiles(chrome_dir, output_dir) # Drop the call-trace client DLL into the temp dir. shutil.copy2(runner._GetExePath(client_dll), output_dir) for path in _EXECUTABLES: _LOGGER.info('Instrumenting "%s".', path) src_file = os.path.join(chrome_dir, path) dst_file = os.path.join(output_dir, path) cmd = [runner._GetExePath('instrument.exe'), '--input-image=%s' % src_file, '--output-image=%s' % dst_file, '--call-trace-client=%s' % client_dll, '--overwrite'] if client_dll == 'profile_client.dll': cmd.append('--no-interior-refs') ret = chrome_utils.Subprocess(cmd) if ret != 0: raise InstrumentationError('Failed to instrument "%s".' % path)
def InstrumentExecutable( chrome_dir, output_dir, mode, args, agent_dll, executable, optional): """Makes an instrumented copy of the Chrome files in |chrome_dir| in |output_dir|. Args: chrome_dir: the directory containing the input files. output_dir: the directory where the output will be generated. mode: the instrumentation mode to use. args: additional arguments for instrument.exe agent_dll: the basename of the agent DLL to use. executable: the basename of the executable to instrument. optional: if True then this will succeed even if the binary does not exist; otherwise, this will fail if the binary does not exist. Raises: InstrumentationError if instrumentation fails. """ src_file = os.path.join(chrome_dir, executable) if not os.path.isfile(src_file): if not optional: raise InstrumentationError('Missing mandatory "%s".' % executable) _LOGGER.info('Not instrumenting missing optional "%s".', executable) return _LOGGER.info('Instrumenting %s "%s".', "optional" if optional else "mandatory", executable) dst_file = os.path.join(output_dir, executable) # In generating the command-line we place the additional arguments first # so that we can subsequently override those arguments we explicitly set. cmd = [runner._GetExePath('instrument.exe')] cmd += args cmd += ['--input-image=%s' % src_file, '--output-image=%s' % dst_file, '--mode=%s' % mode, '--overwrite'] if agent_dll: cmd.append('--agent=%s' % agent_dll) if mode == 'profile': cmd.append('--no-interior-refs') ret = chrome_utils.Subprocess(cmd) if ret != 0: raise InstrumentationError('Failed to instrument "%s".' % executable)
def _ProcessBBEntries(log_files, output_dir): """Summarize the basic-block entry counts in @p log_files to a JSON file in @p output_dir. The file path to the generated JSON file is returned. """ output_file = os.path.join(output_dir, 'bbentries.json') cmd = [ runner._GetExePath('grinder.exe'), # pylint: disable=W0212 '--mode=bbentry', '--output-file=%s' % output_file, ] cmd.extend(log_files) ret = chrome_utils.Subprocess(cmd) if ret != 0: raise OptimizationError('Failed to process basic-block entries.') return output_file
def _OptimizeChromeBinary(chrome_dir, work_dir, output_dir, log_files, bb_entry_file, basename, optional): """Optimizes a single Chrome binary with the given |basename|. The original binary in |chrome_dir| will be optimized using the data in the provided |log_files|, assuming that they have been generated with respect to the instrumented binary in |work_dir|. If |bb_entry_file| is provided then basic block optimizations will also be applied. Args: chrome_dir: The directory holding the original Chrome binaries. work_dir: The work directory containing the instrumented files. output_dir: The directory where the optimized binaries will be written. log_files: The calltrace log files to be used in generating an order file. bb_entry_file: The BB entry counts to be used in optimizing the binary. basename: The basename of the binary to optimize. optional: If False then the binary must be present and must optimized. Otherwise, this will do nothing and silently return if the input binary does not exist. """ # Calculate all the path parameters. input_image = os.path.join(chrome_dir, basename) if not os.path.isfile(input_image): if not optional: raise OptimizationError('Missing mandatory "%s".' % basename) _LOGGER.info('Not instrumenting missing optional "%s".', basename) return instrumented_image = os.path.join( work_dir, 'calltrace', 'instrumented', basename) order_file = os.path.join(work_dir, basename + '-order.json') output_image = os.path.join(output_dir, basename) # Generate the ordering file for the binary. _LOGGER.info('Generating ordering file for "%s".', basename) cmd = [ runner._GetExePath('reorder.exe'), '--verbose', '--output-stats', '--input-image=%s' % input_image, '--instrumented-image=%s' % instrumented_image, '--output-file=%s' % order_file, ] if bb_entry_file: cmd.append('--basic_block_entry_counts=%s' % bb_entry_file) cmd.extend(log_files) ret = chrome_utils.Subprocess(cmd) if ret != 0: raise OptimizationError( 'Failed to generate an ordering for "%s".' % basename) # Populate output_dir with a copy of the original chrome installation. _LOGGER.info('Copying "%s" to output dir "%s".', chrome_dir, output_dir) if os.path.isfile(output_dir): raise OptimizationError('File present at output dir location: "%s"', output_dir) chrome_utils.CopyChromeFiles(chrome_dir, output_dir) # Replace the binary in output_dir with an optimized version. _LOGGER.info('Optimizing "%s".', basename) cmd = [runner._GetExePath('relink.exe'), '--verbose', '--input-image=%s' % input_image, '--output-image=%s' % output_image, '--order-file=%s' % order_file, '--overwrite'] ret = chrome_utils.Subprocess(cmd) if ret != 0: raise OptimizationError('Failed to relink "%s".' % basename)