1 // Copyright (c) 2012 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 5 'use strict'; 6 7 /** 8 * @fileoverview Provides the ProcessBase class. 9 */ 10 base.require('range'); 11 base.require('guid'); 12 base.require('model.thread'); 13 base.require('model.counter'); 14 base.exportTo('tracing.model', function() { 15 16 var Thread = tracing.model.Thread; 17 var Counter = tracing.model.Counter; 18 19 /** 20 * The ProcessBase is an partial base class, upon which Kernel 21 * and Process are built. 22 * 23 * @constructor 24 */ 25 function ProcessBase() { 26 this.guid_ = tracing.GUID.allocate(); 27 this.threads = {}; 28 this.counters = {}; 29 this.bounds = new base.Range(); 30 }; 31 32 ProcessBase.prototype = { 33 /* 34 * @return {Number} A globally unique identifier for this counter. 35 */ 36 get guid() { 37 return this.guid_; 38 }, 39 40 /** 41 * Gets the number of threads in this process. 42 */ 43 get numThreads() { 44 var n = 0; 45 for (var p in this.threads) { 46 n++; 47 } 48 return n; 49 }, 50 51 /** 52 * Shifts all the timestamps inside this process forward by the amount 53 * specified. 54 */ 55 shiftTimestampsForward: function(amount) { 56 for (var tid in this.threads) 57 this.threads[tid].shiftTimestampsForward(amount); 58 for (var id in this.counters) 59 this.counters[id].shiftTimestampsForward(amount); 60 }, 61 62 /** 63 * Closes any open slices. 64 */ 65 autoCloseOpenSlices: function(opt_maxTimestamp) { 66 for (var tid in this.threads) { 67 var thread = this.threads[tid]; 68 thread.autoCloseOpenSlices(opt_maxTimestamp); 69 } 70 }, 71 72 /** 73 * Merge slices from the kernel with those from userland for each thread. 74 */ 75 mergeKernelWithUserland: function() { 76 for (var tid in this.threads) { 77 var thread = this.threads[tid]; 78 thread.mergeKernelWithUserland(); 79 } 80 }, 81 82 updateBounds: function() { 83 this.bounds.reset(); 84 for (var tid in this.threads) { 85 this.threads[tid].updateBounds(); 86 this.bounds.addRange(this.threads[tid].bounds); 87 } 88 for (var id in this.counters) { 89 this.counters[id].updateBounds(); 90 this.bounds.addRange(this.counters[id].bounds); 91 } 92 }, 93 94 addCategoriesToDict: function(categoriesDict) { 95 for (var tid in this.threads) 96 this.threads[tid].addCategoriesToDict(categoriesDict); 97 for (var id in this.counters) 98 categoriesDict[this.counters[id].category] = true; 99 }, 100 101 /** 102 * @param {String} The name of the thread to find. 103 * @return {Array} An array of all the matched threads. 104 */ 105 findAllThreadsNamed: function(name) { 106 var namedThreads = []; 107 for (var tid in this.threads) { 108 var thread = this.threads[tid]; 109 if (thread.name == name) 110 namedThreads.push(thread); 111 } 112 return namedThreads; 113 }, 114 115 /** 116 * Removes threads from the process that are fully empty. 117 */ 118 pruneEmptyContainers: function() { 119 var threadsToKeep = {}; 120 for (var tid in this.threads) { 121 var thread = this.threads[tid]; 122 if (!thread.isEmpty) 123 threadsToKeep[tid] = thread; 124 } 125 this.threads = threadsToKeep; 126 }, 127 128 /** 129 * @return {TimlineThread} The thread identified by tid on this process, 130 * creating it if it doesn't exist. 131 */ 132 getOrCreateThread: function(tid) { 133 if (!this.threads[tid]) 134 this.threads[tid] = new Thread(this, tid); 135 return this.threads[tid]; 136 }, 137 138 /** 139 * @return {TimlineCounter} The counter on this process named 'name', 140 * creating it if it doesn't exist. 141 */ 142 getOrCreateCounter: function(cat, name) { 143 var id = cat + '.' + name; 144 if (!this.counters[id]) 145 this.counters[id] = new Counter(this, id, cat, name); 146 return this.counters[id]; 147 } 148 }; 149 150 return { 151 ProcessBase: ProcessBase 152 }; 153 }); 154