forked from mozilla/cleopatra
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathwaterfall.js
516 lines (462 loc) · 17.2 KB
/
waterfall.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// Test profile: e74815d8695ccf8580d4af3be5cd1371f202f6ae
// 1305aa31f417005934020cd7181d8331691945d1
function createElement(name, props) {
var el = document.createElement(name);
for (var key in props) {
if (key === "style") {
for (var styleName in props.style) {
el.style[styleName] = props.style[styleName];
}
} else {
el[key] = props[key];
}
}
return el;
}
var gLayersDumps = [];
var Waterfall = function() {
this.container = createElement("div", {
className: "histogram waterfallContainer",
});
this.canvas = createElement("canvas", {
className: "waterfallCanvas",
style: {
overflow: "hidden",
},
});
this.busyCover = createElement("div", { className: "busyCover" });
this.busyCover.classList.add("busy");
this.container.appendChild(this.canvas);
this.container.appendChild(this.busyCover);
var timeout;
var throttler = function () {
if (timeout)
return;
timeout = setTimeout(function () {
timeout = null;
this.scheduleRender();
}.bind(this), 200);
}.bind(this);
window.addEventListener("resize", throttler, false);
}
Waterfall.createFrameUniformityView = function(compositeTimes) {
function findByAddress(root, address) {
if (root.address == address) {
return root;
}
for (var i = 0; i < root.children.length; i++) {
var find = findByAddress(root.children[i], address);
if (find) {
return find;
}
}
return null;
}
function compareLayers(prevLayerTree, root, graph, time) {
if (root.address && root['screen-transform']) {
var prevInstance = findByAddress(prevLayerTree, root.address);
if (prevInstance && prevInstance['screen-transform']) {
if (root['screen-transform'][0] != prevInstance['screen-transform'][0]) {
graph[root.address] = graph[root.address] || {};
graph[root.address]['transformX'] = graph[root.address]['transformX'] || [{ // Original value
time: prevLayerTree.compositeTime,
value: prevInstance['screen-transform'][0],
}];
graph[root.address]['transformX'].push({
time: time,
value: root['screen-transform'][0],
});
}
if (root['screen-transform'][1] != prevInstance['screen-transform'][1]) {
graph[root.address] = graph[root.address] || {};
graph[root.address]['transformY'] = graph[root.address]['transformY'] || [{ // Original value
time: prevLayerTree.compositeTime,
value: prevInstance['screen-transform'][1],
}];
graph[root.address]['transformY'].push({
time: time,
value: root['screen-transform'][1],
});
dump(root['screen-transform'][1] + "\n");
}
}
}
for (var i = 0; i < root.children.length; i++) {
compareLayers(prevLayerTree, root.children[i], graph, time);
}
return graph;
}
function computeLayerUniformity(layersDumps) {
if (layersDumps == null || layersDumps.length < 2) {
return null;
}
var prevLayersDump = parseLayers(layersDumps[0]);
var graph = {};
for (var i = 1; i < layersDumps.length; i++) {
var currLayersDump = parseLayers(layersDumps[i]);
compareLayers(prevLayersDump, currLayersDump, graph, currLayersDump.compositeTime);
prevLayersDump = currLayersDump;
}
return graph;
}
var layerUniformityGraphs = computeLayerUniformity(gLayersDumps);
var container = createElement("div", {
className: "frameUniformityContainer",
style: {
background: "white",
height: "100%",
}
});
var graph = createElement("div", {
id: "frameUniformityGraph",
className: "frameGraph",
style: {
width: "600px",
height: "400px",
padding: "5px",
}
});
var caption = createElement("span", {
textContent: "Each point represents the time between composites. " +
"During active refresh all points should be near 16ms. " +
"\n" +
"\n" +
"Warning: Composite don't occur when idle and will cause " +
"spikes in the graph.",
style: {
whiteSpace: "pre",
},
});
var data = ["Time between composites"];
for (var i = 1; i < compositeTimes.length; i++) {
data.push((compositeTimes[i] - compositeTimes[i-1]).toFixed(2));
}
document.body.appendChild(graph);
var chart = c3.generate({
bindto: '#frameUniformityGraph',
data: {
columns: [
data,
]
}
});
document.body.removeChild(graph);
graph.id = "";
container.appendChild(graph);
container.appendChild(caption);
var layerUniformityHeader = createElement("h2", {
textContent: "Layer Animation Uniformity"
});
container.appendChild(layerUniformityHeader);
var layerUniformityDesc = createElement("span", {
textContent: "The following graph track the movement of layers. " +
"Curves should be smooth during most animation like deceleration.",
});
container.appendChild(layerUniformityDesc);
layerUniformityGraphs = layerUniformityGraphs || {};
for (var address in layerUniformityGraphs) {
var layerUniformityGraph = layerUniformityGraphs[address];
if (layerUniformityGraph.transformY && layerUniformityGraph.transformY.length > 2) {
var time = ['Time'];
var data = ["Layer " + address + " transformY"];
for (var i = 0; i < layerUniformityGraph.transformY.length; i++) {
var obj = layerUniformityGraph.transformY[i];
time.push(obj.time.toFixed(2));
data.push(obj.value);
}
var graph = createElement("div", {
id: "frameUniformityGraph",
className: "frameGraph",
style: {
width: "600px",
height: "400px",
padding: "5px",
}
});
document.body.appendChild(graph);
var chart = c3.generate({
bindto: '#frameUniformityGraph',
data: {
x: 'Time',
columns: [
time,
data,
]
}
});
document.body.removeChild(graph);
graph.id = "";
container.appendChild(graph);
}
}
return container;
};
// Frame Positions is in {layerLabel : [[x0, y0], [x1, y1]] } format
Waterfall.createFramePositionView = function(framePositions) {
// Returns [ [layer1X, x0, x1..], [layer1Y, y0, y1..] ]
function formatForChart() {
var result = [];
for (layer in framePositions) {
var layerData = framePositions[layer];
var layerX = [layer + ".x"];
var layerY = [layer + ".y"];
for (var i = 0; i < layerData.length; i++) {
layerX.push(layerData[i][0]);
layerY.push(layerData[i][1]);
}
result.push(layerX);
result.push(layerY);
}
return result;
}
function createGraph() {
var graph = createElement("div", {
id: "positionUniformityGraph",
className: "frameGraph",
style: {
width: "1400px",
height: "800px",
padding: "5px",
}
});
var layerData = formatForChart();
document.body.appendChild(graph);
var chart = c3.generate({
bindto: '#positionUniformityGraph',
data: {
columns: layerData
}
});
document.body.removeChild(graph);
// Have to reset graph.id to something else so when we repaint
// we can override this graph
graph.id = "";
container.appendChild(graph);
}
function createFrameUniformityUsage(container) {
var text = "No frame position uniformity data. You can enable this view by " +
"setting the preference layers.uniformity-info to true and by " +
"profiling the compositor thread. To profile the compositor thread, " +
"Use the command './profile.sh start -p b2g -t Compositor'. To get touch data, " +
"also profile the 'GeckoMain' thread. e.g. './profile.sh start -p b2g -t Compositor,GeckoMain'";
container.innerHTML = text;
}
var container = createElement("div", {
className: "frameUniformityContainer",
id: "framePositionContainer",
style: {
background: "white",
height: "100%",
}
});
var markerCount = Object.keys(framePositions).length;
if (markerCount != 0) {
createGraph()
} else {
createFrameUniformityUsage(container);
}
return container;
};
Waterfall.prototype = {
getContainer: function Waterfall_getContainer() {
return this.container;
},
scheduleRender: function () {
},
getWaterfallThreadId: function() {
if (this.threadSelect.value == "") {
return null;
}
return this.threadSelect.value;
},
setThreadSelect: function(threadSelect) {
this.threadSelect = threadSelect;
},
dataIsOutdated: function() {
this.busyCover.classList.add("busy");
},
formatStack: function(stack) {
var str = "";
for (var i = stack.length - 1; i >= 0; i--) {
var frame = stack[i];
str += frame + "\n";
}
return str;
},
formatDisplayListDump: function(displayListDump) {
var str = "";
for (var i = 0; i < displayListDump.length; i++) {
var line = displayListDump[i];
str += line.name + "\n";
}
return str;
},
formatLayersDump: function(layersDump) {
var str = "";
for (var i = 0; i < layersDump.length; i++) {
var line = layersDump[i];
str += line.name + "\n";
}
return str;
},
display: function Waterfall_display(data) {
// we assume the data of each type is in order and non-overlapping
this.busyCover.classList.remove("busy");
var i, item;
this.container.innerHTML = "";
var self = this;
// On a 1080p monitor this is about 1px. We want to merge things that are invisible so we don't want to create separate element for every item if they are too small and too close.
var maxCloseness = 0.1;
var maxWidth = 0.1;
var typeOrder = ['RD', 'Scripts', 'Styles', 'Reflow', 'DisplayList', 'Rasterize', 'Composite', 'Vsync', 'LayerTransaction', "ContentGPU", "CompositorGPU", 'Other'];
var cssClasses = ['waterfallFrame', 'waterfallItem', 'waterfallItem', 'waterfallItem', 'waterfallItem', 'waterfallItem', 'waterfallItem', 'waterfallItem', 'waterfallItem', 'waterfallItem', 'waterfallItem', 'watefallItem'];
var colorList = ['rgba(0,200,0,0.5)', 'rgb(250,100,40)', 'rgb(40,40,100)', 'rgb(40,40,100)', 'rgb(150,40,100)', 'rgb(100,250,40)', 'rgb(100,40,250)', 'rgb(255, 128, 0', 'rgb(250, 0, 33)', 'rgba(0,200,0,0.5)', 'rgba(0,200,0,0.5)', 'rgb(200,0,0)'];
var barHeight = [0.5, 0, 1, 1, 1, 2, 3, 0, 3, 4, 5, 0];
gLayersDumps = [];
var filtered = {};
for (i = 0; i < typeOrder.length; i++) {
filtered[typeOrder[i]] = [];
}
this.threadSelect.innerHTML = "";
if (data.threadsToView.length > 1) {
this.threadSelect.style.display = "";
for (i = 0; i < data.threadsToView.length; i++) {
var option = createElement("option", {
text: data.threadsToView[i].name,
value: data.threadsToView[i].threadId,
});
this.threadSelect.appendChild(option);
}
this.threadSelect.value = data.selectedThreadId;
this.threadSelect.onchange = function() {
window.AppUI.filtersChanged();
}
} else {
this.threadSelect.style.display = "none";
}
// separate the data.items input into categories by type of marker and filter out any outside the view
for (i = 0; i < data.items.length; i++) {
item = data.items[i];
if (item.startTime > data.boundaries.min && item.startTime < data.boundaries.max ||
item.endTime > data.boundaries.min && item.endTime < data.boundaries.max) {
// if the item is in the list, put it in the corresponding category, otherwise in the "Other"
if (~typeOrder.indexOf(item.type)) {
filtered[item.type].push(item);
} else {
filtered['Other'].push(item);
}
}
}
function makeWaterfallBar(cssClass, text, title, startX, startY, width, color) {
return createElement("div", {
className: cssClass,
innerHTML: "<center>" + text + "</center>", //TODO XSS filter
title: title,
style: {
left: startX + "%",
top: startY + "px",
width: width + "%",
background: color,
},
});
}
// this state machine combines contiguous blocks of elements with width less than maxWidth % and
// distance between them of less than maxCloseness
function appendFilteredMarkers(container, markers, cssClass, startY, maxCloseness, maxWidth, color) {
var i, item;
var duration = data.boundaries.max - data.boundaries.min;
var mergeLength = 0, mergeStartTime, mergeEndTime, mergeSumOfdurations;
var prevText, prevItemTitle, prevStartX, prevWidth;
var startX, width, itemTitle, text;
// if there is one element in the merge, display that element, otherwise combine all elements inside
function endMerge() {
// if there's only one item merged, display it as if it wasn't merged
if (mergeLength == 1) {
container.appendChild(makeWaterfallBar(cssClass, prevText, prevItemTitle, prevStartX, startY, prevWidth, color));
} else {
// draw the merged bar
container.appendChild(makeWaterfallBar(cssClass, " ", text + " x" + mergeLength + " over " + mergeSumOfdurations.toFixed(2) + " ms", mergeStartTime, startY, mergeEndTime - mergeStartTime, "#000"));
}
// mark the merge as processed and reset its duration
mergeLength = 0;
mergeSumOfdurations = 0;
}
// go through each marker and either create a bar for it or combine it with subsequent markers into a merged bar
for (i = 0; i < markers.length; i++) {
item = markers[i];
// calculate the positions on the canvas
startX = (item.startTime - data.boundaries.min) * 100 / duration;
width = (item.endTime - data.boundaries.min) * 100 / duration - startX;
// set the marker's text and title
itemTitle = item.text + " " + (item.endTime - item.startTime).toFixed(2) + " ms";
text = item.text;
if (item.causeStack) {
itemTitle += "\n" + self.formatStack(item.causeStack);
}
if (item.layersDump) {
item.layersDump.compositeTime = item.endTime;
gLayersDumps.push(item.layersDump);
if (!this.hasSeenLayersDump) {
this.hasSeenLayersDump = true;
tab_showInstruction("LayerTree", "To view a layers dump you must click on a 'Composite' bubble in the Frames timeline.");
}
//itemTitle += "\n" + self.formatLayersDump(item.layersDump);
}
if (item.displayListDump) {
if (!this.hasSeenDisplayListDump) {
this.hasSeenDisplayListDump = true;
tab_showInstruction("DisplayList", "To view a Display List dump you must click on a 'DisplayList' bubble in the Frames timeline.");
}
//itemTitle += "\n" + self.formatDisplayListDump(item.displayListDump);
}
// if there was a merge happening and we are too far or too wide to join, end it
if (mergeLength > 0 && (mergeEndTime + maxCloseness < startX || width > maxWidth)) {
endMerge();
}
// if this element is big enough to be visible on its own we just draw it
if (width > maxWidth) {
// render the current element because it can stand on its own
var loneElement = makeWaterfallBar(cssClass, text, itemTitle, startX, startY, width, color);
if (item.layersDump) {
(function (layersDump, text, startTime) {
loneElement.onclick = function() {
tab_showLayersDump(layersDump, text, startTime);
}
})(item.layersDump, text, item.startTime);
}
if (item.displayListDump) {
(function (displayListDump, text, startTime) {
loneElement.onclick = function() {
tab_showDisplayListDump(displayListDump, text, startTime);
}
})(item.displayListDump, text, item.startTime);
}
container.appendChild(loneElement);
} else {
// since our bar is too small we create or join a merge
if (mergeLength == 0) {
mergeStartTime = startX;
}
mergeLength++;
mergeSumOfdurations += item.endTime - item.startTime;
mergeEndTime = startX + width;
}
// we keep track of the previous item because in the case of one item being a part of a merge, we might want to cancel the merge and display the item instead
prevText = text; prevItemTitle = itemTitle; prevStartX = startX; prevWidth = width;
}
// if there's an unclosed merge at the end close it
if (mergeLength > 0) {
endMerge();
}
}
var startY = 0;
// go over every type of item and display each type on its own row with its own color
for (i = 0; i < typeOrder.length; i++) {
var type = typeOrder[i];
if (filtered[type]) {
//TODO: possible optimization: createFragment
appendFilteredMarkers(this.container, filtered[type], cssClasses[i], barHeight[i]*15, maxCloseness, maxWidth, colorList[i]);
}
}
},
};