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'''Imports event data obtained from the inspector's timeline.''' 5 6from telemetry.timeline import importer 7from telemetry.timeline import inspector_timeline_data 8import telemetry.timeline.thread as timeline_thread 9import telemetry.timeline.slice as tracing_slice 10 11class InspectorTimelineImporter(importer.TimelineImporter): 12 def __init__(self, model, timeline_data): 13 super(InspectorTimelineImporter, self).__init__(model, timeline_data) 14 15 @staticmethod 16 def CanImport(timeline_data): 17 ''' Checks if timeline_data is from the inspector timeline. We assume 18 that if the first event is a valid inspector event, we can import the 19 entire list. 20 ''' 21 if not isinstance(timeline_data, 22 inspector_timeline_data.InspectorTimelineData): 23 return False 24 25 event_data = timeline_data.EventData() 26 27 if isinstance(event_data, list) and len(event_data): 28 event_datum = event_data[0] 29 return 'startTime' in event_datum and 'type' in event_datum 30 return False 31 32 def ImportEvents(self): 33 render_process = self._model.GetOrCreateProcess(0) 34 for raw_event in self._timeline_data.EventData(): 35 thread = render_process.GetOrCreateThread(raw_event.get('thread', 0)) 36 InspectorTimelineImporter.AddRawEventToThreadRecursive(thread, raw_event) 37 38 def FinalizeImport(self): 39 pass 40 41 @staticmethod 42 def AddRawEventToThreadRecursive(thread, raw_inspector_event): 43 pending_slice = None 44 if ('startTime' in raw_inspector_event and 45 'type' in raw_inspector_event): 46 args = {} 47 for x in raw_inspector_event: 48 if x in ('startTime', 'endTime', 'children'): 49 continue 50 args[x] = raw_inspector_event[x] 51 if len(args) == 0: 52 args = None 53 start_time = raw_inspector_event['startTime'] 54 end_time = raw_inspector_event.get('endTime', start_time) 55 56 pending_slice = tracing_slice.Slice( 57 thread, 'inspector', 58 raw_inspector_event['type'], 59 start_time, 60 thread_timestamp=None, 61 args=args) 62 63 for child in raw_inspector_event.get('children', []): 64 InspectorTimelineImporter.AddRawEventToThreadRecursive( 65 thread, child) 66 67 if pending_slice: 68 pending_slice.duration = end_time - pending_slice.start 69 thread.PushSlice(pending_slice) 70 71 @staticmethod 72 def RawEventToTimelineEvent(raw_inspector_event): 73 """Converts raw_inspector_event to TimelineEvent.""" 74 thread = timeline_thread.Thread(None, 0) 75 InspectorTimelineImporter.AddRawEventToThreadRecursive( 76 thread, raw_inspector_event) 77 thread.FinalizeImport() 78 assert len(thread.toplevel_slices) <= 1 79 if len(thread.toplevel_slices) == 0: 80 return None 81 return thread.toplevel_slices[0] 82