1 <!DOCTYPE html> 2 <!-- 3 Copyright (c) 2015 The Chromium Authors. All rights reserved. 4 Use of this source code is governed by a BSD-style license that can be 5 found in the LICENSE file. 6 --> 7 8 <link rel="import" href="/tracing/base/iteration_helpers.html"> 9 <link rel="import" href="/tracing/model/helpers/chrome_model_helper.html"> 10 11 <script> 12 'use strict'; 13 14 tr.exportTo('pi.m', function() { 15 function ThreadGrouping() { 16 this.groupNameForThreadGUID_ = {}; 17 } 18 19 ThreadGrouping.prototype = { 20 autoInitUsingHelpers: function(model) { 21 // Everything is 'other' by default. 22 model.getAllThreads().forEach(function(thread) { 23 this.groupNameForThreadGUID_[thread.guid] = 'Other'; 24 }, this); 25 var chromeHelper = model.getOrCreateHelper( 26 tr.model.helpers.ChromeModelHelper); 27 28 if (chromeHelper) { 29 var browserHelper = chromeHelper.browserHelper; 30 this.addThreadsInProcessToGroup_(browserHelper.process, 'Browser'); 31 32 var gpuHelper = chromeHelper.gpuHelper; 33 if (gpuHelper) { 34 this.addThreadsInProcessToGroup_(gpuHelper.process, 'GPU'); 35 } 36 37 for (var pid in chromeHelper.rendererHelpers) { 38 var rendererHelper = chromeHelper.rendererHelpers[pid]; 39 this.addThreadsInProcessToGroup_(rendererHelper.process, 'Renderer'); 40 } 41 42 // TODO(nduca): Modify the helpers to detect plugin processes 43 // and include those in separate processes. 44 } 45 46 // It would be very easy to add processes recognized by android helper 47 // here, too. 48 }, 49 50 getGroupNameForThread: function(thread) { 51 if (!(thread instanceof tr.model.EventContainer)) 52 return 'error'; 53 54 var groupName = this.groupNameForThreadGUID_[thread.guid]; 55 if (groupName === undefined) 56 return 'Other'; 57 return groupName; 58 }, 59 60 getGroupNameForEvent: function(event) { 61 var parentContainer = event.parentContainer; 62 if (parentContainer === undefined) 63 return 'Unknown'; 64 return this.getGroupNameForThread(parentContainer); 65 }, 66 67 addThreadsInProcessToGroup_: function(process, groupName, 68 opt_predicate, opt_this) { 69 var predicate = opt_predicate || tr.b.identity; 70 71 for (var tid in process.threads) { 72 var thread = process.threads[tid]; 73 if (predicate.call(opt_this, thread)) 74 this.groupNameForThreadGUID_[thread.guid] = groupName; 75 } 76 }, 77 78 divideEventSetIntoSubGroups: function(eventSet) { 79 var resultingEventSets = { 80 other: new tr.model.EventSet() 81 }; 82 tr.b.iterItems(this.groupNameForThreadGUID_, function(guid, groupName) { 83 if (resultingEventSets[groupName] !== undefined) 84 return; 85 resultingEventSets[groupName] = new tr.model.EventSet(); 86 }); 87 88 eventSet.forEach(function(event) { 89 var parentContainer = event.parentContainer; 90 if (parentContainer === undefined) 91 return; 92 93 if (!(parentContainer instanceof tr.model.EventContainer)) 94 return; 95 96 var groupName = this.groupNameForThreadGUID_[parentContainer.guid]; 97 if (groupName === undefined) 98 groupName = 'Other'; 99 resultingEventSets[groupName].push(event); 100 101 }, this); 102 103 return resultingEventSets; 104 } 105 }; 106 107 return { 108 ThreadGrouping: ThreadGrouping 109 }; 110 }); 111 </script> 112