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/core/filter.html">
9<link rel="import" href="/tracing/model/event_set.html">
10<link rel="import" href="/tracing/model/model.html">
11<link rel="import" href="/tracing/ui/base/ui.html">
12<link rel="import" href="/tracing/ui/tracks/container_track.html">
13<link rel="import" href="/tracing/ui/tracks/slice_track.html">
14
15<script>
16'use strict';
17
18tr.exportTo('tr.ui.tracks', function() {
19
20  /**
21   * Visualizes a Cpu using a series of SliceTracks.
22   * @constructor
23   */
24  var CpuTrack =
25      tr.ui.b.define('cpu-track', tr.ui.tracks.ContainerTrack);
26  CpuTrack.prototype = {
27    __proto__: tr.ui.tracks.ContainerTrack.prototype,
28
29    decorate: function(viewport) {
30      tr.ui.tracks.ContainerTrack.prototype.decorate.call(this, viewport);
31      this.classList.add('cpu-track');
32      this.detailedMode_ = true;
33    },
34
35    get cpu() {
36      return this.cpu_;
37    },
38
39    set cpu(cpu) {
40      this.cpu_ = cpu;
41      this.updateContents_();
42    },
43
44    get detailedMode() {
45      return this.detailedMode_;
46    },
47
48    set detailedMode(detailedMode) {
49      this.detailedMode_ = detailedMode;
50      this.updateContents_();
51    },
52
53    get tooltip() {
54      return this.tooltip_;
55    },
56
57    set tooltip(value) {
58      this.tooltip_ = value;
59      this.updateContents_();
60    },
61
62    get hasVisibleContent() {
63      if (this.cpu_ === undefined)
64        return false;
65      var cpu = this.cpu_;
66      if (cpu.slices.length)
67        return true;
68      if (cpu.samples && cpu.samples.length)
69        return true;
70      if (tr.b.dictionaryLength(cpu.counters) > 0)
71        return true;
72      return false;
73    },
74
75    updateContents_: function() {
76      this.detach();
77      if (!this.cpu_)
78        return;
79      var slices = this.cpu_.slices;
80      if (slices.length) {
81        var track = new tr.ui.tracks.SliceTrack(this.viewport);
82        track.slices = slices;
83        track.heading = this.cpu_.userFriendlyName + ':';
84        this.appendChild(track);
85      }
86
87      if (this.detailedMode_) {
88        this.appendSamplesTracks_();
89
90        for (var counterName in this.cpu_.counters) {
91          var counter = this.cpu_.counters[counterName];
92          track = new tr.ui.tracks.CounterTrack(this.viewport);
93          track.heading = this.cpu_.userFriendlyName + ' ' +
94              counter.name + ':';
95          track.counter = counter;
96          this.appendChild(track);
97        }
98      }
99    },
100
101    appendSamplesTracks_: function() {
102      var samples = this.cpu_.samples;
103      if (samples === undefined || samples.length === 0)
104        return;
105      var samplesByTitle = {};
106      samples.forEach(function(sample) {
107        if (samplesByTitle[sample.title] === undefined)
108          samplesByTitle[sample.title] = [];
109        samplesByTitle[sample.title].push(sample);
110      });
111
112      var sampleTitles = tr.b.dictionaryKeys(samplesByTitle);
113      sampleTitles.sort();
114
115      sampleTitles.forEach(function(sampleTitle) {
116        var samples = samplesByTitle[sampleTitle];
117        var samplesTrack = new tr.ui.tracks.SliceTrack(this.viewport);
118        samplesTrack.group = this.cpu_;
119        samplesTrack.slices = samples;
120        samplesTrack.heading = this.cpu_.userFriendlyName + ': ' +
121            sampleTitle;
122        samplesTrack.tooltip = this.cpu_.userFriendlyDetails;
123        samplesTrack.selectionGenerator = function() {
124          var selection = new tr.model.EventSet();
125          for (var i = 0; i < samplesTrack.slices.length; i++) {
126            selection.push(samplesTrack.slices[i]);
127          }
128          return selection;
129        };
130        this.appendChild(samplesTrack);
131      }, this);
132    }
133  };
134
135  return {
136    CpuTrack: CpuTrack
137  };
138});
139</script>
140