- add sources.
[platform/framework/web/crosswalk.git] / src / tools / telemetry / telemetry / core / platform / profiler / __init__.py
1 # Copyright (c) 2013 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 import collections
6
7 class Profiler(object):
8   """A sampling profiler provided by the platform.
9
10   A profiler is started on its constructor, and should
11   gather data until CollectProfile().
12   The life cycle is normally tied to a single page,
13   i.e., multiple profilers will be created for a page set.
14   WillCloseBrowser() is called right before the browser
15   is closed to allow any further cleanup.
16   """
17
18   def __init__(self, browser_backend, platform_backend, output_path, state):
19     self._browser_backend = browser_backend
20     self._platform_backend = platform_backend
21     self._output_path = output_path
22     self._state = state
23
24   @classmethod
25   def name(cls):
26     """User-friendly name of this profiler."""
27     raise NotImplementedError()
28
29   @classmethod
30   def is_supported(cls, browser_type):
31     """True iff this profiler is currently supported by the platform."""
32     raise NotImplementedError()
33
34   @classmethod
35   def CustomizeBrowserOptions(cls, browser_type, options):
36     """Override to customize the Browser's options before it is created."""
37     pass
38
39   @classmethod
40   def WillCloseBrowser(cls, browser_backend, platform_backend):
41     """Called before the browser is stopped."""
42     pass
43
44   def _GetProcessOutputFileMap(self):
45     """Returns a dict with pid: output_file."""
46     all_pids = ([self._browser_backend.pid] +
47                 self._platform_backend.GetChildPids(self._browser_backend.pid))
48
49     process_name_counts = collections.defaultdict(int)
50     process_output_file_map = {}
51     for pid in all_pids:
52       cmd_line = self._platform_backend.GetCommandLine(pid)
53       process_name = self._browser_backend.GetProcessName(cmd_line)
54       output_file = '%s.%s%s' % (self._output_path, process_name,
55                                  process_name_counts[process_name])
56       process_name_counts[process_name] += 1
57       process_output_file_map[pid] = output_file
58     return process_output_file_map
59
60   def CollectProfile(self):
61     """Collect the profile from the profiler."""
62     raise NotImplementedError()