summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/model/user_model/user_expectation.html
blob: 43e8888740ca662514c1b34f004198c73009d56f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html>
<!--
Copyright (c) 2015 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/base/range_utils.html">
<link rel="import" href="/tracing/base/statistics.html">
<link rel="import" href="/tracing/model/compound_event_selection_state.html">
<link rel="import" href="/tracing/model/event_set.html">
<link rel="import" href="/tracing/model/timed_event.html">
<link rel="import" href="/tracing/value/unit.html">

<script>
'use strict';

tr.exportTo('tr.model.um', function() {
  var CompoundEventSelectionState = tr.model.CompoundEventSelectionState;

  function UserExpectation(parentModel, initiatorTitle, start, duration) {
    tr.model.TimedEvent.call(this, start);
    this.associatedEvents = new tr.model.EventSet();
    this.duration = duration;
    this.initiatorTitle_ = initiatorTitle;
    this.parentModel = parentModel;
    this.typeInfo_ = undefined;

    // sourceEvents are the ones that caused the UserModelBuilder to create this
    // UserExpectation.
    this.sourceEvents = new tr.model.EventSet();
  }

  UserExpectation.prototype = {
    __proto__: tr.model.TimedEvent.prototype,

    computeCompoundEvenSelectionState: function(selection) {
      var cess = CompoundEventSelectionState.NOT_SELECTED;
      if (selection.contains(this))
        cess |= CompoundEventSelectionState.EVENT_SELECTED;

      if (this.associatedEvents.intersectionIsEmpty(selection))
        return cess;

      var allContained = this.associatedEvents.every(function(event) {
        return selection.contains(event);
      });

      if (allContained)
        cess |= CompoundEventSelectionState.ALL_ASSOCIATED_EVENTS_SELECTED;
      else
        cess |= CompoundEventSelectionState.SOME_ASSOCIATED_EVENTS_SELECTED;
      return cess;
    },

    // Returns samples which are overlapping with V8.Execute
    get associatedSamples() {
      var samples = new tr.model.EventSet();
      this.associatedEvents.forEach(function(event) {
        if (event instanceof tr.model.ThreadSlice)
          samples.addEventSet(event.overlappingSamples);
      });
      return samples;
    },

    get userFriendlyName() {
      return this.title + ' User Expectation at ' +
          tr.v.Unit.byName.timeStampInMs.format(this.start);
    },

    get stableId() {
      return ('UserExpectation.' + this.guid);
    },

    get typeInfo() {
      if (!this.typeInfo_)
        this.typeInfo_ = UserExpectation.findTypeInfo(this.constructor);

      // If you set Subclass.prototype = {}, then you must explicitly specify
      // constructor in that prototype object!
      // http://javascript.info/tutorial/constructor

      if (!this.typeInfo_)
        throw new Error('Unregistered UserExpectation');

      return this.typeInfo_;
    },

    get colorId() {
      return this.typeInfo.metadata.colorId;
    },

    get stageTitle() {
      return this.typeInfo.metadata.stageTitle;
    },

    get initiatorTitle() {
      return this.initiatorTitle_;
    },

    get title() {
      if (!this.initiatorTitle)
        return this.stageTitle;

      return this.initiatorTitle + ' ' + this.stageTitle;
    },

    /**
     * Returns the sum of the number of CPU ms spent by this UserExpectation.
     */
    get totalCpuMs() {
      var cpuMs = 0;
      this.associatedEvents.forEach(function(event) {
        if (event.cpuSelfTime)
          cpuMs += event.cpuSelfTime;
      });
      return cpuMs;
    }
  };

  var options = new tr.b.ExtensionRegistryOptions(tr.b.BASIC_REGISTRY_MODE);
  tr.b.decorateExtensionRegistry(UserExpectation, options);

  UserExpectation.addEventListener('will-register', function(e) {
    var metadata = e.typeInfo.metadata;

    if (metadata.stageTitle === undefined) {
      throw new Error('Registered UserExpectations must provide ' +
          'stageTitle');
    }

    if (metadata.colorId === undefined) {
      throw new Error('Registered UserExpectations must provide ' +
          'colorId');
    }
  });

  tr.model.EventRegistry.register(
      UserExpectation,
      {
        name: 'user-expectation',
        pluralName: 'user-expectations',
        singleViewElementName: 'tr-ui-a-single-user-expectation-sub-view',
        multiViewElementName: 'tr-ui-a-multi-user-expectation-sub-view'
      });

  return {
    UserExpectation: UserExpectation
  };
});
</script>