1# Copyright 2014 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. 4from telemetry.core.platform.profiler import profiler_finder 5 6 7class ProfilingControllerBackend(object): 8 def __init__(self, platform_backend): 9 self._platform_backend = platform_backend 10 self._active_profilers = [] 11 self._profilers_states = {} 12 13 def Start(self, profiler_name, base_output_file): 14 """Starts profiling using |profiler_name|. Results are saved to 15 |base_output_file|.<process_name>.""" 16 assert not self._active_profilers, 'Already profiling. Must stop first.' 17 18 browser_backend = None 19 # Note that many profilers doesn't rely on browser_backends, so the number 20 # of running_browser_backends may not matter for them. 21 if len(self._platform_backend.running_browser_backends) == 1: 22 browser_backend = self._platform_backend.running_browser_backends[0] 23 else: 24 raise NotImplementedError() 25 26 profiler_class = profiler_finder.FindProfiler(profiler_name) 27 28 if not profiler_class.is_supported(browser_backend.browser_type): 29 raise Exception('The %s profiler is not ' 30 'supported on this platform.' % profiler_name) 31 32 if not profiler_class in self._profilers_states: 33 self._profilers_states[profiler_class] = {} 34 35 self._active_profilers.append( 36 profiler_class(browser_backend, self._platform_backend, 37 base_output_file, self._profilers_states[profiler_class])) 38 39 def Stop(self): 40 """Stops all active profilers and saves their results. 41 42 Returns: 43 A list of filenames produced by the profiler. 44 """ 45 output_files = [] 46 for profiler in self._active_profilers: 47 output_files.extend(profiler.CollectProfile()) 48 self._active_profilers = [] 49 return output_files 50 51 def WillCloseBrowser(self, browser_backend): 52 for profiler_class in self._profilers_states: 53 profiler_class.WillCloseBrowser(browser_backend, self._platform_backend) 54