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.
4
5import logging
6
7from telemetry.timeline import importer
8from tracing.trace_data import trace_data as trace_data_module
9
10
11class TraceBufferOverflowException(Exception):
12  pass
13
14
15class TabIdImporter(importer.TimelineImporter):
16  def __init__(self, model, trace_data):
17    # Needs to run after all other importers so overflow events have been
18    # created on the model.
19    super(TabIdImporter, self).__init__(
20        model,
21        trace_data,
22        import_order=999)
23    self._trace_data = trace_data
24
25  @staticmethod
26  def GetSupportedPart():
27    return trace_data_module.TAB_ID_PART
28
29  def ImportEvents(self):
30    pass
31
32  def FinalizeImport(self):
33    self._CheckTraceBufferOverflow()
34    self._CreateTabIdsToThreadsMap()
35
36  def _CheckTraceBufferOverflow(self):
37    # Since _CreateTabIdsToThreadsMap() relies on markers output on timeline
38    # tracing data, it may not work in case we have trace events dropped due to
39    # trace buffer overflow.
40    for process in self._model.GetAllProcesses():
41      if process.trace_buffer_did_overflow:
42        raise TraceBufferOverflowException(
43            'Trace buffer of process with pid=%d overflowed at timestamp %d. '
44            'Raw trace data:\n%s' %
45            (process.pid, process.trace_buffer_overflow_event.start,
46             repr(self._trace_data)))
47
48  def _CreateTabIdsToThreadsMap(self):
49    tab_id_events = []
50    for tab_ids in self._trace_data.GetTracesFor(trace_data_module.TAB_ID_PART):
51      tab_id_events.extend(tab_ids)
52
53    for tab_id in tab_id_events:
54      try:
55        timeline_markers = self._model.FindTimelineMarkers(tab_id)
56      # If timeline_markers with name equals |tab_id| can't be found, it's
57      # non-fatal.
58      except Exception:
59        logging.warning('Cannot find timeline marker for tab with id=%s' %
60                        tab_id)
61        continue
62      assert len(timeline_markers) == 1
63      assert timeline_markers[0].start_thread == timeline_markers[0].end_thread
64      self._model.AddMappingFromTabIdToRendererThread(
65          tab_id, timeline_markers[0].start_thread)
66