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