aboutsummaryrefslogtreecommitdiff
path: root/catapult/telemetry/telemetry/timeline/tab_id_importer.py
blob: e0d85dd135a414da0e2582b0e9b987f6b7520e8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import logging

from telemetry.timeline import importer
from telemetry.timeline import trace_data as trace_data_module

class TraceBufferOverflowException(Exception):
  pass


class TabIdImporter(importer.TimelineImporter):
  def __init__(self, model, trace_data):
    # Needs to run after all other importers so overflow events have been
    # created on the model.
    super(TabIdImporter, self).__init__(
        model,
        trace_data,
        import_order=999)
    self._trace_data = trace_data

  @staticmethod
  def GetSupportedPart():
    return trace_data_module.TAB_ID_PART

  def ImportEvents(self):
    pass

  def FinalizeImport(self):
    self._CheckTraceBufferOverflow()
    self._CreateTabIdsToThreadsMap()

  def _CheckTraceBufferOverflow(self):
    # Since _CreateTabIdsToThreadsMap() relies on markers output on timeline
    # tracing data, it may not work in case we have trace events dropped due to
    # trace buffer overflow.
    for process in self._model.GetAllProcesses():
      if process.trace_buffer_did_overflow:
        raise TraceBufferOverflowException(
            'Trace buffer of process with pid=%d overflowed at timestamp %d. '
            'Raw trace data:\n%s' %
            (process.pid, process.trace_buffer_overflow_event.start,
             repr(self._trace_data)))

  def _CreateTabIdsToThreadsMap(self):
    tab_id_events = []
    for tab_ids in self._trace_data.GetTracesFor(trace_data_module.TAB_ID_PART):
      tab_id_events.extend(tab_ids)

    for tab_id in tab_id_events:
      try:
        timeline_markers = self._model.FindTimelineMarkers(tab_id)
      # If timeline_markers with name equals |tab_id| can't be found, it's
      # non-fatal.
      except Exception:
        logging.warning('Cannot find timeline marker for tab with id=%s' %
                        tab_id)
        continue
      assert len(timeline_markers) == 1
      assert timeline_markers[0].start_thread == timeline_markers[0].end_thread
      self._model.AddMappingFromTabIdToRendererThread(
          tab_id, timeline_markers[0].start_thread)