1// Copyright (c) 2013 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 AsyncSliceGroup class.
9 */
10base.require('base.range');
11base.require('tracing.trace_model.async_slice');
12
13base.exportTo('tracing.trace_model', function() {
14  /**
15   * A group of AsyncSlices.
16   * @constructor
17   */
18  function AsyncSliceGroup() {
19    this.slices = [];
20    this.bounds = new base.Range();
21  }
22
23  AsyncSliceGroup.prototype = {
24    __proto__: Object.prototype,
25
26    /**
27     * Helper function that pushes the provided slice onto the slices array.
28     */
29    push: function(slice) {
30      this.slices.push(slice);
31    },
32
33    /**
34     * @return {Number} The number of slices in this group.
35     */
36    get length() {
37      return this.slices.length;
38    },
39
40    /**
41     * Shifts all the timestamps inside this group forward by the amount
42     * specified.
43     */
44    shiftTimestampsForward: function(amount) {
45      for (var sI = 0; sI < this.slices.length; sI++) {
46        var slice = this.slices[sI];
47        slice.start = (slice.start + amount);
48        for (var sJ = 0; sJ < slice.subSlices.length; sJ++)
49          slice.subSlices[sJ].start += amount;
50      }
51    },
52
53    /**
54     * Updates the bounds for this group based on the slices it contains.
55     */
56    updateBounds: function() {
57      this.bounds.reset();
58      for (var i = 0; i < this.slices.length; i++) {
59        this.bounds.addValue(this.slices[i].start);
60        this.bounds.addValue(this.slices[i].end);
61      }
62    },
63
64    /**
65     * Breaks up this group into slices based on start thread.
66     *
67     * @return {Array} An array of AsyncSliceGroups where each group has
68     * slices that started on the same thread.
69     */
70    computeSubGroups: function() {
71      var subGroupsByGUID = {};
72      for (var i = 0; i < this.slices.length; ++i) {
73        var slice = this.slices[i];
74        var sliceGUID = slice.startThread.guid;
75        if (!subGroupsByGUID[sliceGUID])
76          subGroupsByGUID[sliceGUID] = new AsyncSliceGroup();
77        subGroupsByGUID[sliceGUID].slices.push(slice);
78      }
79      var groups = [];
80      for (var guid in subGroupsByGUID) {
81        var group = subGroupsByGUID[guid];
82        group.updateBounds();
83        groups.push(group);
84      }
85      return groups;
86    }
87  };
88
89  return {
90    AsyncSliceGroup: AsyncSliceGroup
91  };
92});
93