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
|
// Copyright (c) 2011 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.
/**
* @fileoverview State and UI for trace data collection.
*/
cr.define('tracing', function() {
function TracingController() {
this.overlay_ = document.createElement('div');
this.overlay_.className = 'tracing-overlay';
cr.ui.decorate(this.overlay_, tracing.Overlay);
this.statusDiv_ = document.createElement('div');
this.overlay_.appendChild(this.statusDiv_);
this.bufferPercentDiv_ = document.createElement('div');
this.overlay_.appendChild(this.bufferPercentDiv_);
this.stopButton_ = document.createElement('button');
this.stopButton_.onclick = this.endTracing.bind(this);
this.stopButton_.textContent = 'Stop tracing';
this.overlay_.appendChild(this.stopButton_);
this.traceEvents_ = [];
if (browserBridge.debugMode) {
var tracingControllerTests = document.createElement('script');
tracingControllerTests.src =
'./tracing/tracing_controller_tests.js';
document.body.appendChild(tracingControllerTests);
}
this.onKeydownBoundToThis_ = this.onKeydown_.bind(this);
this.onKeypressBoundToThis_ = this.onKeypress_.bind(this);
chrome.send('tracingControllerInitialized');
}
TracingController.prototype = {
__proto__: cr.EventTarget.prototype,
gpuInfo_: undefined,
clientInfo_: undefined,
tracingEnabled_: false,
tracingEnding_: false,
onRequestBufferPercentFullComplete: function(percent_full) {
if (!this.overlay_.visible)
return;
window.setTimeout(this.beginRequestBufferPercentFull_.bind(this), 250);
this.bufferPercentDiv_.textContent = 'Buffer usage: ' +
Math.round(100 * percent_full) + '%';
},
/**
* Begin requesting the buffer fullness
*/
beginRequestBufferPercentFull_: function() {
chrome.send('beginRequestBufferPercentFull');
},
/**
* Called by info_view to empty the trace buffer
*/
beginTracing: function() {
if (this.tracingEnabled_)
throw Error('Tracing already begun.');
this.stopButton_.hidden = false;
this.statusDiv_.textContent = 'Tracing active.';
this.overlay_.visible = true;
this.tracingEnabled_ = true;
console.log('Beginning to trace...');
this.statusDiv_.textContent = 'Tracing active.';
this.traceEvents_ = [];
if (!browserBridge.debugMode) {
chrome.send('beginTracing');
this.beginRequestBufferPercentFull_();
} else {
tracing.tracingControllerTestHarness.beginTracing();
}
this.tracingEnabled_ = true;
var e = new cr.Event('traceBegun');
e.events = this.traceEvents_;
this.dispatchEvent(e);
e = new cr.Event('traceEventsChanged');
e.numEvents = this.traceEvents_.length;
this.dispatchEvent(e);
window.addEventListener('keypress', this.onKeypressBoundToThis_);
window.addEventListener('keydown', this.onKeydownBoundToThis_);
},
onKeydown_: function(e) {
if (e.keyCode == 27) {
this.endTracing();
}
},
onKeypress_: function(e) {
if (e.keyIdentifier == 'Enter') {
this.endTracing();
}
},
/**
* Called from gpu c++ code when ClientInfo is updated.
*/
onClientInfoUpdate: function(clientInfo) {
this.clientInfo_ = clientInfo;
},
/**
* Called from gpu c++ code when GPU Info is updated.
*/
onGpuInfoUpdate: function(gpuInfo) {
this.gpuInfo_ = gpuInfo;
},
/**
* Checks whether tracing is enabled
*/
get isTracingEnabled() {
return this.tracingEnabled_;
},
/**
* Gets the currently traced events. If tracing is active, then
* this can change on the fly.
*/
get traceEvents() {
return this.traceEvents_;
},
/**
* Called by tracing c++ code when new trace data arrives.
*/
onTraceDataCollected: function(events) {
this.statusDiv_.textContent = 'Processing trace...';
this.traceEvents_.push.apply(this.traceEvents_, events);
},
/**
* Called by info_view to finish tracing and update all views.
*/
endTracing: function() {
if (!this.tracingEnabled_) throw new Error('Tracing not begun.');
if (this.tracingEnding_) return;
this.tracingEnding_ = true;
this.statusDiv_.textContent = 'Ending trace...';
console.log('Finishing trace');
this.statusDiv_.textContent = 'Downloading trace data...';
this.stopButton_.hidden = true;
// delay sending endTracingAsync until we get a chance to
// update the screen...
window.setTimeout(function() {
if (!browserBridge.debugMode) {
chrome.send('endTracingAsync');
} else {
tracing.tracingControllerTestHarness.endTracing();
}
}, 100);
},
/**
* Called by the browser when all processes complete tracing.
*/
onEndTracingComplete: function() {
window.removeEventListener('keydown', this.onKeydownBoundToThis_);
window.removeEventListener('keypress', this.onKeypressBoundToThis_);
this.overlay_.visible = false;
this.tracingEnabled_ = false;
this.tracingEnding_ = false;
console.log('onEndTracingComplete p1 with ' +
this.traceEvents_.length + ' events.');
var e = new cr.Event('traceEnded');
e.events = this.traceEvents_;
this.dispatchEvent(e);
},
/**
* Tells browser to put up a load dialog and load the trace file
*/
beginLoadTraceFile: function() {
chrome.send('loadTraceFile');
},
/**
* Called by the browser when a trace file is loaded.
*/
onLoadTraceFileComplete: function(data) {
if (data.traceEvents) {
this.traceEvents_ = data.traceEvents;
} else { // path for loading traces saved without metadata
if (!data.length)
console.log('Expected an array when loading the trace file');
else
this.traceEvents_ = data;
}
var e = new cr.Event('loadTraceFileComplete');
e.events = this.traceEvents_;
this.dispatchEvent(e);
},
/**
* Called by the browser when loading a trace file was canceled.
*/
onLoadTraceFileCanceled: function() {
cr.dispatchSimpleEvent(this, 'loadTraceFileCanceled');
},
/**
* Tells browser to put up a save dialog and save the trace file
*/
beginSaveTraceFile: function(traceEvents) {
var data = {
traceEvents: traceEvents,
clientInfo: this.clientInfo_,
gpuInfo: this.gpuInfo_
};
chrome.send('saveTraceFile', [JSON.stringify(data)]);
},
/**
* Called by the browser when a trace file is saveed.
*/
onSaveTraceFileComplete: function() {
cr.dispatchSimpleEvent(this, 'saveTraceFileComplete');
},
/**
* Called by the browser when saving a trace file was canceled.
*/
onSaveTraceFileCanceled: function() {
cr.dispatchSimpleEvent(this, 'saveTraceFileCanceled');
},
selfTest: function() {
this.beginTracing();
window.setTimeout(this.endTracing.bind(This), 500);
}
};
return {
TracingController: TracingController
};
});
|