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.
8 * @fileoverview Provides the Thread class.
10 tvcm.require('tvcm.guid');
11 tvcm.require('tvcm.range');
12 tvcm.require('tracing.trace_model.slice');
13 tvcm.require('tracing.trace_model.slice_group');
14 tvcm.require('tracing.trace_model.async_slice_group');
15 tvcm.require('tracing.trace_model.sample');
17 tvcm.exportTo('tracing.trace_model', function() {
19 var Slice = tracing.trace_model.Slice;
20 var SliceGroup = tracing.trace_model.SliceGroup;
21 var AsyncSlice = tracing.trace_model.AsyncSlice;
22 var AsyncSliceGroup = tracing.trace_model.AsyncSliceGroup;
25 * A ThreadSlice represents an interval of time on a thread resource
26 * with associated nestinged slice information.
28 * ThreadSlices are typically associated with a specific trace event pair on a
31 * TRACE_EVENT_BEGIN1("x","myArg", 7) at time=0.1ms
32 * TRACE_EVENT_END0() at time=0.3ms
33 * This results in a single slice from 0.1 with duration 0.2 on a
38 function ThreadSlice(cat, title, colorId, start, args, opt_duration,
39 opt_threadStart, opt_threadDuration) {
40 Slice.call(this, cat, title, colorId, start, args, opt_duration,
41 opt_threadStart, opt_threadDuration);
42 // Do not modify this directly.
43 // subSlices is configured by SliceGroup.rebuildSubRows_.
47 ThreadSlice.prototype = {
48 __proto__: Slice.prototype
52 * A Thread stores all the trace events collected for a particular
53 * thread. We organize the synchronous slices on a thread by "subrows," where
54 * subrow 0 has all the root slices, subrow 1 those nested 1 deep, and so on.
55 * The asynchronous slices are stored in an AsyncSliceGroup object.
57 * The slices stored on a Thread should be instances of
62 function Thread(parent, tid) {
63 this.guid_ = tvcm.GUID.allocate();
65 throw new Error('Parent must be provided.');
69 this.sliceGroup = new SliceGroup(ThreadSlice);
70 this.timeSlices = undefined;
72 this.kernelSliceGroup = new SliceGroup();
73 this.asyncSliceGroup = new AsyncSliceGroup();
74 this.bounds = new tvcm.Range();
75 this.ephemeralSettings = {};
81 * @return {Number} A globally unique identifier for this counter.
87 compareTo: function(that) {
88 return Thread.compare(this, that);
92 var obj = new Object();
93 var keys = Object.keys(this);
94 for (var i = 0; i < keys.length; i++) {
96 if (typeof this[key] == 'function')
98 if (key == 'parent') {
99 obj[key] = this[key].guid;
102 obj[key] = this[key];
108 * Adds a new sample in the thread's samples.
110 * Calls to addSample must be made with non-monotonically-decreasing
113 * @param {String} category Category of the sample to add.
114 * @param {String} title Title of the sample to add.
115 * @param {Number} ts The timetsamp of the sample, in milliseconds.
116 * @param {Object.<string, Object>=} opt_args Arguments associated with
119 addSample: function(category, title, ts, opt_args) {
120 if (this.samples_.length) {
121 var lastSample = this.samples_[this.samples_.length - 1];
122 if (ts < lastSample.start) {
124 Error('Samples must be added in increasing timestamp order.');
127 var colorId = tvcm.ui.getStringColorId(title);
128 var sample = new tracing.trace_model.Sample(category, title, colorId, ts,
129 opt_args ? opt_args : {});
130 this.samples_.push(sample);
135 * Returns the array of samples added to this thread. If no samples
136 * have been added, an empty array is returned.
138 * @return {Array<Sample>} array of samples.
141 return this.samples_;
145 * Name of the thread, if present.
150 * Shifts all the timestamps inside this thread forward by the amount
153 shiftTimestampsForward: function(amount) {
154 this.sliceGroup.shiftTimestampsForward(amount);
156 if (this.timeSlices) {
157 for (var i = 0; i < this.timeSlices.length; i++) {
158 var slice = this.timeSlices[i];
159 slice.start += amount;
163 if (this.samples_.length) {
164 for (var i = 0; i < this.samples_.length; i++) {
165 var sample = this.samples_[i];
166 sample.start += amount;
170 this.kernelSliceGroup.shiftTimestampsForward(amount);
171 this.asyncSliceGroup.shiftTimestampsForward(amount);
175 * Determines whether this thread is empty. If true, it usually implies
176 * that it should be pruned from the model.
179 if (this.sliceGroup.length)
181 if (this.sliceGroup.openSliceCount)
183 if (this.timeSlices && this.timeSlices.length)
185 if (this.kernelSliceGroup.length)
187 if (this.asyncSliceGroup.length)
189 if (this.samples_.length)
195 * Updates the bounds based on the
196 * current objects associated with the thread.
198 updateBounds: function() {
201 this.sliceGroup.updateBounds();
202 this.bounds.addRange(this.sliceGroup.bounds);
204 this.kernelSliceGroup.updateBounds();
205 this.bounds.addRange(this.kernelSliceGroup.bounds);
207 this.asyncSliceGroup.updateBounds();
208 this.bounds.addRange(this.asyncSliceGroup.bounds);
210 if (this.timeSlices && this.timeSlices.length) {
211 this.bounds.addValue(this.timeSlices[0].start);
212 this.bounds.addValue(
213 this.timeSlices[this.timeSlices.length - 1].end);
215 if (this.samples_.length) {
216 this.bounds.addValue(this.samples_[0].start);
217 this.bounds.addValue(
218 this.samples_[this.samples_.length - 1].end);
222 addCategoriesToDict: function(categoriesDict) {
223 for (var i = 0; i < this.sliceGroup.length; i++)
224 categoriesDict[this.sliceGroup.slices[i].category] = true;
225 for (var i = 0; i < this.kernelSliceGroup.length; i++)
226 categoriesDict[this.kernelSliceGroup.slices[i].category] = true;
227 for (var i = 0; i < this.asyncSliceGroup.length; i++)
228 categoriesDict[this.asyncSliceGroup.slices[i].category] = true;
229 for (var i = 0; i < this.samples_.length; i++)
230 categoriesDict[this.samples_[i].category] = true;
233 autoCloseOpenSlices: function(opt_maxTimestamp) {
234 this.sliceGroup.autoCloseOpenSlices(opt_maxTimestamp);
235 this.kernelSliceGroup.autoCloseOpenSlices(opt_maxTimestamp);
238 mergeKernelWithUserland: function() {
239 if (this.kernelSliceGroup.length > 0) {
240 var newSlices = SliceGroup.merge(
241 this.sliceGroup, this.kernelSliceGroup);
242 this.sliceGroup.slices = newSlices.slices;
243 this.kernelSliceGroup = new SliceGroup();
248 createSubSlices: function() {
249 this.sliceGroup.createSubSlices();
253 * @return {String} A user-friendly name for this thread.
255 get userFriendlyName() {
256 return this.name || this.tid;
260 * @return {String} User friendly details about this thread.
262 get userFriendlyDetails() {
263 return 'tid: ' + this.tid +
264 (this.name ? ', name: ' + this.name : '');
267 getSettingsKey: function() {
270 var parentKey = this.parent.getSettingsKey();
273 return parentKey + '.' + this.name;
277 * Returns the index of the slice in the timeSlices array, or undefined.
279 indexOfTimeSlice: function(timeSlice) {
280 var i = tvcm.findLowIndexInSortedArray(
282 function(slice) { return slice.start; },
284 if (this.timeSlices[i] !== timeSlice)
289 iterateAllEvents: function(callback) {
290 this.sliceGroup.iterateAllEvents(callback);
291 this.kernelSliceGroup.iterateAllEvents(callback);
292 this.asyncSliceGroup.iterateAllEvents(callback);
294 if (this.timeSlices && this.timeSlices.length)
295 this.timeSlices.forEach(callback);
297 this.samples_.forEach(callback);
302 * Comparison between threads that orders first by parent.compareTo,
303 * then by names, then by tid.
305 Thread.compare = function(x, y) {
306 var tmp = x.parent.compareTo(y.parent);
310 tmp = x.sortIndex - y.sortIndex;
314 tmp = tvcm.comparePossiblyUndefinedValues(
316 function(x, y) { return x.localeCompare(y); });
320 return x.tid - y.tid;
324 ThreadSlice: ThreadSlice,