Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / tracing / analysis / analyze_slices.html
1 <!DOCTYPE html>
2 <!--
3 Copyright (c) 2013 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
6 -->
7
8 <link rel="import" href="/tracing/analysis/analysis_results.html">
9 <link rel="import" href="/tracing/analysis/analysis_sub_view.html">
10 <link rel="import" href="/tracing/analysis/util.html">
11 <link rel="import" href="/base.html">
12 <link rel="import" href="/base/ui.html">
13 <link rel="import" href="/base/ui/sortable_table.html">
14
15 <polymer-element name="single-slice-analysis-sub-view"
16     extends="tracing-analysis-sub-view"
17     constructor="SingleSliceAnalysisSubView">
18   <script>
19   'use strict';
20
21   tv.onPolymerReady(function() {
22     tracing.registerAnalysisSubViewType(
23         0,
24         function supported(selection) {
25           if (selection.length != 1)
26             return false;
27           return selection[0] instanceof tracing.trace_model.Slice ||
28               selection[0] instanceof tracing.trace_model.FlowEvent ||
29               selection[0] instanceof tracing.trace_model.InstantEvent;
30         },
31         function constructView(selection) {
32           var event = selection[0];
33           var typeName = event.analysisTypeName;
34           var customViewInfo = tracing.analysis.SliceView.getViewInfo(typeName);
35           if (customViewInfo)
36             return new customViewInfo.constructor();
37           return new SingleSliceAnalysisSubView();
38         },
39         {
40           passSelectionToConstructor: true
41         });
42     tracing.registerAnalysisSubViewType(
43         0,
44         function(selection) {
45           if (selection.length <= 1)
46             return false;
47           return selection[0] instanceof tracing.trace_model.Slice ||
48               selection[0] instanceof tracing.trace_model.FlowEvent ||
49               selection[0] instanceof tracing.trace_model.InstantEvent;
50         },
51         MultipleSlicesAnalysisSubView);
52   });
53
54   (function() {
55     var TYPE_TO_NAME_LIST = [
56       {
57         type: tracing.trace_model.Slice,
58         name: 'Slice'
59       },
60       {
61         type: tracing.trace_model.FlowEvent,
62         name: 'Flow Event'
63       },
64       {
65         type: tracing.trace_model.InstantEvent,
66         name: 'Instant Event'
67       },
68     ];
69
70     Polymer({
71       created: function() {
72         this.currentSelection_ = undefined;
73       },
74
75       set selection(selection) {
76         this.currentSelection_ = selection;
77         this.textContent = '';
78
79         if (selection.length === 0)
80           return;
81
82         var typeName = '';
83         for (var i = 0; i < TYPE_TO_NAME_LIST.length; i++) {
84           var item = TYPE_TO_NAME_LIST[i];
85           if (selection[0] instanceof item.type) {
86             typeName = item.name;
87             break;
88           }
89         }
90
91         var results = new tracing.analysis.AnalysisResults();
92         this.appendChild(results);
93
94         this.analyzeSingleSlice_(results, selection[0], typeName);
95       },
96
97       get selection() {
98         return this.currentSelection_;
99       },
100
101       analyzeSingleSlice_: function(results, slice, type) {
102         results.appendHeader('Selected ' + type + ':');
103         var table = results.appendTable('analysis-slice-table', 2);
104
105         if (slice.error)
106           results.appendInfoRow(table, 'Error', slice.error);
107
108         if (slice.title)
109           results.appendInfoRow(table, 'Title', slice.title);
110
111         if (slice.category)
112           results.appendInfoRow(table, 'Category', slice.category);
113
114         results.appendInfoRowTime(table, 'Start', slice.start);
115         results.appendInfoRowTime(table, 'Wall Duration', slice.duration);
116
117         if (slice.cpuDuration)
118           results.appendInfoRowTime(table, 'CPU Duration', slice.cpuDuration);
119
120         if (slice.selfTime)
121           results.appendInfoRowTime(table, 'Self Time', slice.selfTime);
122
123         if (slice.cpuSelfTime) {
124           var warning;
125           if (slice.cpuSelfTime > slice.selfTime) {
126             warning =
127                 'Note that CPU Self Time is larger than Self Time. ' +
128                 'This is a known limitation of this system, which occurs ' +
129                 'due to several subslices, rounding issues, and inprecise ' +
130                 'time at which we get cpu- and real-time.';
131           }
132           results.appendInfoRowTime(table, 'CPU Self Time', slice.cpuSelfTime,
133                                     false, warning);
134         }
135
136         if (slice.durationInUserTime) {
137           results.appendInfoRowTime(table, 'Duration (U)',
138                                     slice.durationInUserTime);
139         }
140
141         var n = 0;
142         for (var argName in slice.args) {
143           n += 1;
144         }
145         if (n > 0) {
146           results.appendInfoRow(table, 'Args');
147           for (var argName in slice.args) {
148             var argVal = slice.args[argName];
149             // TODO(sleffler) use span instead?
150             results.appendInfoRow(table, ' ' + argName, argVal);
151           }
152         }
153       }
154     });
155   })();
156   </script>
157 </polymer-element>
158
159 <polymer-element name="multiple-slices-analysis-sub-view"
160     extends="tracing-analysis-sub-view"
161     constructor="MultipleSlicesAnalysisSubView">
162   <script>
163   'use strict';
164   (function() {
165     var TYPE_TO_NAME_LIST = [
166       {
167         type: tracing.trace_model.Slice,
168         name: 'Slices'
169       },
170       {
171         type: tracing.trace_model.FlowEvent,
172         name: 'Flow Events'
173       },
174       {
175         type: tracing.trace_model.InstantEvent,
176         name: 'Instant Events'
177       },
178     ];
179
180     Polymer({
181       created: function() {
182         this.currentSelection_ = undefined;
183       },
184
185       set selection(selection) {
186         this.currentSelection_ = selection;
187         this.textContent = '';
188
189         if (selection.length === 0)
190           return;
191
192         var typeName = '';
193         for (var i = 0; i < TYPE_TO_NAME_LIST.length; i++) {
194           var item = TYPE_TO_NAME_LIST[i];
195           if (selection[0] instanceof item.type) {
196             typeName = item.name;
197             break;
198           }
199         }
200
201         var results = new tracing.analysis.AnalysisResults();
202         this.appendChild(results);
203
204         this.analyzeMultipleSlices_(results, selection, typeName);
205       },
206
207       get selection() {
208         return this.currentSelection_;
209       },
210
211       analyzeSingleTypeSlices_: function(results, sliceGroup, hasCpuDuration) {
212         results.appendInfo('Title: ', sliceGroup[0].title);
213         results.appendInfo('Category: ', sliceGroup[0].category);
214
215         var table = results.appendTable('analysis-slice-table',
216                                         4 + hasCpuDuration);
217         var row = results.appendHeadRow(table);
218         results.appendTableCell(table, row, 'Start');
219         results.appendTableCell(table, row, 'Wall Duration (ms)');
220         if (hasCpuDuration)
221           results.appendTableCell(table, row, 'CPU Duration (ms)');
222         results.appendTableCell(table, row, 'Self Time (ms)');
223         results.appendTableCell(table, row, 'Args');
224
225         var numSlices = 0;
226         tv.iterItems(sliceGroup, function(title, slice) {
227           numSlices++;
228           results.appendDetailsRow(table, slice.start, slice.duration,
229               slice.selfTime ? slice.selfTime : slice.duration, slice.args,
230               function() {
231                 return new tracing.Selection([slice]);
232               }, slice.cpuDuration);
233         });
234         if (numSlices > 1)
235           tv.ui.SortableTable.decorate(table);
236       },
237
238       analyzeMultipleSlices_: function(results, slices, type) {
239         var tsLo = slices.bounds.min;
240         var tsHi = slices.bounds.max;
241
242         var numTitles = 0;
243         var sliceGroups = {};
244         var hasCpuDuration = false;
245
246         for (var i = 0; i < slices.length; i++) {
247           var slice = slices[i];
248           if (sliceGroups[slice.title] === undefined) {
249             sliceGroups[slice.title] = [];
250             numTitles++;
251           }
252
253           if (slice.cpuDuration)
254             hasCpuDuration = true;
255
256           var sliceGroup = sliceGroups[slice.title];
257           sliceGroup.push(slices[i]);
258         }
259
260         results.appendHeader(type + ':');
261         var table = results.appendTable('analysis-slice-table',
262                                         4 + hasCpuDuration);
263         var row = results.appendHeadRow(table);
264         results.appendTableCell(table, row, 'Name');
265         results.appendTableCell(table, row, 'Wall Duration (ms)');
266         if (hasCpuDuration)
267           results.appendTableCell(table, row, 'CPU Duration (ms)');
268         results.appendTableCell(table, row, 'Self Time (ms)');
269         if (hasCpuDuration)
270           results.appendTableCell(table, row, 'CPU Self Time (ms)');
271         results.appendTableCell(table, row, 'Occurrences');
272
273         var thisComponent = this;
274         var totalDuration = 0;
275         var totalCpuDuration = 0;
276         var totalSelfTime = 0;
277         var totalCpuSelfTime = 0;
278         tv.iterItems(sliceGroups, function(sliceGroupTitle, sliceGroup) {
279           var duration = 0;
280           var cpuDuration = 0;
281           var selfTime = 0;
282           var cpuSelfTime = 0;
283           var avg = 0;
284           var startOfFirstOccurrence = Number.MAX_VALUE;
285           var startOfLastOccurrence = -Number.MAX_VALUE;
286           var min = Number.MAX_VALUE;
287           var max = -Number.MAX_VALUE;
288           for (var i = 0; i < sliceGroup.length; i++) {
289             var slice = sliceGroup[i];
290             duration += slice.duration;
291             if (slice.cpuDuration) {
292               cpuDuration += slice.cpuDuration;
293               cpuSelfTime += slice.cpuSelfTime ? slice.cpuSelfTime :
294                                                  slice.cpuDuration;
295             }
296             selfTime += slice.selfTime ? slice.selfTime : slice.duration;
297             startOfFirstOccurrence = Math.min(slice.start, startOfFirstOccurrence);
298             startOfLastOccurrence = Math.max(slice.start, startOfLastOccurrence);
299             min = Math.min(slice.duration, min);
300             max = Math.max(slice.duration, max);
301           }
302
303           totalDuration += duration;
304           totalCpuDuration += cpuDuration;
305           totalSelfTime += selfTime;
306           totalCpuSelfTime += cpuSelfTime;
307
308           if (sliceGroup.length == 0)
309             avg = 0;
310           avg = duration / sliceGroup.length;
311
312           var statistics = {
313             min: min,
314             max: max,
315             avg: avg,
316             avg_stddev: undefined,
317             frequency: undefined,
318             frequency_stddev: undefined
319           };
320
321           // Compute the stddev of the slice durations.
322           var sumOfSquaredDistancesToMean = 0;
323           for (var i = 0; i < sliceGroup.length; i++) {
324             var signedDistance = statistics.avg - sliceGroup[i].duration;
325             sumOfSquaredDistancesToMean += signedDistance * signedDistance;
326           }
327
328           statistics.avg_stddev =
329               Math.sqrt(sumOfSquaredDistancesToMean / (sliceGroup.length - 1));
330
331           // We require at least 3 samples to compute the stddev.
332           var elapsed = startOfLastOccurrence - startOfFirstOccurrence;
333           if (sliceGroup.length > 2 && elapsed > 0) {
334             var numDistances = sliceGroup.length - 1;
335             statistics.frequency = (1000 * numDistances) / elapsed;
336
337             // Compute the stddev.
338             sumOfSquaredDistancesToMean = 0;
339             for (var i = 1; i < sliceGroup.length; i++) {
340               var currentFrequency =
341                   1000 / (sliceGroup[i].start - sliceGroup[i - 1].start);
342               var signedDistance = statistics.frequency - currentFrequency;
343               sumOfSquaredDistancesToMean += signedDistance * signedDistance;
344             }
345
346             statistics.frequency_stddev =
347                 Math.sqrt(sumOfSquaredDistancesToMean / (numDistances - 1));
348           }
349           results.appendDataRow(table, sliceGroupTitle, duration,
350                                 hasCpuDuration ? (cpuDuration > 0 ?
351                                     cpuDuration : '') : null,
352                                 selfTime,
353                                 hasCpuDuration ? (cpuSelfTime > 0 ?
354                                     cpuSelfTime : '') : null,
355                                 sliceGroup.length, null, statistics, function() {
356                                   return new tracing.Selection(sliceGroup);
357                                 });
358
359           // The whole selection is a single type so list out the information
360           // for each sub slice.
361           if (numTitles === 1)
362             thisComponent.analyzeSingleTypeSlices_(results, sliceGroup,
363                                                    hasCpuDuration);
364         });
365
366         // Only one row so we already know the totals.
367         if (numTitles !== 1) {
368           results.appendDataRow(table, 'Totals', totalDuration,
369                                 hasCpuDuration ? totalCpuDuration : null,
370                                 totalSelfTime,
371                                 hasCpuDuration ? totalCpuSelfTime : null,
372                                 slices.length,
373                                 null, null, null, true);
374           results.appendSpacingRow(table, true);
375           tv.ui.SortableTable.decorate(table);
376         }
377
378         results.appendInfoRowTime(table, 'Selection start', tsLo, true);
379         results.appendInfoRowTime(table, 'Selection extent', tsHi - tsLo, true);
380       }
381     });
382   })();
383   </script>
384 </polymer-element>