1<!DOCTYPE html>
2<!--
3Copyright (c) 2013 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/tracing/base/extension_registry.html">
9<link rel="import" href="/tracing/model/timed_event.html">
10<link rel="import" href="/tracing/value/unit.html">
11
12<script>
13'use strict';
14
15/**
16 * @fileoverview Provides the AsyncSlice class.
17 */
18tr.exportTo('tr.model', function() {
19  /**
20   * A AsyncSlice represents an interval of time during which an
21   * asynchronous operation is in progress. An AsyncSlice consumes no CPU time
22   * itself and so is only associated with Threads at its start and end point.
23   *
24   * @constructor
25   */
26  function AsyncSlice(category, title, colorId, start, args, duration,
27                      opt_isTopLevel, opt_cpuStart, opt_cpuDuration,
28                      opt_argsStripped) {
29    tr.model.TimedEvent.call(this, start);
30
31    this.category = category || '';
32    // We keep the original title from the trace file in originalTitle since
33    // some sub-classes, e.g. NetAsyncSlice, change the title field.
34    this.originalTitle = title;
35    this.title = title;
36    this.colorId = colorId;
37    this.args = args;
38    this.startStackFrame = undefined;
39    this.endStackFrame = undefined;
40    this.didNotFinish = false;
41    this.important = false;
42    this.subSlices = [];
43    this.parentContainer_ = undefined;
44
45    this.id = undefined;
46    this.startThread = undefined;
47    this.endThread = undefined;
48    this.cpuStart = undefined;
49    this.cpuDuration = undefined;
50    this.argsStripped = false;
51
52    this.startStackFrame = undefined;
53    this.endStackFrame = undefined;
54
55    this.duration = duration;
56
57
58    // TODO(nduca): Forgive me for what I must do.
59    this.isTopLevel = (opt_isTopLevel === true);
60
61    if (opt_cpuStart !== undefined)
62      this.cpuStart = opt_cpuStart;
63
64    if (opt_cpuDuration !== undefined)
65      this.cpuDuration = opt_cpuDuration;
66
67    if (opt_argsStripped !== undefined)
68      this.argsStripped = opt_argsStripped;
69  };
70
71  AsyncSlice.prototype = {
72    __proto__: tr.model.TimedEvent.prototype,
73
74    get analysisTypeName() {
75      return this.title;
76    },
77
78    get parentContainer() {
79      return this.parentContainer_;
80    },
81
82    set parentContainer(parentContainer) {
83      this.parentContainer_ = parentContainer;
84      for (var i = 0; i < this.subSlices.length; i++) {
85        var subSlice = this.subSlices[i];
86        if (subSlice.parentContainer === undefined)
87          subSlice.parentContainer = parentContainer;
88      }
89    },
90
91    get viewSubGroupTitle() {
92      return this.title;
93    },
94
95    get userFriendlyName() {
96      return 'Async slice ' + this.title + ' at ' +
97          tr.v.Unit.byName.timeStampInMs.format(this.start);
98    },
99
100    get stableId() {
101      var parentAsyncSliceGroup = this.parentContainer.asyncSliceGroup;
102      return parentAsyncSliceGroup.stableId + '.' +
103          parentAsyncSliceGroup.slices.indexOf(this);
104    },
105
106    findDescendentSlice: function(targetTitle) {
107      if (!this.subSlices)
108        return undefined;
109
110      for (var i = 0; i < this.subSlices.length; i++) {
111        if (this.subSlices[i].title == targetTitle)
112          return this.subSlices[i];
113        var slice = this.subSlices[i].findDescendentSlice(targetTitle);
114        if (slice) return slice;
115      }
116      return undefined;
117    },
118
119    iterateAllDescendents: function(callback, opt_this) {
120      this.subSlices.forEach(callback, opt_this);
121      this.subSlices.forEach(function(subSlice) {
122        subSlice.iterateAllDescendents(callback, opt_this);
123      }, opt_this);
124    },
125
126    compareTo: function(that) {
127      return this.title.localeCompare(that.title);
128    }
129  };
130
131  tr.model.EventRegistry.register(
132      AsyncSlice,
133      {
134        name: 'asyncSlice',
135        pluralName: 'asyncSlices',
136        singleViewElementName: 'tr-ui-a-single-async-slice-sub-view',
137        multiViewElementName: 'tr-ui-a-multi-async-slice-sub-view'
138      });
139
140
141  var options = new tr.b.ExtensionRegistryOptions(
142      tr.b.TYPE_BASED_REGISTRY_MODE);
143  options.mandatoryBaseClass = AsyncSlice;
144  options.defaultConstructor = AsyncSlice;
145  tr.b.decorateExtensionRegistry(AsyncSlice, options);
146
147  return {
148    AsyncSlice: AsyncSlice
149  };
150});
151</script>
152